]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ofp-util.c
ofp-util: Add flow metadata to ofputil_packet_out
[mirror_ovs.git] / lib / ofp-util.c
CommitLineData
fa37b408 1/*
04f48a68 2 * Copyright (c) 2008-2017 Nicira, Inc.
fa37b408
BP
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
03e1125c 18#include <ctype.h>
dc4762ed 19#include <errno.h>
fa37b408 20#include <inttypes.h>
6ca00f6f
ETN
21#include <sys/types.h>
22#include <netinet/in.h>
b459a924 23#include <netinet/icmp6.h>
fa37b408 24#include <stdlib.h>
25d436fb 25#include "bitmap.h"
daff3353 26#include "bundle.h"
10a24935 27#include "byte-order.h"
d8ae4d67 28#include "classifier.h"
75a75043 29#include "learn.h"
9e1fd49b 30#include "multipath.h"
6c038611 31#include "netdev.h"
b6c9e612 32#include "nx-match.h"
18ac06d3 33#include "id-pool.h"
d271907f
BW
34#include "openflow/netronome-ext.h"
35#include "openvswitch/dynamic-string.h"
36#include "openvswitch/meta-flow.h"
b598f214 37#include "openvswitch/ofp-actions.h"
d271907f
BW
38#include "openvswitch/ofp-errors.h"
39#include "openvswitch/ofp-msgs.h"
25d436fb 40#include "openvswitch/ofp-print.h"
66bd43fa 41#include "openvswitch/ofp-prop.h"
f4248336 42#include "openvswitch/ofp-util.h"
64c96779 43#include "openvswitch/ofpbuf.h"
d271907f
BW
44#include "openvswitch/type-props.h"
45#include "openvswitch/vlog.h"
d6e3feb5 46#include "openflow/intel-ext.h"
fa37b408
BP
47#include "packets.h"
48#include "random.h"
6159c531 49#include "tun-metadata.h"
4ffd1b43 50#include "unaligned.h"
ee89ea7b 51#include "util.h"
d5dc60f0 52#include "uuid.h"
fa37b408 53
d98e6007 54VLOG_DEFINE_THIS_MODULE(ofp_util);
fa37b408
BP
55
56/* Rate limit for OpenFlow message parse errors. These always indicate a bug
57 * in the peer and so there's not much point in showing a lot of them. */
58static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
59
de7d3c07
SJ
60static enum ofputil_table_vacancy ofputil_decode_table_vacancy(
61 ovs_be32 config, enum ofp_version);
82c22d34
BP
62static enum ofputil_table_eviction ofputil_decode_table_eviction(
63 ovs_be32 config, enum ofp_version);
64static ovs_be32 ofputil_encode_table_config(enum ofputil_table_miss,
65 enum ofputil_table_eviction,
de7d3c07 66 enum ofputil_table_vacancy,
82c22d34 67 enum ofp_version);
3c1bb396 68
0596e897
BP
69/* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
70 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
71 * is wildcarded.
72 *
73 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
74 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
75 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
76 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
77 * wildcarded. */
78ovs_be32
79ofputil_wcbits_to_netmask(int wcbits)
80{
81 wcbits &= 0x3f;
82 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
83}
84
85/* Given the IP netmask 'netmask', returns the number of bits of the IP address
c08201d6
BP
86 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
87 * between 0 and 32 inclusive.
88 *
89 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
90 * still be in the valid range but isn't otherwise meaningful. */
0596e897
BP
91int
92ofputil_netmask_to_wcbits(ovs_be32 netmask)
93{
aad29cd1 94 return 32 - ip_count_cidr_bits(netmask);
0596e897
BP
95}
96
eec25dc1 97/* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
81a76618 98 * flow_wildcards in 'wc' for use in struct match. It is the caller's
eec25dc1
BP
99 * responsibility to handle the special case where the flow match's dl_vlan is
100 * set to OFP_VLAN_NONE. */
7286b1e1 101void
eec25dc1 102ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
d8ae4d67 103{
2482b0b0 104 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 39);
a877206f 105
81a76618 106 /* Initialize most of wc. */
f9ba8dad 107 flow_wildcards_init_catchall(wc);
bad68a99 108
0bdc4bec 109 if (!(ofpfw & OFPFW10_IN_PORT)) {
4e022ec0 110 wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
27cafc5f 111 }
5d9499c4
BP
112
113 if (!(ofpfw & OFPFW10_NW_TOS)) {
26720e24 114 wc->masks.nw_tos |= IP_DSCP_MASK;
d8ae4d67 115 }
7257b535 116
851d3105 117 if (!(ofpfw & OFPFW10_NW_PROTO)) {
26720e24 118 wc->masks.nw_proto = UINT8_MAX;
851d3105 119 }
26720e24
BP
120 wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
121 >> OFPFW10_NW_SRC_SHIFT);
122 wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
123 >> OFPFW10_NW_DST_SHIFT);
d8ae4d67 124
eec25dc1 125 if (!(ofpfw & OFPFW10_TP_SRC)) {
b8266395 126 wc->masks.tp_src = OVS_BE16_MAX;
73f33563 127 }
eec25dc1 128 if (!(ofpfw & OFPFW10_TP_DST)) {
b8266395 129 wc->masks.tp_dst = OVS_BE16_MAX;
73f33563
BP
130 }
131
eec25dc1 132 if (!(ofpfw & OFPFW10_DL_SRC)) {
74ff3298 133 WC_MASK_FIELD(wc, dl_src);
73c0ce34 134 }
eec25dc1 135 if (!(ofpfw & OFPFW10_DL_DST)) {
74ff3298 136 WC_MASK_FIELD(wc, dl_dst);
d8ae4d67 137 }
e2170cff 138 if (!(ofpfw & OFPFW10_DL_TYPE)) {
b8266395 139 wc->masks.dl_type = OVS_BE16_MAX;
e2170cff 140 }
d8ae4d67 141
eb6f28db 142 /* VLAN TCI mask. */
eec25dc1 143 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
f0fb825a 144 wc->masks.vlans[0].tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
eb6f28db 145 }
eec25dc1 146 if (!(ofpfw & OFPFW10_DL_VLAN)) {
f0fb825a 147 wc->masks.vlans[0].tci |= htons(VLAN_VID_MASK | VLAN_CFI);
eb6f28db
BP
148 }
149}
150
81a76618 151/* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
eb6f28db 152void
81a76618
BP
153ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
154 struct match *match)
155{
156 uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
157
158 /* Initialize match->wc. */
296e07ac 159 memset(&match->flow, 0, sizeof match->flow);
81a76618
BP
160 ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
161
162 /* Initialize most of match->flow. */
163 match->flow.nw_src = ofmatch->nw_src;
164 match->flow.nw_dst = ofmatch->nw_dst;
4e022ec0 165 match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port));
81a76618
BP
166 match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
167 match->flow.tp_src = ofmatch->tp_src;
168 match->flow.tp_dst = ofmatch->tp_dst;
74ff3298
JR
169 match->flow.dl_src = ofmatch->dl_src;
170 match->flow.dl_dst = ofmatch->dl_dst;
81a76618
BP
171 match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
172 match->flow.nw_proto = ofmatch->nw_proto;
d8ae4d67 173
66642cb4 174 /* Translate VLANs. */
0c436519 175 if (!(ofpfw & OFPFW10_DL_VLAN) &&
81a76618 176 ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
47271d0d
BP
177 /* Match only packets without 802.1Q header.
178 *
eec25dc1 179 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
47271d0d 180 *
eec25dc1 181 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
47271d0d
BP
182 * because we can't have a specific PCP without an 802.1Q header.
183 * However, older versions of OVS treated this as matching packets
184 * withut an 802.1Q header, so we do here too. */
f0fb825a
EG
185 match->flow.vlans[0].tci = htons(0);
186 match->wc.masks.vlans[0].tci = htons(0xffff);
47271d0d
BP
187 } else {
188 ovs_be16 vid, pcp, tci;
bf062576 189 uint16_t hpcp;
47271d0d 190
81a76618 191 vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
bf062576
YT
192 hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK;
193 pcp = htons(hpcp);
47271d0d 194 tci = vid | pcp | htons(VLAN_CFI);
f0fb825a 195 match->flow.vlans[0].tci = tci & match->wc.masks.vlans[0].tci;
66642cb4
BP
196 }
197
d8ae4d67 198 /* Clean up. */
81a76618 199 match_zero_wildcarded_fields(match);
d8ae4d67
BP
200}
201
81a76618 202/* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
d8ae4d67 203void
81a76618
BP
204ofputil_match_to_ofp10_match(const struct match *match,
205 struct ofp10_match *ofmatch)
d8ae4d67 206{
81a76618 207 const struct flow_wildcards *wc = &match->wc;
eeba8e4f 208 uint32_t ofpfw;
d8ae4d67 209
66642cb4 210 /* Figure out most OpenFlow wildcards. */
27cafc5f 211 ofpfw = 0;
4e022ec0 212 if (!wc->masks.in_port.ofp_port) {
27cafc5f
BP
213 ofpfw |= OFPFW10_IN_PORT;
214 }
26720e24 215 if (!wc->masks.dl_type) {
27cafc5f
BP
216 ofpfw |= OFPFW10_DL_TYPE;
217 }
26720e24 218 if (!wc->masks.nw_proto) {
27cafc5f
BP
219 ofpfw |= OFPFW10_NW_PROTO;
220 }
26720e24 221 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
eec25dc1 222 << OFPFW10_NW_SRC_SHIFT);
26720e24 223 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
eec25dc1 224 << OFPFW10_NW_DST_SHIFT);
26720e24 225 if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
eec25dc1 226 ofpfw |= OFPFW10_NW_TOS;
d8ae4d67 227 }
26720e24 228 if (!wc->masks.tp_src) {
eec25dc1 229 ofpfw |= OFPFW10_TP_SRC;
73f33563 230 }
26720e24 231 if (!wc->masks.tp_dst) {
eec25dc1 232 ofpfw |= OFPFW10_TP_DST;
73f33563 233 }
26720e24 234 if (eth_addr_is_zero(wc->masks.dl_src)) {
eec25dc1 235 ofpfw |= OFPFW10_DL_SRC;
73c0ce34 236 }
26720e24 237 if (eth_addr_is_zero(wc->masks.dl_dst)) {
eec25dc1 238 ofpfw |= OFPFW10_DL_DST;
73c0ce34 239 }
ff9d3826 240
66642cb4 241 /* Translate VLANs. */
81a76618
BP
242 ofmatch->dl_vlan = htons(0);
243 ofmatch->dl_vlan_pcp = 0;
f0fb825a 244 if (match->wc.masks.vlans[0].tci == htons(0)) {
eec25dc1 245 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
f0fb825a
EG
246 } else if (match->wc.masks.vlans[0].tci & htons(VLAN_CFI)
247 && !(match->flow.vlans[0].tci & htons(VLAN_CFI))) {
81a76618 248 ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
66642cb4 249 } else {
f0fb825a 250 if (!(match->wc.masks.vlans[0].tci & htons(VLAN_VID_MASK))) {
eec25dc1 251 ofpfw |= OFPFW10_DL_VLAN;
66642cb4 252 } else {
f0fb825a
EG
253 ofmatch->dl_vlan =
254 htons(vlan_tci_to_vid(match->flow.vlans[0].tci));
66642cb4
BP
255 }
256
f0fb825a 257 if (!(match->wc.masks.vlans[0].tci & htons(VLAN_PCP_MASK))) {
eec25dc1 258 ofpfw |= OFPFW10_DL_VLAN_PCP;
66642cb4 259 } else {
f0fb825a 260 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlans[0].tci);
66642cb4
BP
261 }
262 }
263
264 /* Compose most of the match structure. */
81a76618 265 ofmatch->wildcards = htonl(ofpfw);
4e022ec0 266 ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port));
74ff3298
JR
267 ofmatch->dl_src = match->flow.dl_src;
268 ofmatch->dl_dst = match->flow.dl_dst;
81a76618
BP
269 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
270 ofmatch->nw_src = match->flow.nw_src;
271 ofmatch->nw_dst = match->flow.nw_dst;
272 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
273 ofmatch->nw_proto = match->flow.nw_proto;
274 ofmatch->tp_src = match->flow.tp_src;
275 ofmatch->tp_dst = match->flow.tp_dst;
276 memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
277 memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
d8ae4d67
BP
278}
279
aa319503 280enum ofperr
8d8ab6c2 281ofputil_pull_ofp11_match(struct ofpbuf *buf, const struct tun_table *tun_table,
3cddeff0 282 const struct vl_mff_map *vl_mff_map,
8d8ab6c2 283 struct match *match, uint16_t *padded_match_len)
aa319503 284{
6fd6ed71 285 struct ofp11_match_header *omh = buf->data;
36a16881 286 uint16_t match_len;
aa319503 287
6fd6ed71 288 if (buf->size < sizeof *omh) {
aa319503
BP
289 return OFPERR_OFPBMC_BAD_LEN;
290 }
291
36a16881
SH
292 match_len = ntohs(omh->length);
293
aa319503 294 switch (ntohs(omh->type)) {
36a16881
SH
295 case OFPMT_STANDARD: {
296 struct ofp11_match *om;
297
6fd6ed71 298 if (match_len != sizeof *om || buf->size < sizeof *om) {
aa319503
BP
299 return OFPERR_OFPBMC_BAD_LEN;
300 }
301 om = ofpbuf_pull(buf, sizeof *om);
36a16881
SH
302 if (padded_match_len) {
303 *padded_match_len = match_len;
304 }
81a76618 305 return ofputil_match_from_ofp11_match(om, match);
36a16881
SH
306 }
307
308 case OFPMT_OXM:
309 if (padded_match_len) {
310 *padded_match_len = ROUND_UP(match_len, 8);
311 }
3cddeff0 312 return oxm_pull_match(buf, tun_table, vl_mff_map, match);
aa319503
BP
313
314 default:
315 return OFPERR_OFPBMC_BAD_TYPE;
316 }
317}
318
3f0f48cf
YT
319/* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
320 * Returns 0 if successful, otherwise an OFPERR_* value. */
410698cf 321enum ofperr
81a76618
BP
322ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
323 struct match *match)
410698cf 324{
81a76618 325 uint16_t wc = ntohl(ofmatch->wildcards);
8087f5ff 326 bool ipv4, arp, rarp;
410698cf 327
81a76618 328 match_init_catchall(match);
410698cf
BP
329
330 if (!(wc & OFPFW11_IN_PORT)) {
4e022ec0 331 ofp_port_t ofp_port;
410698cf
BP
332 enum ofperr error;
333
81a76618 334 error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
410698cf
BP
335 if (error) {
336 return OFPERR_OFPBMC_BAD_VALUE;
337 }
81a76618 338 match_set_in_port(match, ofp_port);
410698cf
BP
339 }
340
74ff3298
JR
341 match_set_dl_src_masked(match, ofmatch->dl_src,
342 eth_addr_invert(ofmatch->dl_src_mask));
343 match_set_dl_dst_masked(match, ofmatch->dl_dst,
344 eth_addr_invert(ofmatch->dl_dst_mask));
410698cf
BP
345
346 if (!(wc & OFPFW11_DL_VLAN)) {
81a76618 347 if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
410698cf 348 /* Match only packets without a VLAN tag. */
f0fb825a
EG
349 match->flow.vlans[0].tci = htons(0);
350 match->wc.masks.vlans[0].tci = OVS_BE16_MAX;
410698cf 351 } else {
81a76618 352 if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
410698cf 353 /* Match any packet with a VLAN tag regardless of VID. */
f0fb825a
EG
354 match->flow.vlans[0].tci = htons(VLAN_CFI);
355 match->wc.masks.vlans[0].tci = htons(VLAN_CFI);
81a76618 356 } else if (ntohs(ofmatch->dl_vlan) < 4096) {
410698cf 357 /* Match only packets with the specified VLAN VID. */
f0fb825a
EG
358 match->flow.vlans[0].tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
359 match->wc.masks.vlans[0].tci = htons(VLAN_CFI | VLAN_VID_MASK);
410698cf
BP
360 } else {
361 /* Invalid VID. */
362 return OFPERR_OFPBMC_BAD_VALUE;
363 }
364
365 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
81a76618 366 if (ofmatch->dl_vlan_pcp <= 7) {
f0fb825a 367 match->flow.vlans[0].tci |= htons(ofmatch->dl_vlan_pcp
81a76618 368 << VLAN_PCP_SHIFT);
f0fb825a 369 match->wc.masks.vlans[0].tci |= htons(VLAN_PCP_MASK);
410698cf
BP
370 } else {
371 /* Invalid PCP. */
372 return OFPERR_OFPBMC_BAD_VALUE;
373 }
374 }
375 }
376 }
377
378 if (!(wc & OFPFW11_DL_TYPE)) {
81a76618
BP
379 match_set_dl_type(match,
380 ofputil_dl_type_from_openflow(ofmatch->dl_type));
410698cf
BP
381 }
382
81a76618
BP
383 ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
384 arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
8087f5ff 385 rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
410698cf
BP
386
387 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
81a76618 388 if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
410698cf
BP
389 /* Invalid TOS. */
390 return OFPERR_OFPBMC_BAD_VALUE;
391 }
392
81a76618 393 match_set_nw_dscp(match, ofmatch->nw_tos);
410698cf
BP
394 }
395
8087f5ff 396 if (ipv4 || arp || rarp) {
410698cf 397 if (!(wc & OFPFW11_NW_PROTO)) {
81a76618 398 match_set_nw_proto(match, ofmatch->nw_proto);
410698cf 399 }
81a76618
BP
400 match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
401 match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
410698cf
BP
402 }
403
404#define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
405 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
81a76618 406 switch (match->flow.nw_proto) {
410698cf
BP
407 case IPPROTO_ICMP:
408 /* "A.2.3 Flow Match Structures" in OF1.1 says:
409 *
410 * The tp_src and tp_dst fields will be ignored unless the
411 * network protocol specified is as TCP, UDP or SCTP.
412 *
413 * but I'm pretty sure we should support ICMP too, otherwise
414 * that's a regression from OF1.0. */
415 if (!(wc & OFPFW11_TP_SRC)) {
81a76618 416 uint16_t icmp_type = ntohs(ofmatch->tp_src);
410698cf 417 if (icmp_type < 0x100) {
81a76618 418 match_set_icmp_type(match, icmp_type);
410698cf
BP
419 } else {
420 return OFPERR_OFPBMC_BAD_FIELD;
421 }
422 }
423 if (!(wc & OFPFW11_TP_DST)) {
81a76618 424 uint16_t icmp_code = ntohs(ofmatch->tp_dst);
410698cf 425 if (icmp_code < 0x100) {
81a76618 426 match_set_icmp_code(match, icmp_code);
410698cf
BP
427 } else {
428 return OFPERR_OFPBMC_BAD_FIELD;
429 }
430 }
431 break;
432
433 case IPPROTO_TCP:
434 case IPPROTO_UDP:
0d56eaf2 435 case IPPROTO_SCTP:
410698cf 436 if (!(wc & (OFPFW11_TP_SRC))) {
81a76618 437 match_set_tp_src(match, ofmatch->tp_src);
410698cf
BP
438 }
439 if (!(wc & (OFPFW11_TP_DST))) {
81a76618 440 match_set_tp_dst(match, ofmatch->tp_dst);
410698cf
BP
441 }
442 break;
443
410698cf
BP
444 default:
445 /* OF1.1 says explicitly to ignore this. */
446 break;
447 }
448 }
449
b02475c5 450 if (eth_type_mpls(match->flow.dl_type)) {
097d4939 451 if (!(wc & OFPFW11_MPLS_LABEL)) {
8bfd0fda 452 match_set_mpls_label(match, 0, ofmatch->mpls_label);
097d4939
JR
453 }
454 if (!(wc & OFPFW11_MPLS_TC)) {
8bfd0fda 455 match_set_mpls_tc(match, 0, ofmatch->mpls_tc);
410698cf
BP
456 }
457 }
458
81a76618
BP
459 match_set_metadata_masked(match, ofmatch->metadata,
460 ~ofmatch->metadata_mask);
410698cf
BP
461
462 return 0;
463}
464
81a76618 465/* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
410698cf 466void
81a76618
BP
467ofputil_match_to_ofp11_match(const struct match *match,
468 struct ofp11_match *ofmatch)
410698cf
BP
469{
470 uint32_t wc = 0;
410698cf 471
81a76618
BP
472 memset(ofmatch, 0, sizeof *ofmatch);
473 ofmatch->omh.type = htons(OFPMT_STANDARD);
474 ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
410698cf 475
4e022ec0 476 if (!match->wc.masks.in_port.ofp_port) {
410698cf
BP
477 wc |= OFPFW11_IN_PORT;
478 } else {
4e022ec0 479 ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port);
410698cf
BP
480 }
481
74ff3298
JR
482 ofmatch->dl_src = match->flow.dl_src;
483 ofmatch->dl_src_mask = eth_addr_invert(match->wc.masks.dl_src);
484 ofmatch->dl_dst = match->flow.dl_dst;
485 ofmatch->dl_dst_mask = eth_addr_invert(match->wc.masks.dl_dst);
410698cf 486
f0fb825a 487 if (match->wc.masks.vlans[0].tci == htons(0)) {
410698cf 488 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
f0fb825a
EG
489 } else if (match->wc.masks.vlans[0].tci & htons(VLAN_CFI)
490 && !(match->flow.vlans[0].tci & htons(VLAN_CFI))) {
81a76618 491 ofmatch->dl_vlan = htons(OFPVID11_NONE);
410698cf
BP
492 wc |= OFPFW11_DL_VLAN_PCP;
493 } else {
f0fb825a 494 if (!(match->wc.masks.vlans[0].tci & htons(VLAN_VID_MASK))) {
81a76618 495 ofmatch->dl_vlan = htons(OFPVID11_ANY);
410698cf 496 } else {
f0fb825a
EG
497 ofmatch->dl_vlan =
498 htons(vlan_tci_to_vid(match->flow.vlans[0].tci));
410698cf
BP
499 }
500
f0fb825a 501 if (!(match->wc.masks.vlans[0].tci & htons(VLAN_PCP_MASK))) {
410698cf
BP
502 wc |= OFPFW11_DL_VLAN_PCP;
503 } else {
f0fb825a 504 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlans[0].tci);
410698cf
BP
505 }
506 }
507
81a76618 508 if (!match->wc.masks.dl_type) {
410698cf
BP
509 wc |= OFPFW11_DL_TYPE;
510 } else {
81a76618 511 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
410698cf
BP
512 }
513
81a76618 514 if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
410698cf
BP
515 wc |= OFPFW11_NW_TOS;
516 } else {
81a76618 517 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
410698cf
BP
518 }
519
81a76618 520 if (!match->wc.masks.nw_proto) {
410698cf
BP
521 wc |= OFPFW11_NW_PROTO;
522 } else {
81a76618 523 ofmatch->nw_proto = match->flow.nw_proto;
410698cf
BP
524 }
525
81a76618
BP
526 ofmatch->nw_src = match->flow.nw_src;
527 ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
528 ofmatch->nw_dst = match->flow.nw_dst;
529 ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
410698cf 530
81a76618 531 if (!match->wc.masks.tp_src) {
410698cf
BP
532 wc |= OFPFW11_TP_SRC;
533 } else {
81a76618 534 ofmatch->tp_src = match->flow.tp_src;
410698cf
BP
535 }
536
81a76618 537 if (!match->wc.masks.tp_dst) {
410698cf
BP
538 wc |= OFPFW11_TP_DST;
539 } else {
81a76618 540 ofmatch->tp_dst = match->flow.tp_dst;
410698cf
BP
541 }
542
8bfd0fda 543 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) {
097d4939
JR
544 wc |= OFPFW11_MPLS_LABEL;
545 } else {
8bfd0fda
BP
546 ofmatch->mpls_label = htonl(mpls_lse_to_label(
547 match->flow.mpls_lse[0]));
097d4939
JR
548 }
549
8bfd0fda 550 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) {
097d4939
JR
551 wc |= OFPFW11_MPLS_TC;
552 } else {
8bfd0fda 553 ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]);
097d4939 554 }
410698cf 555
81a76618
BP
556 ofmatch->metadata = match->flow.metadata;
557 ofmatch->metadata_mask = ~match->wc.masks.metadata;
410698cf 558
81a76618 559 ofmatch->wildcards = htonl(wc);
410698cf
BP
560}
561
75fa58f8
BP
562/* Returns the "typical" length of a match for 'protocol', for use in
563 * estimating space to preallocate. */
564int
565ofputil_match_typical_len(enum ofputil_protocol protocol)
566{
567 switch (protocol) {
568 case OFPUTIL_P_OF10_STD:
569 case OFPUTIL_P_OF10_STD_TID:
570 return sizeof(struct ofp10_match);
571
572 case OFPUTIL_P_OF10_NXM:
573 case OFPUTIL_P_OF10_NXM_TID:
574 return NXM_TYPICAL_LEN;
575
576 case OFPUTIL_P_OF11_STD:
577 return sizeof(struct ofp11_match);
578
579 case OFPUTIL_P_OF12_OXM:
580 case OFPUTIL_P_OF13_OXM:
c37c0382 581 case OFPUTIL_P_OF14_OXM:
42dccab5 582 case OFPUTIL_P_OF15_OXM:
b79d45a1 583 case OFPUTIL_P_OF16_OXM:
75fa58f8
BP
584 return NXM_TYPICAL_LEN;
585
586 default:
428b2edd 587 OVS_NOT_REACHED();
75fa58f8
BP
588 }
589}
590
591/* Appends to 'b' an struct ofp11_match_header followed by a match that
592 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
593 * data appended out to a multiple of 8. 'protocol' must be one that is usable
594 * in OpenFlow 1.1 or later.
595 *
596 * This function can cause 'b''s data to be reallocated.
597 *
598 * Returns the number of bytes appended to 'b', excluding the padding. Never
599 * returns zero. */
600int
601ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match,
602 enum ofputil_protocol protocol)
603{
604 switch (protocol) {
605 case OFPUTIL_P_OF10_STD:
606 case OFPUTIL_P_OF10_STD_TID:
607 case OFPUTIL_P_OF10_NXM:
608 case OFPUTIL_P_OF10_NXM_TID:
428b2edd 609 OVS_NOT_REACHED();
75fa58f8
BP
610
611 case OFPUTIL_P_OF11_STD: {
612 struct ofp11_match *om;
613
614 /* Make sure that no padding is needed. */
615 BUILD_ASSERT_DECL(sizeof *om % 8 == 0);
616
617 om = ofpbuf_put_uninit(b, sizeof *om);
618 ofputil_match_to_ofp11_match(match, om);
619 return sizeof *om;
620 }
621
622 case OFPUTIL_P_OF12_OXM:
623 case OFPUTIL_P_OF13_OXM:
c37c0382 624 case OFPUTIL_P_OF14_OXM:
42dccab5 625 case OFPUTIL_P_OF15_OXM:
b79d45a1 626 case OFPUTIL_P_OF16_OXM:
9d84066c
BP
627 return oxm_put_match(b, match,
628 ofputil_protocol_to_ofp_version(protocol));
75fa58f8
BP
629 }
630
428b2edd 631 OVS_NOT_REACHED();
75fa58f8
BP
632}
633
36956a7d 634/* Given a 'dl_type' value in the format used in struct flow, returns the
eec25dc1
BP
635 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
636 * structure. */
36956a7d
BP
637ovs_be16
638ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
639{
640 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
641 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
642 : flow_dl_type);
643}
644
eec25dc1 645/* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
36956a7d
BP
646 * structure, returns the corresponding 'dl_type' value for use in struct
647 * flow. */
648ovs_be16
649ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
650{
651 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
652 ? htons(FLOW_DL_TYPE_NONE)
653 : ofp_dl_type);
654}
2e4f5fcf 655\f
27527aa0 656/* Protocols. */
7fa91113 657
27527aa0
BP
658struct proto_abbrev {
659 enum ofputil_protocol protocol;
660 const char *name;
661};
662
663/* Most users really don't care about some of the differences between
aa233d57 664 * protocols. These abbreviations help with that. */
27527aa0 665static const struct proto_abbrev proto_abbrevs[] = {
aa233d57
BP
666 { OFPUTIL_P_ANY, "any" },
667 { OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" },
668 { OFPUTIL_P_OF10_NXM_ANY, "NXM" },
669 { OFPUTIL_P_ANY_OXM, "OXM" },
27527aa0
BP
670};
671#define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
672
673enum ofputil_protocol ofputil_flow_dump_protocols[] = {
b79d45a1 674 OFPUTIL_P_OF16_OXM,
42dccab5 675 OFPUTIL_P_OF15_OXM,
c37c0382 676 OFPUTIL_P_OF14_OXM,
2e1ae200 677 OFPUTIL_P_OF13_OXM,
8d7785bd 678 OFPUTIL_P_OF12_OXM,
75fa58f8 679 OFPUTIL_P_OF11_STD,
85813857
BP
680 OFPUTIL_P_OF10_NXM,
681 OFPUTIL_P_OF10_STD,
27527aa0
BP
682};
683size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
684
4f13da56
BP
685/* Returns the set of ofputil_protocols that are supported with the given
686 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
687 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
688 * if 'version' is not supported or outside the valid range. */
27527aa0 689enum ofputil_protocol
4f13da56 690ofputil_protocols_from_ofp_version(enum ofp_version version)
27527aa0
BP
691{
692 switch (version) {
2e3fa633 693 case OFP10_VERSION:
4f13da56 694 return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY;
75fa58f8
BP
695 case OFP11_VERSION:
696 return OFPUTIL_P_OF11_STD;
2e3fa633 697 case OFP12_VERSION:
85813857 698 return OFPUTIL_P_OF12_OXM;
2e1ae200
JR
699 case OFP13_VERSION:
700 return OFPUTIL_P_OF13_OXM;
c37c0382
AC
701 case OFP14_VERSION:
702 return OFPUTIL_P_OF14_OXM;
42dccab5
BP
703 case OFP15_VERSION:
704 return OFPUTIL_P_OF15_OXM;
b79d45a1
BP
705 case OFP16_VERSION:
706 return OFPUTIL_P_OF16_OXM;
2e3fa633
SH
707 default:
708 return 0;
27527aa0
BP
709 }
710}
711
4f13da56
BP
712/* Returns the ofputil_protocol that is initially in effect on an OpenFlow
713 * connection that has negotiated the given 'version'. 'version' should
714 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
715 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
716 * outside the valid range. */
717enum ofputil_protocol
718ofputil_protocol_from_ofp_version(enum ofp_version version)
719{
720 return rightmost_1bit(ofputil_protocols_from_ofp_version(version));
721}
722
44d3732d 723/* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
2e1ae200 724 * etc.) that corresponds to 'protocol'. */
2e3fa633 725enum ofp_version
9e1fd49b
BP
726ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
727{
728 switch (protocol) {
85813857
BP
729 case OFPUTIL_P_OF10_STD:
730 case OFPUTIL_P_OF10_STD_TID:
731 case OFPUTIL_P_OF10_NXM:
732 case OFPUTIL_P_OF10_NXM_TID:
9e1fd49b 733 return OFP10_VERSION;
75fa58f8
BP
734 case OFPUTIL_P_OF11_STD:
735 return OFP11_VERSION;
85813857 736 case OFPUTIL_P_OF12_OXM:
44d3732d 737 return OFP12_VERSION;
2e1ae200
JR
738 case OFPUTIL_P_OF13_OXM:
739 return OFP13_VERSION;
c37c0382
AC
740 case OFPUTIL_P_OF14_OXM:
741 return OFP14_VERSION;
42dccab5
BP
742 case OFPUTIL_P_OF15_OXM:
743 return OFP15_VERSION;
b79d45a1
BP
744 case OFPUTIL_P_OF16_OXM:
745 return OFP16_VERSION;
9e1fd49b
BP
746 }
747
428b2edd 748 OVS_NOT_REACHED();
9e1fd49b
BP
749}
750
4f13da56
BP
751/* Returns a bitmap of OpenFlow versions that are supported by at
752 * least one of the 'protocols'. */
753uint32_t
754ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols)
755{
756 uint32_t bitmap = 0;
757
758 for (; protocols; protocols = zero_rightmost_1bit(protocols)) {
759 enum ofputil_protocol protocol = rightmost_1bit(protocols);
760
761 bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol);
762 }
763
764 return bitmap;
765}
766
767/* Returns the set of protocols that are supported on top of the
768 * OpenFlow versions included in 'bitmap'. */
769enum ofputil_protocol
770ofputil_protocols_from_version_bitmap(uint32_t bitmap)
771{
772 enum ofputil_protocol protocols = 0;
773
774 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
775 enum ofp_version version = rightmost_1bit_idx(bitmap);
776
777 protocols |= ofputil_protocols_from_ofp_version(version);
778 }
779
780 return protocols;
781}
782
27527aa0
BP
783/* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
784 * otherwise. */
7fa91113 785bool
27527aa0 786ofputil_protocol_is_valid(enum ofputil_protocol protocol)
7fa91113 787{
27527aa0
BP
788 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
789}
790
791/* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
792 * extension turned on or off if 'enable' is true or false, respectively.
793 *
794 * This extension is only useful for protocols whose "standard" version does
795 * not allow specific tables to be modified. In particular, this is true of
796 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
797 * specifies a table ID and so there is no need for such an extension. When
798 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
799 * extension, this function just returns its 'protocol' argument unchanged
800 * regardless of the value of 'enable'. */
801enum ofputil_protocol
802ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
803{
804 switch (protocol) {
85813857
BP
805 case OFPUTIL_P_OF10_STD:
806 case OFPUTIL_P_OF10_STD_TID:
807 return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
27527aa0 808
85813857
BP
809 case OFPUTIL_P_OF10_NXM:
810 case OFPUTIL_P_OF10_NXM_TID:
811 return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
27527aa0 812
75fa58f8
BP
813 case OFPUTIL_P_OF11_STD:
814 return OFPUTIL_P_OF11_STD;
815
85813857
BP
816 case OFPUTIL_P_OF12_OXM:
817 return OFPUTIL_P_OF12_OXM;
44d3732d 818
2e1ae200
JR
819 case OFPUTIL_P_OF13_OXM:
820 return OFPUTIL_P_OF13_OXM;
821
c37c0382
AC
822 case OFPUTIL_P_OF14_OXM:
823 return OFPUTIL_P_OF14_OXM;
824
42dccab5
BP
825 case OFPUTIL_P_OF15_OXM:
826 return OFPUTIL_P_OF15_OXM;
827
b79d45a1
BP
828 case OFPUTIL_P_OF16_OXM:
829 return OFPUTIL_P_OF16_OXM;
830
27527aa0 831 default:
428b2edd 832 OVS_NOT_REACHED();
7fa91113 833 }
27527aa0 834}
7fa91113 835
27527aa0
BP
836/* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
837 * some extension to a standard protocol version, the return value is the
838 * standard version of that protocol without any extension. If 'protocol' is a
839 * standard protocol version, returns 'protocol' unchanged. */
840enum ofputil_protocol
841ofputil_protocol_to_base(enum ofputil_protocol protocol)
842{
843 return ofputil_protocol_set_tid(protocol, false);
7fa91113
BP
844}
845
27527aa0
BP
846/* Returns 'new_base' with any extensions taken from 'cur'. */
847enum ofputil_protocol
848ofputil_protocol_set_base(enum ofputil_protocol cur,
849 enum ofputil_protocol new_base)
7fa91113 850{
27527aa0
BP
851 bool tid = (cur & OFPUTIL_P_TID) != 0;
852
853 switch (new_base) {
85813857
BP
854 case OFPUTIL_P_OF10_STD:
855 case OFPUTIL_P_OF10_STD_TID:
856 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
27527aa0 857
85813857
BP
858 case OFPUTIL_P_OF10_NXM:
859 case OFPUTIL_P_OF10_NXM_TID:
860 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
27527aa0 861
75fa58f8
BP
862 case OFPUTIL_P_OF11_STD:
863 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD, tid);
864
85813857
BP
865 case OFPUTIL_P_OF12_OXM:
866 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
44d3732d 867
2e1ae200
JR
868 case OFPUTIL_P_OF13_OXM:
869 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
870
c37c0382
AC
871 case OFPUTIL_P_OF14_OXM:
872 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM, tid);
873
42dccab5
BP
874 case OFPUTIL_P_OF15_OXM:
875 return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM, tid);
876
b79d45a1
BP
877 case OFPUTIL_P_OF16_OXM:
878 return ofputil_protocol_set_tid(OFPUTIL_P_OF16_OXM, tid);
879
7fa91113 880 default:
428b2edd 881 OVS_NOT_REACHED();
7fa91113
BP
882 }
883}
884
27527aa0
BP
885/* Returns a string form of 'protocol', if a simple form exists (that is, if
886 * 'protocol' is either a single protocol or it is a combination of protocols
887 * that have a single abbreviation). Otherwise, returns NULL. */
888const char *
889ofputil_protocol_to_string(enum ofputil_protocol protocol)
88ca35ee 890{
27527aa0
BP
891 const struct proto_abbrev *p;
892
893 /* Use a "switch" statement for single-bit names so that we get a compiler
894 * warning if we forget any. */
895 switch (protocol) {
85813857 896 case OFPUTIL_P_OF10_NXM:
27527aa0
BP
897 return "NXM-table_id";
898
85813857 899 case OFPUTIL_P_OF10_NXM_TID:
27527aa0
BP
900 return "NXM+table_id";
901
85813857 902 case OFPUTIL_P_OF10_STD:
27527aa0
BP
903 return "OpenFlow10-table_id";
904
85813857 905 case OFPUTIL_P_OF10_STD_TID:
27527aa0 906 return "OpenFlow10+table_id";
44d3732d 907
75fa58f8
BP
908 case OFPUTIL_P_OF11_STD:
909 return "OpenFlow11";
910
85813857 911 case OFPUTIL_P_OF12_OXM:
e71bff1b 912 return "OXM-OpenFlow12";
2e1ae200
JR
913
914 case OFPUTIL_P_OF13_OXM:
e71bff1b 915 return "OXM-OpenFlow13";
c37c0382
AC
916
917 case OFPUTIL_P_OF14_OXM:
918 return "OXM-OpenFlow14";
42dccab5
BP
919
920 case OFPUTIL_P_OF15_OXM:
921 return "OXM-OpenFlow15";
b79d45a1
BP
922
923 case OFPUTIL_P_OF16_OXM:
924 return "OXM-OpenFlow16";
27527aa0
BP
925 }
926
927 /* Check abbreviations. */
928 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
929 if (protocol == p->protocol) {
930 return p->name;
931 }
932 }
933
934 return NULL;
935}
936
937/* Returns a string that represents 'protocols'. The return value might be a
938 * comma-separated list if 'protocols' doesn't have a simple name. The return
939 * value is "none" if 'protocols' is 0.
940 *
941 * The caller must free the returned string (with free()). */
942char *
943ofputil_protocols_to_string(enum ofputil_protocol protocols)
944{
945 struct ds s;
946
cb22974d 947 ovs_assert(!(protocols & ~OFPUTIL_P_ANY));
27527aa0
BP
948 if (protocols == 0) {
949 return xstrdup("none");
950 }
951
952 ds_init(&s);
953 while (protocols) {
954 const struct proto_abbrev *p;
955 int i;
956
957 if (s.length) {
958 ds_put_char(&s, ',');
959 }
960
961 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
962 if ((protocols & p->protocol) == p->protocol) {
963 ds_put_cstr(&s, p->name);
964 protocols &= ~p->protocol;
965 goto match;
966 }
967 }
968
969 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
970 enum ofputil_protocol bit = 1u << i;
971
972 if (protocols & bit) {
973 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
974 protocols &= ~bit;
975 goto match;
976 }
977 }
428b2edd 978 OVS_NOT_REACHED();
27527aa0
BP
979
980 match: ;
981 }
982 return ds_steal_cstr(&s);
983}
984
985static enum ofputil_protocol
986ofputil_protocol_from_string__(const char *s, size_t n)
987{
988 const struct proto_abbrev *p;
989 int i;
990
991 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
992 enum ofputil_protocol bit = 1u << i;
993 const char *name = ofputil_protocol_to_string(bit);
994
995 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
996 return bit;
997 }
998 }
999
1000 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1001 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
1002 return p->protocol;
1003 }
1004 }
1005
1006 return 0;
1007}
1008
1009/* Returns the nonempty set of protocols represented by 's', which can be a
1010 * single protocol name or abbreviation or a comma-separated list of them.
1011 *
1012 * Aborts the program with an error message if 's' is invalid. */
1013enum ofputil_protocol
1014ofputil_protocols_from_string(const char *s)
1015{
1016 const char *orig_s = s;
1017 enum ofputil_protocol protocols;
1018
1019 protocols = 0;
1020 while (*s) {
1021 enum ofputil_protocol p;
1022 size_t n;
1023
1024 n = strcspn(s, ",");
1025 if (n == 0) {
1026 s++;
1027 continue;
1028 }
1029
1030 p = ofputil_protocol_from_string__(s, n);
1031 if (!p) {
1032 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
1033 }
1034 protocols |= p;
1035
1036 s += n;
1037 }
1038
1039 if (!protocols) {
1040 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
1041 }
1042 return protocols;
88ca35ee
BP
1043}
1044
9d84066c 1045enum ofp_version
03e1125c
SH
1046ofputil_version_from_string(const char *s)
1047{
1048 if (!strcasecmp(s, "OpenFlow10")) {
1049 return OFP10_VERSION;
1050 }
1051 if (!strcasecmp(s, "OpenFlow11")) {
1052 return OFP11_VERSION;
1053 }
1054 if (!strcasecmp(s, "OpenFlow12")) {
1055 return OFP12_VERSION;
1056 }
2e1ae200
JR
1057 if (!strcasecmp(s, "OpenFlow13")) {
1058 return OFP13_VERSION;
1059 }
c37c0382
AC
1060 if (!strcasecmp(s, "OpenFlow14")) {
1061 return OFP14_VERSION;
1062 }
42dccab5
BP
1063 if (!strcasecmp(s, "OpenFlow15")) {
1064 return OFP15_VERSION;
1065 }
b79d45a1
BP
1066 if (!strcasecmp(s, "OpenFlow16")) {
1067 return OFP16_VERSION;
1068 }
7beaa082 1069 return 0;
03e1125c
SH
1070}
1071
1072static bool
e091ef84 1073is_delimiter(unsigned char c)
03e1125c
SH
1074{
1075 return isspace(c) || c == ',';
1076}
1077
1078uint32_t
1079ofputil_versions_from_string(const char *s)
1080{
1081 size_t i = 0;
1082 uint32_t bitmap = 0;
1083
1084 while (s[i]) {
1085 size_t j;
7beaa082 1086 int version;
03e1125c
SH
1087 char *key;
1088
1089 if (is_delimiter(s[i])) {
1090 i++;
1091 continue;
1092 }
1093 j = 0;
1094 while (s[i + j] && !is_delimiter(s[i + j])) {
1095 j++;
1096 }
1097 key = xmemdup0(s + i, j);
1098 version = ofputil_version_from_string(key);
7beaa082
SH
1099 if (!version) {
1100 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key);
1101 }
03e1125c
SH
1102 free(key);
1103 bitmap |= 1u << version;
1104 i += j;
1105 }
1106
1107 return bitmap;
1108}
1109
7beaa082
SH
1110uint32_t
1111ofputil_versions_from_strings(char ** const s, size_t count)
1112{
1113 uint32_t bitmap = 0;
1114
1115 while (count--) {
1116 int version = ofputil_version_from_string(s[count]);
1117 if (!version) {
1118 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]);
1119 } else {
1120 bitmap |= 1u << version;
1121 }
1122 }
1123
1124 return bitmap;
1125}
1126
03e1125c
SH
1127const char *
1128ofputil_version_to_string(enum ofp_version ofp_version)
1129{
1130 switch (ofp_version) {
1131 case OFP10_VERSION:
1132 return "OpenFlow10";
1133 case OFP11_VERSION:
1134 return "OpenFlow11";
1135 case OFP12_VERSION:
1136 return "OpenFlow12";
2e1ae200
JR
1137 case OFP13_VERSION:
1138 return "OpenFlow13";
c37c0382
AC
1139 case OFP14_VERSION:
1140 return "OpenFlow14";
42dccab5
BP
1141 case OFP15_VERSION:
1142 return "OpenFlow15";
b79d45a1
BP
1143 case OFP16_VERSION:
1144 return "OpenFlow16";
03e1125c 1145 default:
428b2edd 1146 OVS_NOT_REACHED();
03e1125c
SH
1147 }
1148}
1149
54834960
EJ
1150bool
1151ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1152{
1153 switch (packet_in_format) {
6409e008
BP
1154 case NXPIF_STANDARD:
1155 case NXPIF_NXT_PACKET_IN:
1156 case NXPIF_NXT_PACKET_IN2:
54834960
EJ
1157 return true;
1158 }
1159
1160 return false;
1161}
1162
1163const char *
1164ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1165{
1166 switch (packet_in_format) {
6409e008
BP
1167 case NXPIF_STANDARD:
1168 return "standard";
1169 case NXPIF_NXT_PACKET_IN:
1170 return "nxt_packet_in";
1171 case NXPIF_NXT_PACKET_IN2:
1172 return "nxt_packet_in2";
54834960 1173 default:
428b2edd 1174 OVS_NOT_REACHED();
54834960
EJ
1175 }
1176}
1177
1178int
1179ofputil_packet_in_format_from_string(const char *s)
1180{
6409e008
BP
1181 return (!strcmp(s, "standard") || !strcmp(s, "openflow10")
1182 ? NXPIF_STANDARD
1183 : !strcmp(s, "nxt_packet_in") || !strcmp(s, "nxm")
1184 ? NXPIF_NXT_PACKET_IN
1185 : !strcmp(s, "nxt_packet_in2")
1186 ? NXPIF_NXT_PACKET_IN2
54834960
EJ
1187 : -1);
1188}
1189
03e1125c
SH
1190void
1191ofputil_format_version(struct ds *msg, enum ofp_version version)
1192{
8989046d 1193 ds_put_format(msg, "0x%02x", version);
03e1125c
SH
1194}
1195
1196void
1197ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1198{
1199 ds_put_cstr(msg, ofputil_version_to_string(version));
1200}
1201
1202static void
1203ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1204 void (*format_version)(struct ds *msg,
1205 enum ofp_version))
1206{
1207 while (bitmap) {
1208 format_version(msg, raw_ctz(bitmap));
1209 bitmap = zero_rightmost_1bit(bitmap);
1210 if (bitmap) {
1211 ds_put_cstr(msg, ", ");
1212 }
1213 }
1214}
1215
1216void
1217ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1218{
1219 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1220}
1221
1222void
1223ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1224{
1225 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1226}
1227
de6c85b0
SH
1228static bool
1229ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
0935de41 1230 uint32_t *allowed_versionsp)
de6c85b0
SH
1231{
1232 uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
db5a1019 1233 const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1);
0935de41 1234 uint32_t allowed_versions;
de6c85b0
SH
1235
1236 if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1237 return false;
1238 }
1239
1240 /* Only use the first 32-bit element of the bitmap as that is all the
1241 * current implementation supports. Subsequent elements are ignored which
7e9383cc 1242 * should have no effect on session negotiation until Open vSwitch supports
de6c85b0
SH
1243 * wire-protocol versions greater than 31.
1244 */
0935de41 1245 allowed_versions = ntohl(bitmap[0]);
de6c85b0 1246
0935de41 1247 if (allowed_versions & 1) {
de6c85b0
SH
1248 /* There's no OpenFlow version 0. */
1249 VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1250 "version 0x00");
0935de41 1251 allowed_versions &= ~1u;
de6c85b0
SH
1252 }
1253
0935de41 1254 if (!allowed_versions) {
de6c85b0
SH
1255 VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1256 "version (between 0x01 and 0x1f)");
1257 return false;
1258 }
1259
0935de41 1260 *allowed_versionsp = allowed_versions;
de6c85b0
SH
1261 return true;
1262}
1263
1264static uint32_t
1265version_bitmap_from_version(uint8_t ofp_version)
1266{
1267 return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1268}
1269
1270/* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1271 * the set of OpenFlow versions for which 'oh' announces support.
1272 *
1273 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1274 * successful, and thus '*allowed_versions' is always initialized. However, it
1275 * returns false if 'oh' contains some data that could not be fully understood,
1276 * true if 'oh' was completely parsed. */
1277bool
1278ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1279{
0a2869d5 1280 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
de6c85b0
SH
1281 ofpbuf_pull(&msg, sizeof *oh);
1282
1283 *allowed_versions = version_bitmap_from_version(oh->version);
0a2869d5
BP
1284
1285 bool ok = true;
6fd6ed71 1286 while (msg.size) {
de6c85b0
SH
1287 const struct ofp_hello_elem_header *oheh;
1288 unsigned int len;
1289
6fd6ed71 1290 if (msg.size < sizeof *oheh) {
de6c85b0
SH
1291 return false;
1292 }
1293
6fd6ed71 1294 oheh = msg.data;
de6c85b0
SH
1295 len = ntohs(oheh->length);
1296 if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1297 return false;
1298 }
1299
1300 if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1301 || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1302 ok = false;
1303 }
1304 }
1305
1306 return ok;
1307}
1308
1309/* Returns true if 'allowed_versions' needs to be accompanied by a version
1310 * bitmap to be correctly expressed in an OFPT_HELLO message. */
5ddea998 1311static bool
de6c85b0
SH
1312should_send_version_bitmap(uint32_t allowed_versions)
1313{
1314 return !is_pow2((allowed_versions >> 1) + 1);
1315}
1316
1317/* Create an OFPT_HELLO message that expresses support for the OpenFlow
1318 * versions in the 'allowed_versions' bitmaps and returns the message. */
1319struct ofpbuf *
1320ofputil_encode_hello(uint32_t allowed_versions)
1321{
1322 enum ofp_version ofp_version;
1323 struct ofpbuf *msg;
1324
1325 ofp_version = leftmost_1bit_idx(allowed_versions);
1326 msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1327
1328 if (should_send_version_bitmap(allowed_versions)) {
1329 struct ofp_hello_elem_header *oheh;
1330 uint16_t map_len;
1331
74c4b9c1 1332 map_len = sizeof allowed_versions;
de6c85b0
SH
1333 oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1334 oheh->type = htons(OFPHET_VERSIONBITMAP);
1335 oheh->length = htons(map_len + sizeof *oheh);
db5a1019 1336 *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions);
1804d25a
BP
1337
1338 ofpmsg_update_length(msg);
de6c85b0
SH
1339 }
1340
1341 return msg;
1342}
1343
27527aa0
BP
1344/* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1345 * protocol is 'current', at least partly transitions the protocol to 'want'.
1346 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1347 * connection if the switch processes the returned message correctly. (If
1348 * '*next != want' then the caller will have to iterate.)
1349 *
e43928f2
BP
1350 * If 'current == want', or if it is not possible to transition from 'current'
1351 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1352 * protocol versions), returns NULL and stores 'current' in '*next'. */
27527aa0
BP
1353struct ofpbuf *
1354ofputil_encode_set_protocol(enum ofputil_protocol current,
1355 enum ofputil_protocol want,
1356 enum ofputil_protocol *next)
1357{
e43928f2 1358 enum ofp_version cur_version, want_version;
27527aa0
BP
1359 enum ofputil_protocol cur_base, want_base;
1360 bool cur_tid, want_tid;
1361
e43928f2
BP
1362 cur_version = ofputil_protocol_to_ofp_version(current);
1363 want_version = ofputil_protocol_to_ofp_version(want);
1364 if (cur_version != want_version) {
1365 *next = current;
1366 return NULL;
1367 }
1368
27527aa0
BP
1369 cur_base = ofputil_protocol_to_base(current);
1370 want_base = ofputil_protocol_to_base(want);
1371 if (cur_base != want_base) {
1372 *next = ofputil_protocol_set_base(current, want_base);
1373
1374 switch (want_base) {
85813857 1375 case OFPUTIL_P_OF10_NXM:
27527aa0
BP
1376 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1377
85813857 1378 case OFPUTIL_P_OF10_STD:
27527aa0
BP
1379 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1380
75fa58f8 1381 case OFPUTIL_P_OF11_STD:
85813857 1382 case OFPUTIL_P_OF12_OXM:
2e1ae200 1383 case OFPUTIL_P_OF13_OXM:
c37c0382 1384 case OFPUTIL_P_OF14_OXM:
42dccab5 1385 case OFPUTIL_P_OF15_OXM:
b79d45a1 1386 case OFPUTIL_P_OF16_OXM:
75fa58f8 1387 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
2e1ae200 1388 * verified above that we're not trying to change versions. */
428b2edd 1389 OVS_NOT_REACHED();
44d3732d 1390
85813857
BP
1391 case OFPUTIL_P_OF10_STD_TID:
1392 case OFPUTIL_P_OF10_NXM_TID:
428b2edd 1393 OVS_NOT_REACHED();
27527aa0
BP
1394 }
1395 }
1396
1397 cur_tid = (current & OFPUTIL_P_TID) != 0;
1398 want_tid = (want & OFPUTIL_P_TID) != 0;
1399 if (cur_tid != want_tid) {
1400 *next = ofputil_protocol_set_tid(current, want_tid);
1401 return ofputil_make_flow_mod_table_id(want_tid);
1402 }
1403
cb22974d 1404 ovs_assert(current == want);
27527aa0
BP
1405
1406 *next = current;
1407 return NULL;
88ca35ee
BP
1408}
1409
27527aa0
BP
1410/* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1411 * format to 'nxff'. */
88ca35ee 1412struct ofpbuf *
27527aa0 1413ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
88ca35ee 1414{
73dbf4ab 1415 struct nx_set_flow_format *sff;
88ca35ee
BP
1416 struct ofpbuf *msg;
1417
cb22974d 1418 ovs_assert(ofputil_nx_flow_format_is_valid(nxff));
27527aa0 1419
982697a4
BP
1420 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1421 sff = ofpbuf_put_zeros(msg, sizeof *sff);
27527aa0 1422 sff->format = htonl(nxff);
88ca35ee
BP
1423
1424 return msg;
1425}
1426
27527aa0
BP
1427/* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1428 * otherwise. */
1429enum ofputil_protocol
1430ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1431{
1432 switch (flow_format) {
1433 case NXFF_OPENFLOW10:
85813857 1434 return OFPUTIL_P_OF10_STD;
27527aa0
BP
1435
1436 case NXFF_NXM:
85813857 1437 return OFPUTIL_P_OF10_NXM;
27527aa0
BP
1438
1439 default:
1440 return 0;
1441 }
1442}
1443
1444/* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1445bool
1446ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1447{
1448 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1449}
1450
1451/* Returns a string version of 'flow_format', which must be a valid NXFF_*
1452 * value. */
1453const char *
1454ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1455{
1456 switch (flow_format) {
1457 case NXFF_OPENFLOW10:
1458 return "openflow10";
1459 case NXFF_NXM:
1460 return "nxm";
1461 default:
428b2edd 1462 OVS_NOT_REACHED();
27527aa0
BP
1463 }
1464}
1465
54834960 1466struct ofpbuf *
3f4a1939
SH
1467ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1468 enum nx_packet_in_format packet_in_format)
54834960 1469{
73dbf4ab 1470 struct nx_set_packet_in_format *spif;
54834960
EJ
1471 struct ofpbuf *msg;
1472
3f4a1939 1473 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
982697a4 1474 spif = ofpbuf_put_zeros(msg, sizeof *spif);
54834960
EJ
1475 spif->format = htonl(packet_in_format);
1476
1477 return msg;
1478}
1479
6c1491fb
BP
1480/* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1481 * extension on or off (according to 'flow_mod_table_id'). */
1482struct ofpbuf *
1483ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1484{
73dbf4ab 1485 struct nx_flow_mod_table_id *nfmti;
6c1491fb
BP
1486 struct ofpbuf *msg;
1487
982697a4
BP
1488 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1489 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
6c1491fb
BP
1490 nfmti->set = flow_mod_table_id;
1491 return msg;
1492}
1493
0fb88c18
BP
1494struct ofputil_flow_mod_flag {
1495 uint16_t raw_flag;
1496 enum ofp_version min_version, max_version;
1497 enum ofputil_flow_mod_flags flag;
1498};
1499
1500static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = {
1501 { OFPFF_SEND_FLOW_REM, OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM },
1502 { OFPFF_CHECK_OVERLAP, OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP },
1503 { OFPFF10_EMERG, OFP10_VERSION, OFP10_VERSION,
1504 OFPUTIL_FF_EMERG },
1505 { OFPFF12_RESET_COUNTS, OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS },
1506 { OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS },
1507 { OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS },
1508 { 0, 0, 0, 0 },
1509};
1510
1511static enum ofperr
1512ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_,
1513 enum ofp_flow_mod_command command,
1514 enum ofp_version version,
1515 enum ofputil_flow_mod_flags *flagsp)
1516{
1517 uint16_t raw_flags = ntohs(raw_flags_);
1518 const struct ofputil_flow_mod_flag *f;
1519
1520 *flagsp = 0;
1521 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1522 if (raw_flags & f->raw_flag
1523 && version >= f->min_version
1524 && (!f->max_version || version <= f->max_version)) {
1525 raw_flags &= ~f->raw_flag;
1526 *flagsp |= f->flag;
1527 }
1528 }
1529
1530 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1531 * never do.
1532 *
1533 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1534 * resets counters. */
1535 if ((version == OFP10_VERSION || version == OFP11_VERSION)
1536 && command == OFPFC_ADD) {
1537 *flagsp |= OFPUTIL_FF_RESET_COUNTS;
1538 }
1539
1540 return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0;
1541}
1542
1543static ovs_be16
1544ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags,
1545 enum ofp_version version)
1546{
1547 const struct ofputil_flow_mod_flag *f;
1548 uint16_t raw_flags;
1549
1550 raw_flags = 0;
1551 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1552 if (f->flag & flags
1553 && version >= f->min_version
1554 && (!f->max_version || version <= f->max_version)) {
1555 raw_flags |= f->raw_flag;
1556 }
1557 }
1558
1559 return htons(raw_flags);
1560}
1561
7fa91113
BP
1562/* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1563 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1564 * code.
1565 *
f25d0cf3
BP
1566 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1567 * The caller must initialize 'ofpacts' and retains ownership of it.
1568 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1569 *
1570 * Does not validate the flow_mod actions. The caller should do that, with
1571 * ofpacts_check(). */
90bf1e07 1572enum ofperr
a9a2da38 1573ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
27527aa0 1574 const struct ofp_header *oh,
f25d0cf3 1575 enum ofputil_protocol protocol,
8d8ab6c2 1576 const struct tun_table *tun_table,
04f48a68 1577 const struct vl_mff_map *vl_mff_map,
7e9f8266
BP
1578 struct ofpbuf *ofpacts,
1579 ofp_port_t max_port, uint8_t max_table)
2e4f5fcf 1580{
0fb88c18
BP
1581 ovs_be16 raw_flags;
1582 enum ofperr error;
0a2869d5
BP
1583 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
1584 enum ofpraw raw = ofpraw_pull_assert(&b);
aa319503 1585 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1828ae51 1586 /* Standard OpenFlow 1.1+ flow_mod. */
aa319503 1587 const struct ofp11_flow_mod *ofm;
2e4f5fcf 1588
bbc32a88 1589 ofm = ofpbuf_pull(&b, sizeof *ofm);
2e4f5fcf 1590
3cddeff0
YHW
1591 error = ofputil_pull_ofp11_match(&b, tun_table, vl_mff_map, &fm->match,
1592 NULL);
aa319503
BP
1593 if (error) {
1594 return error;
1c0b7503
BP
1595 }
1596
2e4f5fcf 1597 /* Translate the message. */
81a76618 1598 fm->priority = ntohs(ofm->priority);
75fa58f8
BP
1599 if (ofm->command == OFPFC_ADD
1600 || (oh->version == OFP11_VERSION
1601 && (ofm->command == OFPFC_MODIFY ||
1602 ofm->command == OFPFC_MODIFY_STRICT)
1603 && ofm->cookie_mask == htonll(0))) {
1604 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1605 * not match on the cookie is treated as an "add" if there is no
1606 * match. */
aa319503
BP
1607 fm->cookie = htonll(0);
1608 fm->cookie_mask = htonll(0);
1609 fm->new_cookie = ofm->cookie;
1610 } else {
aa319503
BP
1611 fm->cookie = ofm->cookie;
1612 fm->cookie_mask = ofm->cookie_mask;
b8266395 1613 fm->new_cookie = OVS_BE64_MAX;
aa319503 1614 }
23342857 1615 fm->modify_cookie = false;
aa319503 1616 fm->command = ofm->command;
0e197060
BP
1617
1618 /* Get table ID.
1619 *
083761ad
SH
1620 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1621 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
aa319503 1622 fm->table_id = ofm->table_id;
083761ad 1623 if (fm->table_id == OFPTT_ALL
0e197060
BP
1624 && (oh->version == OFP11_VERSION
1625 || (ofm->command != OFPFC_DELETE &&
1626 ofm->command != OFPFC_DELETE_STRICT))) {
1627 return OFPERR_OFPFMFC_BAD_TABLE_ID;
1628 }
1629
2e4f5fcf
BP
1630 fm->idle_timeout = ntohs(ofm->idle_timeout);
1631 fm->hard_timeout = ntohs(ofm->hard_timeout);
ca26eb44
RB
1632 if (oh->version >= OFP14_VERSION && ofm->command == OFPFC_ADD) {
1633 fm->importance = ntohs(ofm->importance);
1634 } else {
1635 fm->importance = 0;
1636 }
2e4f5fcf 1637 fm->buffer_id = ntohl(ofm->buffer_id);
aa319503 1638 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
2e4f5fcf
BP
1639 if (error) {
1640 return error;
1641 }
7395c052 1642
1fe4c0a0
BP
1643 fm->out_group = (ofm->command == OFPFC_DELETE ||
1644 ofm->command == OFPFC_DELETE_STRICT
1645 ? ntohl(ofm->out_group)
30ef36c6 1646 : OFPG_ANY);
0fb88c18 1647 raw_flags = ofm->flags;
aa319503 1648 } else {
0fb88c18
BP
1649 uint16_t command;
1650
aa319503
BP
1651 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1652 /* Standard OpenFlow 1.0 flow_mod. */
1653 const struct ofp10_flow_mod *ofm;
aa319503
BP
1654
1655 /* Get the ofp10_flow_mod. */
1656 ofm = ofpbuf_pull(&b, sizeof *ofm);
1657
aa319503 1658 /* Translate the rule. */
81a76618
BP
1659 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1660 ofputil_normalize_match(&fm->match);
aa319503 1661
81a76618
BP
1662 /* OpenFlow 1.0 says that exact-match rules have to have the
1663 * highest possible priority. */
1664 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1665 ? ntohs(ofm->priority)
1666 : UINT16_MAX);
1667
aa319503
BP
1668 /* Translate the message. */
1669 command = ntohs(ofm->command);
1670 fm->cookie = htonll(0);
1671 fm->cookie_mask = htonll(0);
1672 fm->new_cookie = ofm->cookie;
1673 fm->idle_timeout = ntohs(ofm->idle_timeout);
1674 fm->hard_timeout = ntohs(ofm->hard_timeout);
ca26eb44 1675 fm->importance = 0;
aa319503 1676 fm->buffer_id = ntohl(ofm->buffer_id);
4e022ec0 1677 fm->out_port = u16_to_ofp(ntohs(ofm->out_port));
30ef36c6 1678 fm->out_group = OFPG_ANY;
0fb88c18 1679 raw_flags = ofm->flags;
aa319503
BP
1680 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1681 /* Nicira extended flow_mod. */
1682 const struct nx_flow_mod *nfm;
aa319503
BP
1683
1684 /* Dissect the message. */
1685 nfm = ofpbuf_pull(&b, sizeof *nfm);
81a76618 1686 error = nx_pull_match(&b, ntohs(nfm->match_len),
8d8ab6c2 1687 &fm->match, &fm->cookie, &fm->cookie_mask,
3cddeff0 1688 tun_table, vl_mff_map);
aa319503
BP
1689 if (error) {
1690 return error;
1691 }
aa319503
BP
1692
1693 /* Translate the message. */
1694 command = ntohs(nfm->command);
1695 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1696 /* Flow additions may only set a new cookie, not match an
1697 * existing cookie. */
1698 return OFPERR_NXBRC_NXM_INVALID;
1699 }
81a76618 1700 fm->priority = ntohs(nfm->priority);
aa319503
BP
1701 fm->new_cookie = nfm->cookie;
1702 fm->idle_timeout = ntohs(nfm->idle_timeout);
1703 fm->hard_timeout = ntohs(nfm->hard_timeout);
ca26eb44 1704 fm->importance = 0;
aa319503 1705 fm->buffer_id = ntohl(nfm->buffer_id);
4e022ec0 1706 fm->out_port = u16_to_ofp(ntohs(nfm->out_port));
30ef36c6 1707 fm->out_group = OFPG_ANY;
0fb88c18 1708 raw_flags = nfm->flags;
aa319503 1709 } else {
428b2edd 1710 OVS_NOT_REACHED();
aa319503
BP
1711 }
1712
b8266395 1713 fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
aa319503
BP
1714 if (protocol & OFPUTIL_P_TID) {
1715 fm->command = command & 0xff;
1716 fm->table_id = command >> 8;
1717 } else {
1f42be1c
JR
1718 if (command > 0xff) {
1719 VLOG_WARN_RL(&bad_ofmsg_rl, "flow_mod has explicit table_id "
1720 "but flow_mod_table_id extension is not enabled");
1721 }
aa319503
BP
1722 fm->command = command;
1723 fm->table_id = 0xff;
e729e793 1724 }
2e4f5fcf
BP
1725 }
1726
e75aad80
JR
1727 /* Check for mismatched conntrack original direction tuple address fields
1728 * w.r.t. the IP version of the match. */
1729 if (((fm->match.wc.masks.ct_nw_src || fm->match.wc.masks.ct_nw_dst)
1730 && fm->match.flow.dl_type != htons(ETH_TYPE_IP))
1731 || ((ipv6_addr_is_set(&fm->match.wc.masks.ct_ipv6_src)
1732 || ipv6_addr_is_set(&fm->match.wc.masks.ct_ipv6_dst))
1733 && fm->match.flow.dl_type != htons(ETH_TYPE_IPV6))) {
1734 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1735 }
1736
1f42be1c
JR
1737 if (fm->command > OFPFC_DELETE_STRICT) {
1738 return OFPERR_OFPFMFC_BAD_COMMAND;
1739 }
1740
5c7c16d8 1741 fm->ofpacts_tlv_bitmap = 0;
04f48a68 1742 error = ofpacts_pull_openflow_instructions(&b, b.size, oh->version,
5c7c16d8
YHW
1743 vl_mff_map,
1744 &fm->ofpacts_tlv_bitmap,
1745 ofpacts);
8f2cded4
BP
1746 if (error) {
1747 return error;
1748 }
6fd6ed71
PS
1749 fm->ofpacts = ofpacts->data;
1750 fm->ofpacts_len = ofpacts->size;
6c1491fb 1751
0fb88c18
BP
1752 error = ofputil_decode_flow_mod_flags(raw_flags, fm->command,
1753 oh->version, &fm->flags);
1754 if (error) {
1755 return error;
1756 }
1757
1758 if (fm->flags & OFPUTIL_FF_EMERG) {
1759 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1760 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1761 *
1762 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1763 * or hard_timeout is nonzero. Otherwise, there is no good error
1764 * code, so just state that the flow table is full. */
1765 return (fm->hard_timeout || fm->idle_timeout
1766 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1767 : OFPERR_OFPFMFC_TABLE_FULL);
1768 }
1769
ba2fe8e9 1770 return ofpacts_check_consistency(fm->ofpacts, fm->ofpacts_len,
67210a55 1771 &fm->match, max_port,
ba2fe8e9 1772 fm->table_id, max_table, protocol);
2e4f5fcf
BP
1773}
1774
638a19b0
JR
1775static enum ofperr
1776ofputil_pull_bands(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1777 struct ofpbuf *bands)
1778{
1779 const struct ofp13_meter_band_header *ombh;
1780 struct ofputil_meter_band *mb;
1781 uint16_t n = 0;
1782
1783 ombh = ofpbuf_try_pull(msg, len);
1784 if (!ombh) {
1785 return OFPERR_OFPBRC_BAD_LEN;
1786 }
1787
1788 while (len >= sizeof (struct ofp13_meter_band_drop)) {
1789 size_t ombh_len = ntohs(ombh->len);
0445637d 1790 /* All supported band types have the same length. */
638a19b0
JR
1791 if (ombh_len != sizeof (struct ofp13_meter_band_drop)) {
1792 return OFPERR_OFPBRC_BAD_LEN;
1793 }
1794 mb = ofpbuf_put_uninit(bands, sizeof *mb);
1795 mb->type = ntohs(ombh->type);
f99d6aa0
BP
1796 if (mb->type != OFPMBT13_DROP && mb->type != OFPMBT13_DSCP_REMARK) {
1797 return OFPERR_OFPMMFC_BAD_BAND;
1798 }
638a19b0
JR
1799 mb->rate = ntohl(ombh->rate);
1800 mb->burst_size = ntohl(ombh->burst_size);
1801 mb->prec_level = (mb->type == OFPMBT13_DSCP_REMARK) ?
1802 ((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0;
1803 n++;
1804 len -= ombh_len;
db5a1019
AW
1805 ombh = ALIGNED_CAST(struct ofp13_meter_band_header *,
1806 (char *) ombh + ombh_len);
638a19b0
JR
1807 }
1808 if (len) {
1809 return OFPERR_OFPBRC_BAD_LEN;
1810 }
1811 *n_bands = n;
1812 return 0;
1813}
1814
1815enum ofperr
1816ofputil_decode_meter_mod(const struct ofp_header *oh,
1817 struct ofputil_meter_mod *mm,
1818 struct ofpbuf *bands)
1819{
0a2869d5 1820 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
638a19b0 1821 ofpraw_pull_assert(&b);
0a2869d5 1822 const struct ofp13_meter_mod *omm = ofpbuf_pull(&b, sizeof *omm);
638a19b0
JR
1823
1824 /* Translate the message. */
1825 mm->command = ntohs(omm->command);
142cdb01
BP
1826 if (mm->command != OFPMC13_ADD &&
1827 mm->command != OFPMC13_MODIFY &&
1828 mm->command != OFPMC13_DELETE) {
1829 return OFPERR_OFPMMFC_BAD_COMMAND;
1830 }
638a19b0
JR
1831 mm->meter.meter_id = ntohl(omm->meter_id);
1832
1833 if (mm->command == OFPMC13_DELETE) {
1834 mm->meter.flags = 0;
1835 mm->meter.n_bands = 0;
1836 mm->meter.bands = NULL;
1837 } else {
1838 enum ofperr error;
1839
1840 mm->meter.flags = ntohs(omm->flags);
13b1febe
BP
1841 if (mm->meter.flags & OFPMF13_KBPS &&
1842 mm->meter.flags & OFPMF13_PKTPS) {
1843 return OFPERR_OFPMMFC_BAD_FLAGS;
1844 }
6fd6ed71 1845 mm->meter.bands = bands->data;
638a19b0 1846
6fd6ed71 1847 error = ofputil_pull_bands(&b, b.size, &mm->meter.n_bands, bands);
638a19b0
JR
1848 if (error) {
1849 return error;
1850 }
1851 }
1852 return 0;
1853}
1854
1855void
1856ofputil_decode_meter_request(const struct ofp_header *oh, uint32_t *meter_id)
1857{
1858 const struct ofp13_meter_multipart_request *omr = ofpmsg_body(oh);
1859 *meter_id = ntohl(omr->meter_id);
1860}
1861
1862struct ofpbuf *
1863ofputil_encode_meter_request(enum ofp_version ofp_version,
1864 enum ofputil_meter_request_type type,
1865 uint32_t meter_id)
1866{
1867 struct ofpbuf *msg;
1868
1869 enum ofpraw raw;
1870
1871 switch (type) {
1872 case OFPUTIL_METER_CONFIG:
1873 raw = OFPRAW_OFPST13_METER_CONFIG_REQUEST;
1874 break;
1875 case OFPUTIL_METER_STATS:
1876 raw = OFPRAW_OFPST13_METER_REQUEST;
1877 break;
1878 default:
1879 case OFPUTIL_METER_FEATURES:
1880 raw = OFPRAW_OFPST13_METER_FEATURES_REQUEST;
1881 break;
1882 }
1883
1884 msg = ofpraw_alloc(raw, ofp_version, 0);
1885
1886 if (type != OFPUTIL_METER_FEATURES) {
1887 struct ofp13_meter_multipart_request *omr;
1888 omr = ofpbuf_put_zeros(msg, sizeof *omr);
1889 omr->meter_id = htonl(meter_id);
1890 }
1891 return msg;
1892}
1893
1894static void
1895ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb,
1896 struct ofpbuf *msg)
1897{
1898 uint16_t n = 0;
1899
1900 for (n = 0; n < n_bands; ++n) {
0445637d 1901 /* Currently all band types have same size. */
638a19b0
JR
1902 struct ofp13_meter_band_dscp_remark *ombh;
1903 size_t ombh_len = sizeof *ombh;
1904
1905 ombh = ofpbuf_put_zeros(msg, ombh_len);
1906
1907 ombh->type = htons(mb->type);
1908 ombh->len = htons(ombh_len);
1909 ombh->rate = htonl(mb->rate);
1910 ombh->burst_size = htonl(mb->burst_size);
1911 ombh->prec_level = mb->prec_level;
1912
1913 mb++;
1914 }
1915}
1916
1917/* Encode a meter stat for 'mc' and append it to 'replies'. */
1918void
ca6ba700 1919ofputil_append_meter_config(struct ovs_list *replies,
638a19b0
JR
1920 const struct ofputil_meter_config *mc)
1921{
417e7e66 1922 struct ofpbuf *msg = ofpbuf_from_list(ovs_list_back(replies));
6fd6ed71 1923 size_t start_ofs = msg->size;
6a5490c6 1924 struct ofp13_meter_config *reply;
638a19b0 1925
6a5490c6 1926 ofpbuf_put_uninit(msg, sizeof *reply);
638a19b0
JR
1927 ofputil_put_bands(mc->n_bands, mc->bands, msg);
1928
32756a57 1929 reply = ofpbuf_at_assert(msg, start_ofs, sizeof *reply);
6a5490c6
BP
1930 reply->flags = htons(mc->flags);
1931 reply->meter_id = htonl(mc->meter_id);
6fd6ed71 1932 reply->length = htons(msg->size - start_ofs);
638a19b0
JR
1933
1934 ofpmp_postappend(replies, start_ofs);
1935}
1936
1937/* Encode a meter stat for 'ms' and append it to 'replies'. */
1938void
ca6ba700 1939ofputil_append_meter_stats(struct ovs_list *replies,
638a19b0
JR
1940 const struct ofputil_meter_stats *ms)
1941{
1942 struct ofp13_meter_stats *reply;
1943 uint16_t n = 0;
1944 uint16_t len;
1945
1946 len = sizeof *reply + ms->n_bands * sizeof(struct ofp13_meter_band_stats);
1947 reply = ofpmp_append(replies, len);
1948
1949 reply->meter_id = htonl(ms->meter_id);
1950 reply->len = htons(len);
1951 memset(reply->pad, 0, sizeof reply->pad);
1952 reply->flow_count = htonl(ms->flow_count);
1953 reply->packet_in_count = htonll(ms->packet_in_count);
1954 reply->byte_in_count = htonll(ms->byte_in_count);
1955 reply->duration_sec = htonl(ms->duration_sec);
1956 reply->duration_nsec = htonl(ms->duration_nsec);
1957
1958 for (n = 0; n < ms->n_bands; ++n) {
1959 const struct ofputil_meter_band_stats *src = &ms->bands[n];
1960 struct ofp13_meter_band_stats *dst = &reply->band_stats[n];
1961
1962 dst->packet_band_count = htonll(src->packet_count);
1963 dst->byte_band_count = htonll(src->byte_count);
1964 }
1965}
1966
1967/* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
1968 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
1969 * 'bands'. The caller must have initialized 'bands' and retains ownership of
1970 * it across the call.
1971 *
1972 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
1973 * message. Calling this function multiple times for a single 'msg' iterates
1974 * through the replies. 'bands' is cleared for each reply.
1975 *
1976 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1977 * otherwise a positive errno value. */
1978int
1979ofputil_decode_meter_config(struct ofpbuf *msg,
1980 struct ofputil_meter_config *mc,
1981 struct ofpbuf *bands)
1982{
1983 const struct ofp13_meter_config *omc;
1984 enum ofperr err;
1985
1986 /* Pull OpenFlow headers for the first call. */
6fd6ed71 1987 if (!msg->header) {
638a19b0
JR
1988 ofpraw_pull_assert(msg);
1989 }
1990
6fd6ed71 1991 if (!msg->size) {
638a19b0
JR
1992 return EOF;
1993 }
1994
1995 omc = ofpbuf_try_pull(msg, sizeof *omc);
1996 if (!omc) {
0445637d 1997 VLOG_WARN_RL(&bad_ofmsg_rl,
437d0d22 1998 "OFPMP_METER_CONFIG reply has %"PRIu32" leftover bytes at end",
6fd6ed71 1999 msg->size);
638a19b0
JR
2000 return OFPERR_OFPBRC_BAD_LEN;
2001 }
2002
2003 ofpbuf_clear(bands);
2004 err = ofputil_pull_bands(msg, ntohs(omc->length) - sizeof *omc,
2005 &mc->n_bands, bands);
2006 if (err) {
2007 return err;
2008 }
2009 mc->meter_id = ntohl(omc->meter_id);
2010 mc->flags = ntohs(omc->flags);
6fd6ed71 2011 mc->bands = bands->data;
638a19b0
JR
2012
2013 return 0;
2014}
2015
2016static enum ofperr
2017ofputil_pull_band_stats(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
2018 struct ofpbuf *bands)
2019{
2020 const struct ofp13_meter_band_stats *ombs;
2021 struct ofputil_meter_band_stats *mbs;
2022 uint16_t n, i;
2023
0445637d
JR
2024 ombs = ofpbuf_try_pull(msg, len);
2025 if (!ombs) {
2026 return OFPERR_OFPBRC_BAD_LEN;
2027 }
2028
638a19b0
JR
2029 n = len / sizeof *ombs;
2030 if (len != n * sizeof *ombs) {
2031 return OFPERR_OFPBRC_BAD_LEN;
2032 }
2033
638a19b0
JR
2034 mbs = ofpbuf_put_uninit(bands, len);
2035
2036 for (i = 0; i < n; ++i) {
2037 mbs[i].packet_count = ntohll(ombs[i].packet_band_count);
2038 mbs[i].byte_count = ntohll(ombs[i].byte_band_count);
2039 }
2040 *n_bands = n;
2041 return 0;
2042}
2043
2044/* Converts an OFPMP_METER reply in 'msg' into an abstract
2045 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2046 * decoded into 'bands'.
2047 *
2048 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2049 * message. Calling this function multiple times for a single 'msg' iterates
2050 * through the replies. 'bands' is cleared for each reply.
2051 *
2052 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2053 * otherwise a positive errno value. */
2054int
2055ofputil_decode_meter_stats(struct ofpbuf *msg,
2056 struct ofputil_meter_stats *ms,
2057 struct ofpbuf *bands)
2058{
2059 const struct ofp13_meter_stats *oms;
638a19b0
JR
2060 enum ofperr err;
2061
2062 /* Pull OpenFlow headers for the first call. */
6fd6ed71 2063 if (!msg->header) {
638a19b0
JR
2064 ofpraw_pull_assert(msg);
2065 }
2066
6fd6ed71 2067 if (!msg->size) {
638a19b0
JR
2068 return EOF;
2069 }
2070
2071 oms = ofpbuf_try_pull(msg, sizeof *oms);
2072 if (!oms) {
0445637d 2073 VLOG_WARN_RL(&bad_ofmsg_rl,
437d0d22 2074 "OFPMP_METER reply has %"PRIu32" leftover bytes at end",
6fd6ed71 2075 msg->size);
638a19b0
JR
2076 return OFPERR_OFPBRC_BAD_LEN;
2077 }
638a19b0
JR
2078
2079 ofpbuf_clear(bands);
0445637d
JR
2080 err = ofputil_pull_band_stats(msg, ntohs(oms->len) - sizeof *oms,
2081 &ms->n_bands, bands);
638a19b0
JR
2082 if (err) {
2083 return err;
2084 }
2085 ms->meter_id = ntohl(oms->meter_id);
2086 ms->flow_count = ntohl(oms->flow_count);
2087 ms->packet_in_count = ntohll(oms->packet_in_count);
2088 ms->byte_in_count = ntohll(oms->byte_in_count);
2089 ms->duration_sec = ntohl(oms->duration_sec);
2090 ms->duration_nsec = ntohl(oms->duration_nsec);
6fd6ed71 2091 ms->bands = bands->data;
638a19b0
JR
2092
2093 return 0;
2094}
2095
2096void
2097ofputil_decode_meter_features(const struct ofp_header *oh,
2098 struct ofputil_meter_features *mf)
2099{
2100 const struct ofp13_meter_features *omf = ofpmsg_body(oh);
2101
2102 mf->max_meters = ntohl(omf->max_meter);
2103 mf->band_types = ntohl(omf->band_types);
2104 mf->capabilities = ntohl(omf->capabilities);
2105 mf->max_bands = omf->max_bands;
2106 mf->max_color = omf->max_color;
2107}
2108
2109struct ofpbuf *
2110ofputil_encode_meter_features_reply(const struct ofputil_meter_features *mf,
2111 const struct ofp_header *request)
2112{
2113 struct ofpbuf *reply;
2114 struct ofp13_meter_features *omf;
2115
2116 reply = ofpraw_alloc_stats_reply(request, 0);
2117 omf = ofpbuf_put_zeros(reply, sizeof *omf);
2118
2119 omf->max_meter = htonl(mf->max_meters);
2120 omf->band_types = htonl(mf->band_types);
2121 omf->capabilities = htonl(mf->capabilities);
2122 omf->max_bands = mf->max_bands;
2123 omf->max_color = mf->max_color;
2124
2125 return reply;
2126}
2127
2128struct ofpbuf *
2129ofputil_encode_meter_mod(enum ofp_version ofp_version,
2130 const struct ofputil_meter_mod *mm)
2131{
2132 struct ofpbuf *msg;
2133
2134 struct ofp13_meter_mod *omm;
2135
2136 msg = ofpraw_alloc(OFPRAW_OFPT13_METER_MOD, ofp_version,
2137 NXM_TYPICAL_LEN + mm->meter.n_bands * 16);
2138 omm = ofpbuf_put_zeros(msg, sizeof *omm);
2139 omm->command = htons(mm->command);
2140 if (mm->command != OFPMC13_DELETE) {
2141 omm->flags = htons(mm->meter.flags);
2142 }
2143 omm->meter_id = htonl(mm->meter.meter_id);
2144
2145 ofputil_put_bands(mm->meter.n_bands, mm->meter.bands, msg);
2146
2147 ofpmsg_update_length(msg);
2148 return msg;
2149}
2150
aa6305ea
SH
2151static ovs_be16
2152ofputil_tid_command(const struct ofputil_flow_mod *fm,
2153 enum ofputil_protocol protocol)
2154{
2155 return htons(protocol & OFPUTIL_P_TID
2156 ? (fm->command & 0xff) | (fm->table_id << 8)
2157 : fm->command);
2158}
2159
2e4f5fcf 2160/* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
6cbe2c0f 2161 * 'protocol' and returns the message. */
2e4f5fcf 2162struct ofpbuf *
a9a2da38 2163ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
27527aa0 2164 enum ofputil_protocol protocol)
2e4f5fcf 2165{
0fb88c18
BP
2166 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
2167 ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version);
2e4f5fcf
BP
2168 struct ofpbuf *msg;
2169
27527aa0 2170 switch (protocol) {
75fa58f8 2171 case OFPUTIL_P_OF11_STD:
2e1ae200 2172 case OFPUTIL_P_OF12_OXM:
c37c0382 2173 case OFPUTIL_P_OF13_OXM:
42dccab5 2174 case OFPUTIL_P_OF14_OXM:
b79d45a1
BP
2175 case OFPUTIL_P_OF15_OXM:
2176 case OFPUTIL_P_OF16_OXM: {
aa6305ea 2177 struct ofp11_flow_mod *ofm;
75fa58f8 2178 int tailroom;
aa6305ea 2179
75fa58f8 2180 tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len;
0fb88c18 2181 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom);
aa6305ea 2182 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
75fa58f8
BP
2183 if ((protocol == OFPUTIL_P_OF11_STD
2184 && (fm->command == OFPFC_MODIFY ||
2185 fm->command == OFPFC_MODIFY_STRICT)
2186 && fm->cookie_mask == htonll(0))
2187 || fm->command == OFPFC_ADD) {
a5ff8823
SH
2188 ofm->cookie = fm->new_cookie;
2189 } else {
ba5cc068 2190 ofm->cookie = fm->cookie & fm->cookie_mask;
a5ff8823 2191 }
aa6305ea 2192 ofm->cookie_mask = fm->cookie_mask;
083761ad 2193 if (fm->table_id != OFPTT_ALL
0e197060
BP
2194 || (protocol != OFPUTIL_P_OF11_STD
2195 && (fm->command == OFPFC_DELETE ||
2196 fm->command == OFPFC_DELETE_STRICT))) {
2197 ofm->table_id = fm->table_id;
2198 } else {
2199 ofm->table_id = 0;
2200 }
aa6305ea
SH
2201 ofm->command = fm->command;
2202 ofm->idle_timeout = htons(fm->idle_timeout);
2203 ofm->hard_timeout = htons(fm->hard_timeout);
81a76618 2204 ofm->priority = htons(fm->priority);
aa6305ea
SH
2205 ofm->buffer_id = htonl(fm->buffer_id);
2206 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
7395c052 2207 ofm->out_group = htonl(fm->out_group);
0fb88c18 2208 ofm->flags = raw_flags;
ca26eb44
RB
2209 if (version >= OFP14_VERSION && fm->command == OFPFC_ADD) {
2210 ofm->importance = htons(fm->importance);
2211 } else {
2212 ofm->importance = 0;
2213 }
75fa58f8 2214 ofputil_put_ofp11_match(msg, &fm->match, protocol);
e3f8f887
JR
2215 ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg,
2216 version);
aa6305ea
SH
2217 break;
2218 }
2219
85813857
BP
2220 case OFPUTIL_P_OF10_STD:
2221 case OFPUTIL_P_OF10_STD_TID: {
3f192f23
SH
2222 struct ofp10_flow_mod *ofm;
2223
982697a4
BP
2224 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
2225 fm->ofpacts_len);
2226 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
81a76618 2227 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
623e1caf 2228 ofm->cookie = fm->new_cookie;
aa6305ea 2229 ofm->command = ofputil_tid_command(fm, protocol);
2e4f5fcf
BP
2230 ofm->idle_timeout = htons(fm->idle_timeout);
2231 ofm->hard_timeout = htons(fm->hard_timeout);
81a76618 2232 ofm->priority = htons(fm->priority);
2e4f5fcf 2233 ofm->buffer_id = htonl(fm->buffer_id);
4e022ec0 2234 ofm->out_port = htons(ofp_to_u16(fm->out_port));
0fb88c18 2235 ofm->flags = raw_flags;
e3f8f887
JR
2236 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2237 version);
27527aa0 2238 break;
3f192f23 2239 }
2e4f5fcf 2240
85813857
BP
2241 case OFPUTIL_P_OF10_NXM:
2242 case OFPUTIL_P_OF10_NXM_TID: {
3f192f23
SH
2243 struct nx_flow_mod *nfm;
2244 int match_len;
2245
982697a4
BP
2246 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
2247 NXM_TYPICAL_LEN + fm->ofpacts_len);
2248 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
aa6305ea 2249 nfm->command = ofputil_tid_command(fm, protocol);
623e1caf 2250 nfm->cookie = fm->new_cookie;
81a76618 2251 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
6fd6ed71 2252 nfm = msg->msg;
2e4f5fcf
BP
2253 nfm->idle_timeout = htons(fm->idle_timeout);
2254 nfm->hard_timeout = htons(fm->hard_timeout);
81a76618 2255 nfm->priority = htons(fm->priority);
2e4f5fcf 2256 nfm->buffer_id = htonl(fm->buffer_id);
4e022ec0 2257 nfm->out_port = htons(ofp_to_u16(fm->out_port));
0fb88c18 2258 nfm->flags = raw_flags;
2e4f5fcf 2259 nfm->match_len = htons(match_len);
e3f8f887
JR
2260 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2261 version);
27527aa0 2262 break;
3f192f23 2263 }
27527aa0
BP
2264
2265 default:
428b2edd 2266 OVS_NOT_REACHED();
2e4f5fcf
BP
2267 }
2268
982697a4 2269 ofpmsg_update_length(msg);
2e4f5fcf
BP
2270 return msg;
2271}
2272
90bf1e07 2273static enum ofperr
0157ad3a
SH
2274ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
2275 const struct ofp10_flow_stats_request *ofsr,
2276 bool aggregate)
2e4f5fcf 2277{
2e4f5fcf 2278 fsr->aggregate = aggregate;
81a76618 2279 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
4e022ec0 2280 fsr->out_port = u16_to_ofp(ntohs(ofsr->out_port));
30ef36c6 2281 fsr->out_group = OFPG_ANY;
2e4f5fcf 2282 fsr->table_id = ofsr->table_id;
e729e793 2283 fsr->cookie = fsr->cookie_mask = htonll(0);
2e4f5fcf
BP
2284
2285 return 0;
2286}
2287
0157ad3a
SH
2288static enum ofperr
2289ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
8d8ab6c2 2290 struct ofpbuf *b, bool aggregate,
3cddeff0
YHW
2291 const struct tun_table *tun_table,
2292 const struct vl_mff_map *vl_mff_map)
0157ad3a
SH
2293{
2294 const struct ofp11_flow_stats_request *ofsr;
2295 enum ofperr error;
2296
2297 ofsr = ofpbuf_pull(b, sizeof *ofsr);
2298 fsr->aggregate = aggregate;
2299 fsr->table_id = ofsr->table_id;
2300 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
2301 if (error) {
2302 return error;
2303 }
7395c052 2304 fsr->out_group = ntohl(ofsr->out_group);
0157ad3a
SH
2305 fsr->cookie = ofsr->cookie;
2306 fsr->cookie_mask = ofsr->cookie_mask;
3cddeff0
YHW
2307 error = ofputil_pull_ofp11_match(b, tun_table, vl_mff_map, &fsr->match,
2308 NULL);
0157ad3a
SH
2309 if (error) {
2310 return error;
2311 }
2312
2313 return 0;
2314}
2315
90bf1e07 2316static enum ofperr
81d1ea94 2317ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
8d8ab6c2 2318 struct ofpbuf *b, bool aggregate,
3cddeff0
YHW
2319 const struct tun_table *tun_table,
2320 const struct vl_mff_map *vl_mff_map)
2e4f5fcf
BP
2321{
2322 const struct nx_flow_stats_request *nfsr;
90bf1e07 2323 enum ofperr error;
2e4f5fcf 2324
982697a4 2325 nfsr = ofpbuf_pull(b, sizeof *nfsr);
81a76618 2326 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
3cddeff0
YHW
2327 &fsr->cookie, &fsr->cookie_mask, tun_table,
2328 vl_mff_map);
2e4f5fcf
BP
2329 if (error) {
2330 return error;
2331 }
6fd6ed71 2332 if (b->size) {
90bf1e07 2333 return OFPERR_OFPBRC_BAD_LEN;
2e4f5fcf
BP
2334 }
2335
2336 fsr->aggregate = aggregate;
4e022ec0 2337 fsr->out_port = u16_to_ofp(ntohs(nfsr->out_port));
30ef36c6 2338 fsr->out_group = OFPG_ANY;
2e4f5fcf
BP
2339 fsr->table_id = nfsr->table_id;
2340
2341 return 0;
2342}
2343
e8f9a7bb 2344/* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
e016fb63
BP
2345 * 'port' and 'queue', suitable for OpenFlow version 'version'.
2346 *
2347 * 'queue' is honored only for OpenFlow 1.4 and later; older versions always
2348 * request all queues. */
e8f9a7bb
VG
2349struct ofpbuf *
2350ofputil_encode_queue_get_config_request(enum ofp_version version,
e016fb63
BP
2351 ofp_port_t port,
2352 uint32_t queue)
e8f9a7bb
VG
2353{
2354 struct ofpbuf *request;
2355
2356 if (version == OFP10_VERSION) {
2357 struct ofp10_queue_get_config_request *qgcr10;
2358
2359 request = ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST,
2360 version, 0);
2361 qgcr10 = ofpbuf_put_zeros(request, sizeof *qgcr10);
2362 qgcr10->port = htons(ofp_to_u16(port));
e016fb63 2363 } else if (version < OFP14_VERSION) {
e8f9a7bb
VG
2364 struct ofp11_queue_get_config_request *qgcr11;
2365
2366 request = ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST,
2367 version, 0);
2368 qgcr11 = ofpbuf_put_zeros(request, sizeof *qgcr11);
2369 qgcr11->port = ofputil_port_to_ofp11(port);
e016fb63
BP
2370 } else {
2371 struct ofp14_queue_desc_request *qdr14;
2372
2373 request = ofpraw_alloc(OFPRAW_OFPST14_QUEUE_DESC_REQUEST,
2374 version, 0);
2375 qdr14 = ofpbuf_put_zeros(request, sizeof *qdr14);
2376 qdr14->port = ofputil_port_to_ofp11(port);
2377 qdr14->queue = htonl(queue);
e8f9a7bb
VG
2378 }
2379
2380 return request;
2381}
2382
2383/* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2384 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2385 * code. */
2386enum ofperr
2387ofputil_decode_queue_get_config_request(const struct ofp_header *oh,
e016fb63 2388 ofp_port_t *port, uint32_t *queue)
e8f9a7bb
VG
2389{
2390 const struct ofp10_queue_get_config_request *qgcr10;
2391 const struct ofp11_queue_get_config_request *qgcr11;
e016fb63 2392 const struct ofp14_queue_desc_request *qdr14;
0a2869d5
BP
2393 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
2394 enum ofpraw raw = ofpraw_pull_assert(&b);
e8f9a7bb
VG
2395
2396 switch ((int) raw) {
2397 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
6fd6ed71 2398 qgcr10 = b.data;
e8f9a7bb 2399 *port = u16_to_ofp(ntohs(qgcr10->port));
e016fb63 2400 *queue = OFPQ_ALL;
56085be5 2401 break;
e8f9a7bb
VG
2402
2403 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
6fd6ed71 2404 qgcr11 = b.data;
e016fb63 2405 *queue = OFPQ_ALL;
56085be5
BP
2406 enum ofperr error = ofputil_port_from_ofp11(qgcr11->port, port);
2407 if (error || *port == OFPP_ANY) {
2408 return error;
2409 }
2410 break;
2411
e016fb63
BP
2412 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST:
2413 qdr14 = b.data;
2414 *queue = ntohl(qdr14->queue);
2415 return ofputil_port_from_ofp11(qdr14->port, port);
2416
56085be5
BP
2417 default:
2418 OVS_NOT_REACHED();
e8f9a7bb
VG
2419 }
2420
56085be5
BP
2421 return (ofp_to_u16(*port) < ofp_to_u16(OFPP_MAX)
2422 ? 0
2423 : OFPERR_OFPQOFC_BAD_PORT);
e8f9a7bb
VG
2424}
2425
2426/* Constructs and returns the beginning of a reply to
e016fb63
BP
2427 * OFPT_QUEUE_GET_CONFIG_REQUEST or OFPMP_QUEUE_DESC request 'oh'. The caller
2428 * may append information about individual queues with
2429 * ofputil_append_queue_get_config_reply(). */
2430void
2431ofputil_start_queue_get_config_reply(const struct ofp_header *request,
2432 struct ovs_list *replies)
e8f9a7bb 2433{
e8f9a7bb
VG
2434 struct ofpbuf *reply;
2435 enum ofperr error;
e8f9a7bb 2436 ofp_port_t port;
e016fb63 2437 uint32_t queue;
e8f9a7bb 2438
e016fb63 2439 error = ofputil_decode_queue_get_config_request(request, &port, &queue);
e8f9a7bb
VG
2440 ovs_assert(!error);
2441
e016fb63 2442 enum ofpraw raw = ofpraw_decode_assert(request);
e8f9a7bb
VG
2443 switch ((int) raw) {
2444 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2445 reply = ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY,
e016fb63
BP
2446 request, 0);
2447 struct ofp10_queue_get_config_reply *qgcr10
2448 = ofpbuf_put_zeros(reply, sizeof *qgcr10);
e8f9a7bb
VG
2449 qgcr10->port = htons(ofp_to_u16(port));
2450 break;
2451
2452 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2453 reply = ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY,
e016fb63
BP
2454 request, 0);
2455 struct ofp11_queue_get_config_reply *qgcr11
2456 = ofpbuf_put_zeros(reply, sizeof *qgcr11);
e8f9a7bb
VG
2457 qgcr11->port = ofputil_port_to_ofp11(port);
2458 break;
2459
e016fb63
BP
2460 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST:
2461 reply = ofpraw_alloc_stats_reply(request, 0);
2462 break;
2463
e8f9a7bb 2464 default:
428b2edd 2465 OVS_NOT_REACHED();
e8f9a7bb
VG
2466 }
2467
417e7e66
BW
2468 ovs_list_init(replies);
2469 ovs_list_push_back(replies, &reply->list_node);
e8f9a7bb
VG
2470}
2471
2472static void
d2e5fa1f
BP
2473put_ofp10_queue_rate(struct ofpbuf *reply,
2474 enum ofp10_queue_properties property, uint16_t rate)
e8f9a7bb
VG
2475{
2476 if (rate != UINT16_MAX) {
d2e5fa1f 2477 struct ofp10_queue_prop_rate *oqpr;
e8f9a7bb
VG
2478
2479 oqpr = ofpbuf_put_zeros(reply, sizeof *oqpr);
2480 oqpr->prop_header.property = htons(property);
2481 oqpr->prop_header.len = htons(sizeof *oqpr);
2482 oqpr->rate = htons(rate);
2483 }
2484}
2485
e016fb63
BP
2486static void
2487put_ofp14_queue_rate(struct ofpbuf *reply,
2488 enum ofp14_queue_desc_prop_type type, uint16_t rate)
2489{
2490 if (rate != UINT16_MAX) {
2491 ofpprop_put_u16(reply, type, rate);
2492 }
2493}
2494
e8f9a7bb 2495void
e016fb63
BP
2496ofputil_append_queue_get_config_reply(const struct ofputil_queue_config *qc,
2497 struct ovs_list *replies)
e8f9a7bb 2498{
e016fb63 2499 enum ofp_version ofp_version = ofpmp_version(replies);
417e7e66 2500 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
e016fb63
BP
2501 size_t start_ofs = reply->size;
2502 size_t len_ofs;
e8f9a7bb
VG
2503 ovs_be16 *len;
2504
e016fb63
BP
2505 if (ofp_version < OFP14_VERSION) {
2506 if (ofp_version < OFP12_VERSION) {
2507 struct ofp10_packet_queue *opq10;
e8f9a7bb 2508
e016fb63
BP
2509 opq10 = ofpbuf_put_zeros(reply, sizeof *opq10);
2510 opq10->queue_id = htonl(qc->queue);
2511 len_ofs = (char *) &opq10->len - (char *) reply->data;
2512 } else {
2513 struct ofp12_packet_queue *opq12;
e8f9a7bb 2514
e016fb63
BP
2515 opq12 = ofpbuf_put_zeros(reply, sizeof *opq12);
2516 opq12->port = ofputil_port_to_ofp11(qc->port);
2517 opq12->queue_id = htonl(qc->queue);
2518 len_ofs = (char *) &opq12->len - (char *) reply->data;
2519 }
e8f9a7bb 2520
e016fb63
BP
2521 put_ofp10_queue_rate(reply, OFPQT10_MIN_RATE, qc->min_rate);
2522 put_ofp10_queue_rate(reply, OFPQT11_MAX_RATE, qc->max_rate);
2523 } else {
2524 struct ofp14_queue_desc *oqd = ofpbuf_put_zeros(reply, sizeof *oqd);
2525 oqd->port_no = ofputil_port_to_ofp11(qc->port);
2526 oqd->queue_id = htonl(qc->queue);
2527 len_ofs = (char *) &oqd->len - (char *) reply->data;
2528 put_ofp14_queue_rate(reply, OFPQDPT14_MIN_RATE, qc->min_rate);
2529 put_ofp14_queue_rate(reply, OFPQDPT14_MAX_RATE, qc->max_rate);
2530 }
e8f9a7bb
VG
2531
2532 len = ofpbuf_at(reply, len_ofs, sizeof *len);
6fd6ed71 2533 *len = htons(reply->size - start_ofs);
e8f9a7bb 2534
e016fb63
BP
2535 if (ofp_version >= OFP14_VERSION) {
2536 ofpmp_postappend(replies, start_ofs);
e8f9a7bb 2537 }
e8f9a7bb
VG
2538}
2539
2540static enum ofperr
d2e5fa1f
BP
2541parse_ofp10_queue_rate(const struct ofp10_queue_prop_header *hdr,
2542 uint16_t *rate)
e8f9a7bb 2543{
d2e5fa1f 2544 const struct ofp10_queue_prop_rate *oqpr;
e8f9a7bb
VG
2545
2546 if (hdr->len == htons(sizeof *oqpr)) {
d2e5fa1f 2547 oqpr = (const struct ofp10_queue_prop_rate *) hdr;
e8f9a7bb
VG
2548 *rate = ntohs(oqpr->rate);
2549 return 0;
2550 } else {
2551 return OFPERR_OFPBRC_BAD_LEN;
2552 }
2553}
2554
e016fb63
BP
2555static int
2556ofputil_pull_queue_get_config_reply10(struct ofpbuf *msg,
2557 struct ofputil_queue_config *queue)
e8f9a7bb 2558{
e016fb63
BP
2559 const struct ofp_header *oh = msg->header;
2560 unsigned int opq_len; /* Length of protocol-specific queue header. */
2561 unsigned int len; /* Total length of queue + properties. */
e8f9a7bb 2562
e016fb63
BP
2563 /* Obtain the port number from the message header. */
2564 if (oh->version == OFP10_VERSION) {
2565 const struct ofp10_queue_get_config_reply *oqgcr10 = msg->msg;
2566 queue->port = u16_to_ofp(ntohs(oqgcr10->port));
2567 } else {
2568 const struct ofp11_queue_get_config_reply *oqgcr11 = msg->msg;
2569 enum ofperr error = ofputil_port_from_ofp11(oqgcr11->port,
2570 &queue->port);
2571 if (error) {
2572 return error;
2573 }
e8f9a7bb
VG
2574 }
2575
e016fb63 2576 /* Pull off the queue header and get the queue number and length. */
e8f9a7bb
VG
2577 if (oh->version < OFP12_VERSION) {
2578 const struct ofp10_packet_queue *opq10;
e016fb63 2579 opq10 = ofpbuf_try_pull(msg, sizeof *opq10);
e8f9a7bb
VG
2580 if (!opq10) {
2581 return OFPERR_OFPBRC_BAD_LEN;
2582 }
e016fb63 2583 queue->queue = ntohl(opq10->queue_id);
e8f9a7bb
VG
2584 len = ntohs(opq10->len);
2585 opq_len = sizeof *opq10;
2586 } else {
2587 const struct ofp12_packet_queue *opq12;
e016fb63 2588 opq12 = ofpbuf_try_pull(msg, sizeof *opq12);
e8f9a7bb
VG
2589 if (!opq12) {
2590 return OFPERR_OFPBRC_BAD_LEN;
2591 }
e016fb63 2592 queue->queue = ntohl(opq12->queue_id);
e8f9a7bb
VG
2593 len = ntohs(opq12->len);
2594 opq_len = sizeof *opq12;
2595 }
2596
e016fb63
BP
2597 /* Length check. */
2598 if (len < opq_len || len > msg->size + opq_len || len % 8) {
e8f9a7bb
VG
2599 return OFPERR_OFPBRC_BAD_LEN;
2600 }
2601 len -= opq_len;
2602
e016fb63
BP
2603 /* Pull properties. The format of these properties differs from used in
2604 * OF1.4+ so we can't use the common property functions. */
e8f9a7bb 2605 while (len > 0) {
d2e5fa1f 2606 const struct ofp10_queue_prop_header *hdr;
e8f9a7bb
VG
2607 unsigned int property;
2608 unsigned int prop_len;
2609 enum ofperr error = 0;
2610
e016fb63 2611 hdr = ofpbuf_at_assert(msg, 0, sizeof *hdr);
e8f9a7bb 2612 prop_len = ntohs(hdr->len);
fafbfa6e 2613 if (prop_len < sizeof *hdr || prop_len > len || prop_len % 8) {
e8f9a7bb
VG
2614 return OFPERR_OFPBRC_BAD_LEN;
2615 }
2616
2617 property = ntohs(hdr->property);
2618 switch (property) {
d2e5fa1f
BP
2619 case OFPQT10_MIN_RATE:
2620 error = parse_ofp10_queue_rate(hdr, &queue->min_rate);
e8f9a7bb
VG
2621 break;
2622
d2e5fa1f
BP
2623 case OFPQT11_MAX_RATE:
2624 error = parse_ofp10_queue_rate(hdr, &queue->max_rate);
e8f9a7bb
VG
2625 break;
2626
2627 default:
2628 VLOG_INFO_RL(&bad_ofmsg_rl, "unknown queue property %u", property);
2629 break;
2630 }
2631 if (error) {
2632 return error;
2633 }
2634
e016fb63 2635 ofpbuf_pull(msg, prop_len);
e8f9a7bb
VG
2636 len -= prop_len;
2637 }
2638 return 0;
2639}
2640
e016fb63
BP
2641static int
2642ofputil_pull_queue_get_config_reply14(struct ofpbuf *msg,
2643 struct ofputil_queue_config *queue)
2644{
2645 struct ofp14_queue_desc *oqd14 = ofpbuf_try_pull(msg, sizeof *oqd14);
2646 if (!oqd14) {
2647 return OFPERR_OFPBRC_BAD_LEN;
2648 }
2649 enum ofperr error = ofputil_port_from_ofp11(oqd14->port_no, &queue->port);
2650 if (error) {
2651 return error;
2652 }
2653 queue->queue = ntohl(oqd14->queue_id);
2654
2655 /* Length check. */
2656 unsigned int len = ntohs(oqd14->len);
2657 if (len < sizeof *oqd14 || len > msg->size + sizeof *oqd14 || len % 8) {
2658 return OFPERR_OFPBRC_BAD_LEN;
2659 }
2660 len -= sizeof *oqd14;
2661
0a2869d5
BP
2662 struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
2663 len);
e016fb63
BP
2664 while (properties.size > 0) {
2665 struct ofpbuf payload;
2666 uint64_t type;
2667
2668 error = ofpprop_pull(&properties, &payload, &type);
2669 if (error) {
2670 return error;
2671 }
2672
2673 switch (type) {
2674 case OFPQDPT14_MIN_RATE:
2675 error = ofpprop_parse_u16(&payload, &queue->min_rate);
2676 break;
2677
2678 case OFPQDPT14_MAX_RATE:
2679 error = ofpprop_parse_u16(&payload, &queue->max_rate);
2680 break;
2681
2682 default:
2683 error = OFPPROP_UNKNOWN(true, "queue desc", type);
2684 break;
2685 }
2686
2687 if (error) {
2688 return error;
2689 }
2690 }
2691
2692 return 0;
2693}
2694
2695/* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2696 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2697 * must already have pulled off the main header.
2698 *
2699 * This function returns EOF if the last queue has already been decoded, 0 if a
2700 * queue was successfully decoded into '*queue', or an ofperr if there was a
2701 * problem decoding 'reply'. */
2702int
2703ofputil_pull_queue_get_config_reply(struct ofpbuf *msg,
2704 struct ofputil_queue_config *queue)
2705{
2706 enum ofpraw raw;
2707 if (!msg->header) {
2708 /* Pull OpenFlow header. */
2709 raw = ofpraw_pull_assert(msg);
2710
2711 /* Pull protocol-specific ofp_queue_get_config_reply header (OF1.4
2712 * doesn't have one at all). */
2713 if (raw == OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY) {
2714 ofpbuf_pull(msg, sizeof(struct ofp10_queue_get_config_reply));
2715 } else if (raw == OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY) {
2716 ofpbuf_pull(msg, sizeof(struct ofp11_queue_get_config_reply));
2717 } else {
2718 ovs_assert(raw == OFPRAW_OFPST14_QUEUE_DESC_REPLY);
2719 }
2720 } else {
2721 raw = ofpraw_decode_assert(msg->header);
2722 }
2723
2724 queue->min_rate = UINT16_MAX;
2725 queue->max_rate = UINT16_MAX;
2726
2727 if (!msg->size) {
2728 return EOF;
2729 } else if (raw == OFPRAW_OFPST14_QUEUE_DESC_REPLY) {
2730 return ofputil_pull_queue_get_config_reply14(msg, queue);
2731 } else {
2732 return ofputil_pull_queue_get_config_reply10(msg, queue);
2733 }
2734}
2735
2e4f5fcf 2736/* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
b78f6b77 2737 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
3cddeff0
YHW
2738 * successful, otherwise an OpenFlow error code.
2739 *
2740 * 'vl_mff_map' is an optional parameter that is used to validate the length
2741 * of variable length mf_fields in 'match'. If it is not provided, the
2742 * default mf_fields with maximum length will be used. */
90bf1e07 2743enum ofperr
81d1ea94 2744ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
8d8ab6c2 2745 const struct ofp_header *oh,
3cddeff0
YHW
2746 const struct tun_table *tun_table,
2747 const struct vl_mff_map *vl_mff_map)
2e4f5fcf 2748{
0a2869d5
BP
2749 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
2750 enum ofpraw raw = ofpraw_pull_assert(&b);
982697a4 2751 switch ((int) raw) {
cfc23141 2752 case OFPRAW_OFPST10_FLOW_REQUEST:
6fd6ed71 2753 return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
2e4f5fcf 2754
617da9cd 2755 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
6fd6ed71 2756 return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
0157ad3a
SH
2757
2758 case OFPRAW_OFPST11_FLOW_REQUEST:
3cddeff0
YHW
2759 return ofputil_decode_ofpst11_flow_request(fsr, &b, false, tun_table,
2760 vl_mff_map);
2e4f5fcf 2761
617da9cd 2762 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
3cddeff0
YHW
2763 return ofputil_decode_ofpst11_flow_request(fsr, &b, true, tun_table,
2764 vl_mff_map);
617da9cd 2765
982697a4 2766 case OFPRAW_NXST_FLOW_REQUEST:
3cddeff0
YHW
2767 return ofputil_decode_nxst_flow_request(fsr, &b, false, tun_table,
2768 vl_mff_map);
2e4f5fcf 2769
982697a4 2770 case OFPRAW_NXST_AGGREGATE_REQUEST:
3cddeff0
YHW
2771 return ofputil_decode_nxst_flow_request(fsr, &b, true, tun_table,
2772 vl_mff_map);
2e4f5fcf
BP
2773
2774 default:
2775 /* Hey, the caller lied. */
428b2edd 2776 OVS_NOT_REACHED();
2e4f5fcf
BP
2777 }
2778}
2779
2780/* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
4ffd1b43 2781 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
27527aa0 2782 * 'protocol', and returns the message. */
2e4f5fcf 2783struct ofpbuf *
81d1ea94 2784ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
27527aa0 2785 enum ofputil_protocol protocol)
2e4f5fcf
BP
2786{
2787 struct ofpbuf *msg;
982697a4 2788 enum ofpraw raw;
2e4f5fcf 2789
27527aa0 2790 switch (protocol) {
75fa58f8 2791 case OFPUTIL_P_OF11_STD:
2e1ae200 2792 case OFPUTIL_P_OF12_OXM:
c37c0382 2793 case OFPUTIL_P_OF13_OXM:
42dccab5 2794 case OFPUTIL_P_OF14_OXM:
b79d45a1
BP
2795 case OFPUTIL_P_OF15_OXM:
2796 case OFPUTIL_P_OF16_OXM: {
06516c65
SH
2797 struct ofp11_flow_stats_request *ofsr;
2798
2799 raw = (fsr->aggregate
617da9cd 2800 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
cfc23141 2801 : OFPRAW_OFPST11_FLOW_REQUEST);
2e1ae200 2802 msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
75fa58f8 2803 ofputil_match_typical_len(protocol));
06516c65
SH
2804 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2805 ofsr->table_id = fsr->table_id;
2806 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
7395c052 2807 ofsr->out_group = htonl(fsr->out_group);
06516c65
SH
2808 ofsr->cookie = fsr->cookie;
2809 ofsr->cookie_mask = fsr->cookie_mask;
75fa58f8 2810 ofputil_put_ofp11_match(msg, &fsr->match, protocol);
06516c65
SH
2811 break;
2812 }
2813
85813857
BP
2814 case OFPUTIL_P_OF10_STD:
2815 case OFPUTIL_P_OF10_STD_TID: {
e2b9ac44 2816 struct ofp10_flow_stats_request *ofsr;
2e4f5fcf 2817
982697a4 2818 raw = (fsr->aggregate
617da9cd 2819 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
cfc23141 2820 : OFPRAW_OFPST10_FLOW_REQUEST);
982697a4
BP
2821 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
2822 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
81a76618 2823 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
2e4f5fcf 2824 ofsr->table_id = fsr->table_id;
4e022ec0 2825 ofsr->out_port = htons(ofp_to_u16(fsr->out_port));
27527aa0
BP
2826 break;
2827 }
2828
85813857
BP
2829 case OFPUTIL_P_OF10_NXM:
2830 case OFPUTIL_P_OF10_NXM_TID: {
2e4f5fcf
BP
2831 struct nx_flow_stats_request *nfsr;
2832 int match_len;
2833
982697a4
BP
2834 raw = (fsr->aggregate
2835 ? OFPRAW_NXST_AGGREGATE_REQUEST
2836 : OFPRAW_NXST_FLOW_REQUEST);
06516c65 2837 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
982697a4 2838 ofpbuf_put_zeros(msg, sizeof *nfsr);
7623f4dd 2839 match_len = nx_put_match(msg, &fsr->match,
e729e793 2840 fsr->cookie, fsr->cookie_mask);
2e4f5fcf 2841
6fd6ed71 2842 nfsr = msg->msg;
4e022ec0 2843 nfsr->out_port = htons(ofp_to_u16(fsr->out_port));
2e4f5fcf
BP
2844 nfsr->match_len = htons(match_len);
2845 nfsr->table_id = fsr->table_id;
27527aa0
BP
2846 break;
2847 }
2848
2849 default:
428b2edd 2850 OVS_NOT_REACHED();
2e4f5fcf
BP
2851 }
2852
2853 return msg;
2854}
d1e2cf21 2855
4ffd1b43 2856/* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
b78f6b77 2857 * ofputil_flow_stats in 'fs'.
4ffd1b43
BP
2858 *
2859 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2860 * OpenFlow message. Calling this function multiple times for a single 'msg'
2861 * iterates through the replies. The caller must initially leave 'msg''s layer
2862 * pointers null and not modify them between calls.
2863 *
f27f2134
BP
2864 * Most switches don't send the values needed to populate fs->idle_age and
2865 * fs->hard_age, so those members will usually be set to 0. If the switch from
2866 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2867 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2868 * 'idle_age' and 'hard_age' members in 'fs'.
2869 *
f25d0cf3
BP
2870 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2871 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2872 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2873 *
4ffd1b43
BP
2874 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2875 * otherwise a positive errno value. */
2876int
2877ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
f27f2134 2878 struct ofpbuf *msg,
f25d0cf3
BP
2879 bool flow_age_extension,
2880 struct ofpbuf *ofpacts)
4ffd1b43 2881{
0fb88c18 2882 const struct ofp_header *oh;
8f2cded4 2883 size_t instructions_len;
982697a4
BP
2884 enum ofperr error;
2885 enum ofpraw raw;
4ffd1b43 2886
6fd6ed71 2887 error = (msg->header ? ofpraw_decode(&raw, msg->header)
982697a4
BP
2888 : ofpraw_pull(&raw, msg));
2889 if (error) {
2890 return error;
4ffd1b43 2891 }
6fd6ed71 2892 oh = msg->header;
4ffd1b43 2893
6fd6ed71 2894 if (!msg->size) {
4ffd1b43 2895 return EOF;
2e1ae200
JR
2896 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY
2897 || raw == OFPRAW_OFPST13_FLOW_REPLY) {
6ec5f0c5
SH
2898 const struct ofp11_flow_stats *ofs;
2899 size_t length;
2900 uint16_t padded_match_len;
2901
2902 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2903 if (!ofs) {
437d0d22 2904 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
6fd6ed71 2905 "bytes at end", msg->size);
6ec5f0c5
SH
2906 return EINVAL;
2907 }
2908
2909 length = ntohs(ofs->length);
2910 if (length < sizeof *ofs) {
2911 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
34582733 2912 "length %"PRIuSIZE, length);
6ec5f0c5
SH
2913 return EINVAL;
2914 }
2915
3cddeff0 2916 if (ofputil_pull_ofp11_match(msg, NULL, NULL, &fs->match,
8d8ab6c2 2917 &padded_match_len)) {
6ec5f0c5
SH
2918 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
2919 return EINVAL;
2920 }
8f2cded4 2921 instructions_len = length - sizeof *ofs - padded_match_len;
6ec5f0c5 2922
81a76618 2923 fs->priority = ntohs(ofs->priority);
6ec5f0c5
SH
2924 fs->table_id = ofs->table_id;
2925 fs->duration_sec = ntohl(ofs->duration_sec);
2926 fs->duration_nsec = ntohl(ofs->duration_nsec);
2927 fs->idle_timeout = ntohs(ofs->idle_timeout);
2928 fs->hard_timeout = ntohs(ofs->hard_timeout);
ca26eb44
RB
2929 if (oh->version >= OFP14_VERSION) {
2930 fs->importance = ntohs(ofs->importance);
2931 } else {
2932 fs->importance = 0;
2933 }
0fb88c18
BP
2934 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
2935 error = ofputil_decode_flow_mod_flags(ofs->flags, -1, oh->version,
2936 &fs->flags);
2937 if (error) {
2938 return error;
2939 }
2940 } else {
2941 fs->flags = 0;
2942 }
6ec5f0c5
SH
2943 fs->idle_age = -1;
2944 fs->hard_age = -1;
2945 fs->cookie = ofs->cookie;
2946 fs->packet_count = ntohll(ofs->packet_count);
2947 fs->byte_count = ntohll(ofs->byte_count);
cfc23141 2948 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
e2b9ac44 2949 const struct ofp10_flow_stats *ofs;
4ffd1b43
BP
2950 size_t length;
2951
2952 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2953 if (!ofs) {
437d0d22 2954 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
6fd6ed71 2955 "bytes at end", msg->size);
4ffd1b43
BP
2956 return EINVAL;
2957 }
2958
2959 length = ntohs(ofs->length);
2960 if (length < sizeof *ofs) {
2961 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
34582733 2962 "length %"PRIuSIZE, length);
4ffd1b43
BP
2963 return EINVAL;
2964 }
8f2cded4 2965 instructions_len = length - sizeof *ofs;
4ffd1b43
BP
2966
2967 fs->cookie = get_32aligned_be64(&ofs->cookie);
81a76618
BP
2968 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
2969 fs->priority = ntohs(ofs->priority);
4ffd1b43
BP
2970 fs->table_id = ofs->table_id;
2971 fs->duration_sec = ntohl(ofs->duration_sec);
2972 fs->duration_nsec = ntohl(ofs->duration_nsec);
2973 fs->idle_timeout = ntohs(ofs->idle_timeout);
2974 fs->hard_timeout = ntohs(ofs->hard_timeout);
ca26eb44 2975 fs->importance = 0;
f27f2134
BP
2976 fs->idle_age = -1;
2977 fs->hard_age = -1;
4ffd1b43
BP
2978 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
2979 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
2e1ae200 2980 fs->flags = 0;
982697a4 2981 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
4ffd1b43 2982 const struct nx_flow_stats *nfs;
8f2cded4 2983 size_t match_len, length;
4ffd1b43
BP
2984
2985 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
2986 if (!nfs) {
437d0d22 2987 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %"PRIu32" leftover "
6fd6ed71 2988 "bytes at end", msg->size);
4ffd1b43
BP
2989 return EINVAL;
2990 }
2991
2992 length = ntohs(nfs->length);
2993 match_len = ntohs(nfs->match_len);
2994 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
34582733
AS
2995 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%"PRIuSIZE" "
2996 "claims invalid length %"PRIuSIZE, match_len, length);
4ffd1b43
BP
2997 return EINVAL;
2998 }
3cddeff0
YHW
2999 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL, NULL,
3000 NULL)) {
4ffd1b43
BP
3001 return EINVAL;
3002 }
8f2cded4 3003 instructions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
4ffd1b43
BP
3004
3005 fs->cookie = nfs->cookie;
3006 fs->table_id = nfs->table_id;
3007 fs->duration_sec = ntohl(nfs->duration_sec);
3008 fs->duration_nsec = ntohl(nfs->duration_nsec);
81a76618 3009 fs->priority = ntohs(nfs->priority);
4ffd1b43
BP
3010 fs->idle_timeout = ntohs(nfs->idle_timeout);
3011 fs->hard_timeout = ntohs(nfs->hard_timeout);
ca26eb44 3012 fs->importance = 0;
f27f2134
BP
3013 fs->idle_age = -1;
3014 fs->hard_age = -1;
3015 if (flow_age_extension) {
3016 if (nfs->idle_age) {
3017 fs->idle_age = ntohs(nfs->idle_age) - 1;
3018 }
3019 if (nfs->hard_age) {
3020 fs->hard_age = ntohs(nfs->hard_age) - 1;
3021 }
3022 }
4ffd1b43
BP
3023 fs->packet_count = ntohll(nfs->packet_count);
3024 fs->byte_count = ntohll(nfs->byte_count);
2e1ae200 3025 fs->flags = 0;
4ffd1b43 3026 } else {
428b2edd 3027 OVS_NOT_REACHED();
4ffd1b43
BP
3028 }
3029
8f2cded4 3030 if (ofpacts_pull_openflow_instructions(msg, instructions_len, oh->version,
5c7c16d8 3031 NULL, NULL, ofpacts)) {
8f2cded4
BP
3032 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
3033 return EINVAL;
3034 }
6fd6ed71
PS
3035 fs->ofpacts = ofpacts->data;
3036 fs->ofpacts_len = ofpacts->size;
f25d0cf3 3037
4ffd1b43
BP
3038 return 0;
3039}
3040
5e9d0469
BP
3041/* Returns 'count' unchanged except that UINT64_MAX becomes 0.
3042 *
3043 * We use this in situations where OVS internally uses UINT64_MAX to mean
3044 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
3045static uint64_t
3046unknown_to_zero(uint64_t count)
3047{
3048 return count != UINT64_MAX ? count : 0;
3049}
3050
349adfb2
BP
3051/* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
3052 * those already present in the list of ofpbufs in 'replies'. 'replies' should
7395c052 3053 * have been initialized with ofpmp_init(). */
349adfb2
BP
3054void
3055ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
8d8ab6c2
JG
3056 struct ovs_list *replies,
3057 const struct tun_table *tun_table)
349adfb2 3058{
8d8ab6c2
JG
3059 struct ofputil_flow_stats *fs_ = CONST_CAST(struct ofputil_flow_stats *,
3060 fs);
3061 const struct tun_table *orig_tun_table;
417e7e66 3062 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
6fd6ed71 3063 size_t start_ofs = reply->size;
e28ac5cf
BP
3064 enum ofp_version version = ofpmp_version(replies);
3065 enum ofpraw raw = ofpmp_decode_raw(replies);
349adfb2 3066
8d8ab6c2
JG
3067 orig_tun_table = fs->match.flow.tunnel.metadata.tab;
3068 fs_->match.flow.tunnel.metadata.tab = tun_table;
3069
2e1ae200 3070 if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
22a86f18
SH
3071 struct ofp11_flow_stats *ofs;
3072
3073 ofpbuf_put_uninit(reply, sizeof *ofs);
9d84066c 3074 oxm_put_match(reply, &fs->match, version);
e3f8f887
JR
3075 ofpacts_put_openflow_instructions(fs->ofpacts, fs->ofpacts_len, reply,
3076 version);
22a86f18
SH
3077
3078 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
6fd6ed71 3079 ofs->length = htons(reply->size - start_ofs);
22a86f18
SH
3080 ofs->table_id = fs->table_id;
3081 ofs->pad = 0;
3082 ofs->duration_sec = htonl(fs->duration_sec);
3083 ofs->duration_nsec = htonl(fs->duration_nsec);
81a76618 3084 ofs->priority = htons(fs->priority);
22a86f18
SH
3085 ofs->idle_timeout = htons(fs->idle_timeout);
3086 ofs->hard_timeout = htons(fs->hard_timeout);
ca26eb44
RB
3087 if (version >= OFP14_VERSION) {
3088 ofs->importance = htons(fs->importance);
3089 } else {
3090 ofs->importance = 0;
3091 }
0fb88c18 3092 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
e3f8f887 3093 ofs->flags = ofputil_encode_flow_mod_flags(fs->flags, version);
0fb88c18
BP
3094 } else {
3095 ofs->flags = 0;
3096 }
22a86f18
SH
3097 memset(ofs->pad2, 0, sizeof ofs->pad2);
3098 ofs->cookie = fs->cookie;
3099 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
3100 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
3101 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
e2b9ac44 3102 struct ofp10_flow_stats *ofs;
349adfb2 3103
1a59dc2c 3104 ofpbuf_put_uninit(reply, sizeof *ofs);
e3f8f887
JR
3105 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3106 version);
1a59dc2c 3107 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
6fd6ed71 3108 ofs->length = htons(reply->size - start_ofs);
349adfb2
BP
3109 ofs->table_id = fs->table_id;
3110 ofs->pad = 0;
81a76618 3111 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
349adfb2
BP
3112 ofs->duration_sec = htonl(fs->duration_sec);
3113 ofs->duration_nsec = htonl(fs->duration_nsec);
81a76618 3114 ofs->priority = htons(fs->priority);
349adfb2
BP
3115 ofs->idle_timeout = htons(fs->idle_timeout);
3116 ofs->hard_timeout = htons(fs->hard_timeout);
3117 memset(ofs->pad2, 0, sizeof ofs->pad2);
3118 put_32aligned_be64(&ofs->cookie, fs->cookie);
5e9d0469
BP
3119 put_32aligned_be64(&ofs->packet_count,
3120 htonll(unknown_to_zero(fs->packet_count)));
3121 put_32aligned_be64(&ofs->byte_count,
3122 htonll(unknown_to_zero(fs->byte_count)));
982697a4 3123 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
349adfb2 3124 struct nx_flow_stats *nfs;
1a59dc2c 3125 int match_len;
349adfb2 3126
1a59dc2c 3127 ofpbuf_put_uninit(reply, sizeof *nfs);
81a76618 3128 match_len = nx_put_match(reply, &fs->match, 0, 0);
e3f8f887
JR
3129 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3130 version);
1a59dc2c 3131 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
6fd6ed71 3132 nfs->length = htons(reply->size - start_ofs);
349adfb2
BP
3133 nfs->table_id = fs->table_id;
3134 nfs->pad = 0;
3135 nfs->duration_sec = htonl(fs->duration_sec);
3136 nfs->duration_nsec = htonl(fs->duration_nsec);
81a76618 3137 nfs->priority = htons(fs->priority);
349adfb2
BP
3138 nfs->idle_timeout = htons(fs->idle_timeout);
3139 nfs->hard_timeout = htons(fs->hard_timeout);
f27f2134
BP
3140 nfs->idle_age = htons(fs->idle_age < 0 ? 0
3141 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
3142 : UINT16_MAX);
3143 nfs->hard_age = htons(fs->hard_age < 0 ? 0
3144 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
3145 : UINT16_MAX);
1a59dc2c 3146 nfs->match_len = htons(match_len);
349adfb2
BP
3147 nfs->cookie = fs->cookie;
3148 nfs->packet_count = htonll(fs->packet_count);
3149 nfs->byte_count = htonll(fs->byte_count);
349adfb2 3150 } else {
428b2edd 3151 OVS_NOT_REACHED();
349adfb2 3152 }
f25d0cf3 3153
982697a4 3154 ofpmp_postappend(replies, start_ofs);
8d8ab6c2 3155 fs_->match.flow.tunnel.metadata.tab = orig_tun_table;
349adfb2
BP
3156}
3157
76c93b22 3158/* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
a814ba0f 3159 * NXST_AGGREGATE reply matching 'request', and returns the message. */
76c93b22
BP
3160struct ofpbuf *
3161ofputil_encode_aggregate_stats_reply(
3162 const struct ofputil_aggregate_stats *stats,
982697a4 3163 const struct ofp_header *request)
76c93b22 3164{
a814ba0f
BP
3165 struct ofp_aggregate_stats_reply *asr;
3166 uint64_t packet_count;
3167 uint64_t byte_count;
76c93b22 3168 struct ofpbuf *msg;
982697a4 3169 enum ofpraw raw;
76c93b22 3170
982697a4 3171 ofpraw_decode(&raw, request);
617da9cd 3172 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
a814ba0f
BP
3173 packet_count = unknown_to_zero(stats->packet_count);
3174 byte_count = unknown_to_zero(stats->byte_count);
76c93b22 3175 } else {
a814ba0f
BP
3176 packet_count = stats->packet_count;
3177 byte_count = stats->byte_count;
76c93b22
BP
3178 }
3179
a814ba0f
BP
3180 msg = ofpraw_alloc_stats_reply(request, 0);
3181 asr = ofpbuf_put_zeros(msg, sizeof *asr);
3182 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
3183 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
3184 asr->flow_count = htonl(stats->flow_count);
3185
76c93b22
BP
3186 return msg;
3187}
3188
982697a4
BP
3189enum ofperr
3190ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
3191 const struct ofp_header *reply)
3192{
0a2869d5 3193 struct ofpbuf msg = ofpbuf_const_initializer(reply, ntohs(reply->length));
a814ba0f
BP
3194 ofpraw_pull_assert(&msg);
3195
0a2869d5 3196 struct ofp_aggregate_stats_reply *asr = msg.msg;
a814ba0f
BP
3197 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
3198 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
3199 stats->flow_count = ntohl(asr->flow_count);
982697a4
BP
3200
3201 return 0;
3202}
3203
b78f6b77
BP
3204/* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3205 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3206 * an OpenFlow error code. */
90bf1e07 3207enum ofperr
9b045a0c 3208ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
b78f6b77 3209 const struct ofp_header *oh)
9b045a0c 3210{
0a2869d5
BP
3211 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
3212 enum ofpraw raw = ofpraw_pull_assert(&b);
eefbf181
SH
3213 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
3214 const struct ofp12_flow_removed *ofr;
3215 enum ofperr error;
3216
3217 ofr = ofpbuf_pull(&b, sizeof *ofr);
3218
3cddeff0 3219 error = ofputil_pull_ofp11_match(&b, NULL, NULL, &fr->match, NULL);
eefbf181
SH
3220 if (error) {
3221 return error;
3222 }
3223
81a76618 3224 fr->priority = ntohs(ofr->priority);
eefbf181
SH
3225 fr->cookie = ofr->cookie;
3226 fr->reason = ofr->reason;
95216219 3227 fr->table_id = ofr->table_id;
eefbf181
SH
3228 fr->duration_sec = ntohl(ofr->duration_sec);
3229 fr->duration_nsec = ntohl(ofr->duration_nsec);
3230 fr->idle_timeout = ntohs(ofr->idle_timeout);
3231 fr->hard_timeout = ntohs(ofr->hard_timeout);
3232 fr->packet_count = ntohll(ofr->packet_count);
3233 fr->byte_count = ntohll(ofr->byte_count);
3234 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
31a9e63f 3235 const struct ofp10_flow_removed *ofr;
9b045a0c 3236
982697a4
BP
3237 ofr = ofpbuf_pull(&b, sizeof *ofr);
3238
81a76618
BP
3239 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
3240 fr->priority = ntohs(ofr->priority);
9b045a0c
BP
3241 fr->cookie = ofr->cookie;
3242 fr->reason = ofr->reason;
95216219 3243 fr->table_id = 255;
9b045a0c
BP
3244 fr->duration_sec = ntohl(ofr->duration_sec);
3245 fr->duration_nsec = ntohl(ofr->duration_nsec);
3246 fr->idle_timeout = ntohs(ofr->idle_timeout);
fa2bad0f 3247 fr->hard_timeout = 0;
9b045a0c
BP
3248 fr->packet_count = ntohll(ofr->packet_count);
3249 fr->byte_count = ntohll(ofr->byte_count);
982697a4 3250 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
9b045a0c 3251 struct nx_flow_removed *nfr;
c2725b60 3252 enum ofperr error;
9b045a0c 3253
9b045a0c 3254 nfr = ofpbuf_pull(&b, sizeof *nfr);
8d8ab6c2 3255 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match, NULL,
3cddeff0 3256 NULL, NULL, NULL);
9b045a0c
BP
3257 if (error) {
3258 return error;
3259 }
6fd6ed71 3260 if (b.size) {
90bf1e07 3261 return OFPERR_OFPBRC_BAD_LEN;
9b045a0c
BP
3262 }
3263
81a76618 3264 fr->priority = ntohs(nfr->priority);
9b045a0c
BP
3265 fr->cookie = nfr->cookie;
3266 fr->reason = nfr->reason;
745bfd5e 3267 fr->table_id = nfr->table_id ? nfr->table_id - 1 : 255;
9b045a0c
BP
3268 fr->duration_sec = ntohl(nfr->duration_sec);
3269 fr->duration_nsec = ntohl(nfr->duration_nsec);
3270 fr->idle_timeout = ntohs(nfr->idle_timeout);
fa2bad0f 3271 fr->hard_timeout = 0;
9b045a0c
BP
3272 fr->packet_count = ntohll(nfr->packet_count);
3273 fr->byte_count = ntohll(nfr->byte_count);
3274 } else {
428b2edd 3275 OVS_NOT_REACHED();
9b045a0c
BP
3276 }
3277
3278 return 0;
3279}
3280
588cd7b5 3281/* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
27527aa0 3282 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
588cd7b5
BP
3283 * message. */
3284struct ofpbuf *
3285ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
27527aa0 3286 enum ofputil_protocol protocol)
588cd7b5
BP
3287{
3288 struct ofpbuf *msg;
fa42f4f8
SH
3289 enum ofp_flow_removed_reason reason = fr->reason;
3290
3291 if (reason == OFPRR_METER_DELETE && !(protocol & OFPUTIL_P_OF14_UP)) {
3292 reason = OFPRR_DELETE;
3293 }
588cd7b5 3294
27527aa0 3295 switch (protocol) {
75fa58f8 3296 case OFPUTIL_P_OF11_STD:
2e1ae200 3297 case OFPUTIL_P_OF12_OXM:
c37c0382 3298 case OFPUTIL_P_OF13_OXM:
42dccab5 3299 case OFPUTIL_P_OF14_OXM:
b79d45a1
BP
3300 case OFPUTIL_P_OF15_OXM:
3301 case OFPUTIL_P_OF16_OXM: {
83974732
SH
3302 struct ofp12_flow_removed *ofr;
3303
3304 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
3305 ofputil_protocol_to_ofp_version(protocol),
75fa58f8
BP
3306 htonl(0),
3307 ofputil_match_typical_len(protocol));
83974732
SH
3308 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3309 ofr->cookie = fr->cookie;
81a76618 3310 ofr->priority = htons(fr->priority);
fa42f4f8 3311 ofr->reason = reason;
95216219 3312 ofr->table_id = fr->table_id;
83974732
SH
3313 ofr->duration_sec = htonl(fr->duration_sec);
3314 ofr->duration_nsec = htonl(fr->duration_nsec);
3315 ofr->idle_timeout = htons(fr->idle_timeout);
fa2bad0f 3316 ofr->hard_timeout = htons(fr->hard_timeout);
83974732
SH
3317 ofr->packet_count = htonll(fr->packet_count);
3318 ofr->byte_count = htonll(fr->byte_count);
75fa58f8 3319 ofputil_put_ofp11_match(msg, &fr->match, protocol);
83974732
SH
3320 break;
3321 }
3322
85813857
BP
3323 case OFPUTIL_P_OF10_STD:
3324 case OFPUTIL_P_OF10_STD_TID: {
31a9e63f 3325 struct ofp10_flow_removed *ofr;
588cd7b5 3326
982697a4
BP
3327 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
3328 htonl(0), 0);
3329 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
81a76618 3330 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
7fb563b9 3331 ofr->cookie = fr->cookie;
81a76618 3332 ofr->priority = htons(fr->priority);
fa42f4f8 3333 ofr->reason = reason;
588cd7b5
BP
3334 ofr->duration_sec = htonl(fr->duration_sec);
3335 ofr->duration_nsec = htonl(fr->duration_nsec);
3336 ofr->idle_timeout = htons(fr->idle_timeout);
5e9d0469
BP
3337 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
3338 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
27527aa0
BP
3339 break;
3340 }
3341
85813857
BP
3342 case OFPUTIL_P_OF10_NXM:
3343 case OFPUTIL_P_OF10_NXM_TID: {
588cd7b5
BP
3344 struct nx_flow_removed *nfr;
3345 int match_len;
3346
982697a4
BP
3347 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
3348 htonl(0), NXM_TYPICAL_LEN);
a3358085 3349 ofpbuf_put_zeros(msg, sizeof *nfr);
81a76618 3350 match_len = nx_put_match(msg, &fr->match, 0, 0);
588cd7b5 3351
6fd6ed71 3352 nfr = msg->msg;
588cd7b5 3353 nfr->cookie = fr->cookie;
81a76618 3354 nfr->priority = htons(fr->priority);
fa42f4f8 3355 nfr->reason = reason;
745bfd5e 3356 nfr->table_id = fr->table_id + 1;
588cd7b5
BP
3357 nfr->duration_sec = htonl(fr->duration_sec);
3358 nfr->duration_nsec = htonl(fr->duration_nsec);
3359 nfr->idle_timeout = htons(fr->idle_timeout);
3360 nfr->match_len = htons(match_len);
3361 nfr->packet_count = htonll(fr->packet_count);
3362 nfr->byte_count = htonll(fr->byte_count);
27527aa0
BP
3363 break;
3364 }
3365
3366 default:
428b2edd 3367 OVS_NOT_REACHED();
588cd7b5
BP
3368 }
3369
3370 return msg;
3371}
3372
6409e008
BP
3373/* The caller has done basic initialization of '*pin'; the other output
3374 * arguments needs to be initialized. */
3375static enum ofperr
77ab5fd2 3376decode_nx_packet_in2(const struct ofp_header *oh, bool loose,
8d8ab6c2 3377 const struct tun_table *tun_table,
3cddeff0 3378 const struct vl_mff_map *vl_mff_map,
6409e008 3379 struct ofputil_packet_in *pin,
77ab5fd2
BP
3380 size_t *total_len, uint32_t *buffer_id,
3381 struct ofpbuf *continuation)
6409e008
BP
3382{
3383 *total_len = 0;
3384 *buffer_id = UINT32_MAX;
3385
3386 struct ofpbuf properties;
3387 ofpbuf_use_const(&properties, oh, ntohs(oh->length));
3388 ofpraw_pull_assert(&properties);
3389
3390 while (properties.size > 0) {
3391 struct ofpbuf payload;
3392 uint64_t type;
3393
3394 enum ofperr error = ofpprop_pull(&properties, &payload, &type);
3395 if (error) {
3396 return error;
3397 }
3398
3399 switch (type) {
3400 case NXPINT_PACKET:
3401 pin->packet = payload.msg;
4adaf182 3402 pin->packet_len = ofpbuf_msgsize(&payload);
6409e008
BP
3403 break;
3404
3405 case NXPINT_FULL_LEN: {
3406 uint32_t u32;
3407 error = ofpprop_parse_u32(&payload, &u32);
3408 *total_len = u32;
3409 break;
3410 }
3411
3412 case NXPINT_BUFFER_ID:
3413 error = ofpprop_parse_u32(&payload, buffer_id);
3414 break;
3415
3416 case NXPINT_TABLE_ID:
3417 error = ofpprop_parse_u8(&payload, &pin->table_id);
3418 break;
3419
3420 case NXPINT_COOKIE:
3421 error = ofpprop_parse_be64(&payload, &pin->cookie);
3422 break;
3423
3424 case NXPINT_REASON: {
3425 uint8_t reason;
3426 error = ofpprop_parse_u8(&payload, &reason);
3427 pin->reason = reason;
3428 break;
3429 }
3430
3431 case NXPINT_METADATA:
87450a4e 3432 error = oxm_decode_match(payload.msg, ofpbuf_msgsize(&payload),
3cddeff0
YHW
3433 loose, tun_table, vl_mff_map,
3434 &pin->flow_metadata);
6409e008
BP
3435 break;
3436
bdcad671
BP
3437 case NXPINT_USERDATA:
3438 pin->userdata = payload.msg;
3439 pin->userdata_len = ofpbuf_msgsize(&payload);
3440 break;
3441
77ab5fd2
BP
3442 case NXPINT_CONTINUATION:
3443 if (continuation) {
3444 error = ofpprop_parse_nested(&payload, continuation);
3445 }
3446 break;
3447
6409e008 3448 default:
77ab5fd2 3449 error = OFPPROP_UNKNOWN(loose, "NX_PACKET_IN2", type);
6409e008
BP
3450 break;
3451 }
3452 if (error) {
3453 return error;
3454 }
3455 }
3456
4adaf182 3457 if (!pin->packet_len) {
6409e008
BP
3458 VLOG_WARN_RL(&bad_ofmsg_rl, "NXT_PACKET_IN2 lacks packet");
3459 return OFPERR_OFPBRC_BAD_LEN;
3460 } else if (!*total_len) {
4adaf182
BP
3461 *total_len = pin->packet_len;
3462 } else if (*total_len < pin->packet_len) {
6409e008
BP
3463 VLOG_WARN_RL(&bad_ofmsg_rl, "NXT_PACKET_IN2 claimed full_len < len");
3464 return OFPERR_OFPBRC_BAD_LEN;
3465 }
3466
3467 return 0;
3468}
3469
9bfe9334 3470/* Decodes the packet-in message starting at 'oh' into '*pin'. Populates
77ab5fd2
BP
3471 * 'pin->packet' and 'pin->packet_len' with the part of the packet actually
3472 * included in the message. If 'total_lenp' is nonnull, populates
3473 * '*total_lenp' with the original length of the packet (which is larger than
3474 * 'packet->len' if only part of the packet was included). If 'buffer_idp' is
3475 * nonnull, stores the packet's buffer ID in '*buffer_idp' (UINT32_MAX if it
9bfe9334
BP
3476 * was not buffered).
3477 *
77ab5fd2
BP
3478 * Populates 'continuation', if nonnull, with the continuation data from the
3479 * packet-in (an empty buffer, if 'oh' did not contain continuation data). The
3480 * format of this data is supposed to be opaque to anything other than
3481 * ovs-vswitchd, so that in any other process the only reasonable use of this
3482 * data is to be copied into an NXT_RESUME message via ofputil_encode_resume().
3483 *
3484 * This function points 'pin->packet' into 'oh', so the caller should not free
3485 * it separately from the original OpenFlow message. This is also true for
3486 * 'pin->userdata' (which could also end up NULL if there is no userdata).
3487 *
3cddeff0
YHW
3488 * 'vl_mff_map' is an optional parameter that is used to validate the length
3489 * of variable length mf_fields in 'match'. If it is not provided, the
3490 * default mf_fields with maximum length will be used.
3491 *
9bfe9334 3492 * Returns 0 if successful, otherwise an OpenFlow error code. */
f7cc6bd8 3493enum ofperr
77ab5fd2 3494ofputil_decode_packet_in(const struct ofp_header *oh, bool loose,
8d8ab6c2 3495 const struct tun_table *tun_table,
3cddeff0 3496 const struct vl_mff_map *vl_mff_map,
9bfe9334 3497 struct ofputil_packet_in *pin,
77ab5fd2
BP
3498 size_t *total_lenp, uint32_t *buffer_idp,
3499 struct ofpbuf *continuation)
65120a8a 3500{
77ab5fd2
BP
3501 uint32_t buffer_id;
3502 size_t total_len;
3503
65120a8a 3504 memset(pin, 0, sizeof *pin);
d4fa4e79 3505 pin->cookie = OVS_BE64_MAX;
77ab5fd2
BP
3506 if (continuation) {
3507 ofpbuf_use_const(continuation, NULL, 0);
3508 }
65120a8a 3509
0a2869d5
BP
3510 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
3511 enum ofpraw raw = ofpraw_pull_assert(&b);
2e1ae200 3512 if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) {
43948fa5
BP
3513 const struct ofp12_packet_in *opi = ofpbuf_pull(&b, sizeof *opi);
3514 const ovs_be64 *cookie = (raw == OFPRAW_OFPT13_PACKET_IN
3515 ? ofpbuf_pull(&b, sizeof *cookie)
3516 : NULL);
8d8ab6c2
JG
3517 enum ofperr error = oxm_pull_match_loose(&b, tun_table,
3518 &pin->flow_metadata);
7cfb9651
SH
3519 if (error) {
3520 return error;
3521 }
3522
3523 if (!ofpbuf_try_pull(&b, 2)) {
3524 return OFPERR_OFPBRC_BAD_LEN;
3525 }
3526
43948fa5
BP
3527 pin->reason = opi->reason;
3528 pin->table_id = opi->table_id;
77ab5fd2
BP
3529 buffer_id = ntohl(opi->buffer_id);
3530 total_len = ntohs(opi->total_len);
43948fa5
BP
3531 if (cookie) {
3532 pin->cookie = *cookie;
2e1ae200 3533 }
7cfb9651 3534
50dcbd8e 3535 pin->packet = b.data;
4adaf182 3536 pin->packet_len = b.size;
7cfb9651 3537 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
31a9e63f 3538 const struct ofp10_packet_in *opi;
982697a4 3539
31a9e63f 3540 opi = ofpbuf_pull(&b, offsetof(struct ofp10_packet_in, data));
65120a8a 3541
95022b47 3542 pin->packet = CONST_CAST(uint8_t *, opi->data);
4adaf182 3543 pin->packet_len = b.size;
65120a8a 3544
50dcbd8e 3545 match_init_catchall(&pin->flow_metadata);
9bfe9334
BP
3546 match_set_in_port(&pin->flow_metadata,
3547 u16_to_ofp(ntohs(opi->in_port)));
65120a8a 3548 pin->reason = opi->reason;
77ab5fd2
BP
3549 buffer_id = ntohl(opi->buffer_id);
3550 total_len = ntohs(opi->total_len);
4d197ebb
BP
3551 } else if (raw == OFPRAW_OFPT11_PACKET_IN) {
3552 const struct ofp11_packet_in *opi;
50dcbd8e 3553 ofp_port_t in_port;
4d197ebb
BP
3554 enum ofperr error;
3555
3556 opi = ofpbuf_pull(&b, sizeof *opi);
3557
6fd6ed71 3558 pin->packet = b.data;
4adaf182 3559 pin->packet_len = b.size;
4d197ebb 3560
77ab5fd2 3561 buffer_id = ntohl(opi->buffer_id);
50dcbd8e 3562 error = ofputil_port_from_ofp11(opi->in_port, &in_port);
4d197ebb
BP
3563 if (error) {
3564 return error;
3565 }
50dcbd8e
JG
3566 match_init_catchall(&pin->flow_metadata);
3567 match_set_in_port(&pin->flow_metadata, in_port);
77ab5fd2 3568 total_len = ntohs(opi->total_len);
4d197ebb
BP
3569 pin->reason = opi->reason;
3570 pin->table_id = opi->table_id;
982697a4 3571 } else if (raw == OFPRAW_NXT_PACKET_IN) {
73dbf4ab 3572 const struct nx_packet_in *npi;
54834960
EJ
3573 int error;
3574
54834960 3575 npi = ofpbuf_pull(&b, sizeof *npi);
50dcbd8e 3576 error = nx_pull_match_loose(&b, ntohs(npi->match_len),
8d8ab6c2 3577 &pin->flow_metadata, NULL, NULL, NULL);
54834960
EJ
3578 if (error) {
3579 return error;
3580 }
3581
3582 if (!ofpbuf_try_pull(&b, 2)) {
90bf1e07 3583 return OFPERR_OFPBRC_BAD_LEN;
54834960
EJ
3584 }
3585
54834960
EJ
3586 pin->reason = npi->reason;
3587 pin->table_id = npi->table_id;
3588 pin->cookie = npi->cookie;
3589
77ab5fd2
BP
3590 buffer_id = ntohl(npi->buffer_id);
3591 total_len = ntohs(npi->total_len);
7cfb9651 3592
50dcbd8e 3593 pin->packet = b.data;
4adaf182 3594 pin->packet_len = b.size;
77ab5fd2 3595 } else if (raw == OFPRAW_NXT_PACKET_IN2 || raw == OFPRAW_NXT_RESUME) {
3cddeff0
YHW
3596 enum ofperr error = decode_nx_packet_in2(oh, loose, tun_table,
3597 vl_mff_map, pin, &total_len,
3598 &buffer_id, continuation);
77ab5fd2
BP
3599 if (error) {
3600 return error;
3601 }
65120a8a 3602 } else {
428b2edd 3603 OVS_NOT_REACHED();
65120a8a
EJ
3604 }
3605
77ab5fd2
BP
3606 if (total_lenp) {
3607 *total_lenp = total_len;
3608 }
3609 if (buffer_idp) {
3610 *buffer_idp = buffer_id;
3611 }
3612
65120a8a
EJ
3613 return 0;
3614}
3615
9bfe9334
BP
3616static int
3617encode_packet_in_reason(enum ofp_packet_in_reason reason,
3618 enum ofp_version version)
3619{
3620 switch (reason) {
3621 case OFPR_NO_MATCH:
3622 case OFPR_ACTION:
3623 case OFPR_INVALID_TTL:
3624 return reason;
3625
3626 case OFPR_ACTION_SET:
3627 case OFPR_GROUP:
3628 case OFPR_PACKET_OUT:
3629 return version < OFP14_VERSION ? OFPR_ACTION : reason;
3630
3631 case OFPR_EXPLICIT_MISS:
3632 return version < OFP13_VERSION ? OFPR_ACTION : OFPR_NO_MATCH;
3633
3634 case OFPR_IMPLICIT_MISS:
3635 return OFPR_NO_MATCH;
3636
3637 case OFPR_N_REASONS:
3638 default:
3639 OVS_NOT_REACHED();
3640 }
3641}
3642
77ab5fd2
BP
3643/* Only NXT_PACKET_IN2 (not NXT_RESUME) should include NXCPT_USERDATA, so this
3644 * function omits it. The caller can add it itself if desired. */
3645static void
3646ofputil_put_packet_in(const struct ofputil_packet_in *pin,
c184807c
JR
3647 enum ofp_version version, size_t include_bytes,
3648 struct ofpbuf *msg)
77ab5fd2
BP
3649{
3650 /* Add packet properties. */
3651 ofpprop_put(msg, NXPINT_PACKET, pin->packet, include_bytes);
3652 if (include_bytes != pin->packet_len) {
3653 ofpprop_put_u32(msg, NXPINT_FULL_LEN, pin->packet_len);
3654 }
77ab5fd2
BP
3655
3656 /* Add flow properties. */
3657 ofpprop_put_u8(msg, NXPINT_TABLE_ID, pin->table_id);
3658 if (pin->cookie != OVS_BE64_MAX) {
3659 ofpprop_put_be64(msg, NXPINT_COOKIE, pin->cookie);
3660 }
3661
3662 /* Add other properties. */
3663 ofpprop_put_u8(msg, NXPINT_REASON,
3664 encode_packet_in_reason(pin->reason, version));
3665
3666 size_t start = ofpprop_start(msg, NXPINT_METADATA);
3667 oxm_put_raw(msg, &pin->flow_metadata, version);
3668 ofpprop_end(msg, start);
3669}
3670
3671static void
3672put_actions_property(struct ofpbuf *msg, uint64_t prop_type,
3673 enum ofp_version version,
3674 const struct ofpact *actions, size_t actions_len)
3675{
3676 if (actions_len) {
3677 size_t start = ofpprop_start_nested(msg, prop_type);
3678 ofpacts_put_openflow_actions(actions, actions_len, msg, version);
3679 ofpprop_end(msg, start);
3680 }
3681}
3682
3683enum nx_continuation_prop_type {
3684 NXCPT_BRIDGE = 0x8000,
3685 NXCPT_STACK,
3686 NXCPT_MIRRORS,
3687 NXCPT_CONNTRACKED,
3688 NXCPT_TABLE_ID,
3689 NXCPT_COOKIE,
3690 NXCPT_ACTIONS,
3691 NXCPT_ACTION_SET,
3692};
3693
3694/* Only NXT_PACKET_IN2 (not NXT_RESUME) should include NXCPT_USERDATA, so this
3695 * function omits it. The caller can add it itself if desired. */
3696static void
3697ofputil_put_packet_in_private(const struct ofputil_packet_in_private *pin,
c184807c
JR
3698 enum ofp_version version, size_t include_bytes,
3699 struct ofpbuf *msg)
77ab5fd2 3700{
c184807c 3701 ofputil_put_packet_in(&pin->public, version, include_bytes, msg);
77ab5fd2
BP
3702
3703 size_t continuation_ofs = ofpprop_start_nested(msg, NXPINT_CONTINUATION);
3704 size_t inner_ofs = msg->size;
3705
3706 if (!uuid_is_zero(&pin->bridge)) {
3707 ofpprop_put_uuid(msg, NXCPT_BRIDGE, &pin->bridge);
3708 }
3709
84cf3c1f
JR
3710 struct ofpbuf pin_stack;
3711 ofpbuf_use_const(&pin_stack, pin->stack, pin->stack_size);
77ab5fd2 3712
84cf3c1f
JR
3713 while (pin_stack.size) {
3714 uint8_t len;
3715 uint8_t *val = nx_stack_pop(&pin_stack, &len);
3716 ofpprop_put(msg, NXCPT_STACK, val, len);
77ab5fd2
BP
3717 }
3718
3719 if (pin->mirrors) {
3720 ofpprop_put_u32(msg, NXCPT_MIRRORS, pin->mirrors);
3721 }
3722
3723 if (pin->conntracked) {
3724 ofpprop_put_flag(msg, NXCPT_CONNTRACKED);
3725 }
3726
3727 if (pin->actions_len) {
3728 /* Divide 'pin->actions' into groups that begins with an
3729 * unroll_xlate action. For each group, emit a NXCPT_TABLE_ID and
3730 * NXCPT_COOKIE property (if either has changed; each is initially
3731 * assumed 0), then a NXCPT_ACTIONS property with the grouped
3732 * actions.
3733 *
3734 * The alternative is to make OFPACT_UNROLL_XLATE public. We can
3735 * always do that later, since this is a private property. */
3736 const struct ofpact *const end = ofpact_end(pin->actions,
3737 pin->actions_len);
3738 const struct ofpact_unroll_xlate *unroll = NULL;
3739 uint8_t table_id = 0;
3740 ovs_be64 cookie = 0;
3741
3742 const struct ofpact *a;
3743 for (a = pin->actions; ; a = ofpact_next(a)) {
3744 if (a == end || a->type == OFPACT_UNROLL_XLATE) {
3745 if (unroll) {
3746 if (table_id != unroll->rule_table_id) {
3747 ofpprop_put_u8(msg, NXCPT_TABLE_ID,
3748 unroll->rule_table_id);
3749 table_id = unroll->rule_table_id;
3750 }
3751 if (cookie != unroll->rule_cookie) {
3752 ofpprop_put_be64(msg, NXCPT_COOKIE,
3753 unroll->rule_cookie);
3754 cookie = unroll->rule_cookie;
3755 }
3756 }
3757
3758 const struct ofpact *start
3759 = unroll ? ofpact_next(&unroll->ofpact) : pin->actions;
3760 put_actions_property(msg, NXCPT_ACTIONS, version,
3761 start, (a - start) * sizeof *a);
3762
3763 if (a == end) {
3764 break;
3765 }
3766 unroll = ofpact_get_UNROLL_XLATE(a);
3767 }
3768 }
3769 }
3770
3771 if (pin->action_set_len) {
3772 size_t start = ofpprop_start_nested(msg, NXCPT_ACTION_SET);
3773 ofpacts_put_openflow_actions(pin->action_set,
3774 pin->action_set_len, msg, version);
3775 ofpprop_end(msg, start);
3776 }
3777
3778 if (msg->size > inner_ofs) {
3779 ofpprop_end(msg, continuation_ofs);
3780 } else {
3781 msg->size = continuation_ofs;
3782 }
3783}
3784
0f83fea0 3785static struct ofpbuf *
c184807c 3786ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in *pin)
0f83fea0
BP
3787{
3788 struct ofp10_packet_in *opi;
9bfe9334 3789 struct ofpbuf *msg;
0f83fea0 3790
9bfe9334 3791 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
4adaf182 3792 htonl(0), pin->packet_len);
9bfe9334 3793 opi = ofpbuf_put_zeros(msg, offsetof(struct ofp10_packet_in, data));
4adaf182 3794 opi->total_len = htons(pin->packet_len);
50dcbd8e 3795 opi->in_port = htons(ofp_to_u16(pin->flow_metadata.flow.in_port.ofp_port));
9bfe9334 3796 opi->reason = encode_packet_in_reason(pin->reason, OFP10_VERSION);
c184807c 3797 opi->buffer_id = htonl(UINT32_MAX);
0f83fea0 3798
9bfe9334 3799 return msg;
0f83fea0
BP
3800}
3801
3802static struct ofpbuf *
9bfe9334 3803ofputil_encode_nx_packet_in(const struct ofputil_packet_in *pin,
c184807c 3804 enum ofp_version version)
0f83fea0
BP
3805{
3806 struct nx_packet_in *npi;
9bfe9334 3807 struct ofpbuf *msg;
0f83fea0
BP
3808 size_t match_len;
3809
0f83fea0 3810 /* The final argument is just an estimate of the space required. */
6409e008 3811 msg = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, version,
4adaf182 3812 htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
9bfe9334
BP
3813 ofpbuf_put_zeros(msg, sizeof *npi);
3814 match_len = nx_put_match(msg, &pin->flow_metadata, 0, 0);
3815 ofpbuf_put_zeros(msg, 2);
3816
3817 npi = msg->msg;
c184807c 3818 npi->buffer_id = htonl(UINT32_MAX);
4adaf182 3819 npi->total_len = htons(pin->packet_len);
6409e008 3820 npi->reason = encode_packet_in_reason(pin->reason, version);
0f83fea0
BP
3821 npi->table_id = pin->table_id;
3822 npi->cookie = pin->cookie;
3823 npi->match_len = htons(match_len);
3824
9bfe9334 3825 return msg;
0f83fea0
BP
3826}
3827
6409e008 3828static struct ofpbuf *
77ab5fd2 3829ofputil_encode_nx_packet_in2(const struct ofputil_packet_in_private *pin,
c184807c 3830 enum ofp_version version, size_t include_bytes)
6409e008
BP
3831{
3832 /* 'extra' is just an estimate of the space required. */
77ab5fd2
BP
3833 size_t extra = (pin->public.packet_len
3834 + NXM_TYPICAL_LEN /* flow_metadata */
84cf3c1f 3835 + pin->stack_size * 4
77ab5fd2
BP
3836 + pin->actions_len
3837 + pin->action_set_len
3838 + 256); /* fudge factor */
6409e008
BP
3839 struct ofpbuf *msg = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN2, version,
3840 htonl(0), extra);
3841
c184807c 3842 ofputil_put_packet_in_private(pin, version, include_bytes, msg);
77ab5fd2
BP
3843 if (pin->public.userdata_len) {
3844 ofpprop_put(msg, NXPINT_USERDATA, pin->public.userdata,
3845 pin->public.userdata_len);
bdcad671
BP
3846 }
3847
6409e008
BP
3848 ofpmsg_update_length(msg);
3849 return msg;
3850}
3851
4d197ebb 3852static struct ofpbuf *
c184807c 3853ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in *pin)
4d197ebb
BP
3854{
3855 struct ofp11_packet_in *opi;
9bfe9334 3856 struct ofpbuf *msg;
4d197ebb 3857
9bfe9334 3858 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN, OFP11_VERSION,
4adaf182 3859 htonl(0), pin->packet_len);
9bfe9334 3860 opi = ofpbuf_put_zeros(msg, sizeof *opi);
c184807c 3861 opi->buffer_id = htonl(UINT32_MAX);
9bfe9334
BP
3862 opi->in_port = ofputil_port_to_ofp11(
3863 pin->flow_metadata.flow.in_port.ofp_port);
4d197ebb 3864 opi->in_phy_port = opi->in_port;
4adaf182 3865 opi->total_len = htons(pin->packet_len);
9bfe9334 3866 opi->reason = encode_packet_in_reason(pin->reason, OFP11_VERSION);
4d197ebb
BP
3867 opi->table_id = pin->table_id;
3868
9bfe9334 3869 return msg;
4d197ebb
BP
3870}
3871
0f83fea0
BP
3872static struct ofpbuf *
3873ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin,
c184807c 3874 enum ofp_version version)
0f83fea0 3875{
43948fa5
BP
3876 enum ofpraw raw = (version >= OFP13_VERSION
3877 ? OFPRAW_OFPT13_PACKET_IN
3878 : OFPRAW_OFPT12_PACKET_IN);
3879 struct ofpbuf *msg;
0f83fea0 3880
0f83fea0 3881 /* The final argument is just an estimate of the space required. */
43948fa5 3882 msg = ofpraw_alloc_xid(raw, version,
4adaf182 3883 htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
0f83fea0 3884
43948fa5 3885 struct ofp12_packet_in *opi = ofpbuf_put_zeros(msg, sizeof *opi);
c184807c 3886 opi->buffer_id = htonl(UINT32_MAX);
4adaf182 3887 opi->total_len = htons(pin->packet_len);
9bfe9334 3888 opi->reason = encode_packet_in_reason(pin->reason, version);
43948fa5 3889 opi->table_id = pin->table_id;
9bfe9334 3890
43948fa5
BP
3891 if (version >= OFP13_VERSION) {
3892 ovs_be64 cookie = pin->cookie;
3893 ofpbuf_put(msg, &cookie, sizeof cookie);
0f83fea0
BP
3894 }
3895
43948fa5
BP
3896 oxm_put_match(msg, &pin->flow_metadata, version);
3897 ofpbuf_put_zeros(msg, 2);
43948fa5
BP
3898
3899 return msg;
0f83fea0
BP
3900}
3901
77ab5fd2
BP
3902/* Converts abstract ofputil_packet_in_private 'pin' into a PACKET_IN message
3903 * for 'protocol', using the packet-in format specified by 'packet_in_format'.
9bfe9334 3904 *
77ab5fd2
BP
3905 * This function is really meant only for use by ovs-vswitchd. To any other
3906 * code, the "continuation" data, i.e. the data that is in struct
3907 * ofputil_packet_in_private but not in struct ofputil_packet_in, is supposed
3908 * to be opaque (and it might change from one OVS version to another). Thus,
3909 * if any other code wants to encode a packet-in, it should use a non-"private"
3910 * version of this function. (Such a version doesn't currently exist because
3911 * only ovs-vswitchd currently wants to encode packet-ins. If you need one,
3912 * write it...) */
ebb57021 3913struct ofpbuf *
77ab5fd2
BP
3914ofputil_encode_packet_in_private(const struct ofputil_packet_in_private *pin,
3915 enum ofputil_protocol protocol,
c184807c 3916 enum nx_packet_in_format packet_in_format)
ebb57021 3917{
9bfe9334
BP
3918 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
3919
9bfe9334 3920 struct ofpbuf *msg;
6409e008
BP
3921 switch (packet_in_format) {
3922 case NXPIF_STANDARD:
3923 switch (protocol) {
3924 case OFPUTIL_P_OF10_STD:
3925 case OFPUTIL_P_OF10_STD_TID:
3926 case OFPUTIL_P_OF10_NXM:
3927 case OFPUTIL_P_OF10_NXM_TID:
c184807c 3928 msg = ofputil_encode_ofp10_packet_in(&pin->public);
6409e008 3929 break;
2e1ae200 3930
6409e008 3931 case OFPUTIL_P_OF11_STD:
c184807c 3932 msg = ofputil_encode_ofp11_packet_in(&pin->public);
6409e008
BP
3933 break;
3934
3935 case OFPUTIL_P_OF12_OXM:
3936 case OFPUTIL_P_OF13_OXM:
3937 case OFPUTIL_P_OF14_OXM:
3938 case OFPUTIL_P_OF15_OXM:
b79d45a1 3939 case OFPUTIL_P_OF16_OXM:
c184807c 3940 msg = ofputil_encode_ofp12_packet_in(&pin->public, version);
6409e008
BP
3941 break;
3942
3943 default:
3944 OVS_NOT_REACHED();
3945 }
4d197ebb 3946 break;
d94240ec 3947
6409e008 3948 case NXPIF_NXT_PACKET_IN:
c184807c 3949 msg = ofputil_encode_nx_packet_in(&pin->public, version);
0f83fea0
BP
3950 break;
3951
6409e008 3952 case NXPIF_NXT_PACKET_IN2:
c184807c
JR
3953 return ofputil_encode_nx_packet_in2(pin, version,
3954 pin->public.packet_len);
6409e008 3955
0f83fea0 3956 default:
428b2edd 3957 OVS_NOT_REACHED();
54834960 3958 }
54834960 3959
c184807c 3960 ofpbuf_put(msg, pin->public.packet, pin->public.packet_len);
9bfe9334
BP
3961 ofpmsg_update_length(msg);
3962 return msg;
ebb57021
BP
3963}
3964
5014a89d
BP
3965/* Returns a string form of 'reason'. The return value is either a statically
3966 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3967 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
7c1a76a4 3968const char *
5014a89d
BP
3969ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason,
3970 char *reasonbuf, size_t bufsize)
7c1a76a4 3971{
7c1a76a4
BP
3972 switch (reason) {
3973 case OFPR_NO_MATCH:
3974 return "no_match";
3975 case OFPR_ACTION:
3976 return "action";
3977 case OFPR_INVALID_TTL:
3978 return "invalid_ttl";
424467c6
SS
3979 case OFPR_ACTION_SET:
3980 return "action_set";
3981 case OFPR_GROUP:
3982 return "group";
3983 case OFPR_PACKET_OUT:
3984 return "packet_out";
9bfe9334
BP
3985 case OFPR_EXPLICIT_MISS:
3986 case OFPR_IMPLICIT_MISS:
3987 return "";
7c1a76a4
BP
3988
3989 case OFPR_N_REASONS:
3990 default:
5014a89d
BP
3991 snprintf(reasonbuf, bufsize, "%d", (int) reason);
3992 return reasonbuf;
7c1a76a4
BP
3993 }
3994}
3995
3996bool
3997ofputil_packet_in_reason_from_string(const char *s,
3998 enum ofp_packet_in_reason *reason)
3999{
4000 int i;
4001
4002 for (i = 0; i < OFPR_N_REASONS; i++) {
5014a89d
BP
4003 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
4004 const char *reason_s;
4005
4006 reason_s = ofputil_packet_in_reason_to_string(i, reasonbuf,
4007 sizeof reasonbuf);
4008 if (!strcasecmp(s, reason_s)) {
7c1a76a4
BP
4009 *reason = i;
4010 return true;
4011 }
4012 }
4013 return false;
4014}
4015
77ab5fd2
BP
4016/* Returns a newly allocated NXT_RESUME message for 'pin', with the given
4017 * 'continuation', for 'protocol'. This message is suitable for resuming the
4018 * pipeline traveral of the packet represented by 'pin', if sent to the switch
4019 * from which 'pin' was received. */
4020struct ofpbuf *
4021ofputil_encode_resume(const struct ofputil_packet_in *pin,
4022 const struct ofpbuf *continuation,
4023 enum ofputil_protocol protocol)
4024{
4025 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
4026 size_t extra = pin->packet_len + NXM_TYPICAL_LEN + continuation->size;
4027 struct ofpbuf *msg = ofpraw_alloc_xid(OFPRAW_NXT_RESUME, version,
4028 0, extra);
c184807c 4029 ofputil_put_packet_in(pin, version, pin->packet_len, msg);
77ab5fd2
BP
4030 ofpprop_put_nested(msg, NXPINT_CONTINUATION, continuation);
4031 ofpmsg_update_length(msg);
4032 return msg;
4033}
4034
4035static enum ofperr
84cf3c1f 4036parse_stack_prop(const struct ofpbuf *property, struct ofpbuf *stack)
77ab5fd2
BP
4037{
4038 unsigned int len = ofpbuf_msgsize(property);
84cf3c1f 4039 if (len > sizeof(union mf_subvalue)) {
77ab5fd2
BP
4040 VLOG_WARN_RL(&bad_ofmsg_rl, "NXCPT_STACK property has bad length %u",
4041 len);
4042 return OFPERR_OFPBPC_BAD_LEN;
4043 }
84cf3c1f 4044 nx_stack_push_bottom(stack, property->msg, len);
77ab5fd2
BP
4045 return 0;
4046}
4047
4048static enum ofperr
4049parse_actions_property(struct ofpbuf *property, enum ofp_version version,
4050 struct ofpbuf *ofpacts)
4051{
4052 if (!ofpbuf_try_pull(property, ROUND_UP(ofpbuf_headersize(property), 8))) {
4053 VLOG_WARN_RL(&bad_ofmsg_rl, "actions property has bad length %"PRIu32,
4054 property->size);
4055 return OFPERR_OFPBPC_BAD_LEN;
4056 }
4057
4058 return ofpacts_pull_openflow_actions(property, property->size,
5c7c16d8 4059 version, NULL, NULL, ofpacts);
77ab5fd2
BP
4060}
4061
4062/* This is like ofputil_decode_packet_in(), except that it decodes the
4063 * continuation data into 'pin'. The format of this data is supposed to be
4064 * opaque to any process other than ovs-vswitchd, so this function should not
4065 * be used outside ovs-vswitchd.
4066 *
3cddeff0
YHW
4067 * 'vl_mff_map' is an optional parameter that is used to validate the length
4068 * of variable length mf_fields in 'match'. If it is not provided, the
4069 * default mf_fields with maximum length will be used.
4070 *
77ab5fd2
BP
4071 * When successful, 'pin' contains some dynamically allocated data. Call
4072 * ofputil_packet_in_private_destroy() to free this data. */
4073enum ofperr
4074ofputil_decode_packet_in_private(const struct ofp_header *oh, bool loose,
8d8ab6c2 4075 const struct tun_table *tun_table,
3cddeff0 4076 const struct vl_mff_map *vl_mff_map,
77ab5fd2
BP
4077 struct ofputil_packet_in_private *pin,
4078 size_t *total_len, uint32_t *buffer_id)
4079{
4080 memset(pin, 0, sizeof *pin);
4081
4082 struct ofpbuf continuation;
4083 enum ofperr error;
3cddeff0
YHW
4084 error = ofputil_decode_packet_in(oh, loose, tun_table, vl_mff_map,
4085 &pin->public, total_len, buffer_id,
4086 &continuation);
77ab5fd2
BP
4087 if (error) {
4088 return error;
4089 }
4090
4091 struct ofpbuf actions, action_set;
4092 ofpbuf_init(&actions, 0);
4093 ofpbuf_init(&action_set, 0);
4094
4095 uint8_t table_id = 0;
4096 ovs_be64 cookie = 0;
4097
84cf3c1f
JR
4098 struct ofpbuf stack;
4099 ofpbuf_init(&stack, 0);
77ab5fd2
BP
4100
4101 while (continuation.size > 0) {
4102 struct ofpbuf payload;
4103 uint64_t type;
4104
4105 error = ofpprop_pull(&continuation, &payload, &type);
8319a81a
JR
4106 if (error) {
4107 break;
4108 }
77ab5fd2
BP
4109
4110 switch (type) {
4111 case NXCPT_BRIDGE:
4112 error = ofpprop_parse_uuid(&payload, &pin->bridge);
4113 break;
4114
4115 case NXCPT_STACK:
84cf3c1f 4116 error = parse_stack_prop(&payload, &stack);
77ab5fd2
BP
4117 break;
4118
4119 case NXCPT_MIRRORS:
4120 error = ofpprop_parse_u32(&payload, &pin->mirrors);
4121 break;
4122
4123 case NXCPT_CONNTRACKED:
4124 pin->conntracked = true;
4125 break;
4126
4127 case NXCPT_TABLE_ID:
4128 error = ofpprop_parse_u8(&payload, &table_id);
4129 break;
4130
4131 case NXCPT_COOKIE:
4132 error = ofpprop_parse_be64(&payload, &cookie);
4133 break;
4134
4135 case NXCPT_ACTIONS: {
4136 struct ofpact_unroll_xlate *unroll
4137 = ofpact_put_UNROLL_XLATE(&actions);
4138 unroll->rule_table_id = table_id;
4139 unroll->rule_cookie = cookie;
4140 error = parse_actions_property(&payload, oh->version, &actions);
4141 break;
4142 }
4143
4144 case NXCPT_ACTION_SET:
4145 error = parse_actions_property(&payload, oh->version, &action_set);
4146 break;
4147
4148 default:
4149 error = OFPPROP_UNKNOWN(loose, "continuation", type);
4150 break;
4151 }
4152 if (error) {
4153 break;
4154 }
4155 }
4156
4157 pin->actions_len = actions.size;
4158 pin->actions = ofpbuf_steal_data(&actions);
4159 pin->action_set_len = action_set.size;
4160 pin->action_set = ofpbuf_steal_data(&action_set);
84cf3c1f
JR
4161 pin->stack_size = stack.size;
4162 pin->stack = ofpbuf_steal_data(&stack);
77ab5fd2
BP
4163
4164 if (error) {
4165 ofputil_packet_in_private_destroy(pin);
4166 }
4167
8319a81a 4168 return error;
77ab5fd2
BP
4169}
4170
4171/* Frees data in 'pin' that is dynamically allocated by
4172 * ofputil_decode_packet_in_private().
4173 *
4174 * 'pin->public' contains some pointer members that
4175 * ofputil_decode_packet_in_private() doesn't initialize to newly allocated
4176 * data, so this function doesn't free those. */
4177void
4178ofputil_packet_in_private_destroy(struct ofputil_packet_in_private *pin)
4179{
4180 if (pin) {
4181 free(pin->stack);
4182 free(pin->actions);
4183 free(pin->action_set);
4184 }
4185}
4186
f25d0cf3
BP
4187/* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
4188 * 'po'.
4189 *
4190 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
4191 * message's actions. The caller must initialize 'ofpacts' and retains
4192 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
4193 *
6dd3c787
JR
4194 * 'po->packet' refers to the packet data in 'oh', so the buffer containing
4195 * 'oh' must not be destroyed while 'po' is being used.
4196 *
f25d0cf3 4197 * Returns 0 if successful, otherwise an OFPERR_* value. */
c6a93eb7
BP
4198enum ofperr
4199ofputil_decode_packet_out(struct ofputil_packet_out *po,
982697a4 4200 const struct ofp_header *oh,
f25d0cf3 4201 struct ofpbuf *ofpacts)
c6a93eb7 4202{
0a2869d5
BP
4203 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
4204 enum ofpraw raw = ofpraw_pull_assert(&b);
982697a4 4205
9abca1e5 4206 ofpbuf_clear(ofpacts);
35eb6326 4207 match_init_catchall(&po->flow_metadata);
eb5ee596
SH
4208 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
4209 enum ofperr error;
35eb6326 4210 ofp_port_t in_port;
eb5ee596
SH
4211 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
4212
4213 po->buffer_id = ntohl(opo->buffer_id);
35eb6326 4214 error = ofputil_port_from_ofp11(opo->in_port, &in_port);
eb5ee596
SH
4215 if (error) {
4216 return error;
4217 }
35eb6326 4218 match_set_in_port(&po->flow_metadata, in_port);
eb5ee596 4219
e3f8f887 4220 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
5c7c16d8
YHW
4221 oh->version, NULL, NULL,
4222 ofpacts);
eb5ee596
SH
4223 if (error) {
4224 return error;
4225 }
eb5ee596 4226 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
8a6bc7cd 4227 enum ofperr error;
31a9e63f 4228 const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
8a6bc7cd
SH
4229
4230 po->buffer_id = ntohl(opo->buffer_id);
35eb6326 4231 match_set_in_port(&po->flow_metadata, u16_to_ofp(ntohs(opo->in_port)));
8a6bc7cd 4232
e3f8f887 4233 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
5c7c16d8
YHW
4234 oh->version, NULL, NULL,
4235 ofpacts);
8a6bc7cd
SH
4236 if (error) {
4237 return error;
4238 }
4239 } else {
428b2edd 4240 OVS_NOT_REACHED();
8a6bc7cd
SH
4241 }
4242
35eb6326
YHW
4243 ofp_port_t in_port = po->flow_metadata.flow.in_port.ofp_port;
4244 if (ofp_to_u16(in_port) >= ofp_to_u16(OFPP_MAX)
4245 && in_port != OFPP_LOCAL
4246 && in_port != OFPP_NONE
4247 && in_port != OFPP_CONTROLLER) {
94783c7c 4248 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx32,
35eb6326 4249 po->flow_metadata.flow.in_port.ofp_port);
2e1bfcb6 4250 return OFPERR_OFPBRC_BAD_PORT;
c6a93eb7
BP
4251 }
4252
6fd6ed71
PS
4253 po->ofpacts = ofpacts->data;
4254 po->ofpacts_len = ofpacts->size;
c6a93eb7
BP
4255
4256 if (po->buffer_id == UINT32_MAX) {
6fd6ed71
PS
4257 po->packet = b.data;
4258 po->packet_len = b.size;
c6a93eb7
BP
4259 } else {
4260 po->packet = NULL;
4261 po->packet_len = 0;
4262 }
4263
4264 return 0;
4265}
6c038611
BP
4266\f
4267/* ofputil_phy_port */
4268
4269/* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
4270BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
4271BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
4272BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
4273BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
4274BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
4275BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
4276BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
4277
4278/* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
9e1fd49b
BP
4279BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
4280BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
4281BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
4282BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
4283BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
6c038611 4284
9e1fd49b
BP
4285static enum netdev_features
4286netdev_port_features_from_ofp10(ovs_be32 ofp10_)
6c038611
BP
4287{
4288 uint32_t ofp10 = ntohl(ofp10_);
4289 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
4290}
4291
9e1fd49b
BP
4292static ovs_be32
4293netdev_port_features_to_ofp10(enum netdev_features features)
6c038611
BP
4294{
4295 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
4296}
c6a93eb7 4297
9e1fd49b
BP
4298BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
4299BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
4300BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
4301BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
4302BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
4303BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
4304BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
4305BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
4306BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
4307BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
4308BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
4309BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
4310BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
4311BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
4312BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
4313BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
4314
4315static enum netdev_features
4316netdev_port_features_from_ofp11(ovs_be32 ofp11)
4317{
4318 return ntohl(ofp11) & 0xffff;
4319}
4320
4321static ovs_be32
4322netdev_port_features_to_ofp11(enum netdev_features features)
4323{
4324 return htonl(features & 0xffff);
4325}
4326
4327static enum ofperr
4328ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
4329 const struct ofp10_phy_port *opp)
4330{
4e022ec0 4331 pp->port_no = u16_to_ofp(ntohs(opp->port_no));
74ff3298 4332 pp->hw_addr = opp->hw_addr;
f9ac0f03 4333 ovs_strlcpy_arrays(pp->name, opp->name);
9e1fd49b
BP
4334
4335 pp->config = ntohl(opp->config) & OFPPC10_ALL;
4336 pp->state = ntohl(opp->state) & OFPPS10_ALL;
4337
4338 pp->curr = netdev_port_features_from_ofp10(opp->curr);
4339 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
4340 pp->supported = netdev_port_features_from_ofp10(opp->supported);
4341 pp->peer = netdev_port_features_from_ofp10(opp->peer);
4342
d02a5f8e
BP
4343 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
4344 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
9e1fd49b
BP
4345
4346 return 0;
4347}
4348
4349static enum ofperr
4350ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
4351 const struct ofp11_port *op)
4352{
4353 enum ofperr error;
4354
9e1fd49b
BP
4355 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
4356 if (error) {
4357 return error;
4358 }
74ff3298 4359 pp->hw_addr = op->hw_addr;
f9ac0f03 4360 ovs_strlcpy_arrays(pp->name, op->name);
9e1fd49b
BP
4361
4362 pp->config = ntohl(op->config) & OFPPC11_ALL;
646f2b37 4363 pp->state = ntohl(op->state) & OFPPS11_ALL;
9e1fd49b
BP
4364
4365 pp->curr = netdev_port_features_from_ofp11(op->curr);
4366 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
4367 pp->supported = netdev_port_features_from_ofp11(op->supported);
4368 pp->peer = netdev_port_features_from_ofp11(op->peer);
4369
4370 pp->curr_speed = ntohl(op->curr_speed);
4371 pp->max_speed = ntohl(op->max_speed);
4372
4373 return 0;
4374}
4375
8c3cc785
BP
4376static enum ofperr
4377parse_ofp14_port_ethernet_property(const struct ofpbuf *payload,
4378 struct ofputil_phy_port *pp)
4379{
6fd6ed71 4380 struct ofp14_port_desc_prop_ethernet *eth = payload->data;
8c3cc785 4381
6fd6ed71 4382 if (payload->size != sizeof *eth) {
8c3cc785
BP
4383 return OFPERR_OFPBPC_BAD_LEN;
4384 }
4385
4386 pp->curr = netdev_port_features_from_ofp11(eth->curr);
4387 pp->advertised = netdev_port_features_from_ofp11(eth->advertised);
4388 pp->supported = netdev_port_features_from_ofp11(eth->supported);
4389 pp->peer = netdev_port_features_from_ofp11(eth->peer);
4390
4391 pp->curr_speed = ntohl(eth->curr_speed);
4392 pp->max_speed = ntohl(eth->max_speed);
4393
4394 return 0;
4395}
4396
4397static enum ofperr
2f2b904f
BP
4398ofputil_pull_ofp14_port_properties(const void *props, size_t len,
4399 struct ofputil_phy_port *pp)
8c3cc785 4400{
2f2b904f 4401 struct ofpbuf properties = ofpbuf_const_initializer(props, len);
6fd6ed71 4402 while (properties.size > 0) {
8c3cc785
BP
4403 struct ofpbuf payload;
4404 enum ofperr error;
34a543e3 4405 uint64_t type;
8c3cc785 4406
c5562271 4407 error = ofpprop_pull(&properties, &payload, &type);
8c3cc785
BP
4408 if (error) {
4409 return error;
4410 }
4411
4412 switch (type) {
4413 case OFPPDPT14_ETHERNET:
4414 error = parse_ofp14_port_ethernet_property(&payload, pp);
4415 break;
4416
4417 default:
34a543e3 4418 error = OFPPROP_UNKNOWN(true, "port", type);
8c3cc785
BP
4419 break;
4420 }
4421
4422 if (error) {
4423 return error;
4424 }
4425 }
4426
4427 return 0;
4428}
4429
2f2b904f
BP
4430static enum ofperr
4431ofputil_pull_ofp14_port(struct ofputil_phy_port *pp, struct ofpbuf *msg)
4432{
4433 const struct ofp14_port *op = ofpbuf_try_pull(msg, sizeof *op);
4434 if (!op) {
4435 return OFPERR_OFPBRC_BAD_LEN;
4436 }
4437
4438 size_t len = ntohs(op->length);
4439 if (len < sizeof *op || len - sizeof *op > msg->size) {
4440 return OFPERR_OFPBRC_BAD_LEN;
4441 }
4442 len -= sizeof *op;
4443
4444 enum ofperr error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
4445 if (error) {
4446 return error;
4447 }
4448 pp->hw_addr = op->hw_addr;
4449 ovs_strlcpy_arrays(pp->name, op->name);
4450
4451 pp->config = ntohl(op->config) & OFPPC11_ALL;
4452 pp->state = ntohl(op->state) & OFPPS11_ALL;
4453
4454 return ofputil_pull_ofp14_port_properties(ofpbuf_pull(msg, len), len, pp);
4455}
4456
4457static enum ofperr
4458ofputil_pull_ofp16_port(struct ofputil_phy_port *pp, struct ofpbuf *msg)
4459{
4460 const struct ofp16_port *op = ofpbuf_try_pull(msg, sizeof *op);
4461 if (!op) {
4462 return OFPERR_OFPBRC_BAD_LEN;
4463 }
4464
4465 size_t len = ntohs(op->length);
4466 if (len < sizeof *op || len - sizeof *op > msg->size) {
4467 return OFPERR_OFPBRC_BAD_LEN;
4468 }
4469 len -= sizeof *op;
4470
4471 enum ofperr error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
4472 if (error) {
4473 return error;
4474 }
4475 if (op->hw_addr_type & htons(OFPPHAT16_EUI48)) {
4476 pp->hw_addr = op->hw_addr;
4477 }
4478 if (op->hw_addr_type & htons(OFPPHAT16_EUI64)) {
4479 pp->hw_addr64 = op->hw_addr64;
4480 }
4481 ovs_strlcpy_arrays(pp->name, op->name);
4482
4483 pp->config = ntohl(op->config) & OFPPC11_ALL;
4484 pp->state = ntohl(op->state) & OFPPS11_ALL;
4485
4486 return ofputil_pull_ofp14_port_properties(ofpbuf_pull(msg, len), len, pp);
4487}
4488
9e1fd49b
BP
4489static void
4490ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
4491 struct ofp10_phy_port *opp)
4492{
4493 memset(opp, 0, sizeof *opp);
4494
4e022ec0 4495 opp->port_no = htons(ofp_to_u16(pp->port_no));
74ff3298 4496 opp->hw_addr = pp->hw_addr;
f9ac0f03 4497 ovs_strlcpy_arrays(opp->name, pp->name);
9e1fd49b
BP
4498
4499 opp->config = htonl(pp->config & OFPPC10_ALL);
4500 opp->state = htonl(pp->state & OFPPS10_ALL);
4501
4502 opp->curr = netdev_port_features_to_ofp10(pp->curr);
4503 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
4504 opp->supported = netdev_port_features_to_ofp10(pp->supported);
4505 opp->peer = netdev_port_features_to_ofp10(pp->peer);
4506}
4507
4508static void
4509ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
4510 struct ofp11_port *op)
4511{
4512 memset(op, 0, sizeof *op);
4513
4514 op->port_no = ofputil_port_to_ofp11(pp->port_no);
74ff3298 4515 op->hw_addr = pp->hw_addr;
f9ac0f03 4516 ovs_strlcpy_arrays(op->name, pp->name);
9e1fd49b
BP
4517
4518 op->config = htonl(pp->config & OFPPC11_ALL);
4519 op->state = htonl(pp->state & OFPPS11_ALL);
4520
4521 op->curr = netdev_port_features_to_ofp11(pp->curr);
4522 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
4523 op->supported = netdev_port_features_to_ofp11(pp->supported);
4524 op->peer = netdev_port_features_to_ofp11(pp->peer);
4525
4526 op->curr_speed = htonl(pp->curr_speed);
4527 op->max_speed = htonl(pp->max_speed);
4528}
4529
8c3cc785 4530static void
2f2b904f
BP
4531ofputil_encode_ofp14_port_ethernet_prop(
4532 const struct ofputil_phy_port *pp,
4533 struct ofp14_port_desc_prop_ethernet *eth)
4534{
4535 eth->curr = netdev_port_features_to_ofp11(pp->curr);
4536 eth->advertised = netdev_port_features_to_ofp11(pp->advertised);
4537 eth->supported = netdev_port_features_to_ofp11(pp->supported);
4538 eth->peer = netdev_port_features_to_ofp11(pp->peer);
4539 eth->curr_speed = htonl(pp->curr_speed);
4540 eth->max_speed = htonl(pp->max_speed);
4541}
4542
4543static void
4544ofputil_put_ofp14_port(const struct ofputil_phy_port *pp, struct ofpbuf *b)
8c3cc785
BP
4545{
4546 struct ofp14_port *op;
4547 struct ofp14_port_desc_prop_ethernet *eth;
4548
4549 ofpbuf_prealloc_tailroom(b, sizeof *op + sizeof *eth);
4550
4551 op = ofpbuf_put_zeros(b, sizeof *op);
4552 op->port_no = ofputil_port_to_ofp11(pp->port_no);
4553 op->length = htons(sizeof *op + sizeof *eth);
74ff3298 4554 op->hw_addr = pp->hw_addr;
2f2b904f 4555 ovs_strlcpy_arrays(op->name, pp->name);
8c3cc785
BP
4556 op->config = htonl(pp->config & OFPPC11_ALL);
4557 op->state = htonl(pp->state & OFPPS11_ALL);
4558
303721ee 4559 eth = ofpprop_put_zeros(b, OFPPDPT14_ETHERNET, sizeof *eth);
2f2b904f
BP
4560 ofputil_encode_ofp14_port_ethernet_prop(pp, eth);
4561}
4562
4563static void
4564ofputil_put_ofp16_port(const struct ofputil_phy_port *pp, struct ofpbuf *b)
4565{
4566 struct ofp16_port *op;
4567 struct ofp14_port_desc_prop_ethernet *eth;
4568
4569 ofpbuf_prealloc_tailroom(b, sizeof *op + sizeof *eth);
4570
4571 op = ofpbuf_put_zeros(b, sizeof *op);
4572 op->port_no = ofputil_port_to_ofp11(pp->port_no);
4573 op->length = htons(sizeof *op + sizeof *eth);
4574 if (!eth_addr_is_zero(pp->hw_addr)) {
4575 op->hw_addr_type |= htons(OFPPHAT16_EUI48);
4576 op->hw_addr = pp->hw_addr;
4577 }
4578 if (!eth_addr64_is_zero(pp->hw_addr64)) {
4579 op->hw_addr_type |= htons(OFPPHAT16_EUI64);
4580 op->hw_addr64 = pp->hw_addr64;
4581 }
4582 ovs_strlcpy_arrays(op->name, pp->name);
4583 op->config = htonl(pp->config & OFPPC11_ALL);
4584 op->state = htonl(pp->state & OFPPS11_ALL);
4585
4586 eth = ofpprop_put_zeros(b, OFPPDPT14_ETHERNET, sizeof *eth);
4587 ofputil_encode_ofp14_port_ethernet_prop(pp, eth);
8c3cc785
BP
4588}
4589
9e1fd49b 4590static void
2e3fa633
SH
4591ofputil_put_phy_port(enum ofp_version ofp_version,
4592 const struct ofputil_phy_port *pp, struct ofpbuf *b)
9e1fd49b 4593{
2e3fa633
SH
4594 switch (ofp_version) {
4595 case OFP10_VERSION: {
6b0f20ac
BP
4596 struct ofp10_phy_port *opp = ofpbuf_put_uninit(b, sizeof *opp);
4597 ofputil_encode_ofp10_phy_port(pp, opp);
2e3fa633
SH
4598 break;
4599 }
4600
4601 case OFP11_VERSION:
2e1ae200
JR
4602 case OFP12_VERSION:
4603 case OFP13_VERSION: {
6b0f20ac
BP
4604 struct ofp11_port *op = ofpbuf_put_uninit(b, sizeof *op);
4605 ofputil_encode_ofp11_port(pp, op);
2e3fa633
SH
4606 break;
4607 }
4608
c37c0382 4609 case OFP14_VERSION:
42dccab5 4610 case OFP15_VERSION:
8c3cc785 4611 ofputil_put_ofp14_port(pp, b);
c37c0382 4612 break;
2f2b904f
BP
4613 case OFP16_VERSION:
4614 ofputil_put_ofp16_port(pp, b);
4615 break;
c37c0382 4616
2e3fa633 4617 default:
428b2edd 4618 OVS_NOT_REACHED();
9e1fd49b
BP
4619 }
4620}
2be393ed 4621
70ae4f93
BP
4622enum ofperr
4623ofputil_decode_port_desc_stats_request(const struct ofp_header *request,
4624 ofp_port_t *port)
4625{
0a2869d5
BP
4626 struct ofpbuf b = ofpbuf_const_initializer(request,
4627 ntohs(request->length));
4628 enum ofpraw raw = ofpraw_pull_assert(&b);
70ae4f93
BP
4629 if (raw == OFPRAW_OFPST10_PORT_DESC_REQUEST) {
4630 *port = OFPP_ANY;
4631 return 0;
4632 } else if (raw == OFPRAW_OFPST15_PORT_DESC_REQUEST) {
4633 ovs_be32 *ofp11_port;
4634
4635 ofp11_port = ofpbuf_pull(&b, sizeof *ofp11_port);
4636 return ofputil_port_from_ofp11(*ofp11_port, port);
4637 } else {
4638 OVS_NOT_REACHED();
4639 }
4640}
4641
4642struct ofpbuf *
4643ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version,
4644 ofp_port_t port)
4645{
4646 struct ofpbuf *request;
70ae4f93
BP
4647
4648 switch (ofp_version) {
4649 case OFP10_VERSION:
4650 case OFP11_VERSION:
4651 case OFP12_VERSION:
4652 case OFP13_VERSION:
4653 case OFP14_VERSION:
4654 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST,
4655 ofp_version, 0);
4656 break;
b79d45a1
BP
4657 case OFP15_VERSION:
4658 case OFP16_VERSION:{
7448d548 4659 struct ofp15_port_desc_request *req;
70ae4f93
BP
4660 request = ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST,
4661 ofp_version, 0);
7448d548
MT
4662 req = ofpbuf_put_zeros(request, sizeof *req);
4663 req->port_no = ofputil_port_to_ofp11(port);
70ae4f93 4664 break;
7448d548 4665 }
70ae4f93
BP
4666 default:
4667 OVS_NOT_REACHED();
4668 }
4669
4670 return request;
4671}
4672
2be393ed 4673void
e28ac5cf 4674ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
ca6ba700 4675 struct ovs_list *replies)
2be393ed 4676{
417e7e66 4677 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
6fd6ed71 4678 size_t start_ofs = reply->size;
c37c0382 4679
6b0f20ac
BP
4680 ofputil_put_phy_port(ofpmp_version(replies), pp, reply);
4681 ofpmp_postappend(replies, start_ofs);
2be393ed 4682}
9e1fd49b 4683\f
ad99e2ed
BP
4684/* ofputil_switch_config */
4685
4686/* Decodes 'oh', which must be an OFPT_GET_CONFIG_REPLY or OFPT_SET_CONFIG
4687 * message, into 'config'. Returns false if 'oh' contained any flags that
4688 * aren't specified in its version of OpenFlow, true otherwise. */
4689static bool
4690ofputil_decode_switch_config(const struct ofp_header *oh,
4691 struct ofputil_switch_config *config)
4692{
0a2869d5 4693 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
ad99e2ed 4694 ofpraw_pull_assert(&b);
ad99e2ed 4695
0a2869d5 4696 const struct ofp_switch_config *osc = ofpbuf_pull(&b, sizeof *osc);
ad99e2ed
BP
4697 config->frag = ntohs(osc->flags) & OFPC_FRAG_MASK;
4698 config->miss_send_len = ntohs(osc->miss_send_len);
4699
4700 ovs_be16 valid_mask = htons(OFPC_FRAG_MASK);
4701 if (oh->version < OFP13_VERSION) {
4702 const ovs_be16 ttl_bit = htons(OFPC_INVALID_TTL_TO_CONTROLLER);
4703 valid_mask |= ttl_bit;
4704 config->invalid_ttl_to_controller = (osc->flags & ttl_bit) != 0;
4705 } else {
4706 config->invalid_ttl_to_controller = -1;
4707 }
4708
4709 return !(osc->flags & ~valid_mask);
4710}
4711
4712void
4713ofputil_decode_get_config_reply(const struct ofp_header *oh,
4714 struct ofputil_switch_config *config)
4715{
4716 ofputil_decode_switch_config(oh, config);
4717}
4718
4719enum ofperr
4720ofputil_decode_set_config(const struct ofp_header *oh,
4721 struct ofputil_switch_config *config)
4722{
4723 return (ofputil_decode_switch_config(oh, config)
4724 ? 0
4725 : OFPERR_OFPSCFC_BAD_FLAGS);
4726}
4727
4728static struct ofpbuf *
4729ofputil_put_switch_config(const struct ofputil_switch_config *config,
4730 struct ofpbuf *b)
4731{
4732 const struct ofp_header *oh = b->data;
4733 struct ofp_switch_config *osc = ofpbuf_put_zeros(b, sizeof *osc);
4734 osc->flags = htons(config->frag);
4735 if (config->invalid_ttl_to_controller > 0 && oh->version < OFP13_VERSION) {
4736 osc->flags |= htons(OFPC_INVALID_TTL_TO_CONTROLLER);
4737 }
4738 osc->miss_send_len = htons(config->miss_send_len);
4739 return b;
4740}
4741
4742struct ofpbuf *
4743ofputil_encode_get_config_reply(const struct ofp_header *request,
4744 const struct ofputil_switch_config *config)
4745{
4746 struct ofpbuf *b = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY,
4747 request, 0);
4748 return ofputil_put_switch_config(config, b);
4749}
4750
4751struct ofpbuf *
4752ofputil_encode_set_config(const struct ofputil_switch_config *config,
4753 enum ofp_version version)
4754{
4755 struct ofpbuf *b = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, version, 0);
4756 return ofputil_put_switch_config(config, b);
4757}
4758\f
9e1fd49b
BP
4759/* ofputil_switch_features */
4760
4761#define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
60202987 4762 OFPC_IP_REASM | OFPC_QUEUE_STATS)
9e1fd49b
BP
4763BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
4764BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
4765BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
4766BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
4767BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
4768BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
50b73fe1
JR
4769BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_BLOCKED == OFPC12_PORT_BLOCKED);
4770BUILD_ASSERT_DECL((int) OFPUTIL_C_BUNDLES == OFPC14_BUNDLES);
4771BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_MONITORING == OFPC14_FLOW_MONITORING);
9e1fd49b 4772
60202987
SH
4773static uint32_t
4774ofputil_capabilities_mask(enum ofp_version ofp_version)
4775{
0746a84f 4776 /* Handle capabilities whose bit is unique for all OpenFlow versions */
60202987
SH
4777 switch (ofp_version) {
4778 case OFP10_VERSION:
4779 case OFP11_VERSION:
4780 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
4781 case OFP12_VERSION:
2e1ae200 4782 case OFP13_VERSION:
50b73fe1 4783 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
c37c0382 4784 case OFP14_VERSION:
42dccab5 4785 case OFP15_VERSION:
b79d45a1 4786 case OFP16_VERSION:
50b73fe1
JR
4787 return OFPC_COMMON | OFPC12_PORT_BLOCKED | OFPC14_BUNDLES
4788 | OFPC14_FLOW_MONITORING;
60202987
SH
4789 default:
4790 /* Caller needs to check osf->header.version itself */
4791 return 0;
4792 }
4793}
4794
667bb1fb
BP
4795/* Pulls an OpenFlow "switch_features" structure from 'b' and decodes it into
4796 * an abstract representation in '*features', readying 'b' to iterate over the
4797 * OpenFlow port structures following 'osf' with later calls to
4798 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an OFPERR_*
4799 * value. */
9e1fd49b 4800enum ofperr
667bb1fb
BP
4801ofputil_pull_switch_features(struct ofpbuf *b,
4802 struct ofputil_switch_features *features)
9e1fd49b 4803{
667bb1fb
BP
4804 const struct ofp_header *oh = b->data;
4805 enum ofpraw raw = ofpraw_pull_assert(b);
4806 const struct ofp_switch_features *osf = ofpbuf_pull(b, sizeof *osf);
9e1fd49b
BP
4807 features->datapath_id = ntohll(osf->datapath_id);
4808 features->n_buffers = ntohl(osf->n_buffers);
4809 features->n_tables = osf->n_tables;
2e1ae200 4810 features->auxiliary_id = 0;
9e1fd49b 4811
60202987
SH
4812 features->capabilities = ntohl(osf->capabilities) &
4813 ofputil_capabilities_mask(oh->version);
9e1fd49b 4814
982697a4 4815 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
9e1fd49b
BP
4816 if (osf->capabilities & htonl(OFPC10_STP)) {
4817 features->capabilities |= OFPUTIL_C_STP;
4818 }
08d1e234
BP
4819 features->ofpacts = ofpact_bitmap_from_openflow(osf->actions,
4820 OFP10_VERSION);
2e1ae200
JR
4821 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
4822 || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
9e1fd49b
BP
4823 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
4824 features->capabilities |= OFPUTIL_C_GROUP_STATS;
4825 }
08d1e234 4826 features->ofpacts = 0;
2e1ae200
JR
4827 if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4828 features->auxiliary_id = osf->auxiliary_id;
4829 }
9e1fd49b
BP
4830 } else {
4831 return OFPERR_OFPBRC_BAD_VERSION;
4832 }
4833
4834 return 0;
4835}
4836
fca6d553
BP
4837/* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4838 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4839 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4840 *
4841 * Given a buffer 'b' that contains a Features Reply message, this message
4842 * checks if it contains a complete list of the switch's ports. Returns true,
4843 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4844 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4845 * message.
4846 *
4847 * When this function returns false, the caller should send an OFPST_PORT_DESC
4848 * stats request to get the ports. */
347b7ac4 4849bool
fca6d553 4850ofputil_switch_features_has_ports(struct ofpbuf *b)
347b7ac4 4851{
6fd6ed71 4852 struct ofp_header *oh = b->data;
13e1aff8 4853 size_t phy_port_size;
347b7ac4 4854
fca6d553 4855 if (oh->version >= OFP13_VERSION) {
13e1aff8 4856 /* OpenFlow 1.3+ never has ports in the feature reply. */
fca6d553 4857 return false;
13e1aff8
BP
4858 }
4859
4860 phy_port_size = (oh->version == OFP10_VERSION
4861 ? sizeof(struct ofp10_phy_port)
4862 : sizeof(struct ofp11_port));
4863 if (ntohs(oh->length) + phy_port_size <= UINT16_MAX) {
4864 /* There's room for additional ports in the feature reply.
4865 * Assume that the list is complete. */
347b7ac4
JP
4866 return true;
4867 }
13e1aff8
BP
4868
4869 /* The feature reply has no room for more ports. Probably the list is
4870 * truncated. Drop the ports and tell the caller to retrieve them with
4871 * OFPST_PORT_DESC. */
6fd6ed71 4872 b->size = sizeof *oh + sizeof(struct ofp_switch_features);
13e1aff8
BP
4873 ofpmsg_update_length(b);
4874 return false;
347b7ac4
JP
4875}
4876
9e1fd49b
BP
4877/* Returns a buffer owned by the caller that encodes 'features' in the format
4878 * required by 'protocol' with the given 'xid'. The caller should append port
4879 * information to the buffer with subsequent calls to
4880 * ofputil_put_switch_features_port(). */
4881struct ofpbuf *
4882ofputil_encode_switch_features(const struct ofputil_switch_features *features,
4883 enum ofputil_protocol protocol, ovs_be32 xid)
4884{
4885 struct ofp_switch_features *osf;
4886 struct ofpbuf *b;
2e3fa633
SH
4887 enum ofp_version version;
4888 enum ofpraw raw;
982697a4
BP
4889
4890 version = ofputil_protocol_to_ofp_version(protocol);
2e3fa633
SH
4891 switch (version) {
4892 case OFP10_VERSION:
4893 raw = OFPRAW_OFPT10_FEATURES_REPLY;
4894 break;
4895 case OFP11_VERSION:
4896 case OFP12_VERSION:
4897 raw = OFPRAW_OFPT11_FEATURES_REPLY;
4898 break;
2e1ae200 4899 case OFP13_VERSION:
c37c0382 4900 case OFP14_VERSION:
42dccab5 4901 case OFP15_VERSION:
b79d45a1 4902 case OFP16_VERSION:
2e1ae200
JR
4903 raw = OFPRAW_OFPT13_FEATURES_REPLY;
4904 break;
2e3fa633 4905 default:
428b2edd 4906 OVS_NOT_REACHED();
2e3fa633
SH
4907 }
4908 b = ofpraw_alloc_xid(raw, version, xid, 0);
982697a4 4909 osf = ofpbuf_put_zeros(b, sizeof *osf);
9e1fd49b
BP
4910 osf->datapath_id = htonll(features->datapath_id);
4911 osf->n_buffers = htonl(features->n_buffers);
4912 osf->n_tables = features->n_tables;
4913
60202987
SH
4914 osf->capabilities = htonl(features->capabilities &
4915 ofputil_capabilities_mask(version));
2e3fa633
SH
4916 switch (version) {
4917 case OFP10_VERSION:
9e1fd49b
BP
4918 if (features->capabilities & OFPUTIL_C_STP) {
4919 osf->capabilities |= htonl(OFPC10_STP);
4920 }
08d1e234
BP
4921 osf->actions = ofpact_bitmap_to_openflow(features->ofpacts,
4922 OFP10_VERSION);
2e3fa633 4923 break;
2e1ae200 4924 case OFP13_VERSION:
c37c0382 4925 case OFP14_VERSION:
42dccab5 4926 case OFP15_VERSION:
b79d45a1 4927 case OFP16_VERSION:
2e1ae200
JR
4928 osf->auxiliary_id = features->auxiliary_id;
4929 /* fall through */
2e3fa633
SH
4930 case OFP11_VERSION:
4931 case OFP12_VERSION:
9e1fd49b
BP
4932 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
4933 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
4934 }
2e3fa633
SH
4935 break;
4936 default:
428b2edd 4937 OVS_NOT_REACHED();
9e1fd49b
BP
4938 }
4939
4940 return b;
4941}
4942
4943/* Encodes 'pp' into the format required by the switch_features message already
4944 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4945 * and appends the encoded version to 'b'. */
4946void
4947ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
4948 struct ofpbuf *b)
4949{
6fd6ed71 4950 const struct ofp_header *oh = b->data;
9e1fd49b 4951
e0c91c0c 4952 if (oh->version < OFP13_VERSION) {
6b0f20ac
BP
4953 /* Try adding a port description to the message, but drop it again if
4954 * the buffer overflows. (This possibility for overflow is why
4955 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
6fd6ed71 4956 size_t start_ofs = b->size;
e0c91c0c 4957 ofputil_put_phy_port(oh->version, pp, b);
6fd6ed71
PS
4958 if (b->size > UINT16_MAX) {
4959 b->size = start_ofs;
6b0f20ac 4960 }
e0c91c0c 4961 }
9e1fd49b
BP
4962}
4963\f
4964/* ofputil_port_status */
4965
4966/* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4967 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4968enum ofperr
982697a4 4969ofputil_decode_port_status(const struct ofp_header *oh,
9e1fd49b
BP
4970 struct ofputil_port_status *ps)
4971{
0a2869d5 4972 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
982697a4 4973 ofpraw_pull_assert(&b);
982697a4 4974
0a2869d5 4975 const struct ofp_port_status *ops = ofpbuf_pull(&b, sizeof *ops);
9e1fd49b
BP
4976 if (ops->reason != OFPPR_ADD &&
4977 ops->reason != OFPPR_DELETE &&
4978 ops->reason != OFPPR_MODIFY) {
4979 return OFPERR_NXBRC_BAD_REASON;
4980 }
4981 ps->reason = ops->reason;
4982
0a2869d5 4983 int retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
cb22974d 4984 ovs_assert(retval != EOF);
9e1fd49b
BP
4985 return retval;
4986}
4987
4988/* Converts the abstract form of a "port status" message in '*ps' into an
4989 * OpenFlow message suitable for 'protocol', and returns that encoded form in
4990 * a buffer owned by the caller. */
4991struct ofpbuf *
4992ofputil_encode_port_status(const struct ofputil_port_status *ps,
4993 enum ofputil_protocol protocol)
4994{
4995 struct ofp_port_status *ops;
4996 struct ofpbuf *b;
2e3fa633
SH
4997 enum ofp_version version;
4998 enum ofpraw raw;
982697a4
BP
4999
5000 version = ofputil_protocol_to_ofp_version(protocol);
2e3fa633
SH
5001 switch (version) {
5002 case OFP10_VERSION:
5003 raw = OFPRAW_OFPT10_PORT_STATUS;
5004 break;
5005
5006 case OFP11_VERSION:
5007 case OFP12_VERSION:
2e1ae200 5008 case OFP13_VERSION:
2e3fa633
SH
5009 raw = OFPRAW_OFPT11_PORT_STATUS;
5010 break;
5011
8c3cc785 5012 case OFP14_VERSION:
42dccab5 5013 case OFP15_VERSION:
8c3cc785
BP
5014 raw = OFPRAW_OFPT14_PORT_STATUS;
5015 break;
5016
2f2b904f
BP
5017 case OFP16_VERSION:
5018 raw = OFPRAW_OFPT16_PORT_STATUS;
5019 break;
5020
2e3fa633 5021 default:
428b2edd 5022 OVS_NOT_REACHED();
2e3fa633
SH
5023 }
5024
5025 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
982697a4 5026 ops = ofpbuf_put_zeros(b, sizeof *ops);
9e1fd49b 5027 ops->reason = ps->reason;
982697a4
BP
5028 ofputil_put_phy_port(version, &ps->desc, b);
5029 ofpmsg_update_length(b);
9e1fd49b
BP
5030 return b;
5031}
918f2b82 5032
9e1fd49b
BP
5033/* ofputil_port_mod */
5034
18cc69d9
BP
5035static enum ofperr
5036parse_port_mod_ethernet_property(struct ofpbuf *property,
5037 struct ofputil_port_mod *pm)
5038{
b611d3ac
BP
5039 ovs_be32 advertise;
5040 enum ofperr error;
18cc69d9 5041
b611d3ac
BP
5042 error = ofpprop_parse_be32(property, &advertise);
5043 if (!error) {
5044 pm->advertise = netdev_port_features_from_ofp11(advertise);
18cc69d9 5045 }
b611d3ac 5046 return error;
18cc69d9
BP
5047}
5048
2f2b904f
BP
5049static enum ofperr
5050ofputil_decode_ofp10_port_mod(const struct ofp10_port_mod *opm,
5051 struct ofputil_port_mod *pm)
5052{
5053 pm->port_no = u16_to_ofp(ntohs(opm->port_no));
5054 pm->hw_addr = opm->hw_addr;
5055 pm->config = ntohl(opm->config) & OFPPC10_ALL;
5056 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
5057 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
5058 return 0;
5059}
5060
5061static enum ofperr
5062ofputil_decode_ofp11_port_mod(const struct ofp11_port_mod *opm,
5063 struct ofputil_port_mod *pm)
9e1fd49b 5064{
2f2b904f 5065 enum ofperr error;
9e1fd49b 5066
2f2b904f
BP
5067 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
5068 if (error) {
5069 return error;
5070 }
5071
5072 pm->hw_addr = opm->hw_addr;
5073 pm->config = ntohl(opm->config) & OFPPC11_ALL;
5074 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
5075 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
5076
5077 return 0;
5078}
5079
5080static enum ofperr
5081ofputil_decode_ofp14_port_mod_properties(struct ofpbuf *b, bool loose,
5082 struct ofputil_port_mod *pm)
5083{
5084 while (b->size > 0) {
5085 struct ofpbuf property;
9e1fd49b 5086 enum ofperr error;
2f2b904f 5087 uint64_t type;
9e1fd49b 5088
2f2b904f 5089 error = ofpprop_pull(b, &property, &type);
9e1fd49b
BP
5090 if (error) {
5091 return error;
5092 }
5093
2f2b904f
BP
5094 switch (type) {
5095 case OFPPMPT14_ETHERNET:
5096 error = parse_port_mod_ethernet_property(&property, pm);
5097 break;
18cc69d9 5098
2f2b904f
BP
5099 default:
5100 error = OFPPROP_UNKNOWN(loose, "port_mod", type);
5101 break;
5102 }
18cc69d9 5103
18cc69d9
BP
5104 if (error) {
5105 return error;
5106 }
2f2b904f
BP
5107 }
5108 return 0;
5109}
18cc69d9 5110
2f2b904f
BP
5111static enum ofperr
5112ofputil_decode_ofp14_port_mod(struct ofpbuf *b, bool loose,
5113 struct ofputil_port_mod *pm)
5114{
5115 const struct ofp14_port_mod *opm = ofpbuf_pull(b, sizeof *opm);
5116 enum ofperr error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
5117 if (error) {
5118 return error;
5119 }
18cc69d9 5120
2f2b904f
BP
5121 pm->hw_addr = opm->hw_addr;
5122 pm->config = ntohl(opm->config) & OFPPC11_ALL;
5123 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
18cc69d9 5124
2f2b904f
BP
5125 return ofputil_decode_ofp14_port_mod_properties(b, loose, pm);
5126}
18cc69d9 5127
2f2b904f
BP
5128static enum ofperr
5129ofputil_decode_ofp16_port_mod(struct ofpbuf *b, bool loose,
5130 struct ofputil_port_mod *pm)
5131{
5132 const struct ofp16_port_mod *opm = ofpbuf_pull(b, sizeof *opm);
5133 enum ofperr error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
5134 if (error) {
5135 return error;
5136 }
18cc69d9 5137
2f2b904f
BP
5138 if (opm->hw_addr_type & htons(OFPPHAT16_EUI48)) {
5139 pm->hw_addr = opm->hw_addr;
5140 }
5141 if (opm->hw_addr_type & htons(OFPPHAT16_EUI64)) {
5142 pm->hw_addr64 = opm->hw_addr64;
5143 }
5144 pm->hw_addr = opm->hw_addr;
5145 pm->config = ntohl(opm->config) & OFPPC11_ALL;
5146 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
18cc69d9 5147
2f2b904f
BP
5148 return ofputil_decode_ofp14_port_mod_properties(b, loose, pm);
5149}
5150
5151/* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
5152 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5153enum ofperr
5154ofputil_decode_port_mod(const struct ofp_header *oh,
5155 struct ofputil_port_mod *pm, bool loose)
5156{
5157 memset(pm, 0, sizeof *pm);
5158
5159 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
5160 enum ofpraw raw = ofpraw_pull_assert(&b);
5161 if (raw == OFPRAW_OFPT10_PORT_MOD) {
5162 return ofputil_decode_ofp10_port_mod(b.data, pm);
5163 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
5164 return ofputil_decode_ofp11_port_mod(b.data, pm);
5165 } else if (raw == OFPRAW_OFPT14_PORT_MOD) {
5166 return ofputil_decode_ofp14_port_mod(&b, loose, pm);
5167 } else if (raw == OFPRAW_OFPT16_PORT_MOD) {
5168 return ofputil_decode_ofp16_port_mod(&b, loose, pm);
9e1fd49b 5169 } else {
982697a4 5170 return OFPERR_OFPBRC_BAD_TYPE;
9e1fd49b
BP
5171 }
5172
5173 pm->config &= pm->mask;
5174 return 0;
5175}
5176
5177/* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
5178 * message suitable for 'protocol', and returns that encoded form in a buffer
5179 * owned by the caller. */
5180struct ofpbuf *
5181ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
5182 enum ofputil_protocol protocol)
5183{
2e3fa633 5184 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
9e1fd49b
BP
5185 struct ofpbuf *b;
5186
2e3fa633
SH
5187 switch (ofp_version) {
5188 case OFP10_VERSION: {
9e1fd49b
BP
5189 struct ofp10_port_mod *opm;
5190
982697a4
BP
5191 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
5192 opm = ofpbuf_put_zeros(b, sizeof *opm);
4e022ec0 5193 opm->port_no = htons(ofp_to_u16(pm->port_no));
74ff3298 5194 opm->hw_addr = pm->hw_addr;
9e1fd49b
BP
5195 opm->config = htonl(pm->config & OFPPC10_ALL);
5196 opm->mask = htonl(pm->mask & OFPPC10_ALL);
5197 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
2e3fa633
SH
5198 break;
5199 }
5200
5fe7c919 5201 case OFP11_VERSION:
2e1ae200
JR
5202 case OFP12_VERSION:
5203 case OFP13_VERSION: {
9e1fd49b
BP
5204 struct ofp11_port_mod *opm;
5205
982697a4
BP
5206 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
5207 opm = ofpbuf_put_zeros(b, sizeof *opm);
026a5179 5208 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
74ff3298 5209 opm->hw_addr = pm->hw_addr;
9e1fd49b
BP
5210 opm->config = htonl(pm->config & OFPPC11_ALL);
5211 opm->mask = htonl(pm->mask & OFPPC11_ALL);
5212 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
2e3fa633
SH
5213 break;
5214 }
42dccab5 5215 case OFP14_VERSION:
2f2b904f 5216 case OFP15_VERSION: {
18cc69d9
BP
5217 struct ofp14_port_mod *opm;
5218
b611d3ac 5219 b = ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD, ofp_version, 0);
18cc69d9
BP
5220 opm = ofpbuf_put_zeros(b, sizeof *opm);
5221 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
74ff3298 5222 opm->hw_addr = pm->hw_addr;
18cc69d9
BP
5223 opm->config = htonl(pm->config & OFPPC11_ALL);
5224 opm->mask = htonl(pm->mask & OFPPC11_ALL);
5225
5226 if (pm->advertise) {
b611d3ac
BP
5227 ofpprop_put_be32(b, OFPPMPT14_ETHERNET,
5228 netdev_port_features_to_ofp11(pm->advertise));
18cc69d9 5229 }
c37c0382 5230 break;
18cc69d9 5231 }
2f2b904f
BP
5232 case OFP16_VERSION: {
5233 struct ofp16_port_mod *opm;
5234
5235 b = ofpraw_alloc(OFPRAW_OFPT16_PORT_MOD, ofp_version, 0);
5236 opm = ofpbuf_put_zeros(b, sizeof *opm);
5237 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
5238 if (!eth_addr_is_zero(pm->hw_addr)) {
5239 opm->hw_addr_type |= htons(OFPPHAT16_EUI48);
5240 opm->hw_addr = pm->hw_addr;
5241 }
5242 if (!eth_addr64_is_zero(pm->hw_addr64)) {
5243 opm->hw_addr_type |= htons(OFPPHAT16_EUI64);
5244 opm->hw_addr64 = pm->hw_addr64;
5245 }
5246 opm->config = htonl(pm->config & OFPPC11_ALL);
5247 opm->mask = htonl(pm->mask & OFPPC11_ALL);
5248
5249 if (pm->advertise) {
5250 ofpprop_put_be32(b, OFPPMPT14_ETHERNET,
5251 netdev_port_features_to_ofp11(pm->advertise));
5252 }
5253 break;
5254 }
918f2b82 5255 default:
428b2edd 5256 OVS_NOT_REACHED();
918f2b82
AZ
5257 }
5258
5259 return b;
5260}
72b25867
BP
5261\f
5262/* Table features. */
918f2b82 5263
5deff5aa 5264static enum ofperr
9a4eddbb 5265pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload,
34a543e3 5266 uint64_t *typep)
5deff5aa 5267{
9a4eddbb 5268 enum ofperr error;
5deff5aa 5269
c5562271 5270 error = ofpprop_pull(msg, payload, typep);
9a4eddbb 5271 if (payload && !error) {
b611d3ac 5272 ofpbuf_pull(payload, (char *)payload->msg - (char *)payload->header);
5deff5aa 5273 }
9a4eddbb 5274 return error;
5deff5aa
AW
5275}
5276
5277static enum ofperr
08d1e234
BP
5278parse_action_bitmap(struct ofpbuf *payload, enum ofp_version ofp_version,
5279 uint64_t *ofpacts)
5deff5aa 5280{
08d1e234 5281 uint32_t types = 0;
5deff5aa 5282
6fd6ed71 5283 while (payload->size > 0) {
08d1e234 5284 enum ofperr error;
34a543e3 5285 uint64_t type;
08d1e234 5286
34a543e3 5287 error = ofpprop_pull__(payload, NULL, 1, 0x10000, &type);
5deff5aa
AW
5288 if (error) {
5289 return error;
5290 }
08d1e234
BP
5291 if (type < CHAR_BIT * sizeof types) {
5292 types |= 1u << type;
5deff5aa
AW
5293 }
5294 }
08d1e234
BP
5295
5296 *ofpacts = ofpact_bitmap_from_openflow(htonl(types), ofp_version);
5deff5aa
AW
5297 return 0;
5298}
5299
5300static enum ofperr
5301parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts)
5302{
5303 *insts = 0;
6fd6ed71 5304 while (payload->size > 0) {
5deff5aa
AW
5305 enum ovs_instruction_type inst;
5306 enum ofperr error;
34a543e3 5307 uint64_t ofpit;
5deff5aa 5308
72b25867
BP
5309 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
5310 * It seems clear that they aren't padded to 8 bytes, though, because
5311 * both standards say that "non-experimenter instructions are 4 bytes"
5312 * and do not mention any padding before the first instruction ID.
5313 * (There wouldn't be any point in padding to 8 bytes if the IDs were
5314 * aligned on an odd 4-byte boundary.)
5315 *
5316 * Anyway, we just assume they're all glommed together on byte
5317 * boundaries. */
34a543e3 5318 error = ofpprop_pull__(payload, NULL, 1, 0x10000, &ofpit);
5deff5aa
AW
5319 if (error) {
5320 return error;
5321 }
5322
5323 error = ovs_instruction_type_from_inst_type(&inst, ofpit);
5324 if (!error) {
5325 *insts |= 1u << inst;
5326 } else if (!loose) {
5327 return error;
5328 }
5329 }
5330 return 0;
5331}
5332
5333static enum ofperr
5334parse_table_features_next_table(struct ofpbuf *payload,
5335 unsigned long int *next_tables)
5336{
5337 size_t i;
5338
5339 memset(next_tables, 0, bitmap_n_bytes(255));
6fd6ed71
PS
5340 for (i = 0; i < payload->size; i++) {
5341 uint8_t id = ((const uint8_t *) payload->data)[i];
5deff5aa 5342 if (id >= 255) {
9a4eddbb 5343 return OFPERR_OFPBPC_BAD_VALUE;
5deff5aa
AW
5344 }
5345 bitmap_set1(next_tables, id);
5346 }
5347 return 0;
5348}
5349
5deff5aa
AW
5350static enum ofperr
5351parse_oxms(struct ofpbuf *payload, bool loose,
abadfcb0 5352 struct mf_bitmap *exactp, struct mf_bitmap *maskedp)
5deff5aa 5353{
abadfcb0
BP
5354 struct mf_bitmap exact = MF_BITMAP_INITIALIZER;
5355 struct mf_bitmap masked = MF_BITMAP_INITIALIZER;
5deff5aa 5356
6fd6ed71 5357 while (payload->size > 0) {
5deff5aa
AW
5358 const struct mf_field *field;
5359 enum ofperr error;
5360 bool hasmask;
5361
04f48a68 5362 error = nx_pull_header(payload, NULL, &field, &hasmask);
5deff5aa 5363 if (!error) {
abadfcb0 5364 bitmap_set1(hasmask ? masked.bm : exact.bm, field->id);
5deff5aa
AW
5365 } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) {
5366 return error;
5367 }
5368 }
5369 if (exactp) {
5370 *exactp = exact;
abadfcb0 5371 } else if (!bitmap_is_all_zeros(exact.bm, MFF_N_IDS)) {
5deff5aa
AW
5372 return OFPERR_OFPBMC_BAD_MASK;
5373 }
5374 if (maskedp) {
5375 *maskedp = masked;
abadfcb0 5376 } else if (!bitmap_is_all_zeros(masked.bm, MFF_N_IDS)) {
5deff5aa
AW
5377 return OFPERR_OFPBMC_BAD_MASK;
5378 }
5379 return 0;
5380}
5381
5382/* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
5383 * ofputil_table_features in 'tf'.
5384 *
5385 * If 'loose' is true, this function ignores properties and values that it does
5386 * not understand, as a controller would want to do when interpreting
5387 * capabilities provided by a switch. If 'loose' is false, this function
5388 * treats unknown properties and values as an error, as a switch would want to
5389 * do when interpreting a configuration request made by a controller.
5390 *
5391 * A single OpenFlow message can specify features for multiple tables. Calling
5392 * this function multiple times for a single 'msg' iterates through the tables
5393 * in the message. The caller must initially leave 'msg''s layer pointers null
5394 * and not modify them between calls.
5395 *
5396 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
5397 * a positive "enum ofperr" value. */
5398int
5399ofputil_decode_table_features(struct ofpbuf *msg,
5400 struct ofputil_table_features *tf, bool loose)
5401{
3c1bb396
BP
5402 memset(tf, 0, sizeof *tf);
5403
6fd6ed71 5404 if (!msg->header) {
5deff5aa
AW
5405 ofpraw_pull_assert(msg);
5406 }
5407
6fd6ed71 5408 if (!msg->size) {
5deff5aa
AW
5409 return EOF;
5410 }
5411
0a2869d5
BP
5412 const struct ofp_header *oh = msg->header;
5413 struct ofp13_table_features *otf = msg->data;
6fd6ed71 5414 if (msg->size < sizeof *otf) {
9a4eddbb 5415 return OFPERR_OFPBPC_BAD_LEN;
5deff5aa
AW
5416 }
5417
0a2869d5 5418 unsigned int len = ntohs(otf->length);
6fd6ed71 5419 if (len < sizeof *otf || len % 8 || len > msg->size) {
9a4eddbb 5420 return OFPERR_OFPBPC_BAD_LEN;
5deff5aa 5421 }
5deff5aa
AW
5422
5423 tf->table_id = otf->table_id;
5424 if (tf->table_id == OFPTT_ALL) {
5425 return OFPERR_OFPTFFC_BAD_TABLE;
5426 }
5427
f9ac0f03 5428 ovs_strlcpy_arrays(tf->name, otf->name);
5deff5aa
AW
5429 tf->metadata_match = otf->metadata_match;
5430 tf->metadata_write = otf->metadata_write;
82c22d34
BP
5431 tf->miss_config = OFPUTIL_TABLE_MISS_DEFAULT;
5432 if (oh->version >= OFP14_VERSION) {
5433 uint32_t caps = ntohl(otf->capabilities);
5434 tf->supports_eviction = (caps & OFPTC14_EVICTION) != 0;
5435 tf->supports_vacancy_events = (caps & OFPTC14_VACANCY_EVENTS) != 0;
5436 } else {
5437 tf->supports_eviction = -1;
5438 tf->supports_vacancy_events = -1;
5439 }
5deff5aa
AW
5440 tf->max_entries = ntohl(otf->max_entries);
5441
0a2869d5
BP
5442 struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
5443 len);
5444 ofpbuf_pull(&properties, sizeof *otf);
6fd6ed71 5445 while (properties.size > 0) {
5deff5aa
AW
5446 struct ofpbuf payload;
5447 enum ofperr error;
34a543e3 5448 uint64_t type;
5deff5aa 5449
ae665de5 5450 error = pull_table_feature_property(&properties, &payload, &type);
5deff5aa
AW
5451 if (error) {
5452 return error;
5453 }
5454
5455 switch ((enum ofp13_table_feature_prop_type) type) {
5456 case OFPTFPT13_INSTRUCTIONS:
5457 error = parse_instruction_ids(&payload, loose,
5458 &tf->nonmiss.instructions);
5459 break;
5460 case OFPTFPT13_INSTRUCTIONS_MISS:
5461 error = parse_instruction_ids(&payload, loose,
5462 &tf->miss.instructions);
5463 break;
5464
5465 case OFPTFPT13_NEXT_TABLES:
5466 error = parse_table_features_next_table(&payload,
5467 tf->nonmiss.next);
5468 break;
5469 case OFPTFPT13_NEXT_TABLES_MISS:
5470 error = parse_table_features_next_table(&payload, tf->miss.next);
5471 break;
5472
5473 case OFPTFPT13_WRITE_ACTIONS:
08d1e234
BP
5474 error = parse_action_bitmap(&payload, oh->version,
5475 &tf->nonmiss.write.ofpacts);
5deff5aa
AW
5476 break;
5477 case OFPTFPT13_WRITE_ACTIONS_MISS:
08d1e234
BP
5478 error = parse_action_bitmap(&payload, oh->version,
5479 &tf->miss.write.ofpacts);
5deff5aa
AW
5480 break;
5481
5482 case OFPTFPT13_APPLY_ACTIONS:
08d1e234
BP
5483 error = parse_action_bitmap(&payload, oh->version,
5484 &tf->nonmiss.apply.ofpacts);
5deff5aa
AW
5485 break;
5486 case OFPTFPT13_APPLY_ACTIONS_MISS:
08d1e234
BP
5487 error = parse_action_bitmap(&payload, oh->version,
5488 &tf->miss.apply.ofpacts);
5deff5aa
AW
5489 break;
5490
5491 case OFPTFPT13_MATCH:
5492 error = parse_oxms(&payload, loose, &tf->match, &tf->mask);
5493 break;
5494 case OFPTFPT13_WILDCARDS:
5495 error = parse_oxms(&payload, loose, &tf->wildcard, NULL);
5496 break;
5497
5498 case OFPTFPT13_WRITE_SETFIELD:
5499 error = parse_oxms(&payload, loose,
5500 &tf->nonmiss.write.set_fields, NULL);
5501 break;
5502 case OFPTFPT13_WRITE_SETFIELD_MISS:
5503 error = parse_oxms(&payload, loose,
5504 &tf->miss.write.set_fields, NULL);
5505 break;
5506 case OFPTFPT13_APPLY_SETFIELD:
5507 error = parse_oxms(&payload, loose,
5508 &tf->nonmiss.apply.set_fields, NULL);
5509 break;
5510 case OFPTFPT13_APPLY_SETFIELD_MISS:
5511 error = parse_oxms(&payload, loose,
5512 &tf->miss.apply.set_fields, NULL);
5513 break;
5514
5515 case OFPTFPT13_EXPERIMENTER:
5516 case OFPTFPT13_EXPERIMENTER_MISS:
9a4eddbb 5517 default:
34a543e3 5518 error = OFPPROP_UNKNOWN(loose, "table features", type);
5deff5aa
AW
5519 break;
5520 }
5521 if (error) {
5522 return error;
5523 }
5524 }
5525
5526 /* Fix inconsistencies:
5527 *
e7227821
BP
5528 * - Turn on 'match' bits that are set in 'mask', because maskable
5529 * fields are matchable.
5deff5aa
AW
5530 *
5531 * - Turn on 'wildcard' bits that are set in 'mask', because a field
e7227821
BP
5532 * that is arbitrarily maskable can be wildcarded entirely.
5533 *
5534 * - Turn off 'wildcard' bits that are not in 'match', because a field
5535 * must be matchable for it to be meaningfully wildcarded. */
5536 bitmap_or(tf->match.bm, tf->mask.bm, MFF_N_IDS);
abadfcb0 5537 bitmap_or(tf->wildcard.bm, tf->mask.bm, MFF_N_IDS);
e7227821 5538 bitmap_and(tf->wildcard.bm, tf->match.bm, MFF_N_IDS);
5deff5aa
AW
5539
5540 return 0;
5541}
5542
5543/* Encodes and returns a request to obtain the table features of a switch.
5544 * The message is encoded for OpenFlow version 'ofp_version'. */
5545struct ofpbuf *
5546ofputil_encode_table_features_request(enum ofp_version ofp_version)
5547{
5548 struct ofpbuf *request = NULL;
5549
5550 switch (ofp_version) {
5551 case OFP10_VERSION:
5552 case OFP11_VERSION:
5553 case OFP12_VERSION:
5554 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
5555 "(\'-O OpenFlow13\')");
5556 case OFP13_VERSION:
5557 case OFP14_VERSION:
42dccab5 5558 case OFP15_VERSION:
b79d45a1 5559 case OFP16_VERSION:
5deff5aa
AW
5560 request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST,
5561 ofp_version, 0);
5562 break;
5563 default:
5564 OVS_NOT_REACHED();
5565 }
5566
5567 return request;
5568}
5569
3c4e10fb
BP
5570static void
5571put_fields_property(struct ofpbuf *reply,
5572 const struct mf_bitmap *fields,
5573 const struct mf_bitmap *masks,
5574 enum ofp13_table_feature_prop_type property,
5575 enum ofp_version version)
5576{
5577 size_t start_ofs;
5578 int field;
5579
c5562271 5580 start_ofs = ofpprop_start(reply, property);
3c4e10fb 5581 BITMAP_FOR_EACH_1 (field, MFF_N_IDS, fields->bm) {
178742f9
BP
5582 nx_put_header(reply, field, version,
5583 masks && bitmap_is_set(masks->bm, field));
3c4e10fb 5584 }
c5562271 5585 ofpprop_end(reply, start_ofs);
3c4e10fb
BP
5586}
5587
5588static void
5589put_table_action_features(struct ofpbuf *reply,
5590 const struct ofputil_table_action_features *taf,
5591 enum ofp13_table_feature_prop_type actions_type,
5592 enum ofp13_table_feature_prop_type set_fields_type,
5593 int miss_offset, enum ofp_version version)
5594{
c5562271
BP
5595 ofpprop_put_bitmap(reply, actions_type + miss_offset,
5596 ntohl(ofpact_bitmap_to_openflow(taf->ofpacts,
5597 version)));
3c4e10fb
BP
5598 put_fields_property(reply, &taf->set_fields, NULL,
5599 set_fields_type + miss_offset, version);
5600}
5601
5602static void
5603put_table_instruction_features(
5604 struct ofpbuf *reply, const struct ofputil_table_instruction_features *tif,
5605 int miss_offset, enum ofp_version version)
5606{
5607 size_t start_ofs;
5608 uint8_t table_id;
5609
c5562271
BP
5610 ofpprop_put_bitmap(reply, OFPTFPT13_INSTRUCTIONS + miss_offset,
5611 ntohl(ovsinst_bitmap_to_openflow(tif->instructions,
5612 version)));
3c4e10fb 5613
c5562271 5614 start_ofs = ofpprop_start(reply, OFPTFPT13_NEXT_TABLES + miss_offset);
3c4e10fb
BP
5615 BITMAP_FOR_EACH_1 (table_id, 255, tif->next) {
5616 ofpbuf_put(reply, &table_id, 1);
5617 }
c5562271 5618 ofpprop_end(reply, start_ofs);
3c4e10fb
BP
5619
5620 put_table_action_features(reply, &tif->write,
5621 OFPTFPT13_WRITE_ACTIONS,
5622 OFPTFPT13_WRITE_SETFIELD, miss_offset, version);
5623 put_table_action_features(reply, &tif->apply,
5624 OFPTFPT13_APPLY_ACTIONS,
5625 OFPTFPT13_APPLY_SETFIELD, miss_offset, version);
5626}
5627
5628void
5629ofputil_append_table_features_reply(const struct ofputil_table_features *tf,
ca6ba700 5630 struct ovs_list *replies)
3c4e10fb 5631{
417e7e66 5632 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
3c4e10fb 5633 enum ofp_version version = ofpmp_version(replies);
6fd6ed71 5634 size_t start_ofs = reply->size;
3c4e10fb
BP
5635 struct ofp13_table_features *otf;
5636
5637 otf = ofpbuf_put_zeros(reply, sizeof *otf);
5638 otf->table_id = tf->table_id;
f9ac0f03 5639 ovs_strlcpy_arrays(otf->name, tf->name);
3c4e10fb
BP
5640 otf->metadata_match = tf->metadata_match;
5641 otf->metadata_write = tf->metadata_write;
82c22d34
BP
5642 if (version >= OFP14_VERSION) {
5643 if (tf->supports_eviction) {
5644 otf->capabilities |= htonl(OFPTC14_EVICTION);
5645 }
5646 if (tf->supports_vacancy_events) {
5647 otf->capabilities |= htonl(OFPTC14_VACANCY_EVENTS);
5648 }
5649 }
3c4e10fb
BP
5650 otf->max_entries = htonl(tf->max_entries);
5651
5652 put_table_instruction_features(reply, &tf->nonmiss, 0, version);
5653 put_table_instruction_features(reply, &tf->miss, 1, version);
5654
5655 put_fields_property(reply, &tf->match, &tf->mask,
5656 OFPTFPT13_MATCH, version);
5657 put_fields_property(reply, &tf->wildcard, NULL,
5658 OFPTFPT13_WILDCARDS, version);
5659
5660 otf = ofpbuf_at_assert(reply, start_ofs, sizeof *otf);
6fd6ed71 5661 otf->length = htons(reply->size - start_ofs);
3c4e10fb
BP
5662 ofpmp_postappend(replies, start_ofs);
5663}
5664
bab86012
SJ
5665static enum ofperr
5666parse_table_desc_vacancy_property(struct ofpbuf *property,
5667 struct ofputil_table_desc *td)
5668{
5669 struct ofp14_table_mod_prop_vacancy *otv = property->data;
5670
5671 if (property->size != sizeof *otv) {
5672 return OFPERR_OFPBPC_BAD_LEN;
5673 }
5674
5675 td->table_vacancy.vacancy_down = otv->vacancy_down;
5676 td->table_vacancy.vacancy_up = otv->vacancy_up;
5677 td->table_vacancy.vacancy = otv->vacancy;
5678 return 0;
5679}
5680
03c72922
BP
5681/* Decodes the next OpenFlow "table desc" message (of possibly several) from
5682 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
5683 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
5684 * value. */
5685int
5686ofputil_decode_table_desc(struct ofpbuf *msg,
5687 struct ofputil_table_desc *td,
5688 enum ofp_version version)
5689{
03c72922
BP
5690 memset(td, 0, sizeof *td);
5691
5692 if (!msg->header) {
5693 ofpraw_pull_assert(msg);
5694 }
5695
5696 if (!msg->size) {
5697 return EOF;
5698 }
5699
0a2869d5 5700 struct ofp14_table_desc *otd = ofpbuf_try_pull(msg, sizeof *otd);
03c72922
BP
5701 if (!otd) {
5702 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply has %"PRIu32" "
5703 "leftover bytes at end", msg->size);
5704 return OFPERR_OFPBRC_BAD_LEN;
5705 }
5706
5707 td->table_id = otd->table_id;
0a2869d5 5708 size_t length = ntohs(otd->length);
03c72922
BP
5709 if (length < sizeof *otd || length - sizeof *otd > msg->size) {
5710 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply claims invalid "
5711 "length %"PRIuSIZE, length);
5712 return OFPERR_OFPBRC_BAD_LEN;
5713 }
5714 length -= sizeof *otd;
03c72922
BP
5715
5716 td->eviction = ofputil_decode_table_eviction(otd->config, version);
bab86012 5717 td->vacancy = ofputil_decode_table_vacancy(otd->config, version);
03c72922
BP
5718 td->eviction_flags = UINT32_MAX;
5719
0a2869d5
BP
5720 struct ofpbuf properties = ofpbuf_const_initializer(
5721 ofpbuf_pull(msg, length), length);
03c72922
BP
5722 while (properties.size > 0) {
5723 struct ofpbuf payload;
5724 enum ofperr error;
34a543e3 5725 uint64_t type;
03c72922 5726
c5562271 5727 error = ofpprop_pull(&properties, &payload, &type);
03c72922
BP
5728 if (error) {
5729 return error;
5730 }
5731
5732 switch (type) {
5733 case OFPTMPT14_EVICTION:
b611d3ac 5734 error = ofpprop_parse_u32(&payload, &td->eviction_flags);
03c72922
BP
5735 break;
5736
bab86012
SJ
5737 case OFPTMPT14_VACANCY:
5738 error = parse_table_desc_vacancy_property(&payload, td);
5739 break;
5740
03c72922 5741 default:
34a543e3 5742 error = OFPPROP_UNKNOWN(true, "table_desc", type);
03c72922
BP
5743 break;
5744 }
5745
5746 if (error) {
5747 return error;
5748 }
5749 }
5750
5751 return 0;
5752}
5753
5754/* Encodes and returns a request to obtain description of tables of a switch.
5755 * The message is encoded for OpenFlow version 'ofp_version'. */
5756struct ofpbuf *
5757ofputil_encode_table_desc_request(enum ofp_version ofp_version)
5758{
5759 struct ofpbuf *request = NULL;
5760
5761 if (ofp_version >= OFP14_VERSION) {
5762 request = ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST,
5763 ofp_version, 0);
5764 } else {
5765 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
5766 "(\'-O OpenFlow14\')");
5767 }
5768
5769 return request;
5770}
5771
5772/* Function to append Table desc information in a reply list. */
5773void
5774ofputil_append_table_desc_reply(const struct ofputil_table_desc *td,
5775 struct ovs_list *replies,
5776 enum ofp_version version)
5777{
417e7e66 5778 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
03c72922
BP
5779 size_t start_otd;
5780 struct ofp14_table_desc *otd;
5781
5782 start_otd = reply->size;
5783 ofpbuf_put_zeros(reply, sizeof *otd);
5784 if (td->eviction_flags != UINT32_MAX) {
b611d3ac 5785 ofpprop_put_u32(reply, OFPTMPT14_EVICTION, td->eviction_flags);
03c72922 5786 }
bab86012
SJ
5787 if (td->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5788 struct ofp14_table_mod_prop_vacancy *otv;
5789
303721ee 5790 otv = ofpprop_put_zeros(reply, OFPTMPT14_VACANCY, sizeof *otv);
bab86012
SJ
5791 otv->vacancy_down = td->table_vacancy.vacancy_down;
5792 otv->vacancy_up = td->table_vacancy.vacancy_up;
5793 otv->vacancy = td->table_vacancy.vacancy;
5794 }
03c72922
BP
5795
5796 otd = ofpbuf_at_assert(reply, start_otd, sizeof *otd);
5797 otd->length = htons(reply->size - start_otd);
5798 otd->table_id = td->table_id;
5799 otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
de7d3c07
SJ
5800 td->eviction, td->vacancy,
5801 version);
03c72922
BP
5802 ofpmp_postappend(replies, start_otd);
5803}
5804
de7d3c07
SJ
5805/* This function parses Vacancy property, and decodes the
5806 * ofp14_table_mod_prop_vacancy in ofputil_table_mod.
5807 * Returns OFPERR_OFPBPC_BAD_VALUE error code when vacancy_down is
5808 * greater than vacancy_up and also when current vacancy has non-zero
5809 * value. Returns 0 on success. */
5810static enum ofperr
5811parse_table_mod_vacancy_property(struct ofpbuf *property,
5812 struct ofputil_table_mod *tm)
5813{
5814 struct ofp14_table_mod_prop_vacancy *otv = property->data;
5815
5816 if (property->size != sizeof *otv) {
5817 return OFPERR_OFPBPC_BAD_LEN;
5818 }
5819 tm->table_vacancy.vacancy_down = otv->vacancy_down;
5820 tm->table_vacancy.vacancy_up = otv->vacancy_up;
5821 if (tm->table_vacancy.vacancy_down > tm->table_vacancy.vacancy_up) {
c5562271
BP
5822 OFPPROP_LOG(&bad_ofmsg_rl, false,
5823 "Value of vacancy_down is greater than vacancy_up");
de7d3c07
SJ
5824 return OFPERR_OFPBPC_BAD_VALUE;
5825 }
5826 if (tm->table_vacancy.vacancy_down > 100 ||
5827 tm->table_vacancy.vacancy_up > 100) {
c5562271
BP
5828 OFPPROP_LOG(&bad_ofmsg_rl, false, "Vacancy threshold percentage "
5829 "should not be greater than 100");
de7d3c07
SJ
5830 return OFPERR_OFPBPC_BAD_VALUE;
5831 }
5832 tm->table_vacancy.vacancy = otv->vacancy;
5833 if (tm->table_vacancy.vacancy) {
c5562271
BP
5834 OFPPROP_LOG(&bad_ofmsg_rl, false,
5835 "Vacancy value should be zero for table-mod messages");
de7d3c07
SJ
5836 return OFPERR_OFPBPC_BAD_VALUE;
5837 }
5838 return 0;
5839}
5840
5841/* Given 'config', taken from an OpenFlow 'version' message that specifies
5842 * table configuration (a table mod, table stats, or table features message),
5843 * returns the table vacancy configuration that it specifies.
5844 *
5845 * Only OpenFlow 1.4 and later specify table vacancy configuration this way,
5846 * so for other 'version' this function always returns
5847 * OFPUTIL_TABLE_VACANCY_DEFAULT. */
5848static enum ofputil_table_vacancy
5849ofputil_decode_table_vacancy(ovs_be32 config, enum ofp_version version)
5850{
5851 return (version < OFP14_VERSION ? OFPUTIL_TABLE_VACANCY_DEFAULT
5852 : config & htonl(OFPTC14_VACANCY_EVENTS) ? OFPUTIL_TABLE_VACANCY_ON
5853 : OFPUTIL_TABLE_VACANCY_OFF);
5854}
5855
3c1bb396
BP
5856/* Given 'config', taken from an OpenFlow 'version' message that specifies
5857 * table configuration (a table mod, table stats, or table features message),
82c22d34
BP
5858 * returns the table eviction configuration that it specifies.
5859 *
5860 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
5861 * so for other 'version' values this function always returns
5862 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
5863static enum ofputil_table_eviction
5864ofputil_decode_table_eviction(ovs_be32 config, enum ofp_version version)
5865{
5866 return (version < OFP14_VERSION ? OFPUTIL_TABLE_EVICTION_DEFAULT
5867 : config & htonl(OFPTC14_EVICTION) ? OFPUTIL_TABLE_EVICTION_ON
5868 : OFPUTIL_TABLE_EVICTION_OFF);
5869}
5870
5871/* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
5872 * OpenFlow messages of the given 'version', based on the provided 'miss' and
5873 * 'eviction' values. */
5874static ovs_be32
5875ofputil_encode_table_config(enum ofputil_table_miss miss,
5876 enum ofputil_table_eviction eviction,
de7d3c07 5877 enum ofputil_table_vacancy vacancy,
82c22d34
BP
5878 enum ofp_version version)
5879{
de7d3c07 5880 uint32_t config = 0;
7c9afefd 5881 /* Search for "OFPTC_* Table Configuration" in the documentation for more
82c22d34
BP
5882 * information on the crazy evolution of this field. */
5883 switch (version) {
5884 case OFP10_VERSION:
5885 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
5886 return htonl(0);
5887
5888 case OFP11_VERSION:
5889 case OFP12_VERSION:
5890 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
5891 switch (miss) {
5892 case OFPUTIL_TABLE_MISS_DEFAULT:
5893 /* Really this shouldn't be used for encoding (the caller should
5894 * provide a specific value) but I can't imagine that defaulting to
5895 * the fall-through case here will hurt. */
5896 case OFPUTIL_TABLE_MISS_CONTROLLER:
5897 default:
5898 return htonl(OFPTC11_TABLE_MISS_CONTROLLER);
5899 case OFPUTIL_TABLE_MISS_CONTINUE:
5900 return htonl(OFPTC11_TABLE_MISS_CONTINUE);
5901 case OFPUTIL_TABLE_MISS_DROP:
5902 return htonl(OFPTC11_TABLE_MISS_DROP);
5903 }
5904 OVS_NOT_REACHED();
5905
5906 case OFP13_VERSION:
5907 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
5908 * flags, so this is correct. */
5909 return htonl(0);
5910
5911 case OFP14_VERSION:
5912 case OFP15_VERSION:
b79d45a1 5913 case OFP16_VERSION:
de7d3c07
SJ
5914 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and
5915 * OFPTC14_VACANCY_EVENTS. */
5916 if (eviction == OFPUTIL_TABLE_EVICTION_ON) {
5917 config |= OFPTC14_EVICTION;
5918 }
5919 if (vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5920 config |= OFPTC14_VACANCY_EVENTS;
5921 }
5922 return htonl(config);
82c22d34
BP
5923 }
5924
5925 OVS_NOT_REACHED();
5926}
5927
5928/* Given 'config', taken from an OpenFlow 'version' message that specifies
5929 * table configuration (a table mod, table stats, or table features message),
5930 * returns the table miss configuration that it specifies.
5931 *
5932 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
5933 * other 'version' values this function always returns
5934 * OFPUTIL_TABLE_MISS_DEFAULT. */
3c1bb396 5935static enum ofputil_table_miss
82c22d34 5936ofputil_decode_table_miss(ovs_be32 config_, enum ofp_version version)
3c1bb396
BP
5937{
5938 uint32_t config = ntohl(config_);
5939
82c22d34 5940 if (version == OFP11_VERSION || version == OFP12_VERSION) {
3c1bb396
BP
5941 switch (config & OFPTC11_TABLE_MISS_MASK) {
5942 case OFPTC11_TABLE_MISS_CONTROLLER:
5943 return OFPUTIL_TABLE_MISS_CONTROLLER;
5944
5945 case OFPTC11_TABLE_MISS_CONTINUE:
5946 return OFPUTIL_TABLE_MISS_CONTINUE;
5947
5948 case OFPTC11_TABLE_MISS_DROP:
5949 return OFPUTIL_TABLE_MISS_DROP;
5950
5951 default:
5952 VLOG_WARN_RL(&bad_ofmsg_rl, "bad table miss config %d", config);
5953 return OFPUTIL_TABLE_MISS_CONTROLLER;
5954 }
5955 } else {
5956 return OFPUTIL_TABLE_MISS_DEFAULT;
5957 }
5958}
5959
918f2b82
AZ
5960/* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
5961 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5962enum ofperr
5963ofputil_decode_table_mod(const struct ofp_header *oh,
5964 struct ofputil_table_mod *pm)
5965{
82c22d34
BP
5966 memset(pm, 0, sizeof *pm);
5967 pm->miss = OFPUTIL_TABLE_MISS_DEFAULT;
5968 pm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT;
5969 pm->eviction_flags = UINT32_MAX;
de7d3c07 5970 pm->vacancy = OFPUTIL_TABLE_VACANCY_DEFAULT;
918f2b82 5971
0a2869d5
BP
5972 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
5973 enum ofpraw raw = ofpraw_pull_assert(&b);
918f2b82 5974 if (raw == OFPRAW_OFPT11_TABLE_MOD) {
6fd6ed71 5975 const struct ofp11_table_mod *otm = b.data;
918f2b82
AZ
5976
5977 pm->table_id = otm->table_id;
82c22d34 5978 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
37ab26e8
BP
5979 } else if (raw == OFPRAW_OFPT14_TABLE_MOD) {
5980 const struct ofp14_table_mod *otm = ofpbuf_pull(&b, sizeof *otm);
5981
5982 pm->table_id = otm->table_id;
82c22d34
BP
5983 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5984 pm->eviction = ofputil_decode_table_eviction(otm->config, oh->version);
de7d3c07 5985 pm->vacancy = ofputil_decode_table_vacancy(otm->config, oh->version);
82c22d34
BP
5986 while (b.size > 0) {
5987 struct ofpbuf property;
5988 enum ofperr error;
34a543e3 5989 uint64_t type;
82c22d34 5990
c5562271 5991 error = ofpprop_pull(&b, &property, &type);
82c22d34
BP
5992 if (error) {
5993 return error;
5994 }
5995
5996 switch (type) {
5997 case OFPTMPT14_EVICTION:
b611d3ac 5998 error = ofpprop_parse_u32(&property, &pm->eviction);
82c22d34
BP
5999 break;
6000
de7d3c07
SJ
6001 case OFPTMPT14_VACANCY:
6002 error = parse_table_mod_vacancy_property(&property, pm);
6003 break;
6004
82c22d34
BP
6005 default:
6006 error = OFPERR_OFPBRC_BAD_TYPE;
6007 break;
6008 }
6009
6010 if (error) {
6011 return error;
6012 }
6013 }
918f2b82
AZ
6014 } else {
6015 return OFPERR_OFPBRC_BAD_TYPE;
6016 }
6017
6018 return 0;
6019}
6020
82c22d34
BP
6021/* Converts the abstract form of a "table mod" message in '*tm' into an
6022 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
6023 * buffer owned by the caller. */
918f2b82 6024struct ofpbuf *
82c22d34 6025ofputil_encode_table_mod(const struct ofputil_table_mod *tm,
918f2b82
AZ
6026 enum ofputil_protocol protocol)
6027{
6028 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
6029 struct ofpbuf *b;
6030
6031 switch (ofp_version) {
6032 case OFP10_VERSION: {
6033 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
6034 "(\'-O OpenFlow11\')");
6035 break;
6036 }
6037 case OFP11_VERSION:
6038 case OFP12_VERSION:
6039 case OFP13_VERSION: {
6040 struct ofp11_table_mod *otm;
2e3fa633 6041
918f2b82
AZ
6042 b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0);
6043 otm = ofpbuf_put_zeros(b, sizeof *otm);
82c22d34
BP
6044 otm->table_id = tm->table_id;
6045 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
de7d3c07 6046 tm->vacancy, ofp_version);
918f2b82
AZ
6047 break;
6048 }
42dccab5 6049 case OFP14_VERSION:
b79d45a1
BP
6050 case OFP15_VERSION:
6051 case OFP16_VERSION: {
37ab26e8
BP
6052 struct ofp14_table_mod *otm;
6053
6054 b = ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD, ofp_version, 0);
6055 otm = ofpbuf_put_zeros(b, sizeof *otm);
82c22d34
BP
6056 otm->table_id = tm->table_id;
6057 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
de7d3c07 6058 tm->vacancy, ofp_version);
82c22d34
BP
6059
6060 if (tm->eviction_flags != UINT32_MAX) {
b611d3ac 6061 ofpprop_put_u32(b, OFPTMPT14_EVICTION, tm->eviction_flags);
82c22d34 6062 }
de7d3c07 6063 if (tm->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
303721ee
BP
6064 struct ofp14_table_mod_prop_vacancy *otv;
6065
6066 otv = ofpprop_put_zeros(b, OFPTMPT14_VACANCY, sizeof *otv);
de7d3c07
SJ
6067 otv->vacancy_down = tm->table_vacancy.vacancy_down;
6068 otv->vacancy_up = tm->table_vacancy.vacancy_up;
6069 }
c37c0382 6070 break;
37ab26e8 6071 }
2e3fa633 6072 default:
428b2edd 6073 OVS_NOT_REACHED();
9e1fd49b
BP
6074 }
6075
6076 return b;
6077}
2b07c8b1 6078\f
6ea4776b
JR
6079/* ofputil_role_request */
6080
6081/* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
6082 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
6083 * OFPERR_* value. */
6084enum ofperr
6085ofputil_decode_role_message(const struct ofp_header *oh,
6086 struct ofputil_role_request *rr)
6087{
0a2869d5
BP
6088 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
6089 enum ofpraw raw = ofpraw_pull_assert(&b);
f4f1ea7e
BP
6090 if (raw == OFPRAW_OFPT12_ROLE_REQUEST ||
6091 raw == OFPRAW_OFPT12_ROLE_REPLY) {
6fd6ed71 6092 const struct ofp12_role_request *orr = b.msg;
6ea4776b 6093
f4f1ea7e
BP
6094 if (orr->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
6095 orr->role != htonl(OFPCR12_ROLE_EQUAL) &&
6096 orr->role != htonl(OFPCR12_ROLE_MASTER) &&
6097 orr->role != htonl(OFPCR12_ROLE_SLAVE)) {
6098 return OFPERR_OFPRRFC_BAD_ROLE;
6ea4776b
JR
6099 }
6100
f4f1ea7e 6101 rr->role = ntohl(orr->role);
147cc9d3
BP
6102 if (raw == OFPRAW_OFPT12_ROLE_REQUEST
6103 ? orr->role == htonl(OFPCR12_ROLE_NOCHANGE)
b8266395 6104 : orr->generation_id == OVS_BE64_MAX) {
f4f1ea7e
BP
6105 rr->have_generation_id = false;
6106 rr->generation_id = 0;
6107 } else {
6108 rr->have_generation_id = true;
6109 rr->generation_id = ntohll(orr->generation_id);
6110 }
6111 } else if (raw == OFPRAW_NXT_ROLE_REQUEST ||
6112 raw == OFPRAW_NXT_ROLE_REPLY) {
6fd6ed71 6113 const struct nx_role_request *nrr = b.msg;
f4f1ea7e
BP
6114
6115 BUILD_ASSERT(NX_ROLE_OTHER + 1 == OFPCR12_ROLE_EQUAL);
6116 BUILD_ASSERT(NX_ROLE_MASTER + 1 == OFPCR12_ROLE_MASTER);
6117 BUILD_ASSERT(NX_ROLE_SLAVE + 1 == OFPCR12_ROLE_SLAVE);
6118
6119 if (nrr->role != htonl(NX_ROLE_OTHER) &&
6120 nrr->role != htonl(NX_ROLE_MASTER) &&
6121 nrr->role != htonl(NX_ROLE_SLAVE)) {
6122 return OFPERR_OFPRRFC_BAD_ROLE;
6123 }
6ea4776b 6124
f4f1ea7e
BP
6125 rr->role = ntohl(nrr->role) + 1;
6126 rr->have_generation_id = false;
6127 rr->generation_id = 0;
6128 } else {
428b2edd 6129 OVS_NOT_REACHED();
6ea4776b
JR
6130 }
6131
6ea4776b
JR
6132 return 0;
6133}
6134
6135/* Returns an encoded form of a role reply suitable for the "request" in a
6136 * buffer owned by the caller. */
6137struct ofpbuf *
6138ofputil_encode_role_reply(const struct ofp_header *request,
f4f1ea7e 6139 const struct ofputil_role_request *rr)
6ea4776b 6140{
6ea4776b 6141 struct ofpbuf *buf;
6ea4776b
JR
6142 enum ofpraw raw;
6143
f4f1ea7e 6144 raw = ofpraw_decode_assert(request);
6ea4776b 6145 if (raw == OFPRAW_OFPT12_ROLE_REQUEST) {
f4f1ea7e 6146 struct ofp12_role_request *orr;
6ea4776b 6147
f4f1ea7e
BP
6148 buf = ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY, request, 0);
6149 orr = ofpbuf_put_zeros(buf, sizeof *orr);
6ea4776b 6150
147cc9d3
BP
6151 orr->role = htonl(rr->role);
6152 orr->generation_id = htonll(rr->have_generation_id
6153 ? rr->generation_id
6154 : UINT64_MAX);
f4f1ea7e
BP
6155 } else if (raw == OFPRAW_NXT_ROLE_REQUEST) {
6156 struct nx_role_request *nrr;
6157
6158 BUILD_ASSERT(NX_ROLE_OTHER == OFPCR12_ROLE_EQUAL - 1);
6159 BUILD_ASSERT(NX_ROLE_MASTER == OFPCR12_ROLE_MASTER - 1);
6160 BUILD_ASSERT(NX_ROLE_SLAVE == OFPCR12_ROLE_SLAVE - 1);
6161
6162 buf = ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY, request, 0);
6163 nrr = ofpbuf_put_zeros(buf, sizeof *nrr);
6164 nrr->role = htonl(rr->role - 1);
6165 } else {
428b2edd 6166 OVS_NOT_REACHED();
6ea4776b 6167 }
6ea4776b
JR
6168
6169 return buf;
6170}
6171\f
6751a4b4
BP
6172/* Encodes "role status" message 'status' for sending in the given
6173 * 'protocol'. Returns the role status message, if 'protocol' supports them,
6174 * otherwise a null pointer. */
00467f73
AC
6175struct ofpbuf *
6176ofputil_encode_role_status(const struct ofputil_role_status *status,
6177 enum ofputil_protocol protocol)
6178{
00467f73 6179 enum ofp_version version;
00467f73
AC
6180
6181 version = ofputil_protocol_to_ofp_version(protocol);
6751a4b4
BP
6182 if (version >= OFP14_VERSION) {
6183 struct ofp14_role_status *rstatus;
6184 struct ofpbuf *buf;
6185
6186 buf = ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS, version, htonl(0),
6187 0);
6188 rstatus = ofpbuf_put_zeros(buf, sizeof *rstatus);
6189 rstatus->role = htonl(status->role);
6190 rstatus->reason = status->reason;
6191 rstatus->generation_id = htonll(status->generation_id);
6192
6193 return buf;
6194 } else {
6195 return NULL;
6196 }
00467f73
AC
6197}
6198
6199enum ofperr
6200ofputil_decode_role_status(const struct ofp_header *oh,
6201 struct ofputil_role_status *rs)
6202{
0a2869d5
BP
6203 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
6204 enum ofpraw raw = ofpraw_pull_assert(&b);
00467f73
AC
6205 ovs_assert(raw == OFPRAW_OFPT14_ROLE_STATUS);
6206
0a2869d5 6207 const struct ofp14_role_status *r = b.msg;
00467f73
AC
6208 if (r->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
6209 r->role != htonl(OFPCR12_ROLE_EQUAL) &&
6210 r->role != htonl(OFPCR12_ROLE_MASTER) &&
6211 r->role != htonl(OFPCR12_ROLE_SLAVE)) {
6212 return OFPERR_OFPRRFC_BAD_ROLE;
6213 }
6214
6215 rs->role = ntohl(r->role);
6216 rs->generation_id = ntohll(r->generation_id);
6217 rs->reason = r->reason;
6218
6219 return 0;
6220}
6221
3c35db62
NR
6222/* Encodes 'rf' according to 'protocol', and returns the encoded message.
6223 * 'protocol' must be for OpenFlow 1.4 or later. */
6224struct ofpbuf *
6225ofputil_encode_requestforward(const struct ofputil_requestforward *rf,
6226 enum ofputil_protocol protocol)
6227{
6228 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
6229 struct ofpbuf *inner;
6230
6231 switch (rf->reason) {
6232 case OFPRFR_GROUP_MOD:
6233 inner = ofputil_encode_group_mod(ofp_version, rf->group_mod);
6234 break;
6235
6236 case OFPRFR_METER_MOD:
6237 inner = ofputil_encode_meter_mod(ofp_version, rf->meter_mod);
6238 break;
6239
d18cc1ee 6240 case OFPRFR_N_REASONS:
3c35db62
NR
6241 default:
6242 OVS_NOT_REACHED();
6243 }
6244
6245 struct ofp_header *inner_oh = inner->data;
6246 inner_oh->xid = rf->xid;
6247 inner_oh->length = htons(inner->size);
6248
6249 struct ofpbuf *outer = ofpraw_alloc_xid(OFPRAW_OFPT14_REQUESTFORWARD,
6250 ofp_version, htonl(0),
6251 inner->size);
6252 ofpbuf_put(outer, inner->data, inner->size);
6253 ofpbuf_delete(inner);
6254
6255 return outer;
6256}
6257
6258/* Decodes OFPT_REQUESTFORWARD message 'outer'. On success, puts the decoded
6259 * form into '*rf' and returns 0, and the caller is later responsible for
6260 * freeing the content of 'rf', with ofputil_destroy_requestforward(rf). On
6261 * failure, returns an ofperr and '*rf' is indeterminate. */
6262enum ofperr
6263ofputil_decode_requestforward(const struct ofp_header *outer,
6264 struct ofputil_requestforward *rf)
6265{
0a2869d5 6266 struct ofpbuf b = ofpbuf_const_initializer(outer, ntohs(outer->length));
3c35db62
NR
6267
6268 /* Skip past outer message. */
6269 enum ofpraw outer_raw = ofpraw_pull_assert(&b);
6270 ovs_assert(outer_raw == OFPRAW_OFPT14_REQUESTFORWARD);
6271
6272 /* Validate inner message. */
6273 if (b.size < sizeof(struct ofp_header)) {
6274 return OFPERR_OFPBFC_MSG_BAD_LEN;
6275 }
6276 const struct ofp_header *inner = b.data;
6277 unsigned int inner_len = ntohs(inner->length);
6278 if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
6279 return OFPERR_OFPBFC_MSG_BAD_LEN;
6280 }
6281 if (inner->version != outer->version) {
6282 return OFPERR_OFPBRC_BAD_VERSION;
6283 }
6284
6285 /* Parse inner message. */
6286 enum ofptype type;
0a2869d5 6287 enum ofperr error = ofptype_decode(&type, inner);
3c35db62
NR
6288 if (error) {
6289 return error;
6290 }
6291
6292 rf->xid = inner->xid;
6293 if (type == OFPTYPE_GROUP_MOD) {
6294 rf->reason = OFPRFR_GROUP_MOD;
6295 rf->group_mod = xmalloc(sizeof *rf->group_mod);
6296 error = ofputil_decode_group_mod(inner, rf->group_mod);
6297 if (error) {
6298 free(rf->group_mod);
6299 return error;
6300 }
6301 } else if (type == OFPTYPE_METER_MOD) {
6302 rf->reason = OFPRFR_METER_MOD;
6303 rf->meter_mod = xmalloc(sizeof *rf->meter_mod);
6304 ofpbuf_init(&rf->bands, 64);
6305 error = ofputil_decode_meter_mod(inner, rf->meter_mod, &rf->bands);
6306 if (error) {
6307 free(rf->meter_mod);
6308 ofpbuf_uninit(&rf->bands);
6309 return error;
6310 }
6311 } else {
6312 return OFPERR_OFPBFC_MSG_UNSUP;
6313 }
6314
6315 return 0;
6316}
6317
6318/* Frees the content of 'rf', which should have been initialized through a
6319 * successful call to ofputil_decode_requestforward(). */
6320void
6321ofputil_destroy_requestforward(struct ofputil_requestforward *rf)
6322{
6323 if (!rf) {
6324 return;
6325 }
6326
6327 switch (rf->reason) {
6328 case OFPRFR_GROUP_MOD:
6329 ofputil_uninit_group_mod(rf->group_mod);
6330 free(rf->group_mod);
6331 break;
6332
6333 case OFPRFR_METER_MOD:
6334 ofpbuf_uninit(&rf->bands);
6335 free(rf->meter_mod);
d18cc1ee
AA
6336 break;
6337
6338 case OFPRFR_N_REASONS:
6339 OVS_NOT_REACHED();
3c35db62
NR
6340 }
6341}
6342
307975da
SH
6343/* Table stats. */
6344
3c1bb396
BP
6345/* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
6346 * matched and a field that must be wildcarded. This function returns a bitmap
6347 * that contains both kinds of fields. */
6348static struct mf_bitmap
6349wild_or_nonmatchable_fields(const struct ofputil_table_features *features)
6350{
6351 struct mf_bitmap wc = features->match;
6352 bitmap_not(wc.bm, MFF_N_IDS);
6353 bitmap_or(wc.bm, features->wildcard.bm, MFF_N_IDS);
6354 return wc;
6355}
6356
6357struct ofp10_wc_map {
6358 enum ofp10_flow_wildcards wc10;
6359 enum mf_field_id mf;
6360};
6361
6362static const struct ofp10_wc_map ofp10_wc_map[] = {
6363 { OFPFW10_IN_PORT, MFF_IN_PORT },
6364 { OFPFW10_DL_VLAN, MFF_VLAN_VID },
6365 { OFPFW10_DL_SRC, MFF_ETH_SRC },
6366 { OFPFW10_DL_DST, MFF_ETH_DST},
6367 { OFPFW10_DL_TYPE, MFF_ETH_TYPE },
6368 { OFPFW10_NW_PROTO, MFF_IP_PROTO },
6369 { OFPFW10_TP_SRC, MFF_TCP_SRC },
6370 { OFPFW10_TP_DST, MFF_TCP_DST },
6371 { OFPFW10_NW_SRC_MASK, MFF_IPV4_SRC },
6372 { OFPFW10_NW_DST_MASK, MFF_IPV4_DST },
6373 { OFPFW10_DL_VLAN_PCP, MFF_VLAN_PCP },
6374 { OFPFW10_NW_TOS, MFF_IP_DSCP },
6375};
6376
6377static ovs_be32
6378mf_bitmap_to_of10(const struct mf_bitmap *fields)
6379{
6380 const struct ofp10_wc_map *p;
6381 uint32_t wc10 = 0;
6382
6383 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
6384 if (bitmap_is_set(fields->bm, p->mf)) {
6385 wc10 |= p->wc10;
6386 }
6387 }
6388 return htonl(wc10);
6389}
6390
6391static struct mf_bitmap
6392mf_bitmap_from_of10(ovs_be32 wc10_)
6393{
6394 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
6395 const struct ofp10_wc_map *p;
6396 uint32_t wc10 = ntohl(wc10_);
6397
6398 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
6399 if (wc10 & p->wc10) {
6400 bitmap_set1(fields.bm, p->mf);
6401 }
6402 }
6403 return fields;
6404}
6405
307975da 6406static void
3c1bb396
BP
6407ofputil_put_ofp10_table_stats(const struct ofputil_table_stats *stats,
6408 const struct ofputil_table_features *features,
307975da
SH
6409 struct ofpbuf *buf)
6410{
3c1bb396 6411 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
307975da 6412 struct ofp10_table_stats *out;
307975da 6413
3bdc692b 6414 out = ofpbuf_put_zeros(buf, sizeof *out);
3c1bb396 6415 out->table_id = features->table_id;
f9ac0f03 6416 ovs_strlcpy_arrays(out->name, features->name);
3c1bb396
BP
6417 out->wildcards = mf_bitmap_to_of10(&wc);
6418 out->max_entries = htonl(features->max_entries);
6419 out->active_count = htonl(stats->active_count);
6420 put_32aligned_be64(&out->lookup_count, htonll(stats->lookup_count));
6421 put_32aligned_be64(&out->matched_count, htonll(stats->matched_count));
6422}
6423
6424struct ofp11_wc_map {
6425 enum ofp11_flow_match_fields wc11;
6426 enum mf_field_id mf;
6427};
6428
6429static const struct ofp11_wc_map ofp11_wc_map[] = {
6430 { OFPFMF11_IN_PORT, MFF_IN_PORT },
6431 { OFPFMF11_DL_VLAN, MFF_VLAN_VID },
6432 { OFPFMF11_DL_VLAN_PCP, MFF_VLAN_PCP },
6433 { OFPFMF11_DL_TYPE, MFF_ETH_TYPE },
6434 { OFPFMF11_NW_TOS, MFF_IP_DSCP },
6435 { OFPFMF11_NW_PROTO, MFF_IP_PROTO },
6436 { OFPFMF11_TP_SRC, MFF_TCP_SRC },
6437 { OFPFMF11_TP_DST, MFF_TCP_DST },
6438 { OFPFMF11_MPLS_LABEL, MFF_MPLS_LABEL },
6439 { OFPFMF11_MPLS_TC, MFF_MPLS_TC },
6440 /* I don't know what OFPFMF11_TYPE means. */
6441 { OFPFMF11_DL_SRC, MFF_ETH_SRC },
6442 { OFPFMF11_DL_DST, MFF_ETH_DST },
6443 { OFPFMF11_NW_SRC, MFF_IPV4_SRC },
6444 { OFPFMF11_NW_DST, MFF_IPV4_DST },
6445 { OFPFMF11_METADATA, MFF_METADATA },
6446};
6447
6448static ovs_be32
6449mf_bitmap_to_of11(const struct mf_bitmap *fields)
6450{
6451 const struct ofp11_wc_map *p;
6452 uint32_t wc11 = 0;
6453
6454 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
6455 if (bitmap_is_set(fields->bm, p->mf)) {
6456 wc11 |= p->wc11;
307975da
SH
6457 }
6458 }
3c1bb396 6459 return htonl(wc11);
307975da
SH
6460}
6461
3c1bb396
BP
6462static struct mf_bitmap
6463mf_bitmap_from_of11(ovs_be32 wc11_)
6464{
6465 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
6466 const struct ofp11_wc_map *p;
6467 uint32_t wc11 = ntohl(wc11_);
6468
6469 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
6470 if (wc11 & p->wc11) {
6471 bitmap_set1(fields.bm, p->mf);
307975da
SH
6472 }
6473 }
3c1bb396 6474 return fields;
307975da
SH
6475}
6476
6477static void
3c1bb396
BP
6478ofputil_put_ofp11_table_stats(const struct ofputil_table_stats *stats,
6479 const struct ofputil_table_features *features,
307975da
SH
6480 struct ofpbuf *buf)
6481{
3c1bb396 6482 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
307975da
SH
6483 struct ofp11_table_stats *out;
6484
3bdc692b 6485 out = ofpbuf_put_zeros(buf, sizeof *out);
3c1bb396 6486 out->table_id = features->table_id;
f9ac0f03 6487 ovs_strlcpy_arrays(out->name, features->name);
3c1bb396
BP
6488 out->wildcards = mf_bitmap_to_of11(&wc);
6489 out->match = mf_bitmap_to_of11(&features->match);
6490 out->instructions = ovsinst_bitmap_to_openflow(
6491 features->nonmiss.instructions, OFP11_VERSION);
6492 out->write_actions = ofpact_bitmap_to_openflow(
6493 features->nonmiss.write.ofpacts, OFP11_VERSION);
6494 out->apply_actions = ofpact_bitmap_to_openflow(
6495 features->nonmiss.apply.ofpacts, OFP11_VERSION);
6496 out->config = htonl(features->miss_config);
6497 out->max_entries = htonl(features->max_entries);
6498 out->active_count = htonl(stats->active_count);
6499 out->lookup_count = htonll(stats->lookup_count);
6500 out->matched_count = htonll(stats->matched_count);
08d1e234
BP
6501}
6502
6240624b 6503static void
3c1bb396
BP
6504ofputil_put_ofp12_table_stats(const struct ofputil_table_stats *stats,
6505 const struct ofputil_table_features *features,
6240624b
BP
6506 struct ofpbuf *buf)
6507{
08d1e234 6508 struct ofp12_table_stats *out;
6240624b 6509
08d1e234 6510 out = ofpbuf_put_zeros(buf, sizeof *out);
3c1bb396 6511 out->table_id = features->table_id;
f9ac0f03 6512 ovs_strlcpy_arrays(out->name, features->name);
178742f9
BP
6513 out->match = oxm_bitmap_from_mf_bitmap(&features->match, OFP12_VERSION);
6514 out->wildcards = oxm_bitmap_from_mf_bitmap(&features->wildcard,
3c1bb396
BP
6515 OFP12_VERSION);
6516 out->write_actions = ofpact_bitmap_to_openflow(
6517 features->nonmiss.write.ofpacts, OFP12_VERSION);
6518 out->apply_actions = ofpact_bitmap_to_openflow(
6519 features->nonmiss.apply.ofpacts, OFP12_VERSION);
178742f9 6520 out->write_setfields = oxm_bitmap_from_mf_bitmap(
3c1bb396 6521 &features->nonmiss.write.set_fields, OFP12_VERSION);
178742f9 6522 out->apply_setfields = oxm_bitmap_from_mf_bitmap(
3c1bb396
BP
6523 &features->nonmiss.apply.set_fields, OFP12_VERSION);
6524 out->metadata_match = features->metadata_match;
6525 out->metadata_write = features->metadata_write;
6526 out->instructions = ovsinst_bitmap_to_openflow(
6527 features->nonmiss.instructions, OFP12_VERSION);
82c22d34
BP
6528 out->config = ofputil_encode_table_config(features->miss_config,
6529 OFPUTIL_TABLE_EVICTION_DEFAULT,
de7d3c07 6530 OFPUTIL_TABLE_VACANCY_DEFAULT,
82c22d34 6531 OFP12_VERSION);
3c1bb396
BP
6532 out->max_entries = htonl(features->max_entries);
6533 out->active_count = htonl(stats->active_count);
6534 out->lookup_count = htonll(stats->lookup_count);
6535 out->matched_count = htonll(stats->matched_count);
6240624b
BP
6536}
6537
2e1ae200 6538static void
3c1bb396 6539ofputil_put_ofp13_table_stats(const struct ofputil_table_stats *stats,
2e1ae200
JR
6540 struct ofpbuf *buf)
6541{
6542 struct ofp13_table_stats *out;
6543
3c1bb396
BP
6544 out = ofpbuf_put_zeros(buf, sizeof *out);
6545 out->table_id = stats->table_id;
6546 out->active_count = htonl(stats->active_count);
6547 out->lookup_count = htonll(stats->lookup_count);
6548 out->matched_count = htonll(stats->matched_count);
2e1ae200
JR
6549}
6550
307975da 6551struct ofpbuf *
3c1bb396 6552ofputil_encode_table_stats_reply(const struct ofp_header *request)
307975da 6553{
3c1bb396
BP
6554 return ofpraw_alloc_stats_reply(request, 0);
6555}
307975da 6556
3c1bb396
BP
6557void
6558ofputil_append_table_stats_reply(struct ofpbuf *reply,
6559 const struct ofputil_table_stats *stats,
6560 const struct ofputil_table_features *features)
6561{
6fd6ed71 6562 struct ofp_header *oh = reply->header;
307975da 6563
3c1bb396 6564 ovs_assert(stats->table_id == features->table_id);
307975da 6565
3c1bb396
BP
6566 switch ((enum ofp_version) oh->version) {
6567 case OFP10_VERSION:
6568 ofputil_put_ofp10_table_stats(stats, features, reply);
6569 break;
307975da 6570
3c1bb396
BP
6571 case OFP11_VERSION:
6572 ofputil_put_ofp11_table_stats(stats, features, reply);
6573 break;
307975da 6574
3c1bb396
BP
6575 case OFP12_VERSION:
6576 ofputil_put_ofp12_table_stats(stats, features, reply);
6577 break;
2e1ae200 6578
3c1bb396
BP
6579 case OFP13_VERSION:
6580 case OFP14_VERSION:
6581 case OFP15_VERSION:
b79d45a1 6582 case OFP16_VERSION:
3c1bb396
BP
6583 ofputil_put_ofp13_table_stats(stats, reply);
6584 break;
6585
6586 default:
6587 OVS_NOT_REACHED();
307975da 6588 }
3c1bb396 6589}
307975da 6590
3c1bb396
BP
6591static int
6592ofputil_decode_ofp10_table_stats(struct ofpbuf *msg,
6593 struct ofputil_table_stats *stats,
6594 struct ofputil_table_features *features)
6595{
6596 struct ofp10_table_stats *ots;
6597
6598 ots = ofpbuf_try_pull(msg, sizeof *ots);
6599 if (!ots) {
6600 return OFPERR_OFPBRC_BAD_LEN;
6601 }
6602
6603 features->table_id = ots->table_id;
f9ac0f03 6604 ovs_strlcpy_arrays(features->name, ots->name);
3c1bb396
BP
6605 features->max_entries = ntohl(ots->max_entries);
6606 features->match = features->wildcard = mf_bitmap_from_of10(ots->wildcards);
6607
6608 stats->table_id = ots->table_id;
6609 stats->active_count = ntohl(ots->active_count);
6610 stats->lookup_count = ntohll(get_32aligned_be64(&ots->lookup_count));
6611 stats->matched_count = ntohll(get_32aligned_be64(&ots->matched_count));
6612
6613 return 0;
6614}
6615
6616static int
6617ofputil_decode_ofp11_table_stats(struct ofpbuf *msg,
6618 struct ofputil_table_stats *stats,
6619 struct ofputil_table_features *features)
6620{
6621 struct ofp11_table_stats *ots;
6622
6623 ots = ofpbuf_try_pull(msg, sizeof *ots);
6624 if (!ots) {
6625 return OFPERR_OFPBRC_BAD_LEN;
6626 }
6627
6628 features->table_id = ots->table_id;
f9ac0f03 6629 ovs_strlcpy_arrays(features->name, ots->name);
3c1bb396
BP
6630 features->max_entries = ntohl(ots->max_entries);
6631 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
6632 ots->instructions, OFP11_VERSION);
6633 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
6634 ots->write_actions, OFP11_VERSION);
6635 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
6636 ots->write_actions, OFP11_VERSION);
6637 features->miss = features->nonmiss;
82c22d34
BP
6638 features->miss_config = ofputil_decode_table_miss(ots->config,
6639 OFP11_VERSION);
3c1bb396
BP
6640 features->match = mf_bitmap_from_of11(ots->match);
6641 features->wildcard = mf_bitmap_from_of11(ots->wildcards);
6642 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
6643
6644 stats->table_id = ots->table_id;
6645 stats->active_count = ntohl(ots->active_count);
6646 stats->lookup_count = ntohll(ots->lookup_count);
6647 stats->matched_count = ntohll(ots->matched_count);
6648
6649 return 0;
6650}
6651
6652static int
6653ofputil_decode_ofp12_table_stats(struct ofpbuf *msg,
6654 struct ofputil_table_stats *stats,
6655 struct ofputil_table_features *features)
6656{
6657 struct ofp12_table_stats *ots;
6658
6659 ots = ofpbuf_try_pull(msg, sizeof *ots);
6660 if (!ots) {
6661 return OFPERR_OFPBRC_BAD_LEN;
6662 }
6663
6664 features->table_id = ots->table_id;
f9ac0f03 6665 ovs_strlcpy_arrays(features->name, ots->name);
3c1bb396
BP
6666 features->metadata_match = ots->metadata_match;
6667 features->metadata_write = ots->metadata_write;
82c22d34
BP
6668 features->miss_config = ofputil_decode_table_miss(ots->config,
6669 OFP12_VERSION);
3c1bb396
BP
6670 features->max_entries = ntohl(ots->max_entries);
6671
6672 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
6673 ots->instructions, OFP12_VERSION);
6674 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
6675 ots->write_actions, OFP12_VERSION);
6676 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
6677 ots->apply_actions, OFP12_VERSION);
178742f9 6678 features->nonmiss.write.set_fields = oxm_bitmap_to_mf_bitmap(
3c1bb396 6679 ots->write_setfields, OFP12_VERSION);
178742f9 6680 features->nonmiss.apply.set_fields = oxm_bitmap_to_mf_bitmap(
3c1bb396
BP
6681 ots->apply_setfields, OFP12_VERSION);
6682 features->miss = features->nonmiss;
6683
178742f9
BP
6684 features->match = oxm_bitmap_to_mf_bitmap(ots->match, OFP12_VERSION);
6685 features->wildcard = oxm_bitmap_to_mf_bitmap(ots->wildcards,
6686 OFP12_VERSION);
3c1bb396
BP
6687 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
6688
6689 stats->table_id = ots->table_id;
6690 stats->active_count = ntohl(ots->active_count);
6691 stats->lookup_count = ntohll(ots->lookup_count);
6692 stats->matched_count = ntohll(ots->matched_count);
6693
6694 return 0;
6695}
6696
6697static int
6698ofputil_decode_ofp13_table_stats(struct ofpbuf *msg,
6699 struct ofputil_table_stats *stats,
6700 struct ofputil_table_features *features)
6701{
6702 struct ofp13_table_stats *ots;
6703
6704 ots = ofpbuf_try_pull(msg, sizeof *ots);
6705 if (!ots) {
6706 return OFPERR_OFPBRC_BAD_LEN;
6707 }
6708
6709 features->table_id = ots->table_id;
6710
6711 stats->table_id = ots->table_id;
6712 stats->active_count = ntohl(ots->active_count);
6713 stats->lookup_count = ntohll(ots->lookup_count);
6714 stats->matched_count = ntohll(ots->matched_count);
6715
6716 return 0;
6717}
6718
6719int
6720ofputil_decode_table_stats_reply(struct ofpbuf *msg,
6721 struct ofputil_table_stats *stats,
6722 struct ofputil_table_features *features)
6723{
6724 const struct ofp_header *oh;
6725
6fd6ed71 6726 if (!msg->header) {
3c1bb396
BP
6727 ofpraw_pull_assert(msg);
6728 }
6fd6ed71 6729 oh = msg->header;
3c1bb396 6730
6fd6ed71 6731 if (!msg->size) {
3c1bb396
BP
6732 return EOF;
6733 }
6734
6735 memset(stats, 0, sizeof *stats);
6736 memset(features, 0, sizeof *features);
82c22d34
BP
6737 features->supports_eviction = -1;
6738 features->supports_vacancy_events = -1;
3c1bb396
BP
6739
6740 switch ((enum ofp_version) oh->version) {
6741 case OFP10_VERSION:
6742 return ofputil_decode_ofp10_table_stats(msg, stats, features);
6743
6744 case OFP11_VERSION:
6745 return ofputil_decode_ofp11_table_stats(msg, stats, features);
6746
6747 case OFP12_VERSION:
6748 return ofputil_decode_ofp12_table_stats(msg, stats, features);
6749
6750 case OFP13_VERSION:
6751 case OFP14_VERSION:
6752 case OFP15_VERSION:
b79d45a1 6753 case OFP16_VERSION:
3c1bb396
BP
6754 return ofputil_decode_ofp13_table_stats(msg, stats, features);
6755
6756 default:
6757 OVS_NOT_REACHED();
6758 }
307975da
SH
6759}
6760\f
2b07c8b1
BP
6761/* ofputil_flow_monitor_request */
6762
6763/* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
6764 * ofputil_flow_monitor_request in 'rq'.
6765 *
6766 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
6767 * message. Calling this function multiple times for a single 'msg' iterates
6768 * through the requests. The caller must initially leave 'msg''s layer
6769 * pointers null and not modify them between calls.
6770 *
6771 * Returns 0 if successful, EOF if no requests were left in this 'msg',
6772 * otherwise an OFPERR_* value. */
6773int
6774ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
6775 struct ofpbuf *msg)
6776{
6777 struct nx_flow_monitor_request *nfmr;
6778 uint16_t flags;
6779
6fd6ed71 6780 if (!msg->header) {
982697a4 6781 ofpraw_pull_assert(msg);
2b07c8b1
BP
6782 }
6783
6fd6ed71 6784 if (!msg->size) {
2b07c8b1
BP
6785 return EOF;
6786 }
6787
6788 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
6789 if (!nfmr) {
437d0d22 6790 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %"PRIu32" "
6fd6ed71 6791 "leftover bytes at end", msg->size);
2b07c8b1
BP
6792 return OFPERR_OFPBRC_BAD_LEN;
6793 }
6794
6795 flags = ntohs(nfmr->flags);
6796 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
6797 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
6798 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
6799 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
6800 flags);
04f9f286 6801 return OFPERR_OFPMOFC_BAD_FLAGS;
2b07c8b1
BP
6802 }
6803
6804 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
6805 return OFPERR_NXBRC_MUST_BE_ZERO;
6806 }
6807
6808 rq->id = ntohl(nfmr->id);
6809 rq->flags = flags;
4e022ec0 6810 rq->out_port = u16_to_ofp(ntohs(nfmr->out_port));
2b07c8b1
BP
6811 rq->table_id = nfmr->table_id;
6812
8d8ab6c2 6813 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL,
3cddeff0 6814 NULL, NULL, NULL);
2b07c8b1
BP
6815}
6816
6817void
6818ofputil_append_flow_monitor_request(
6819 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
6820{
6821 struct nx_flow_monitor_request *nfmr;
6822 size_t start_ofs;
6823 int match_len;
6824
6fd6ed71 6825 if (!msg->size) {
982697a4 6826 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
2b07c8b1
BP
6827 }
6828
6fd6ed71 6829 start_ofs = msg->size;
2b07c8b1 6830 ofpbuf_put_zeros(msg, sizeof *nfmr);
7623f4dd 6831 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
2b07c8b1
BP
6832
6833 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
6834 nfmr->id = htonl(rq->id);
6835 nfmr->flags = htons(rq->flags);
4e022ec0 6836 nfmr->out_port = htons(ofp_to_u16(rq->out_port));
2b07c8b1
BP
6837 nfmr->match_len = htons(match_len);
6838 nfmr->table_id = rq->table_id;
6839}
6840
6841/* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
6842 * into an abstract ofputil_flow_update in 'update'. The caller must have
81a76618 6843 * initialized update->match to point to space allocated for a match.
2b07c8b1
BP
6844 *
6845 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
6846 * actions (except for NXFME_ABBREV, which never includes actions). The caller
6847 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
6848 * will point into the 'ofpacts' buffer.
6849 *
6850 * Multiple flow updates can be packed into a single OpenFlow message. Calling
6851 * this function multiple times for a single 'msg' iterates through the
6852 * updates. The caller must initially leave 'msg''s layer pointers null and
6853 * not modify them between calls.
6854 *
6855 * Returns 0 if successful, EOF if no updates were left in this 'msg',
6856 * otherwise an OFPERR_* value. */
6857int
6858ofputil_decode_flow_update(struct ofputil_flow_update *update,
6859 struct ofpbuf *msg, struct ofpbuf *ofpacts)
6860{
6861 struct nx_flow_update_header *nfuh;
6862 unsigned int length;
e3f8f887 6863 struct ofp_header *oh;
2b07c8b1 6864
6fd6ed71 6865 if (!msg->header) {
982697a4 6866 ofpraw_pull_assert(msg);
2b07c8b1
BP
6867 }
6868
9abca1e5 6869 ofpbuf_clear(ofpacts);
6fd6ed71 6870 if (!msg->size) {
2b07c8b1
BP
6871 return EOF;
6872 }
6873
6fd6ed71 6874 if (msg->size < sizeof(struct nx_flow_update_header)) {
2b07c8b1
BP
6875 goto bad_len;
6876 }
6877
6fd6ed71 6878 oh = msg->header;
e3f8f887 6879
6fd6ed71 6880 nfuh = msg->data;
2b07c8b1
BP
6881 update->event = ntohs(nfuh->event);
6882 length = ntohs(nfuh->length);
6fd6ed71 6883 if (length > msg->size || length % 8) {
2b07c8b1
BP
6884 goto bad_len;
6885 }
6886
6887 if (update->event == NXFME_ABBREV) {
6888 struct nx_flow_update_abbrev *nfua;
6889
6890 if (length != sizeof *nfua) {
6891 goto bad_len;
6892 }
6893
6894 nfua = ofpbuf_pull(msg, sizeof *nfua);
6895 update->xid = nfua->xid;
6896 return 0;
6897 } else if (update->event == NXFME_ADDED
6898 || update->event == NXFME_DELETED
6899 || update->event == NXFME_MODIFIED) {
6900 struct nx_flow_update_full *nfuf;
6901 unsigned int actions_len;
6902 unsigned int match_len;
6903 enum ofperr error;
6904
6905 if (length < sizeof *nfuf) {
6906 goto bad_len;
6907 }
6908
6909 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
6910 match_len = ntohs(nfuf->match_len);
6911 if (sizeof *nfuf + match_len > length) {
6912 goto bad_len;
6913 }
6914
6915 update->reason = ntohs(nfuf->reason);
6916 update->idle_timeout = ntohs(nfuf->idle_timeout);
6917 update->hard_timeout = ntohs(nfuf->hard_timeout);
6918 update->table_id = nfuf->table_id;
6919 update->cookie = nfuf->cookie;
81a76618 6920 update->priority = ntohs(nfuf->priority);
2b07c8b1 6921
3cddeff0
YHW
6922 error = nx_pull_match(msg, match_len, &update->match, NULL, NULL, NULL,
6923 NULL);
2b07c8b1
BP
6924 if (error) {
6925 return error;
6926 }
6927
6928 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
e3f8f887 6929 error = ofpacts_pull_openflow_actions(msg, actions_len, oh->version,
5c7c16d8 6930 NULL, NULL, ofpacts);
2b07c8b1
BP
6931 if (error) {
6932 return error;
6933 }
6934
6fd6ed71
PS
6935 update->ofpacts = ofpacts->data;
6936 update->ofpacts_len = ofpacts->size;
2b07c8b1
BP
6937 return 0;
6938 } else {
6939 VLOG_WARN_RL(&bad_ofmsg_rl,
6940 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
6941 ntohs(nfuh->event));
15549878 6942 return OFPERR_NXBRC_FM_BAD_EVENT;
2b07c8b1
BP
6943 }
6944
6945bad_len:
437d0d22 6946 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %"PRIu32" "
6fd6ed71 6947 "leftover bytes at end", msg->size);
2b07c8b1
BP
6948 return OFPERR_OFPBRC_BAD_LEN;
6949}
6950
6951uint32_t
6952ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
6953{
982697a4
BP
6954 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
6955
6956 return ntohl(cancel->id);
2b07c8b1 6957}
9e1fd49b 6958
2b07c8b1
BP
6959struct ofpbuf *
6960ofputil_encode_flow_monitor_cancel(uint32_t id)
6961{
6962 struct nx_flow_monitor_cancel *nfmc;
6963 struct ofpbuf *msg;
6964
982697a4
BP
6965 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
6966 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
2b07c8b1
BP
6967 nfmc->id = htonl(id);
6968 return msg;
6969}
6970
6971void
ca6ba700 6972ofputil_start_flow_update(struct ovs_list *replies)
2b07c8b1
BP
6973{
6974 struct ofpbuf *msg;
6975
982697a4
BP
6976 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
6977 htonl(0), 1024);
2b07c8b1 6978
417e7e66
BW
6979 ovs_list_init(replies);
6980 ovs_list_push_back(replies, &msg->list_node);
2b07c8b1
BP
6981}
6982
6983void
6984ofputil_append_flow_update(const struct ofputil_flow_update *update,
140f36ba
DDP
6985 struct ovs_list *replies,
6986 const struct tun_table *tun_table)
2b07c8b1 6987{
140f36ba
DDP
6988 struct ofputil_flow_update *update_ =
6989 CONST_CAST(struct ofputil_flow_update *, update);
6990 const struct tun_table *orig_tun_table;
e28ac5cf 6991 enum ofp_version version = ofpmp_version(replies);
2b07c8b1
BP
6992 struct nx_flow_update_header *nfuh;
6993 struct ofpbuf *msg;
6994 size_t start_ofs;
6995
140f36ba
DDP
6996 orig_tun_table = update->match.flow.tunnel.metadata.tab;
6997 update_->match.flow.tunnel.metadata.tab = tun_table;
6998
417e7e66 6999 msg = ofpbuf_from_list(ovs_list_back(replies));
6fd6ed71 7000 start_ofs = msg->size;
2b07c8b1
BP
7001
7002 if (update->event == NXFME_ABBREV) {
7003 struct nx_flow_update_abbrev *nfua;
7004
7005 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
7006 nfua->xid = update->xid;
7007 } else {
7008 struct nx_flow_update_full *nfuf;
7009 int match_len;
7010
7011 ofpbuf_put_zeros(msg, sizeof *nfuf);
140f36ba 7012 match_len = nx_put_match(msg, &update->match, htonll(0), htonll(0));
e3f8f887
JR
7013 ofpacts_put_openflow_actions(update->ofpacts, update->ofpacts_len, msg,
7014 version);
2b07c8b1
BP
7015 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
7016 nfuf->reason = htons(update->reason);
81a76618 7017 nfuf->priority = htons(update->priority);
2b07c8b1
BP
7018 nfuf->idle_timeout = htons(update->idle_timeout);
7019 nfuf->hard_timeout = htons(update->hard_timeout);
7020 nfuf->match_len = htons(match_len);
7021 nfuf->table_id = update->table_id;
7022 nfuf->cookie = update->cookie;
7023 }
7024
7025 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
6fd6ed71 7026 nfuh->length = htons(msg->size - start_ofs);
2b07c8b1
BP
7027 nfuh->event = htons(update->event);
7028
982697a4 7029 ofpmp_postappend(replies, start_ofs);
140f36ba 7030 update_->match.flow.tunnel.metadata.tab = orig_tun_table;
2b07c8b1
BP
7031}
7032\f
c6a93eb7 7033struct ofpbuf *
de0f3156
SH
7034ofputil_encode_packet_out(const struct ofputil_packet_out *po,
7035 enum ofputil_protocol protocol)
c6a93eb7 7036{
de0f3156 7037 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
c6a93eb7
BP
7038 struct ofpbuf *msg;
7039 size_t size;
7040
982697a4 7041 size = po->ofpacts_len;
c6a93eb7
BP
7042 if (po->buffer_id == UINT32_MAX) {
7043 size += po->packet_len;
7044 }
7045
de0f3156
SH
7046 switch (ofp_version) {
7047 case OFP10_VERSION: {
31a9e63f 7048 struct ofp10_packet_out *opo;
de0f3156
SH
7049 size_t actions_ofs;
7050
7051 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
7052 ofpbuf_put_zeros(msg, sizeof *opo);
6fd6ed71 7053 actions_ofs = msg->size;
e3f8f887
JR
7054 ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
7055 ofp_version);
de0f3156 7056
6fd6ed71 7057 opo = msg->msg;
de0f3156 7058 opo->buffer_id = htonl(po->buffer_id);
35eb6326
YHW
7059 opo->in_port =htons(ofp_to_u16(
7060 po->flow_metadata.flow.in_port.ofp_port));
6fd6ed71 7061 opo->actions_len = htons(msg->size - actions_ofs);
de0f3156
SH
7062 break;
7063 }
f25d0cf3 7064
de0f3156 7065 case OFP11_VERSION:
2e1ae200 7066 case OFP12_VERSION:
c37c0382 7067 case OFP13_VERSION:
42dccab5 7068 case OFP14_VERSION:
b79d45a1
BP
7069 case OFP15_VERSION:
7070 case OFP16_VERSION: {
7c1b1a0d
SH
7071 struct ofp11_packet_out *opo;
7072 size_t len;
7073
7074 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
7075 ofpbuf_put_zeros(msg, sizeof *opo);
e3f8f887
JR
7076 len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
7077 ofp_version);
6fd6ed71 7078 opo = msg->msg;
7c1b1a0d 7079 opo->buffer_id = htonl(po->buffer_id);
35eb6326
YHW
7080 opo->in_port =
7081 ofputil_port_to_ofp11(po->flow_metadata.flow.in_port.ofp_port);
7c1b1a0d
SH
7082 opo->actions_len = htons(len);
7083 break;
7084 }
7085
de0f3156 7086 default:
428b2edd 7087 OVS_NOT_REACHED();
de0f3156 7088 }
f25d0cf3 7089
c6a93eb7
BP
7090 if (po->buffer_id == UINT32_MAX) {
7091 ofpbuf_put(msg, po->packet, po->packet_len);
7092 }
f25d0cf3 7093
982697a4 7094 ofpmsg_update_length(msg);
c6a93eb7
BP
7095
7096 return msg;
7097}
d1e2cf21 7098\f
fa37b408
BP
7099/* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
7100struct ofpbuf *
1a126c0c 7101make_echo_request(enum ofp_version ofp_version)
fa37b408 7102{
1a126c0c 7103 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
982697a4 7104 htonl(0), 0);
fa37b408
BP
7105}
7106
7107/* Creates and returns an OFPT_ECHO_REPLY message matching the
7108 * OFPT_ECHO_REQUEST message in 'rq'. */
7109struct ofpbuf *
7110make_echo_reply(const struct ofp_header *rq)
7111{
0a2869d5 7112 struct ofpbuf rq_buf = ofpbuf_const_initializer(rq, ntohs(rq->length));
982697a4
BP
7113 ofpraw_pull_assert(&rq_buf);
7114
0a2869d5
BP
7115 struct ofpbuf *reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY,
7116 rq, rq_buf.size);
6fd6ed71 7117 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
982697a4 7118 return reply;
fa37b408
BP
7119}
7120
efb80167 7121struct ofpbuf *
a0ae0b6e 7122ofputil_encode_barrier_request(enum ofp_version ofp_version)
efb80167 7123{
a0ae0b6e
SH
7124 enum ofpraw type;
7125
7126 switch (ofp_version) {
b79d45a1 7127 case OFP16_VERSION:
42dccab5 7128 case OFP15_VERSION:
c37c0382 7129 case OFP14_VERSION:
2e1ae200 7130 case OFP13_VERSION:
a0ae0b6e
SH
7131 case OFP12_VERSION:
7132 case OFP11_VERSION:
7133 type = OFPRAW_OFPT11_BARRIER_REQUEST;
7134 break;
7135
7136 case OFP10_VERSION:
7137 type = OFPRAW_OFPT10_BARRIER_REQUEST;
7138 break;
7139
7140 default:
428b2edd 7141 OVS_NOT_REACHED();
a0ae0b6e
SH
7142 }
7143
7144 return ofpraw_alloc(type, ofp_version, 0);
efb80167
BP
7145}
7146
7257b535 7147const char *
ad99e2ed 7148ofputil_frag_handling_to_string(enum ofputil_frag_handling frag)
7257b535 7149{
ad99e2ed
BP
7150 switch (frag) {
7151 case OFPUTIL_FRAG_NORMAL: return "normal";
7152 case OFPUTIL_FRAG_DROP: return "drop";
7153 case OFPUTIL_FRAG_REASM: return "reassemble";
7154 case OFPUTIL_FRAG_NX_MATCH: return "nx-match";
7257b535
BP
7155 }
7156
428b2edd 7157 OVS_NOT_REACHED();
7257b535
BP
7158}
7159
7160bool
ad99e2ed
BP
7161ofputil_frag_handling_from_string(const char *s,
7162 enum ofputil_frag_handling *frag)
7257b535
BP
7163{
7164 if (!strcasecmp(s, "normal")) {
ad99e2ed 7165 *frag = OFPUTIL_FRAG_NORMAL;
7257b535 7166 } else if (!strcasecmp(s, "drop")) {
ad99e2ed 7167 *frag = OFPUTIL_FRAG_DROP;
7257b535 7168 } else if (!strcasecmp(s, "reassemble")) {
ad99e2ed 7169 *frag = OFPUTIL_FRAG_REASM;
7257b535 7170 } else if (!strcasecmp(s, "nx-match")) {
ad99e2ed 7171 *frag = OFPUTIL_FRAG_NX_MATCH;
7257b535
BP
7172 } else {
7173 return false;
7174 }
7175 return true;
7176}
7177
7b7503ea
BP
7178/* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
7179 * port number and stores the latter in '*ofp10_port', for the purpose of
7180 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
bc146369 7181 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
7b7503ea
BP
7182 *
7183 * See the definition of OFP11_MAX for an explanation of the mapping. */
7184enum ofperr
4e022ec0 7185ofputil_port_from_ofp11(ovs_be32 ofp11_port, ofp_port_t *ofp10_port)
7b7503ea
BP
7186{
7187 uint32_t ofp11_port_h = ntohl(ofp11_port);
7188
4e022ec0
AW
7189 if (ofp11_port_h < ofp_to_u16(OFPP_MAX)) {
7190 *ofp10_port = u16_to_ofp(ofp11_port_h);
7b7503ea 7191 return 0;
4e022ec0
AW
7192 } else if (ofp11_port_h >= ofp11_to_u32(OFPP11_MAX)) {
7193 *ofp10_port = u16_to_ofp(ofp11_port_h - OFPP11_OFFSET);
7b7503ea
BP
7194 return 0;
7195 } else {
bc146369 7196 *ofp10_port = OFPP_NONE;
7b7503ea
BP
7197 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
7198 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
4e022ec0
AW
7199 ofp11_port_h, ofp_to_u16(OFPP_MAX) - 1,
7200 ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
7b7503ea
BP
7201 return OFPERR_OFPBAC_BAD_OUT_PORT;
7202 }
7203}
7204
7205/* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
7206 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
7207 *
7208 * See the definition of OFP11_MAX for an explanation of the mapping. */
7209ovs_be32
4e022ec0 7210ofputil_port_to_ofp11(ofp_port_t ofp10_port)
7b7503ea 7211{
4e022ec0
AW
7212 return htonl(ofp_to_u16(ofp10_port) < ofp_to_u16(OFPP_MAX)
7213 ? ofp_to_u16(ofp10_port)
7214 : ofp_to_u16(ofp10_port) + OFPP11_OFFSET);
7b7503ea
BP
7215}
7216
39dc9082
BP
7217#define OFPUTIL_NAMED_PORTS \
7218 OFPUTIL_NAMED_PORT(IN_PORT) \
7219 OFPUTIL_NAMED_PORT(TABLE) \
7220 OFPUTIL_NAMED_PORT(NORMAL) \
7221 OFPUTIL_NAMED_PORT(FLOOD) \
7222 OFPUTIL_NAMED_PORT(ALL) \
7223 OFPUTIL_NAMED_PORT(CONTROLLER) \
7224 OFPUTIL_NAMED_PORT(LOCAL) \
c61f3870
BP
7225 OFPUTIL_NAMED_PORT(ANY) \
7226 OFPUTIL_NAMED_PORT(UNSET)
7f05e7ab
JR
7227
7228/* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
7229#define OFPUTIL_NAMED_PORTS_WITH_NONE \
7230 OFPUTIL_NAMED_PORTS \
39dc9082
BP
7231 OFPUTIL_NAMED_PORT(NONE)
7232
8010100b
BP
7233/* Stores the port number represented by 's' into '*portp'. 's' may be an
7234 * integer or, for reserved ports, the standard OpenFlow name for the port
7235 * (e.g. "LOCAL").
c6100d92 7236 *
8010100b
BP
7237 * Returns true if successful, false if 's' is not a valid OpenFlow port number
7238 * or name. The caller should issue an error message in this case, because
7239 * this function usually does not. (This gives the caller an opportunity to
7240 * look up the port name another way, e.g. by contacting the switch and listing
7241 * the names of all its ports).
c6100d92
BP
7242 *
7243 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
7244 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
7245 * range as described in include/openflow/openflow-1.1.h. */
8010100b 7246bool
4e022ec0 7247ofputil_port_from_string(const char *s, ofp_port_t *portp)
39dc9082 7248{
5b23ff7e 7249 unsigned int port32; /* int is at least 32 bits wide. */
c6100d92 7250
5b23ff7e
JR
7251 if (*s == '-') {
7252 VLOG_WARN("Negative value %s is not a valid port number.", s);
7253 return false;
7254 }
8010100b 7255 *portp = 0;
c6100d92 7256 if (str_to_uint(s, 10, &port32)) {
4e022ec0
AW
7257 if (port32 < ofp_to_u16(OFPP_MAX)) {
7258 /* Pass. */
7259 } else if (port32 < ofp_to_u16(OFPP_FIRST_RESV)) {
c6100d92
BP
7260 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
7261 "be translated to %u when talking to an OF1.1 or "
7262 "later controller", port32, port32 + OFPP11_OFFSET);
4e022ec0 7263 } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) {
2f2b904f 7264 char name[OFP10_MAX_PORT_NAME_LEN];
28b11432
BP
7265
7266 ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name);
7267 VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated "
7268 "for compatibility with OpenFlow 1.1 and later",
7269 name, port32);
4e022ec0 7270 } else if (port32 < ofp11_to_u32(OFPP11_MAX)) {
c6100d92 7271 VLOG_WARN("port %u is outside the supported range 0 through "
fd13c6b5 7272 "%x or 0x%x through 0x%"PRIx32, port32,
4e022ec0 7273 UINT16_MAX, ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
8010100b 7274 return false;
c6100d92 7275 } else {
4e022ec0 7276 port32 -= OFPP11_OFFSET;
c6100d92 7277 }
4e022ec0
AW
7278
7279 *portp = u16_to_ofp(port32);
7280 return true;
c6100d92
BP
7281 } else {
7282 struct pair {
7283 const char *name;
4e022ec0 7284 ofp_port_t value;
c6100d92
BP
7285 };
7286 static const struct pair pairs[] = {
39dc9082 7287#define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
7f05e7ab 7288 OFPUTIL_NAMED_PORTS_WITH_NONE
39dc9082 7289#undef OFPUTIL_NAMED_PORT
c6100d92
BP
7290 };
7291 const struct pair *p;
39dc9082 7292
c6100d92
BP
7293 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
7294 if (!strcasecmp(s, p->name)) {
8010100b
BP
7295 *portp = p->value;
7296 return true;
c6100d92 7297 }
39dc9082 7298 }
8010100b 7299 return false;
39dc9082 7300 }
39dc9082
BP
7301}
7302
7303/* Appends to 's' a string representation of the OpenFlow port number 'port'.
7304 * Most ports' string representation is just the port number, but for special
7305 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
7306void
4e022ec0 7307ofputil_format_port(ofp_port_t port, struct ds *s)
39dc9082 7308{
2f2b904f 7309 char name[OFP10_MAX_PORT_NAME_LEN];
28b11432
BP
7310
7311 ofputil_port_to_string(port, name, sizeof name);
7312 ds_put_cstr(s, name);
7313}
39dc9082 7314
28b11432
BP
7315/* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7316 * representation of OpenFlow port number 'port'. Most ports are represented
7317 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
7318 * by name, e.g. "LOCAL". */
7319void
7320ofputil_port_to_string(ofp_port_t port,
2f2b904f 7321 char namebuf[OFP10_MAX_PORT_NAME_LEN], size_t bufsize)
28b11432 7322{
39dc9082 7323 switch (port) {
28b11432
BP
7324#define OFPUTIL_NAMED_PORT(NAME) \
7325 case OFPP_##NAME: \
7326 ovs_strlcpy(namebuf, #NAME, bufsize); \
7327 break;
39dc9082
BP
7328 OFPUTIL_NAMED_PORTS
7329#undef OFPUTIL_NAMED_PORT
7330
7331 default:
94783c7c 7332 snprintf(namebuf, bufsize, "%"PRIu32, port);
28b11432 7333 break;
39dc9082 7334 }
39dc9082
BP
7335}
7336
7395c052
NZ
7337/* Stores the group id represented by 's' into '*group_idp'. 's' may be an
7338 * integer or, for reserved group IDs, the standard OpenFlow name for the group
7339 * (either "ANY" or "ALL").
7340 *
7341 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
7342 * name. */
7343bool
7344ofputil_group_from_string(const char *s, uint32_t *group_idp)
7345{
7346 if (!strcasecmp(s, "any")) {
30ef36c6 7347 *group_idp = OFPG_ANY;
7395c052 7348 } else if (!strcasecmp(s, "all")) {
30ef36c6 7349 *group_idp = OFPG_ALL;
7395c052
NZ
7350 } else if (!str_to_uint(s, 10, group_idp)) {
7351 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
7352 "32-bit nonnegative integers or the keywords ANY or "
7353 "ALL.)", s);
7354 return false;
7355 }
7356
7357 return true;
7358}
7359
7360/* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
7361 * Most groups' string representation is just the number, but for special
30ef36c6 7362 * groups, e.g. OFPG_ALL, it is the name, e.g. "ALL". */
7395c052
NZ
7363void
7364ofputil_format_group(uint32_t group_id, struct ds *s)
7365{
7366 char name[MAX_GROUP_NAME_LEN];
7367
7368 ofputil_group_to_string(group_id, name, sizeof name);
7369 ds_put_cstr(s, name);
7370}
7371
7372
7373/* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7374 * representation of OpenFlow group ID 'group_id'. Most group are represented
30ef36c6 7375 * as just their number, but special groups, e.g. OFPG_ALL, are represented
7395c052
NZ
7376 * by name, e.g. "ALL". */
7377void
7378ofputil_group_to_string(uint32_t group_id,
7379 char namebuf[MAX_GROUP_NAME_LEN + 1], size_t bufsize)
7380{
7381 switch (group_id) {
30ef36c6 7382 case OFPG_ALL:
7395c052
NZ
7383 ovs_strlcpy(namebuf, "ALL", bufsize);
7384 break;
7385
30ef36c6 7386 case OFPG_ANY:
7395c052
NZ
7387 ovs_strlcpy(namebuf, "ANY", bufsize);
7388 break;
7389
7390 default:
7391 snprintf(namebuf, bufsize, "%"PRIu32, group_id);
7392 break;
7393 }
7394}
7395
2be393ed
JP
7396/* Given a buffer 'b' that contains an array of OpenFlow ports of type
7397 * 'ofp_version', tries to pull the first element from the array. If
7398 * successful, initializes '*pp' with an abstract representation of the
7399 * port and returns 0. If no ports remain to be decoded, returns EOF.
7400 * On an error, returns a positive OFPERR_* value. */
7401int
2e3fa633 7402ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
2be393ed
JP
7403 struct ofputil_phy_port *pp)
7404{
8c3cc785
BP
7405 memset(pp, 0, sizeof *pp);
7406
2e3fa633
SH
7407 switch (ofp_version) {
7408 case OFP10_VERSION: {
2be393ed
JP
7409 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
7410 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
2e3fa633
SH
7411 }
7412 case OFP11_VERSION:
2e1ae200
JR
7413 case OFP12_VERSION:
7414 case OFP13_VERSION: {
2be393ed
JP
7415 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
7416 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
7417 }
c37c0382 7418 case OFP14_VERSION:
42dccab5 7419 case OFP15_VERSION:
6fd6ed71 7420 return b->size ? ofputil_pull_ofp14_port(pp, b) : EOF;
2f2b904f
BP
7421 case OFP16_VERSION:
7422 return b->size ? ofputil_pull_ofp16_port(pp, b) : EOF;
2e3fa633 7423 default:
428b2edd 7424 OVS_NOT_REACHED();
2e3fa633 7425 }
2be393ed
JP
7426}
7427
3cbd9931 7428static void
81a76618 7429ofputil_normalize_match__(struct match *match, bool may_log)
b459a924
BP
7430{
7431 enum {
7432 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
7433 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
7434 MAY_NW_PROTO = 1 << 2, /* nw_proto */
a61680c6 7435 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
b459a924
BP
7436 MAY_ARP_SHA = 1 << 4, /* arp_sha */
7437 MAY_ARP_THA = 1 << 5, /* arp_tha */
d78477ec 7438 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
b02475c5
SH
7439 MAY_ND_TARGET = 1 << 7, /* nd_target */
7440 MAY_MPLS = 1 << 8, /* mpls label and tc */
b459a924
BP
7441 } may_match;
7442
7443 struct flow_wildcards wc;
7444
7445 /* Figure out what fields may be matched. */
81a76618 7446 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
9e44d715 7447 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
81a76618
BP
7448 if (match->flow.nw_proto == IPPROTO_TCP ||
7449 match->flow.nw_proto == IPPROTO_UDP ||
0d56eaf2 7450 match->flow.nw_proto == IPPROTO_SCTP ||
81a76618 7451 match->flow.nw_proto == IPPROTO_ICMP) {
b459a924
BP
7452 may_match |= MAY_TP_ADDR;
7453 }
81a76618 7454 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
d78477ec 7455 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
81a76618 7456 if (match->flow.nw_proto == IPPROTO_TCP ||
0d56eaf2
JS
7457 match->flow.nw_proto == IPPROTO_UDP ||
7458 match->flow.nw_proto == IPPROTO_SCTP) {
b459a924 7459 may_match |= MAY_TP_ADDR;
81a76618 7460 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
b459a924 7461 may_match |= MAY_TP_ADDR;
81a76618 7462 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
b459a924 7463 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
81a76618 7464 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
b459a924
BP
7465 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
7466 }
7467 }
8087f5ff
MM
7468 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
7469 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
27527aa0 7470 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
b02475c5
SH
7471 } else if (eth_type_mpls(match->flow.dl_type)) {
7472 may_match = MAY_MPLS;
1c0b7503 7473 } else {
b459a924
BP
7474 may_match = 0;
7475 }
7476
7477 /* Clear the fields that may not be matched. */
81a76618 7478 wc = match->wc;
b459a924 7479 if (!(may_match & MAY_NW_ADDR)) {
26720e24 7480 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
b459a924
BP
7481 }
7482 if (!(may_match & MAY_TP_ADDR)) {
26720e24 7483 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
b459a924
BP
7484 }
7485 if (!(may_match & MAY_NW_PROTO)) {
26720e24 7486 wc.masks.nw_proto = 0;
b459a924 7487 }
9e44d715 7488 if (!(may_match & MAY_IPVx)) {
26720e24
BP
7489 wc.masks.nw_tos = 0;
7490 wc.masks.nw_ttl = 0;
b459a924
BP
7491 }
7492 if (!(may_match & MAY_ARP_SHA)) {
74ff3298 7493 WC_UNMASK_FIELD(&wc, arp_sha);
b459a924
BP
7494 }
7495 if (!(may_match & MAY_ARP_THA)) {
74ff3298 7496 WC_UNMASK_FIELD(&wc, arp_tha);
b459a924 7497 }
d78477ec 7498 if (!(may_match & MAY_IPV6)) {
26720e24
BP
7499 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
7500 wc.masks.ipv6_label = htonl(0);
b459a924
BP
7501 }
7502 if (!(may_match & MAY_ND_TARGET)) {
26720e24 7503 wc.masks.nd_target = in6addr_any;
b459a924 7504 }
b02475c5 7505 if (!(may_match & MAY_MPLS)) {
8bfd0fda 7506 memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse);
b02475c5 7507 }
b459a924
BP
7508
7509 /* Log any changes. */
81a76618 7510 if (!flow_wildcards_equal(&wc, &match->wc)) {
3cbd9931 7511 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
81a76618 7512 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
b459a924 7513
81a76618
BP
7514 match->wc = wc;
7515 match_zero_wildcarded_fields(match);
b459a924
BP
7516
7517 if (log) {
81a76618 7518 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
b459a924
BP
7519 VLOG_INFO("normalization changed ofp_match, details:");
7520 VLOG_INFO(" pre: %s", pre);
7521 VLOG_INFO("post: %s", post);
7522 free(pre);
7523 free(post);
7524 }
fa37b408 7525 }
3f09c339 7526}
26c112c2 7527
81a76618 7528/* "Normalizes" the wildcards in 'match'. That means:
3cbd9931
BP
7529 *
7530 * 1. If the type of level N is known, then only the valid fields for that
7531 * level may be specified. For example, ARP does not have a TOS field,
81a76618 7532 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
3cbd9931 7533 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
81a76618 7534 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
3cbd9931
BP
7535 * IPv4 flow.
7536 *
7537 * 2. If the type of level N is not known (or not understood by Open
7538 * vSwitch), then no fields at all for that level may be specified. For
7539 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
81a76618 7540 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
3cbd9931
BP
7541 * SCTP flow.
7542 *
81a76618 7543 * If this function changes 'match', it logs a rate-limited informational
3cbd9931
BP
7544 * message. */
7545void
81a76618 7546ofputil_normalize_match(struct match *match)
3cbd9931 7547{
81a76618 7548 ofputil_normalize_match__(match, true);
3cbd9931
BP
7549}
7550
81a76618
BP
7551/* Same as ofputil_normalize_match() without the logging. Thus, this function
7552 * is suitable for a program's internal use, whereas ofputil_normalize_match()
3cbd9931
BP
7553 * sense for use on flows received from elsewhere (so that a bug in the program
7554 * that sent them can be reported and corrected). */
7555void
81a76618 7556ofputil_normalize_match_quiet(struct match *match)
3cbd9931 7557{
81a76618 7558 ofputil_normalize_match__(match, false);
3cbd9931
BP
7559}
7560
4a48cdfb
BP
7561static size_t
7562parse_value(const char *s, const char *delimiters)
7563{
7564 size_t n = 0;
7565
7566 /* Iterate until we reach a delimiter.
7567 *
7568 * strchr(s, '\0') returns s+strlen(s), so this test handles the null
7569 * terminator at the end of 's'. */
7570 while (!strchr(delimiters, s[n])) {
7571 if (s[n] == '(') {
7572 int level = 0;
7573 do {
7574 switch (s[n]) {
7575 case '\0':
7576 return n;
7577 case '(':
7578 level++;
7579 break;
7580 case ')':
7581 level--;
7582 break;
7583 }
7584 n++;
7585 } while (level > 0);
7586 } else {
7587 n++;
7588 }
7589 }
7590 return n;
7591}
7592
0ff22822
BP
7593/* Parses a key or a key-value pair from '*stringp'.
7594 *
7595 * On success: Stores the key into '*keyp'. Stores the value, if present, into
7596 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
7597 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
7598 * are substrings of '*stringp' created by replacing some of its bytes by null
7599 * terminators. Returns true.
7600 *
7601 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
7602 * NULL and returns false. */
7603bool
7604ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
7605{
4a48cdfb
BP
7606 /* Skip white space and delimiters. If that brings us to the end of the
7607 * input string, we are done and there are no more key-value pairs. */
7608 *stringp += strspn(*stringp, ", \t\r\n");
7609 if (**stringp == '\0') {
0ff22822
BP
7610 *keyp = *valuep = NULL;
7611 return false;
7612 }
7613
4a48cdfb
BP
7614 /* Extract the key and the delimiter that ends the key-value pair or begins
7615 * the value. Advance the input position past the key and delimiter. */
7616 char *key = *stringp;
7617 size_t key_len = strcspn(key, ":=(, \t\r\n");
7618 char key_delim = key[key_len];
7619 key[key_len] = '\0';
7620 *stringp += key_len + (key_delim != '\0');
0ff22822 7621
4a48cdfb
BP
7622 /* Figure out what delimiter ends the value:
7623 *
7624 * - If key_delim is ":" or "=", the value extends until white space
7625 * or a comma.
7626 *
7627 * - If key_delim is "(", the value extends until ")".
7628 *
7629 * If there is no value, we are done. */
7630 const char *value_delims;
7631 if (key_delim == ':' || key_delim == '=') {
7632 value_delims = ", \t\r\n";
7633 } else if (key_delim == '(') {
7634 value_delims = ")";
0ff22822 7635 } else {
4a48cdfb
BP
7636 *keyp = key;
7637 *valuep = key + key_len; /* Empty string. */
7638 return true;
0ff22822 7639 }
0ff22822 7640
4a48cdfb
BP
7641 /* Extract the value. Advance the input position past the value and
7642 * delimiter. */
7643 char *value = *stringp;
7644 size_t value_len = parse_value(value, value_delims);
7645 char value_delim = value[value_len];
7646 value[value_len] = '\0';
7647 *stringp += value_len + (value_delim != '\0');
7648
0ff22822
BP
7649 *keyp = key;
7650 *valuep = value;
7651 return true;
7652}
f8e4867e
SH
7653
7654/* Encode a dump ports request for 'port', the encoded message
0746a84f 7655 * will be for OpenFlow version 'ofp_version'. Returns message
f8e4867e
SH
7656 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
7657struct ofpbuf *
4e022ec0 7658ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port)
f8e4867e
SH
7659{
7660 struct ofpbuf *request;
7661
7662 switch (ofp_version) {
7663 case OFP10_VERSION: {
7664 struct ofp10_port_stats_request *req;
7665 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
7666 req = ofpbuf_put_zeros(request, sizeof *req);
4e022ec0 7667 req->port_no = htons(ofp_to_u16(port));
f8e4867e
SH
7668 break;
7669 }
7670 case OFP11_VERSION:
2e1ae200 7671 case OFP12_VERSION:
c37c0382 7672 case OFP13_VERSION:
42dccab5 7673 case OFP14_VERSION:
b79d45a1
BP
7674 case OFP15_VERSION:
7675 case OFP16_VERSION: {
f8e4867e
SH
7676 struct ofp11_port_stats_request *req;
7677 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
7678 req = ofpbuf_put_zeros(request, sizeof *req);
7679 req->port_no = ofputil_port_to_ofp11(port);
7680 break;
7681 }
7682 default:
428b2edd 7683 OVS_NOT_REACHED();
f8e4867e
SH
7684 }
7685
7686 return request;
7687}
7688
7689static void
7690ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
7691 struct ofp10_port_stats *ps10)
7692{
4e022ec0 7693 ps10->port_no = htons(ofp_to_u16(ops->port_no));
f8e4867e
SH
7694 memset(ps10->pad, 0, sizeof ps10->pad);
7695 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
7696 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
7697 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
7698 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
7699 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
7700 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
7701 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
7702 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
7703 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
7704 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
7705 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
7706 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
7707}
7708
7709static void
7710ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
7711 struct ofp11_port_stats *ps11)
7712{
7713 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
7714 memset(ps11->pad, 0, sizeof ps11->pad);
7715 ps11->rx_packets = htonll(ops->stats.rx_packets);
7716 ps11->tx_packets = htonll(ops->stats.tx_packets);
7717 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
7718 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
7719 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
7720 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
7721 ps11->rx_errors = htonll(ops->stats.rx_errors);
7722 ps11->tx_errors = htonll(ops->stats.tx_errors);
7723 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
7724 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
7725 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
7726 ps11->collisions = htonll(ops->stats.collisions);
7727}
7728
2e1ae200
JR
7729static void
7730ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
7731 struct ofp13_port_stats *ps13)
7732{
7733 ofputil_port_stats_to_ofp11(ops, &ps13->ps);
65e0be10
BP
7734 ps13->duration_sec = htonl(ops->duration_sec);
7735 ps13->duration_nsec = htonl(ops->duration_nsec);
2e1ae200
JR
7736}
7737
5469537b
BP
7738static void
7739ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops,
ca6ba700 7740 struct ovs_list *replies)
5469537b
BP
7741{
7742 struct ofp14_port_stats_prop_ethernet *eth;
d6e3feb5 7743 struct intel_port_stats_rfc2819 *stats_rfc2819;
5469537b
BP
7744 struct ofp14_port_stats *ps14;
7745 struct ofpbuf *reply;
7746
d6e3feb5 7747 reply = ofpmp_reserve(replies, sizeof *ps14 + sizeof *eth +
7748 sizeof *stats_rfc2819);
5469537b
BP
7749
7750 ps14 = ofpbuf_put_uninit(reply, sizeof *ps14);
d6e3feb5 7751 ps14->length = htons(sizeof *ps14 + sizeof *eth +
7752 sizeof *stats_rfc2819);
5469537b
BP
7753 memset(ps14->pad, 0, sizeof ps14->pad);
7754 ps14->port_no = ofputil_port_to_ofp11(ops->port_no);
7755 ps14->duration_sec = htonl(ops->duration_sec);
7756 ps14->duration_nsec = htonl(ops->duration_nsec);
7757 ps14->rx_packets = htonll(ops->stats.rx_packets);
7758 ps14->tx_packets = htonll(ops->stats.tx_packets);
7759 ps14->rx_bytes = htonll(ops->stats.rx_bytes);
7760 ps14->tx_bytes = htonll(ops->stats.tx_bytes);
7761 ps14->rx_dropped = htonll(ops->stats.rx_dropped);
7762 ps14->tx_dropped = htonll(ops->stats.tx_dropped);
7763 ps14->rx_errors = htonll(ops->stats.rx_errors);
7764 ps14->tx_errors = htonll(ops->stats.tx_errors);
7765
303721ee 7766 eth = ofpprop_put_zeros(reply, OFPPSPT14_ETHERNET, sizeof *eth);
5469537b
BP
7767 eth->rx_frame_err = htonll(ops->stats.rx_frame_errors);
7768 eth->rx_over_err = htonll(ops->stats.rx_over_errors);
7769 eth->rx_crc_err = htonll(ops->stats.rx_crc_errors);
7770 eth->collisions = htonll(ops->stats.collisions);
d6e3feb5 7771
7772 uint64_t prop_type = OFPPROP_EXP(INTEL_VENDOR_ID,
7773 INTEL_PORT_STATS_RFC2819);
7774
7775 stats_rfc2819 = ofpprop_put_zeros(reply, prop_type,
7776 sizeof *stats_rfc2819);
7777
7778 memset(stats_rfc2819->pad, 0, sizeof stats_rfc2819->pad);
7779 stats_rfc2819->rx_1_to_64_packets = htonll(ops->stats.rx_1_to_64_packets);
7780 stats_rfc2819->rx_65_to_127_packets =
7781 htonll(ops->stats.rx_65_to_127_packets);
7782 stats_rfc2819->rx_128_to_255_packets =
7783 htonll(ops->stats.rx_128_to_255_packets);
7784 stats_rfc2819->rx_256_to_511_packets =
7785 htonll(ops->stats.rx_256_to_511_packets);
7786 stats_rfc2819->rx_512_to_1023_packets =
7787 htonll(ops->stats.rx_512_to_1023_packets);
7788 stats_rfc2819->rx_1024_to_1522_packets =
7789 htonll(ops->stats.rx_1024_to_1522_packets);
7790 stats_rfc2819->rx_1523_to_max_packets =
7791 htonll(ops->stats.rx_1523_to_max_packets);
7792
7793 stats_rfc2819->tx_1_to_64_packets = htonll(ops->stats.tx_1_to_64_packets);
7794 stats_rfc2819->tx_65_to_127_packets =
7795 htonll(ops->stats.tx_65_to_127_packets);
7796 stats_rfc2819->tx_128_to_255_packets =
7797 htonll(ops->stats.tx_128_to_255_packets);
7798 stats_rfc2819->tx_256_to_511_packets =
7799 htonll(ops->stats.tx_256_to_511_packets);
7800 stats_rfc2819->tx_512_to_1023_packets =
7801 htonll(ops->stats.tx_512_to_1023_packets);
7802 stats_rfc2819->tx_1024_to_1522_packets =
7803 htonll(ops->stats.tx_1024_to_1522_packets);
7804 stats_rfc2819->tx_1523_to_max_packets =
7805 htonll(ops->stats.tx_1523_to_max_packets);
7806
7807 stats_rfc2819->tx_multicast_packets =
7808 htonll(ops->stats.tx_multicast_packets);
7809 stats_rfc2819->rx_broadcast_packets =
7810 htonll(ops->stats.rx_broadcast_packets);
7811 stats_rfc2819->tx_broadcast_packets =
7812 htonll(ops->stats.tx_broadcast_packets);
7813 stats_rfc2819->rx_undersized_errors =
7814 htonll(ops->stats.rx_undersized_errors);
7815 stats_rfc2819->rx_oversize_errors =
7816 htonll(ops->stats.rx_oversize_errors);
7817 stats_rfc2819->rx_fragmented_errors =
7818 htonll(ops->stats.rx_fragmented_errors);
7819 stats_rfc2819->rx_jabber_errors =
7820 htonll(ops->stats.rx_jabber_errors);
5469537b 7821}
2e1ae200 7822
ad4c35fe 7823/* Encode a ports stat for 'ops' and append it to 'replies'. */
f8e4867e 7824void
ca6ba700 7825ofputil_append_port_stat(struct ovs_list *replies,
f8e4867e
SH
7826 const struct ofputil_port_stats *ops)
7827{
e28ac5cf 7828 switch (ofpmp_version(replies)) {
2e1ae200
JR
7829 case OFP13_VERSION: {
7830 struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7831 ofputil_port_stats_to_ofp13(ops, reply);
7832 break;
7833 }
f8e4867e
SH
7834 case OFP12_VERSION:
7835 case OFP11_VERSION: {
7836 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7837 ofputil_port_stats_to_ofp11(ops, reply);
7838 break;
7839 }
7840
7841 case OFP10_VERSION: {
7842 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7843 ofputil_port_stats_to_ofp10(ops, reply);
7844 break;
7845 }
7846
c37c0382 7847 case OFP14_VERSION:
42dccab5 7848 case OFP15_VERSION:
b79d45a1 7849 case OFP16_VERSION:
5469537b 7850 ofputil_append_ofp14_port_stats(ops, replies);
c37c0382
AC
7851 break;
7852
f8e4867e 7853 default:
428b2edd 7854 OVS_NOT_REACHED();
f8e4867e
SH
7855 }
7856}
7857
7858static enum ofperr
7859ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
7860 const struct ofp10_port_stats *ps10)
7861{
f8e4867e 7862
4e022ec0 7863 ops->port_no = u16_to_ofp(ntohs(ps10->port_no));
f8e4867e
SH
7864 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
7865 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
7866 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
7867 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
7868 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
7869 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
7870 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
7871 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
7872 ops->stats.rx_frame_errors =
7873 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
7874 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
7875 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
7876 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
65e0be10 7877 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
f8e4867e
SH
7878
7879 return 0;
7880}
7881
7882static enum ofperr
7883ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
7884 const struct ofp11_port_stats *ps11)
7885{
7886 enum ofperr error;
7887
f8e4867e
SH
7888 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
7889 if (error) {
7890 return error;
7891 }
7892
7893 ops->stats.rx_packets = ntohll(ps11->rx_packets);
7894 ops->stats.tx_packets = ntohll(ps11->tx_packets);
7895 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
7896 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
7897 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
7898 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
7899 ops->stats.rx_errors = ntohll(ps11->rx_errors);
7900 ops->stats.tx_errors = ntohll(ps11->tx_errors);
7901 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
7902 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
7903 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
7904 ops->stats.collisions = ntohll(ps11->collisions);
65e0be10 7905 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
f8e4867e
SH
7906
7907 return 0;
7908}
7909
2e1ae200
JR
7910static enum ofperr
7911ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
7912 const struct ofp13_port_stats *ps13)
7913{
65e0be10 7914 enum ofperr error = ofputil_port_stats_from_ofp11(ops, &ps13->ps);
2e1ae200 7915 if (!error) {
65e0be10
BP
7916 ops->duration_sec = ntohl(ps13->duration_sec);
7917 ops->duration_nsec = ntohl(ps13->duration_nsec);
2e1ae200 7918 }
2e1ae200
JR
7919 return error;
7920}
7921
5469537b
BP
7922static enum ofperr
7923parse_ofp14_port_stats_ethernet_property(const struct ofpbuf *payload,
7924 struct ofputil_port_stats *ops)
be0c30df 7925{
6fd6ed71 7926 const struct ofp14_port_stats_prop_ethernet *eth = payload->data;
5469537b 7927
6fd6ed71 7928 if (payload->size != sizeof *eth) {
5469537b
BP
7929 return OFPERR_OFPBPC_BAD_LEN;
7930 }
7931
7932 ops->stats.rx_frame_errors = ntohll(eth->rx_frame_err);
7933 ops->stats.rx_over_errors = ntohll(eth->rx_over_err);
7934 ops->stats.rx_crc_errors = ntohll(eth->rx_crc_err);
7935 ops->stats.collisions = ntohll(eth->collisions);
7936
7937 return 0;
7938}
7939
d6e3feb5 7940static enum ofperr
7941parse_intel_port_stats_rfc2819_property(const struct ofpbuf *payload,
7942 struct ofputil_port_stats *ops)
7943{
7944 const struct intel_port_stats_rfc2819 *rfc2819 = payload->data;
7945
7946 if (payload->size != sizeof *rfc2819) {
7947 return OFPERR_OFPBPC_BAD_LEN;
7948 }
7949 ops->stats.rx_1_to_64_packets = ntohll(rfc2819->rx_1_to_64_packets);
7950 ops->stats.rx_65_to_127_packets = ntohll(rfc2819->rx_65_to_127_packets);
7951 ops->stats.rx_128_to_255_packets = ntohll(rfc2819->rx_128_to_255_packets);
7952 ops->stats.rx_256_to_511_packets = ntohll(rfc2819->rx_256_to_511_packets);
7953 ops->stats.rx_512_to_1023_packets =
7954 ntohll(rfc2819->rx_512_to_1023_packets);
7955 ops->stats.rx_1024_to_1522_packets =
7956 ntohll(rfc2819->rx_1024_to_1522_packets);
7957 ops->stats.rx_1523_to_max_packets =
7958 ntohll(rfc2819->rx_1523_to_max_packets);
7959
7960 ops->stats.tx_1_to_64_packets = ntohll(rfc2819->tx_1_to_64_packets);
7961 ops->stats.tx_65_to_127_packets = ntohll(rfc2819->tx_65_to_127_packets);
7962 ops->stats.tx_128_to_255_packets = ntohll(rfc2819->tx_128_to_255_packets);
7963 ops->stats.tx_256_to_511_packets = ntohll(rfc2819->tx_256_to_511_packets);
7964 ops->stats.tx_512_to_1023_packets =
7965 ntohll(rfc2819->tx_512_to_1023_packets);
7966 ops->stats.tx_1024_to_1522_packets =
7967 ntohll(rfc2819->tx_1024_to_1522_packets);
7968 ops->stats.tx_1523_to_max_packets =
7969 ntohll(rfc2819->tx_1523_to_max_packets);
7970
7971 ops->stats.tx_multicast_packets = ntohll(rfc2819->tx_multicast_packets);
7972 ops->stats.rx_broadcast_packets = ntohll(rfc2819->rx_broadcast_packets);
7973 ops->stats.tx_broadcast_packets = ntohll(rfc2819->tx_broadcast_packets);
7974 ops->stats.rx_undersized_errors = ntohll(rfc2819->rx_undersized_errors);
7975
7976 ops->stats.rx_oversize_errors = ntohll(rfc2819->rx_oversize_errors);
7977 ops->stats.rx_fragmented_errors = ntohll(rfc2819->rx_fragmented_errors);
7978 ops->stats.rx_jabber_errors = ntohll(rfc2819->rx_jabber_errors);
7979
7980 return 0;
7981}
7982
7983static enum ofperr
7984parse_intel_port_stats_property(const struct ofpbuf *payload,
7985 uint32_t exp_type,
7986 struct ofputil_port_stats *ops)
7987{
7988 enum ofperr error;
7989
7990 switch (exp_type) {
7991 case INTEL_PORT_STATS_RFC2819:
7992 error = parse_intel_port_stats_rfc2819_property(payload, ops);
7993 break;
7994 default:
7995 error = OFPERR_OFPBPC_BAD_EXP_TYPE;
7996 break;
7997 }
7998
7999 return error;
8000}
8001
5469537b
BP
8002static enum ofperr
8003ofputil_pull_ofp14_port_stats(struct ofputil_port_stats *ops,
8004 struct ofpbuf *msg)
8005{
0a2869d5 8006 const struct ofp14_port_stats *ps14 = ofpbuf_try_pull(msg, sizeof *ps14);
5469537b
BP
8007 if (!ps14) {
8008 return OFPERR_OFPBRC_BAD_LEN;
8009 }
8010
0a2869d5 8011 size_t len = ntohs(ps14->length);
6fd6ed71 8012 if (len < sizeof *ps14 || len - sizeof *ps14 > msg->size) {
5469537b 8013 return OFPERR_OFPBRC_BAD_LEN;
be0c30df 8014 }
5469537b 8015 len -= sizeof *ps14;
5469537b 8016
0a2869d5 8017 enum ofperr error = ofputil_port_from_ofp11(ps14->port_no, &ops->port_no);
5469537b
BP
8018 if (error) {
8019 return error;
8020 }
8021
8022 ops->duration_sec = ntohl(ps14->duration_sec);
8023 ops->duration_nsec = ntohl(ps14->duration_nsec);
8024 ops->stats.rx_packets = ntohll(ps14->rx_packets);
8025 ops->stats.tx_packets = ntohll(ps14->tx_packets);
8026 ops->stats.rx_bytes = ntohll(ps14->rx_bytes);
8027 ops->stats.tx_bytes = ntohll(ps14->tx_bytes);
8028 ops->stats.rx_dropped = ntohll(ps14->rx_dropped);
8029 ops->stats.tx_dropped = ntohll(ps14->tx_dropped);
8030 ops->stats.rx_errors = ntohll(ps14->rx_errors);
8031 ops->stats.tx_errors = ntohll(ps14->tx_errors);
d6e3feb5 8032
5469537b 8033
0a2869d5
BP
8034 struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
8035 len);
6fd6ed71 8036 while (properties.size > 0) {
5469537b
BP
8037 struct ofpbuf payload;
8038 enum ofperr error;
d6e3feb5 8039 uint64_t type = 0;
5469537b 8040
c5562271 8041 error = ofpprop_pull(&properties, &payload, &type);
5469537b
BP
8042 if (error) {
8043 return error;
8044 }
5469537b
BP
8045 switch (type) {
8046 case OFPPSPT14_ETHERNET:
8047 error = parse_ofp14_port_stats_ethernet_property(&payload, ops);
8048 break;
d6e3feb5 8049 case OFPPROP_EXP(INTEL_VENDOR_ID, INTEL_PORT_STATS_RFC2819):
8050 error = parse_intel_port_stats_property(&payload,
8051 INTEL_PORT_STATS_RFC2819,
8052 ops);
8053 break;
5469537b 8054 default:
34a543e3 8055 error = OFPPROP_UNKNOWN(true, "port stats", type);
5469537b
BP
8056 break;
8057 }
8058
8059 if (error) {
8060 return error;
8061 }
8062 }
8063
8064 return 0;
be0c30df 8065}
2e1ae200 8066
f8e4867e
SH
8067/* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
8068 * message 'oh'. */
8069size_t
8070ofputil_count_port_stats(const struct ofp_header *oh)
8071{
0a2869d5 8072 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
f8e4867e 8073 ofpraw_pull_assert(&b);
0a2869d5
BP
8074
8075 for (size_t n = 0; ; n++) {
8076 struct ofputil_port_stats ps;
8077 if (ofputil_decode_port_stats(&ps, &b)) {
8078 return n;
8079 }
5469537b 8080 }
f8e4867e
SH
8081}
8082
8083/* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
8084 * ofputil_port_stats in 'ps'.
8085 *
8086 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
8087 * message. Calling this function multiple times for a single 'msg' iterates
8088 * through the replies. The caller must initially leave 'msg''s layer pointers
8089 * null and not modify them between calls.
8090 *
8091 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8092 * otherwise a positive errno value. */
8093int
8094ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
8095{
8096 enum ofperr error;
8097 enum ofpraw raw;
8098
d6e3feb5 8099 memset(&(ps->stats), 0xFF, sizeof (ps->stats));
8100
6fd6ed71 8101 error = (msg->header ? ofpraw_decode(&raw, msg->header)
f8e4867e
SH
8102 : ofpraw_pull(&raw, msg));
8103 if (error) {
8104 return error;
8105 }
8106
6fd6ed71 8107 if (!msg->size) {
f8e4867e 8108 return EOF;
5469537b
BP
8109 } else if (raw == OFPRAW_OFPST14_PORT_REPLY) {
8110 return ofputil_pull_ofp14_port_stats(ps, msg);
2e1ae200
JR
8111 } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
8112 const struct ofp13_port_stats *ps13;
2e1ae200
JR
8113 ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
8114 if (!ps13) {
8115 goto bad_len;
8116 }
8117 return ofputil_port_stats_from_ofp13(ps, ps13);
f8e4867e
SH
8118 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
8119 const struct ofp11_port_stats *ps11;
8120
8121 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
8122 if (!ps11) {
2e1ae200 8123 goto bad_len;
f8e4867e
SH
8124 }
8125 return ofputil_port_stats_from_ofp11(ps, ps11);
8126 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
8127 const struct ofp10_port_stats *ps10;
8128
8129 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
8130 if (!ps10) {
2e1ae200 8131 goto bad_len;
f8e4867e
SH
8132 }
8133 return ofputil_port_stats_from_ofp10(ps, ps10);
8134 } else {
428b2edd 8135 OVS_NOT_REACHED();
f8e4867e
SH
8136 }
8137
2e1ae200 8138 bad_len:
437d0d22 8139 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %"PRIu32" leftover "
6fd6ed71 8140 "bytes at end", msg->size);
2e1ae200 8141 return OFPERR_OFPBRC_BAD_LEN;
f8e4867e
SH
8142}
8143
8144/* Parse a port status request message into a 16 bit OpenFlow 1.0
8145 * port number and stores the latter in '*ofp10_port'.
8146 * Returns 0 if successful, otherwise an OFPERR_* number. */
8147enum ofperr
8148ofputil_decode_port_stats_request(const struct ofp_header *request,
4e022ec0 8149 ofp_port_t *ofp10_port)
f8e4867e
SH
8150{
8151 switch ((enum ofp_version)request->version) {
b79d45a1 8152 case OFP16_VERSION:
42dccab5 8153 case OFP15_VERSION:
5469537b 8154 case OFP14_VERSION:
2e1ae200 8155 case OFP13_VERSION:
f8e4867e
SH
8156 case OFP12_VERSION:
8157 case OFP11_VERSION: {
8158 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
8159 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
8160 }
8161
8162 case OFP10_VERSION: {
8163 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
4e022ec0 8164 *ofp10_port = u16_to_ofp(ntohs(psr10->port_no));
f8e4867e
SH
8165 return 0;
8166 }
8167
8168 default:
428b2edd 8169 OVS_NOT_REACHED();
f8e4867e
SH
8170 }
8171}
64626975 8172
fb8f22c1
BY
8173static void
8174ofputil_ipfix_stats_to_reply(const struct ofputil_ipfix_stats *ois,
8175 struct nx_ipfix_stats_reply *reply)
8176{
8177 reply->collector_set_id = htonl(ois->collector_set_id);
8178 reply->total_flows = htonll(ois->total_flows);
8179 reply->current_flows = htonll(ois->current_flows);
8180 reply->pkts = htonll(ois->pkts);
8181 reply->ipv4_pkts = htonll(ois->ipv4_pkts);
8182 reply->ipv6_pkts = htonll(ois->ipv6_pkts);
8183 reply->error_pkts = htonll(ois->error_pkts);
8184 reply->ipv4_error_pkts = htonll(ois->ipv4_error_pkts);
8185 reply->ipv6_error_pkts = htonll(ois->ipv6_error_pkts);
8186 reply->tx_pkts = htonll(ois->tx_pkts);
8187 reply->tx_errors = htonll(ois->tx_errors);
05e81316 8188 memset(reply->pad, 0, sizeof reply->pad);
fb8f22c1
BY
8189}
8190
8191/* Encode a ipfix stat for 'ois' and append it to 'replies'. */
8192void
8193ofputil_append_ipfix_stat(struct ovs_list *replies,
8194 const struct ofputil_ipfix_stats *ois)
8195{
8196 struct nx_ipfix_stats_reply *reply = ofpmp_append(replies, sizeof *reply);
8197 ofputil_ipfix_stats_to_reply(ois, reply);
8198}
8199
8200static enum ofperr
8201ofputil_ipfix_stats_from_nx(struct ofputil_ipfix_stats *is,
8202 const struct nx_ipfix_stats_reply *reply)
8203{
8204 is->collector_set_id = ntohl(reply->collector_set_id);
8205 is->total_flows = ntohll(reply->total_flows);
8206 is->current_flows = ntohll(reply->current_flows);
8207 is->pkts = ntohll(reply->pkts);
8208 is->ipv4_pkts = ntohll(reply->ipv4_pkts);
8209 is->ipv6_pkts = ntohll(reply->ipv6_pkts);
8210 is->error_pkts = ntohll(reply->error_pkts);
8211 is->ipv4_error_pkts = ntohll(reply->ipv4_error_pkts);
8212 is->ipv6_error_pkts = ntohll(reply->ipv6_error_pkts);
8213 is->tx_pkts = ntohll(reply->tx_pkts);
8214 is->tx_errors = ntohll(reply->tx_errors);
8215
8216 return 0;
8217}
8218
8219int
8220ofputil_pull_ipfix_stats(struct ofputil_ipfix_stats *is, struct ofpbuf *msg)
8221{
8222 enum ofperr error;
8223 enum ofpraw raw;
8224
8225 memset(is, 0xFF, sizeof (*is));
8226
8227 error = (msg->header ? ofpraw_decode(&raw, msg->header)
8228 : ofpraw_pull(&raw, msg));
8229 if (error) {
8230 return error;
8231 }
8232
8233 if (!msg->size) {
8234 return EOF;
8235 } else if (raw == OFPRAW_NXST_IPFIX_BRIDGE_REPLY ||
8236 raw == OFPRAW_NXST_IPFIX_FLOW_REPLY) {
8237 struct nx_ipfix_stats_reply *reply;
8238
8239 reply = ofpbuf_try_pull(msg, sizeof *reply);
8240 return ofputil_ipfix_stats_from_nx(is, reply);
8241 } else {
8242 OVS_NOT_REACHED();
8243 }
8244}
8245
8246
8247/* Returns the number of ipfix stats elements in
8248 * OFPTYPE_IPFIX_BRIDGE_STATS_REPLY or OFPTYPE_IPFIX_FLOW_STATS_REPLY
8249 * message 'oh'. */
8250size_t
8251ofputil_count_ipfix_stats(const struct ofp_header *oh)
8252{
8253 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
8254 ofpraw_pull_assert(&b);
8255
8256 return b.size / sizeof(struct ofputil_ipfix_stats);
8257}
8258
7395c052
NZ
8259/* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
8260void
ca6ba700 8261ofputil_bucket_list_destroy(struct ovs_list *buckets)
7395c052 8262{
5f03c983 8263 struct ofputil_bucket *bucket;
7395c052 8264
5f03c983 8265 LIST_FOR_EACH_POP (bucket, list_node, buckets) {
7395c052
NZ
8266 free(bucket->ofpacts);
8267 free(bucket);
8268 }
8269}
8270
103b4866
SH
8271/* Clones 'bucket' and its ofpacts data */
8272static struct ofputil_bucket *
8273ofputil_bucket_clone_data(const struct ofputil_bucket *bucket)
8274{
8275 struct ofputil_bucket *new;
8276
8277 new = xmemdup(bucket, sizeof *bucket);
8278 new->ofpacts = xmemdup(bucket->ofpacts, bucket->ofpacts_len);
8279
8280 return new;
8281}
8282
8283/* Clones each of the buckets in the list 'src' appending them
8284 * in turn to 'dest' which should be an initialised list.
8285 * An exception is that if the pointer value of a bucket in 'src'
8286 * matches 'skip' then it is not cloned or appended to 'dest'.
8287 * This allows all of 'src' or 'all of 'src' except 'skip' to
8288 * be cloned and appended to 'dest'. */
8289void
ca6ba700 8290ofputil_bucket_clone_list(struct ovs_list *dest, const struct ovs_list *src,
103b4866
SH
8291 const struct ofputil_bucket *skip)
8292{
8293 struct ofputil_bucket *bucket;
8294
8295 LIST_FOR_EACH (bucket, list_node, src) {
8296 struct ofputil_bucket *new_bucket;
8297
8298 if (bucket == skip) {
8299 continue;
8300 }
8301
8302 new_bucket = ofputil_bucket_clone_data(bucket);
417e7e66 8303 ovs_list_push_back(dest, &new_bucket->list_node);
103b4866
SH
8304 }
8305}
8306
8307/* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
8308 * Returns the first bucket found or NULL if no buckets are found. */
8309struct ofputil_bucket *
ca6ba700 8310ofputil_bucket_find(const struct ovs_list *buckets, uint32_t bucket_id)
103b4866
SH
8311{
8312 struct ofputil_bucket *bucket;
8313
8314 if (bucket_id > OFPG15_BUCKET_MAX) {
8315 return NULL;
8316 }
8317
8318 LIST_FOR_EACH (bucket, list_node, buckets) {
8319 if (bucket->bucket_id == bucket_id) {
8320 return bucket;
8321 }
8322 }
8323
8324 return NULL;
8325}
8326
8327/* Returns true if more than one bucket in the list 'buckets'
8328 * have the same bucket id. Returns false otherwise. */
18ac06d3 8329bool
ca6ba700 8330ofputil_bucket_check_duplicate_id(const struct ovs_list *buckets)
18ac06d3
SH
8331{
8332 struct ofputil_bucket *i, *j;
8333
8334 LIST_FOR_EACH (i, list_node, buckets) {
8335 LIST_FOR_EACH_REVERSE (j, list_node, buckets) {
8336 if (i == j) {
8337 break;
8338 }
8339 if (i->bucket_id == j->bucket_id) {
8340 return true;
8341 }
8342 }
8343 }
8344
8345 return false;
8346}
8347
103b4866
SH
8348/* Returns the bucket at the front of the list 'buckets'.
8349 * Undefined if 'buckets is empty. */
8350struct ofputil_bucket *
ca6ba700 8351ofputil_bucket_list_front(const struct ovs_list *buckets)
103b4866
SH
8352{
8353 static struct ofputil_bucket *bucket;
8354
417e7e66 8355 ASSIGN_CONTAINER(bucket, ovs_list_front(buckets), list_node);
103b4866
SH
8356
8357 return bucket;
8358}
8359
8360/* Returns the bucket at the back of the list 'buckets'.
8361 * Undefined if 'buckets is empty. */
8362struct ofputil_bucket *
ca6ba700 8363ofputil_bucket_list_back(const struct ovs_list *buckets)
103b4866
SH
8364{
8365 static struct ofputil_bucket *bucket;
8366
417e7e66 8367 ASSIGN_CONTAINER(bucket, ovs_list_back(buckets), list_node);
103b4866
SH
8368
8369 return bucket;
8370}
8371
7395c052
NZ
8372/* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
8373 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
8374 * for all groups.)
8375 *
8376 * Group statistics include packet and byte counts for each group. */
8377struct ofpbuf *
8378ofputil_encode_group_stats_request(enum ofp_version ofp_version,
8379 uint32_t group_id)
8380{
8381 struct ofpbuf *request;
8382
8383 switch (ofp_version) {
8384 case OFP10_VERSION:
8385 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
8386 "(\'-O OpenFlow11\')");
8387 case OFP11_VERSION:
8388 case OFP12_VERSION:
c37c0382 8389 case OFP13_VERSION:
42dccab5 8390 case OFP14_VERSION:
b79d45a1
BP
8391 case OFP15_VERSION:
8392 case OFP16_VERSION: {
7395c052
NZ
8393 struct ofp11_group_stats_request *req;
8394 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST, ofp_version, 0);
8395 req = ofpbuf_put_zeros(request, sizeof *req);
8396 req->group_id = htonl(group_id);
8397 break;
8398 }
8399 default:
428b2edd 8400 OVS_NOT_REACHED();
7395c052
NZ
8401 }
8402
8403 return request;
8404}
8405
bc65c25a
SH
8406void
8407ofputil_uninit_group_desc(struct ofputil_group_desc *gd)
8408{
8409 ofputil_bucket_list_destroy(&gd->buckets);
e8dba719 8410 ofputil_group_properties_destroy(&gd->props);
bc65c25a
SH
8411}
8412
19187a71
BP
8413/* Decodes the OpenFlow group description request in 'oh', returning the group
8414 * whose description is requested, or OFPG_ALL if stats for all groups was
8415 * requested. */
8416uint32_t
8417ofputil_decode_group_desc_request(const struct ofp_header *oh)
8418{
0a2869d5
BP
8419 struct ofpbuf request = ofpbuf_const_initializer(oh, ntohs(oh->length));
8420 enum ofpraw raw = ofpraw_pull_assert(&request);
19187a71
BP
8421 if (raw == OFPRAW_OFPST11_GROUP_DESC_REQUEST) {
8422 return OFPG_ALL;
8423 } else if (raw == OFPRAW_OFPST15_GROUP_DESC_REQUEST) {
8424 ovs_be32 *group_id = ofpbuf_pull(&request, sizeof *group_id);
8425 return ntohl(*group_id);
8426 } else {
8427 OVS_NOT_REACHED();
8428 }
8429}
8430
7395c052 8431/* Returns an OpenFlow group description request for OpenFlow version
19187a71
BP
8432 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
8433 * request stats for all groups (OpenFlow 1.4 and earlier always request all
8434 * groups).
7395c052
NZ
8435 *
8436 * Group descriptions include the bucket and action configuration for each
8437 * group. */
8438struct ofpbuf *
19187a71
BP
8439ofputil_encode_group_desc_request(enum ofp_version ofp_version,
8440 uint32_t group_id)
7395c052
NZ
8441{
8442 struct ofpbuf *request;
8443
8444 switch (ofp_version) {
8445 case OFP10_VERSION:
8446 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
8447 "(\'-O OpenFlow11\')");
8448 case OFP11_VERSION:
8449 case OFP12_VERSION:
c37c0382
AC
8450 case OFP13_VERSION:
8451 case OFP14_VERSION:
19187a71
BP
8452 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST,
8453 ofp_version, 0);
8454 break;
b79d45a1
BP
8455 case OFP15_VERSION:
8456 case OFP16_VERSION: {
d4d3f33e 8457 struct ofp15_group_desc_request *req;
19187a71
BP
8458 request = ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST,
8459 ofp_version, 0);
d4d3f33e
MT
8460 req = ofpbuf_put_zeros(request, sizeof *req);
8461 req->group_id = htonl(group_id);
7395c052 8462 break;
d4d3f33e 8463 }
7395c052 8464 default:
428b2edd 8465 OVS_NOT_REACHED();
7395c052
NZ
8466 }
8467
8468 return request;
8469}
8470
dcbe78ad 8471static void
63759e71
AZ
8472ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats *gs,
8473 struct ofp11_bucket_counter bucket_cnts[])
7395c052 8474{
7395c052
NZ
8475 int i;
8476
dcbe78ad
AZ
8477 for (i = 0; i < gs->n_buckets; i++) {
8478 bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count);
8479 bucket_cnts[i].byte_count = htonll(gs->bucket_stats[i].byte_count);
7395c052 8480 }
7395c052
NZ
8481}
8482
8483static void
dcbe78ad 8484ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs,
63759e71
AZ
8485 struct ofp11_group_stats *gs11, size_t length,
8486 struct ofp11_bucket_counter bucket_cnts[])
7395c052 8487{
63759e71
AZ
8488 memset(gs11, 0, sizeof *gs11);
8489 gs11->length = htons(length);
8490 gs11->group_id = htonl(gs->group_id);
8491 gs11->ref_count = htonl(gs->ref_count);
8492 gs11->packet_count = htonll(gs->packet_count);
8493 gs11->byte_count = htonll(gs->byte_count);
8494 ofputil_group_bucket_counters_to_ofp11(gs, bucket_cnts);
dcbe78ad 8495}
7395c052 8496
dcbe78ad
AZ
8497static void
8498ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs,
63759e71
AZ
8499 struct ofp13_group_stats *gs13, size_t length,
8500 struct ofp11_bucket_counter bucket_cnts[])
dcbe78ad 8501{
63759e71 8502 ofputil_group_stats_to_ofp11(gs, &gs13->gs, length, bucket_cnts);
dcbe78ad
AZ
8503 gs13->duration_sec = htonl(gs->duration_sec);
8504 gs13->duration_nsec = htonl(gs->duration_nsec);
63759e71 8505
7395c052
NZ
8506}
8507
dcbe78ad 8508/* Encodes 'gs' properly for the format of the list of group statistics
7395c052
NZ
8509 * replies already begun in 'replies' and appends it to the list. 'replies'
8510 * must have originally been initialized with ofpmp_init(). */
8511void
ca6ba700 8512ofputil_append_group_stats(struct ovs_list *replies,
dcbe78ad 8513 const struct ofputil_group_stats *gs)
7395c052 8514{
63759e71
AZ
8515 size_t bucket_counter_size;
8516 struct ofp11_bucket_counter *bucket_counters;
dcbe78ad 8517 size_t length;
7395c052 8518
63759e71
AZ
8519 bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter);
8520
e28ac5cf 8521 switch (ofpmp_version(replies)) {
7395c052 8522 case OFP11_VERSION:
dcbe78ad 8523 case OFP12_VERSION:{
63759e71 8524 struct ofp11_group_stats *gs11;
dcbe78ad 8525
63759e71
AZ
8526 length = sizeof *gs11 + bucket_counter_size;
8527 gs11 = ofpmp_append(replies, length);
8528 bucket_counters = (struct ofp11_bucket_counter *)(gs11 + 1);
8529 ofputil_group_stats_to_ofp11(gs, gs11, length, bucket_counters);
dcbe78ad
AZ
8530 break;
8531 }
7395c052
NZ
8532
8533 case OFP13_VERSION:
42dccab5 8534 case OFP14_VERSION:
b79d45a1
BP
8535 case OFP15_VERSION:
8536 case OFP16_VERSION: {
63759e71 8537 struct ofp13_group_stats *gs13;
7395c052 8538
63759e71
AZ
8539 length = sizeof *gs13 + bucket_counter_size;
8540 gs13 = ofpmp_append(replies, length);
8541 bucket_counters = (struct ofp11_bucket_counter *)(gs13 + 1);
8542 ofputil_group_stats_to_ofp13(gs, gs13, length, bucket_counters);
dcbe78ad
AZ
8543 break;
8544 }
c37c0382 8545
7395c052
NZ
8546 case OFP10_VERSION:
8547 default:
428b2edd 8548 OVS_NOT_REACHED();
7395c052
NZ
8549 }
8550}
7395c052
NZ
8551/* Returns an OpenFlow group features request for OpenFlow version
8552 * 'ofp_version'. */
8553struct ofpbuf *
8554ofputil_encode_group_features_request(enum ofp_version ofp_version)
8555{
8556 struct ofpbuf *request = NULL;
8557
8558 switch (ofp_version) {
8559 case OFP10_VERSION:
8560 case OFP11_VERSION:
8561 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
8562 "(\'-O OpenFlow12\')");
8563 case OFP12_VERSION:
c37c0382
AC
8564 case OFP13_VERSION:
8565 case OFP14_VERSION:
42dccab5 8566 case OFP15_VERSION:
b79d45a1 8567 case OFP16_VERSION:
7395c052 8568 request = ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST,
c37c0382 8569 ofp_version, 0);
7395c052 8570 break;
7395c052 8571 default:
428b2edd 8572 OVS_NOT_REACHED();
7395c052
NZ
8573 }
8574
8575 return request;
8576}
8577
8578/* Returns a OpenFlow message that encodes 'features' properly as a reply to
8579 * group features request 'request'. */
8580struct ofpbuf *
8581ofputil_encode_group_features_reply(
8582 const struct ofputil_group_features *features,
8583 const struct ofp_header *request)
8584{
8585 struct ofp12_group_features_stats *ogf;
8586 struct ofpbuf *reply;
08d1e234 8587 int i;
7395c052
NZ
8588
8589 reply = ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY,
8590 request->version, request->xid, 0);
8591 ogf = ofpbuf_put_zeros(reply, sizeof *ogf);
8592 ogf->types = htonl(features->types);
8593 ogf->capabilities = htonl(features->capabilities);
08d1e234
BP
8594 for (i = 0; i < OFPGT12_N_TYPES; i++) {
8595 ogf->max_groups[i] = htonl(features->max_groups[i]);
8596 ogf->actions[i] = ofpact_bitmap_to_openflow(features->ofpacts[i],
8597 request->version);
8598 }
7395c052
NZ
8599
8600 return reply;
8601}
8602
8603/* Decodes group features reply 'oh' into 'features'. */
8604void
8605ofputil_decode_group_features_reply(const struct ofp_header *oh,
8606 struct ofputil_group_features *features)
8607{
8608 const struct ofp12_group_features_stats *ogf = ofpmsg_body(oh);
08d1e234 8609 int i;
7395c052
NZ
8610
8611 features->types = ntohl(ogf->types);
8612 features->capabilities = ntohl(ogf->capabilities);
08d1e234
BP
8613 for (i = 0; i < OFPGT12_N_TYPES; i++) {
8614 features->max_groups[i] = ntohl(ogf->max_groups[i]);
8615 features->ofpacts[i] = ofpact_bitmap_from_openflow(
8616 ogf->actions[i], oh->version);
8617 }
7395c052
NZ
8618}
8619
8620/* Parse a group status request message into a 32 bit OpenFlow 1.1
8621 * group ID and stores the latter in '*group_id'.
8622 * Returns 0 if successful, otherwise an OFPERR_* number. */
8623enum ofperr
8624ofputil_decode_group_stats_request(const struct ofp_header *request,
8625 uint32_t *group_id)
8626{
8627 const struct ofp11_group_stats_request *gsr11 = ofpmsg_body(request);
8628 *group_id = ntohl(gsr11->group_id);
8629 return 0;
8630}
8631
8632/* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
646b2a9c
SH
8633 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
8634 * caller to eventually free.
7395c052
NZ
8635 *
8636 * Multiple group stats replies can be packed into a single OpenFlow message.
8637 * Calling this function multiple times for a single 'msg' iterates through the
8638 * replies. The caller must initially leave 'msg''s layer pointers null and
8639 * not modify them between calls.
8640 *
8641 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8642 * otherwise a positive errno value. */
8643int
8644ofputil_decode_group_stats_reply(struct ofpbuf *msg,
8645 struct ofputil_group_stats *gs)
8646{
8647 struct ofp11_bucket_counter *obc;
8648 struct ofp11_group_stats *ogs11;
8649 enum ofpraw raw;
8650 enum ofperr error;
8651 size_t base_len;
8652 size_t length;
8653 size_t i;
8654
646b2a9c 8655 gs->bucket_stats = NULL;
6fd6ed71 8656 error = (msg->header ? ofpraw_decode(&raw, msg->header)
7395c052
NZ
8657 : ofpraw_pull(&raw, msg));
8658 if (error) {
8659 return error;
8660 }
8661
6fd6ed71 8662 if (!msg->size) {
7395c052
NZ
8663 return EOF;
8664 }
8665
8666 if (raw == OFPRAW_OFPST11_GROUP_REPLY) {
8667 base_len = sizeof *ogs11;
8668 ogs11 = ofpbuf_try_pull(msg, sizeof *ogs11);
8669 gs->duration_sec = gs->duration_nsec = UINT32_MAX;
8670 } else if (raw == OFPRAW_OFPST13_GROUP_REPLY) {
8671 struct ofp13_group_stats *ogs13;
8672
8673 base_len = sizeof *ogs13;
8674 ogs13 = ofpbuf_try_pull(msg, sizeof *ogs13);
8675 if (ogs13) {
8676 ogs11 = &ogs13->gs;
8677 gs->duration_sec = ntohl(ogs13->duration_sec);
8678 gs->duration_nsec = ntohl(ogs13->duration_nsec);
8679 } else {
8680 ogs11 = NULL;
8681 }
8682 } else {
428b2edd 8683 OVS_NOT_REACHED();
7395c052
NZ
8684 }
8685
8686 if (!ogs11) {
437d0d22 8687 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
6fd6ed71 8688 ofpraw_get_name(raw), msg->size);
7395c052
NZ
8689 return OFPERR_OFPBRC_BAD_LEN;
8690 }
8691 length = ntohs(ogs11->length);
8692 if (length < sizeof base_len) {
34582733 8693 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply claims invalid length %"PRIuSIZE,
7395c052
NZ
8694 ofpraw_get_name(raw), length);
8695 return OFPERR_OFPBRC_BAD_LEN;
8696 }
8697
8698 gs->group_id = ntohl(ogs11->group_id);
8699 gs->ref_count = ntohl(ogs11->ref_count);
8700 gs->packet_count = ntohll(ogs11->packet_count);
8701 gs->byte_count = ntohll(ogs11->byte_count);
8702
8703 gs->n_buckets = (length - base_len) / sizeof *obc;
8704 obc = ofpbuf_try_pull(msg, gs->n_buckets * sizeof *obc);
8705 if (!obc) {
437d0d22 8706 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
6fd6ed71 8707 ofpraw_get_name(raw), msg->size);
7395c052
NZ
8708 return OFPERR_OFPBRC_BAD_LEN;
8709 }
8710
646b2a9c 8711 gs->bucket_stats = xmalloc(gs->n_buckets * sizeof *gs->bucket_stats);
7395c052
NZ
8712 for (i = 0; i < gs->n_buckets; i++) {
8713 gs->bucket_stats[i].packet_count = ntohll(obc[i].packet_count);
8714 gs->bucket_stats[i].byte_count = ntohll(obc[i].byte_count);
8715 }
8716
8717 return 0;
8718}
8719
5097fee5
SH
8720static void
8721ofputil_put_ofp11_bucket(const struct ofputil_bucket *bucket,
8722 struct ofpbuf *openflow, enum ofp_version ofp_version)
8723{
8724 struct ofp11_bucket *ob;
8725 size_t start;
8726
6fd6ed71 8727 start = openflow->size;
5097fee5
SH
8728 ofpbuf_put_zeros(openflow, sizeof *ob);
8729 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
8730 openflow, ofp_version);
8731 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
6fd6ed71 8732 ob->len = htons(openflow->size - start);
5097fee5
SH
8733 ob->weight = htons(bucket->weight);
8734 ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port);
8735 ob->watch_group = htonl(bucket->watch_group);
8736}
8737
18ac06d3
SH
8738static void
8739ofputil_put_ofp15_bucket(const struct ofputil_bucket *bucket,
8740 uint32_t bucket_id, enum ofp11_group_type group_type,
8741 struct ofpbuf *openflow, enum ofp_version ofp_version)
8742{
8743 struct ofp15_bucket *ob;
8744 size_t start, actions_start, actions_len;
8745
6fd6ed71 8746 start = openflow->size;
18ac06d3
SH
8747 ofpbuf_put_zeros(openflow, sizeof *ob);
8748
6fd6ed71 8749 actions_start = openflow->size;
18ac06d3
SH
8750 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
8751 openflow, ofp_version);
6fd6ed71 8752 actions_len = openflow->size - actions_start;
18ac06d3
SH
8753
8754 if (group_type == OFPGT11_SELECT) {
b611d3ac 8755 ofpprop_put_u16(openflow, OFPGBPT15_WEIGHT, bucket->weight);
18ac06d3
SH
8756 }
8757 if (bucket->watch_port != OFPP_ANY) {
b611d3ac
BP
8758 ofpprop_put_be32(openflow, OFPGBPT15_WATCH_PORT,
8759 ofputil_port_to_ofp11(bucket->watch_port));
18ac06d3
SH
8760 }
8761 if (bucket->watch_group != OFPG_ANY) {
b611d3ac 8762 ofpprop_put_u32(openflow, OFPGBPT15_WATCH_GROUP, bucket->watch_group);
18ac06d3
SH
8763 }
8764
8765 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
6fd6ed71 8766 ob->len = htons(openflow->size - start);
79870963 8767 ob->action_array_len = htons(actions_len);
18ac06d3
SH
8768 ob->bucket_id = htonl(bucket_id);
8769}
8770
53eb84a5
SH
8771static void
8772ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version,
8773 const struct ofputil_group_props *gp,
8774 struct ofpbuf *openflow)
8775{
8776 struct ntr_group_prop_selection_method *prop;
8777 size_t start;
8778
8779 start = openflow->size;
8780 ofpbuf_put_zeros(openflow, sizeof *prop);
8781 oxm_put_field_array(openflow, &gp->fields, ofp_version);
8782 prop = ofpbuf_at_assert(openflow, start, sizeof *prop);
8783 prop->type = htons(OFPGPT15_EXPERIMENTER);
8784 prop->experimenter = htonl(NTR_VENDOR_ID);
8785 prop->exp_type = htonl(NTRT_SELECTION_METHOD);
8786 strcpy(prop->selection_method, gp->selection_method);
8787 prop->selection_method_param = htonll(gp->selection_method_param);
c5562271 8788 ofpprop_end(openflow, start);
53eb84a5
SH
8789}
8790
5be50252
SH
8791static void
8792ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc *gds,
1667bb34 8793 const struct ovs_list *buckets,
ca6ba700 8794 struct ovs_list *replies,
5be50252 8795 enum ofp_version version)
7395c052 8796{
417e7e66 8797 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
7395c052
NZ
8798 struct ofp11_group_desc_stats *ogds;
8799 struct ofputil_bucket *bucket;
8800 size_t start_ogds;
8801
6fd6ed71 8802 start_ogds = reply->size;
7395c052
NZ
8803 ofpbuf_put_zeros(reply, sizeof *ogds);
8804 LIST_FOR_EACH (bucket, list_node, buckets) {
5097fee5 8805 ofputil_put_ofp11_bucket(bucket, reply, version);
7395c052
NZ
8806 }
8807 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
6fd6ed71 8808 ogds->length = htons(reply->size - start_ogds);
7395c052
NZ
8809 ogds->type = gds->type;
8810 ogds->group_id = htonl(gds->group_id);
8811
8812 ofpmp_postappend(replies, start_ogds);
8813}
8814
18ac06d3
SH
8815static void
8816ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc *gds,
1667bb34 8817 const struct ovs_list *buckets,
ca6ba700 8818 struct ovs_list *replies,
18ac06d3
SH
8819 enum ofp_version version)
8820{
417e7e66 8821 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
18ac06d3
SH
8822 struct ofp15_group_desc_stats *ogds;
8823 struct ofputil_bucket *bucket;
8824 size_t start_ogds, start_buckets;
8825
6fd6ed71 8826 start_ogds = reply->size;
18ac06d3 8827 ofpbuf_put_zeros(reply, sizeof *ogds);
6fd6ed71 8828 start_buckets = reply->size;
18ac06d3
SH
8829 LIST_FOR_EACH (bucket, list_node, buckets) {
8830 ofputil_put_ofp15_bucket(bucket, bucket->bucket_id,
8831 gds->type, reply, version);
8832 }
8833 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
18ac06d3
SH
8834 ogds->type = gds->type;
8835 ogds->group_id = htonl(gds->group_id);
6fd6ed71 8836 ogds->bucket_list_len = htons(reply->size - start_buckets);
18ac06d3 8837
53eb84a5
SH
8838 /* Add group properties */
8839 if (gds->props.selection_method[0]) {
8840 ofputil_put_group_prop_ntr_selection_method(version, &gds->props,
8841 reply);
8842 }
ef5774e3 8843 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
3986cae6 8844 ogds->length = htons(reply->size - start_ogds);
53eb84a5 8845
18ac06d3
SH
8846 ofpmp_postappend(replies, start_ogds);
8847}
8848
5be50252
SH
8849/* Appends a group stats reply that contains the data in 'gds' to those already
8850 * present in the list of ofpbufs in 'replies'. 'replies' should have been
8851 * initialized with ofpmp_init(). */
8852void
8853ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
1667bb34 8854 const struct ovs_list *buckets,
ca6ba700 8855 struct ovs_list *replies)
5be50252
SH
8856{
8857 enum ofp_version version = ofpmp_version(replies);
8858
8859 switch (version)
8860 {
8861 case OFP11_VERSION:
8862 case OFP12_VERSION:
8863 case OFP13_VERSION:
8864 case OFP14_VERSION:
5be50252
SH
8865 ofputil_append_ofp11_group_desc_reply(gds, buckets, replies, version);
8866 break;
8867
18ac06d3 8868 case OFP15_VERSION:
b79d45a1 8869 case OFP16_VERSION:
18ac06d3
SH
8870 ofputil_append_ofp15_group_desc_reply(gds, buckets, replies, version);
8871 break;
8872
5be50252
SH
8873 case OFP10_VERSION:
8874 default:
8875 OVS_NOT_REACHED();
8876 }
8877}
8878
7395c052 8879static enum ofperr
655ed3ae 8880ofputil_pull_ofp11_buckets(struct ofpbuf *msg, size_t buckets_length,
ca6ba700 8881 enum ofp_version version, struct ovs_list *buckets)
7395c052
NZ
8882{
8883 struct ofp11_bucket *ob;
18ac06d3 8884 uint32_t bucket_id = 0;
7395c052 8885
417e7e66 8886 ovs_list_init(buckets);
7395c052
NZ
8887 while (buckets_length > 0) {
8888 struct ofputil_bucket *bucket;
8889 struct ofpbuf ofpacts;
8890 enum ofperr error;
8891 size_t ob_len;
8892
8893 ob = (buckets_length >= sizeof *ob
8894 ? ofpbuf_try_pull(msg, sizeof *ob)
8895 : NULL);
8896 if (!ob) {
34582733 8897 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE" leftover bytes",
7395c052 8898 buckets_length);
e27e4ce9 8899 return OFPERR_OFPGMFC_BAD_BUCKET;
7395c052
NZ
8900 }
8901
8902 ob_len = ntohs(ob->len);
8903 if (ob_len < sizeof *ob) {
8904 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
34582733 8905 "%"PRIuSIZE" is not valid", ob_len);
7395c052
NZ
8906 return OFPERR_OFPGMFC_BAD_BUCKET;
8907 } else if (ob_len > buckets_length) {
8908 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
34582733 8909 "%"PRIuSIZE" exceeds remaining buckets data size %"PRIuSIZE,
7395c052
NZ
8910 ob_len, buckets_length);
8911 return OFPERR_OFPGMFC_BAD_BUCKET;
8912 }
8913 buckets_length -= ob_len;
8914
8915 ofpbuf_init(&ofpacts, 0);
e3f8f887 8916 error = ofpacts_pull_openflow_actions(msg, ob_len - sizeof *ob,
5c7c16d8 8917 version, NULL, NULL, &ofpacts);
7395c052
NZ
8918 if (error) {
8919 ofpbuf_uninit(&ofpacts);
8920 ofputil_bucket_list_destroy(buckets);
8921 return error;
8922 }
8923
8924 bucket = xzalloc(sizeof *bucket);
8925 bucket->weight = ntohs(ob->weight);
8926 error = ofputil_port_from_ofp11(ob->watch_port, &bucket->watch_port);
8927 if (error) {
8928 ofpbuf_uninit(&ofpacts);
8929 ofputil_bucket_list_destroy(buckets);
8930 return OFPERR_OFPGMFC_BAD_WATCH;
8931 }
8932 bucket->watch_group = ntohl(ob->watch_group);
18ac06d3
SH
8933 bucket->bucket_id = bucket_id++;
8934
7395c052 8935 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
6fd6ed71 8936 bucket->ofpacts_len = ofpacts.size;
417e7e66 8937 ovs_list_push_back(buckets, &bucket->list_node);
7395c052
NZ
8938 }
8939
8940 return 0;
8941}
8942
18ac06d3
SH
8943static enum ofperr
8944ofputil_pull_ofp15_buckets(struct ofpbuf *msg, size_t buckets_length,
64e8c446
BP
8945 enum ofp_version version, uint8_t group_type,
8946 struct ovs_list *buckets)
18ac06d3
SH
8947{
8948 struct ofp15_bucket *ob;
8949
417e7e66 8950 ovs_list_init(buckets);
18ac06d3
SH
8951 while (buckets_length > 0) {
8952 struct ofputil_bucket *bucket = NULL;
8953 struct ofpbuf ofpacts;
8954 enum ofperr err = OFPERR_OFPGMFC_BAD_BUCKET;
18ac06d3
SH
8955 size_t ob_len, actions_len, properties_len;
8956 ovs_be32 watch_port = ofputil_port_to_ofp11(OFPP_ANY);
8957 ovs_be32 watch_group = htonl(OFPG_ANY);
64e8c446 8958 ovs_be16 weight = htons(group_type == OFPGT11_SELECT ? 1 : 0);
18ac06d3
SH
8959
8960 ofpbuf_init(&ofpacts, 0);
8961
8962 ob = ofpbuf_try_pull(msg, sizeof *ob);
8963 if (!ob) {
8964 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE
8965 " leftover bytes", buckets_length);
8966 goto err;
8967 }
8968
8969 ob_len = ntohs(ob->len);
79870963 8970 actions_len = ntohs(ob->action_array_len);
18ac06d3
SH
8971
8972 if (ob_len < sizeof *ob) {
8973 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8974 "%"PRIuSIZE" is not valid", ob_len);
8975 goto err;
8976 } else if (ob_len > buckets_length) {
8977 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8978 "%"PRIuSIZE" exceeds remaining buckets data size %"
8979 PRIuSIZE, ob_len, buckets_length);
8980 goto err;
8981 } else if (actions_len > ob_len - sizeof *ob) {
8982 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket actions "
8983 "length %"PRIuSIZE" exceeds remaining bucket "
8984 "data size %"PRIuSIZE, actions_len,
8985 ob_len - sizeof *ob);
8986 goto err;
8987 }
8988 buckets_length -= ob_len;
8989
8990 err = ofpacts_pull_openflow_actions(msg, actions_len, version,
5c7c16d8 8991 NULL, NULL, &ofpacts);
18ac06d3
SH
8992 if (err) {
8993 goto err;
8994 }
8995
8996 properties_len = ob_len - sizeof *ob - actions_len;
0a2869d5
BP
8997 struct ofpbuf properties = ofpbuf_const_initializer(
8998 ofpbuf_pull(msg, properties_len), properties_len);
6fd6ed71 8999 while (properties.size > 0) {
18ac06d3 9000 struct ofpbuf payload;
34a543e3 9001 uint64_t type;
18ac06d3 9002
c5562271 9003 err = ofpprop_pull(&properties, &payload, &type);
18ac06d3
SH
9004 if (err) {
9005 goto err;
9006 }
9007
9008 switch (type) {
9009 case OFPGBPT15_WEIGHT:
b611d3ac 9010 err = ofpprop_parse_be16(&payload, &weight);
18ac06d3
SH
9011 break;
9012
9013 case OFPGBPT15_WATCH_PORT:
b611d3ac 9014 err = ofpprop_parse_be32(&payload, &watch_port);
18ac06d3
SH
9015 break;
9016
9017 case OFPGBPT15_WATCH_GROUP:
b611d3ac 9018 err = ofpprop_parse_be32(&payload, &watch_group);
18ac06d3
SH
9019 break;
9020
9021 default:
34a543e3 9022 err = OFPPROP_UNKNOWN(false, "group bucket", type);
18ac06d3
SH
9023 break;
9024 }
9025
9026 if (err) {
9027 goto err;
9028 }
9029 }
9030
9031 bucket = xzalloc(sizeof *bucket);
9032
9033 bucket->weight = ntohs(weight);
9034 err = ofputil_port_from_ofp11(watch_port, &bucket->watch_port);
9035 if (err) {
9036 err = OFPERR_OFPGMFC_BAD_WATCH;
9037 goto err;
9038 }
9039 bucket->watch_group = ntohl(watch_group);
9040 bucket->bucket_id = ntohl(ob->bucket_id);
9041 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
9042 VLOG_WARN_RL(&bad_ofmsg_rl, "bucket id (%u) is out of range",
9043 bucket->bucket_id);
9044 err = OFPERR_OFPGMFC_BAD_BUCKET;
9045 goto err;
9046 }
9047
9048 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
6fd6ed71 9049 bucket->ofpacts_len = ofpacts.size;
417e7e66 9050 ovs_list_push_back(buckets, &bucket->list_node);
18ac06d3
SH
9051
9052 continue;
9053
9054 err:
9055 free(bucket);
9056 ofpbuf_uninit(&ofpacts);
9057 ofputil_bucket_list_destroy(buckets);
9058 return err;
9059 }
9060
9061 if (ofputil_bucket_check_duplicate_id(buckets)) {
9062 VLOG_WARN_RL(&bad_ofmsg_rl, "Duplicate bucket id");
9063 ofputil_bucket_list_destroy(buckets);
9064 return OFPERR_OFPGMFC_BAD_BUCKET;
9065 }
9066
9067 return 0;
9068}
9069
bc65c25a
SH
9070static void
9071ofputil_init_group_properties(struct ofputil_group_props *gp)
9072{
9073 memset(gp, 0, sizeof *gp);
9074}
9075
e8dba719
JR
9076void
9077ofputil_group_properties_copy(struct ofputil_group_props *to,
9078 const struct ofputil_group_props *from)
9079{
9080 *to = *from;
9081 to->fields.values = xmemdup(from->fields.values, from->fields.values_size);
9082}
9083
9084void
9085ofputil_group_properties_destroy(struct ofputil_group_props *gp)
9086{
9087 free(gp->fields.values);
9088}
9089
bc65c25a
SH
9090static enum ofperr
9091parse_group_prop_ntr_selection_method(struct ofpbuf *payload,
9092 enum ofp11_group_type group_type,
9093 enum ofp15_group_mod_command group_cmd,
9094 struct ofputil_group_props *gp)
9095{
9096 struct ntr_group_prop_selection_method *prop = payload->data;
9097 size_t fields_len, method_len;
9098 enum ofperr error;
9099
9100 switch (group_type) {
9101 case OFPGT11_SELECT:
9102 break;
9103 case OFPGT11_ALL:
9104 case OFPGT11_INDIRECT:
9105 case OFPGT11_FF:
c5562271
BP
9106 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property is "
9107 "only allowed for select groups");
bc65c25a
SH
9108 return OFPERR_OFPBPC_BAD_VALUE;
9109 default:
9110 OVS_NOT_REACHED();
9111 }
9112
9113 switch (group_cmd) {
9114 case OFPGC15_ADD:
9115 case OFPGC15_MODIFY:
88b87a36 9116 case OFPGC15_ADD_OR_MOD:
bc65c25a
SH
9117 break;
9118 case OFPGC15_DELETE:
9119 case OFPGC15_INSERT_BUCKET:
9120 case OFPGC15_REMOVE_BUCKET:
c5562271
BP
9121 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property is "
9122 "only allowed for add and delete group modifications");
bc65c25a
SH
9123 return OFPERR_OFPBPC_BAD_VALUE;
9124 default:
9125 OVS_NOT_REACHED();
9126 }
9127
9128 if (payload->size < sizeof *prop) {
c5562271
BP
9129 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property "
9130 "length %u is not valid", payload->size);
bc65c25a
SH
9131 return OFPERR_OFPBPC_BAD_LEN;
9132 }
9133
9134 method_len = strnlen(prop->selection_method, NTR_MAX_SELECTION_METHOD_LEN);
9135
9136 if (method_len == NTR_MAX_SELECTION_METHOD_LEN) {
c5562271
BP
9137 OFPPROP_LOG(&bad_ofmsg_rl, false,
9138 "ntr selection method is not null terminated");
bc65c25a
SH
9139 return OFPERR_OFPBPC_BAD_VALUE;
9140 }
9141
53cc166a
JR
9142 if (strcmp("hash", prop->selection_method)
9143 && strcmp("dp_hash", prop->selection_method)) {
c5562271
BP
9144 OFPPROP_LOG(&bad_ofmsg_rl, false,
9145 "ntr selection method '%s' is not supported",
9146 prop->selection_method);
0c4b9393
SH
9147 return OFPERR_OFPBPC_BAD_VALUE;
9148 }
53cc166a 9149 /* 'method_len' is now non-zero. */
bc65c25a
SH
9150
9151 strcpy(gp->selection_method, prop->selection_method);
9152 gp->selection_method_param = ntohll(prop->selection_method_param);
9153
bc65c25a
SH
9154 ofpbuf_pull(payload, sizeof *prop);
9155
9156 fields_len = ntohs(prop->length) - sizeof *prop;
53cc166a
JR
9157 if (fields_len && strcmp("hash", gp->selection_method)) {
9158 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method %s "
9159 "does not support fields", gp->selection_method);
bc65c25a
SH
9160 return OFPERR_OFPBPC_BAD_VALUE;
9161 }
9162
9163 error = oxm_pull_field_array(payload->data, fields_len,
9164 &gp->fields);
9165 if (error) {
c5562271
BP
9166 OFPPROP_LOG(&bad_ofmsg_rl, false,
9167 "ntr selection method fields are invalid");
bc65c25a
SH
9168 return error;
9169 }
9170
9171 return 0;
9172}
9173
bc65c25a
SH
9174static enum ofperr
9175parse_ofp15_group_properties(struct ofpbuf *msg,
9176 enum ofp11_group_type group_type,
9177 enum ofp15_group_mod_command group_cmd,
9178 struct ofputil_group_props *gp,
9179 size_t properties_len)
9180{
0a2869d5
BP
9181 struct ofpbuf properties = ofpbuf_const_initializer(
9182 ofpbuf_pull(msg, properties_len), properties_len);
bc65c25a
SH
9183 while (properties.size > 0) {
9184 struct ofpbuf payload;
9185 enum ofperr error;
34a543e3 9186 uint64_t type;
bc65c25a 9187
c5562271 9188 error = ofpprop_pull(&properties, &payload, &type);
bc65c25a
SH
9189 if (error) {
9190 return error;
9191 }
9192
9193 switch (type) {
34a543e3
BP
9194 case OFPPROP_EXP(NTR_VENDOR_ID, NTRT_SELECTION_METHOD):
9195 case OFPPROP_EXP(NTR_COMPAT_VENDOR_ID, NTRT_SELECTION_METHOD):
9196 error = parse_group_prop_ntr_selection_method(&payload, group_type,
9197 group_cmd, gp);
bc65c25a
SH
9198 break;
9199
9200 default:
34a543e3 9201 error = OFPPROP_UNKNOWN(false, "group", type);
bc65c25a
SH
9202 break;
9203 }
9204
9205 if (error) {
9206 return error;
9207 }
9208 }
9209
9210 return 0;
9211}
9212
975910f2
SH
9213static int
9214ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc *gd,
9215 struct ofpbuf *msg,
9216 enum ofp_version version)
7395c052
NZ
9217{
9218 struct ofp11_group_desc_stats *ogds;
9219 size_t length;
9220
6fd6ed71 9221 if (!msg->header) {
7395c052
NZ
9222 ofpraw_pull_assert(msg);
9223 }
9224
6fd6ed71 9225 if (!msg->size) {
7395c052
NZ
9226 return EOF;
9227 }
9228
9229 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
9230 if (!ogds) {
437d0d22 9231 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
6fd6ed71 9232 "leftover bytes at end", msg->size);
7395c052
NZ
9233 return OFPERR_OFPBRC_BAD_LEN;
9234 }
9235 gd->type = ogds->type;
9236 gd->group_id = ntohl(ogds->group_id);
9237
9238 length = ntohs(ogds->length);
6fd6ed71 9239 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
7395c052 9240 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
34582733 9241 "length %"PRIuSIZE, length);
7395c052
NZ
9242 return OFPERR_OFPBRC_BAD_LEN;
9243 }
9244
655ed3ae
SH
9245 return ofputil_pull_ofp11_buckets(msg, length - sizeof *ogds, version,
9246 &gd->buckets);
7395c052
NZ
9247}
9248
18ac06d3
SH
9249static int
9250ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc *gd,
9251 struct ofpbuf *msg,
9252 enum ofp_version version)
9253{
9254 struct ofp15_group_desc_stats *ogds;
9255 uint16_t length, bucket_list_len;
bc65c25a 9256 int error;
18ac06d3 9257
6fd6ed71 9258 if (!msg->header) {
18ac06d3
SH
9259 ofpraw_pull_assert(msg);
9260 }
9261
6fd6ed71 9262 if (!msg->size) {
18ac06d3
SH
9263 return EOF;
9264 }
9265
9266 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
9267 if (!ogds) {
9268 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
6fd6ed71 9269 "leftover bytes at end", msg->size);
18ac06d3
SH
9270 return OFPERR_OFPBRC_BAD_LEN;
9271 }
9272 gd->type = ogds->type;
9273 gd->group_id = ntohl(ogds->group_id);
9274
9275 length = ntohs(ogds->length);
6fd6ed71 9276 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
18ac06d3
SH
9277 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
9278 "length %u", length);
9279 return OFPERR_OFPBRC_BAD_LEN;
9280 }
9281
9282 bucket_list_len = ntohs(ogds->bucket_list_len);
9283 if (length < bucket_list_len + sizeof *ogds) {
9284 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
9285 "bucket list length %u", bucket_list_len);
9286 return OFPERR_OFPBRC_BAD_LEN;
9287 }
64e8c446 9288 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, version, gd->type,
bc65c25a
SH
9289 &gd->buckets);
9290 if (error) {
9291 return error;
9292 }
18ac06d3 9293
bc65c25a
SH
9294 /* By definition group desc messages don't have a group mod command.
9295 * However, parse_group_prop_ntr_selection_method() checks to make sure
9296 * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
9297 * against group mod messages with other commands supplying
9298 * a NTR selection method group experimenter property.
9299 * Such properties are valid for group desc replies so
9300 * claim that the group mod command is OFPGC15_ADD to
9301 * satisfy the check in parse_group_prop_ntr_selection_method() */
9302 return parse_ofp15_group_properties(msg, gd->type, OFPGC15_ADD, &gd->props,
3986cae6 9303 length - sizeof *ogds - bucket_list_len);
18ac06d3
SH
9304}
9305
975910f2
SH
9306/* Converts a group description reply in 'msg' into an abstract
9307 * ofputil_group_desc in 'gd'.
9308 *
9309 * Multiple group description replies can be packed into a single OpenFlow
9310 * message. Calling this function multiple times for a single 'msg' iterates
9311 * through the replies. The caller must initially leave 'msg''s layer pointers
9312 * null and not modify them between calls.
9313 *
9314 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9315 * otherwise a positive errno value. */
9316int
9317ofputil_decode_group_desc_reply(struct ofputil_group_desc *gd,
9318 struct ofpbuf *msg, enum ofp_version version)
9319{
bc65c25a
SH
9320 ofputil_init_group_properties(&gd->props);
9321
975910f2
SH
9322 switch (version)
9323 {
9324 case OFP11_VERSION:
9325 case OFP12_VERSION:
9326 case OFP13_VERSION:
9327 case OFP14_VERSION:
975910f2
SH
9328 return ofputil_decode_ofp11_group_desc_reply(gd, msg, version);
9329
18ac06d3 9330 case OFP15_VERSION:
b79d45a1 9331 case OFP16_VERSION:
18ac06d3
SH
9332 return ofputil_decode_ofp15_group_desc_reply(gd, msg, version);
9333
975910f2
SH
9334 case OFP10_VERSION:
9335 default:
9336 OVS_NOT_REACHED();
9337 }
9338}
9339
bc65c25a
SH
9340void
9341ofputil_uninit_group_mod(struct ofputil_group_mod *gm)
9342{
9343 ofputil_bucket_list_destroy(&gm->buckets);
e8dba719 9344 ofputil_group_properties_destroy(&gm->props);
bc65c25a
SH
9345}
9346
4498bea5
SH
9347static struct ofpbuf *
9348ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version,
9349 const struct ofputil_group_mod *gm)
7395c052
NZ
9350{
9351 struct ofpbuf *b;
9352 struct ofp11_group_mod *ogm;
9353 size_t start_ogm;
7395c052 9354 struct ofputil_bucket *bucket;
7395c052 9355
4498bea5 9356 b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0);
6fd6ed71 9357 start_ogm = b->size;
4498bea5
SH
9358 ofpbuf_put_zeros(b, sizeof *ogm);
9359
9360 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
9361 ofputil_put_ofp11_bucket(bucket, b, ofp_version);
9362 }
9363 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
9364 ogm->command = htons(gm->command);
9365 ogm->type = gm->type;
9366 ogm->group_id = htonl(gm->group_id);
9367
9368 return b;
9369}
9370
18ac06d3
SH
9371static struct ofpbuf *
9372ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version,
9373 const struct ofputil_group_mod *gm)
9374{
9375 struct ofpbuf *b;
9376 struct ofp15_group_mod *ogm;
9377 size_t start_ogm;
9378 struct ofputil_bucket *bucket;
9379 struct id_pool *bucket_ids = NULL;
9380
9381 b = ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD, ofp_version, 0);
6fd6ed71 9382 start_ogm = b->size;
18ac06d3
SH
9383 ofpbuf_put_zeros(b, sizeof *ogm);
9384
9385 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
9386 uint32_t bucket_id;
9387
9388 /* Generate a bucket id if none was supplied */
9389 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
9390 if (!bucket_ids) {
9391 const struct ofputil_bucket *bkt;
9392
9393 bucket_ids = id_pool_create(0, OFPG15_BUCKET_MAX + 1);
9394
9395 /* Mark all bucket_ids that are present in gm
9396 * as used in the pool. */
9397 LIST_FOR_EACH_REVERSE (bkt, list_node, &gm->buckets) {
9398 if (bkt == bucket) {
9399 break;
9400 }
9401 if (bkt->bucket_id <= OFPG15_BUCKET_MAX) {
9402 id_pool_add(bucket_ids, bkt->bucket_id);
9403 }
9404 }
9405 }
9406
9407 if (!id_pool_alloc_id(bucket_ids, &bucket_id)) {
9408 OVS_NOT_REACHED();
9409 }
9410 } else {
9411 bucket_id = bucket->bucket_id;
9412 }
9413
9414 ofputil_put_ofp15_bucket(bucket, bucket_id, gm->type, b, ofp_version);
9415 }
9416 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
9417 ogm->command = htons(gm->command);
9418 ogm->type = gm->type;
9419 ogm->group_id = htonl(gm->group_id);
9420 ogm->command_bucket_id = htonl(gm->command_bucket_id);
6fd6ed71 9421 ogm->bucket_array_len = htons(b->size - start_ogm - sizeof *ogm);
18ac06d3 9422
53eb84a5
SH
9423 /* Add group properties */
9424 if (gm->props.selection_method[0]) {
9425 ofputil_put_group_prop_ntr_selection_method(ofp_version, &gm->props, b);
9426 }
9427
18ac06d3
SH
9428 id_pool_destroy(bucket_ids);
9429 return b;
9430}
9431
d4b1d0ca 9432static void
9dd30b05
BP
9433bad_group_cmd(enum ofp15_group_mod_command cmd)
9434{
d4b1d0ca
SH
9435 const char *opt_version;
9436 const char *version;
9437 const char *cmd_str;
9438
9439 switch (cmd) {
9440 case OFPGC15_ADD:
9441 case OFPGC15_MODIFY:
88b87a36 9442 case OFPGC15_ADD_OR_MOD:
d4b1d0ca
SH
9443 case OFPGC15_DELETE:
9444 version = "1.1";
9445 opt_version = "11";
9446 break;
9447
9448 case OFPGC15_INSERT_BUCKET:
9449 case OFPGC15_REMOVE_BUCKET:
9450 version = "1.5";
9451 opt_version = "15";
9dd30b05 9452 break;
d4b1d0ca
SH
9453
9454 default:
9455 OVS_NOT_REACHED();
9456 }
9457
9458 switch (cmd) {
9459 case OFPGC15_ADD:
9460 cmd_str = "add-group";
9461 break;
9462
9463 case OFPGC15_MODIFY:
88b87a36 9464 case OFPGC15_ADD_OR_MOD:
d4b1d0ca
SH
9465 cmd_str = "mod-group";
9466 break;
9467
9468 case OFPGC15_DELETE:
9469 cmd_str = "del-group";
9470 break;
9471
9472 case OFPGC15_INSERT_BUCKET:
9473 cmd_str = "insert-bucket";
9474 break;
9475
9476 case OFPGC15_REMOVE_BUCKET:
9dd30b05 9477 cmd_str = "remove-bucket";
d4b1d0ca
SH
9478 break;
9479
9480 default:
9481 OVS_NOT_REACHED();
9482 }
9483
9484 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
9485 cmd_str, version, opt_version);
9486
9487}
9488
4498bea5
SH
9489/* Converts abstract group mod 'gm' into a message for OpenFlow version
9490 * 'ofp_version' and returns the message. */
9491struct ofpbuf *
9492ofputil_encode_group_mod(enum ofp_version ofp_version,
9493 const struct ofputil_group_mod *gm)
9494{
d4b1d0ca 9495
7395c052 9496 switch (ofp_version) {
d4b1d0ca
SH
9497 case OFP10_VERSION:
9498 bad_group_cmd(gm->command);
7395c052
NZ
9499
9500 case OFP11_VERSION:
9501 case OFP12_VERSION:
c37c0382
AC
9502 case OFP13_VERSION:
9503 case OFP14_VERSION:
88b87a36 9504 if (gm->command > OFPGC11_DELETE && gm->command != OFPGC11_ADD_OR_MOD) {
d4b1d0ca
SH
9505 bad_group_cmd(gm->command);
9506 }
4498bea5 9507 return ofputil_encode_ofp11_group_mod(ofp_version, gm);
7395c052 9508
18ac06d3 9509 case OFP15_VERSION:
b79d45a1 9510 case OFP16_VERSION:
18ac06d3
SH
9511 return ofputil_encode_ofp15_group_mod(ofp_version, gm);
9512
7395c052 9513 default:
428b2edd 9514 OVS_NOT_REACHED();
7395c052 9515 }
7395c052
NZ
9516}
9517
aead63f2
SH
9518static enum ofperr
9519ofputil_pull_ofp11_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
9520 struct ofputil_group_mod *gm)
9521{
9522 const struct ofp11_group_mod *ogm;
08ba0810 9523 enum ofperr error;
aead63f2
SH
9524
9525 ogm = ofpbuf_pull(msg, sizeof *ogm);
9526 gm->command = ntohs(ogm->command);
9527 gm->type = ogm->type;
9528 gm->group_id = ntohl(ogm->group_id);
18ac06d3 9529 gm->command_bucket_id = OFPG15_BUCKET_ALL;
aead63f2 9530
6fd6ed71 9531 error = ofputil_pull_ofp11_buckets(msg, msg->size, ofp_version,
08ba0810
BP
9532 &gm->buckets);
9533
9534 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
9535 if (!error
9536 && ofp_version >= OFP13_VERSION
9537 && gm->command == OFPGC11_DELETE
417e7e66 9538 && !ovs_list_is_empty(&gm->buckets)) {
08ba0810
BP
9539 error = OFPERR_OFPGMFC_INVALID_GROUP;
9540 }
9541
9542 return error;
aead63f2
SH
9543}
9544
18ac06d3
SH
9545static enum ofperr
9546ofputil_pull_ofp15_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
9547 struct ofputil_group_mod *gm)
9548{
9549 const struct ofp15_group_mod *ogm;
9550 uint16_t bucket_list_len;
9551 enum ofperr error = OFPERR_OFPGMFC_BAD_BUCKET;
9552
9553 ogm = ofpbuf_pull(msg, sizeof *ogm);
9554 gm->command = ntohs(ogm->command);
9555 gm->type = ogm->type;
9556 gm->group_id = ntohl(ogm->group_id);
9557
9558 gm->command_bucket_id = ntohl(ogm->command_bucket_id);
9559 switch (gm->command) {
9560 case OFPGC15_REMOVE_BUCKET:
9561 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
9562 error = 0;
9563 }
9564 /* Fall through */
9565 case OFPGC15_INSERT_BUCKET:
9566 if (gm->command_bucket_id <= OFPG15_BUCKET_MAX ||
9567 gm->command_bucket_id == OFPG15_BUCKET_FIRST
9568 || gm->command_bucket_id == OFPG15_BUCKET_LAST) {
9569 error = 0;
9570 }
9571 break;
9572
9573 case OFPGC11_ADD:
9574 case OFPGC11_MODIFY:
88b87a36 9575 case OFPGC11_ADD_OR_MOD:
18ac06d3
SH
9576 case OFPGC11_DELETE:
9577 default:
9578 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
9579 error = 0;
9580 }
9581 break;
9582 }
9583 if (error) {
9584 VLOG_WARN_RL(&bad_ofmsg_rl,
9585 "group command bucket id (%u) is out of range",
9586 gm->command_bucket_id);
9587 return OFPERR_OFPGMFC_BAD_BUCKET;
9588 }
9589
79870963 9590 bucket_list_len = ntohs(ogm->bucket_array_len);
bc65c25a 9591 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, ofp_version,
64e8c446 9592 gm->type, &gm->buckets);
bc65c25a
SH
9593 if (error) {
9594 return error;
18ac06d3
SH
9595 }
9596
bc65c25a
SH
9597 return parse_ofp15_group_properties(msg, gm->type, gm->command, &gm->props,
9598 msg->size);
18ac06d3
SH
9599}
9600
7395c052
NZ
9601/* Converts OpenFlow group mod message 'oh' into an abstract group mod in
9602 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
9603enum ofperr
9604ofputil_decode_group_mod(const struct ofp_header *oh,
9605 struct ofputil_group_mod *gm)
9606{
0a2869d5 9607 ofputil_init_group_properties(&gm->props);
7395c052 9608
0a2869d5
BP
9609 enum ofp_version ofp_version = oh->version;
9610 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
7395c052
NZ
9611 ofpraw_pull_assert(&msg);
9612
0a2869d5 9613 enum ofperr err;
aead63f2
SH
9614 switch (ofp_version)
9615 {
9616 case OFP11_VERSION:
9617 case OFP12_VERSION:
9618 case OFP13_VERSION:
9619 case OFP14_VERSION:
aead63f2
SH
9620 err = ofputil_pull_ofp11_group_mod(&msg, ofp_version, gm);
9621 break;
9622
18ac06d3 9623 case OFP15_VERSION:
b79d45a1 9624 case OFP16_VERSION:
18ac06d3
SH
9625 err = ofputil_pull_ofp15_group_mod(&msg, ofp_version, gm);
9626 break;
9627
aead63f2
SH
9628 case OFP10_VERSION:
9629 default:
9630 OVS_NOT_REACHED();
9631 }
e57681e5
SH
9632 if (err) {
9633 return err;
9634 }
9635
e1799eb7
SH
9636 switch (gm->type) {
9637 case OFPGT11_INDIRECT:
beaa2b87
LS
9638 if (gm->command != OFPGC11_DELETE
9639 && !ovs_list_is_singleton(&gm->buckets) ) {
290eada7 9640 return OFPERR_OFPGMFC_INVALID_GROUP;
e1799eb7
SH
9641 }
9642 break;
9643 case OFPGT11_ALL:
9644 case OFPGT11_SELECT:
9645 case OFPGT11_FF:
9646 break;
9647 default:
09d4b951 9648 return OFPERR_OFPGMFC_BAD_TYPE;
65075b99
SH
9649 }
9650
9651 switch (gm->command) {
9652 case OFPGC11_ADD:
9653 case OFPGC11_MODIFY:
88b87a36 9654 case OFPGC11_ADD_OR_MOD:
65075b99
SH
9655 case OFPGC11_DELETE:
9656 case OFPGC15_INSERT_BUCKET:
9657 break;
9658 case OFPGC15_REMOVE_BUCKET:
417e7e66 9659 if (!ovs_list_is_empty(&gm->buckets)) {
65075b99
SH
9660 return OFPERR_OFPGMFC_BAD_BUCKET;
9661 }
9662 break;
9663 default:
09d4b951 9664 return OFPERR_OFPGMFC_BAD_COMMAND;
e1799eb7
SH
9665 }
9666
0a2869d5 9667 struct ofputil_bucket *bucket;
e57681e5 9668 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
64e8c446
BP
9669 if (bucket->weight && gm->type != OFPGT11_SELECT) {
9670 return OFPERR_OFPGMFC_INVALID_GROUP;
9671 }
9672
e57681e5
SH
9673 switch (gm->type) {
9674 case OFPGT11_ALL:
9675 case OFPGT11_INDIRECT:
9676 if (ofputil_bucket_has_liveness(bucket)) {
9677 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED;
9678 }
9679 break;
9680 case OFPGT11_SELECT:
9681 break;
9682 case OFPGT11_FF:
9683 if (!ofputil_bucket_has_liveness(bucket)) {
9684 return OFPERR_OFPGMFC_INVALID_GROUP;
9685 }
9686 break;
9687 default:
896313f3
FL
9688 /* Returning BAD TYPE to be consistent
9689 * though gm->type has been checked already. */
9690 return OFPERR_OFPGMFC_BAD_TYPE;
e57681e5
SH
9691 }
9692 }
9693
9694 return 0;
7395c052
NZ
9695}
9696
25070e04
JR
9697/* Destroys 'bms'. */
9698void
6dd3c787
JR
9699ofputil_free_bundle_msgs(struct ofputil_bundle_msg *bms, size_t n_bms)
9700{
9701 for (size_t i = 0; i < n_bms; i++) {
9702 switch ((int)bms[i].type) {
9703 case OFPTYPE_FLOW_MOD:
9704 free(CONST_CAST(struct ofpact *, bms[i].fm.ofpacts));
9705 break;
9706 case OFPTYPE_GROUP_MOD:
9707 ofputil_uninit_group_mod(&bms[i].gm);
9708 break;
9709 case OFPTYPE_PACKET_OUT:
9710 free(bms[i].po.ofpacts);
9711 free(CONST_CAST(void *, bms[i].po.packet));
9712 break;
9713 default:
9714 break;
9715 }
9716 }
9717 free(bms);
9718}
9719
9720void
9721ofputil_encode_bundle_msgs(const struct ofputil_bundle_msg *bms,
9722 size_t n_bms, struct ovs_list *requests,
25070e04
JR
9723 enum ofputil_protocol protocol)
9724{
9725 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
9726
9727 for (size_t i = 0; i < n_bms; i++) {
9728 struct ofpbuf *request = NULL;
9729
9730 switch ((int)bms[i].type) {
9731 case OFPTYPE_FLOW_MOD:
9732 request = ofputil_encode_flow_mod(&bms[i].fm, protocol);
25070e04
JR
9733 break;
9734 case OFPTYPE_GROUP_MOD:
9735 request = ofputil_encode_group_mod(version, &bms[i].gm);
6dd3c787
JR
9736 break;
9737 case OFPTYPE_PACKET_OUT:
9738 request = ofputil_encode_packet_out(&bms[i].po, protocol);
25070e04
JR
9739 break;
9740 default:
9741 break;
9742 }
9743 if (request) {
9744 ovs_list_push_back(requests, &request->list_node);
9745 }
9746 }
25070e04
JR
9747}
9748
64626975
SH
9749/* Parse a queue status request message into 'oqsr'.
9750 * Returns 0 if successful, otherwise an OFPERR_* number. */
9751enum ofperr
9752ofputil_decode_queue_stats_request(const struct ofp_header *request,
9753 struct ofputil_queue_stats_request *oqsr)
9754{
9755 switch ((enum ofp_version)request->version) {
b79d45a1 9756 case OFP16_VERSION:
42dccab5 9757 case OFP15_VERSION:
c37c0382 9758 case OFP14_VERSION:
2e1ae200 9759 case OFP13_VERSION:
64626975
SH
9760 case OFP12_VERSION:
9761 case OFP11_VERSION: {
9762 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
9763 oqsr->queue_id = ntohl(qsr11->queue_id);
9764 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
9765 }
9766
9767 case OFP10_VERSION: {
7f05e7ab
JR
9768 const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
9769 oqsr->queue_id = ntohl(qsr10->queue_id);
4e022ec0 9770 oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no));
7f05e7ab
JR
9771 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
9772 if (oqsr->port_no == OFPP_ALL) {
9773 oqsr->port_no = OFPP_ANY;
9774 }
64626975
SH
9775 return 0;
9776 }
9777
9778 default:
428b2edd 9779 OVS_NOT_REACHED();
64626975
SH
9780 }
9781}
9782
0746a84f
BP
9783/* Encode a queue stats request for 'oqsr', the encoded message
9784 * will be for OpenFlow version 'ofp_version'. Returns message
9785 * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
64626975
SH
9786struct ofpbuf *
9787ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
9788 const struct ofputil_queue_stats_request *oqsr)
9789{
9790 struct ofpbuf *request;
9791
9792 switch (ofp_version) {
9793 case OFP11_VERSION:
2e1ae200 9794 case OFP12_VERSION:
c37c0382 9795 case OFP13_VERSION:
42dccab5 9796 case OFP14_VERSION:
b79d45a1
BP
9797 case OFP15_VERSION:
9798 case OFP16_VERSION: {
64626975
SH
9799 struct ofp11_queue_stats_request *req;
9800 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
9801 req = ofpbuf_put_zeros(request, sizeof *req);
9802 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
9803 req->queue_id = htonl(oqsr->queue_id);
9804 break;
9805 }
9806 case OFP10_VERSION: {
9807 struct ofp10_queue_stats_request *req;
9808 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
9809 req = ofpbuf_put_zeros(request, sizeof *req);
7f05e7ab 9810 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
4e022ec0
AW
9811 req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY
9812 ? OFPP_ALL : oqsr->port_no));
64626975
SH
9813 req->queue_id = htonl(oqsr->queue_id);
9814 break;
9815 }
9816 default:
428b2edd 9817 OVS_NOT_REACHED();
64626975
SH
9818 }
9819
9820 return request;
9821}
9822
9823/* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
9824 * message 'oh'. */
9825size_t
9826ofputil_count_queue_stats(const struct ofp_header *oh)
9827{
0a2869d5 9828 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
64626975 9829 ofpraw_pull_assert(&b);
0a2869d5
BP
9830
9831 for (size_t n = 0; ; n++) {
9832 struct ofputil_queue_stats qs;
9833 if (ofputil_decode_queue_stats(&qs, &b)) {
9834 return n;
9835 }
1bb2cdbe 9836 }
64626975
SH
9837}
9838
9839static enum ofperr
9840ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
9841 const struct ofp10_queue_stats *qs10)
9842{
4e022ec0 9843 oqs->port_no = u16_to_ofp(ntohs(qs10->port_no));
64626975 9844 oqs->queue_id = ntohl(qs10->queue_id);
6dc34a0d
BP
9845 oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
9846 oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
9847 oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
9848 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
64626975
SH
9849
9850 return 0;
9851}
9852
9853static enum ofperr
9854ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
9855 const struct ofp11_queue_stats *qs11)
9856{
9857 enum ofperr error;
9858
9859 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
9860 if (error) {
9861 return error;
9862 }
9863
9864 oqs->queue_id = ntohl(qs11->queue_id);
6dc34a0d
BP
9865 oqs->tx_bytes = ntohll(qs11->tx_bytes);
9866 oqs->tx_packets = ntohll(qs11->tx_packets);
9867 oqs->tx_errors = ntohll(qs11->tx_errors);
9868 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
64626975
SH
9869
9870 return 0;
9871}
9872
2e1ae200
JR
9873static enum ofperr
9874ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
9875 const struct ofp13_queue_stats *qs13)
9876{
6dc34a0d 9877 enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
2e1ae200 9878 if (!error) {
6dc34a0d
BP
9879 oqs->duration_sec = ntohl(qs13->duration_sec);
9880 oqs->duration_nsec = ntohl(qs13->duration_nsec);
2e1ae200
JR
9881 }
9882
9883 return error;
9884}
9885
1bb2cdbe
BP
9886static enum ofperr
9887ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats *oqs,
9888 struct ofpbuf *msg)
9889{
9890 const struct ofp14_queue_stats *qs14;
9891 size_t len;
9892
9893 qs14 = ofpbuf_try_pull(msg, sizeof *qs14);
9894 if (!qs14) {
9895 return OFPERR_OFPBRC_BAD_LEN;
9896 }
9897
9898 len = ntohs(qs14->length);
6fd6ed71 9899 if (len < sizeof *qs14 || len - sizeof *qs14 > msg->size) {
1bb2cdbe
BP
9900 return OFPERR_OFPBRC_BAD_LEN;
9901 }
9902 ofpbuf_pull(msg, len - sizeof *qs14);
9903
9904 /* No properties yet defined, so ignore them for now. */
9905
9906 return ofputil_queue_stats_from_ofp13(oqs, &qs14->qs);
9907}
9908
64626975
SH
9909/* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
9910 * ofputil_queue_stats in 'qs'.
9911 *
9912 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
9913 * message. Calling this function multiple times for a single 'msg' iterates
9914 * through the replies. The caller must initially leave 'msg''s layer pointers
9915 * null and not modify them between calls.
9916 *
9917 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9918 * otherwise a positive errno value. */
9919int
9920ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
9921{
9922 enum ofperr error;
9923 enum ofpraw raw;
9924
6fd6ed71 9925 error = (msg->header ? ofpraw_decode(&raw, msg->header)
64626975
SH
9926 : ofpraw_pull(&raw, msg));
9927 if (error) {
9928 return error;
9929 }
9930
6fd6ed71 9931 if (!msg->size) {
64626975 9932 return EOF;
1bb2cdbe
BP
9933 } else if (raw == OFPRAW_OFPST14_QUEUE_REPLY) {
9934 return ofputil_pull_ofp14_queue_stats(qs, msg);
2e1ae200
JR
9935 } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
9936 const struct ofp13_queue_stats *qs13;
9937
9938 qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
9939 if (!qs13) {
9940 goto bad_len;
9941 }
9942 return ofputil_queue_stats_from_ofp13(qs, qs13);
64626975
SH
9943 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
9944 const struct ofp11_queue_stats *qs11;
9945
9946 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
9947 if (!qs11) {
2e1ae200 9948 goto bad_len;
64626975
SH
9949 }
9950 return ofputil_queue_stats_from_ofp11(qs, qs11);
9951 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
9952 const struct ofp10_queue_stats *qs10;
9953
9954 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
9955 if (!qs10) {
2e1ae200 9956 goto bad_len;
64626975
SH
9957 }
9958 return ofputil_queue_stats_from_ofp10(qs, qs10);
9959 } else {
428b2edd 9960 OVS_NOT_REACHED();
64626975 9961 }
2e1ae200
JR
9962
9963 bad_len:
437d0d22 9964 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover "
6fd6ed71 9965 "bytes at end", msg->size);
2e1ae200 9966 return OFPERR_OFPBRC_BAD_LEN;
64626975
SH
9967}
9968
9969static void
9970ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
9971 struct ofp10_queue_stats *qs10)
9972{
4e022ec0 9973 qs10->port_no = htons(ofp_to_u16(oqs->port_no));
64626975
SH
9974 memset(qs10->pad, 0, sizeof qs10->pad);
9975 qs10->queue_id = htonl(oqs->queue_id);
6dc34a0d
BP
9976 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes));
9977 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets));
9978 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors));
64626975
SH
9979}
9980
9981static void
9982ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
9983 struct ofp11_queue_stats *qs11)
9984{
9985 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
9986 qs11->queue_id = htonl(oqs->queue_id);
6dc34a0d
BP
9987 qs11->tx_bytes = htonll(oqs->tx_bytes);
9988 qs11->tx_packets = htonll(oqs->tx_packets);
9989 qs11->tx_errors = htonll(oqs->tx_errors);
64626975
SH
9990}
9991
2e1ae200
JR
9992static void
9993ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
9994 struct ofp13_queue_stats *qs13)
9995{
9996 ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
6dc34a0d
BP
9997 if (oqs->duration_sec != UINT32_MAX) {
9998 qs13->duration_sec = htonl(oqs->duration_sec);
9999 qs13->duration_nsec = htonl(oqs->duration_nsec);
10000 } else {
b8266395
BP
10001 qs13->duration_sec = OVS_BE32_MAX;
10002 qs13->duration_nsec = OVS_BE32_MAX;
6dc34a0d 10003 }
2e1ae200
JR
10004}
10005
1bb2cdbe
BP
10006static void
10007ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs,
10008 struct ofp14_queue_stats *qs14)
10009{
10010 qs14->length = htons(sizeof *qs14);
10011 memset(qs14->pad, 0, sizeof qs14->pad);
10012 ofputil_queue_stats_to_ofp13(oqs, &qs14->qs);
10013}
10014
10015
64626975
SH
10016/* Encode a queue stat for 'oqs' and append it to 'replies'. */
10017void
ca6ba700 10018ofputil_append_queue_stat(struct ovs_list *replies,
64626975
SH
10019 const struct ofputil_queue_stats *oqs)
10020{
e28ac5cf 10021 switch (ofpmp_version(replies)) {
2e1ae200
JR
10022 case OFP13_VERSION: {
10023 struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
10024 ofputil_queue_stats_to_ofp13(oqs, reply);
10025 break;
10026 }
10027
64626975
SH
10028 case OFP12_VERSION:
10029 case OFP11_VERSION: {
2e1ae200 10030 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
64626975
SH
10031 ofputil_queue_stats_to_ofp11(oqs, reply);
10032 break;
10033 }
10034
10035 case OFP10_VERSION: {
2e1ae200 10036 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
64626975
SH
10037 ofputil_queue_stats_to_ofp10(oqs, reply);
10038 break;
10039 }
10040
42dccab5 10041 case OFP14_VERSION:
b79d45a1
BP
10042 case OFP15_VERSION:
10043 case OFP16_VERSION: {
1bb2cdbe
BP
10044 struct ofp14_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
10045 ofputil_queue_stats_to_ofp14(oqs, reply);
c37c0382 10046 break;
1bb2cdbe 10047 }
c37c0382 10048
64626975 10049 default:
428b2edd 10050 OVS_NOT_REACHED();
64626975
SH
10051 }
10052}
777af88d
AC
10053
10054enum ofperr
10055ofputil_decode_bundle_ctrl(const struct ofp_header *oh,
10056 struct ofputil_bundle_ctrl_msg *msg)
10057{
0a2869d5
BP
10058 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
10059 enum ofpraw raw = ofpraw_pull_assert(&b);
06d4d4b6
JR
10060 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL
10061 || raw == OFPRAW_ONFT13_BUNDLE_CONTROL);
777af88d 10062
0a2869d5 10063 const struct ofp14_bundle_ctrl_msg *m = b.msg;
777af88d
AC
10064 msg->bundle_id = ntohl(m->bundle_id);
10065 msg->type = ntohs(m->type);
10066 msg->flags = ntohs(m->flags);
10067
10068 return 0;
10069}
10070
db5076ee
JR
10071struct ofpbuf *
10072ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version,
10073 struct ofputil_bundle_ctrl_msg *bc)
10074{
10075 struct ofpbuf *request;
10076 struct ofp14_bundle_ctrl_msg *m;
10077
10078 switch (ofp_version) {
10079 case OFP10_VERSION:
10080 case OFP11_VERSION:
10081 case OFP12_VERSION:
06d4d4b6 10082 ovs_fatal(0, "bundles need OpenFlow 1.3 or later "
db5076ee 10083 "(\'-O OpenFlow14\')");
06d4d4b6 10084 case OFP13_VERSION:
db5076ee
JR
10085 case OFP14_VERSION:
10086 case OFP15_VERSION:
b79d45a1 10087 case OFP16_VERSION:
06d4d4b6
JR
10088 request = ofpraw_alloc(ofp_version == OFP13_VERSION
10089 ? OFPRAW_ONFT13_BUNDLE_CONTROL
10090 : OFPRAW_OFPT14_BUNDLE_CONTROL, ofp_version, 0);
db5076ee
JR
10091 m = ofpbuf_put_zeros(request, sizeof *m);
10092
10093 m->bundle_id = htonl(bc->bundle_id);
10094 m->type = htons(bc->type);
10095 m->flags = htons(bc->flags);
10096 break;
10097 default:
10098 OVS_NOT_REACHED();
10099 }
10100
10101 return request;
10102}
10103
777af88d
AC
10104struct ofpbuf *
10105ofputil_encode_bundle_ctrl_reply(const struct ofp_header *oh,
10106 struct ofputil_bundle_ctrl_msg *msg)
10107{
10108 struct ofpbuf *buf;
10109 struct ofp14_bundle_ctrl_msg *m;
10110
06d4d4b6
JR
10111 buf = ofpraw_alloc_reply(oh->version == OFP13_VERSION
10112 ? OFPRAW_ONFT13_BUNDLE_CONTROL
10113 : OFPRAW_OFPT14_BUNDLE_CONTROL, oh, 0);
777af88d
AC
10114 m = ofpbuf_put_zeros(buf, sizeof *m);
10115
10116 m->bundle_id = htonl(msg->bundle_id);
10117 m->type = htons(msg->type);
10118 m->flags = htons(msg->flags);
10119
10120 return buf;
10121}
10122
c25ce22d
JR
10123/* Return true for bundlable state change requests, false for other messages.
10124 */
10125static bool
10126ofputil_is_bundlable(enum ofptype type)
10127{
10128 switch (type) {
10129 /* Minimum required by OpenFlow 1.4. */
10130 case OFPTYPE_PORT_MOD:
10131 case OFPTYPE_FLOW_MOD:
25070e04
JR
10132 /* Other supported types. */
10133 case OFPTYPE_GROUP_MOD:
6dd3c787 10134 case OFPTYPE_PACKET_OUT:
c25ce22d
JR
10135 return true;
10136
10137 /* Nice to have later. */
10138 case OFPTYPE_FLOW_MOD_TABLE_ID:
c25ce22d
JR
10139 case OFPTYPE_TABLE_MOD:
10140 case OFPTYPE_METER_MOD:
4e548ad9 10141 case OFPTYPE_NXT_TLV_TABLE_MOD:
c25ce22d
JR
10142
10143 /* Not to be bundlable. */
10144 case OFPTYPE_ECHO_REQUEST:
10145 case OFPTYPE_FEATURES_REQUEST:
10146 case OFPTYPE_GET_CONFIG_REQUEST:
10147 case OFPTYPE_SET_CONFIG:
10148 case OFPTYPE_BARRIER_REQUEST:
10149 case OFPTYPE_ROLE_REQUEST:
10150 case OFPTYPE_ECHO_REPLY:
10151 case OFPTYPE_SET_FLOW_FORMAT:
10152 case OFPTYPE_SET_PACKET_IN_FORMAT:
10153 case OFPTYPE_SET_CONTROLLER_ID:
10154 case OFPTYPE_FLOW_AGE:
10155 case OFPTYPE_FLOW_MONITOR_CANCEL:
10156 case OFPTYPE_SET_ASYNC_CONFIG:
10157 case OFPTYPE_GET_ASYNC_REQUEST:
10158 case OFPTYPE_DESC_STATS_REQUEST:
10159 case OFPTYPE_FLOW_STATS_REQUEST:
10160 case OFPTYPE_AGGREGATE_STATS_REQUEST:
10161 case OFPTYPE_TABLE_STATS_REQUEST:
10162 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
03c72922 10163 case OFPTYPE_TABLE_DESC_REQUEST:
c25ce22d
JR
10164 case OFPTYPE_PORT_STATS_REQUEST:
10165 case OFPTYPE_QUEUE_STATS_REQUEST:
10166 case OFPTYPE_PORT_DESC_STATS_REQUEST:
10167 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
10168 case OFPTYPE_METER_STATS_REQUEST:
10169 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
10170 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
10171 case OFPTYPE_GROUP_STATS_REQUEST:
10172 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
10173 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
10174 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
10175 case OFPTYPE_BUNDLE_CONTROL:
10176 case OFPTYPE_BUNDLE_ADD_MESSAGE:
10177 case OFPTYPE_HELLO:
10178 case OFPTYPE_ERROR:
10179 case OFPTYPE_FEATURES_REPLY:
10180 case OFPTYPE_GET_CONFIG_REPLY:
10181 case OFPTYPE_PACKET_IN:
10182 case OFPTYPE_FLOW_REMOVED:
10183 case OFPTYPE_PORT_STATUS:
10184 case OFPTYPE_BARRIER_REPLY:
10185 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
10186 case OFPTYPE_DESC_STATS_REPLY:
10187 case OFPTYPE_FLOW_STATS_REPLY:
10188 case OFPTYPE_QUEUE_STATS_REPLY:
10189 case OFPTYPE_PORT_STATS_REPLY:
10190 case OFPTYPE_TABLE_STATS_REPLY:
10191 case OFPTYPE_AGGREGATE_STATS_REPLY:
10192 case OFPTYPE_PORT_DESC_STATS_REPLY:
10193 case OFPTYPE_ROLE_REPLY:
10194 case OFPTYPE_FLOW_MONITOR_PAUSED:
10195 case OFPTYPE_FLOW_MONITOR_RESUMED:
10196 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
10197 case OFPTYPE_GET_ASYNC_REPLY:
10198 case OFPTYPE_GROUP_STATS_REPLY:
10199 case OFPTYPE_GROUP_DESC_STATS_REPLY:
10200 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
10201 case OFPTYPE_METER_STATS_REPLY:
10202 case OFPTYPE_METER_CONFIG_STATS_REPLY:
10203 case OFPTYPE_METER_FEATURES_STATS_REPLY:
10204 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
03c72922 10205 case OFPTYPE_TABLE_DESC_REPLY:
c25ce22d 10206 case OFPTYPE_ROLE_STATUS:
3c35db62 10207 case OFPTYPE_REQUESTFORWARD:
6c6eedc5 10208 case OFPTYPE_TABLE_STATUS:
4e548ad9
ML
10209 case OFPTYPE_NXT_TLV_TABLE_REQUEST:
10210 case OFPTYPE_NXT_TLV_TABLE_REPLY:
77ab5fd2 10211 case OFPTYPE_NXT_RESUME:
fb8f22c1
BY
10212 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST:
10213 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY:
10214 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST:
10215 case OFPTYPE_IPFIX_FLOW_STATS_REPLY:
2a7c4805 10216 case OFPTYPE_CT_FLUSH_ZONE:
c25ce22d
JR
10217 break;
10218 }
10219
10220 return false;
10221}
10222
777af88d
AC
10223enum ofperr
10224ofputil_decode_bundle_add(const struct ofp_header *oh,
7ac27a04 10225 struct ofputil_bundle_add_msg *msg,
0a2869d5 10226 enum ofptype *typep)
777af88d 10227{
0a2869d5
BP
10228 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
10229 enum ofpraw raw = ofpraw_pull_assert(&b);
06d4d4b6
JR
10230 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE
10231 || raw == OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE);
777af88d 10232
0a2869d5 10233 const struct ofp14_bundle_ctrl_msg *m = ofpbuf_pull(&b, sizeof *m);
777af88d
AC
10234 msg->bundle_id = ntohl(m->bundle_id);
10235 msg->flags = ntohs(m->flags);
10236
6fd6ed71 10237 msg->msg = b.data;
46be7132 10238 if (msg->msg->version != oh->version) {
bc02606a 10239 return OFPERR_OFPBFC_BAD_VERSION;
46be7132 10240 }
0a2869d5 10241 size_t inner_len = ntohs(msg->msg->length);
6fd6ed71 10242 if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
777af88d
AC
10243 return OFPERR_OFPBFC_MSG_BAD_LEN;
10244 }
be6f6393
JR
10245 if (msg->msg->xid != oh->xid) {
10246 return OFPERR_OFPBFC_MSG_BAD_XID;
10247 }
777af88d 10248
c25ce22d 10249 /* Reject unbundlable messages. */
0a2869d5
BP
10250 enum ofptype type;
10251 enum ofperr error = ofptype_decode(&type, msg->msg);
c25ce22d
JR
10252 if (error) {
10253 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPT14_BUNDLE_ADD_MESSAGE contained "
10254 "message is unparsable (%s)", ofperr_get_name(error));
10255 return OFPERR_OFPBFC_MSG_UNSUP; /* 'error' would be confusing. */
10256 }
10257
0a2869d5 10258 if (!ofputil_is_bundlable(type)) {
44648b0f 10259 VLOG_WARN_RL(&bad_ofmsg_rl, "%s message not allowed inside "
0a2869d5 10260 "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(type));
c25ce22d
JR
10261 return OFPERR_OFPBFC_MSG_UNSUP;
10262 }
0a2869d5
BP
10263 if (typep) {
10264 *typep = type;
10265 }
c25ce22d 10266
777af88d
AC
10267 return 0;
10268}
10269
10270struct ofpbuf *
10271ofputil_encode_bundle_add(enum ofp_version ofp_version,
10272 struct ofputil_bundle_add_msg *msg)
10273{
10274 struct ofpbuf *request;
10275 struct ofp14_bundle_ctrl_msg *m;
10276
7b3dca89 10277 /* Must use the same xid as the embedded message. */
06d4d4b6
JR
10278 request = ofpraw_alloc_xid(ofp_version == OFP13_VERSION
10279 ? OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE
10280 : OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE, ofp_version,
bcf899f7 10281 msg->msg->xid, ntohs(msg->msg->length));
777af88d
AC
10282 m = ofpbuf_put_zeros(request, sizeof *m);
10283
10284 m->bundle_id = htonl(msg->bundle_id);
10285 m->flags = htons(msg->flags);
10286 ofpbuf_put(request, msg->msg, ntohs(msg->msg->length));
10287
bcf899f7 10288 ofpmsg_update_length(request);
777af88d
AC
10289 return request;
10290}
6159c531
JG
10291
10292static void
4e548ad9 10293encode_tlv_table_mappings(struct ofpbuf *b, struct ovs_list *mappings)
6159c531 10294{
4e548ad9 10295 struct ofputil_tlv_map *map;
6159c531
JG
10296
10297 LIST_FOR_EACH (map, list_node, mappings) {
4e548ad9 10298 struct nx_tlv_map *nx_map;
6159c531
JG
10299
10300 nx_map = ofpbuf_put_zeros(b, sizeof *nx_map);
10301 nx_map->option_class = htons(map->option_class);
10302 nx_map->option_type = map->option_type;
10303 nx_map->option_len = map->option_len;
10304 nx_map->index = htons(map->index);
10305 }
10306}
10307
10308struct ofpbuf *
4e548ad9
ML
10309ofputil_encode_tlv_table_mod(enum ofp_version ofp_version,
10310 struct ofputil_tlv_table_mod *ttm)
6159c531
JG
10311{
10312 struct ofpbuf *b;
4e548ad9 10313 struct nx_tlv_table_mod *nx_ttm;
6159c531 10314
4e548ad9
ML
10315 b = ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_MOD, ofp_version, 0);
10316 nx_ttm = ofpbuf_put_zeros(b, sizeof *nx_ttm);
10317 nx_ttm->command = htons(ttm->command);
10318 encode_tlv_table_mappings(b, &ttm->mappings);
6159c531
JG
10319
10320 return b;
10321}
10322
10323static enum ofperr
4e548ad9 10324decode_tlv_table_mappings(struct ofpbuf *msg, unsigned int max_fields,
dfe5044c 10325 struct ovs_list *mappings)
6159c531 10326{
417e7e66 10327 ovs_list_init(mappings);
6159c531
JG
10328
10329 while (msg->size) {
4e548ad9
ML
10330 struct nx_tlv_map *nx_map;
10331 struct ofputil_tlv_map *map;
6159c531
JG
10332
10333 nx_map = ofpbuf_pull(msg, sizeof *nx_map);
10334 map = xmalloc(sizeof *map);
417e7e66 10335 ovs_list_push_back(mappings, &map->list_node);
6159c531
JG
10336
10337 map->option_class = ntohs(nx_map->option_class);
10338 map->option_type = nx_map->option_type;
10339
10340 map->option_len = nx_map->option_len;
4e548ad9 10341 if (map->option_len % 4 || map->option_len > TLV_MAX_OPT_SIZE) {
6159c531 10342 VLOG_WARN_RL(&bad_ofmsg_rl,
4e548ad9 10343 "tlv table option length (%u) is not a valid option size",
6159c531 10344 map->option_len);
4e548ad9
ML
10345 ofputil_uninit_tlv_table(mappings);
10346 return OFPERR_NXTTMFC_BAD_OPT_LEN;
6159c531
JG
10347 }
10348
10349 map->index = ntohs(nx_map->index);
dfe5044c 10350 if (map->index >= max_fields) {
6159c531 10351 VLOG_WARN_RL(&bad_ofmsg_rl,
4e548ad9 10352 "tlv table field index (%u) is too large (max %u)",
dfe5044c 10353 map->index, max_fields - 1);
4e548ad9
ML
10354 ofputil_uninit_tlv_table(mappings);
10355 return OFPERR_NXTTMFC_BAD_FIELD_IDX;
6159c531
JG
10356 }
10357 }
10358
10359 return 0;
10360}
10361
10362enum ofperr
4e548ad9
ML
10363ofputil_decode_tlv_table_mod(const struct ofp_header *oh,
10364 struct ofputil_tlv_table_mod *ttm)
6159c531 10365{
0a2869d5 10366 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
6159c531
JG
10367 ofpraw_pull_assert(&msg);
10368
0a2869d5 10369 struct nx_tlv_table_mod *nx_ttm = ofpbuf_pull(&msg, sizeof *nx_ttm);
4e548ad9
ML
10370 ttm->command = ntohs(nx_ttm->command);
10371 if (ttm->command > NXTTMC_CLEAR) {
6159c531 10372 VLOG_WARN_RL(&bad_ofmsg_rl,
4e548ad9
ML
10373 "tlv table mod command (%u) is out of range",
10374 ttm->command);
10375 return OFPERR_NXTTMFC_BAD_COMMAND;
6159c531
JG
10376 }
10377
4e548ad9
ML
10378 return decode_tlv_table_mappings(&msg, TUN_METADATA_NUM_OPTS,
10379 &ttm->mappings);
6159c531
JG
10380}
10381
10382struct ofpbuf *
4e548ad9
ML
10383ofputil_encode_tlv_table_reply(const struct ofp_header *oh,
10384 struct ofputil_tlv_table_reply *ttr)
6159c531
JG
10385{
10386 struct ofpbuf *b;
4e548ad9 10387 struct nx_tlv_table_reply *nx_ttr;
6159c531 10388
4e548ad9
ML
10389 b = ofpraw_alloc_reply(OFPRAW_NXT_TLV_TABLE_REPLY, oh, 0);
10390 nx_ttr = ofpbuf_put_zeros(b, sizeof *nx_ttr);
10391 nx_ttr->max_option_space = htonl(ttr->max_option_space);
10392 nx_ttr->max_fields = htons(ttr->max_fields);
6159c531 10393
4e548ad9 10394 encode_tlv_table_mappings(b, &ttr->mappings);
6159c531
JG
10395
10396 return b;
10397}
10398
4e548ad9 10399/* Decodes the NXT_TLV_TABLE_REPLY message in 'oh' into '*ttr'. Returns 0
dfe5044c
BP
10400 * if successful, otherwise an ofperr.
10401 *
4e548ad9
ML
10402 * The decoder verifies that the indexes in 'ttr->mappings' are less than
10403 * 'ttr->max_fields', but the caller must ensure, if necessary, that they are
dfe5044c 10404 * less than TUN_METADATA_NUM_OPTS. */
6159c531 10405enum ofperr
4e548ad9
ML
10406ofputil_decode_tlv_table_reply(const struct ofp_header *oh,
10407 struct ofputil_tlv_table_reply *ttr)
6159c531 10408{
0a2869d5 10409 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
6159c531
JG
10410 ofpraw_pull_assert(&msg);
10411
0a2869d5 10412 struct nx_tlv_table_reply *nx_ttr = ofpbuf_pull(&msg, sizeof *nx_ttr);
4e548ad9
ML
10413 ttr->max_option_space = ntohl(nx_ttr->max_option_space);
10414 ttr->max_fields = ntohs(nx_ttr->max_fields);
6159c531 10415
4e548ad9 10416 return decode_tlv_table_mappings(&msg, ttr->max_fields, &ttr->mappings);
6159c531
JG
10417}
10418
10419void
4e548ad9 10420ofputil_uninit_tlv_table(struct ovs_list *mappings)
6159c531 10421{
4e548ad9 10422 struct ofputil_tlv_map *map;
6159c531
JG
10423
10424 LIST_FOR_EACH_POP (map, list_node, mappings) {
10425 free(map);
10426 }
10427}
98090482 10428
2da7974c
BP
10429const char *
10430ofputil_async_msg_type_to_string(enum ofputil_async_msg_type type)
10431{
10432 switch (type) {
10433 case OAM_PACKET_IN: return "PACKET_IN";
10434 case OAM_PORT_STATUS: return "PORT_STATUS";
10435 case OAM_FLOW_REMOVED: return "FLOW_REMOVED";
10436 case OAM_ROLE_STATUS: return "ROLE_STATUS";
10437 case OAM_TABLE_STATUS: return "TABLE_STATUS";
10438 case OAM_REQUESTFORWARD: return "REQUESTFORWARD";
10439
10440 case OAM_N_TYPES:
10441 default:
10442 OVS_NOT_REACHED();
10443 }
10444}
10445
8fd0bb60
BP
10446struct ofp14_async_prop {
10447 uint64_t prop_type;
10448 enum ofputil_async_msg_type oam;
10449 bool master;
10450 uint32_t allowed10, allowed14;
10451};
10452
10453#define AP_PAIR(SLAVE_PROP_TYPE, OAM, A10, A14) \
10454 { SLAVE_PROP_TYPE, OAM, false, A10, (A14) ? (A14) : (A10) }, \
10455 { (SLAVE_PROP_TYPE + 1), OAM, true, A10, (A14) ? (A14) : (A10) }
10456
10457static const struct ofp14_async_prop async_props[] = {
10458 AP_PAIR( 0, OAM_PACKET_IN, OFPR10_BITS, OFPR14_BITS),
10459 AP_PAIR( 2, OAM_PORT_STATUS, (1 << OFPPR_N_REASONS) - 1, 0),
10460 AP_PAIR( 4, OAM_FLOW_REMOVED, (1 << OVS_OFPRR_NONE) - 1, 0),
10461 AP_PAIR( 6, OAM_ROLE_STATUS, (1 << OFPCRR_N_REASONS) - 1, 0),
10462 AP_PAIR( 8, OAM_TABLE_STATUS, OFPTR_BITS, 0),
10463 AP_PAIR(10, OAM_REQUESTFORWARD, (1 << OFPRFR_N_REASONS) - 1, 0),
10464};
10465
10466#define FOR_EACH_ASYNC_PROP(VAR) \
10467 for (const struct ofp14_async_prop *VAR = async_props; \
10468 VAR < &async_props[ARRAY_SIZE(async_props)]; VAR++)
10469
10470static const struct ofp14_async_prop *
10471get_ofp14_async_config_prop_by_prop_type(uint64_t prop_type)
10472{
10473 FOR_EACH_ASYNC_PROP (ap) {
10474 if (prop_type == ap->prop_type) {
10475 return ap;
10476 }
10477 }
10478 return NULL;
10479}
10480
10481static const struct ofp14_async_prop *
10482get_ofp14_async_config_prop_by_oam(enum ofputil_async_msg_type oam,
10483 bool master)
10484{
10485 FOR_EACH_ASYNC_PROP (ap) {
10486 if (ap->oam == oam && ap->master == master) {
10487 return ap;
10488 }
10489 }
10490 return NULL;
10491}
10492
10493static uint32_t
10494ofp14_async_prop_allowed(const struct ofp14_async_prop *prop,
10495 enum ofp_version version)
10496{
10497 return version >= OFP14_VERSION ? prop->allowed14 : prop->allowed10;
10498}
10499
10500static ovs_be32
10501encode_async_mask(const struct ofputil_async_cfg *src,
10502 const struct ofp14_async_prop *ap,
10503 enum ofp_version version)
10504{
10505 uint32_t mask = ap->master ? src->master[ap->oam] : src->slave[ap->oam];
10506 return htonl(mask & ofp14_async_prop_allowed(ap, version));
10507}
10508
10509static enum ofperr
10510decode_async_mask(ovs_be32 src,
10511 const struct ofp14_async_prop *ap, enum ofp_version version,
10512 bool loose, struct ofputil_async_cfg *dst)
10513{
10514 uint32_t mask = ntohl(src);
10515 uint32_t allowed = ofp14_async_prop_allowed(ap, version);
10516 if (mask & ~allowed) {
10517 OFPPROP_LOG(&bad_ofmsg_rl, loose,
10518 "bad value %#x for %s (allowed mask %#x)",
10519 mask, ofputil_async_msg_type_to_string(ap->oam),
10520 allowed);
10521 mask &= allowed;
10522 if (!loose) {
10523 return OFPERR_OFPACFC_INVALID;
10524 }
10525 }
10526
9bfe9334
BP
10527 if (ap->oam == OAM_PACKET_IN) {
10528 if (mask & (1u << OFPR_NO_MATCH)) {
10529 mask |= 1u << OFPR_EXPLICIT_MISS;
10530 if (version < OFP13_VERSION) {
10531 mask |= 1u << OFPR_IMPLICIT_MISS;
10532 }
10533 }
10534 }
10535
8fd0bb60
BP
10536 uint32_t *array = ap->master ? dst->master : dst->slave;
10537 array[ap->oam] = mask;
10538 return 0;
10539}
10540
10541static enum ofperr
10542parse_async_tlv(const struct ofpbuf *property,
10543 const struct ofp14_async_prop *ap,
10544 struct ofputil_async_cfg *ac,
10545 enum ofp_version version, bool loose)
10546{
10547 enum ofperr error;
10548 ovs_be32 mask;
10549
10550 error = ofpprop_parse_be32(property, &mask);
10551 if (error) {
10552 return error;
10553 }
10554
10555 if (ofpprop_is_experimenter(ap->prop_type)) {
10556 /* For experimenter properties, whether a property is for the master or
10557 * slave role is indicated by both 'type' and 'exp_type' in struct
10558 * ofp_prop_experimenter. Check that these are consistent. */
10559 const struct ofp_prop_experimenter *ope = property->data;
10560 bool should_be_master = ope->type == htons(0xffff);
10561 if (should_be_master != ap->master) {
10562 VLOG_WARN_RL(&bad_ofmsg_rl, "async property type %#"PRIx16" "
10563 "indicates %s role but exp_type %"PRIu32" indicates "
10564 "%s role",
10565 ntohs(ope->type),
10566 should_be_master ? "master" : "slave",
10567 ntohl(ope->exp_type),
10568 ap->master ? "master" : "slave");
10569 return OFPERR_OFPBPC_BAD_EXP_TYPE;
10570 }
10571 }
10572
10573 return decode_async_mask(mask, ap, version, loose, ac);
10574}
10575
10576static void
10577decode_legacy_async_masks(const ovs_be32 masks[2],
10578 enum ofputil_async_msg_type oam,
10579 enum ofp_version version,
10580 struct ofputil_async_cfg *dst)
10581{
10582 for (int i = 0; i < 2; i++) {
10583 bool master = i == 0;
10584 const struct ofp14_async_prop *ap
10585 = get_ofp14_async_config_prop_by_oam(oam, master);
10586 decode_async_mask(masks[i], ap, version, true, dst);
10587 }
10588}
10589
98090482 10590/* Decodes the OpenFlow "set async config" request and "get async config
a930d4c5 10591 * reply" message in '*oh' into an abstract form in 'ac'.
98090482 10592 *
5876b4db
BP
10593 * Some versions of the "set async config" request change only some of the
10594 * settings and leave the others alone. This function uses 'basis' as the
10595 * initial state for decoding these. Other versions of the request change all
10596 * the settings; this function ignores 'basis' when decoding these.
10597 *
98090482
NR
10598 * If 'loose' is true, this function ignores properties and values that it does
10599 * not understand, as a controller would want to do when interpreting
10600 * capabilities provided by a switch. If 'loose' is false, this function
10601 * treats unknown properties and values as an error, as a switch would want to
10602 * do when interpreting a configuration request made by a controller.
10603 *
d18cc1ee
AA
10604 * Returns 0 if successful, otherwise an OFPERR_* value.
10605 *
10606 * Returns error code OFPERR_OFPACFC_INVALID if the value of mask is not in
10607 * the valid range of mask.
10608 *
10609 * Returns error code OFPERR_OFPACFC_UNSUPPORTED if the configuration is not
10610 * supported.*/
98090482 10611enum ofperr
a930d4c5 10612ofputil_decode_set_async_config(const struct ofp_header *oh, bool loose,
5876b4db 10613 const struct ofputil_async_cfg *basis,
a930d4c5 10614 struct ofputil_async_cfg *ac)
98090482 10615{
0a2869d5
BP
10616 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
10617 enum ofpraw raw = ofpraw_pull_assert(&b);
98090482
NR
10618
10619 if (raw == OFPRAW_OFPT13_SET_ASYNC ||
10620 raw == OFPRAW_NXT_SET_ASYNC_CONFIG ||
10621 raw == OFPRAW_OFPT13_GET_ASYNC_REPLY) {
10622 const struct nx_async_config *msg = ofpmsg_body(oh);
10623
5876b4db 10624 *ac = OFPUTIL_ASYNC_CFG_INIT;
8fd0bb60
BP
10625 decode_legacy_async_masks(msg->packet_in_mask, OAM_PACKET_IN,
10626 oh->version, ac);
10627 decode_legacy_async_masks(msg->port_status_mask, OAM_PORT_STATUS,
10628 oh->version, ac);
10629 decode_legacy_async_masks(msg->flow_removed_mask, OAM_FLOW_REMOVED,
10630 oh->version, ac);
98090482 10631 } else if (raw == OFPRAW_OFPT14_SET_ASYNC ||
af7bc161
BP
10632 raw == OFPRAW_OFPT14_GET_ASYNC_REPLY ||
10633 raw == OFPRAW_NXT_SET_ASYNC_CONFIG2) {
5876b4db 10634 *ac = *basis;
98090482 10635 while (b.size > 0) {
98090482
NR
10636 struct ofpbuf property;
10637 enum ofperr error;
34a543e3 10638 uint64_t type;
98090482 10639
34a543e3 10640 error = ofpprop_pull__(&b, &property, 8, 0xfffe, &type);
98090482
NR
10641 if (error) {
10642 return error;
10643 }
10644
8fd0bb60
BP
10645 const struct ofp14_async_prop *ap
10646 = get_ofp14_async_config_prop_by_prop_type(type);
10647 error = (ap
10648 ? parse_async_tlv(&property, ap, ac, oh->version, loose)
10649 : OFPPROP_UNKNOWN(loose, "async config", type));
b611d3ac 10650 if (error) {
8fd0bb60
BP
10651 /* Most messages use OFPBPC_BAD_TYPE but async has its own (who
10652 * knows why, it's OpenFlow. */
10653 if (error == OFPERR_OFPBPC_BAD_TYPE) {
10654 error = OFPERR_OFPACFC_UNSUPPORTED;
d18cc1ee 10655 }
98090482
NR
10656 return error;
10657 }
10658 }
10659 } else {
10660 return OFPERR_OFPBRC_BAD_VERSION;
10661 }
10662 return 0;
10663}
10664
8fd0bb60
BP
10665static void
10666encode_legacy_async_masks(const struct ofputil_async_cfg *ac,
10667 enum ofputil_async_msg_type oam,
10668 enum ofp_version version,
10669 ovs_be32 masks[2])
98090482 10670{
8fd0bb60
BP
10671 for (int i = 0; i < 2; i++) {
10672 bool master = i == 0;
10673 const struct ofp14_async_prop *ap
10674 = get_ofp14_async_config_prop_by_oam(oam, master);
10675 masks[i] = encode_async_mask(ac, ap, version);
10676 }
10677}
98090482 10678
8fd0bb60
BP
10679static void
10680ofputil_put_async_config__(const struct ofputil_async_cfg *ac,
10681 struct ofpbuf *buf, bool tlv,
10682 enum ofp_version version, uint32_t oams)
10683{
10684 if (!tlv) {
10685 struct nx_async_config *msg = ofpbuf_put_zeros(buf, sizeof *msg);
10686 encode_legacy_async_masks(ac, OAM_PACKET_IN, version,
10687 msg->packet_in_mask);
10688 encode_legacy_async_masks(ac, OAM_PORT_STATUS, version,
10689 msg->port_status_mask);
10690 encode_legacy_async_masks(ac, OAM_FLOW_REMOVED, version,
10691 msg->flow_removed_mask);
10692 } else {
10693 FOR_EACH_ASYNC_PROP (ap) {
10694 if (oams & (1u << ap->oam)) {
10695 size_t ofs = buf->size;
10696 ofpprop_put_be32(buf, ap->prop_type,
10697 encode_async_mask(ac, ap, version));
10698
10699 /* For experimenter properties, we need to use type 0xfffe for
10700 * master and 0xffff for slaves. */
10701 if (ofpprop_is_experimenter(ap->prop_type)) {
10702 struct ofp_prop_experimenter *ope
10703 = ofpbuf_at_assert(buf, ofs, sizeof *ope);
10704 ope->type = ap->master ? htons(0xffff) : htons(0xfffe);
10705 }
10706 }
98090482 10707 }
98090482 10708 }
98090482
NR
10709}
10710
8fd0bb60
BP
10711/* Encodes and returns a reply to the OFPT_GET_ASYNC_REQUEST in 'oh' that
10712 * states that the asynchronous message configuration is 'ac'. */
98090482 10713struct ofpbuf *
af7bc161
BP
10714ofputil_encode_get_async_reply(const struct ofp_header *oh,
10715 const struct ofputil_async_cfg *ac)
98090482
NR
10716{
10717 struct ofpbuf *buf;
98090482 10718
8fd0bb60
BP
10719 enum ofpraw raw = (oh->version < OFP14_VERSION
10720 ? OFPRAW_OFPT13_GET_ASYNC_REPLY
10721 : OFPRAW_OFPT14_GET_ASYNC_REPLY);
10722 struct ofpbuf *reply = ofpraw_alloc_reply(raw, oh, 0);
10723 ofputil_put_async_config__(ac, reply,
10724 raw == OFPRAW_OFPT14_GET_ASYNC_REPLY,
10725 oh->version, UINT32_MAX);
10726 return reply;
98090482
NR
10727
10728 return buf;
10729}
a930d4c5 10730
af7bc161
BP
10731/* Encodes and returns a message, in a format appropriate for OpenFlow version
10732 * 'ofp_version', that sets the asynchronous message configuration to 'ac'.
10733 *
10734 * Specify 'oams' as a bitmap of OAM_* that indicate the asynchronous messages
10735 * to configure. OF1.0 through OF1.3 can't natively configure a subset of
10736 * messages, so more messages than requested may be configured. OF1.0 through
10737 * OF1.3 also can't configure OVS extension OAM_* values, so if 'oam' includes
10738 * any extensions then this function encodes an Open vSwitch extension message
10739 * that does support configuring OVS extension OAM_*. */
10740struct ofpbuf *
10741ofputil_encode_set_async_config(const struct ofputil_async_cfg *ac,
10742 uint32_t oams, enum ofp_version ofp_version)
10743{
10744 enum ofpraw raw = (ofp_version >= OFP14_VERSION ? OFPRAW_OFPT14_SET_ASYNC
10745 : oams & OAM_EXTENSIONS ? OFPRAW_NXT_SET_ASYNC_CONFIG2
10746 : ofp_version >= OFP13_VERSION ? OFPRAW_OFPT13_SET_ASYNC
10747 : OFPRAW_NXT_SET_ASYNC_CONFIG);
10748 struct ofpbuf *request = ofpraw_alloc(raw, ofp_version, 0);
10749 ofputil_put_async_config__(ac, request,
10750 (raw == OFPRAW_OFPT14_SET_ASYNC ||
10751 raw == OFPRAW_NXT_SET_ASYNC_CONFIG2),
10752 ofp_version, oams);
10753 return request;
10754}
10755
a930d4c5
BP
10756struct ofputil_async_cfg
10757ofputil_async_cfg_default(enum ofp_version version)
10758{
9bfe9334
BP
10759 /* We enable all of the OF1.4 reasons regardless of 'version' because the
10760 * reasons added in OF1.4 just are just refinements of the OFPR_ACTION
10761 * introduced in OF1.0, breaking it into more specific categories. When we
10762 * encode these for earlier OpenFlow versions, we translate them into
10763 * OFPR_ACTION. */
10764 uint32_t pin = OFPR14_BITS & ~(1u << OFPR_INVALID_TTL);
10765 pin |= 1u << OFPR_EXPLICIT_MISS;
10766 if (version <= OFP12_VERSION) {
10767 pin |= 1u << OFPR_IMPLICIT_MISS;
10768 }
10769
a930d4c5 10770 return (struct ofputil_async_cfg) {
9bfe9334 10771 .master[OAM_PACKET_IN] = pin,
a930d4c5
BP
10772
10773 .master[OAM_FLOW_REMOVED]
10774 = (version >= OFP14_VERSION ? OFPRR14_BITS : OFPRR10_BITS),
10775
10776 .master[OAM_PORT_STATUS] = OFPPR_BITS,
10777 .slave[OAM_PORT_STATUS] = OFPPR_BITS,
10778 };
10779}
6c6eedc5
SJ
10780
10781static void
10782ofputil_put_ofp14_table_desc(const struct ofputil_table_desc *td,
10783 struct ofpbuf *b, enum ofp_version version)
10784{
10785 struct ofp14_table_desc *otd;
10786 struct ofp14_table_mod_prop_vacancy *otv;
10787 size_t start_otd;
10788
10789 start_otd = b->size;
10790 ofpbuf_put_zeros(b, sizeof *otd);
10791
10792 ofpprop_put_u32(b, OFPTMPT14_EVICTION, td->eviction_flags);
10793
10794 otv = ofpbuf_put_zeros(b, sizeof *otv);
10795 otv->type = htons(OFPTMPT14_VACANCY);
10796 otv->length = htons(sizeof *otv);
10797 otv->vacancy_down = td->table_vacancy.vacancy_down;
10798 otv->vacancy_up = td->table_vacancy.vacancy_up;
10799 otv->vacancy = td->table_vacancy.vacancy;
10800
10801 otd = ofpbuf_at_assert(b, start_otd, sizeof *otd);
10802 otd->length = htons(b->size - start_otd);
10803 otd->table_id = td->table_id;
10804 otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
10805 td->eviction, td->vacancy,
10806 version);
10807}
10808
10809/* Converts the abstract form of a "table status" message in '*ts' into an
10810 * OpenFlow message suitable for 'protocol', and returns that encoded form in
10811 * a buffer owned by the caller. */
10812struct ofpbuf *
10813ofputil_encode_table_status(const struct ofputil_table_status *ts,
10814 enum ofputil_protocol protocol)
10815{
10816 enum ofp_version version;
10817 struct ofpbuf *b;
10818
10819 version = ofputil_protocol_to_ofp_version(protocol);
10820 if (version >= OFP14_VERSION) {
10821 enum ofpraw raw;
10822 struct ofp14_table_status *ots;
10823
10824 raw = OFPRAW_OFPT14_TABLE_STATUS;
10825 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
10826 ots = ofpbuf_put_zeros(b, sizeof *ots);
10827 ots->reason = ts->reason;
10828 ofputil_put_ofp14_table_desc(&ts->desc, b, version);
10829 ofpmsg_update_length(b);
10830 return b;
10831 } else {
10832 return NULL;
10833 }
10834}
10835
10836/* Decodes the OpenFlow "table status" message in '*ots' into an abstract form
10837 * in '*ts'. Returns 0 if successful, otherwise an OFPERR_* value. */
10838enum ofperr
10839ofputil_decode_table_status(const struct ofp_header *oh,
10840 struct ofputil_table_status *ts)
10841{
10842 const struct ofp14_table_status *ots;
10843 struct ofpbuf b;
10844 enum ofperr error;
10845 enum ofpraw raw;
10846
10847 ofpbuf_use_const(&b, oh, ntohs(oh->length));
10848 raw = ofpraw_pull_assert(&b);
10849 ots = ofpbuf_pull(&b, sizeof *ots);
10850
10851 if (raw == OFPRAW_OFPT14_TABLE_STATUS) {
10852 if (ots->reason != OFPTR_VACANCY_DOWN
10853 && ots->reason != OFPTR_VACANCY_UP) {
10854 return OFPERR_OFPBPC_BAD_VALUE;
10855 }
10856 ts->reason = ots->reason;
10857
10858 error = ofputil_decode_table_desc(&b, &ts->desc, oh->version);
10859 return error;
10860 } else {
10861 return OFPERR_OFPBRC_BAD_VERSION;
10862 }
10863
10864 return 0;
10865}