]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ofp-util.c
ofp-util: Add OpenFlow 1.5 packet-out support
[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);
577bfa9f
YHW
4208 if (raw == OFPRAW_OFPT15_PACKET_OUT) {
4209 enum ofperr error;
4210 const struct ofp15_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
4211
4212 po->buffer_id = ntohl(opo->buffer_id);
4213 error = oxm_pull_match_loose(&b, NULL, &po->flow_metadata);
4214 if (error) {
4215 return error;
4216 }
4217
4218 if (!po->flow_metadata.wc.masks.in_port.ofp_port) {
4219 return OFPERR_OFPBRC_BAD_PORT;
4220 }
4221
4222 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
4223 oh->version, NULL, NULL,
4224 ofpacts);
4225 if (error) {
4226 return error;
4227 }
4228 } else if (raw == OFPRAW_OFPT11_PACKET_OUT) {
eb5ee596 4229 enum ofperr error;
35eb6326 4230 ofp_port_t in_port;
eb5ee596
SH
4231 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
4232
4233 po->buffer_id = ntohl(opo->buffer_id);
35eb6326 4234 error = ofputil_port_from_ofp11(opo->in_port, &in_port);
eb5ee596
SH
4235 if (error) {
4236 return error;
4237 }
35eb6326 4238 match_set_in_port(&po->flow_metadata, in_port);
eb5ee596 4239
e3f8f887 4240 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
5c7c16d8
YHW
4241 oh->version, NULL, NULL,
4242 ofpacts);
eb5ee596
SH
4243 if (error) {
4244 return error;
4245 }
eb5ee596 4246 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
8a6bc7cd 4247 enum ofperr error;
31a9e63f 4248 const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
8a6bc7cd
SH
4249
4250 po->buffer_id = ntohl(opo->buffer_id);
35eb6326 4251 match_set_in_port(&po->flow_metadata, u16_to_ofp(ntohs(opo->in_port)));
8a6bc7cd 4252
e3f8f887 4253 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
5c7c16d8
YHW
4254 oh->version, NULL, NULL,
4255 ofpacts);
8a6bc7cd
SH
4256 if (error) {
4257 return error;
4258 }
4259 } else {
428b2edd 4260 OVS_NOT_REACHED();
8a6bc7cd
SH
4261 }
4262
35eb6326
YHW
4263 ofp_port_t in_port = po->flow_metadata.flow.in_port.ofp_port;
4264 if (ofp_to_u16(in_port) >= ofp_to_u16(OFPP_MAX)
4265 && in_port != OFPP_LOCAL
4266 && in_port != OFPP_NONE
4267 && in_port != OFPP_CONTROLLER) {
94783c7c 4268 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx32,
35eb6326 4269 po->flow_metadata.flow.in_port.ofp_port);
2e1bfcb6 4270 return OFPERR_OFPBRC_BAD_PORT;
c6a93eb7
BP
4271 }
4272
6fd6ed71
PS
4273 po->ofpacts = ofpacts->data;
4274 po->ofpacts_len = ofpacts->size;
c6a93eb7
BP
4275
4276 if (po->buffer_id == UINT32_MAX) {
6fd6ed71
PS
4277 po->packet = b.data;
4278 po->packet_len = b.size;
c6a93eb7
BP
4279 } else {
4280 po->packet = NULL;
4281 po->packet_len = 0;
4282 }
4283
4284 return 0;
4285}
6c038611
BP
4286\f
4287/* ofputil_phy_port */
4288
4289/* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
4290BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
4291BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
4292BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
4293BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
4294BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
4295BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
4296BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
4297
4298/* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
9e1fd49b
BP
4299BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
4300BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
4301BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
4302BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
4303BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
6c038611 4304
9e1fd49b
BP
4305static enum netdev_features
4306netdev_port_features_from_ofp10(ovs_be32 ofp10_)
6c038611
BP
4307{
4308 uint32_t ofp10 = ntohl(ofp10_);
4309 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
4310}
4311
9e1fd49b
BP
4312static ovs_be32
4313netdev_port_features_to_ofp10(enum netdev_features features)
6c038611
BP
4314{
4315 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
4316}
c6a93eb7 4317
9e1fd49b
BP
4318BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
4319BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
4320BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
4321BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
4322BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
4323BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
4324BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
4325BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
4326BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
4327BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
4328BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
4329BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
4330BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
4331BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
4332BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
4333BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
4334
4335static enum netdev_features
4336netdev_port_features_from_ofp11(ovs_be32 ofp11)
4337{
4338 return ntohl(ofp11) & 0xffff;
4339}
4340
4341static ovs_be32
4342netdev_port_features_to_ofp11(enum netdev_features features)
4343{
4344 return htonl(features & 0xffff);
4345}
4346
4347static enum ofperr
4348ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
4349 const struct ofp10_phy_port *opp)
4350{
4e022ec0 4351 pp->port_no = u16_to_ofp(ntohs(opp->port_no));
74ff3298 4352 pp->hw_addr = opp->hw_addr;
f9ac0f03 4353 ovs_strlcpy_arrays(pp->name, opp->name);
9e1fd49b
BP
4354
4355 pp->config = ntohl(opp->config) & OFPPC10_ALL;
4356 pp->state = ntohl(opp->state) & OFPPS10_ALL;
4357
4358 pp->curr = netdev_port_features_from_ofp10(opp->curr);
4359 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
4360 pp->supported = netdev_port_features_from_ofp10(opp->supported);
4361 pp->peer = netdev_port_features_from_ofp10(opp->peer);
4362
d02a5f8e
BP
4363 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
4364 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
9e1fd49b
BP
4365
4366 return 0;
4367}
4368
4369static enum ofperr
4370ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
4371 const struct ofp11_port *op)
4372{
4373 enum ofperr error;
4374
9e1fd49b
BP
4375 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
4376 if (error) {
4377 return error;
4378 }
74ff3298 4379 pp->hw_addr = op->hw_addr;
f9ac0f03 4380 ovs_strlcpy_arrays(pp->name, op->name);
9e1fd49b
BP
4381
4382 pp->config = ntohl(op->config) & OFPPC11_ALL;
646f2b37 4383 pp->state = ntohl(op->state) & OFPPS11_ALL;
9e1fd49b
BP
4384
4385 pp->curr = netdev_port_features_from_ofp11(op->curr);
4386 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
4387 pp->supported = netdev_port_features_from_ofp11(op->supported);
4388 pp->peer = netdev_port_features_from_ofp11(op->peer);
4389
4390 pp->curr_speed = ntohl(op->curr_speed);
4391 pp->max_speed = ntohl(op->max_speed);
4392
4393 return 0;
4394}
4395
8c3cc785
BP
4396static enum ofperr
4397parse_ofp14_port_ethernet_property(const struct ofpbuf *payload,
4398 struct ofputil_phy_port *pp)
4399{
6fd6ed71 4400 struct ofp14_port_desc_prop_ethernet *eth = payload->data;
8c3cc785 4401
6fd6ed71 4402 if (payload->size != sizeof *eth) {
8c3cc785
BP
4403 return OFPERR_OFPBPC_BAD_LEN;
4404 }
4405
4406 pp->curr = netdev_port_features_from_ofp11(eth->curr);
4407 pp->advertised = netdev_port_features_from_ofp11(eth->advertised);
4408 pp->supported = netdev_port_features_from_ofp11(eth->supported);
4409 pp->peer = netdev_port_features_from_ofp11(eth->peer);
4410
4411 pp->curr_speed = ntohl(eth->curr_speed);
4412 pp->max_speed = ntohl(eth->max_speed);
4413
4414 return 0;
4415}
4416
4417static enum ofperr
2f2b904f
BP
4418ofputil_pull_ofp14_port_properties(const void *props, size_t len,
4419 struct ofputil_phy_port *pp)
8c3cc785 4420{
2f2b904f 4421 struct ofpbuf properties = ofpbuf_const_initializer(props, len);
6fd6ed71 4422 while (properties.size > 0) {
8c3cc785
BP
4423 struct ofpbuf payload;
4424 enum ofperr error;
34a543e3 4425 uint64_t type;
8c3cc785 4426
c5562271 4427 error = ofpprop_pull(&properties, &payload, &type);
8c3cc785
BP
4428 if (error) {
4429 return error;
4430 }
4431
4432 switch (type) {
4433 case OFPPDPT14_ETHERNET:
4434 error = parse_ofp14_port_ethernet_property(&payload, pp);
4435 break;
4436
4437 default:
34a543e3 4438 error = OFPPROP_UNKNOWN(true, "port", type);
8c3cc785
BP
4439 break;
4440 }
4441
4442 if (error) {
4443 return error;
4444 }
4445 }
4446
4447 return 0;
4448}
4449
2f2b904f
BP
4450static enum ofperr
4451ofputil_pull_ofp14_port(struct ofputil_phy_port *pp, struct ofpbuf *msg)
4452{
4453 const struct ofp14_port *op = ofpbuf_try_pull(msg, sizeof *op);
4454 if (!op) {
4455 return OFPERR_OFPBRC_BAD_LEN;
4456 }
4457
4458 size_t len = ntohs(op->length);
4459 if (len < sizeof *op || len - sizeof *op > msg->size) {
4460 return OFPERR_OFPBRC_BAD_LEN;
4461 }
4462 len -= sizeof *op;
4463
4464 enum ofperr error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
4465 if (error) {
4466 return error;
4467 }
4468 pp->hw_addr = op->hw_addr;
4469 ovs_strlcpy_arrays(pp->name, op->name);
4470
4471 pp->config = ntohl(op->config) & OFPPC11_ALL;
4472 pp->state = ntohl(op->state) & OFPPS11_ALL;
4473
4474 return ofputil_pull_ofp14_port_properties(ofpbuf_pull(msg, len), len, pp);
4475}
4476
4477static enum ofperr
4478ofputil_pull_ofp16_port(struct ofputil_phy_port *pp, struct ofpbuf *msg)
4479{
4480 const struct ofp16_port *op = ofpbuf_try_pull(msg, sizeof *op);
4481 if (!op) {
4482 return OFPERR_OFPBRC_BAD_LEN;
4483 }
4484
4485 size_t len = ntohs(op->length);
4486 if (len < sizeof *op || len - sizeof *op > msg->size) {
4487 return OFPERR_OFPBRC_BAD_LEN;
4488 }
4489 len -= sizeof *op;
4490
4491 enum ofperr error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
4492 if (error) {
4493 return error;
4494 }
4495 if (op->hw_addr_type & htons(OFPPHAT16_EUI48)) {
4496 pp->hw_addr = op->hw_addr;
4497 }
4498 if (op->hw_addr_type & htons(OFPPHAT16_EUI64)) {
4499 pp->hw_addr64 = op->hw_addr64;
4500 }
4501 ovs_strlcpy_arrays(pp->name, op->name);
4502
4503 pp->config = ntohl(op->config) & OFPPC11_ALL;
4504 pp->state = ntohl(op->state) & OFPPS11_ALL;
4505
4506 return ofputil_pull_ofp14_port_properties(ofpbuf_pull(msg, len), len, pp);
4507}
4508
9e1fd49b
BP
4509static void
4510ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
4511 struct ofp10_phy_port *opp)
4512{
4513 memset(opp, 0, sizeof *opp);
4514
4e022ec0 4515 opp->port_no = htons(ofp_to_u16(pp->port_no));
74ff3298 4516 opp->hw_addr = pp->hw_addr;
f9ac0f03 4517 ovs_strlcpy_arrays(opp->name, pp->name);
9e1fd49b
BP
4518
4519 opp->config = htonl(pp->config & OFPPC10_ALL);
4520 opp->state = htonl(pp->state & OFPPS10_ALL);
4521
4522 opp->curr = netdev_port_features_to_ofp10(pp->curr);
4523 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
4524 opp->supported = netdev_port_features_to_ofp10(pp->supported);
4525 opp->peer = netdev_port_features_to_ofp10(pp->peer);
4526}
4527
4528static void
4529ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
4530 struct ofp11_port *op)
4531{
4532 memset(op, 0, sizeof *op);
4533
4534 op->port_no = ofputil_port_to_ofp11(pp->port_no);
74ff3298 4535 op->hw_addr = pp->hw_addr;
f9ac0f03 4536 ovs_strlcpy_arrays(op->name, pp->name);
9e1fd49b
BP
4537
4538 op->config = htonl(pp->config & OFPPC11_ALL);
4539 op->state = htonl(pp->state & OFPPS11_ALL);
4540
4541 op->curr = netdev_port_features_to_ofp11(pp->curr);
4542 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
4543 op->supported = netdev_port_features_to_ofp11(pp->supported);
4544 op->peer = netdev_port_features_to_ofp11(pp->peer);
4545
4546 op->curr_speed = htonl(pp->curr_speed);
4547 op->max_speed = htonl(pp->max_speed);
4548}
4549
8c3cc785 4550static void
2f2b904f
BP
4551ofputil_encode_ofp14_port_ethernet_prop(
4552 const struct ofputil_phy_port *pp,
4553 struct ofp14_port_desc_prop_ethernet *eth)
4554{
4555 eth->curr = netdev_port_features_to_ofp11(pp->curr);
4556 eth->advertised = netdev_port_features_to_ofp11(pp->advertised);
4557 eth->supported = netdev_port_features_to_ofp11(pp->supported);
4558 eth->peer = netdev_port_features_to_ofp11(pp->peer);
4559 eth->curr_speed = htonl(pp->curr_speed);
4560 eth->max_speed = htonl(pp->max_speed);
4561}
4562
4563static void
4564ofputil_put_ofp14_port(const struct ofputil_phy_port *pp, struct ofpbuf *b)
8c3cc785
BP
4565{
4566 struct ofp14_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);
74ff3298 4574 op->hw_addr = pp->hw_addr;
2f2b904f 4575 ovs_strlcpy_arrays(op->name, pp->name);
8c3cc785
BP
4576 op->config = htonl(pp->config & OFPPC11_ALL);
4577 op->state = htonl(pp->state & OFPPS11_ALL);
4578
303721ee 4579 eth = ofpprop_put_zeros(b, OFPPDPT14_ETHERNET, sizeof *eth);
2f2b904f
BP
4580 ofputil_encode_ofp14_port_ethernet_prop(pp, eth);
4581}
4582
4583static void
4584ofputil_put_ofp16_port(const struct ofputil_phy_port *pp, struct ofpbuf *b)
4585{
4586 struct ofp16_port *op;
4587 struct ofp14_port_desc_prop_ethernet *eth;
4588
4589 ofpbuf_prealloc_tailroom(b, sizeof *op + sizeof *eth);
4590
4591 op = ofpbuf_put_zeros(b, sizeof *op);
4592 op->port_no = ofputil_port_to_ofp11(pp->port_no);
4593 op->length = htons(sizeof *op + sizeof *eth);
4594 if (!eth_addr_is_zero(pp->hw_addr)) {
4595 op->hw_addr_type |= htons(OFPPHAT16_EUI48);
4596 op->hw_addr = pp->hw_addr;
4597 }
4598 if (!eth_addr64_is_zero(pp->hw_addr64)) {
4599 op->hw_addr_type |= htons(OFPPHAT16_EUI64);
4600 op->hw_addr64 = pp->hw_addr64;
4601 }
4602 ovs_strlcpy_arrays(op->name, pp->name);
4603 op->config = htonl(pp->config & OFPPC11_ALL);
4604 op->state = htonl(pp->state & OFPPS11_ALL);
4605
4606 eth = ofpprop_put_zeros(b, OFPPDPT14_ETHERNET, sizeof *eth);
4607 ofputil_encode_ofp14_port_ethernet_prop(pp, eth);
8c3cc785
BP
4608}
4609
9e1fd49b 4610static void
2e3fa633
SH
4611ofputil_put_phy_port(enum ofp_version ofp_version,
4612 const struct ofputil_phy_port *pp, struct ofpbuf *b)
9e1fd49b 4613{
2e3fa633
SH
4614 switch (ofp_version) {
4615 case OFP10_VERSION: {
6b0f20ac
BP
4616 struct ofp10_phy_port *opp = ofpbuf_put_uninit(b, sizeof *opp);
4617 ofputil_encode_ofp10_phy_port(pp, opp);
2e3fa633
SH
4618 break;
4619 }
4620
4621 case OFP11_VERSION:
2e1ae200
JR
4622 case OFP12_VERSION:
4623 case OFP13_VERSION: {
6b0f20ac
BP
4624 struct ofp11_port *op = ofpbuf_put_uninit(b, sizeof *op);
4625 ofputil_encode_ofp11_port(pp, op);
2e3fa633
SH
4626 break;
4627 }
4628
c37c0382 4629 case OFP14_VERSION:
42dccab5 4630 case OFP15_VERSION:
8c3cc785 4631 ofputil_put_ofp14_port(pp, b);
c37c0382 4632 break;
2f2b904f
BP
4633 case OFP16_VERSION:
4634 ofputil_put_ofp16_port(pp, b);
4635 break;
c37c0382 4636
2e3fa633 4637 default:
428b2edd 4638 OVS_NOT_REACHED();
9e1fd49b
BP
4639 }
4640}
2be393ed 4641
70ae4f93
BP
4642enum ofperr
4643ofputil_decode_port_desc_stats_request(const struct ofp_header *request,
4644 ofp_port_t *port)
4645{
0a2869d5
BP
4646 struct ofpbuf b = ofpbuf_const_initializer(request,
4647 ntohs(request->length));
4648 enum ofpraw raw = ofpraw_pull_assert(&b);
70ae4f93
BP
4649 if (raw == OFPRAW_OFPST10_PORT_DESC_REQUEST) {
4650 *port = OFPP_ANY;
4651 return 0;
4652 } else if (raw == OFPRAW_OFPST15_PORT_DESC_REQUEST) {
4653 ovs_be32 *ofp11_port;
4654
4655 ofp11_port = ofpbuf_pull(&b, sizeof *ofp11_port);
4656 return ofputil_port_from_ofp11(*ofp11_port, port);
4657 } else {
4658 OVS_NOT_REACHED();
4659 }
4660}
4661
4662struct ofpbuf *
4663ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version,
4664 ofp_port_t port)
4665{
4666 struct ofpbuf *request;
70ae4f93
BP
4667
4668 switch (ofp_version) {
4669 case OFP10_VERSION:
4670 case OFP11_VERSION:
4671 case OFP12_VERSION:
4672 case OFP13_VERSION:
4673 case OFP14_VERSION:
4674 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST,
4675 ofp_version, 0);
4676 break;
b79d45a1
BP
4677 case OFP15_VERSION:
4678 case OFP16_VERSION:{
7448d548 4679 struct ofp15_port_desc_request *req;
70ae4f93
BP
4680 request = ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST,
4681 ofp_version, 0);
7448d548
MT
4682 req = ofpbuf_put_zeros(request, sizeof *req);
4683 req->port_no = ofputil_port_to_ofp11(port);
70ae4f93 4684 break;
7448d548 4685 }
70ae4f93
BP
4686 default:
4687 OVS_NOT_REACHED();
4688 }
4689
4690 return request;
4691}
4692
2be393ed 4693void
e28ac5cf 4694ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
ca6ba700 4695 struct ovs_list *replies)
2be393ed 4696{
417e7e66 4697 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
6fd6ed71 4698 size_t start_ofs = reply->size;
c37c0382 4699
6b0f20ac
BP
4700 ofputil_put_phy_port(ofpmp_version(replies), pp, reply);
4701 ofpmp_postappend(replies, start_ofs);
2be393ed 4702}
9e1fd49b 4703\f
ad99e2ed
BP
4704/* ofputil_switch_config */
4705
4706/* Decodes 'oh', which must be an OFPT_GET_CONFIG_REPLY or OFPT_SET_CONFIG
4707 * message, into 'config'. Returns false if 'oh' contained any flags that
4708 * aren't specified in its version of OpenFlow, true otherwise. */
4709static bool
4710ofputil_decode_switch_config(const struct ofp_header *oh,
4711 struct ofputil_switch_config *config)
4712{
0a2869d5 4713 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
ad99e2ed 4714 ofpraw_pull_assert(&b);
ad99e2ed 4715
0a2869d5 4716 const struct ofp_switch_config *osc = ofpbuf_pull(&b, sizeof *osc);
ad99e2ed
BP
4717 config->frag = ntohs(osc->flags) & OFPC_FRAG_MASK;
4718 config->miss_send_len = ntohs(osc->miss_send_len);
4719
4720 ovs_be16 valid_mask = htons(OFPC_FRAG_MASK);
4721 if (oh->version < OFP13_VERSION) {
4722 const ovs_be16 ttl_bit = htons(OFPC_INVALID_TTL_TO_CONTROLLER);
4723 valid_mask |= ttl_bit;
4724 config->invalid_ttl_to_controller = (osc->flags & ttl_bit) != 0;
4725 } else {
4726 config->invalid_ttl_to_controller = -1;
4727 }
4728
4729 return !(osc->flags & ~valid_mask);
4730}
4731
4732void
4733ofputil_decode_get_config_reply(const struct ofp_header *oh,
4734 struct ofputil_switch_config *config)
4735{
4736 ofputil_decode_switch_config(oh, config);
4737}
4738
4739enum ofperr
4740ofputil_decode_set_config(const struct ofp_header *oh,
4741 struct ofputil_switch_config *config)
4742{
4743 return (ofputil_decode_switch_config(oh, config)
4744 ? 0
4745 : OFPERR_OFPSCFC_BAD_FLAGS);
4746}
4747
4748static struct ofpbuf *
4749ofputil_put_switch_config(const struct ofputil_switch_config *config,
4750 struct ofpbuf *b)
4751{
4752 const struct ofp_header *oh = b->data;
4753 struct ofp_switch_config *osc = ofpbuf_put_zeros(b, sizeof *osc);
4754 osc->flags = htons(config->frag);
4755 if (config->invalid_ttl_to_controller > 0 && oh->version < OFP13_VERSION) {
4756 osc->flags |= htons(OFPC_INVALID_TTL_TO_CONTROLLER);
4757 }
4758 osc->miss_send_len = htons(config->miss_send_len);
4759 return b;
4760}
4761
4762struct ofpbuf *
4763ofputil_encode_get_config_reply(const struct ofp_header *request,
4764 const struct ofputil_switch_config *config)
4765{
4766 struct ofpbuf *b = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY,
4767 request, 0);
4768 return ofputil_put_switch_config(config, b);
4769}
4770
4771struct ofpbuf *
4772ofputil_encode_set_config(const struct ofputil_switch_config *config,
4773 enum ofp_version version)
4774{
4775 struct ofpbuf *b = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, version, 0);
4776 return ofputil_put_switch_config(config, b);
4777}
4778\f
9e1fd49b
BP
4779/* ofputil_switch_features */
4780
4781#define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
60202987 4782 OFPC_IP_REASM | OFPC_QUEUE_STATS)
9e1fd49b
BP
4783BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
4784BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
4785BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
4786BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
4787BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
4788BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
50b73fe1
JR
4789BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_BLOCKED == OFPC12_PORT_BLOCKED);
4790BUILD_ASSERT_DECL((int) OFPUTIL_C_BUNDLES == OFPC14_BUNDLES);
4791BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_MONITORING == OFPC14_FLOW_MONITORING);
9e1fd49b 4792
60202987
SH
4793static uint32_t
4794ofputil_capabilities_mask(enum ofp_version ofp_version)
4795{
0746a84f 4796 /* Handle capabilities whose bit is unique for all OpenFlow versions */
60202987
SH
4797 switch (ofp_version) {
4798 case OFP10_VERSION:
4799 case OFP11_VERSION:
4800 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
4801 case OFP12_VERSION:
2e1ae200 4802 case OFP13_VERSION:
50b73fe1 4803 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
c37c0382 4804 case OFP14_VERSION:
42dccab5 4805 case OFP15_VERSION:
b79d45a1 4806 case OFP16_VERSION:
50b73fe1
JR
4807 return OFPC_COMMON | OFPC12_PORT_BLOCKED | OFPC14_BUNDLES
4808 | OFPC14_FLOW_MONITORING;
60202987
SH
4809 default:
4810 /* Caller needs to check osf->header.version itself */
4811 return 0;
4812 }
4813}
4814
667bb1fb
BP
4815/* Pulls an OpenFlow "switch_features" structure from 'b' and decodes it into
4816 * an abstract representation in '*features', readying 'b' to iterate over the
4817 * OpenFlow port structures following 'osf' with later calls to
4818 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an OFPERR_*
4819 * value. */
9e1fd49b 4820enum ofperr
667bb1fb
BP
4821ofputil_pull_switch_features(struct ofpbuf *b,
4822 struct ofputil_switch_features *features)
9e1fd49b 4823{
667bb1fb
BP
4824 const struct ofp_header *oh = b->data;
4825 enum ofpraw raw = ofpraw_pull_assert(b);
4826 const struct ofp_switch_features *osf = ofpbuf_pull(b, sizeof *osf);
9e1fd49b
BP
4827 features->datapath_id = ntohll(osf->datapath_id);
4828 features->n_buffers = ntohl(osf->n_buffers);
4829 features->n_tables = osf->n_tables;
2e1ae200 4830 features->auxiliary_id = 0;
9e1fd49b 4831
60202987
SH
4832 features->capabilities = ntohl(osf->capabilities) &
4833 ofputil_capabilities_mask(oh->version);
9e1fd49b 4834
982697a4 4835 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
9e1fd49b
BP
4836 if (osf->capabilities & htonl(OFPC10_STP)) {
4837 features->capabilities |= OFPUTIL_C_STP;
4838 }
08d1e234
BP
4839 features->ofpacts = ofpact_bitmap_from_openflow(osf->actions,
4840 OFP10_VERSION);
2e1ae200
JR
4841 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
4842 || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
9e1fd49b
BP
4843 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
4844 features->capabilities |= OFPUTIL_C_GROUP_STATS;
4845 }
08d1e234 4846 features->ofpacts = 0;
2e1ae200
JR
4847 if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4848 features->auxiliary_id = osf->auxiliary_id;
4849 }
9e1fd49b
BP
4850 } else {
4851 return OFPERR_OFPBRC_BAD_VERSION;
4852 }
4853
4854 return 0;
4855}
4856
fca6d553
BP
4857/* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4858 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4859 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4860 *
4861 * Given a buffer 'b' that contains a Features Reply message, this message
4862 * checks if it contains a complete list of the switch's ports. Returns true,
4863 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4864 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4865 * message.
4866 *
4867 * When this function returns false, the caller should send an OFPST_PORT_DESC
4868 * stats request to get the ports. */
347b7ac4 4869bool
fca6d553 4870ofputil_switch_features_has_ports(struct ofpbuf *b)
347b7ac4 4871{
6fd6ed71 4872 struct ofp_header *oh = b->data;
13e1aff8 4873 size_t phy_port_size;
347b7ac4 4874
fca6d553 4875 if (oh->version >= OFP13_VERSION) {
13e1aff8 4876 /* OpenFlow 1.3+ never has ports in the feature reply. */
fca6d553 4877 return false;
13e1aff8
BP
4878 }
4879
4880 phy_port_size = (oh->version == OFP10_VERSION
4881 ? sizeof(struct ofp10_phy_port)
4882 : sizeof(struct ofp11_port));
4883 if (ntohs(oh->length) + phy_port_size <= UINT16_MAX) {
4884 /* There's room for additional ports in the feature reply.
4885 * Assume that the list is complete. */
347b7ac4
JP
4886 return true;
4887 }
13e1aff8
BP
4888
4889 /* The feature reply has no room for more ports. Probably the list is
4890 * truncated. Drop the ports and tell the caller to retrieve them with
4891 * OFPST_PORT_DESC. */
6fd6ed71 4892 b->size = sizeof *oh + sizeof(struct ofp_switch_features);
13e1aff8
BP
4893 ofpmsg_update_length(b);
4894 return false;
347b7ac4
JP
4895}
4896
9e1fd49b
BP
4897/* Returns a buffer owned by the caller that encodes 'features' in the format
4898 * required by 'protocol' with the given 'xid'. The caller should append port
4899 * information to the buffer with subsequent calls to
4900 * ofputil_put_switch_features_port(). */
4901struct ofpbuf *
4902ofputil_encode_switch_features(const struct ofputil_switch_features *features,
4903 enum ofputil_protocol protocol, ovs_be32 xid)
4904{
4905 struct ofp_switch_features *osf;
4906 struct ofpbuf *b;
2e3fa633
SH
4907 enum ofp_version version;
4908 enum ofpraw raw;
982697a4
BP
4909
4910 version = ofputil_protocol_to_ofp_version(protocol);
2e3fa633
SH
4911 switch (version) {
4912 case OFP10_VERSION:
4913 raw = OFPRAW_OFPT10_FEATURES_REPLY;
4914 break;
4915 case OFP11_VERSION:
4916 case OFP12_VERSION:
4917 raw = OFPRAW_OFPT11_FEATURES_REPLY;
4918 break;
2e1ae200 4919 case OFP13_VERSION:
c37c0382 4920 case OFP14_VERSION:
42dccab5 4921 case OFP15_VERSION:
b79d45a1 4922 case OFP16_VERSION:
2e1ae200
JR
4923 raw = OFPRAW_OFPT13_FEATURES_REPLY;
4924 break;
2e3fa633 4925 default:
428b2edd 4926 OVS_NOT_REACHED();
2e3fa633
SH
4927 }
4928 b = ofpraw_alloc_xid(raw, version, xid, 0);
982697a4 4929 osf = ofpbuf_put_zeros(b, sizeof *osf);
9e1fd49b
BP
4930 osf->datapath_id = htonll(features->datapath_id);
4931 osf->n_buffers = htonl(features->n_buffers);
4932 osf->n_tables = features->n_tables;
4933
60202987
SH
4934 osf->capabilities = htonl(features->capabilities &
4935 ofputil_capabilities_mask(version));
2e3fa633
SH
4936 switch (version) {
4937 case OFP10_VERSION:
9e1fd49b
BP
4938 if (features->capabilities & OFPUTIL_C_STP) {
4939 osf->capabilities |= htonl(OFPC10_STP);
4940 }
08d1e234
BP
4941 osf->actions = ofpact_bitmap_to_openflow(features->ofpacts,
4942 OFP10_VERSION);
2e3fa633 4943 break;
2e1ae200 4944 case OFP13_VERSION:
c37c0382 4945 case OFP14_VERSION:
42dccab5 4946 case OFP15_VERSION:
b79d45a1 4947 case OFP16_VERSION:
2e1ae200
JR
4948 osf->auxiliary_id = features->auxiliary_id;
4949 /* fall through */
2e3fa633
SH
4950 case OFP11_VERSION:
4951 case OFP12_VERSION:
9e1fd49b
BP
4952 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
4953 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
4954 }
2e3fa633
SH
4955 break;
4956 default:
428b2edd 4957 OVS_NOT_REACHED();
9e1fd49b
BP
4958 }
4959
4960 return b;
4961}
4962
4963/* Encodes 'pp' into the format required by the switch_features message already
4964 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4965 * and appends the encoded version to 'b'. */
4966void
4967ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
4968 struct ofpbuf *b)
4969{
6fd6ed71 4970 const struct ofp_header *oh = b->data;
9e1fd49b 4971
e0c91c0c 4972 if (oh->version < OFP13_VERSION) {
6b0f20ac
BP
4973 /* Try adding a port description to the message, but drop it again if
4974 * the buffer overflows. (This possibility for overflow is why
4975 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
6fd6ed71 4976 size_t start_ofs = b->size;
e0c91c0c 4977 ofputil_put_phy_port(oh->version, pp, b);
6fd6ed71
PS
4978 if (b->size > UINT16_MAX) {
4979 b->size = start_ofs;
6b0f20ac 4980 }
e0c91c0c 4981 }
9e1fd49b
BP
4982}
4983\f
4984/* ofputil_port_status */
4985
4986/* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4987 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4988enum ofperr
982697a4 4989ofputil_decode_port_status(const struct ofp_header *oh,
9e1fd49b
BP
4990 struct ofputil_port_status *ps)
4991{
0a2869d5 4992 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
982697a4 4993 ofpraw_pull_assert(&b);
982697a4 4994
0a2869d5 4995 const struct ofp_port_status *ops = ofpbuf_pull(&b, sizeof *ops);
9e1fd49b
BP
4996 if (ops->reason != OFPPR_ADD &&
4997 ops->reason != OFPPR_DELETE &&
4998 ops->reason != OFPPR_MODIFY) {
4999 return OFPERR_NXBRC_BAD_REASON;
5000 }
5001 ps->reason = ops->reason;
5002
0a2869d5 5003 int retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
cb22974d 5004 ovs_assert(retval != EOF);
9e1fd49b
BP
5005 return retval;
5006}
5007
5008/* Converts the abstract form of a "port status" message in '*ps' into an
5009 * OpenFlow message suitable for 'protocol', and returns that encoded form in
5010 * a buffer owned by the caller. */
5011struct ofpbuf *
5012ofputil_encode_port_status(const struct ofputil_port_status *ps,
5013 enum ofputil_protocol protocol)
5014{
5015 struct ofp_port_status *ops;
5016 struct ofpbuf *b;
2e3fa633
SH
5017 enum ofp_version version;
5018 enum ofpraw raw;
982697a4
BP
5019
5020 version = ofputil_protocol_to_ofp_version(protocol);
2e3fa633
SH
5021 switch (version) {
5022 case OFP10_VERSION:
5023 raw = OFPRAW_OFPT10_PORT_STATUS;
5024 break;
5025
5026 case OFP11_VERSION:
5027 case OFP12_VERSION:
2e1ae200 5028 case OFP13_VERSION:
2e3fa633
SH
5029 raw = OFPRAW_OFPT11_PORT_STATUS;
5030 break;
5031
8c3cc785 5032 case OFP14_VERSION:
42dccab5 5033 case OFP15_VERSION:
8c3cc785
BP
5034 raw = OFPRAW_OFPT14_PORT_STATUS;
5035 break;
5036
2f2b904f
BP
5037 case OFP16_VERSION:
5038 raw = OFPRAW_OFPT16_PORT_STATUS;
5039 break;
5040
2e3fa633 5041 default:
428b2edd 5042 OVS_NOT_REACHED();
2e3fa633
SH
5043 }
5044
5045 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
982697a4 5046 ops = ofpbuf_put_zeros(b, sizeof *ops);
9e1fd49b 5047 ops->reason = ps->reason;
982697a4
BP
5048 ofputil_put_phy_port(version, &ps->desc, b);
5049 ofpmsg_update_length(b);
9e1fd49b
BP
5050 return b;
5051}
918f2b82 5052
9e1fd49b
BP
5053/* ofputil_port_mod */
5054
18cc69d9
BP
5055static enum ofperr
5056parse_port_mod_ethernet_property(struct ofpbuf *property,
5057 struct ofputil_port_mod *pm)
5058{
b611d3ac
BP
5059 ovs_be32 advertise;
5060 enum ofperr error;
18cc69d9 5061
b611d3ac
BP
5062 error = ofpprop_parse_be32(property, &advertise);
5063 if (!error) {
5064 pm->advertise = netdev_port_features_from_ofp11(advertise);
18cc69d9 5065 }
b611d3ac 5066 return error;
18cc69d9
BP
5067}
5068
2f2b904f
BP
5069static enum ofperr
5070ofputil_decode_ofp10_port_mod(const struct ofp10_port_mod *opm,
5071 struct ofputil_port_mod *pm)
5072{
5073 pm->port_no = u16_to_ofp(ntohs(opm->port_no));
5074 pm->hw_addr = opm->hw_addr;
5075 pm->config = ntohl(opm->config) & OFPPC10_ALL;
5076 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
5077 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
5078 return 0;
5079}
5080
5081static enum ofperr
5082ofputil_decode_ofp11_port_mod(const struct ofp11_port_mod *opm,
5083 struct ofputil_port_mod *pm)
9e1fd49b 5084{
2f2b904f 5085 enum ofperr error;
9e1fd49b 5086
2f2b904f
BP
5087 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
5088 if (error) {
5089 return error;
5090 }
5091
5092 pm->hw_addr = opm->hw_addr;
5093 pm->config = ntohl(opm->config) & OFPPC11_ALL;
5094 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
5095 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
5096
5097 return 0;
5098}
5099
5100static enum ofperr
5101ofputil_decode_ofp14_port_mod_properties(struct ofpbuf *b, bool loose,
5102 struct ofputil_port_mod *pm)
5103{
5104 while (b->size > 0) {
5105 struct ofpbuf property;
9e1fd49b 5106 enum ofperr error;
2f2b904f 5107 uint64_t type;
9e1fd49b 5108
2f2b904f 5109 error = ofpprop_pull(b, &property, &type);
9e1fd49b
BP
5110 if (error) {
5111 return error;
5112 }
5113
2f2b904f
BP
5114 switch (type) {
5115 case OFPPMPT14_ETHERNET:
5116 error = parse_port_mod_ethernet_property(&property, pm);
5117 break;
18cc69d9 5118
2f2b904f
BP
5119 default:
5120 error = OFPPROP_UNKNOWN(loose, "port_mod", type);
5121 break;
5122 }
18cc69d9 5123
18cc69d9
BP
5124 if (error) {
5125 return error;
5126 }
2f2b904f
BP
5127 }
5128 return 0;
5129}
18cc69d9 5130
2f2b904f
BP
5131static enum ofperr
5132ofputil_decode_ofp14_port_mod(struct ofpbuf *b, bool loose,
5133 struct ofputil_port_mod *pm)
5134{
5135 const struct ofp14_port_mod *opm = ofpbuf_pull(b, sizeof *opm);
5136 enum ofperr error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
5137 if (error) {
5138 return error;
5139 }
18cc69d9 5140
2f2b904f
BP
5141 pm->hw_addr = opm->hw_addr;
5142 pm->config = ntohl(opm->config) & OFPPC11_ALL;
5143 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
18cc69d9 5144
2f2b904f
BP
5145 return ofputil_decode_ofp14_port_mod_properties(b, loose, pm);
5146}
18cc69d9 5147
2f2b904f
BP
5148static enum ofperr
5149ofputil_decode_ofp16_port_mod(struct ofpbuf *b, bool loose,
5150 struct ofputil_port_mod *pm)
5151{
5152 const struct ofp16_port_mod *opm = ofpbuf_pull(b, sizeof *opm);
5153 enum ofperr error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
5154 if (error) {
5155 return error;
5156 }
18cc69d9 5157
2f2b904f
BP
5158 if (opm->hw_addr_type & htons(OFPPHAT16_EUI48)) {
5159 pm->hw_addr = opm->hw_addr;
5160 }
5161 if (opm->hw_addr_type & htons(OFPPHAT16_EUI64)) {
5162 pm->hw_addr64 = opm->hw_addr64;
5163 }
5164 pm->hw_addr = opm->hw_addr;
5165 pm->config = ntohl(opm->config) & OFPPC11_ALL;
5166 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
18cc69d9 5167
2f2b904f
BP
5168 return ofputil_decode_ofp14_port_mod_properties(b, loose, pm);
5169}
5170
5171/* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
5172 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5173enum ofperr
5174ofputil_decode_port_mod(const struct ofp_header *oh,
5175 struct ofputil_port_mod *pm, bool loose)
5176{
5177 memset(pm, 0, sizeof *pm);
5178
5179 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
5180 enum ofpraw raw = ofpraw_pull_assert(&b);
5181 if (raw == OFPRAW_OFPT10_PORT_MOD) {
5182 return ofputil_decode_ofp10_port_mod(b.data, pm);
5183 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
5184 return ofputil_decode_ofp11_port_mod(b.data, pm);
5185 } else if (raw == OFPRAW_OFPT14_PORT_MOD) {
5186 return ofputil_decode_ofp14_port_mod(&b, loose, pm);
5187 } else if (raw == OFPRAW_OFPT16_PORT_MOD) {
5188 return ofputil_decode_ofp16_port_mod(&b, loose, pm);
9e1fd49b 5189 } else {
982697a4 5190 return OFPERR_OFPBRC_BAD_TYPE;
9e1fd49b
BP
5191 }
5192
5193 pm->config &= pm->mask;
5194 return 0;
5195}
5196
5197/* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
5198 * message suitable for 'protocol', and returns that encoded form in a buffer
5199 * owned by the caller. */
5200struct ofpbuf *
5201ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
5202 enum ofputil_protocol protocol)
5203{
2e3fa633 5204 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
9e1fd49b
BP
5205 struct ofpbuf *b;
5206
2e3fa633
SH
5207 switch (ofp_version) {
5208 case OFP10_VERSION: {
9e1fd49b
BP
5209 struct ofp10_port_mod *opm;
5210
982697a4
BP
5211 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
5212 opm = ofpbuf_put_zeros(b, sizeof *opm);
4e022ec0 5213 opm->port_no = htons(ofp_to_u16(pm->port_no));
74ff3298 5214 opm->hw_addr = pm->hw_addr;
9e1fd49b
BP
5215 opm->config = htonl(pm->config & OFPPC10_ALL);
5216 opm->mask = htonl(pm->mask & OFPPC10_ALL);
5217 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
2e3fa633
SH
5218 break;
5219 }
5220
5fe7c919 5221 case OFP11_VERSION:
2e1ae200
JR
5222 case OFP12_VERSION:
5223 case OFP13_VERSION: {
9e1fd49b
BP
5224 struct ofp11_port_mod *opm;
5225
982697a4
BP
5226 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
5227 opm = ofpbuf_put_zeros(b, sizeof *opm);
026a5179 5228 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
74ff3298 5229 opm->hw_addr = pm->hw_addr;
9e1fd49b
BP
5230 opm->config = htonl(pm->config & OFPPC11_ALL);
5231 opm->mask = htonl(pm->mask & OFPPC11_ALL);
5232 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
2e3fa633
SH
5233 break;
5234 }
42dccab5 5235 case OFP14_VERSION:
2f2b904f 5236 case OFP15_VERSION: {
18cc69d9
BP
5237 struct ofp14_port_mod *opm;
5238
b611d3ac 5239 b = ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD, ofp_version, 0);
18cc69d9
BP
5240 opm = ofpbuf_put_zeros(b, sizeof *opm);
5241 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
74ff3298 5242 opm->hw_addr = pm->hw_addr;
18cc69d9
BP
5243 opm->config = htonl(pm->config & OFPPC11_ALL);
5244 opm->mask = htonl(pm->mask & OFPPC11_ALL);
5245
5246 if (pm->advertise) {
b611d3ac
BP
5247 ofpprop_put_be32(b, OFPPMPT14_ETHERNET,
5248 netdev_port_features_to_ofp11(pm->advertise));
18cc69d9 5249 }
c37c0382 5250 break;
18cc69d9 5251 }
2f2b904f
BP
5252 case OFP16_VERSION: {
5253 struct ofp16_port_mod *opm;
5254
5255 b = ofpraw_alloc(OFPRAW_OFPT16_PORT_MOD, ofp_version, 0);
5256 opm = ofpbuf_put_zeros(b, sizeof *opm);
5257 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
5258 if (!eth_addr_is_zero(pm->hw_addr)) {
5259 opm->hw_addr_type |= htons(OFPPHAT16_EUI48);
5260 opm->hw_addr = pm->hw_addr;
5261 }
5262 if (!eth_addr64_is_zero(pm->hw_addr64)) {
5263 opm->hw_addr_type |= htons(OFPPHAT16_EUI64);
5264 opm->hw_addr64 = pm->hw_addr64;
5265 }
5266 opm->config = htonl(pm->config & OFPPC11_ALL);
5267 opm->mask = htonl(pm->mask & OFPPC11_ALL);
5268
5269 if (pm->advertise) {
5270 ofpprop_put_be32(b, OFPPMPT14_ETHERNET,
5271 netdev_port_features_to_ofp11(pm->advertise));
5272 }
5273 break;
5274 }
918f2b82 5275 default:
428b2edd 5276 OVS_NOT_REACHED();
918f2b82
AZ
5277 }
5278
5279 return b;
5280}
72b25867
BP
5281\f
5282/* Table features. */
918f2b82 5283
5deff5aa 5284static enum ofperr
9a4eddbb 5285pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload,
34a543e3 5286 uint64_t *typep)
5deff5aa 5287{
9a4eddbb 5288 enum ofperr error;
5deff5aa 5289
c5562271 5290 error = ofpprop_pull(msg, payload, typep);
9a4eddbb 5291 if (payload && !error) {
b611d3ac 5292 ofpbuf_pull(payload, (char *)payload->msg - (char *)payload->header);
5deff5aa 5293 }
9a4eddbb 5294 return error;
5deff5aa
AW
5295}
5296
5297static enum ofperr
08d1e234
BP
5298parse_action_bitmap(struct ofpbuf *payload, enum ofp_version ofp_version,
5299 uint64_t *ofpacts)
5deff5aa 5300{
08d1e234 5301 uint32_t types = 0;
5deff5aa 5302
6fd6ed71 5303 while (payload->size > 0) {
08d1e234 5304 enum ofperr error;
34a543e3 5305 uint64_t type;
08d1e234 5306
34a543e3 5307 error = ofpprop_pull__(payload, NULL, 1, 0x10000, &type);
5deff5aa
AW
5308 if (error) {
5309 return error;
5310 }
08d1e234
BP
5311 if (type < CHAR_BIT * sizeof types) {
5312 types |= 1u << type;
5deff5aa
AW
5313 }
5314 }
08d1e234
BP
5315
5316 *ofpacts = ofpact_bitmap_from_openflow(htonl(types), ofp_version);
5deff5aa
AW
5317 return 0;
5318}
5319
5320static enum ofperr
5321parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts)
5322{
5323 *insts = 0;
6fd6ed71 5324 while (payload->size > 0) {
5deff5aa
AW
5325 enum ovs_instruction_type inst;
5326 enum ofperr error;
34a543e3 5327 uint64_t ofpit;
5deff5aa 5328
72b25867
BP
5329 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
5330 * It seems clear that they aren't padded to 8 bytes, though, because
5331 * both standards say that "non-experimenter instructions are 4 bytes"
5332 * and do not mention any padding before the first instruction ID.
5333 * (There wouldn't be any point in padding to 8 bytes if the IDs were
5334 * aligned on an odd 4-byte boundary.)
5335 *
5336 * Anyway, we just assume they're all glommed together on byte
5337 * boundaries. */
34a543e3 5338 error = ofpprop_pull__(payload, NULL, 1, 0x10000, &ofpit);
5deff5aa
AW
5339 if (error) {
5340 return error;
5341 }
5342
5343 error = ovs_instruction_type_from_inst_type(&inst, ofpit);
5344 if (!error) {
5345 *insts |= 1u << inst;
5346 } else if (!loose) {
5347 return error;
5348 }
5349 }
5350 return 0;
5351}
5352
5353static enum ofperr
5354parse_table_features_next_table(struct ofpbuf *payload,
5355 unsigned long int *next_tables)
5356{
5357 size_t i;
5358
5359 memset(next_tables, 0, bitmap_n_bytes(255));
6fd6ed71
PS
5360 for (i = 0; i < payload->size; i++) {
5361 uint8_t id = ((const uint8_t *) payload->data)[i];
5deff5aa 5362 if (id >= 255) {
9a4eddbb 5363 return OFPERR_OFPBPC_BAD_VALUE;
5deff5aa
AW
5364 }
5365 bitmap_set1(next_tables, id);
5366 }
5367 return 0;
5368}
5369
5deff5aa
AW
5370static enum ofperr
5371parse_oxms(struct ofpbuf *payload, bool loose,
abadfcb0 5372 struct mf_bitmap *exactp, struct mf_bitmap *maskedp)
5deff5aa 5373{
abadfcb0
BP
5374 struct mf_bitmap exact = MF_BITMAP_INITIALIZER;
5375 struct mf_bitmap masked = MF_BITMAP_INITIALIZER;
5deff5aa 5376
6fd6ed71 5377 while (payload->size > 0) {
5deff5aa
AW
5378 const struct mf_field *field;
5379 enum ofperr error;
5380 bool hasmask;
5381
04f48a68 5382 error = nx_pull_header(payload, NULL, &field, &hasmask);
5deff5aa 5383 if (!error) {
abadfcb0 5384 bitmap_set1(hasmask ? masked.bm : exact.bm, field->id);
5deff5aa
AW
5385 } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) {
5386 return error;
5387 }
5388 }
5389 if (exactp) {
5390 *exactp = exact;
abadfcb0 5391 } else if (!bitmap_is_all_zeros(exact.bm, MFF_N_IDS)) {
5deff5aa
AW
5392 return OFPERR_OFPBMC_BAD_MASK;
5393 }
5394 if (maskedp) {
5395 *maskedp = masked;
abadfcb0 5396 } else if (!bitmap_is_all_zeros(masked.bm, MFF_N_IDS)) {
5deff5aa
AW
5397 return OFPERR_OFPBMC_BAD_MASK;
5398 }
5399 return 0;
5400}
5401
5402/* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
5403 * ofputil_table_features in 'tf'.
5404 *
5405 * If 'loose' is true, this function ignores properties and values that it does
5406 * not understand, as a controller would want to do when interpreting
5407 * capabilities provided by a switch. If 'loose' is false, this function
5408 * treats unknown properties and values as an error, as a switch would want to
5409 * do when interpreting a configuration request made by a controller.
5410 *
5411 * A single OpenFlow message can specify features for multiple tables. Calling
5412 * this function multiple times for a single 'msg' iterates through the tables
5413 * in the message. The caller must initially leave 'msg''s layer pointers null
5414 * and not modify them between calls.
5415 *
5416 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
5417 * a positive "enum ofperr" value. */
5418int
5419ofputil_decode_table_features(struct ofpbuf *msg,
5420 struct ofputil_table_features *tf, bool loose)
5421{
3c1bb396
BP
5422 memset(tf, 0, sizeof *tf);
5423
6fd6ed71 5424 if (!msg->header) {
5deff5aa
AW
5425 ofpraw_pull_assert(msg);
5426 }
5427
6fd6ed71 5428 if (!msg->size) {
5deff5aa
AW
5429 return EOF;
5430 }
5431
0a2869d5
BP
5432 const struct ofp_header *oh = msg->header;
5433 struct ofp13_table_features *otf = msg->data;
6fd6ed71 5434 if (msg->size < sizeof *otf) {
9a4eddbb 5435 return OFPERR_OFPBPC_BAD_LEN;
5deff5aa
AW
5436 }
5437
0a2869d5 5438 unsigned int len = ntohs(otf->length);
6fd6ed71 5439 if (len < sizeof *otf || len % 8 || len > msg->size) {
9a4eddbb 5440 return OFPERR_OFPBPC_BAD_LEN;
5deff5aa 5441 }
5deff5aa
AW
5442
5443 tf->table_id = otf->table_id;
5444 if (tf->table_id == OFPTT_ALL) {
5445 return OFPERR_OFPTFFC_BAD_TABLE;
5446 }
5447
f9ac0f03 5448 ovs_strlcpy_arrays(tf->name, otf->name);
5deff5aa
AW
5449 tf->metadata_match = otf->metadata_match;
5450 tf->metadata_write = otf->metadata_write;
82c22d34
BP
5451 tf->miss_config = OFPUTIL_TABLE_MISS_DEFAULT;
5452 if (oh->version >= OFP14_VERSION) {
5453 uint32_t caps = ntohl(otf->capabilities);
5454 tf->supports_eviction = (caps & OFPTC14_EVICTION) != 0;
5455 tf->supports_vacancy_events = (caps & OFPTC14_VACANCY_EVENTS) != 0;
5456 } else {
5457 tf->supports_eviction = -1;
5458 tf->supports_vacancy_events = -1;
5459 }
5deff5aa
AW
5460 tf->max_entries = ntohl(otf->max_entries);
5461
0a2869d5
BP
5462 struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
5463 len);
5464 ofpbuf_pull(&properties, sizeof *otf);
6fd6ed71 5465 while (properties.size > 0) {
5deff5aa
AW
5466 struct ofpbuf payload;
5467 enum ofperr error;
34a543e3 5468 uint64_t type;
5deff5aa 5469
ae665de5 5470 error = pull_table_feature_property(&properties, &payload, &type);
5deff5aa
AW
5471 if (error) {
5472 return error;
5473 }
5474
5475 switch ((enum ofp13_table_feature_prop_type) type) {
5476 case OFPTFPT13_INSTRUCTIONS:
5477 error = parse_instruction_ids(&payload, loose,
5478 &tf->nonmiss.instructions);
5479 break;
5480 case OFPTFPT13_INSTRUCTIONS_MISS:
5481 error = parse_instruction_ids(&payload, loose,
5482 &tf->miss.instructions);
5483 break;
5484
5485 case OFPTFPT13_NEXT_TABLES:
5486 error = parse_table_features_next_table(&payload,
5487 tf->nonmiss.next);
5488 break;
5489 case OFPTFPT13_NEXT_TABLES_MISS:
5490 error = parse_table_features_next_table(&payload, tf->miss.next);
5491 break;
5492
5493 case OFPTFPT13_WRITE_ACTIONS:
08d1e234
BP
5494 error = parse_action_bitmap(&payload, oh->version,
5495 &tf->nonmiss.write.ofpacts);
5deff5aa
AW
5496 break;
5497 case OFPTFPT13_WRITE_ACTIONS_MISS:
08d1e234
BP
5498 error = parse_action_bitmap(&payload, oh->version,
5499 &tf->miss.write.ofpacts);
5deff5aa
AW
5500 break;
5501
5502 case OFPTFPT13_APPLY_ACTIONS:
08d1e234
BP
5503 error = parse_action_bitmap(&payload, oh->version,
5504 &tf->nonmiss.apply.ofpacts);
5deff5aa
AW
5505 break;
5506 case OFPTFPT13_APPLY_ACTIONS_MISS:
08d1e234
BP
5507 error = parse_action_bitmap(&payload, oh->version,
5508 &tf->miss.apply.ofpacts);
5deff5aa
AW
5509 break;
5510
5511 case OFPTFPT13_MATCH:
5512 error = parse_oxms(&payload, loose, &tf->match, &tf->mask);
5513 break;
5514 case OFPTFPT13_WILDCARDS:
5515 error = parse_oxms(&payload, loose, &tf->wildcard, NULL);
5516 break;
5517
5518 case OFPTFPT13_WRITE_SETFIELD:
5519 error = parse_oxms(&payload, loose,
5520 &tf->nonmiss.write.set_fields, NULL);
5521 break;
5522 case OFPTFPT13_WRITE_SETFIELD_MISS:
5523 error = parse_oxms(&payload, loose,
5524 &tf->miss.write.set_fields, NULL);
5525 break;
5526 case OFPTFPT13_APPLY_SETFIELD:
5527 error = parse_oxms(&payload, loose,
5528 &tf->nonmiss.apply.set_fields, NULL);
5529 break;
5530 case OFPTFPT13_APPLY_SETFIELD_MISS:
5531 error = parse_oxms(&payload, loose,
5532 &tf->miss.apply.set_fields, NULL);
5533 break;
5534
5535 case OFPTFPT13_EXPERIMENTER:
5536 case OFPTFPT13_EXPERIMENTER_MISS:
9a4eddbb 5537 default:
34a543e3 5538 error = OFPPROP_UNKNOWN(loose, "table features", type);
5deff5aa
AW
5539 break;
5540 }
5541 if (error) {
5542 return error;
5543 }
5544 }
5545
5546 /* Fix inconsistencies:
5547 *
e7227821
BP
5548 * - Turn on 'match' bits that are set in 'mask', because maskable
5549 * fields are matchable.
5deff5aa
AW
5550 *
5551 * - Turn on 'wildcard' bits that are set in 'mask', because a field
e7227821
BP
5552 * that is arbitrarily maskable can be wildcarded entirely.
5553 *
5554 * - Turn off 'wildcard' bits that are not in 'match', because a field
5555 * must be matchable for it to be meaningfully wildcarded. */
5556 bitmap_or(tf->match.bm, tf->mask.bm, MFF_N_IDS);
abadfcb0 5557 bitmap_or(tf->wildcard.bm, tf->mask.bm, MFF_N_IDS);
e7227821 5558 bitmap_and(tf->wildcard.bm, tf->match.bm, MFF_N_IDS);
5deff5aa
AW
5559
5560 return 0;
5561}
5562
5563/* Encodes and returns a request to obtain the table features of a switch.
5564 * The message is encoded for OpenFlow version 'ofp_version'. */
5565struct ofpbuf *
5566ofputil_encode_table_features_request(enum ofp_version ofp_version)
5567{
5568 struct ofpbuf *request = NULL;
5569
5570 switch (ofp_version) {
5571 case OFP10_VERSION:
5572 case OFP11_VERSION:
5573 case OFP12_VERSION:
5574 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
5575 "(\'-O OpenFlow13\')");
5576 case OFP13_VERSION:
5577 case OFP14_VERSION:
42dccab5 5578 case OFP15_VERSION:
b79d45a1 5579 case OFP16_VERSION:
5deff5aa
AW
5580 request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST,
5581 ofp_version, 0);
5582 break;
5583 default:
5584 OVS_NOT_REACHED();
5585 }
5586
5587 return request;
5588}
5589
3c4e10fb
BP
5590static void
5591put_fields_property(struct ofpbuf *reply,
5592 const struct mf_bitmap *fields,
5593 const struct mf_bitmap *masks,
5594 enum ofp13_table_feature_prop_type property,
5595 enum ofp_version version)
5596{
5597 size_t start_ofs;
5598 int field;
5599
c5562271 5600 start_ofs = ofpprop_start(reply, property);
3c4e10fb 5601 BITMAP_FOR_EACH_1 (field, MFF_N_IDS, fields->bm) {
178742f9
BP
5602 nx_put_header(reply, field, version,
5603 masks && bitmap_is_set(masks->bm, field));
3c4e10fb 5604 }
c5562271 5605 ofpprop_end(reply, start_ofs);
3c4e10fb
BP
5606}
5607
5608static void
5609put_table_action_features(struct ofpbuf *reply,
5610 const struct ofputil_table_action_features *taf,
5611 enum ofp13_table_feature_prop_type actions_type,
5612 enum ofp13_table_feature_prop_type set_fields_type,
5613 int miss_offset, enum ofp_version version)
5614{
c5562271
BP
5615 ofpprop_put_bitmap(reply, actions_type + miss_offset,
5616 ntohl(ofpact_bitmap_to_openflow(taf->ofpacts,
5617 version)));
3c4e10fb
BP
5618 put_fields_property(reply, &taf->set_fields, NULL,
5619 set_fields_type + miss_offset, version);
5620}
5621
5622static void
5623put_table_instruction_features(
5624 struct ofpbuf *reply, const struct ofputil_table_instruction_features *tif,
5625 int miss_offset, enum ofp_version version)
5626{
5627 size_t start_ofs;
5628 uint8_t table_id;
5629
c5562271
BP
5630 ofpprop_put_bitmap(reply, OFPTFPT13_INSTRUCTIONS + miss_offset,
5631 ntohl(ovsinst_bitmap_to_openflow(tif->instructions,
5632 version)));
3c4e10fb 5633
c5562271 5634 start_ofs = ofpprop_start(reply, OFPTFPT13_NEXT_TABLES + miss_offset);
3c4e10fb
BP
5635 BITMAP_FOR_EACH_1 (table_id, 255, tif->next) {
5636 ofpbuf_put(reply, &table_id, 1);
5637 }
c5562271 5638 ofpprop_end(reply, start_ofs);
3c4e10fb
BP
5639
5640 put_table_action_features(reply, &tif->write,
5641 OFPTFPT13_WRITE_ACTIONS,
5642 OFPTFPT13_WRITE_SETFIELD, miss_offset, version);
5643 put_table_action_features(reply, &tif->apply,
5644 OFPTFPT13_APPLY_ACTIONS,
5645 OFPTFPT13_APPLY_SETFIELD, miss_offset, version);
5646}
5647
5648void
5649ofputil_append_table_features_reply(const struct ofputil_table_features *tf,
ca6ba700 5650 struct ovs_list *replies)
3c4e10fb 5651{
417e7e66 5652 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
3c4e10fb 5653 enum ofp_version version = ofpmp_version(replies);
6fd6ed71 5654 size_t start_ofs = reply->size;
3c4e10fb
BP
5655 struct ofp13_table_features *otf;
5656
5657 otf = ofpbuf_put_zeros(reply, sizeof *otf);
5658 otf->table_id = tf->table_id;
f9ac0f03 5659 ovs_strlcpy_arrays(otf->name, tf->name);
3c4e10fb
BP
5660 otf->metadata_match = tf->metadata_match;
5661 otf->metadata_write = tf->metadata_write;
82c22d34
BP
5662 if (version >= OFP14_VERSION) {
5663 if (tf->supports_eviction) {
5664 otf->capabilities |= htonl(OFPTC14_EVICTION);
5665 }
5666 if (tf->supports_vacancy_events) {
5667 otf->capabilities |= htonl(OFPTC14_VACANCY_EVENTS);
5668 }
5669 }
3c4e10fb
BP
5670 otf->max_entries = htonl(tf->max_entries);
5671
5672 put_table_instruction_features(reply, &tf->nonmiss, 0, version);
5673 put_table_instruction_features(reply, &tf->miss, 1, version);
5674
5675 put_fields_property(reply, &tf->match, &tf->mask,
5676 OFPTFPT13_MATCH, version);
5677 put_fields_property(reply, &tf->wildcard, NULL,
5678 OFPTFPT13_WILDCARDS, version);
5679
5680 otf = ofpbuf_at_assert(reply, start_ofs, sizeof *otf);
6fd6ed71 5681 otf->length = htons(reply->size - start_ofs);
3c4e10fb
BP
5682 ofpmp_postappend(replies, start_ofs);
5683}
5684
bab86012
SJ
5685static enum ofperr
5686parse_table_desc_vacancy_property(struct ofpbuf *property,
5687 struct ofputil_table_desc *td)
5688{
5689 struct ofp14_table_mod_prop_vacancy *otv = property->data;
5690
5691 if (property->size != sizeof *otv) {
5692 return OFPERR_OFPBPC_BAD_LEN;
5693 }
5694
5695 td->table_vacancy.vacancy_down = otv->vacancy_down;
5696 td->table_vacancy.vacancy_up = otv->vacancy_up;
5697 td->table_vacancy.vacancy = otv->vacancy;
5698 return 0;
5699}
5700
03c72922
BP
5701/* Decodes the next OpenFlow "table desc" message (of possibly several) from
5702 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
5703 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
5704 * value. */
5705int
5706ofputil_decode_table_desc(struct ofpbuf *msg,
5707 struct ofputil_table_desc *td,
5708 enum ofp_version version)
5709{
03c72922
BP
5710 memset(td, 0, sizeof *td);
5711
5712 if (!msg->header) {
5713 ofpraw_pull_assert(msg);
5714 }
5715
5716 if (!msg->size) {
5717 return EOF;
5718 }
5719
0a2869d5 5720 struct ofp14_table_desc *otd = ofpbuf_try_pull(msg, sizeof *otd);
03c72922
BP
5721 if (!otd) {
5722 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply has %"PRIu32" "
5723 "leftover bytes at end", msg->size);
5724 return OFPERR_OFPBRC_BAD_LEN;
5725 }
5726
5727 td->table_id = otd->table_id;
0a2869d5 5728 size_t length = ntohs(otd->length);
03c72922
BP
5729 if (length < sizeof *otd || length - sizeof *otd > msg->size) {
5730 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply claims invalid "
5731 "length %"PRIuSIZE, length);
5732 return OFPERR_OFPBRC_BAD_LEN;
5733 }
5734 length -= sizeof *otd;
03c72922
BP
5735
5736 td->eviction = ofputil_decode_table_eviction(otd->config, version);
bab86012 5737 td->vacancy = ofputil_decode_table_vacancy(otd->config, version);
03c72922
BP
5738 td->eviction_flags = UINT32_MAX;
5739
0a2869d5
BP
5740 struct ofpbuf properties = ofpbuf_const_initializer(
5741 ofpbuf_pull(msg, length), length);
03c72922
BP
5742 while (properties.size > 0) {
5743 struct ofpbuf payload;
5744 enum ofperr error;
34a543e3 5745 uint64_t type;
03c72922 5746
c5562271 5747 error = ofpprop_pull(&properties, &payload, &type);
03c72922
BP
5748 if (error) {
5749 return error;
5750 }
5751
5752 switch (type) {
5753 case OFPTMPT14_EVICTION:
b611d3ac 5754 error = ofpprop_parse_u32(&payload, &td->eviction_flags);
03c72922
BP
5755 break;
5756
bab86012
SJ
5757 case OFPTMPT14_VACANCY:
5758 error = parse_table_desc_vacancy_property(&payload, td);
5759 break;
5760
03c72922 5761 default:
34a543e3 5762 error = OFPPROP_UNKNOWN(true, "table_desc", type);
03c72922
BP
5763 break;
5764 }
5765
5766 if (error) {
5767 return error;
5768 }
5769 }
5770
5771 return 0;
5772}
5773
5774/* Encodes and returns a request to obtain description of tables of a switch.
5775 * The message is encoded for OpenFlow version 'ofp_version'. */
5776struct ofpbuf *
5777ofputil_encode_table_desc_request(enum ofp_version ofp_version)
5778{
5779 struct ofpbuf *request = NULL;
5780
5781 if (ofp_version >= OFP14_VERSION) {
5782 request = ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST,
5783 ofp_version, 0);
5784 } else {
5785 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
5786 "(\'-O OpenFlow14\')");
5787 }
5788
5789 return request;
5790}
5791
5792/* Function to append Table desc information in a reply list. */
5793void
5794ofputil_append_table_desc_reply(const struct ofputil_table_desc *td,
5795 struct ovs_list *replies,
5796 enum ofp_version version)
5797{
417e7e66 5798 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
03c72922
BP
5799 size_t start_otd;
5800 struct ofp14_table_desc *otd;
5801
5802 start_otd = reply->size;
5803 ofpbuf_put_zeros(reply, sizeof *otd);
5804 if (td->eviction_flags != UINT32_MAX) {
b611d3ac 5805 ofpprop_put_u32(reply, OFPTMPT14_EVICTION, td->eviction_flags);
03c72922 5806 }
bab86012
SJ
5807 if (td->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5808 struct ofp14_table_mod_prop_vacancy *otv;
5809
303721ee 5810 otv = ofpprop_put_zeros(reply, OFPTMPT14_VACANCY, sizeof *otv);
bab86012
SJ
5811 otv->vacancy_down = td->table_vacancy.vacancy_down;
5812 otv->vacancy_up = td->table_vacancy.vacancy_up;
5813 otv->vacancy = td->table_vacancy.vacancy;
5814 }
03c72922
BP
5815
5816 otd = ofpbuf_at_assert(reply, start_otd, sizeof *otd);
5817 otd->length = htons(reply->size - start_otd);
5818 otd->table_id = td->table_id;
5819 otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
de7d3c07
SJ
5820 td->eviction, td->vacancy,
5821 version);
03c72922
BP
5822 ofpmp_postappend(replies, start_otd);
5823}
5824
de7d3c07
SJ
5825/* This function parses Vacancy property, and decodes the
5826 * ofp14_table_mod_prop_vacancy in ofputil_table_mod.
5827 * Returns OFPERR_OFPBPC_BAD_VALUE error code when vacancy_down is
5828 * greater than vacancy_up and also when current vacancy has non-zero
5829 * value. Returns 0 on success. */
5830static enum ofperr
5831parse_table_mod_vacancy_property(struct ofpbuf *property,
5832 struct ofputil_table_mod *tm)
5833{
5834 struct ofp14_table_mod_prop_vacancy *otv = property->data;
5835
5836 if (property->size != sizeof *otv) {
5837 return OFPERR_OFPBPC_BAD_LEN;
5838 }
5839 tm->table_vacancy.vacancy_down = otv->vacancy_down;
5840 tm->table_vacancy.vacancy_up = otv->vacancy_up;
5841 if (tm->table_vacancy.vacancy_down > tm->table_vacancy.vacancy_up) {
c5562271
BP
5842 OFPPROP_LOG(&bad_ofmsg_rl, false,
5843 "Value of vacancy_down is greater than vacancy_up");
de7d3c07
SJ
5844 return OFPERR_OFPBPC_BAD_VALUE;
5845 }
5846 if (tm->table_vacancy.vacancy_down > 100 ||
5847 tm->table_vacancy.vacancy_up > 100) {
c5562271
BP
5848 OFPPROP_LOG(&bad_ofmsg_rl, false, "Vacancy threshold percentage "
5849 "should not be greater than 100");
de7d3c07
SJ
5850 return OFPERR_OFPBPC_BAD_VALUE;
5851 }
5852 tm->table_vacancy.vacancy = otv->vacancy;
5853 if (tm->table_vacancy.vacancy) {
c5562271
BP
5854 OFPPROP_LOG(&bad_ofmsg_rl, false,
5855 "Vacancy value should be zero for table-mod messages");
de7d3c07
SJ
5856 return OFPERR_OFPBPC_BAD_VALUE;
5857 }
5858 return 0;
5859}
5860
5861/* Given 'config', taken from an OpenFlow 'version' message that specifies
5862 * table configuration (a table mod, table stats, or table features message),
5863 * returns the table vacancy configuration that it specifies.
5864 *
5865 * Only OpenFlow 1.4 and later specify table vacancy configuration this way,
5866 * so for other 'version' this function always returns
5867 * OFPUTIL_TABLE_VACANCY_DEFAULT. */
5868static enum ofputil_table_vacancy
5869ofputil_decode_table_vacancy(ovs_be32 config, enum ofp_version version)
5870{
5871 return (version < OFP14_VERSION ? OFPUTIL_TABLE_VACANCY_DEFAULT
5872 : config & htonl(OFPTC14_VACANCY_EVENTS) ? OFPUTIL_TABLE_VACANCY_ON
5873 : OFPUTIL_TABLE_VACANCY_OFF);
5874}
5875
3c1bb396
BP
5876/* Given 'config', taken from an OpenFlow 'version' message that specifies
5877 * table configuration (a table mod, table stats, or table features message),
82c22d34
BP
5878 * returns the table eviction configuration that it specifies.
5879 *
5880 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
5881 * so for other 'version' values this function always returns
5882 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
5883static enum ofputil_table_eviction
5884ofputil_decode_table_eviction(ovs_be32 config, enum ofp_version version)
5885{
5886 return (version < OFP14_VERSION ? OFPUTIL_TABLE_EVICTION_DEFAULT
5887 : config & htonl(OFPTC14_EVICTION) ? OFPUTIL_TABLE_EVICTION_ON
5888 : OFPUTIL_TABLE_EVICTION_OFF);
5889}
5890
5891/* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
5892 * OpenFlow messages of the given 'version', based on the provided 'miss' and
5893 * 'eviction' values. */
5894static ovs_be32
5895ofputil_encode_table_config(enum ofputil_table_miss miss,
5896 enum ofputil_table_eviction eviction,
de7d3c07 5897 enum ofputil_table_vacancy vacancy,
82c22d34
BP
5898 enum ofp_version version)
5899{
de7d3c07 5900 uint32_t config = 0;
7c9afefd 5901 /* Search for "OFPTC_* Table Configuration" in the documentation for more
82c22d34
BP
5902 * information on the crazy evolution of this field. */
5903 switch (version) {
5904 case OFP10_VERSION:
5905 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
5906 return htonl(0);
5907
5908 case OFP11_VERSION:
5909 case OFP12_VERSION:
5910 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
5911 switch (miss) {
5912 case OFPUTIL_TABLE_MISS_DEFAULT:
5913 /* Really this shouldn't be used for encoding (the caller should
5914 * provide a specific value) but I can't imagine that defaulting to
5915 * the fall-through case here will hurt. */
5916 case OFPUTIL_TABLE_MISS_CONTROLLER:
5917 default:
5918 return htonl(OFPTC11_TABLE_MISS_CONTROLLER);
5919 case OFPUTIL_TABLE_MISS_CONTINUE:
5920 return htonl(OFPTC11_TABLE_MISS_CONTINUE);
5921 case OFPUTIL_TABLE_MISS_DROP:
5922 return htonl(OFPTC11_TABLE_MISS_DROP);
5923 }
5924 OVS_NOT_REACHED();
5925
5926 case OFP13_VERSION:
5927 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
5928 * flags, so this is correct. */
5929 return htonl(0);
5930
5931 case OFP14_VERSION:
5932 case OFP15_VERSION:
b79d45a1 5933 case OFP16_VERSION:
de7d3c07
SJ
5934 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and
5935 * OFPTC14_VACANCY_EVENTS. */
5936 if (eviction == OFPUTIL_TABLE_EVICTION_ON) {
5937 config |= OFPTC14_EVICTION;
5938 }
5939 if (vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5940 config |= OFPTC14_VACANCY_EVENTS;
5941 }
5942 return htonl(config);
82c22d34
BP
5943 }
5944
5945 OVS_NOT_REACHED();
5946}
5947
5948/* Given 'config', taken from an OpenFlow 'version' message that specifies
5949 * table configuration (a table mod, table stats, or table features message),
5950 * returns the table miss configuration that it specifies.
5951 *
5952 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
5953 * other 'version' values this function always returns
5954 * OFPUTIL_TABLE_MISS_DEFAULT. */
3c1bb396 5955static enum ofputil_table_miss
82c22d34 5956ofputil_decode_table_miss(ovs_be32 config_, enum ofp_version version)
3c1bb396
BP
5957{
5958 uint32_t config = ntohl(config_);
5959
82c22d34 5960 if (version == OFP11_VERSION || version == OFP12_VERSION) {
3c1bb396
BP
5961 switch (config & OFPTC11_TABLE_MISS_MASK) {
5962 case OFPTC11_TABLE_MISS_CONTROLLER:
5963 return OFPUTIL_TABLE_MISS_CONTROLLER;
5964
5965 case OFPTC11_TABLE_MISS_CONTINUE:
5966 return OFPUTIL_TABLE_MISS_CONTINUE;
5967
5968 case OFPTC11_TABLE_MISS_DROP:
5969 return OFPUTIL_TABLE_MISS_DROP;
5970
5971 default:
5972 VLOG_WARN_RL(&bad_ofmsg_rl, "bad table miss config %d", config);
5973 return OFPUTIL_TABLE_MISS_CONTROLLER;
5974 }
5975 } else {
5976 return OFPUTIL_TABLE_MISS_DEFAULT;
5977 }
5978}
5979
918f2b82
AZ
5980/* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
5981 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5982enum ofperr
5983ofputil_decode_table_mod(const struct ofp_header *oh,
5984 struct ofputil_table_mod *pm)
5985{
82c22d34
BP
5986 memset(pm, 0, sizeof *pm);
5987 pm->miss = OFPUTIL_TABLE_MISS_DEFAULT;
5988 pm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT;
5989 pm->eviction_flags = UINT32_MAX;
de7d3c07 5990 pm->vacancy = OFPUTIL_TABLE_VACANCY_DEFAULT;
918f2b82 5991
0a2869d5
BP
5992 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
5993 enum ofpraw raw = ofpraw_pull_assert(&b);
918f2b82 5994 if (raw == OFPRAW_OFPT11_TABLE_MOD) {
6fd6ed71 5995 const struct ofp11_table_mod *otm = b.data;
918f2b82
AZ
5996
5997 pm->table_id = otm->table_id;
82c22d34 5998 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
37ab26e8
BP
5999 } else if (raw == OFPRAW_OFPT14_TABLE_MOD) {
6000 const struct ofp14_table_mod *otm = ofpbuf_pull(&b, sizeof *otm);
6001
6002 pm->table_id = otm->table_id;
82c22d34
BP
6003 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
6004 pm->eviction = ofputil_decode_table_eviction(otm->config, oh->version);
de7d3c07 6005 pm->vacancy = ofputil_decode_table_vacancy(otm->config, oh->version);
82c22d34
BP
6006 while (b.size > 0) {
6007 struct ofpbuf property;
6008 enum ofperr error;
34a543e3 6009 uint64_t type;
82c22d34 6010
c5562271 6011 error = ofpprop_pull(&b, &property, &type);
82c22d34
BP
6012 if (error) {
6013 return error;
6014 }
6015
6016 switch (type) {
6017 case OFPTMPT14_EVICTION:
b611d3ac 6018 error = ofpprop_parse_u32(&property, &pm->eviction);
82c22d34
BP
6019 break;
6020
de7d3c07
SJ
6021 case OFPTMPT14_VACANCY:
6022 error = parse_table_mod_vacancy_property(&property, pm);
6023 break;
6024
82c22d34
BP
6025 default:
6026 error = OFPERR_OFPBRC_BAD_TYPE;
6027 break;
6028 }
6029
6030 if (error) {
6031 return error;
6032 }
6033 }
918f2b82
AZ
6034 } else {
6035 return OFPERR_OFPBRC_BAD_TYPE;
6036 }
6037
6038 return 0;
6039}
6040
82c22d34
BP
6041/* Converts the abstract form of a "table mod" message in '*tm' into an
6042 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
6043 * buffer owned by the caller. */
918f2b82 6044struct ofpbuf *
82c22d34 6045ofputil_encode_table_mod(const struct ofputil_table_mod *tm,
918f2b82
AZ
6046 enum ofputil_protocol protocol)
6047{
6048 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
6049 struct ofpbuf *b;
6050
6051 switch (ofp_version) {
6052 case OFP10_VERSION: {
6053 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
6054 "(\'-O OpenFlow11\')");
6055 break;
6056 }
6057 case OFP11_VERSION:
6058 case OFP12_VERSION:
6059 case OFP13_VERSION: {
6060 struct ofp11_table_mod *otm;
2e3fa633 6061
918f2b82
AZ
6062 b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0);
6063 otm = ofpbuf_put_zeros(b, sizeof *otm);
82c22d34
BP
6064 otm->table_id = tm->table_id;
6065 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
de7d3c07 6066 tm->vacancy, ofp_version);
918f2b82
AZ
6067 break;
6068 }
42dccab5 6069 case OFP14_VERSION:
b79d45a1
BP
6070 case OFP15_VERSION:
6071 case OFP16_VERSION: {
37ab26e8
BP
6072 struct ofp14_table_mod *otm;
6073
6074 b = ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD, ofp_version, 0);
6075 otm = ofpbuf_put_zeros(b, sizeof *otm);
82c22d34
BP
6076 otm->table_id = tm->table_id;
6077 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
de7d3c07 6078 tm->vacancy, ofp_version);
82c22d34
BP
6079
6080 if (tm->eviction_flags != UINT32_MAX) {
b611d3ac 6081 ofpprop_put_u32(b, OFPTMPT14_EVICTION, tm->eviction_flags);
82c22d34 6082 }
de7d3c07 6083 if (tm->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
303721ee
BP
6084 struct ofp14_table_mod_prop_vacancy *otv;
6085
6086 otv = ofpprop_put_zeros(b, OFPTMPT14_VACANCY, sizeof *otv);
de7d3c07
SJ
6087 otv->vacancy_down = tm->table_vacancy.vacancy_down;
6088 otv->vacancy_up = tm->table_vacancy.vacancy_up;
6089 }
c37c0382 6090 break;
37ab26e8 6091 }
2e3fa633 6092 default:
428b2edd 6093 OVS_NOT_REACHED();
9e1fd49b
BP
6094 }
6095
6096 return b;
6097}
2b07c8b1 6098\f
6ea4776b
JR
6099/* ofputil_role_request */
6100
6101/* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
6102 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
6103 * OFPERR_* value. */
6104enum ofperr
6105ofputil_decode_role_message(const struct ofp_header *oh,
6106 struct ofputil_role_request *rr)
6107{
0a2869d5
BP
6108 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
6109 enum ofpraw raw = ofpraw_pull_assert(&b);
f4f1ea7e
BP
6110 if (raw == OFPRAW_OFPT12_ROLE_REQUEST ||
6111 raw == OFPRAW_OFPT12_ROLE_REPLY) {
6fd6ed71 6112 const struct ofp12_role_request *orr = b.msg;
6ea4776b 6113
f4f1ea7e
BP
6114 if (orr->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
6115 orr->role != htonl(OFPCR12_ROLE_EQUAL) &&
6116 orr->role != htonl(OFPCR12_ROLE_MASTER) &&
6117 orr->role != htonl(OFPCR12_ROLE_SLAVE)) {
6118 return OFPERR_OFPRRFC_BAD_ROLE;
6ea4776b
JR
6119 }
6120
f4f1ea7e 6121 rr->role = ntohl(orr->role);
147cc9d3
BP
6122 if (raw == OFPRAW_OFPT12_ROLE_REQUEST
6123 ? orr->role == htonl(OFPCR12_ROLE_NOCHANGE)
b8266395 6124 : orr->generation_id == OVS_BE64_MAX) {
f4f1ea7e
BP
6125 rr->have_generation_id = false;
6126 rr->generation_id = 0;
6127 } else {
6128 rr->have_generation_id = true;
6129 rr->generation_id = ntohll(orr->generation_id);
6130 }
6131 } else if (raw == OFPRAW_NXT_ROLE_REQUEST ||
6132 raw == OFPRAW_NXT_ROLE_REPLY) {
6fd6ed71 6133 const struct nx_role_request *nrr = b.msg;
f4f1ea7e
BP
6134
6135 BUILD_ASSERT(NX_ROLE_OTHER + 1 == OFPCR12_ROLE_EQUAL);
6136 BUILD_ASSERT(NX_ROLE_MASTER + 1 == OFPCR12_ROLE_MASTER);
6137 BUILD_ASSERT(NX_ROLE_SLAVE + 1 == OFPCR12_ROLE_SLAVE);
6138
6139 if (nrr->role != htonl(NX_ROLE_OTHER) &&
6140 nrr->role != htonl(NX_ROLE_MASTER) &&
6141 nrr->role != htonl(NX_ROLE_SLAVE)) {
6142 return OFPERR_OFPRRFC_BAD_ROLE;
6143 }
6ea4776b 6144
f4f1ea7e
BP
6145 rr->role = ntohl(nrr->role) + 1;
6146 rr->have_generation_id = false;
6147 rr->generation_id = 0;
6148 } else {
428b2edd 6149 OVS_NOT_REACHED();
6ea4776b
JR
6150 }
6151
6ea4776b
JR
6152 return 0;
6153}
6154
6155/* Returns an encoded form of a role reply suitable for the "request" in a
6156 * buffer owned by the caller. */
6157struct ofpbuf *
6158ofputil_encode_role_reply(const struct ofp_header *request,
f4f1ea7e 6159 const struct ofputil_role_request *rr)
6ea4776b 6160{
6ea4776b 6161 struct ofpbuf *buf;
6ea4776b
JR
6162 enum ofpraw raw;
6163
f4f1ea7e 6164 raw = ofpraw_decode_assert(request);
6ea4776b 6165 if (raw == OFPRAW_OFPT12_ROLE_REQUEST) {
f4f1ea7e 6166 struct ofp12_role_request *orr;
6ea4776b 6167
f4f1ea7e
BP
6168 buf = ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY, request, 0);
6169 orr = ofpbuf_put_zeros(buf, sizeof *orr);
6ea4776b 6170
147cc9d3
BP
6171 orr->role = htonl(rr->role);
6172 orr->generation_id = htonll(rr->have_generation_id
6173 ? rr->generation_id
6174 : UINT64_MAX);
f4f1ea7e
BP
6175 } else if (raw == OFPRAW_NXT_ROLE_REQUEST) {
6176 struct nx_role_request *nrr;
6177
6178 BUILD_ASSERT(NX_ROLE_OTHER == OFPCR12_ROLE_EQUAL - 1);
6179 BUILD_ASSERT(NX_ROLE_MASTER == OFPCR12_ROLE_MASTER - 1);
6180 BUILD_ASSERT(NX_ROLE_SLAVE == OFPCR12_ROLE_SLAVE - 1);
6181
6182 buf = ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY, request, 0);
6183 nrr = ofpbuf_put_zeros(buf, sizeof *nrr);
6184 nrr->role = htonl(rr->role - 1);
6185 } else {
428b2edd 6186 OVS_NOT_REACHED();
6ea4776b 6187 }
6ea4776b
JR
6188
6189 return buf;
6190}
6191\f
6751a4b4
BP
6192/* Encodes "role status" message 'status' for sending in the given
6193 * 'protocol'. Returns the role status message, if 'protocol' supports them,
6194 * otherwise a null pointer. */
00467f73
AC
6195struct ofpbuf *
6196ofputil_encode_role_status(const struct ofputil_role_status *status,
6197 enum ofputil_protocol protocol)
6198{
00467f73 6199 enum ofp_version version;
00467f73
AC
6200
6201 version = ofputil_protocol_to_ofp_version(protocol);
6751a4b4
BP
6202 if (version >= OFP14_VERSION) {
6203 struct ofp14_role_status *rstatus;
6204 struct ofpbuf *buf;
6205
6206 buf = ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS, version, htonl(0),
6207 0);
6208 rstatus = ofpbuf_put_zeros(buf, sizeof *rstatus);
6209 rstatus->role = htonl(status->role);
6210 rstatus->reason = status->reason;
6211 rstatus->generation_id = htonll(status->generation_id);
6212
6213 return buf;
6214 } else {
6215 return NULL;
6216 }
00467f73
AC
6217}
6218
6219enum ofperr
6220ofputil_decode_role_status(const struct ofp_header *oh,
6221 struct ofputil_role_status *rs)
6222{
0a2869d5
BP
6223 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
6224 enum ofpraw raw = ofpraw_pull_assert(&b);
00467f73
AC
6225 ovs_assert(raw == OFPRAW_OFPT14_ROLE_STATUS);
6226
0a2869d5 6227 const struct ofp14_role_status *r = b.msg;
00467f73
AC
6228 if (r->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
6229 r->role != htonl(OFPCR12_ROLE_EQUAL) &&
6230 r->role != htonl(OFPCR12_ROLE_MASTER) &&
6231 r->role != htonl(OFPCR12_ROLE_SLAVE)) {
6232 return OFPERR_OFPRRFC_BAD_ROLE;
6233 }
6234
6235 rs->role = ntohl(r->role);
6236 rs->generation_id = ntohll(r->generation_id);
6237 rs->reason = r->reason;
6238
6239 return 0;
6240}
6241
3c35db62
NR
6242/* Encodes 'rf' according to 'protocol', and returns the encoded message.
6243 * 'protocol' must be for OpenFlow 1.4 or later. */
6244struct ofpbuf *
6245ofputil_encode_requestforward(const struct ofputil_requestforward *rf,
6246 enum ofputil_protocol protocol)
6247{
6248 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
6249 struct ofpbuf *inner;
6250
6251 switch (rf->reason) {
6252 case OFPRFR_GROUP_MOD:
6253 inner = ofputil_encode_group_mod(ofp_version, rf->group_mod);
6254 break;
6255
6256 case OFPRFR_METER_MOD:
6257 inner = ofputil_encode_meter_mod(ofp_version, rf->meter_mod);
6258 break;
6259
d18cc1ee 6260 case OFPRFR_N_REASONS:
3c35db62
NR
6261 default:
6262 OVS_NOT_REACHED();
6263 }
6264
6265 struct ofp_header *inner_oh = inner->data;
6266 inner_oh->xid = rf->xid;
6267 inner_oh->length = htons(inner->size);
6268
6269 struct ofpbuf *outer = ofpraw_alloc_xid(OFPRAW_OFPT14_REQUESTFORWARD,
6270 ofp_version, htonl(0),
6271 inner->size);
6272 ofpbuf_put(outer, inner->data, inner->size);
6273 ofpbuf_delete(inner);
6274
6275 return outer;
6276}
6277
6278/* Decodes OFPT_REQUESTFORWARD message 'outer'. On success, puts the decoded
6279 * form into '*rf' and returns 0, and the caller is later responsible for
6280 * freeing the content of 'rf', with ofputil_destroy_requestforward(rf). On
6281 * failure, returns an ofperr and '*rf' is indeterminate. */
6282enum ofperr
6283ofputil_decode_requestforward(const struct ofp_header *outer,
6284 struct ofputil_requestforward *rf)
6285{
0a2869d5 6286 struct ofpbuf b = ofpbuf_const_initializer(outer, ntohs(outer->length));
3c35db62
NR
6287
6288 /* Skip past outer message. */
6289 enum ofpraw outer_raw = ofpraw_pull_assert(&b);
6290 ovs_assert(outer_raw == OFPRAW_OFPT14_REQUESTFORWARD);
6291
6292 /* Validate inner message. */
6293 if (b.size < sizeof(struct ofp_header)) {
6294 return OFPERR_OFPBFC_MSG_BAD_LEN;
6295 }
6296 const struct ofp_header *inner = b.data;
6297 unsigned int inner_len = ntohs(inner->length);
6298 if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
6299 return OFPERR_OFPBFC_MSG_BAD_LEN;
6300 }
6301 if (inner->version != outer->version) {
6302 return OFPERR_OFPBRC_BAD_VERSION;
6303 }
6304
6305 /* Parse inner message. */
6306 enum ofptype type;
0a2869d5 6307 enum ofperr error = ofptype_decode(&type, inner);
3c35db62
NR
6308 if (error) {
6309 return error;
6310 }
6311
6312 rf->xid = inner->xid;
6313 if (type == OFPTYPE_GROUP_MOD) {
6314 rf->reason = OFPRFR_GROUP_MOD;
6315 rf->group_mod = xmalloc(sizeof *rf->group_mod);
6316 error = ofputil_decode_group_mod(inner, rf->group_mod);
6317 if (error) {
6318 free(rf->group_mod);
6319 return error;
6320 }
6321 } else if (type == OFPTYPE_METER_MOD) {
6322 rf->reason = OFPRFR_METER_MOD;
6323 rf->meter_mod = xmalloc(sizeof *rf->meter_mod);
6324 ofpbuf_init(&rf->bands, 64);
6325 error = ofputil_decode_meter_mod(inner, rf->meter_mod, &rf->bands);
6326 if (error) {
6327 free(rf->meter_mod);
6328 ofpbuf_uninit(&rf->bands);
6329 return error;
6330 }
6331 } else {
6332 return OFPERR_OFPBFC_MSG_UNSUP;
6333 }
6334
6335 return 0;
6336}
6337
6338/* Frees the content of 'rf', which should have been initialized through a
6339 * successful call to ofputil_decode_requestforward(). */
6340void
6341ofputil_destroy_requestforward(struct ofputil_requestforward *rf)
6342{
6343 if (!rf) {
6344 return;
6345 }
6346
6347 switch (rf->reason) {
6348 case OFPRFR_GROUP_MOD:
6349 ofputil_uninit_group_mod(rf->group_mod);
6350 free(rf->group_mod);
6351 break;
6352
6353 case OFPRFR_METER_MOD:
6354 ofpbuf_uninit(&rf->bands);
6355 free(rf->meter_mod);
d18cc1ee
AA
6356 break;
6357
6358 case OFPRFR_N_REASONS:
6359 OVS_NOT_REACHED();
3c35db62
NR
6360 }
6361}
6362
307975da
SH
6363/* Table stats. */
6364
3c1bb396
BP
6365/* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
6366 * matched and a field that must be wildcarded. This function returns a bitmap
6367 * that contains both kinds of fields. */
6368static struct mf_bitmap
6369wild_or_nonmatchable_fields(const struct ofputil_table_features *features)
6370{
6371 struct mf_bitmap wc = features->match;
6372 bitmap_not(wc.bm, MFF_N_IDS);
6373 bitmap_or(wc.bm, features->wildcard.bm, MFF_N_IDS);
6374 return wc;
6375}
6376
6377struct ofp10_wc_map {
6378 enum ofp10_flow_wildcards wc10;
6379 enum mf_field_id mf;
6380};
6381
6382static const struct ofp10_wc_map ofp10_wc_map[] = {
6383 { OFPFW10_IN_PORT, MFF_IN_PORT },
6384 { OFPFW10_DL_VLAN, MFF_VLAN_VID },
6385 { OFPFW10_DL_SRC, MFF_ETH_SRC },
6386 { OFPFW10_DL_DST, MFF_ETH_DST},
6387 { OFPFW10_DL_TYPE, MFF_ETH_TYPE },
6388 { OFPFW10_NW_PROTO, MFF_IP_PROTO },
6389 { OFPFW10_TP_SRC, MFF_TCP_SRC },
6390 { OFPFW10_TP_DST, MFF_TCP_DST },
6391 { OFPFW10_NW_SRC_MASK, MFF_IPV4_SRC },
6392 { OFPFW10_NW_DST_MASK, MFF_IPV4_DST },
6393 { OFPFW10_DL_VLAN_PCP, MFF_VLAN_PCP },
6394 { OFPFW10_NW_TOS, MFF_IP_DSCP },
6395};
6396
6397static ovs_be32
6398mf_bitmap_to_of10(const struct mf_bitmap *fields)
6399{
6400 const struct ofp10_wc_map *p;
6401 uint32_t wc10 = 0;
6402
6403 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
6404 if (bitmap_is_set(fields->bm, p->mf)) {
6405 wc10 |= p->wc10;
6406 }
6407 }
6408 return htonl(wc10);
6409}
6410
6411static struct mf_bitmap
6412mf_bitmap_from_of10(ovs_be32 wc10_)
6413{
6414 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
6415 const struct ofp10_wc_map *p;
6416 uint32_t wc10 = ntohl(wc10_);
6417
6418 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
6419 if (wc10 & p->wc10) {
6420 bitmap_set1(fields.bm, p->mf);
6421 }
6422 }
6423 return fields;
6424}
6425
307975da 6426static void
3c1bb396
BP
6427ofputil_put_ofp10_table_stats(const struct ofputil_table_stats *stats,
6428 const struct ofputil_table_features *features,
307975da
SH
6429 struct ofpbuf *buf)
6430{
3c1bb396 6431 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
307975da 6432 struct ofp10_table_stats *out;
307975da 6433
3bdc692b 6434 out = ofpbuf_put_zeros(buf, sizeof *out);
3c1bb396 6435 out->table_id = features->table_id;
f9ac0f03 6436 ovs_strlcpy_arrays(out->name, features->name);
3c1bb396
BP
6437 out->wildcards = mf_bitmap_to_of10(&wc);
6438 out->max_entries = htonl(features->max_entries);
6439 out->active_count = htonl(stats->active_count);
6440 put_32aligned_be64(&out->lookup_count, htonll(stats->lookup_count));
6441 put_32aligned_be64(&out->matched_count, htonll(stats->matched_count));
6442}
6443
6444struct ofp11_wc_map {
6445 enum ofp11_flow_match_fields wc11;
6446 enum mf_field_id mf;
6447};
6448
6449static const struct ofp11_wc_map ofp11_wc_map[] = {
6450 { OFPFMF11_IN_PORT, MFF_IN_PORT },
6451 { OFPFMF11_DL_VLAN, MFF_VLAN_VID },
6452 { OFPFMF11_DL_VLAN_PCP, MFF_VLAN_PCP },
6453 { OFPFMF11_DL_TYPE, MFF_ETH_TYPE },
6454 { OFPFMF11_NW_TOS, MFF_IP_DSCP },
6455 { OFPFMF11_NW_PROTO, MFF_IP_PROTO },
6456 { OFPFMF11_TP_SRC, MFF_TCP_SRC },
6457 { OFPFMF11_TP_DST, MFF_TCP_DST },
6458 { OFPFMF11_MPLS_LABEL, MFF_MPLS_LABEL },
6459 { OFPFMF11_MPLS_TC, MFF_MPLS_TC },
6460 /* I don't know what OFPFMF11_TYPE means. */
6461 { OFPFMF11_DL_SRC, MFF_ETH_SRC },
6462 { OFPFMF11_DL_DST, MFF_ETH_DST },
6463 { OFPFMF11_NW_SRC, MFF_IPV4_SRC },
6464 { OFPFMF11_NW_DST, MFF_IPV4_DST },
6465 { OFPFMF11_METADATA, MFF_METADATA },
6466};
6467
6468static ovs_be32
6469mf_bitmap_to_of11(const struct mf_bitmap *fields)
6470{
6471 const struct ofp11_wc_map *p;
6472 uint32_t wc11 = 0;
6473
6474 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
6475 if (bitmap_is_set(fields->bm, p->mf)) {
6476 wc11 |= p->wc11;
307975da
SH
6477 }
6478 }
3c1bb396 6479 return htonl(wc11);
307975da
SH
6480}
6481
3c1bb396
BP
6482static struct mf_bitmap
6483mf_bitmap_from_of11(ovs_be32 wc11_)
6484{
6485 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
6486 const struct ofp11_wc_map *p;
6487 uint32_t wc11 = ntohl(wc11_);
6488
6489 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
6490 if (wc11 & p->wc11) {
6491 bitmap_set1(fields.bm, p->mf);
307975da
SH
6492 }
6493 }
3c1bb396 6494 return fields;
307975da
SH
6495}
6496
6497static void
3c1bb396
BP
6498ofputil_put_ofp11_table_stats(const struct ofputil_table_stats *stats,
6499 const struct ofputil_table_features *features,
307975da
SH
6500 struct ofpbuf *buf)
6501{
3c1bb396 6502 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
307975da
SH
6503 struct ofp11_table_stats *out;
6504
3bdc692b 6505 out = ofpbuf_put_zeros(buf, sizeof *out);
3c1bb396 6506 out->table_id = features->table_id;
f9ac0f03 6507 ovs_strlcpy_arrays(out->name, features->name);
3c1bb396
BP
6508 out->wildcards = mf_bitmap_to_of11(&wc);
6509 out->match = mf_bitmap_to_of11(&features->match);
6510 out->instructions = ovsinst_bitmap_to_openflow(
6511 features->nonmiss.instructions, OFP11_VERSION);
6512 out->write_actions = ofpact_bitmap_to_openflow(
6513 features->nonmiss.write.ofpacts, OFP11_VERSION);
6514 out->apply_actions = ofpact_bitmap_to_openflow(
6515 features->nonmiss.apply.ofpacts, OFP11_VERSION);
6516 out->config = htonl(features->miss_config);
6517 out->max_entries = htonl(features->max_entries);
6518 out->active_count = htonl(stats->active_count);
6519 out->lookup_count = htonll(stats->lookup_count);
6520 out->matched_count = htonll(stats->matched_count);
08d1e234
BP
6521}
6522
6240624b 6523static void
3c1bb396
BP
6524ofputil_put_ofp12_table_stats(const struct ofputil_table_stats *stats,
6525 const struct ofputil_table_features *features,
6240624b
BP
6526 struct ofpbuf *buf)
6527{
08d1e234 6528 struct ofp12_table_stats *out;
6240624b 6529
08d1e234 6530 out = ofpbuf_put_zeros(buf, sizeof *out);
3c1bb396 6531 out->table_id = features->table_id;
f9ac0f03 6532 ovs_strlcpy_arrays(out->name, features->name);
178742f9
BP
6533 out->match = oxm_bitmap_from_mf_bitmap(&features->match, OFP12_VERSION);
6534 out->wildcards = oxm_bitmap_from_mf_bitmap(&features->wildcard,
3c1bb396
BP
6535 OFP12_VERSION);
6536 out->write_actions = ofpact_bitmap_to_openflow(
6537 features->nonmiss.write.ofpacts, OFP12_VERSION);
6538 out->apply_actions = ofpact_bitmap_to_openflow(
6539 features->nonmiss.apply.ofpacts, OFP12_VERSION);
178742f9 6540 out->write_setfields = oxm_bitmap_from_mf_bitmap(
3c1bb396 6541 &features->nonmiss.write.set_fields, OFP12_VERSION);
178742f9 6542 out->apply_setfields = oxm_bitmap_from_mf_bitmap(
3c1bb396
BP
6543 &features->nonmiss.apply.set_fields, OFP12_VERSION);
6544 out->metadata_match = features->metadata_match;
6545 out->metadata_write = features->metadata_write;
6546 out->instructions = ovsinst_bitmap_to_openflow(
6547 features->nonmiss.instructions, OFP12_VERSION);
82c22d34
BP
6548 out->config = ofputil_encode_table_config(features->miss_config,
6549 OFPUTIL_TABLE_EVICTION_DEFAULT,
de7d3c07 6550 OFPUTIL_TABLE_VACANCY_DEFAULT,
82c22d34 6551 OFP12_VERSION);
3c1bb396
BP
6552 out->max_entries = htonl(features->max_entries);
6553 out->active_count = htonl(stats->active_count);
6554 out->lookup_count = htonll(stats->lookup_count);
6555 out->matched_count = htonll(stats->matched_count);
6240624b
BP
6556}
6557
2e1ae200 6558static void
3c1bb396 6559ofputil_put_ofp13_table_stats(const struct ofputil_table_stats *stats,
2e1ae200
JR
6560 struct ofpbuf *buf)
6561{
6562 struct ofp13_table_stats *out;
6563
3c1bb396
BP
6564 out = ofpbuf_put_zeros(buf, sizeof *out);
6565 out->table_id = stats->table_id;
6566 out->active_count = htonl(stats->active_count);
6567 out->lookup_count = htonll(stats->lookup_count);
6568 out->matched_count = htonll(stats->matched_count);
2e1ae200
JR
6569}
6570
307975da 6571struct ofpbuf *
3c1bb396 6572ofputil_encode_table_stats_reply(const struct ofp_header *request)
307975da 6573{
3c1bb396
BP
6574 return ofpraw_alloc_stats_reply(request, 0);
6575}
307975da 6576
3c1bb396
BP
6577void
6578ofputil_append_table_stats_reply(struct ofpbuf *reply,
6579 const struct ofputil_table_stats *stats,
6580 const struct ofputil_table_features *features)
6581{
6fd6ed71 6582 struct ofp_header *oh = reply->header;
307975da 6583
3c1bb396 6584 ovs_assert(stats->table_id == features->table_id);
307975da 6585
3c1bb396
BP
6586 switch ((enum ofp_version) oh->version) {
6587 case OFP10_VERSION:
6588 ofputil_put_ofp10_table_stats(stats, features, reply);
6589 break;
307975da 6590
3c1bb396
BP
6591 case OFP11_VERSION:
6592 ofputil_put_ofp11_table_stats(stats, features, reply);
6593 break;
307975da 6594
3c1bb396
BP
6595 case OFP12_VERSION:
6596 ofputil_put_ofp12_table_stats(stats, features, reply);
6597 break;
2e1ae200 6598
3c1bb396
BP
6599 case OFP13_VERSION:
6600 case OFP14_VERSION:
6601 case OFP15_VERSION:
b79d45a1 6602 case OFP16_VERSION:
3c1bb396
BP
6603 ofputil_put_ofp13_table_stats(stats, reply);
6604 break;
6605
6606 default:
6607 OVS_NOT_REACHED();
307975da 6608 }
3c1bb396 6609}
307975da 6610
3c1bb396
BP
6611static int
6612ofputil_decode_ofp10_table_stats(struct ofpbuf *msg,
6613 struct ofputil_table_stats *stats,
6614 struct ofputil_table_features *features)
6615{
6616 struct ofp10_table_stats *ots;
6617
6618 ots = ofpbuf_try_pull(msg, sizeof *ots);
6619 if (!ots) {
6620 return OFPERR_OFPBRC_BAD_LEN;
6621 }
6622
6623 features->table_id = ots->table_id;
f9ac0f03 6624 ovs_strlcpy_arrays(features->name, ots->name);
3c1bb396
BP
6625 features->max_entries = ntohl(ots->max_entries);
6626 features->match = features->wildcard = mf_bitmap_from_of10(ots->wildcards);
6627
6628 stats->table_id = ots->table_id;
6629 stats->active_count = ntohl(ots->active_count);
6630 stats->lookup_count = ntohll(get_32aligned_be64(&ots->lookup_count));
6631 stats->matched_count = ntohll(get_32aligned_be64(&ots->matched_count));
6632
6633 return 0;
6634}
6635
6636static int
6637ofputil_decode_ofp11_table_stats(struct ofpbuf *msg,
6638 struct ofputil_table_stats *stats,
6639 struct ofputil_table_features *features)
6640{
6641 struct ofp11_table_stats *ots;
6642
6643 ots = ofpbuf_try_pull(msg, sizeof *ots);
6644 if (!ots) {
6645 return OFPERR_OFPBRC_BAD_LEN;
6646 }
6647
6648 features->table_id = ots->table_id;
f9ac0f03 6649 ovs_strlcpy_arrays(features->name, ots->name);
3c1bb396
BP
6650 features->max_entries = ntohl(ots->max_entries);
6651 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
6652 ots->instructions, OFP11_VERSION);
6653 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
6654 ots->write_actions, OFP11_VERSION);
6655 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
6656 ots->write_actions, OFP11_VERSION);
6657 features->miss = features->nonmiss;
82c22d34
BP
6658 features->miss_config = ofputil_decode_table_miss(ots->config,
6659 OFP11_VERSION);
3c1bb396
BP
6660 features->match = mf_bitmap_from_of11(ots->match);
6661 features->wildcard = mf_bitmap_from_of11(ots->wildcards);
6662 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
6663
6664 stats->table_id = ots->table_id;
6665 stats->active_count = ntohl(ots->active_count);
6666 stats->lookup_count = ntohll(ots->lookup_count);
6667 stats->matched_count = ntohll(ots->matched_count);
6668
6669 return 0;
6670}
6671
6672static int
6673ofputil_decode_ofp12_table_stats(struct ofpbuf *msg,
6674 struct ofputil_table_stats *stats,
6675 struct ofputil_table_features *features)
6676{
6677 struct ofp12_table_stats *ots;
6678
6679 ots = ofpbuf_try_pull(msg, sizeof *ots);
6680 if (!ots) {
6681 return OFPERR_OFPBRC_BAD_LEN;
6682 }
6683
6684 features->table_id = ots->table_id;
f9ac0f03 6685 ovs_strlcpy_arrays(features->name, ots->name);
3c1bb396
BP
6686 features->metadata_match = ots->metadata_match;
6687 features->metadata_write = ots->metadata_write;
82c22d34
BP
6688 features->miss_config = ofputil_decode_table_miss(ots->config,
6689 OFP12_VERSION);
3c1bb396
BP
6690 features->max_entries = ntohl(ots->max_entries);
6691
6692 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
6693 ots->instructions, OFP12_VERSION);
6694 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
6695 ots->write_actions, OFP12_VERSION);
6696 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
6697 ots->apply_actions, OFP12_VERSION);
178742f9 6698 features->nonmiss.write.set_fields = oxm_bitmap_to_mf_bitmap(
3c1bb396 6699 ots->write_setfields, OFP12_VERSION);
178742f9 6700 features->nonmiss.apply.set_fields = oxm_bitmap_to_mf_bitmap(
3c1bb396
BP
6701 ots->apply_setfields, OFP12_VERSION);
6702 features->miss = features->nonmiss;
6703
178742f9
BP
6704 features->match = oxm_bitmap_to_mf_bitmap(ots->match, OFP12_VERSION);
6705 features->wildcard = oxm_bitmap_to_mf_bitmap(ots->wildcards,
6706 OFP12_VERSION);
3c1bb396
BP
6707 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
6708
6709 stats->table_id = ots->table_id;
6710 stats->active_count = ntohl(ots->active_count);
6711 stats->lookup_count = ntohll(ots->lookup_count);
6712 stats->matched_count = ntohll(ots->matched_count);
6713
6714 return 0;
6715}
6716
6717static int
6718ofputil_decode_ofp13_table_stats(struct ofpbuf *msg,
6719 struct ofputil_table_stats *stats,
6720 struct ofputil_table_features *features)
6721{
6722 struct ofp13_table_stats *ots;
6723
6724 ots = ofpbuf_try_pull(msg, sizeof *ots);
6725 if (!ots) {
6726 return OFPERR_OFPBRC_BAD_LEN;
6727 }
6728
6729 features->table_id = ots->table_id;
6730
6731 stats->table_id = ots->table_id;
6732 stats->active_count = ntohl(ots->active_count);
6733 stats->lookup_count = ntohll(ots->lookup_count);
6734 stats->matched_count = ntohll(ots->matched_count);
6735
6736 return 0;
6737}
6738
6739int
6740ofputil_decode_table_stats_reply(struct ofpbuf *msg,
6741 struct ofputil_table_stats *stats,
6742 struct ofputil_table_features *features)
6743{
6744 const struct ofp_header *oh;
6745
6fd6ed71 6746 if (!msg->header) {
3c1bb396
BP
6747 ofpraw_pull_assert(msg);
6748 }
6fd6ed71 6749 oh = msg->header;
3c1bb396 6750
6fd6ed71 6751 if (!msg->size) {
3c1bb396
BP
6752 return EOF;
6753 }
6754
6755 memset(stats, 0, sizeof *stats);
6756 memset(features, 0, sizeof *features);
82c22d34
BP
6757 features->supports_eviction = -1;
6758 features->supports_vacancy_events = -1;
3c1bb396
BP
6759
6760 switch ((enum ofp_version) oh->version) {
6761 case OFP10_VERSION:
6762 return ofputil_decode_ofp10_table_stats(msg, stats, features);
6763
6764 case OFP11_VERSION:
6765 return ofputil_decode_ofp11_table_stats(msg, stats, features);
6766
6767 case OFP12_VERSION:
6768 return ofputil_decode_ofp12_table_stats(msg, stats, features);
6769
6770 case OFP13_VERSION:
6771 case OFP14_VERSION:
6772 case OFP15_VERSION:
b79d45a1 6773 case OFP16_VERSION:
3c1bb396
BP
6774 return ofputil_decode_ofp13_table_stats(msg, stats, features);
6775
6776 default:
6777 OVS_NOT_REACHED();
6778 }
307975da
SH
6779}
6780\f
2b07c8b1
BP
6781/* ofputil_flow_monitor_request */
6782
6783/* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
6784 * ofputil_flow_monitor_request in 'rq'.
6785 *
6786 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
6787 * message. Calling this function multiple times for a single 'msg' iterates
6788 * through the requests. The caller must initially leave 'msg''s layer
6789 * pointers null and not modify them between calls.
6790 *
6791 * Returns 0 if successful, EOF if no requests were left in this 'msg',
6792 * otherwise an OFPERR_* value. */
6793int
6794ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
6795 struct ofpbuf *msg)
6796{
6797 struct nx_flow_monitor_request *nfmr;
6798 uint16_t flags;
6799
6fd6ed71 6800 if (!msg->header) {
982697a4 6801 ofpraw_pull_assert(msg);
2b07c8b1
BP
6802 }
6803
6fd6ed71 6804 if (!msg->size) {
2b07c8b1
BP
6805 return EOF;
6806 }
6807
6808 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
6809 if (!nfmr) {
437d0d22 6810 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %"PRIu32" "
6fd6ed71 6811 "leftover bytes at end", msg->size);
2b07c8b1
BP
6812 return OFPERR_OFPBRC_BAD_LEN;
6813 }
6814
6815 flags = ntohs(nfmr->flags);
6816 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
6817 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
6818 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
6819 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
6820 flags);
04f9f286 6821 return OFPERR_OFPMOFC_BAD_FLAGS;
2b07c8b1
BP
6822 }
6823
6824 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
6825 return OFPERR_NXBRC_MUST_BE_ZERO;
6826 }
6827
6828 rq->id = ntohl(nfmr->id);
6829 rq->flags = flags;
4e022ec0 6830 rq->out_port = u16_to_ofp(ntohs(nfmr->out_port));
2b07c8b1
BP
6831 rq->table_id = nfmr->table_id;
6832
8d8ab6c2 6833 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL,
3cddeff0 6834 NULL, NULL, NULL);
2b07c8b1
BP
6835}
6836
6837void
6838ofputil_append_flow_monitor_request(
6839 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
6840{
6841 struct nx_flow_monitor_request *nfmr;
6842 size_t start_ofs;
6843 int match_len;
6844
6fd6ed71 6845 if (!msg->size) {
982697a4 6846 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
2b07c8b1
BP
6847 }
6848
6fd6ed71 6849 start_ofs = msg->size;
2b07c8b1 6850 ofpbuf_put_zeros(msg, sizeof *nfmr);
7623f4dd 6851 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
2b07c8b1
BP
6852
6853 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
6854 nfmr->id = htonl(rq->id);
6855 nfmr->flags = htons(rq->flags);
4e022ec0 6856 nfmr->out_port = htons(ofp_to_u16(rq->out_port));
2b07c8b1
BP
6857 nfmr->match_len = htons(match_len);
6858 nfmr->table_id = rq->table_id;
6859}
6860
6861/* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
6862 * into an abstract ofputil_flow_update in 'update'. The caller must have
81a76618 6863 * initialized update->match to point to space allocated for a match.
2b07c8b1
BP
6864 *
6865 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
6866 * actions (except for NXFME_ABBREV, which never includes actions). The caller
6867 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
6868 * will point into the 'ofpacts' buffer.
6869 *
6870 * Multiple flow updates can be packed into a single OpenFlow message. Calling
6871 * this function multiple times for a single 'msg' iterates through the
6872 * updates. The caller must initially leave 'msg''s layer pointers null and
6873 * not modify them between calls.
6874 *
6875 * Returns 0 if successful, EOF if no updates were left in this 'msg',
6876 * otherwise an OFPERR_* value. */
6877int
6878ofputil_decode_flow_update(struct ofputil_flow_update *update,
6879 struct ofpbuf *msg, struct ofpbuf *ofpacts)
6880{
6881 struct nx_flow_update_header *nfuh;
6882 unsigned int length;
e3f8f887 6883 struct ofp_header *oh;
2b07c8b1 6884
6fd6ed71 6885 if (!msg->header) {
982697a4 6886 ofpraw_pull_assert(msg);
2b07c8b1
BP
6887 }
6888
9abca1e5 6889 ofpbuf_clear(ofpacts);
6fd6ed71 6890 if (!msg->size) {
2b07c8b1
BP
6891 return EOF;
6892 }
6893
6fd6ed71 6894 if (msg->size < sizeof(struct nx_flow_update_header)) {
2b07c8b1
BP
6895 goto bad_len;
6896 }
6897
6fd6ed71 6898 oh = msg->header;
e3f8f887 6899
6fd6ed71 6900 nfuh = msg->data;
2b07c8b1
BP
6901 update->event = ntohs(nfuh->event);
6902 length = ntohs(nfuh->length);
6fd6ed71 6903 if (length > msg->size || length % 8) {
2b07c8b1
BP
6904 goto bad_len;
6905 }
6906
6907 if (update->event == NXFME_ABBREV) {
6908 struct nx_flow_update_abbrev *nfua;
6909
6910 if (length != sizeof *nfua) {
6911 goto bad_len;
6912 }
6913
6914 nfua = ofpbuf_pull(msg, sizeof *nfua);
6915 update->xid = nfua->xid;
6916 return 0;
6917 } else if (update->event == NXFME_ADDED
6918 || update->event == NXFME_DELETED
6919 || update->event == NXFME_MODIFIED) {
6920 struct nx_flow_update_full *nfuf;
6921 unsigned int actions_len;
6922 unsigned int match_len;
6923 enum ofperr error;
6924
6925 if (length < sizeof *nfuf) {
6926 goto bad_len;
6927 }
6928
6929 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
6930 match_len = ntohs(nfuf->match_len);
6931 if (sizeof *nfuf + match_len > length) {
6932 goto bad_len;
6933 }
6934
6935 update->reason = ntohs(nfuf->reason);
6936 update->idle_timeout = ntohs(nfuf->idle_timeout);
6937 update->hard_timeout = ntohs(nfuf->hard_timeout);
6938 update->table_id = nfuf->table_id;
6939 update->cookie = nfuf->cookie;
81a76618 6940 update->priority = ntohs(nfuf->priority);
2b07c8b1 6941
3cddeff0
YHW
6942 error = nx_pull_match(msg, match_len, &update->match, NULL, NULL, NULL,
6943 NULL);
2b07c8b1
BP
6944 if (error) {
6945 return error;
6946 }
6947
6948 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
e3f8f887 6949 error = ofpacts_pull_openflow_actions(msg, actions_len, oh->version,
5c7c16d8 6950 NULL, NULL, ofpacts);
2b07c8b1
BP
6951 if (error) {
6952 return error;
6953 }
6954
6fd6ed71
PS
6955 update->ofpacts = ofpacts->data;
6956 update->ofpacts_len = ofpacts->size;
2b07c8b1
BP
6957 return 0;
6958 } else {
6959 VLOG_WARN_RL(&bad_ofmsg_rl,
6960 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
6961 ntohs(nfuh->event));
15549878 6962 return OFPERR_NXBRC_FM_BAD_EVENT;
2b07c8b1
BP
6963 }
6964
6965bad_len:
437d0d22 6966 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %"PRIu32" "
6fd6ed71 6967 "leftover bytes at end", msg->size);
2b07c8b1
BP
6968 return OFPERR_OFPBRC_BAD_LEN;
6969}
6970
6971uint32_t
6972ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
6973{
982697a4
BP
6974 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
6975
6976 return ntohl(cancel->id);
2b07c8b1 6977}
9e1fd49b 6978
2b07c8b1
BP
6979struct ofpbuf *
6980ofputil_encode_flow_monitor_cancel(uint32_t id)
6981{
6982 struct nx_flow_monitor_cancel *nfmc;
6983 struct ofpbuf *msg;
6984
982697a4
BP
6985 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
6986 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
2b07c8b1
BP
6987 nfmc->id = htonl(id);
6988 return msg;
6989}
6990
6991void
ca6ba700 6992ofputil_start_flow_update(struct ovs_list *replies)
2b07c8b1
BP
6993{
6994 struct ofpbuf *msg;
6995
982697a4
BP
6996 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
6997 htonl(0), 1024);
2b07c8b1 6998
417e7e66
BW
6999 ovs_list_init(replies);
7000 ovs_list_push_back(replies, &msg->list_node);
2b07c8b1
BP
7001}
7002
7003void
7004ofputil_append_flow_update(const struct ofputil_flow_update *update,
140f36ba
DDP
7005 struct ovs_list *replies,
7006 const struct tun_table *tun_table)
2b07c8b1 7007{
140f36ba
DDP
7008 struct ofputil_flow_update *update_ =
7009 CONST_CAST(struct ofputil_flow_update *, update);
7010 const struct tun_table *orig_tun_table;
e28ac5cf 7011 enum ofp_version version = ofpmp_version(replies);
2b07c8b1
BP
7012 struct nx_flow_update_header *nfuh;
7013 struct ofpbuf *msg;
7014 size_t start_ofs;
7015
140f36ba
DDP
7016 orig_tun_table = update->match.flow.tunnel.metadata.tab;
7017 update_->match.flow.tunnel.metadata.tab = tun_table;
7018
417e7e66 7019 msg = ofpbuf_from_list(ovs_list_back(replies));
6fd6ed71 7020 start_ofs = msg->size;
2b07c8b1
BP
7021
7022 if (update->event == NXFME_ABBREV) {
7023 struct nx_flow_update_abbrev *nfua;
7024
7025 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
7026 nfua->xid = update->xid;
7027 } else {
7028 struct nx_flow_update_full *nfuf;
7029 int match_len;
7030
7031 ofpbuf_put_zeros(msg, sizeof *nfuf);
140f36ba 7032 match_len = nx_put_match(msg, &update->match, htonll(0), htonll(0));
e3f8f887
JR
7033 ofpacts_put_openflow_actions(update->ofpacts, update->ofpacts_len, msg,
7034 version);
2b07c8b1
BP
7035 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
7036 nfuf->reason = htons(update->reason);
81a76618 7037 nfuf->priority = htons(update->priority);
2b07c8b1
BP
7038 nfuf->idle_timeout = htons(update->idle_timeout);
7039 nfuf->hard_timeout = htons(update->hard_timeout);
7040 nfuf->match_len = htons(match_len);
7041 nfuf->table_id = update->table_id;
7042 nfuf->cookie = update->cookie;
7043 }
7044
7045 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
6fd6ed71 7046 nfuh->length = htons(msg->size - start_ofs);
2b07c8b1
BP
7047 nfuh->event = htons(update->event);
7048
982697a4 7049 ofpmp_postappend(replies, start_ofs);
140f36ba 7050 update_->match.flow.tunnel.metadata.tab = orig_tun_table;
2b07c8b1
BP
7051}
7052\f
c6a93eb7 7053struct ofpbuf *
de0f3156
SH
7054ofputil_encode_packet_out(const struct ofputil_packet_out *po,
7055 enum ofputil_protocol protocol)
c6a93eb7 7056{
de0f3156 7057 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
c6a93eb7
BP
7058 struct ofpbuf *msg;
7059 size_t size;
7060
982697a4 7061 size = po->ofpacts_len;
c6a93eb7
BP
7062 if (po->buffer_id == UINT32_MAX) {
7063 size += po->packet_len;
7064 }
7065
de0f3156
SH
7066 switch (ofp_version) {
7067 case OFP10_VERSION: {
31a9e63f 7068 struct ofp10_packet_out *opo;
de0f3156
SH
7069 size_t actions_ofs;
7070
7071 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
7072 ofpbuf_put_zeros(msg, sizeof *opo);
6fd6ed71 7073 actions_ofs = msg->size;
e3f8f887
JR
7074 ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
7075 ofp_version);
de0f3156 7076
6fd6ed71 7077 opo = msg->msg;
de0f3156 7078 opo->buffer_id = htonl(po->buffer_id);
35eb6326
YHW
7079 opo->in_port =htons(ofp_to_u16(
7080 po->flow_metadata.flow.in_port.ofp_port));
6fd6ed71 7081 opo->actions_len = htons(msg->size - actions_ofs);
de0f3156
SH
7082 break;
7083 }
f25d0cf3 7084
de0f3156 7085 case OFP11_VERSION:
2e1ae200 7086 case OFP12_VERSION:
c37c0382 7087 case OFP13_VERSION:
577bfa9f 7088 case OFP14_VERSION: {
7c1b1a0d
SH
7089 struct ofp11_packet_out *opo;
7090 size_t len;
7091
7092 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
7093 ofpbuf_put_zeros(msg, sizeof *opo);
e3f8f887
JR
7094 len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
7095 ofp_version);
6fd6ed71 7096 opo = msg->msg;
7c1b1a0d 7097 opo->buffer_id = htonl(po->buffer_id);
35eb6326
YHW
7098 opo->in_port =
7099 ofputil_port_to_ofp11(po->flow_metadata.flow.in_port.ofp_port);
7c1b1a0d
SH
7100 opo->actions_len = htons(len);
7101 break;
7102 }
7103
577bfa9f
YHW
7104 case OFP15_VERSION:
7105 case OFP16_VERSION: {
7106 struct ofp15_packet_out *opo;
7107 size_t len;
7108
7109 /* The final argument is just an estimate of the space required. */
7110 msg = ofpraw_alloc(OFPRAW_OFPT15_PACKET_OUT, ofp_version,
7111 size + NXM_TYPICAL_LEN);
7112 ofpbuf_put_zeros(msg, sizeof *opo);
7113 oxm_put_match(msg, &po->flow_metadata, ofp_version);
7114 len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
7115 ofp_version);
7116 opo = msg->msg;
7117 opo->buffer_id = htonl(po->buffer_id);
7118 opo->actions_len = htons(len);
7119 break;
7120 }
7121
de0f3156 7122 default:
428b2edd 7123 OVS_NOT_REACHED();
de0f3156 7124 }
f25d0cf3 7125
c6a93eb7
BP
7126 if (po->buffer_id == UINT32_MAX) {
7127 ofpbuf_put(msg, po->packet, po->packet_len);
7128 }
f25d0cf3 7129
982697a4 7130 ofpmsg_update_length(msg);
c6a93eb7
BP
7131
7132 return msg;
7133}
d1e2cf21 7134\f
fa37b408
BP
7135/* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
7136struct ofpbuf *
1a126c0c 7137make_echo_request(enum ofp_version ofp_version)
fa37b408 7138{
1a126c0c 7139 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
982697a4 7140 htonl(0), 0);
fa37b408
BP
7141}
7142
7143/* Creates and returns an OFPT_ECHO_REPLY message matching the
7144 * OFPT_ECHO_REQUEST message in 'rq'. */
7145struct ofpbuf *
7146make_echo_reply(const struct ofp_header *rq)
7147{
0a2869d5 7148 struct ofpbuf rq_buf = ofpbuf_const_initializer(rq, ntohs(rq->length));
982697a4
BP
7149 ofpraw_pull_assert(&rq_buf);
7150
0a2869d5
BP
7151 struct ofpbuf *reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY,
7152 rq, rq_buf.size);
6fd6ed71 7153 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
982697a4 7154 return reply;
fa37b408
BP
7155}
7156
efb80167 7157struct ofpbuf *
a0ae0b6e 7158ofputil_encode_barrier_request(enum ofp_version ofp_version)
efb80167 7159{
a0ae0b6e
SH
7160 enum ofpraw type;
7161
7162 switch (ofp_version) {
b79d45a1 7163 case OFP16_VERSION:
42dccab5 7164 case OFP15_VERSION:
c37c0382 7165 case OFP14_VERSION:
2e1ae200 7166 case OFP13_VERSION:
a0ae0b6e
SH
7167 case OFP12_VERSION:
7168 case OFP11_VERSION:
7169 type = OFPRAW_OFPT11_BARRIER_REQUEST;
7170 break;
7171
7172 case OFP10_VERSION:
7173 type = OFPRAW_OFPT10_BARRIER_REQUEST;
7174 break;
7175
7176 default:
428b2edd 7177 OVS_NOT_REACHED();
a0ae0b6e
SH
7178 }
7179
7180 return ofpraw_alloc(type, ofp_version, 0);
efb80167
BP
7181}
7182
7257b535 7183const char *
ad99e2ed 7184ofputil_frag_handling_to_string(enum ofputil_frag_handling frag)
7257b535 7185{
ad99e2ed
BP
7186 switch (frag) {
7187 case OFPUTIL_FRAG_NORMAL: return "normal";
7188 case OFPUTIL_FRAG_DROP: return "drop";
7189 case OFPUTIL_FRAG_REASM: return "reassemble";
7190 case OFPUTIL_FRAG_NX_MATCH: return "nx-match";
7257b535
BP
7191 }
7192
428b2edd 7193 OVS_NOT_REACHED();
7257b535
BP
7194}
7195
7196bool
ad99e2ed
BP
7197ofputil_frag_handling_from_string(const char *s,
7198 enum ofputil_frag_handling *frag)
7257b535
BP
7199{
7200 if (!strcasecmp(s, "normal")) {
ad99e2ed 7201 *frag = OFPUTIL_FRAG_NORMAL;
7257b535 7202 } else if (!strcasecmp(s, "drop")) {
ad99e2ed 7203 *frag = OFPUTIL_FRAG_DROP;
7257b535 7204 } else if (!strcasecmp(s, "reassemble")) {
ad99e2ed 7205 *frag = OFPUTIL_FRAG_REASM;
7257b535 7206 } else if (!strcasecmp(s, "nx-match")) {
ad99e2ed 7207 *frag = OFPUTIL_FRAG_NX_MATCH;
7257b535
BP
7208 } else {
7209 return false;
7210 }
7211 return true;
7212}
7213
7b7503ea
BP
7214/* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
7215 * port number and stores the latter in '*ofp10_port', for the purpose of
7216 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
bc146369 7217 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
7b7503ea
BP
7218 *
7219 * See the definition of OFP11_MAX for an explanation of the mapping. */
7220enum ofperr
4e022ec0 7221ofputil_port_from_ofp11(ovs_be32 ofp11_port, ofp_port_t *ofp10_port)
7b7503ea
BP
7222{
7223 uint32_t ofp11_port_h = ntohl(ofp11_port);
7224
4e022ec0
AW
7225 if (ofp11_port_h < ofp_to_u16(OFPP_MAX)) {
7226 *ofp10_port = u16_to_ofp(ofp11_port_h);
7b7503ea 7227 return 0;
4e022ec0
AW
7228 } else if (ofp11_port_h >= ofp11_to_u32(OFPP11_MAX)) {
7229 *ofp10_port = u16_to_ofp(ofp11_port_h - OFPP11_OFFSET);
7b7503ea
BP
7230 return 0;
7231 } else {
bc146369 7232 *ofp10_port = OFPP_NONE;
7b7503ea
BP
7233 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
7234 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
4e022ec0
AW
7235 ofp11_port_h, ofp_to_u16(OFPP_MAX) - 1,
7236 ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
7b7503ea
BP
7237 return OFPERR_OFPBAC_BAD_OUT_PORT;
7238 }
7239}
7240
7241/* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
7242 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
7243 *
7244 * See the definition of OFP11_MAX for an explanation of the mapping. */
7245ovs_be32
4e022ec0 7246ofputil_port_to_ofp11(ofp_port_t ofp10_port)
7b7503ea 7247{
4e022ec0
AW
7248 return htonl(ofp_to_u16(ofp10_port) < ofp_to_u16(OFPP_MAX)
7249 ? ofp_to_u16(ofp10_port)
7250 : ofp_to_u16(ofp10_port) + OFPP11_OFFSET);
7b7503ea
BP
7251}
7252
39dc9082
BP
7253#define OFPUTIL_NAMED_PORTS \
7254 OFPUTIL_NAMED_PORT(IN_PORT) \
7255 OFPUTIL_NAMED_PORT(TABLE) \
7256 OFPUTIL_NAMED_PORT(NORMAL) \
7257 OFPUTIL_NAMED_PORT(FLOOD) \
7258 OFPUTIL_NAMED_PORT(ALL) \
7259 OFPUTIL_NAMED_PORT(CONTROLLER) \
7260 OFPUTIL_NAMED_PORT(LOCAL) \
c61f3870
BP
7261 OFPUTIL_NAMED_PORT(ANY) \
7262 OFPUTIL_NAMED_PORT(UNSET)
7f05e7ab
JR
7263
7264/* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
7265#define OFPUTIL_NAMED_PORTS_WITH_NONE \
7266 OFPUTIL_NAMED_PORTS \
39dc9082
BP
7267 OFPUTIL_NAMED_PORT(NONE)
7268
8010100b
BP
7269/* Stores the port number represented by 's' into '*portp'. 's' may be an
7270 * integer or, for reserved ports, the standard OpenFlow name for the port
7271 * (e.g. "LOCAL").
c6100d92 7272 *
8010100b
BP
7273 * Returns true if successful, false if 's' is not a valid OpenFlow port number
7274 * or name. The caller should issue an error message in this case, because
7275 * this function usually does not. (This gives the caller an opportunity to
7276 * look up the port name another way, e.g. by contacting the switch and listing
7277 * the names of all its ports).
c6100d92
BP
7278 *
7279 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
7280 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
7281 * range as described in include/openflow/openflow-1.1.h. */
8010100b 7282bool
4e022ec0 7283ofputil_port_from_string(const char *s, ofp_port_t *portp)
39dc9082 7284{
5b23ff7e 7285 unsigned int port32; /* int is at least 32 bits wide. */
c6100d92 7286
5b23ff7e
JR
7287 if (*s == '-') {
7288 VLOG_WARN("Negative value %s is not a valid port number.", s);
7289 return false;
7290 }
8010100b 7291 *portp = 0;
c6100d92 7292 if (str_to_uint(s, 10, &port32)) {
4e022ec0
AW
7293 if (port32 < ofp_to_u16(OFPP_MAX)) {
7294 /* Pass. */
7295 } else if (port32 < ofp_to_u16(OFPP_FIRST_RESV)) {
c6100d92
BP
7296 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
7297 "be translated to %u when talking to an OF1.1 or "
7298 "later controller", port32, port32 + OFPP11_OFFSET);
4e022ec0 7299 } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) {
2f2b904f 7300 char name[OFP10_MAX_PORT_NAME_LEN];
28b11432
BP
7301
7302 ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name);
7303 VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated "
7304 "for compatibility with OpenFlow 1.1 and later",
7305 name, port32);
4e022ec0 7306 } else if (port32 < ofp11_to_u32(OFPP11_MAX)) {
c6100d92 7307 VLOG_WARN("port %u is outside the supported range 0 through "
fd13c6b5 7308 "%x or 0x%x through 0x%"PRIx32, port32,
4e022ec0 7309 UINT16_MAX, ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
8010100b 7310 return false;
c6100d92 7311 } else {
4e022ec0 7312 port32 -= OFPP11_OFFSET;
c6100d92 7313 }
4e022ec0
AW
7314
7315 *portp = u16_to_ofp(port32);
7316 return true;
c6100d92
BP
7317 } else {
7318 struct pair {
7319 const char *name;
4e022ec0 7320 ofp_port_t value;
c6100d92
BP
7321 };
7322 static const struct pair pairs[] = {
39dc9082 7323#define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
7f05e7ab 7324 OFPUTIL_NAMED_PORTS_WITH_NONE
39dc9082 7325#undef OFPUTIL_NAMED_PORT
c6100d92
BP
7326 };
7327 const struct pair *p;
39dc9082 7328
c6100d92
BP
7329 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
7330 if (!strcasecmp(s, p->name)) {
8010100b
BP
7331 *portp = p->value;
7332 return true;
c6100d92 7333 }
39dc9082 7334 }
8010100b 7335 return false;
39dc9082 7336 }
39dc9082
BP
7337}
7338
7339/* Appends to 's' a string representation of the OpenFlow port number 'port'.
7340 * Most ports' string representation is just the port number, but for special
7341 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
7342void
4e022ec0 7343ofputil_format_port(ofp_port_t port, struct ds *s)
39dc9082 7344{
2f2b904f 7345 char name[OFP10_MAX_PORT_NAME_LEN];
28b11432
BP
7346
7347 ofputil_port_to_string(port, name, sizeof name);
7348 ds_put_cstr(s, name);
7349}
39dc9082 7350
28b11432
BP
7351/* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7352 * representation of OpenFlow port number 'port'. Most ports are represented
7353 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
7354 * by name, e.g. "LOCAL". */
7355void
7356ofputil_port_to_string(ofp_port_t port,
2f2b904f 7357 char namebuf[OFP10_MAX_PORT_NAME_LEN], size_t bufsize)
28b11432 7358{
39dc9082 7359 switch (port) {
28b11432
BP
7360#define OFPUTIL_NAMED_PORT(NAME) \
7361 case OFPP_##NAME: \
7362 ovs_strlcpy(namebuf, #NAME, bufsize); \
7363 break;
39dc9082
BP
7364 OFPUTIL_NAMED_PORTS
7365#undef OFPUTIL_NAMED_PORT
7366
7367 default:
94783c7c 7368 snprintf(namebuf, bufsize, "%"PRIu32, port);
28b11432 7369 break;
39dc9082 7370 }
39dc9082
BP
7371}
7372
7395c052
NZ
7373/* Stores the group id represented by 's' into '*group_idp'. 's' may be an
7374 * integer or, for reserved group IDs, the standard OpenFlow name for the group
7375 * (either "ANY" or "ALL").
7376 *
7377 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
7378 * name. */
7379bool
7380ofputil_group_from_string(const char *s, uint32_t *group_idp)
7381{
7382 if (!strcasecmp(s, "any")) {
30ef36c6 7383 *group_idp = OFPG_ANY;
7395c052 7384 } else if (!strcasecmp(s, "all")) {
30ef36c6 7385 *group_idp = OFPG_ALL;
7395c052
NZ
7386 } else if (!str_to_uint(s, 10, group_idp)) {
7387 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
7388 "32-bit nonnegative integers or the keywords ANY or "
7389 "ALL.)", s);
7390 return false;
7391 }
7392
7393 return true;
7394}
7395
7396/* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
7397 * Most groups' string representation is just the number, but for special
30ef36c6 7398 * groups, e.g. OFPG_ALL, it is the name, e.g. "ALL". */
7395c052
NZ
7399void
7400ofputil_format_group(uint32_t group_id, struct ds *s)
7401{
7402 char name[MAX_GROUP_NAME_LEN];
7403
7404 ofputil_group_to_string(group_id, name, sizeof name);
7405 ds_put_cstr(s, name);
7406}
7407
7408
7409/* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7410 * representation of OpenFlow group ID 'group_id'. Most group are represented
30ef36c6 7411 * as just their number, but special groups, e.g. OFPG_ALL, are represented
7395c052
NZ
7412 * by name, e.g. "ALL". */
7413void
7414ofputil_group_to_string(uint32_t group_id,
7415 char namebuf[MAX_GROUP_NAME_LEN + 1], size_t bufsize)
7416{
7417 switch (group_id) {
30ef36c6 7418 case OFPG_ALL:
7395c052
NZ
7419 ovs_strlcpy(namebuf, "ALL", bufsize);
7420 break;
7421
30ef36c6 7422 case OFPG_ANY:
7395c052
NZ
7423 ovs_strlcpy(namebuf, "ANY", bufsize);
7424 break;
7425
7426 default:
7427 snprintf(namebuf, bufsize, "%"PRIu32, group_id);
7428 break;
7429 }
7430}
7431
2be393ed
JP
7432/* Given a buffer 'b' that contains an array of OpenFlow ports of type
7433 * 'ofp_version', tries to pull the first element from the array. If
7434 * successful, initializes '*pp' with an abstract representation of the
7435 * port and returns 0. If no ports remain to be decoded, returns EOF.
7436 * On an error, returns a positive OFPERR_* value. */
7437int
2e3fa633 7438ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
2be393ed
JP
7439 struct ofputil_phy_port *pp)
7440{
8c3cc785
BP
7441 memset(pp, 0, sizeof *pp);
7442
2e3fa633
SH
7443 switch (ofp_version) {
7444 case OFP10_VERSION: {
2be393ed
JP
7445 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
7446 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
2e3fa633
SH
7447 }
7448 case OFP11_VERSION:
2e1ae200
JR
7449 case OFP12_VERSION:
7450 case OFP13_VERSION: {
2be393ed
JP
7451 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
7452 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
7453 }
c37c0382 7454 case OFP14_VERSION:
42dccab5 7455 case OFP15_VERSION:
6fd6ed71 7456 return b->size ? ofputil_pull_ofp14_port(pp, b) : EOF;
2f2b904f
BP
7457 case OFP16_VERSION:
7458 return b->size ? ofputil_pull_ofp16_port(pp, b) : EOF;
2e3fa633 7459 default:
428b2edd 7460 OVS_NOT_REACHED();
2e3fa633 7461 }
2be393ed
JP
7462}
7463
3cbd9931 7464static void
81a76618 7465ofputil_normalize_match__(struct match *match, bool may_log)
b459a924
BP
7466{
7467 enum {
7468 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
7469 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
7470 MAY_NW_PROTO = 1 << 2, /* nw_proto */
a61680c6 7471 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
b459a924
BP
7472 MAY_ARP_SHA = 1 << 4, /* arp_sha */
7473 MAY_ARP_THA = 1 << 5, /* arp_tha */
d78477ec 7474 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
b02475c5
SH
7475 MAY_ND_TARGET = 1 << 7, /* nd_target */
7476 MAY_MPLS = 1 << 8, /* mpls label and tc */
b459a924
BP
7477 } may_match;
7478
7479 struct flow_wildcards wc;
7480
7481 /* Figure out what fields may be matched. */
81a76618 7482 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
9e44d715 7483 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
81a76618
BP
7484 if (match->flow.nw_proto == IPPROTO_TCP ||
7485 match->flow.nw_proto == IPPROTO_UDP ||
0d56eaf2 7486 match->flow.nw_proto == IPPROTO_SCTP ||
81a76618 7487 match->flow.nw_proto == IPPROTO_ICMP) {
b459a924
BP
7488 may_match |= MAY_TP_ADDR;
7489 }
81a76618 7490 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
d78477ec 7491 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
81a76618 7492 if (match->flow.nw_proto == IPPROTO_TCP ||
0d56eaf2
JS
7493 match->flow.nw_proto == IPPROTO_UDP ||
7494 match->flow.nw_proto == IPPROTO_SCTP) {
b459a924 7495 may_match |= MAY_TP_ADDR;
81a76618 7496 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
b459a924 7497 may_match |= MAY_TP_ADDR;
81a76618 7498 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
b459a924 7499 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
81a76618 7500 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
b459a924
BP
7501 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
7502 }
7503 }
8087f5ff
MM
7504 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
7505 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
27527aa0 7506 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
b02475c5
SH
7507 } else if (eth_type_mpls(match->flow.dl_type)) {
7508 may_match = MAY_MPLS;
1c0b7503 7509 } else {
b459a924
BP
7510 may_match = 0;
7511 }
7512
7513 /* Clear the fields that may not be matched. */
81a76618 7514 wc = match->wc;
b459a924 7515 if (!(may_match & MAY_NW_ADDR)) {
26720e24 7516 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
b459a924
BP
7517 }
7518 if (!(may_match & MAY_TP_ADDR)) {
26720e24 7519 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
b459a924
BP
7520 }
7521 if (!(may_match & MAY_NW_PROTO)) {
26720e24 7522 wc.masks.nw_proto = 0;
b459a924 7523 }
9e44d715 7524 if (!(may_match & MAY_IPVx)) {
26720e24
BP
7525 wc.masks.nw_tos = 0;
7526 wc.masks.nw_ttl = 0;
b459a924
BP
7527 }
7528 if (!(may_match & MAY_ARP_SHA)) {
74ff3298 7529 WC_UNMASK_FIELD(&wc, arp_sha);
b459a924
BP
7530 }
7531 if (!(may_match & MAY_ARP_THA)) {
74ff3298 7532 WC_UNMASK_FIELD(&wc, arp_tha);
b459a924 7533 }
d78477ec 7534 if (!(may_match & MAY_IPV6)) {
26720e24
BP
7535 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
7536 wc.masks.ipv6_label = htonl(0);
b459a924
BP
7537 }
7538 if (!(may_match & MAY_ND_TARGET)) {
26720e24 7539 wc.masks.nd_target = in6addr_any;
b459a924 7540 }
b02475c5 7541 if (!(may_match & MAY_MPLS)) {
8bfd0fda 7542 memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse);
b02475c5 7543 }
b459a924
BP
7544
7545 /* Log any changes. */
81a76618 7546 if (!flow_wildcards_equal(&wc, &match->wc)) {
3cbd9931 7547 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
81a76618 7548 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
b459a924 7549
81a76618
BP
7550 match->wc = wc;
7551 match_zero_wildcarded_fields(match);
b459a924
BP
7552
7553 if (log) {
81a76618 7554 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
b459a924
BP
7555 VLOG_INFO("normalization changed ofp_match, details:");
7556 VLOG_INFO(" pre: %s", pre);
7557 VLOG_INFO("post: %s", post);
7558 free(pre);
7559 free(post);
7560 }
fa37b408 7561 }
3f09c339 7562}
26c112c2 7563
81a76618 7564/* "Normalizes" the wildcards in 'match'. That means:
3cbd9931
BP
7565 *
7566 * 1. If the type of level N is known, then only the valid fields for that
7567 * level may be specified. For example, ARP does not have a TOS field,
81a76618 7568 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
3cbd9931 7569 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
81a76618 7570 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
3cbd9931
BP
7571 * IPv4 flow.
7572 *
7573 * 2. If the type of level N is not known (or not understood by Open
7574 * vSwitch), then no fields at all for that level may be specified. For
7575 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
81a76618 7576 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
3cbd9931
BP
7577 * SCTP flow.
7578 *
81a76618 7579 * If this function changes 'match', it logs a rate-limited informational
3cbd9931
BP
7580 * message. */
7581void
81a76618 7582ofputil_normalize_match(struct match *match)
3cbd9931 7583{
81a76618 7584 ofputil_normalize_match__(match, true);
3cbd9931
BP
7585}
7586
81a76618
BP
7587/* Same as ofputil_normalize_match() without the logging. Thus, this function
7588 * is suitable for a program's internal use, whereas ofputil_normalize_match()
3cbd9931
BP
7589 * sense for use on flows received from elsewhere (so that a bug in the program
7590 * that sent them can be reported and corrected). */
7591void
81a76618 7592ofputil_normalize_match_quiet(struct match *match)
3cbd9931 7593{
81a76618 7594 ofputil_normalize_match__(match, false);
3cbd9931
BP
7595}
7596
4a48cdfb
BP
7597static size_t
7598parse_value(const char *s, const char *delimiters)
7599{
7600 size_t n = 0;
7601
7602 /* Iterate until we reach a delimiter.
7603 *
7604 * strchr(s, '\0') returns s+strlen(s), so this test handles the null
7605 * terminator at the end of 's'. */
7606 while (!strchr(delimiters, s[n])) {
7607 if (s[n] == '(') {
7608 int level = 0;
7609 do {
7610 switch (s[n]) {
7611 case '\0':
7612 return n;
7613 case '(':
7614 level++;
7615 break;
7616 case ')':
7617 level--;
7618 break;
7619 }
7620 n++;
7621 } while (level > 0);
7622 } else {
7623 n++;
7624 }
7625 }
7626 return n;
7627}
7628
0ff22822
BP
7629/* Parses a key or a key-value pair from '*stringp'.
7630 *
7631 * On success: Stores the key into '*keyp'. Stores the value, if present, into
7632 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
7633 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
7634 * are substrings of '*stringp' created by replacing some of its bytes by null
7635 * terminators. Returns true.
7636 *
7637 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
7638 * NULL and returns false. */
7639bool
7640ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
7641{
4a48cdfb
BP
7642 /* Skip white space and delimiters. If that brings us to the end of the
7643 * input string, we are done and there are no more key-value pairs. */
7644 *stringp += strspn(*stringp, ", \t\r\n");
7645 if (**stringp == '\0') {
0ff22822
BP
7646 *keyp = *valuep = NULL;
7647 return false;
7648 }
7649
4a48cdfb
BP
7650 /* Extract the key and the delimiter that ends the key-value pair or begins
7651 * the value. Advance the input position past the key and delimiter. */
7652 char *key = *stringp;
7653 size_t key_len = strcspn(key, ":=(, \t\r\n");
7654 char key_delim = key[key_len];
7655 key[key_len] = '\0';
7656 *stringp += key_len + (key_delim != '\0');
0ff22822 7657
4a48cdfb
BP
7658 /* Figure out what delimiter ends the value:
7659 *
7660 * - If key_delim is ":" or "=", the value extends until white space
7661 * or a comma.
7662 *
7663 * - If key_delim is "(", the value extends until ")".
7664 *
7665 * If there is no value, we are done. */
7666 const char *value_delims;
7667 if (key_delim == ':' || key_delim == '=') {
7668 value_delims = ", \t\r\n";
7669 } else if (key_delim == '(') {
7670 value_delims = ")";
0ff22822 7671 } else {
4a48cdfb
BP
7672 *keyp = key;
7673 *valuep = key + key_len; /* Empty string. */
7674 return true;
0ff22822 7675 }
0ff22822 7676
4a48cdfb
BP
7677 /* Extract the value. Advance the input position past the value and
7678 * delimiter. */
7679 char *value = *stringp;
7680 size_t value_len = parse_value(value, value_delims);
7681 char value_delim = value[value_len];
7682 value[value_len] = '\0';
7683 *stringp += value_len + (value_delim != '\0');
7684
0ff22822
BP
7685 *keyp = key;
7686 *valuep = value;
7687 return true;
7688}
f8e4867e
SH
7689
7690/* Encode a dump ports request for 'port', the encoded message
0746a84f 7691 * will be for OpenFlow version 'ofp_version'. Returns message
f8e4867e
SH
7692 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
7693struct ofpbuf *
4e022ec0 7694ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port)
f8e4867e
SH
7695{
7696 struct ofpbuf *request;
7697
7698 switch (ofp_version) {
7699 case OFP10_VERSION: {
7700 struct ofp10_port_stats_request *req;
7701 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
7702 req = ofpbuf_put_zeros(request, sizeof *req);
4e022ec0 7703 req->port_no = htons(ofp_to_u16(port));
f8e4867e
SH
7704 break;
7705 }
7706 case OFP11_VERSION:
2e1ae200 7707 case OFP12_VERSION:
c37c0382 7708 case OFP13_VERSION:
42dccab5 7709 case OFP14_VERSION:
b79d45a1
BP
7710 case OFP15_VERSION:
7711 case OFP16_VERSION: {
f8e4867e
SH
7712 struct ofp11_port_stats_request *req;
7713 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
7714 req = ofpbuf_put_zeros(request, sizeof *req);
7715 req->port_no = ofputil_port_to_ofp11(port);
7716 break;
7717 }
7718 default:
428b2edd 7719 OVS_NOT_REACHED();
f8e4867e
SH
7720 }
7721
7722 return request;
7723}
7724
7725static void
7726ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
7727 struct ofp10_port_stats *ps10)
7728{
4e022ec0 7729 ps10->port_no = htons(ofp_to_u16(ops->port_no));
f8e4867e
SH
7730 memset(ps10->pad, 0, sizeof ps10->pad);
7731 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
7732 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
7733 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
7734 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
7735 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
7736 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
7737 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
7738 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
7739 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
7740 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
7741 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
7742 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
7743}
7744
7745static void
7746ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
7747 struct ofp11_port_stats *ps11)
7748{
7749 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
7750 memset(ps11->pad, 0, sizeof ps11->pad);
7751 ps11->rx_packets = htonll(ops->stats.rx_packets);
7752 ps11->tx_packets = htonll(ops->stats.tx_packets);
7753 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
7754 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
7755 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
7756 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
7757 ps11->rx_errors = htonll(ops->stats.rx_errors);
7758 ps11->tx_errors = htonll(ops->stats.tx_errors);
7759 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
7760 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
7761 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
7762 ps11->collisions = htonll(ops->stats.collisions);
7763}
7764
2e1ae200
JR
7765static void
7766ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
7767 struct ofp13_port_stats *ps13)
7768{
7769 ofputil_port_stats_to_ofp11(ops, &ps13->ps);
65e0be10
BP
7770 ps13->duration_sec = htonl(ops->duration_sec);
7771 ps13->duration_nsec = htonl(ops->duration_nsec);
2e1ae200
JR
7772}
7773
5469537b
BP
7774static void
7775ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops,
ca6ba700 7776 struct ovs_list *replies)
5469537b
BP
7777{
7778 struct ofp14_port_stats_prop_ethernet *eth;
d6e3feb5 7779 struct intel_port_stats_rfc2819 *stats_rfc2819;
5469537b
BP
7780 struct ofp14_port_stats *ps14;
7781 struct ofpbuf *reply;
7782
d6e3feb5 7783 reply = ofpmp_reserve(replies, sizeof *ps14 + sizeof *eth +
7784 sizeof *stats_rfc2819);
5469537b
BP
7785
7786 ps14 = ofpbuf_put_uninit(reply, sizeof *ps14);
d6e3feb5 7787 ps14->length = htons(sizeof *ps14 + sizeof *eth +
7788 sizeof *stats_rfc2819);
5469537b
BP
7789 memset(ps14->pad, 0, sizeof ps14->pad);
7790 ps14->port_no = ofputil_port_to_ofp11(ops->port_no);
7791 ps14->duration_sec = htonl(ops->duration_sec);
7792 ps14->duration_nsec = htonl(ops->duration_nsec);
7793 ps14->rx_packets = htonll(ops->stats.rx_packets);
7794 ps14->tx_packets = htonll(ops->stats.tx_packets);
7795 ps14->rx_bytes = htonll(ops->stats.rx_bytes);
7796 ps14->tx_bytes = htonll(ops->stats.tx_bytes);
7797 ps14->rx_dropped = htonll(ops->stats.rx_dropped);
7798 ps14->tx_dropped = htonll(ops->stats.tx_dropped);
7799 ps14->rx_errors = htonll(ops->stats.rx_errors);
7800 ps14->tx_errors = htonll(ops->stats.tx_errors);
7801
303721ee 7802 eth = ofpprop_put_zeros(reply, OFPPSPT14_ETHERNET, sizeof *eth);
5469537b
BP
7803 eth->rx_frame_err = htonll(ops->stats.rx_frame_errors);
7804 eth->rx_over_err = htonll(ops->stats.rx_over_errors);
7805 eth->rx_crc_err = htonll(ops->stats.rx_crc_errors);
7806 eth->collisions = htonll(ops->stats.collisions);
d6e3feb5 7807
7808 uint64_t prop_type = OFPPROP_EXP(INTEL_VENDOR_ID,
7809 INTEL_PORT_STATS_RFC2819);
7810
7811 stats_rfc2819 = ofpprop_put_zeros(reply, prop_type,
7812 sizeof *stats_rfc2819);
7813
7814 memset(stats_rfc2819->pad, 0, sizeof stats_rfc2819->pad);
7815 stats_rfc2819->rx_1_to_64_packets = htonll(ops->stats.rx_1_to_64_packets);
7816 stats_rfc2819->rx_65_to_127_packets =
7817 htonll(ops->stats.rx_65_to_127_packets);
7818 stats_rfc2819->rx_128_to_255_packets =
7819 htonll(ops->stats.rx_128_to_255_packets);
7820 stats_rfc2819->rx_256_to_511_packets =
7821 htonll(ops->stats.rx_256_to_511_packets);
7822 stats_rfc2819->rx_512_to_1023_packets =
7823 htonll(ops->stats.rx_512_to_1023_packets);
7824 stats_rfc2819->rx_1024_to_1522_packets =
7825 htonll(ops->stats.rx_1024_to_1522_packets);
7826 stats_rfc2819->rx_1523_to_max_packets =
7827 htonll(ops->stats.rx_1523_to_max_packets);
7828
7829 stats_rfc2819->tx_1_to_64_packets = htonll(ops->stats.tx_1_to_64_packets);
7830 stats_rfc2819->tx_65_to_127_packets =
7831 htonll(ops->stats.tx_65_to_127_packets);
7832 stats_rfc2819->tx_128_to_255_packets =
7833 htonll(ops->stats.tx_128_to_255_packets);
7834 stats_rfc2819->tx_256_to_511_packets =
7835 htonll(ops->stats.tx_256_to_511_packets);
7836 stats_rfc2819->tx_512_to_1023_packets =
7837 htonll(ops->stats.tx_512_to_1023_packets);
7838 stats_rfc2819->tx_1024_to_1522_packets =
7839 htonll(ops->stats.tx_1024_to_1522_packets);
7840 stats_rfc2819->tx_1523_to_max_packets =
7841 htonll(ops->stats.tx_1523_to_max_packets);
7842
7843 stats_rfc2819->tx_multicast_packets =
7844 htonll(ops->stats.tx_multicast_packets);
7845 stats_rfc2819->rx_broadcast_packets =
7846 htonll(ops->stats.rx_broadcast_packets);
7847 stats_rfc2819->tx_broadcast_packets =
7848 htonll(ops->stats.tx_broadcast_packets);
7849 stats_rfc2819->rx_undersized_errors =
7850 htonll(ops->stats.rx_undersized_errors);
7851 stats_rfc2819->rx_oversize_errors =
7852 htonll(ops->stats.rx_oversize_errors);
7853 stats_rfc2819->rx_fragmented_errors =
7854 htonll(ops->stats.rx_fragmented_errors);
7855 stats_rfc2819->rx_jabber_errors =
7856 htonll(ops->stats.rx_jabber_errors);
5469537b 7857}
2e1ae200 7858
ad4c35fe 7859/* Encode a ports stat for 'ops' and append it to 'replies'. */
f8e4867e 7860void
ca6ba700 7861ofputil_append_port_stat(struct ovs_list *replies,
f8e4867e
SH
7862 const struct ofputil_port_stats *ops)
7863{
e28ac5cf 7864 switch (ofpmp_version(replies)) {
2e1ae200
JR
7865 case OFP13_VERSION: {
7866 struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7867 ofputil_port_stats_to_ofp13(ops, reply);
7868 break;
7869 }
f8e4867e
SH
7870 case OFP12_VERSION:
7871 case OFP11_VERSION: {
7872 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7873 ofputil_port_stats_to_ofp11(ops, reply);
7874 break;
7875 }
7876
7877 case OFP10_VERSION: {
7878 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7879 ofputil_port_stats_to_ofp10(ops, reply);
7880 break;
7881 }
7882
c37c0382 7883 case OFP14_VERSION:
42dccab5 7884 case OFP15_VERSION:
b79d45a1 7885 case OFP16_VERSION:
5469537b 7886 ofputil_append_ofp14_port_stats(ops, replies);
c37c0382
AC
7887 break;
7888
f8e4867e 7889 default:
428b2edd 7890 OVS_NOT_REACHED();
f8e4867e
SH
7891 }
7892}
7893
7894static enum ofperr
7895ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
7896 const struct ofp10_port_stats *ps10)
7897{
f8e4867e 7898
4e022ec0 7899 ops->port_no = u16_to_ofp(ntohs(ps10->port_no));
f8e4867e
SH
7900 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
7901 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
7902 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
7903 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
7904 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
7905 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
7906 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
7907 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
7908 ops->stats.rx_frame_errors =
7909 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
7910 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
7911 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
7912 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
65e0be10 7913 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
f8e4867e
SH
7914
7915 return 0;
7916}
7917
7918static enum ofperr
7919ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
7920 const struct ofp11_port_stats *ps11)
7921{
7922 enum ofperr error;
7923
f8e4867e
SH
7924 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
7925 if (error) {
7926 return error;
7927 }
7928
7929 ops->stats.rx_packets = ntohll(ps11->rx_packets);
7930 ops->stats.tx_packets = ntohll(ps11->tx_packets);
7931 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
7932 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
7933 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
7934 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
7935 ops->stats.rx_errors = ntohll(ps11->rx_errors);
7936 ops->stats.tx_errors = ntohll(ps11->tx_errors);
7937 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
7938 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
7939 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
7940 ops->stats.collisions = ntohll(ps11->collisions);
65e0be10 7941 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
f8e4867e
SH
7942
7943 return 0;
7944}
7945
2e1ae200
JR
7946static enum ofperr
7947ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
7948 const struct ofp13_port_stats *ps13)
7949{
65e0be10 7950 enum ofperr error = ofputil_port_stats_from_ofp11(ops, &ps13->ps);
2e1ae200 7951 if (!error) {
65e0be10
BP
7952 ops->duration_sec = ntohl(ps13->duration_sec);
7953 ops->duration_nsec = ntohl(ps13->duration_nsec);
2e1ae200 7954 }
2e1ae200
JR
7955 return error;
7956}
7957
5469537b
BP
7958static enum ofperr
7959parse_ofp14_port_stats_ethernet_property(const struct ofpbuf *payload,
7960 struct ofputil_port_stats *ops)
be0c30df 7961{
6fd6ed71 7962 const struct ofp14_port_stats_prop_ethernet *eth = payload->data;
5469537b 7963
6fd6ed71 7964 if (payload->size != sizeof *eth) {
5469537b
BP
7965 return OFPERR_OFPBPC_BAD_LEN;
7966 }
7967
7968 ops->stats.rx_frame_errors = ntohll(eth->rx_frame_err);
7969 ops->stats.rx_over_errors = ntohll(eth->rx_over_err);
7970 ops->stats.rx_crc_errors = ntohll(eth->rx_crc_err);
7971 ops->stats.collisions = ntohll(eth->collisions);
7972
7973 return 0;
7974}
7975
d6e3feb5 7976static enum ofperr
7977parse_intel_port_stats_rfc2819_property(const struct ofpbuf *payload,
7978 struct ofputil_port_stats *ops)
7979{
7980 const struct intel_port_stats_rfc2819 *rfc2819 = payload->data;
7981
7982 if (payload->size != sizeof *rfc2819) {
7983 return OFPERR_OFPBPC_BAD_LEN;
7984 }
7985 ops->stats.rx_1_to_64_packets = ntohll(rfc2819->rx_1_to_64_packets);
7986 ops->stats.rx_65_to_127_packets = ntohll(rfc2819->rx_65_to_127_packets);
7987 ops->stats.rx_128_to_255_packets = ntohll(rfc2819->rx_128_to_255_packets);
7988 ops->stats.rx_256_to_511_packets = ntohll(rfc2819->rx_256_to_511_packets);
7989 ops->stats.rx_512_to_1023_packets =
7990 ntohll(rfc2819->rx_512_to_1023_packets);
7991 ops->stats.rx_1024_to_1522_packets =
7992 ntohll(rfc2819->rx_1024_to_1522_packets);
7993 ops->stats.rx_1523_to_max_packets =
7994 ntohll(rfc2819->rx_1523_to_max_packets);
7995
7996 ops->stats.tx_1_to_64_packets = ntohll(rfc2819->tx_1_to_64_packets);
7997 ops->stats.tx_65_to_127_packets = ntohll(rfc2819->tx_65_to_127_packets);
7998 ops->stats.tx_128_to_255_packets = ntohll(rfc2819->tx_128_to_255_packets);
7999 ops->stats.tx_256_to_511_packets = ntohll(rfc2819->tx_256_to_511_packets);
8000 ops->stats.tx_512_to_1023_packets =
8001 ntohll(rfc2819->tx_512_to_1023_packets);
8002 ops->stats.tx_1024_to_1522_packets =
8003 ntohll(rfc2819->tx_1024_to_1522_packets);
8004 ops->stats.tx_1523_to_max_packets =
8005 ntohll(rfc2819->tx_1523_to_max_packets);
8006
8007 ops->stats.tx_multicast_packets = ntohll(rfc2819->tx_multicast_packets);
8008 ops->stats.rx_broadcast_packets = ntohll(rfc2819->rx_broadcast_packets);
8009 ops->stats.tx_broadcast_packets = ntohll(rfc2819->tx_broadcast_packets);
8010 ops->stats.rx_undersized_errors = ntohll(rfc2819->rx_undersized_errors);
8011
8012 ops->stats.rx_oversize_errors = ntohll(rfc2819->rx_oversize_errors);
8013 ops->stats.rx_fragmented_errors = ntohll(rfc2819->rx_fragmented_errors);
8014 ops->stats.rx_jabber_errors = ntohll(rfc2819->rx_jabber_errors);
8015
8016 return 0;
8017}
8018
8019static enum ofperr
8020parse_intel_port_stats_property(const struct ofpbuf *payload,
8021 uint32_t exp_type,
8022 struct ofputil_port_stats *ops)
8023{
8024 enum ofperr error;
8025
8026 switch (exp_type) {
8027 case INTEL_PORT_STATS_RFC2819:
8028 error = parse_intel_port_stats_rfc2819_property(payload, ops);
8029 break;
8030 default:
8031 error = OFPERR_OFPBPC_BAD_EXP_TYPE;
8032 break;
8033 }
8034
8035 return error;
8036}
8037
5469537b
BP
8038static enum ofperr
8039ofputil_pull_ofp14_port_stats(struct ofputil_port_stats *ops,
8040 struct ofpbuf *msg)
8041{
0a2869d5 8042 const struct ofp14_port_stats *ps14 = ofpbuf_try_pull(msg, sizeof *ps14);
5469537b
BP
8043 if (!ps14) {
8044 return OFPERR_OFPBRC_BAD_LEN;
8045 }
8046
0a2869d5 8047 size_t len = ntohs(ps14->length);
6fd6ed71 8048 if (len < sizeof *ps14 || len - sizeof *ps14 > msg->size) {
5469537b 8049 return OFPERR_OFPBRC_BAD_LEN;
be0c30df 8050 }
5469537b 8051 len -= sizeof *ps14;
5469537b 8052
0a2869d5 8053 enum ofperr error = ofputil_port_from_ofp11(ps14->port_no, &ops->port_no);
5469537b
BP
8054 if (error) {
8055 return error;
8056 }
8057
8058 ops->duration_sec = ntohl(ps14->duration_sec);
8059 ops->duration_nsec = ntohl(ps14->duration_nsec);
8060 ops->stats.rx_packets = ntohll(ps14->rx_packets);
8061 ops->stats.tx_packets = ntohll(ps14->tx_packets);
8062 ops->stats.rx_bytes = ntohll(ps14->rx_bytes);
8063 ops->stats.tx_bytes = ntohll(ps14->tx_bytes);
8064 ops->stats.rx_dropped = ntohll(ps14->rx_dropped);
8065 ops->stats.tx_dropped = ntohll(ps14->tx_dropped);
8066 ops->stats.rx_errors = ntohll(ps14->rx_errors);
8067 ops->stats.tx_errors = ntohll(ps14->tx_errors);
d6e3feb5 8068
5469537b 8069
0a2869d5
BP
8070 struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
8071 len);
6fd6ed71 8072 while (properties.size > 0) {
5469537b
BP
8073 struct ofpbuf payload;
8074 enum ofperr error;
d6e3feb5 8075 uint64_t type = 0;
5469537b 8076
c5562271 8077 error = ofpprop_pull(&properties, &payload, &type);
5469537b
BP
8078 if (error) {
8079 return error;
8080 }
5469537b
BP
8081 switch (type) {
8082 case OFPPSPT14_ETHERNET:
8083 error = parse_ofp14_port_stats_ethernet_property(&payload, ops);
8084 break;
d6e3feb5 8085 case OFPPROP_EXP(INTEL_VENDOR_ID, INTEL_PORT_STATS_RFC2819):
8086 error = parse_intel_port_stats_property(&payload,
8087 INTEL_PORT_STATS_RFC2819,
8088 ops);
8089 break;
5469537b 8090 default:
34a543e3 8091 error = OFPPROP_UNKNOWN(true, "port stats", type);
5469537b
BP
8092 break;
8093 }
8094
8095 if (error) {
8096 return error;
8097 }
8098 }
8099
8100 return 0;
be0c30df 8101}
2e1ae200 8102
f8e4867e
SH
8103/* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
8104 * message 'oh'. */
8105size_t
8106ofputil_count_port_stats(const struct ofp_header *oh)
8107{
0a2869d5 8108 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
f8e4867e 8109 ofpraw_pull_assert(&b);
0a2869d5
BP
8110
8111 for (size_t n = 0; ; n++) {
8112 struct ofputil_port_stats ps;
8113 if (ofputil_decode_port_stats(&ps, &b)) {
8114 return n;
8115 }
5469537b 8116 }
f8e4867e
SH
8117}
8118
8119/* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
8120 * ofputil_port_stats in 'ps'.
8121 *
8122 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
8123 * message. Calling this function multiple times for a single 'msg' iterates
8124 * through the replies. The caller must initially leave 'msg''s layer pointers
8125 * null and not modify them between calls.
8126 *
8127 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8128 * otherwise a positive errno value. */
8129int
8130ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
8131{
8132 enum ofperr error;
8133 enum ofpraw raw;
8134
d6e3feb5 8135 memset(&(ps->stats), 0xFF, sizeof (ps->stats));
8136
6fd6ed71 8137 error = (msg->header ? ofpraw_decode(&raw, msg->header)
f8e4867e
SH
8138 : ofpraw_pull(&raw, msg));
8139 if (error) {
8140 return error;
8141 }
8142
6fd6ed71 8143 if (!msg->size) {
f8e4867e 8144 return EOF;
5469537b
BP
8145 } else if (raw == OFPRAW_OFPST14_PORT_REPLY) {
8146 return ofputil_pull_ofp14_port_stats(ps, msg);
2e1ae200
JR
8147 } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
8148 const struct ofp13_port_stats *ps13;
2e1ae200
JR
8149 ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
8150 if (!ps13) {
8151 goto bad_len;
8152 }
8153 return ofputil_port_stats_from_ofp13(ps, ps13);
f8e4867e
SH
8154 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
8155 const struct ofp11_port_stats *ps11;
8156
8157 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
8158 if (!ps11) {
2e1ae200 8159 goto bad_len;
f8e4867e
SH
8160 }
8161 return ofputil_port_stats_from_ofp11(ps, ps11);
8162 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
8163 const struct ofp10_port_stats *ps10;
8164
8165 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
8166 if (!ps10) {
2e1ae200 8167 goto bad_len;
f8e4867e
SH
8168 }
8169 return ofputil_port_stats_from_ofp10(ps, ps10);
8170 } else {
428b2edd 8171 OVS_NOT_REACHED();
f8e4867e
SH
8172 }
8173
2e1ae200 8174 bad_len:
437d0d22 8175 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %"PRIu32" leftover "
6fd6ed71 8176 "bytes at end", msg->size);
2e1ae200 8177 return OFPERR_OFPBRC_BAD_LEN;
f8e4867e
SH
8178}
8179
8180/* Parse a port status request message into a 16 bit OpenFlow 1.0
8181 * port number and stores the latter in '*ofp10_port'.
8182 * Returns 0 if successful, otherwise an OFPERR_* number. */
8183enum ofperr
8184ofputil_decode_port_stats_request(const struct ofp_header *request,
4e022ec0 8185 ofp_port_t *ofp10_port)
f8e4867e
SH
8186{
8187 switch ((enum ofp_version)request->version) {
b79d45a1 8188 case OFP16_VERSION:
42dccab5 8189 case OFP15_VERSION:
5469537b 8190 case OFP14_VERSION:
2e1ae200 8191 case OFP13_VERSION:
f8e4867e
SH
8192 case OFP12_VERSION:
8193 case OFP11_VERSION: {
8194 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
8195 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
8196 }
8197
8198 case OFP10_VERSION: {
8199 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
4e022ec0 8200 *ofp10_port = u16_to_ofp(ntohs(psr10->port_no));
f8e4867e
SH
8201 return 0;
8202 }
8203
8204 default:
428b2edd 8205 OVS_NOT_REACHED();
f8e4867e
SH
8206 }
8207}
64626975 8208
fb8f22c1
BY
8209static void
8210ofputil_ipfix_stats_to_reply(const struct ofputil_ipfix_stats *ois,
8211 struct nx_ipfix_stats_reply *reply)
8212{
8213 reply->collector_set_id = htonl(ois->collector_set_id);
8214 reply->total_flows = htonll(ois->total_flows);
8215 reply->current_flows = htonll(ois->current_flows);
8216 reply->pkts = htonll(ois->pkts);
8217 reply->ipv4_pkts = htonll(ois->ipv4_pkts);
8218 reply->ipv6_pkts = htonll(ois->ipv6_pkts);
8219 reply->error_pkts = htonll(ois->error_pkts);
8220 reply->ipv4_error_pkts = htonll(ois->ipv4_error_pkts);
8221 reply->ipv6_error_pkts = htonll(ois->ipv6_error_pkts);
8222 reply->tx_pkts = htonll(ois->tx_pkts);
8223 reply->tx_errors = htonll(ois->tx_errors);
05e81316 8224 memset(reply->pad, 0, sizeof reply->pad);
fb8f22c1
BY
8225}
8226
8227/* Encode a ipfix stat for 'ois' and append it to 'replies'. */
8228void
8229ofputil_append_ipfix_stat(struct ovs_list *replies,
8230 const struct ofputil_ipfix_stats *ois)
8231{
8232 struct nx_ipfix_stats_reply *reply = ofpmp_append(replies, sizeof *reply);
8233 ofputil_ipfix_stats_to_reply(ois, reply);
8234}
8235
8236static enum ofperr
8237ofputil_ipfix_stats_from_nx(struct ofputil_ipfix_stats *is,
8238 const struct nx_ipfix_stats_reply *reply)
8239{
8240 is->collector_set_id = ntohl(reply->collector_set_id);
8241 is->total_flows = ntohll(reply->total_flows);
8242 is->current_flows = ntohll(reply->current_flows);
8243 is->pkts = ntohll(reply->pkts);
8244 is->ipv4_pkts = ntohll(reply->ipv4_pkts);
8245 is->ipv6_pkts = ntohll(reply->ipv6_pkts);
8246 is->error_pkts = ntohll(reply->error_pkts);
8247 is->ipv4_error_pkts = ntohll(reply->ipv4_error_pkts);
8248 is->ipv6_error_pkts = ntohll(reply->ipv6_error_pkts);
8249 is->tx_pkts = ntohll(reply->tx_pkts);
8250 is->tx_errors = ntohll(reply->tx_errors);
8251
8252 return 0;
8253}
8254
8255int
8256ofputil_pull_ipfix_stats(struct ofputil_ipfix_stats *is, struct ofpbuf *msg)
8257{
8258 enum ofperr error;
8259 enum ofpraw raw;
8260
8261 memset(is, 0xFF, sizeof (*is));
8262
8263 error = (msg->header ? ofpraw_decode(&raw, msg->header)
8264 : ofpraw_pull(&raw, msg));
8265 if (error) {
8266 return error;
8267 }
8268
8269 if (!msg->size) {
8270 return EOF;
8271 } else if (raw == OFPRAW_NXST_IPFIX_BRIDGE_REPLY ||
8272 raw == OFPRAW_NXST_IPFIX_FLOW_REPLY) {
8273 struct nx_ipfix_stats_reply *reply;
8274
8275 reply = ofpbuf_try_pull(msg, sizeof *reply);
8276 return ofputil_ipfix_stats_from_nx(is, reply);
8277 } else {
8278 OVS_NOT_REACHED();
8279 }
8280}
8281
8282
8283/* Returns the number of ipfix stats elements in
8284 * OFPTYPE_IPFIX_BRIDGE_STATS_REPLY or OFPTYPE_IPFIX_FLOW_STATS_REPLY
8285 * message 'oh'. */
8286size_t
8287ofputil_count_ipfix_stats(const struct ofp_header *oh)
8288{
8289 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
8290 ofpraw_pull_assert(&b);
8291
8292 return b.size / sizeof(struct ofputil_ipfix_stats);
8293}
8294
7395c052
NZ
8295/* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
8296void
ca6ba700 8297ofputil_bucket_list_destroy(struct ovs_list *buckets)
7395c052 8298{
5f03c983 8299 struct ofputil_bucket *bucket;
7395c052 8300
5f03c983 8301 LIST_FOR_EACH_POP (bucket, list_node, buckets) {
7395c052
NZ
8302 free(bucket->ofpacts);
8303 free(bucket);
8304 }
8305}
8306
103b4866
SH
8307/* Clones 'bucket' and its ofpacts data */
8308static struct ofputil_bucket *
8309ofputil_bucket_clone_data(const struct ofputil_bucket *bucket)
8310{
8311 struct ofputil_bucket *new;
8312
8313 new = xmemdup(bucket, sizeof *bucket);
8314 new->ofpacts = xmemdup(bucket->ofpacts, bucket->ofpacts_len);
8315
8316 return new;
8317}
8318
8319/* Clones each of the buckets in the list 'src' appending them
8320 * in turn to 'dest' which should be an initialised list.
8321 * An exception is that if the pointer value of a bucket in 'src'
8322 * matches 'skip' then it is not cloned or appended to 'dest'.
8323 * This allows all of 'src' or 'all of 'src' except 'skip' to
8324 * be cloned and appended to 'dest'. */
8325void
ca6ba700 8326ofputil_bucket_clone_list(struct ovs_list *dest, const struct ovs_list *src,
103b4866
SH
8327 const struct ofputil_bucket *skip)
8328{
8329 struct ofputil_bucket *bucket;
8330
8331 LIST_FOR_EACH (bucket, list_node, src) {
8332 struct ofputil_bucket *new_bucket;
8333
8334 if (bucket == skip) {
8335 continue;
8336 }
8337
8338 new_bucket = ofputil_bucket_clone_data(bucket);
417e7e66 8339 ovs_list_push_back(dest, &new_bucket->list_node);
103b4866
SH
8340 }
8341}
8342
8343/* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
8344 * Returns the first bucket found or NULL if no buckets are found. */
8345struct ofputil_bucket *
ca6ba700 8346ofputil_bucket_find(const struct ovs_list *buckets, uint32_t bucket_id)
103b4866
SH
8347{
8348 struct ofputil_bucket *bucket;
8349
8350 if (bucket_id > OFPG15_BUCKET_MAX) {
8351 return NULL;
8352 }
8353
8354 LIST_FOR_EACH (bucket, list_node, buckets) {
8355 if (bucket->bucket_id == bucket_id) {
8356 return bucket;
8357 }
8358 }
8359
8360 return NULL;
8361}
8362
8363/* Returns true if more than one bucket in the list 'buckets'
8364 * have the same bucket id. Returns false otherwise. */
18ac06d3 8365bool
ca6ba700 8366ofputil_bucket_check_duplicate_id(const struct ovs_list *buckets)
18ac06d3
SH
8367{
8368 struct ofputil_bucket *i, *j;
8369
8370 LIST_FOR_EACH (i, list_node, buckets) {
8371 LIST_FOR_EACH_REVERSE (j, list_node, buckets) {
8372 if (i == j) {
8373 break;
8374 }
8375 if (i->bucket_id == j->bucket_id) {
8376 return true;
8377 }
8378 }
8379 }
8380
8381 return false;
8382}
8383
103b4866
SH
8384/* Returns the bucket at the front of the list 'buckets'.
8385 * Undefined if 'buckets is empty. */
8386struct ofputil_bucket *
ca6ba700 8387ofputil_bucket_list_front(const struct ovs_list *buckets)
103b4866
SH
8388{
8389 static struct ofputil_bucket *bucket;
8390
417e7e66 8391 ASSIGN_CONTAINER(bucket, ovs_list_front(buckets), list_node);
103b4866
SH
8392
8393 return bucket;
8394}
8395
8396/* Returns the bucket at the back of the list 'buckets'.
8397 * Undefined if 'buckets is empty. */
8398struct ofputil_bucket *
ca6ba700 8399ofputil_bucket_list_back(const struct ovs_list *buckets)
103b4866
SH
8400{
8401 static struct ofputil_bucket *bucket;
8402
417e7e66 8403 ASSIGN_CONTAINER(bucket, ovs_list_back(buckets), list_node);
103b4866
SH
8404
8405 return bucket;
8406}
8407
7395c052
NZ
8408/* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
8409 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
8410 * for all groups.)
8411 *
8412 * Group statistics include packet and byte counts for each group. */
8413struct ofpbuf *
8414ofputil_encode_group_stats_request(enum ofp_version ofp_version,
8415 uint32_t group_id)
8416{
8417 struct ofpbuf *request;
8418
8419 switch (ofp_version) {
8420 case OFP10_VERSION:
8421 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
8422 "(\'-O OpenFlow11\')");
8423 case OFP11_VERSION:
8424 case OFP12_VERSION:
c37c0382 8425 case OFP13_VERSION:
42dccab5 8426 case OFP14_VERSION:
b79d45a1
BP
8427 case OFP15_VERSION:
8428 case OFP16_VERSION: {
7395c052
NZ
8429 struct ofp11_group_stats_request *req;
8430 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST, ofp_version, 0);
8431 req = ofpbuf_put_zeros(request, sizeof *req);
8432 req->group_id = htonl(group_id);
8433 break;
8434 }
8435 default:
428b2edd 8436 OVS_NOT_REACHED();
7395c052
NZ
8437 }
8438
8439 return request;
8440}
8441
bc65c25a
SH
8442void
8443ofputil_uninit_group_desc(struct ofputil_group_desc *gd)
8444{
8445 ofputil_bucket_list_destroy(&gd->buckets);
e8dba719 8446 ofputil_group_properties_destroy(&gd->props);
bc65c25a
SH
8447}
8448
19187a71
BP
8449/* Decodes the OpenFlow group description request in 'oh', returning the group
8450 * whose description is requested, or OFPG_ALL if stats for all groups was
8451 * requested. */
8452uint32_t
8453ofputil_decode_group_desc_request(const struct ofp_header *oh)
8454{
0a2869d5
BP
8455 struct ofpbuf request = ofpbuf_const_initializer(oh, ntohs(oh->length));
8456 enum ofpraw raw = ofpraw_pull_assert(&request);
19187a71
BP
8457 if (raw == OFPRAW_OFPST11_GROUP_DESC_REQUEST) {
8458 return OFPG_ALL;
8459 } else if (raw == OFPRAW_OFPST15_GROUP_DESC_REQUEST) {
8460 ovs_be32 *group_id = ofpbuf_pull(&request, sizeof *group_id);
8461 return ntohl(*group_id);
8462 } else {
8463 OVS_NOT_REACHED();
8464 }
8465}
8466
7395c052 8467/* Returns an OpenFlow group description request for OpenFlow version
19187a71
BP
8468 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
8469 * request stats for all groups (OpenFlow 1.4 and earlier always request all
8470 * groups).
7395c052
NZ
8471 *
8472 * Group descriptions include the bucket and action configuration for each
8473 * group. */
8474struct ofpbuf *
19187a71
BP
8475ofputil_encode_group_desc_request(enum ofp_version ofp_version,
8476 uint32_t group_id)
7395c052
NZ
8477{
8478 struct ofpbuf *request;
8479
8480 switch (ofp_version) {
8481 case OFP10_VERSION:
8482 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
8483 "(\'-O OpenFlow11\')");
8484 case OFP11_VERSION:
8485 case OFP12_VERSION:
c37c0382
AC
8486 case OFP13_VERSION:
8487 case OFP14_VERSION:
19187a71
BP
8488 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST,
8489 ofp_version, 0);
8490 break;
b79d45a1
BP
8491 case OFP15_VERSION:
8492 case OFP16_VERSION: {
d4d3f33e 8493 struct ofp15_group_desc_request *req;
19187a71
BP
8494 request = ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST,
8495 ofp_version, 0);
d4d3f33e
MT
8496 req = ofpbuf_put_zeros(request, sizeof *req);
8497 req->group_id = htonl(group_id);
7395c052 8498 break;
d4d3f33e 8499 }
7395c052 8500 default:
428b2edd 8501 OVS_NOT_REACHED();
7395c052
NZ
8502 }
8503
8504 return request;
8505}
8506
dcbe78ad 8507static void
63759e71
AZ
8508ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats *gs,
8509 struct ofp11_bucket_counter bucket_cnts[])
7395c052 8510{
7395c052
NZ
8511 int i;
8512
dcbe78ad
AZ
8513 for (i = 0; i < gs->n_buckets; i++) {
8514 bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count);
8515 bucket_cnts[i].byte_count = htonll(gs->bucket_stats[i].byte_count);
7395c052 8516 }
7395c052
NZ
8517}
8518
8519static void
dcbe78ad 8520ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs,
63759e71
AZ
8521 struct ofp11_group_stats *gs11, size_t length,
8522 struct ofp11_bucket_counter bucket_cnts[])
7395c052 8523{
63759e71
AZ
8524 memset(gs11, 0, sizeof *gs11);
8525 gs11->length = htons(length);
8526 gs11->group_id = htonl(gs->group_id);
8527 gs11->ref_count = htonl(gs->ref_count);
8528 gs11->packet_count = htonll(gs->packet_count);
8529 gs11->byte_count = htonll(gs->byte_count);
8530 ofputil_group_bucket_counters_to_ofp11(gs, bucket_cnts);
dcbe78ad 8531}
7395c052 8532
dcbe78ad
AZ
8533static void
8534ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs,
63759e71
AZ
8535 struct ofp13_group_stats *gs13, size_t length,
8536 struct ofp11_bucket_counter bucket_cnts[])
dcbe78ad 8537{
63759e71 8538 ofputil_group_stats_to_ofp11(gs, &gs13->gs, length, bucket_cnts);
dcbe78ad
AZ
8539 gs13->duration_sec = htonl(gs->duration_sec);
8540 gs13->duration_nsec = htonl(gs->duration_nsec);
63759e71 8541
7395c052
NZ
8542}
8543
dcbe78ad 8544/* Encodes 'gs' properly for the format of the list of group statistics
7395c052
NZ
8545 * replies already begun in 'replies' and appends it to the list. 'replies'
8546 * must have originally been initialized with ofpmp_init(). */
8547void
ca6ba700 8548ofputil_append_group_stats(struct ovs_list *replies,
dcbe78ad 8549 const struct ofputil_group_stats *gs)
7395c052 8550{
63759e71
AZ
8551 size_t bucket_counter_size;
8552 struct ofp11_bucket_counter *bucket_counters;
dcbe78ad 8553 size_t length;
7395c052 8554
63759e71
AZ
8555 bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter);
8556
e28ac5cf 8557 switch (ofpmp_version(replies)) {
7395c052 8558 case OFP11_VERSION:
dcbe78ad 8559 case OFP12_VERSION:{
63759e71 8560 struct ofp11_group_stats *gs11;
dcbe78ad 8561
63759e71
AZ
8562 length = sizeof *gs11 + bucket_counter_size;
8563 gs11 = ofpmp_append(replies, length);
8564 bucket_counters = (struct ofp11_bucket_counter *)(gs11 + 1);
8565 ofputil_group_stats_to_ofp11(gs, gs11, length, bucket_counters);
dcbe78ad
AZ
8566 break;
8567 }
7395c052
NZ
8568
8569 case OFP13_VERSION:
42dccab5 8570 case OFP14_VERSION:
b79d45a1
BP
8571 case OFP15_VERSION:
8572 case OFP16_VERSION: {
63759e71 8573 struct ofp13_group_stats *gs13;
7395c052 8574
63759e71
AZ
8575 length = sizeof *gs13 + bucket_counter_size;
8576 gs13 = ofpmp_append(replies, length);
8577 bucket_counters = (struct ofp11_bucket_counter *)(gs13 + 1);
8578 ofputil_group_stats_to_ofp13(gs, gs13, length, bucket_counters);
dcbe78ad
AZ
8579 break;
8580 }
c37c0382 8581
7395c052
NZ
8582 case OFP10_VERSION:
8583 default:
428b2edd 8584 OVS_NOT_REACHED();
7395c052
NZ
8585 }
8586}
7395c052
NZ
8587/* Returns an OpenFlow group features request for OpenFlow version
8588 * 'ofp_version'. */
8589struct ofpbuf *
8590ofputil_encode_group_features_request(enum ofp_version ofp_version)
8591{
8592 struct ofpbuf *request = NULL;
8593
8594 switch (ofp_version) {
8595 case OFP10_VERSION:
8596 case OFP11_VERSION:
8597 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
8598 "(\'-O OpenFlow12\')");
8599 case OFP12_VERSION:
c37c0382
AC
8600 case OFP13_VERSION:
8601 case OFP14_VERSION:
42dccab5 8602 case OFP15_VERSION:
b79d45a1 8603 case OFP16_VERSION:
7395c052 8604 request = ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST,
c37c0382 8605 ofp_version, 0);
7395c052 8606 break;
7395c052 8607 default:
428b2edd 8608 OVS_NOT_REACHED();
7395c052
NZ
8609 }
8610
8611 return request;
8612}
8613
8614/* Returns a OpenFlow message that encodes 'features' properly as a reply to
8615 * group features request 'request'. */
8616struct ofpbuf *
8617ofputil_encode_group_features_reply(
8618 const struct ofputil_group_features *features,
8619 const struct ofp_header *request)
8620{
8621 struct ofp12_group_features_stats *ogf;
8622 struct ofpbuf *reply;
08d1e234 8623 int i;
7395c052
NZ
8624
8625 reply = ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY,
8626 request->version, request->xid, 0);
8627 ogf = ofpbuf_put_zeros(reply, sizeof *ogf);
8628 ogf->types = htonl(features->types);
8629 ogf->capabilities = htonl(features->capabilities);
08d1e234
BP
8630 for (i = 0; i < OFPGT12_N_TYPES; i++) {
8631 ogf->max_groups[i] = htonl(features->max_groups[i]);
8632 ogf->actions[i] = ofpact_bitmap_to_openflow(features->ofpacts[i],
8633 request->version);
8634 }
7395c052
NZ
8635
8636 return reply;
8637}
8638
8639/* Decodes group features reply 'oh' into 'features'. */
8640void
8641ofputil_decode_group_features_reply(const struct ofp_header *oh,
8642 struct ofputil_group_features *features)
8643{
8644 const struct ofp12_group_features_stats *ogf = ofpmsg_body(oh);
08d1e234 8645 int i;
7395c052
NZ
8646
8647 features->types = ntohl(ogf->types);
8648 features->capabilities = ntohl(ogf->capabilities);
08d1e234
BP
8649 for (i = 0; i < OFPGT12_N_TYPES; i++) {
8650 features->max_groups[i] = ntohl(ogf->max_groups[i]);
8651 features->ofpacts[i] = ofpact_bitmap_from_openflow(
8652 ogf->actions[i], oh->version);
8653 }
7395c052
NZ
8654}
8655
8656/* Parse a group status request message into a 32 bit OpenFlow 1.1
8657 * group ID and stores the latter in '*group_id'.
8658 * Returns 0 if successful, otherwise an OFPERR_* number. */
8659enum ofperr
8660ofputil_decode_group_stats_request(const struct ofp_header *request,
8661 uint32_t *group_id)
8662{
8663 const struct ofp11_group_stats_request *gsr11 = ofpmsg_body(request);
8664 *group_id = ntohl(gsr11->group_id);
8665 return 0;
8666}
8667
8668/* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
646b2a9c
SH
8669 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
8670 * caller to eventually free.
7395c052
NZ
8671 *
8672 * Multiple group stats replies can be packed into a single OpenFlow message.
8673 * Calling this function multiple times for a single 'msg' iterates through the
8674 * replies. The caller must initially leave 'msg''s layer pointers null and
8675 * not modify them between calls.
8676 *
8677 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8678 * otherwise a positive errno value. */
8679int
8680ofputil_decode_group_stats_reply(struct ofpbuf *msg,
8681 struct ofputil_group_stats *gs)
8682{
8683 struct ofp11_bucket_counter *obc;
8684 struct ofp11_group_stats *ogs11;
8685 enum ofpraw raw;
8686 enum ofperr error;
8687 size_t base_len;
8688 size_t length;
8689 size_t i;
8690
646b2a9c 8691 gs->bucket_stats = NULL;
6fd6ed71 8692 error = (msg->header ? ofpraw_decode(&raw, msg->header)
7395c052
NZ
8693 : ofpraw_pull(&raw, msg));
8694 if (error) {
8695 return error;
8696 }
8697
6fd6ed71 8698 if (!msg->size) {
7395c052
NZ
8699 return EOF;
8700 }
8701
8702 if (raw == OFPRAW_OFPST11_GROUP_REPLY) {
8703 base_len = sizeof *ogs11;
8704 ogs11 = ofpbuf_try_pull(msg, sizeof *ogs11);
8705 gs->duration_sec = gs->duration_nsec = UINT32_MAX;
8706 } else if (raw == OFPRAW_OFPST13_GROUP_REPLY) {
8707 struct ofp13_group_stats *ogs13;
8708
8709 base_len = sizeof *ogs13;
8710 ogs13 = ofpbuf_try_pull(msg, sizeof *ogs13);
8711 if (ogs13) {
8712 ogs11 = &ogs13->gs;
8713 gs->duration_sec = ntohl(ogs13->duration_sec);
8714 gs->duration_nsec = ntohl(ogs13->duration_nsec);
8715 } else {
8716 ogs11 = NULL;
8717 }
8718 } else {
428b2edd 8719 OVS_NOT_REACHED();
7395c052
NZ
8720 }
8721
8722 if (!ogs11) {
437d0d22 8723 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
6fd6ed71 8724 ofpraw_get_name(raw), msg->size);
7395c052
NZ
8725 return OFPERR_OFPBRC_BAD_LEN;
8726 }
8727 length = ntohs(ogs11->length);
8728 if (length < sizeof base_len) {
34582733 8729 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply claims invalid length %"PRIuSIZE,
7395c052
NZ
8730 ofpraw_get_name(raw), length);
8731 return OFPERR_OFPBRC_BAD_LEN;
8732 }
8733
8734 gs->group_id = ntohl(ogs11->group_id);
8735 gs->ref_count = ntohl(ogs11->ref_count);
8736 gs->packet_count = ntohll(ogs11->packet_count);
8737 gs->byte_count = ntohll(ogs11->byte_count);
8738
8739 gs->n_buckets = (length - base_len) / sizeof *obc;
8740 obc = ofpbuf_try_pull(msg, gs->n_buckets * sizeof *obc);
8741 if (!obc) {
437d0d22 8742 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
6fd6ed71 8743 ofpraw_get_name(raw), msg->size);
7395c052
NZ
8744 return OFPERR_OFPBRC_BAD_LEN;
8745 }
8746
646b2a9c 8747 gs->bucket_stats = xmalloc(gs->n_buckets * sizeof *gs->bucket_stats);
7395c052
NZ
8748 for (i = 0; i < gs->n_buckets; i++) {
8749 gs->bucket_stats[i].packet_count = ntohll(obc[i].packet_count);
8750 gs->bucket_stats[i].byte_count = ntohll(obc[i].byte_count);
8751 }
8752
8753 return 0;
8754}
8755
5097fee5
SH
8756static void
8757ofputil_put_ofp11_bucket(const struct ofputil_bucket *bucket,
8758 struct ofpbuf *openflow, enum ofp_version ofp_version)
8759{
8760 struct ofp11_bucket *ob;
8761 size_t start;
8762
6fd6ed71 8763 start = openflow->size;
5097fee5
SH
8764 ofpbuf_put_zeros(openflow, sizeof *ob);
8765 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
8766 openflow, ofp_version);
8767 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
6fd6ed71 8768 ob->len = htons(openflow->size - start);
5097fee5
SH
8769 ob->weight = htons(bucket->weight);
8770 ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port);
8771 ob->watch_group = htonl(bucket->watch_group);
8772}
8773
18ac06d3
SH
8774static void
8775ofputil_put_ofp15_bucket(const struct ofputil_bucket *bucket,
8776 uint32_t bucket_id, enum ofp11_group_type group_type,
8777 struct ofpbuf *openflow, enum ofp_version ofp_version)
8778{
8779 struct ofp15_bucket *ob;
8780 size_t start, actions_start, actions_len;
8781
6fd6ed71 8782 start = openflow->size;
18ac06d3
SH
8783 ofpbuf_put_zeros(openflow, sizeof *ob);
8784
6fd6ed71 8785 actions_start = openflow->size;
18ac06d3
SH
8786 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
8787 openflow, ofp_version);
6fd6ed71 8788 actions_len = openflow->size - actions_start;
18ac06d3
SH
8789
8790 if (group_type == OFPGT11_SELECT) {
b611d3ac 8791 ofpprop_put_u16(openflow, OFPGBPT15_WEIGHT, bucket->weight);
18ac06d3
SH
8792 }
8793 if (bucket->watch_port != OFPP_ANY) {
b611d3ac
BP
8794 ofpprop_put_be32(openflow, OFPGBPT15_WATCH_PORT,
8795 ofputil_port_to_ofp11(bucket->watch_port));
18ac06d3
SH
8796 }
8797 if (bucket->watch_group != OFPG_ANY) {
b611d3ac 8798 ofpprop_put_u32(openflow, OFPGBPT15_WATCH_GROUP, bucket->watch_group);
18ac06d3
SH
8799 }
8800
8801 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
6fd6ed71 8802 ob->len = htons(openflow->size - start);
79870963 8803 ob->action_array_len = htons(actions_len);
18ac06d3
SH
8804 ob->bucket_id = htonl(bucket_id);
8805}
8806
53eb84a5
SH
8807static void
8808ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version,
8809 const struct ofputil_group_props *gp,
8810 struct ofpbuf *openflow)
8811{
8812 struct ntr_group_prop_selection_method *prop;
8813 size_t start;
8814
8815 start = openflow->size;
8816 ofpbuf_put_zeros(openflow, sizeof *prop);
8817 oxm_put_field_array(openflow, &gp->fields, ofp_version);
8818 prop = ofpbuf_at_assert(openflow, start, sizeof *prop);
8819 prop->type = htons(OFPGPT15_EXPERIMENTER);
8820 prop->experimenter = htonl(NTR_VENDOR_ID);
8821 prop->exp_type = htonl(NTRT_SELECTION_METHOD);
8822 strcpy(prop->selection_method, gp->selection_method);
8823 prop->selection_method_param = htonll(gp->selection_method_param);
c5562271 8824 ofpprop_end(openflow, start);
53eb84a5
SH
8825}
8826
5be50252
SH
8827static void
8828ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc *gds,
1667bb34 8829 const struct ovs_list *buckets,
ca6ba700 8830 struct ovs_list *replies,
5be50252 8831 enum ofp_version version)
7395c052 8832{
417e7e66 8833 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
7395c052
NZ
8834 struct ofp11_group_desc_stats *ogds;
8835 struct ofputil_bucket *bucket;
8836 size_t start_ogds;
8837
6fd6ed71 8838 start_ogds = reply->size;
7395c052
NZ
8839 ofpbuf_put_zeros(reply, sizeof *ogds);
8840 LIST_FOR_EACH (bucket, list_node, buckets) {
5097fee5 8841 ofputil_put_ofp11_bucket(bucket, reply, version);
7395c052
NZ
8842 }
8843 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
6fd6ed71 8844 ogds->length = htons(reply->size - start_ogds);
7395c052
NZ
8845 ogds->type = gds->type;
8846 ogds->group_id = htonl(gds->group_id);
8847
8848 ofpmp_postappend(replies, start_ogds);
8849}
8850
18ac06d3
SH
8851static void
8852ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc *gds,
1667bb34 8853 const struct ovs_list *buckets,
ca6ba700 8854 struct ovs_list *replies,
18ac06d3
SH
8855 enum ofp_version version)
8856{
417e7e66 8857 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
18ac06d3
SH
8858 struct ofp15_group_desc_stats *ogds;
8859 struct ofputil_bucket *bucket;
8860 size_t start_ogds, start_buckets;
8861
6fd6ed71 8862 start_ogds = reply->size;
18ac06d3 8863 ofpbuf_put_zeros(reply, sizeof *ogds);
6fd6ed71 8864 start_buckets = reply->size;
18ac06d3
SH
8865 LIST_FOR_EACH (bucket, list_node, buckets) {
8866 ofputil_put_ofp15_bucket(bucket, bucket->bucket_id,
8867 gds->type, reply, version);
8868 }
8869 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
18ac06d3
SH
8870 ogds->type = gds->type;
8871 ogds->group_id = htonl(gds->group_id);
6fd6ed71 8872 ogds->bucket_list_len = htons(reply->size - start_buckets);
18ac06d3 8873
53eb84a5
SH
8874 /* Add group properties */
8875 if (gds->props.selection_method[0]) {
8876 ofputil_put_group_prop_ntr_selection_method(version, &gds->props,
8877 reply);
8878 }
ef5774e3 8879 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
3986cae6 8880 ogds->length = htons(reply->size - start_ogds);
53eb84a5 8881
18ac06d3
SH
8882 ofpmp_postappend(replies, start_ogds);
8883}
8884
5be50252
SH
8885/* Appends a group stats reply that contains the data in 'gds' to those already
8886 * present in the list of ofpbufs in 'replies'. 'replies' should have been
8887 * initialized with ofpmp_init(). */
8888void
8889ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
1667bb34 8890 const struct ovs_list *buckets,
ca6ba700 8891 struct ovs_list *replies)
5be50252
SH
8892{
8893 enum ofp_version version = ofpmp_version(replies);
8894
8895 switch (version)
8896 {
8897 case OFP11_VERSION:
8898 case OFP12_VERSION:
8899 case OFP13_VERSION:
8900 case OFP14_VERSION:
5be50252
SH
8901 ofputil_append_ofp11_group_desc_reply(gds, buckets, replies, version);
8902 break;
8903
18ac06d3 8904 case OFP15_VERSION:
b79d45a1 8905 case OFP16_VERSION:
18ac06d3
SH
8906 ofputil_append_ofp15_group_desc_reply(gds, buckets, replies, version);
8907 break;
8908
5be50252
SH
8909 case OFP10_VERSION:
8910 default:
8911 OVS_NOT_REACHED();
8912 }
8913}
8914
7395c052 8915static enum ofperr
655ed3ae 8916ofputil_pull_ofp11_buckets(struct ofpbuf *msg, size_t buckets_length,
ca6ba700 8917 enum ofp_version version, struct ovs_list *buckets)
7395c052
NZ
8918{
8919 struct ofp11_bucket *ob;
18ac06d3 8920 uint32_t bucket_id = 0;
7395c052 8921
417e7e66 8922 ovs_list_init(buckets);
7395c052
NZ
8923 while (buckets_length > 0) {
8924 struct ofputil_bucket *bucket;
8925 struct ofpbuf ofpacts;
8926 enum ofperr error;
8927 size_t ob_len;
8928
8929 ob = (buckets_length >= sizeof *ob
8930 ? ofpbuf_try_pull(msg, sizeof *ob)
8931 : NULL);
8932 if (!ob) {
34582733 8933 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE" leftover bytes",
7395c052 8934 buckets_length);
e27e4ce9 8935 return OFPERR_OFPGMFC_BAD_BUCKET;
7395c052
NZ
8936 }
8937
8938 ob_len = ntohs(ob->len);
8939 if (ob_len < sizeof *ob) {
8940 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
34582733 8941 "%"PRIuSIZE" is not valid", ob_len);
7395c052
NZ
8942 return OFPERR_OFPGMFC_BAD_BUCKET;
8943 } else if (ob_len > buckets_length) {
8944 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
34582733 8945 "%"PRIuSIZE" exceeds remaining buckets data size %"PRIuSIZE,
7395c052
NZ
8946 ob_len, buckets_length);
8947 return OFPERR_OFPGMFC_BAD_BUCKET;
8948 }
8949 buckets_length -= ob_len;
8950
8951 ofpbuf_init(&ofpacts, 0);
e3f8f887 8952 error = ofpacts_pull_openflow_actions(msg, ob_len - sizeof *ob,
5c7c16d8 8953 version, NULL, NULL, &ofpacts);
7395c052
NZ
8954 if (error) {
8955 ofpbuf_uninit(&ofpacts);
8956 ofputil_bucket_list_destroy(buckets);
8957 return error;
8958 }
8959
8960 bucket = xzalloc(sizeof *bucket);
8961 bucket->weight = ntohs(ob->weight);
8962 error = ofputil_port_from_ofp11(ob->watch_port, &bucket->watch_port);
8963 if (error) {
8964 ofpbuf_uninit(&ofpacts);
8965 ofputil_bucket_list_destroy(buckets);
8966 return OFPERR_OFPGMFC_BAD_WATCH;
8967 }
8968 bucket->watch_group = ntohl(ob->watch_group);
18ac06d3
SH
8969 bucket->bucket_id = bucket_id++;
8970
7395c052 8971 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
6fd6ed71 8972 bucket->ofpacts_len = ofpacts.size;
417e7e66 8973 ovs_list_push_back(buckets, &bucket->list_node);
7395c052
NZ
8974 }
8975
8976 return 0;
8977}
8978
18ac06d3
SH
8979static enum ofperr
8980ofputil_pull_ofp15_buckets(struct ofpbuf *msg, size_t buckets_length,
64e8c446
BP
8981 enum ofp_version version, uint8_t group_type,
8982 struct ovs_list *buckets)
18ac06d3
SH
8983{
8984 struct ofp15_bucket *ob;
8985
417e7e66 8986 ovs_list_init(buckets);
18ac06d3
SH
8987 while (buckets_length > 0) {
8988 struct ofputil_bucket *bucket = NULL;
8989 struct ofpbuf ofpacts;
8990 enum ofperr err = OFPERR_OFPGMFC_BAD_BUCKET;
18ac06d3
SH
8991 size_t ob_len, actions_len, properties_len;
8992 ovs_be32 watch_port = ofputil_port_to_ofp11(OFPP_ANY);
8993 ovs_be32 watch_group = htonl(OFPG_ANY);
64e8c446 8994 ovs_be16 weight = htons(group_type == OFPGT11_SELECT ? 1 : 0);
18ac06d3
SH
8995
8996 ofpbuf_init(&ofpacts, 0);
8997
8998 ob = ofpbuf_try_pull(msg, sizeof *ob);
8999 if (!ob) {
9000 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE
9001 " leftover bytes", buckets_length);
9002 goto err;
9003 }
9004
9005 ob_len = ntohs(ob->len);
79870963 9006 actions_len = ntohs(ob->action_array_len);
18ac06d3
SH
9007
9008 if (ob_len < sizeof *ob) {
9009 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
9010 "%"PRIuSIZE" is not valid", ob_len);
9011 goto err;
9012 } else if (ob_len > buckets_length) {
9013 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
9014 "%"PRIuSIZE" exceeds remaining buckets data size %"
9015 PRIuSIZE, ob_len, buckets_length);
9016 goto err;
9017 } else if (actions_len > ob_len - sizeof *ob) {
9018 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket actions "
9019 "length %"PRIuSIZE" exceeds remaining bucket "
9020 "data size %"PRIuSIZE, actions_len,
9021 ob_len - sizeof *ob);
9022 goto err;
9023 }
9024 buckets_length -= ob_len;
9025
9026 err = ofpacts_pull_openflow_actions(msg, actions_len, version,
5c7c16d8 9027 NULL, NULL, &ofpacts);
18ac06d3
SH
9028 if (err) {
9029 goto err;
9030 }
9031
9032 properties_len = ob_len - sizeof *ob - actions_len;
0a2869d5
BP
9033 struct ofpbuf properties = ofpbuf_const_initializer(
9034 ofpbuf_pull(msg, properties_len), properties_len);
6fd6ed71 9035 while (properties.size > 0) {
18ac06d3 9036 struct ofpbuf payload;
34a543e3 9037 uint64_t type;
18ac06d3 9038
c5562271 9039 err = ofpprop_pull(&properties, &payload, &type);
18ac06d3
SH
9040 if (err) {
9041 goto err;
9042 }
9043
9044 switch (type) {
9045 case OFPGBPT15_WEIGHT:
b611d3ac 9046 err = ofpprop_parse_be16(&payload, &weight);
18ac06d3
SH
9047 break;
9048
9049 case OFPGBPT15_WATCH_PORT:
b611d3ac 9050 err = ofpprop_parse_be32(&payload, &watch_port);
18ac06d3
SH
9051 break;
9052
9053 case OFPGBPT15_WATCH_GROUP:
b611d3ac 9054 err = ofpprop_parse_be32(&payload, &watch_group);
18ac06d3
SH
9055 break;
9056
9057 default:
34a543e3 9058 err = OFPPROP_UNKNOWN(false, "group bucket", type);
18ac06d3
SH
9059 break;
9060 }
9061
9062 if (err) {
9063 goto err;
9064 }
9065 }
9066
9067 bucket = xzalloc(sizeof *bucket);
9068
9069 bucket->weight = ntohs(weight);
9070 err = ofputil_port_from_ofp11(watch_port, &bucket->watch_port);
9071 if (err) {
9072 err = OFPERR_OFPGMFC_BAD_WATCH;
9073 goto err;
9074 }
9075 bucket->watch_group = ntohl(watch_group);
9076 bucket->bucket_id = ntohl(ob->bucket_id);
9077 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
9078 VLOG_WARN_RL(&bad_ofmsg_rl, "bucket id (%u) is out of range",
9079 bucket->bucket_id);
9080 err = OFPERR_OFPGMFC_BAD_BUCKET;
9081 goto err;
9082 }
9083
9084 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
6fd6ed71 9085 bucket->ofpacts_len = ofpacts.size;
417e7e66 9086 ovs_list_push_back(buckets, &bucket->list_node);
18ac06d3
SH
9087
9088 continue;
9089
9090 err:
9091 free(bucket);
9092 ofpbuf_uninit(&ofpacts);
9093 ofputil_bucket_list_destroy(buckets);
9094 return err;
9095 }
9096
9097 if (ofputil_bucket_check_duplicate_id(buckets)) {
9098 VLOG_WARN_RL(&bad_ofmsg_rl, "Duplicate bucket id");
9099 ofputil_bucket_list_destroy(buckets);
9100 return OFPERR_OFPGMFC_BAD_BUCKET;
9101 }
9102
9103 return 0;
9104}
9105
bc65c25a
SH
9106static void
9107ofputil_init_group_properties(struct ofputil_group_props *gp)
9108{
9109 memset(gp, 0, sizeof *gp);
9110}
9111
e8dba719
JR
9112void
9113ofputil_group_properties_copy(struct ofputil_group_props *to,
9114 const struct ofputil_group_props *from)
9115{
9116 *to = *from;
9117 to->fields.values = xmemdup(from->fields.values, from->fields.values_size);
9118}
9119
9120void
9121ofputil_group_properties_destroy(struct ofputil_group_props *gp)
9122{
9123 free(gp->fields.values);
9124}
9125
bc65c25a
SH
9126static enum ofperr
9127parse_group_prop_ntr_selection_method(struct ofpbuf *payload,
9128 enum ofp11_group_type group_type,
9129 enum ofp15_group_mod_command group_cmd,
9130 struct ofputil_group_props *gp)
9131{
9132 struct ntr_group_prop_selection_method *prop = payload->data;
9133 size_t fields_len, method_len;
9134 enum ofperr error;
9135
9136 switch (group_type) {
9137 case OFPGT11_SELECT:
9138 break;
9139 case OFPGT11_ALL:
9140 case OFPGT11_INDIRECT:
9141 case OFPGT11_FF:
c5562271
BP
9142 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property is "
9143 "only allowed for select groups");
bc65c25a
SH
9144 return OFPERR_OFPBPC_BAD_VALUE;
9145 default:
9146 OVS_NOT_REACHED();
9147 }
9148
9149 switch (group_cmd) {
9150 case OFPGC15_ADD:
9151 case OFPGC15_MODIFY:
88b87a36 9152 case OFPGC15_ADD_OR_MOD:
bc65c25a
SH
9153 break;
9154 case OFPGC15_DELETE:
9155 case OFPGC15_INSERT_BUCKET:
9156 case OFPGC15_REMOVE_BUCKET:
c5562271
BP
9157 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property is "
9158 "only allowed for add and delete group modifications");
bc65c25a
SH
9159 return OFPERR_OFPBPC_BAD_VALUE;
9160 default:
9161 OVS_NOT_REACHED();
9162 }
9163
9164 if (payload->size < sizeof *prop) {
c5562271
BP
9165 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property "
9166 "length %u is not valid", payload->size);
bc65c25a
SH
9167 return OFPERR_OFPBPC_BAD_LEN;
9168 }
9169
9170 method_len = strnlen(prop->selection_method, NTR_MAX_SELECTION_METHOD_LEN);
9171
9172 if (method_len == NTR_MAX_SELECTION_METHOD_LEN) {
c5562271
BP
9173 OFPPROP_LOG(&bad_ofmsg_rl, false,
9174 "ntr selection method is not null terminated");
bc65c25a
SH
9175 return OFPERR_OFPBPC_BAD_VALUE;
9176 }
9177
53cc166a
JR
9178 if (strcmp("hash", prop->selection_method)
9179 && strcmp("dp_hash", prop->selection_method)) {
c5562271
BP
9180 OFPPROP_LOG(&bad_ofmsg_rl, false,
9181 "ntr selection method '%s' is not supported",
9182 prop->selection_method);
0c4b9393
SH
9183 return OFPERR_OFPBPC_BAD_VALUE;
9184 }
53cc166a 9185 /* 'method_len' is now non-zero. */
bc65c25a
SH
9186
9187 strcpy(gp->selection_method, prop->selection_method);
9188 gp->selection_method_param = ntohll(prop->selection_method_param);
9189
bc65c25a
SH
9190 ofpbuf_pull(payload, sizeof *prop);
9191
9192 fields_len = ntohs(prop->length) - sizeof *prop;
53cc166a
JR
9193 if (fields_len && strcmp("hash", gp->selection_method)) {
9194 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method %s "
9195 "does not support fields", gp->selection_method);
bc65c25a
SH
9196 return OFPERR_OFPBPC_BAD_VALUE;
9197 }
9198
9199 error = oxm_pull_field_array(payload->data, fields_len,
9200 &gp->fields);
9201 if (error) {
c5562271
BP
9202 OFPPROP_LOG(&bad_ofmsg_rl, false,
9203 "ntr selection method fields are invalid");
bc65c25a
SH
9204 return error;
9205 }
9206
9207 return 0;
9208}
9209
bc65c25a
SH
9210static enum ofperr
9211parse_ofp15_group_properties(struct ofpbuf *msg,
9212 enum ofp11_group_type group_type,
9213 enum ofp15_group_mod_command group_cmd,
9214 struct ofputil_group_props *gp,
9215 size_t properties_len)
9216{
0a2869d5
BP
9217 struct ofpbuf properties = ofpbuf_const_initializer(
9218 ofpbuf_pull(msg, properties_len), properties_len);
bc65c25a
SH
9219 while (properties.size > 0) {
9220 struct ofpbuf payload;
9221 enum ofperr error;
34a543e3 9222 uint64_t type;
bc65c25a 9223
c5562271 9224 error = ofpprop_pull(&properties, &payload, &type);
bc65c25a
SH
9225 if (error) {
9226 return error;
9227 }
9228
9229 switch (type) {
34a543e3
BP
9230 case OFPPROP_EXP(NTR_VENDOR_ID, NTRT_SELECTION_METHOD):
9231 case OFPPROP_EXP(NTR_COMPAT_VENDOR_ID, NTRT_SELECTION_METHOD):
9232 error = parse_group_prop_ntr_selection_method(&payload, group_type,
9233 group_cmd, gp);
bc65c25a
SH
9234 break;
9235
9236 default:
34a543e3 9237 error = OFPPROP_UNKNOWN(false, "group", type);
bc65c25a
SH
9238 break;
9239 }
9240
9241 if (error) {
9242 return error;
9243 }
9244 }
9245
9246 return 0;
9247}
9248
975910f2
SH
9249static int
9250ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc *gd,
9251 struct ofpbuf *msg,
9252 enum ofp_version version)
7395c052
NZ
9253{
9254 struct ofp11_group_desc_stats *ogds;
9255 size_t length;
9256
6fd6ed71 9257 if (!msg->header) {
7395c052
NZ
9258 ofpraw_pull_assert(msg);
9259 }
9260
6fd6ed71 9261 if (!msg->size) {
7395c052
NZ
9262 return EOF;
9263 }
9264
9265 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
9266 if (!ogds) {
437d0d22 9267 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
6fd6ed71 9268 "leftover bytes at end", msg->size);
7395c052
NZ
9269 return OFPERR_OFPBRC_BAD_LEN;
9270 }
9271 gd->type = ogds->type;
9272 gd->group_id = ntohl(ogds->group_id);
9273
9274 length = ntohs(ogds->length);
6fd6ed71 9275 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
7395c052 9276 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
34582733 9277 "length %"PRIuSIZE, length);
7395c052
NZ
9278 return OFPERR_OFPBRC_BAD_LEN;
9279 }
9280
655ed3ae
SH
9281 return ofputil_pull_ofp11_buckets(msg, length - sizeof *ogds, version,
9282 &gd->buckets);
7395c052
NZ
9283}
9284
18ac06d3
SH
9285static int
9286ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc *gd,
9287 struct ofpbuf *msg,
9288 enum ofp_version version)
9289{
9290 struct ofp15_group_desc_stats *ogds;
9291 uint16_t length, bucket_list_len;
bc65c25a 9292 int error;
18ac06d3 9293
6fd6ed71 9294 if (!msg->header) {
18ac06d3
SH
9295 ofpraw_pull_assert(msg);
9296 }
9297
6fd6ed71 9298 if (!msg->size) {
18ac06d3
SH
9299 return EOF;
9300 }
9301
9302 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
9303 if (!ogds) {
9304 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
6fd6ed71 9305 "leftover bytes at end", msg->size);
18ac06d3
SH
9306 return OFPERR_OFPBRC_BAD_LEN;
9307 }
9308 gd->type = ogds->type;
9309 gd->group_id = ntohl(ogds->group_id);
9310
9311 length = ntohs(ogds->length);
6fd6ed71 9312 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
18ac06d3
SH
9313 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
9314 "length %u", length);
9315 return OFPERR_OFPBRC_BAD_LEN;
9316 }
9317
9318 bucket_list_len = ntohs(ogds->bucket_list_len);
9319 if (length < bucket_list_len + sizeof *ogds) {
9320 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
9321 "bucket list length %u", bucket_list_len);
9322 return OFPERR_OFPBRC_BAD_LEN;
9323 }
64e8c446 9324 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, version, gd->type,
bc65c25a
SH
9325 &gd->buckets);
9326 if (error) {
9327 return error;
9328 }
18ac06d3 9329
bc65c25a
SH
9330 /* By definition group desc messages don't have a group mod command.
9331 * However, parse_group_prop_ntr_selection_method() checks to make sure
9332 * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
9333 * against group mod messages with other commands supplying
9334 * a NTR selection method group experimenter property.
9335 * Such properties are valid for group desc replies so
9336 * claim that the group mod command is OFPGC15_ADD to
9337 * satisfy the check in parse_group_prop_ntr_selection_method() */
9338 return parse_ofp15_group_properties(msg, gd->type, OFPGC15_ADD, &gd->props,
3986cae6 9339 length - sizeof *ogds - bucket_list_len);
18ac06d3
SH
9340}
9341
975910f2
SH
9342/* Converts a group description reply in 'msg' into an abstract
9343 * ofputil_group_desc in 'gd'.
9344 *
9345 * Multiple group description replies can be packed into a single OpenFlow
9346 * message. Calling this function multiple times for a single 'msg' iterates
9347 * through the replies. The caller must initially leave 'msg''s layer pointers
9348 * null and not modify them between calls.
9349 *
9350 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9351 * otherwise a positive errno value. */
9352int
9353ofputil_decode_group_desc_reply(struct ofputil_group_desc *gd,
9354 struct ofpbuf *msg, enum ofp_version version)
9355{
bc65c25a
SH
9356 ofputil_init_group_properties(&gd->props);
9357
975910f2
SH
9358 switch (version)
9359 {
9360 case OFP11_VERSION:
9361 case OFP12_VERSION:
9362 case OFP13_VERSION:
9363 case OFP14_VERSION:
975910f2
SH
9364 return ofputil_decode_ofp11_group_desc_reply(gd, msg, version);
9365
18ac06d3 9366 case OFP15_VERSION:
b79d45a1 9367 case OFP16_VERSION:
18ac06d3
SH
9368 return ofputil_decode_ofp15_group_desc_reply(gd, msg, version);
9369
975910f2
SH
9370 case OFP10_VERSION:
9371 default:
9372 OVS_NOT_REACHED();
9373 }
9374}
9375
bc65c25a
SH
9376void
9377ofputil_uninit_group_mod(struct ofputil_group_mod *gm)
9378{
9379 ofputil_bucket_list_destroy(&gm->buckets);
e8dba719 9380 ofputil_group_properties_destroy(&gm->props);
bc65c25a
SH
9381}
9382
4498bea5
SH
9383static struct ofpbuf *
9384ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version,
9385 const struct ofputil_group_mod *gm)
7395c052
NZ
9386{
9387 struct ofpbuf *b;
9388 struct ofp11_group_mod *ogm;
9389 size_t start_ogm;
7395c052 9390 struct ofputil_bucket *bucket;
7395c052 9391
4498bea5 9392 b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0);
6fd6ed71 9393 start_ogm = b->size;
4498bea5
SH
9394 ofpbuf_put_zeros(b, sizeof *ogm);
9395
9396 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
9397 ofputil_put_ofp11_bucket(bucket, b, ofp_version);
9398 }
9399 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
9400 ogm->command = htons(gm->command);
9401 ogm->type = gm->type;
9402 ogm->group_id = htonl(gm->group_id);
9403
9404 return b;
9405}
9406
18ac06d3
SH
9407static struct ofpbuf *
9408ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version,
9409 const struct ofputil_group_mod *gm)
9410{
9411 struct ofpbuf *b;
9412 struct ofp15_group_mod *ogm;
9413 size_t start_ogm;
9414 struct ofputil_bucket *bucket;
9415 struct id_pool *bucket_ids = NULL;
9416
9417 b = ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD, ofp_version, 0);
6fd6ed71 9418 start_ogm = b->size;
18ac06d3
SH
9419 ofpbuf_put_zeros(b, sizeof *ogm);
9420
9421 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
9422 uint32_t bucket_id;
9423
9424 /* Generate a bucket id if none was supplied */
9425 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
9426 if (!bucket_ids) {
9427 const struct ofputil_bucket *bkt;
9428
9429 bucket_ids = id_pool_create(0, OFPG15_BUCKET_MAX + 1);
9430
9431 /* Mark all bucket_ids that are present in gm
9432 * as used in the pool. */
9433 LIST_FOR_EACH_REVERSE (bkt, list_node, &gm->buckets) {
9434 if (bkt == bucket) {
9435 break;
9436 }
9437 if (bkt->bucket_id <= OFPG15_BUCKET_MAX) {
9438 id_pool_add(bucket_ids, bkt->bucket_id);
9439 }
9440 }
9441 }
9442
9443 if (!id_pool_alloc_id(bucket_ids, &bucket_id)) {
9444 OVS_NOT_REACHED();
9445 }
9446 } else {
9447 bucket_id = bucket->bucket_id;
9448 }
9449
9450 ofputil_put_ofp15_bucket(bucket, bucket_id, gm->type, b, ofp_version);
9451 }
9452 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
9453 ogm->command = htons(gm->command);
9454 ogm->type = gm->type;
9455 ogm->group_id = htonl(gm->group_id);
9456 ogm->command_bucket_id = htonl(gm->command_bucket_id);
6fd6ed71 9457 ogm->bucket_array_len = htons(b->size - start_ogm - sizeof *ogm);
18ac06d3 9458
53eb84a5
SH
9459 /* Add group properties */
9460 if (gm->props.selection_method[0]) {
9461 ofputil_put_group_prop_ntr_selection_method(ofp_version, &gm->props, b);
9462 }
9463
18ac06d3
SH
9464 id_pool_destroy(bucket_ids);
9465 return b;
9466}
9467
d4b1d0ca 9468static void
9dd30b05
BP
9469bad_group_cmd(enum ofp15_group_mod_command cmd)
9470{
d4b1d0ca
SH
9471 const char *opt_version;
9472 const char *version;
9473 const char *cmd_str;
9474
9475 switch (cmd) {
9476 case OFPGC15_ADD:
9477 case OFPGC15_MODIFY:
88b87a36 9478 case OFPGC15_ADD_OR_MOD:
d4b1d0ca
SH
9479 case OFPGC15_DELETE:
9480 version = "1.1";
9481 opt_version = "11";
9482 break;
9483
9484 case OFPGC15_INSERT_BUCKET:
9485 case OFPGC15_REMOVE_BUCKET:
9486 version = "1.5";
9487 opt_version = "15";
9dd30b05 9488 break;
d4b1d0ca
SH
9489
9490 default:
9491 OVS_NOT_REACHED();
9492 }
9493
9494 switch (cmd) {
9495 case OFPGC15_ADD:
9496 cmd_str = "add-group";
9497 break;
9498
9499 case OFPGC15_MODIFY:
88b87a36 9500 case OFPGC15_ADD_OR_MOD:
d4b1d0ca
SH
9501 cmd_str = "mod-group";
9502 break;
9503
9504 case OFPGC15_DELETE:
9505 cmd_str = "del-group";
9506 break;
9507
9508 case OFPGC15_INSERT_BUCKET:
9509 cmd_str = "insert-bucket";
9510 break;
9511
9512 case OFPGC15_REMOVE_BUCKET:
9dd30b05 9513 cmd_str = "remove-bucket";
d4b1d0ca
SH
9514 break;
9515
9516 default:
9517 OVS_NOT_REACHED();
9518 }
9519
9520 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
9521 cmd_str, version, opt_version);
9522
9523}
9524
4498bea5
SH
9525/* Converts abstract group mod 'gm' into a message for OpenFlow version
9526 * 'ofp_version' and returns the message. */
9527struct ofpbuf *
9528ofputil_encode_group_mod(enum ofp_version ofp_version,
9529 const struct ofputil_group_mod *gm)
9530{
d4b1d0ca 9531
7395c052 9532 switch (ofp_version) {
d4b1d0ca
SH
9533 case OFP10_VERSION:
9534 bad_group_cmd(gm->command);
7395c052
NZ
9535
9536 case OFP11_VERSION:
9537 case OFP12_VERSION:
c37c0382
AC
9538 case OFP13_VERSION:
9539 case OFP14_VERSION:
88b87a36 9540 if (gm->command > OFPGC11_DELETE && gm->command != OFPGC11_ADD_OR_MOD) {
d4b1d0ca
SH
9541 bad_group_cmd(gm->command);
9542 }
4498bea5 9543 return ofputil_encode_ofp11_group_mod(ofp_version, gm);
7395c052 9544
18ac06d3 9545 case OFP15_VERSION:
b79d45a1 9546 case OFP16_VERSION:
18ac06d3
SH
9547 return ofputil_encode_ofp15_group_mod(ofp_version, gm);
9548
7395c052 9549 default:
428b2edd 9550 OVS_NOT_REACHED();
7395c052 9551 }
7395c052
NZ
9552}
9553
aead63f2
SH
9554static enum ofperr
9555ofputil_pull_ofp11_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
9556 struct ofputil_group_mod *gm)
9557{
9558 const struct ofp11_group_mod *ogm;
08ba0810 9559 enum ofperr error;
aead63f2
SH
9560
9561 ogm = ofpbuf_pull(msg, sizeof *ogm);
9562 gm->command = ntohs(ogm->command);
9563 gm->type = ogm->type;
9564 gm->group_id = ntohl(ogm->group_id);
18ac06d3 9565 gm->command_bucket_id = OFPG15_BUCKET_ALL;
aead63f2 9566
6fd6ed71 9567 error = ofputil_pull_ofp11_buckets(msg, msg->size, ofp_version,
08ba0810
BP
9568 &gm->buckets);
9569
9570 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
9571 if (!error
9572 && ofp_version >= OFP13_VERSION
9573 && gm->command == OFPGC11_DELETE
417e7e66 9574 && !ovs_list_is_empty(&gm->buckets)) {
08ba0810
BP
9575 error = OFPERR_OFPGMFC_INVALID_GROUP;
9576 }
9577
9578 return error;
aead63f2
SH
9579}
9580
18ac06d3
SH
9581static enum ofperr
9582ofputil_pull_ofp15_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
9583 struct ofputil_group_mod *gm)
9584{
9585 const struct ofp15_group_mod *ogm;
9586 uint16_t bucket_list_len;
9587 enum ofperr error = OFPERR_OFPGMFC_BAD_BUCKET;
9588
9589 ogm = ofpbuf_pull(msg, sizeof *ogm);
9590 gm->command = ntohs(ogm->command);
9591 gm->type = ogm->type;
9592 gm->group_id = ntohl(ogm->group_id);
9593
9594 gm->command_bucket_id = ntohl(ogm->command_bucket_id);
9595 switch (gm->command) {
9596 case OFPGC15_REMOVE_BUCKET:
9597 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
9598 error = 0;
9599 }
9600 /* Fall through */
9601 case OFPGC15_INSERT_BUCKET:
9602 if (gm->command_bucket_id <= OFPG15_BUCKET_MAX ||
9603 gm->command_bucket_id == OFPG15_BUCKET_FIRST
9604 || gm->command_bucket_id == OFPG15_BUCKET_LAST) {
9605 error = 0;
9606 }
9607 break;
9608
9609 case OFPGC11_ADD:
9610 case OFPGC11_MODIFY:
88b87a36 9611 case OFPGC11_ADD_OR_MOD:
18ac06d3
SH
9612 case OFPGC11_DELETE:
9613 default:
9614 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
9615 error = 0;
9616 }
9617 break;
9618 }
9619 if (error) {
9620 VLOG_WARN_RL(&bad_ofmsg_rl,
9621 "group command bucket id (%u) is out of range",
9622 gm->command_bucket_id);
9623 return OFPERR_OFPGMFC_BAD_BUCKET;
9624 }
9625
79870963 9626 bucket_list_len = ntohs(ogm->bucket_array_len);
bc65c25a 9627 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, ofp_version,
64e8c446 9628 gm->type, &gm->buckets);
bc65c25a
SH
9629 if (error) {
9630 return error;
18ac06d3
SH
9631 }
9632
bc65c25a
SH
9633 return parse_ofp15_group_properties(msg, gm->type, gm->command, &gm->props,
9634 msg->size);
18ac06d3
SH
9635}
9636
7395c052
NZ
9637/* Converts OpenFlow group mod message 'oh' into an abstract group mod in
9638 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
9639enum ofperr
9640ofputil_decode_group_mod(const struct ofp_header *oh,
9641 struct ofputil_group_mod *gm)
9642{
0a2869d5 9643 ofputil_init_group_properties(&gm->props);
7395c052 9644
0a2869d5
BP
9645 enum ofp_version ofp_version = oh->version;
9646 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
7395c052
NZ
9647 ofpraw_pull_assert(&msg);
9648
0a2869d5 9649 enum ofperr err;
aead63f2
SH
9650 switch (ofp_version)
9651 {
9652 case OFP11_VERSION:
9653 case OFP12_VERSION:
9654 case OFP13_VERSION:
9655 case OFP14_VERSION:
aead63f2
SH
9656 err = ofputil_pull_ofp11_group_mod(&msg, ofp_version, gm);
9657 break;
9658
18ac06d3 9659 case OFP15_VERSION:
b79d45a1 9660 case OFP16_VERSION:
18ac06d3
SH
9661 err = ofputil_pull_ofp15_group_mod(&msg, ofp_version, gm);
9662 break;
9663
aead63f2
SH
9664 case OFP10_VERSION:
9665 default:
9666 OVS_NOT_REACHED();
9667 }
e57681e5
SH
9668 if (err) {
9669 return err;
9670 }
9671
e1799eb7
SH
9672 switch (gm->type) {
9673 case OFPGT11_INDIRECT:
beaa2b87
LS
9674 if (gm->command != OFPGC11_DELETE
9675 && !ovs_list_is_singleton(&gm->buckets) ) {
290eada7 9676 return OFPERR_OFPGMFC_INVALID_GROUP;
e1799eb7
SH
9677 }
9678 break;
9679 case OFPGT11_ALL:
9680 case OFPGT11_SELECT:
9681 case OFPGT11_FF:
9682 break;
9683 default:
09d4b951 9684 return OFPERR_OFPGMFC_BAD_TYPE;
65075b99
SH
9685 }
9686
9687 switch (gm->command) {
9688 case OFPGC11_ADD:
9689 case OFPGC11_MODIFY:
88b87a36 9690 case OFPGC11_ADD_OR_MOD:
65075b99
SH
9691 case OFPGC11_DELETE:
9692 case OFPGC15_INSERT_BUCKET:
9693 break;
9694 case OFPGC15_REMOVE_BUCKET:
417e7e66 9695 if (!ovs_list_is_empty(&gm->buckets)) {
65075b99
SH
9696 return OFPERR_OFPGMFC_BAD_BUCKET;
9697 }
9698 break;
9699 default:
09d4b951 9700 return OFPERR_OFPGMFC_BAD_COMMAND;
e1799eb7
SH
9701 }
9702
0a2869d5 9703 struct ofputil_bucket *bucket;
e57681e5 9704 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
64e8c446
BP
9705 if (bucket->weight && gm->type != OFPGT11_SELECT) {
9706 return OFPERR_OFPGMFC_INVALID_GROUP;
9707 }
9708
e57681e5
SH
9709 switch (gm->type) {
9710 case OFPGT11_ALL:
9711 case OFPGT11_INDIRECT:
9712 if (ofputil_bucket_has_liveness(bucket)) {
9713 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED;
9714 }
9715 break;
9716 case OFPGT11_SELECT:
9717 break;
9718 case OFPGT11_FF:
9719 if (!ofputil_bucket_has_liveness(bucket)) {
9720 return OFPERR_OFPGMFC_INVALID_GROUP;
9721 }
9722 break;
9723 default:
896313f3
FL
9724 /* Returning BAD TYPE to be consistent
9725 * though gm->type has been checked already. */
9726 return OFPERR_OFPGMFC_BAD_TYPE;
e57681e5
SH
9727 }
9728 }
9729
9730 return 0;
7395c052
NZ
9731}
9732
25070e04
JR
9733/* Destroys 'bms'. */
9734void
6dd3c787
JR
9735ofputil_free_bundle_msgs(struct ofputil_bundle_msg *bms, size_t n_bms)
9736{
9737 for (size_t i = 0; i < n_bms; i++) {
9738 switch ((int)bms[i].type) {
9739 case OFPTYPE_FLOW_MOD:
9740 free(CONST_CAST(struct ofpact *, bms[i].fm.ofpacts));
9741 break;
9742 case OFPTYPE_GROUP_MOD:
9743 ofputil_uninit_group_mod(&bms[i].gm);
9744 break;
9745 case OFPTYPE_PACKET_OUT:
9746 free(bms[i].po.ofpacts);
9747 free(CONST_CAST(void *, bms[i].po.packet));
9748 break;
9749 default:
9750 break;
9751 }
9752 }
9753 free(bms);
9754}
9755
9756void
9757ofputil_encode_bundle_msgs(const struct ofputil_bundle_msg *bms,
9758 size_t n_bms, struct ovs_list *requests,
25070e04
JR
9759 enum ofputil_protocol protocol)
9760{
9761 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
9762
9763 for (size_t i = 0; i < n_bms; i++) {
9764 struct ofpbuf *request = NULL;
9765
9766 switch ((int)bms[i].type) {
9767 case OFPTYPE_FLOW_MOD:
9768 request = ofputil_encode_flow_mod(&bms[i].fm, protocol);
25070e04
JR
9769 break;
9770 case OFPTYPE_GROUP_MOD:
9771 request = ofputil_encode_group_mod(version, &bms[i].gm);
6dd3c787
JR
9772 break;
9773 case OFPTYPE_PACKET_OUT:
9774 request = ofputil_encode_packet_out(&bms[i].po, protocol);
25070e04
JR
9775 break;
9776 default:
9777 break;
9778 }
9779 if (request) {
9780 ovs_list_push_back(requests, &request->list_node);
9781 }
9782 }
25070e04
JR
9783}
9784
64626975
SH
9785/* Parse a queue status request message into 'oqsr'.
9786 * Returns 0 if successful, otherwise an OFPERR_* number. */
9787enum ofperr
9788ofputil_decode_queue_stats_request(const struct ofp_header *request,
9789 struct ofputil_queue_stats_request *oqsr)
9790{
9791 switch ((enum ofp_version)request->version) {
b79d45a1 9792 case OFP16_VERSION:
42dccab5 9793 case OFP15_VERSION:
c37c0382 9794 case OFP14_VERSION:
2e1ae200 9795 case OFP13_VERSION:
64626975
SH
9796 case OFP12_VERSION:
9797 case OFP11_VERSION: {
9798 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
9799 oqsr->queue_id = ntohl(qsr11->queue_id);
9800 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
9801 }
9802
9803 case OFP10_VERSION: {
7f05e7ab
JR
9804 const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
9805 oqsr->queue_id = ntohl(qsr10->queue_id);
4e022ec0 9806 oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no));
7f05e7ab
JR
9807 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
9808 if (oqsr->port_no == OFPP_ALL) {
9809 oqsr->port_no = OFPP_ANY;
9810 }
64626975
SH
9811 return 0;
9812 }
9813
9814 default:
428b2edd 9815 OVS_NOT_REACHED();
64626975
SH
9816 }
9817}
9818
0746a84f
BP
9819/* Encode a queue stats request for 'oqsr', the encoded message
9820 * will be for OpenFlow version 'ofp_version'. Returns message
9821 * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
64626975
SH
9822struct ofpbuf *
9823ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
9824 const struct ofputil_queue_stats_request *oqsr)
9825{
9826 struct ofpbuf *request;
9827
9828 switch (ofp_version) {
9829 case OFP11_VERSION:
2e1ae200 9830 case OFP12_VERSION:
c37c0382 9831 case OFP13_VERSION:
42dccab5 9832 case OFP14_VERSION:
b79d45a1
BP
9833 case OFP15_VERSION:
9834 case OFP16_VERSION: {
64626975
SH
9835 struct ofp11_queue_stats_request *req;
9836 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
9837 req = ofpbuf_put_zeros(request, sizeof *req);
9838 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
9839 req->queue_id = htonl(oqsr->queue_id);
9840 break;
9841 }
9842 case OFP10_VERSION: {
9843 struct ofp10_queue_stats_request *req;
9844 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
9845 req = ofpbuf_put_zeros(request, sizeof *req);
7f05e7ab 9846 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
4e022ec0
AW
9847 req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY
9848 ? OFPP_ALL : oqsr->port_no));
64626975
SH
9849 req->queue_id = htonl(oqsr->queue_id);
9850 break;
9851 }
9852 default:
428b2edd 9853 OVS_NOT_REACHED();
64626975
SH
9854 }
9855
9856 return request;
9857}
9858
9859/* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
9860 * message 'oh'. */
9861size_t
9862ofputil_count_queue_stats(const struct ofp_header *oh)
9863{
0a2869d5 9864 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
64626975 9865 ofpraw_pull_assert(&b);
0a2869d5
BP
9866
9867 for (size_t n = 0; ; n++) {
9868 struct ofputil_queue_stats qs;
9869 if (ofputil_decode_queue_stats(&qs, &b)) {
9870 return n;
9871 }
1bb2cdbe 9872 }
64626975
SH
9873}
9874
9875static enum ofperr
9876ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
9877 const struct ofp10_queue_stats *qs10)
9878{
4e022ec0 9879 oqs->port_no = u16_to_ofp(ntohs(qs10->port_no));
64626975 9880 oqs->queue_id = ntohl(qs10->queue_id);
6dc34a0d
BP
9881 oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
9882 oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
9883 oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
9884 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
64626975
SH
9885
9886 return 0;
9887}
9888
9889static enum ofperr
9890ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
9891 const struct ofp11_queue_stats *qs11)
9892{
9893 enum ofperr error;
9894
9895 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
9896 if (error) {
9897 return error;
9898 }
9899
9900 oqs->queue_id = ntohl(qs11->queue_id);
6dc34a0d
BP
9901 oqs->tx_bytes = ntohll(qs11->tx_bytes);
9902 oqs->tx_packets = ntohll(qs11->tx_packets);
9903 oqs->tx_errors = ntohll(qs11->tx_errors);
9904 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
64626975
SH
9905
9906 return 0;
9907}
9908
2e1ae200
JR
9909static enum ofperr
9910ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
9911 const struct ofp13_queue_stats *qs13)
9912{
6dc34a0d 9913 enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
2e1ae200 9914 if (!error) {
6dc34a0d
BP
9915 oqs->duration_sec = ntohl(qs13->duration_sec);
9916 oqs->duration_nsec = ntohl(qs13->duration_nsec);
2e1ae200
JR
9917 }
9918
9919 return error;
9920}
9921
1bb2cdbe
BP
9922static enum ofperr
9923ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats *oqs,
9924 struct ofpbuf *msg)
9925{
9926 const struct ofp14_queue_stats *qs14;
9927 size_t len;
9928
9929 qs14 = ofpbuf_try_pull(msg, sizeof *qs14);
9930 if (!qs14) {
9931 return OFPERR_OFPBRC_BAD_LEN;
9932 }
9933
9934 len = ntohs(qs14->length);
6fd6ed71 9935 if (len < sizeof *qs14 || len - sizeof *qs14 > msg->size) {
1bb2cdbe
BP
9936 return OFPERR_OFPBRC_BAD_LEN;
9937 }
9938 ofpbuf_pull(msg, len - sizeof *qs14);
9939
9940 /* No properties yet defined, so ignore them for now. */
9941
9942 return ofputil_queue_stats_from_ofp13(oqs, &qs14->qs);
9943}
9944
64626975
SH
9945/* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
9946 * ofputil_queue_stats in 'qs'.
9947 *
9948 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
9949 * message. Calling this function multiple times for a single 'msg' iterates
9950 * through the replies. The caller must initially leave 'msg''s layer pointers
9951 * null and not modify them between calls.
9952 *
9953 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9954 * otherwise a positive errno value. */
9955int
9956ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
9957{
9958 enum ofperr error;
9959 enum ofpraw raw;
9960
6fd6ed71 9961 error = (msg->header ? ofpraw_decode(&raw, msg->header)
64626975
SH
9962 : ofpraw_pull(&raw, msg));
9963 if (error) {
9964 return error;
9965 }
9966
6fd6ed71 9967 if (!msg->size) {
64626975 9968 return EOF;
1bb2cdbe
BP
9969 } else if (raw == OFPRAW_OFPST14_QUEUE_REPLY) {
9970 return ofputil_pull_ofp14_queue_stats(qs, msg);
2e1ae200
JR
9971 } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
9972 const struct ofp13_queue_stats *qs13;
9973
9974 qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
9975 if (!qs13) {
9976 goto bad_len;
9977 }
9978 return ofputil_queue_stats_from_ofp13(qs, qs13);
64626975
SH
9979 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
9980 const struct ofp11_queue_stats *qs11;
9981
9982 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
9983 if (!qs11) {
2e1ae200 9984 goto bad_len;
64626975
SH
9985 }
9986 return ofputil_queue_stats_from_ofp11(qs, qs11);
9987 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
9988 const struct ofp10_queue_stats *qs10;
9989
9990 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
9991 if (!qs10) {
2e1ae200 9992 goto bad_len;
64626975
SH
9993 }
9994 return ofputil_queue_stats_from_ofp10(qs, qs10);
9995 } else {
428b2edd 9996 OVS_NOT_REACHED();
64626975 9997 }
2e1ae200
JR
9998
9999 bad_len:
437d0d22 10000 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover "
6fd6ed71 10001 "bytes at end", msg->size);
2e1ae200 10002 return OFPERR_OFPBRC_BAD_LEN;
64626975
SH
10003}
10004
10005static void
10006ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
10007 struct ofp10_queue_stats *qs10)
10008{
4e022ec0 10009 qs10->port_no = htons(ofp_to_u16(oqs->port_no));
64626975
SH
10010 memset(qs10->pad, 0, sizeof qs10->pad);
10011 qs10->queue_id = htonl(oqs->queue_id);
6dc34a0d
BP
10012 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes));
10013 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets));
10014 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors));
64626975
SH
10015}
10016
10017static void
10018ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
10019 struct ofp11_queue_stats *qs11)
10020{
10021 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
10022 qs11->queue_id = htonl(oqs->queue_id);
6dc34a0d
BP
10023 qs11->tx_bytes = htonll(oqs->tx_bytes);
10024 qs11->tx_packets = htonll(oqs->tx_packets);
10025 qs11->tx_errors = htonll(oqs->tx_errors);
64626975
SH
10026}
10027
2e1ae200
JR
10028static void
10029ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
10030 struct ofp13_queue_stats *qs13)
10031{
10032 ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
6dc34a0d
BP
10033 if (oqs->duration_sec != UINT32_MAX) {
10034 qs13->duration_sec = htonl(oqs->duration_sec);
10035 qs13->duration_nsec = htonl(oqs->duration_nsec);
10036 } else {
b8266395
BP
10037 qs13->duration_sec = OVS_BE32_MAX;
10038 qs13->duration_nsec = OVS_BE32_MAX;
6dc34a0d 10039 }
2e1ae200
JR
10040}
10041
1bb2cdbe
BP
10042static void
10043ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs,
10044 struct ofp14_queue_stats *qs14)
10045{
10046 qs14->length = htons(sizeof *qs14);
10047 memset(qs14->pad, 0, sizeof qs14->pad);
10048 ofputil_queue_stats_to_ofp13(oqs, &qs14->qs);
10049}
10050
10051
64626975
SH
10052/* Encode a queue stat for 'oqs' and append it to 'replies'. */
10053void
ca6ba700 10054ofputil_append_queue_stat(struct ovs_list *replies,
64626975
SH
10055 const struct ofputil_queue_stats *oqs)
10056{
e28ac5cf 10057 switch (ofpmp_version(replies)) {
2e1ae200
JR
10058 case OFP13_VERSION: {
10059 struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
10060 ofputil_queue_stats_to_ofp13(oqs, reply);
10061 break;
10062 }
10063
64626975
SH
10064 case OFP12_VERSION:
10065 case OFP11_VERSION: {
2e1ae200 10066 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
64626975
SH
10067 ofputil_queue_stats_to_ofp11(oqs, reply);
10068 break;
10069 }
10070
10071 case OFP10_VERSION: {
2e1ae200 10072 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
64626975
SH
10073 ofputil_queue_stats_to_ofp10(oqs, reply);
10074 break;
10075 }
10076
42dccab5 10077 case OFP14_VERSION:
b79d45a1
BP
10078 case OFP15_VERSION:
10079 case OFP16_VERSION: {
1bb2cdbe
BP
10080 struct ofp14_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
10081 ofputil_queue_stats_to_ofp14(oqs, reply);
c37c0382 10082 break;
1bb2cdbe 10083 }
c37c0382 10084
64626975 10085 default:
428b2edd 10086 OVS_NOT_REACHED();
64626975
SH
10087 }
10088}
777af88d
AC
10089
10090enum ofperr
10091ofputil_decode_bundle_ctrl(const struct ofp_header *oh,
10092 struct ofputil_bundle_ctrl_msg *msg)
10093{
0a2869d5
BP
10094 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
10095 enum ofpraw raw = ofpraw_pull_assert(&b);
06d4d4b6
JR
10096 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL
10097 || raw == OFPRAW_ONFT13_BUNDLE_CONTROL);
777af88d 10098
0a2869d5 10099 const struct ofp14_bundle_ctrl_msg *m = b.msg;
777af88d
AC
10100 msg->bundle_id = ntohl(m->bundle_id);
10101 msg->type = ntohs(m->type);
10102 msg->flags = ntohs(m->flags);
10103
10104 return 0;
10105}
10106
db5076ee
JR
10107struct ofpbuf *
10108ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version,
10109 struct ofputil_bundle_ctrl_msg *bc)
10110{
10111 struct ofpbuf *request;
10112 struct ofp14_bundle_ctrl_msg *m;
10113
10114 switch (ofp_version) {
10115 case OFP10_VERSION:
10116 case OFP11_VERSION:
10117 case OFP12_VERSION:
06d4d4b6 10118 ovs_fatal(0, "bundles need OpenFlow 1.3 or later "
db5076ee 10119 "(\'-O OpenFlow14\')");
06d4d4b6 10120 case OFP13_VERSION:
db5076ee
JR
10121 case OFP14_VERSION:
10122 case OFP15_VERSION:
b79d45a1 10123 case OFP16_VERSION:
06d4d4b6
JR
10124 request = ofpraw_alloc(ofp_version == OFP13_VERSION
10125 ? OFPRAW_ONFT13_BUNDLE_CONTROL
10126 : OFPRAW_OFPT14_BUNDLE_CONTROL, ofp_version, 0);
db5076ee
JR
10127 m = ofpbuf_put_zeros(request, sizeof *m);
10128
10129 m->bundle_id = htonl(bc->bundle_id);
10130 m->type = htons(bc->type);
10131 m->flags = htons(bc->flags);
10132 break;
10133 default:
10134 OVS_NOT_REACHED();
10135 }
10136
10137 return request;
10138}
10139
777af88d
AC
10140struct ofpbuf *
10141ofputil_encode_bundle_ctrl_reply(const struct ofp_header *oh,
10142 struct ofputil_bundle_ctrl_msg *msg)
10143{
10144 struct ofpbuf *buf;
10145 struct ofp14_bundle_ctrl_msg *m;
10146
06d4d4b6
JR
10147 buf = ofpraw_alloc_reply(oh->version == OFP13_VERSION
10148 ? OFPRAW_ONFT13_BUNDLE_CONTROL
10149 : OFPRAW_OFPT14_BUNDLE_CONTROL, oh, 0);
777af88d
AC
10150 m = ofpbuf_put_zeros(buf, sizeof *m);
10151
10152 m->bundle_id = htonl(msg->bundle_id);
10153 m->type = htons(msg->type);
10154 m->flags = htons(msg->flags);
10155
10156 return buf;
10157}
10158
c25ce22d
JR
10159/* Return true for bundlable state change requests, false for other messages.
10160 */
10161static bool
10162ofputil_is_bundlable(enum ofptype type)
10163{
10164 switch (type) {
10165 /* Minimum required by OpenFlow 1.4. */
10166 case OFPTYPE_PORT_MOD:
10167 case OFPTYPE_FLOW_MOD:
25070e04
JR
10168 /* Other supported types. */
10169 case OFPTYPE_GROUP_MOD:
6dd3c787 10170 case OFPTYPE_PACKET_OUT:
c25ce22d
JR
10171 return true;
10172
10173 /* Nice to have later. */
10174 case OFPTYPE_FLOW_MOD_TABLE_ID:
c25ce22d
JR
10175 case OFPTYPE_TABLE_MOD:
10176 case OFPTYPE_METER_MOD:
4e548ad9 10177 case OFPTYPE_NXT_TLV_TABLE_MOD:
c25ce22d
JR
10178
10179 /* Not to be bundlable. */
10180 case OFPTYPE_ECHO_REQUEST:
10181 case OFPTYPE_FEATURES_REQUEST:
10182 case OFPTYPE_GET_CONFIG_REQUEST:
10183 case OFPTYPE_SET_CONFIG:
10184 case OFPTYPE_BARRIER_REQUEST:
10185 case OFPTYPE_ROLE_REQUEST:
10186 case OFPTYPE_ECHO_REPLY:
10187 case OFPTYPE_SET_FLOW_FORMAT:
10188 case OFPTYPE_SET_PACKET_IN_FORMAT:
10189 case OFPTYPE_SET_CONTROLLER_ID:
10190 case OFPTYPE_FLOW_AGE:
10191 case OFPTYPE_FLOW_MONITOR_CANCEL:
10192 case OFPTYPE_SET_ASYNC_CONFIG:
10193 case OFPTYPE_GET_ASYNC_REQUEST:
10194 case OFPTYPE_DESC_STATS_REQUEST:
10195 case OFPTYPE_FLOW_STATS_REQUEST:
10196 case OFPTYPE_AGGREGATE_STATS_REQUEST:
10197 case OFPTYPE_TABLE_STATS_REQUEST:
10198 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
03c72922 10199 case OFPTYPE_TABLE_DESC_REQUEST:
c25ce22d
JR
10200 case OFPTYPE_PORT_STATS_REQUEST:
10201 case OFPTYPE_QUEUE_STATS_REQUEST:
10202 case OFPTYPE_PORT_DESC_STATS_REQUEST:
10203 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
10204 case OFPTYPE_METER_STATS_REQUEST:
10205 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
10206 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
10207 case OFPTYPE_GROUP_STATS_REQUEST:
10208 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
10209 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
10210 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
10211 case OFPTYPE_BUNDLE_CONTROL:
10212 case OFPTYPE_BUNDLE_ADD_MESSAGE:
10213 case OFPTYPE_HELLO:
10214 case OFPTYPE_ERROR:
10215 case OFPTYPE_FEATURES_REPLY:
10216 case OFPTYPE_GET_CONFIG_REPLY:
10217 case OFPTYPE_PACKET_IN:
10218 case OFPTYPE_FLOW_REMOVED:
10219 case OFPTYPE_PORT_STATUS:
10220 case OFPTYPE_BARRIER_REPLY:
10221 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
10222 case OFPTYPE_DESC_STATS_REPLY:
10223 case OFPTYPE_FLOW_STATS_REPLY:
10224 case OFPTYPE_QUEUE_STATS_REPLY:
10225 case OFPTYPE_PORT_STATS_REPLY:
10226 case OFPTYPE_TABLE_STATS_REPLY:
10227 case OFPTYPE_AGGREGATE_STATS_REPLY:
10228 case OFPTYPE_PORT_DESC_STATS_REPLY:
10229 case OFPTYPE_ROLE_REPLY:
10230 case OFPTYPE_FLOW_MONITOR_PAUSED:
10231 case OFPTYPE_FLOW_MONITOR_RESUMED:
10232 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
10233 case OFPTYPE_GET_ASYNC_REPLY:
10234 case OFPTYPE_GROUP_STATS_REPLY:
10235 case OFPTYPE_GROUP_DESC_STATS_REPLY:
10236 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
10237 case OFPTYPE_METER_STATS_REPLY:
10238 case OFPTYPE_METER_CONFIG_STATS_REPLY:
10239 case OFPTYPE_METER_FEATURES_STATS_REPLY:
10240 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
03c72922 10241 case OFPTYPE_TABLE_DESC_REPLY:
c25ce22d 10242 case OFPTYPE_ROLE_STATUS:
3c35db62 10243 case OFPTYPE_REQUESTFORWARD:
6c6eedc5 10244 case OFPTYPE_TABLE_STATUS:
4e548ad9
ML
10245 case OFPTYPE_NXT_TLV_TABLE_REQUEST:
10246 case OFPTYPE_NXT_TLV_TABLE_REPLY:
77ab5fd2 10247 case OFPTYPE_NXT_RESUME:
fb8f22c1
BY
10248 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST:
10249 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY:
10250 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST:
10251 case OFPTYPE_IPFIX_FLOW_STATS_REPLY:
2a7c4805 10252 case OFPTYPE_CT_FLUSH_ZONE:
c25ce22d
JR
10253 break;
10254 }
10255
10256 return false;
10257}
10258
777af88d
AC
10259enum ofperr
10260ofputil_decode_bundle_add(const struct ofp_header *oh,
7ac27a04 10261 struct ofputil_bundle_add_msg *msg,
0a2869d5 10262 enum ofptype *typep)
777af88d 10263{
0a2869d5
BP
10264 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
10265 enum ofpraw raw = ofpraw_pull_assert(&b);
06d4d4b6
JR
10266 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE
10267 || raw == OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE);
777af88d 10268
0a2869d5 10269 const struct ofp14_bundle_ctrl_msg *m = ofpbuf_pull(&b, sizeof *m);
777af88d
AC
10270 msg->bundle_id = ntohl(m->bundle_id);
10271 msg->flags = ntohs(m->flags);
10272
6fd6ed71 10273 msg->msg = b.data;
46be7132 10274 if (msg->msg->version != oh->version) {
bc02606a 10275 return OFPERR_OFPBFC_BAD_VERSION;
46be7132 10276 }
0a2869d5 10277 size_t inner_len = ntohs(msg->msg->length);
6fd6ed71 10278 if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
777af88d
AC
10279 return OFPERR_OFPBFC_MSG_BAD_LEN;
10280 }
be6f6393
JR
10281 if (msg->msg->xid != oh->xid) {
10282 return OFPERR_OFPBFC_MSG_BAD_XID;
10283 }
777af88d 10284
c25ce22d 10285 /* Reject unbundlable messages. */
0a2869d5
BP
10286 enum ofptype type;
10287 enum ofperr error = ofptype_decode(&type, msg->msg);
c25ce22d
JR
10288 if (error) {
10289 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPT14_BUNDLE_ADD_MESSAGE contained "
10290 "message is unparsable (%s)", ofperr_get_name(error));
10291 return OFPERR_OFPBFC_MSG_UNSUP; /* 'error' would be confusing. */
10292 }
10293
0a2869d5 10294 if (!ofputil_is_bundlable(type)) {
44648b0f 10295 VLOG_WARN_RL(&bad_ofmsg_rl, "%s message not allowed inside "
0a2869d5 10296 "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(type));
c25ce22d
JR
10297 return OFPERR_OFPBFC_MSG_UNSUP;
10298 }
0a2869d5
BP
10299 if (typep) {
10300 *typep = type;
10301 }
c25ce22d 10302
777af88d
AC
10303 return 0;
10304}
10305
10306struct ofpbuf *
10307ofputil_encode_bundle_add(enum ofp_version ofp_version,
10308 struct ofputil_bundle_add_msg *msg)
10309{
10310 struct ofpbuf *request;
10311 struct ofp14_bundle_ctrl_msg *m;
10312
7b3dca89 10313 /* Must use the same xid as the embedded message. */
06d4d4b6
JR
10314 request = ofpraw_alloc_xid(ofp_version == OFP13_VERSION
10315 ? OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE
10316 : OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE, ofp_version,
bcf899f7 10317 msg->msg->xid, ntohs(msg->msg->length));
777af88d
AC
10318 m = ofpbuf_put_zeros(request, sizeof *m);
10319
10320 m->bundle_id = htonl(msg->bundle_id);
10321 m->flags = htons(msg->flags);
10322 ofpbuf_put(request, msg->msg, ntohs(msg->msg->length));
10323
bcf899f7 10324 ofpmsg_update_length(request);
777af88d
AC
10325 return request;
10326}
6159c531
JG
10327
10328static void
4e548ad9 10329encode_tlv_table_mappings(struct ofpbuf *b, struct ovs_list *mappings)
6159c531 10330{
4e548ad9 10331 struct ofputil_tlv_map *map;
6159c531
JG
10332
10333 LIST_FOR_EACH (map, list_node, mappings) {
4e548ad9 10334 struct nx_tlv_map *nx_map;
6159c531
JG
10335
10336 nx_map = ofpbuf_put_zeros(b, sizeof *nx_map);
10337 nx_map->option_class = htons(map->option_class);
10338 nx_map->option_type = map->option_type;
10339 nx_map->option_len = map->option_len;
10340 nx_map->index = htons(map->index);
10341 }
10342}
10343
10344struct ofpbuf *
4e548ad9
ML
10345ofputil_encode_tlv_table_mod(enum ofp_version ofp_version,
10346 struct ofputil_tlv_table_mod *ttm)
6159c531
JG
10347{
10348 struct ofpbuf *b;
4e548ad9 10349 struct nx_tlv_table_mod *nx_ttm;
6159c531 10350
4e548ad9
ML
10351 b = ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_MOD, ofp_version, 0);
10352 nx_ttm = ofpbuf_put_zeros(b, sizeof *nx_ttm);
10353 nx_ttm->command = htons(ttm->command);
10354 encode_tlv_table_mappings(b, &ttm->mappings);
6159c531
JG
10355
10356 return b;
10357}
10358
10359static enum ofperr
4e548ad9 10360decode_tlv_table_mappings(struct ofpbuf *msg, unsigned int max_fields,
dfe5044c 10361 struct ovs_list *mappings)
6159c531 10362{
417e7e66 10363 ovs_list_init(mappings);
6159c531
JG
10364
10365 while (msg->size) {
4e548ad9
ML
10366 struct nx_tlv_map *nx_map;
10367 struct ofputil_tlv_map *map;
6159c531
JG
10368
10369 nx_map = ofpbuf_pull(msg, sizeof *nx_map);
10370 map = xmalloc(sizeof *map);
417e7e66 10371 ovs_list_push_back(mappings, &map->list_node);
6159c531
JG
10372
10373 map->option_class = ntohs(nx_map->option_class);
10374 map->option_type = nx_map->option_type;
10375
10376 map->option_len = nx_map->option_len;
4e548ad9 10377 if (map->option_len % 4 || map->option_len > TLV_MAX_OPT_SIZE) {
6159c531 10378 VLOG_WARN_RL(&bad_ofmsg_rl,
4e548ad9 10379 "tlv table option length (%u) is not a valid option size",
6159c531 10380 map->option_len);
4e548ad9
ML
10381 ofputil_uninit_tlv_table(mappings);
10382 return OFPERR_NXTTMFC_BAD_OPT_LEN;
6159c531
JG
10383 }
10384
10385 map->index = ntohs(nx_map->index);
dfe5044c 10386 if (map->index >= max_fields) {
6159c531 10387 VLOG_WARN_RL(&bad_ofmsg_rl,
4e548ad9 10388 "tlv table field index (%u) is too large (max %u)",
dfe5044c 10389 map->index, max_fields - 1);
4e548ad9
ML
10390 ofputil_uninit_tlv_table(mappings);
10391 return OFPERR_NXTTMFC_BAD_FIELD_IDX;
6159c531
JG
10392 }
10393 }
10394
10395 return 0;
10396}
10397
10398enum ofperr
4e548ad9
ML
10399ofputil_decode_tlv_table_mod(const struct ofp_header *oh,
10400 struct ofputil_tlv_table_mod *ttm)
6159c531 10401{
0a2869d5 10402 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
6159c531
JG
10403 ofpraw_pull_assert(&msg);
10404
0a2869d5 10405 struct nx_tlv_table_mod *nx_ttm = ofpbuf_pull(&msg, sizeof *nx_ttm);
4e548ad9
ML
10406 ttm->command = ntohs(nx_ttm->command);
10407 if (ttm->command > NXTTMC_CLEAR) {
6159c531 10408 VLOG_WARN_RL(&bad_ofmsg_rl,
4e548ad9
ML
10409 "tlv table mod command (%u) is out of range",
10410 ttm->command);
10411 return OFPERR_NXTTMFC_BAD_COMMAND;
6159c531
JG
10412 }
10413
4e548ad9
ML
10414 return decode_tlv_table_mappings(&msg, TUN_METADATA_NUM_OPTS,
10415 &ttm->mappings);
6159c531
JG
10416}
10417
10418struct ofpbuf *
4e548ad9
ML
10419ofputil_encode_tlv_table_reply(const struct ofp_header *oh,
10420 struct ofputil_tlv_table_reply *ttr)
6159c531
JG
10421{
10422 struct ofpbuf *b;
4e548ad9 10423 struct nx_tlv_table_reply *nx_ttr;
6159c531 10424
4e548ad9
ML
10425 b = ofpraw_alloc_reply(OFPRAW_NXT_TLV_TABLE_REPLY, oh, 0);
10426 nx_ttr = ofpbuf_put_zeros(b, sizeof *nx_ttr);
10427 nx_ttr->max_option_space = htonl(ttr->max_option_space);
10428 nx_ttr->max_fields = htons(ttr->max_fields);
6159c531 10429
4e548ad9 10430 encode_tlv_table_mappings(b, &ttr->mappings);
6159c531
JG
10431
10432 return b;
10433}
10434
4e548ad9 10435/* Decodes the NXT_TLV_TABLE_REPLY message in 'oh' into '*ttr'. Returns 0
dfe5044c
BP
10436 * if successful, otherwise an ofperr.
10437 *
4e548ad9
ML
10438 * The decoder verifies that the indexes in 'ttr->mappings' are less than
10439 * 'ttr->max_fields', but the caller must ensure, if necessary, that they are
dfe5044c 10440 * less than TUN_METADATA_NUM_OPTS. */
6159c531 10441enum ofperr
4e548ad9
ML
10442ofputil_decode_tlv_table_reply(const struct ofp_header *oh,
10443 struct ofputil_tlv_table_reply *ttr)
6159c531 10444{
0a2869d5 10445 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
6159c531
JG
10446 ofpraw_pull_assert(&msg);
10447
0a2869d5 10448 struct nx_tlv_table_reply *nx_ttr = ofpbuf_pull(&msg, sizeof *nx_ttr);
4e548ad9
ML
10449 ttr->max_option_space = ntohl(nx_ttr->max_option_space);
10450 ttr->max_fields = ntohs(nx_ttr->max_fields);
6159c531 10451
4e548ad9 10452 return decode_tlv_table_mappings(&msg, ttr->max_fields, &ttr->mappings);
6159c531
JG
10453}
10454
10455void
4e548ad9 10456ofputil_uninit_tlv_table(struct ovs_list *mappings)
6159c531 10457{
4e548ad9 10458 struct ofputil_tlv_map *map;
6159c531
JG
10459
10460 LIST_FOR_EACH_POP (map, list_node, mappings) {
10461 free(map);
10462 }
10463}
98090482 10464
2da7974c
BP
10465const char *
10466ofputil_async_msg_type_to_string(enum ofputil_async_msg_type type)
10467{
10468 switch (type) {
10469 case OAM_PACKET_IN: return "PACKET_IN";
10470 case OAM_PORT_STATUS: return "PORT_STATUS";
10471 case OAM_FLOW_REMOVED: return "FLOW_REMOVED";
10472 case OAM_ROLE_STATUS: return "ROLE_STATUS";
10473 case OAM_TABLE_STATUS: return "TABLE_STATUS";
10474 case OAM_REQUESTFORWARD: return "REQUESTFORWARD";
10475
10476 case OAM_N_TYPES:
10477 default:
10478 OVS_NOT_REACHED();
10479 }
10480}
10481
8fd0bb60
BP
10482struct ofp14_async_prop {
10483 uint64_t prop_type;
10484 enum ofputil_async_msg_type oam;
10485 bool master;
10486 uint32_t allowed10, allowed14;
10487};
10488
10489#define AP_PAIR(SLAVE_PROP_TYPE, OAM, A10, A14) \
10490 { SLAVE_PROP_TYPE, OAM, false, A10, (A14) ? (A14) : (A10) }, \
10491 { (SLAVE_PROP_TYPE + 1), OAM, true, A10, (A14) ? (A14) : (A10) }
10492
10493static const struct ofp14_async_prop async_props[] = {
10494 AP_PAIR( 0, OAM_PACKET_IN, OFPR10_BITS, OFPR14_BITS),
10495 AP_PAIR( 2, OAM_PORT_STATUS, (1 << OFPPR_N_REASONS) - 1, 0),
10496 AP_PAIR( 4, OAM_FLOW_REMOVED, (1 << OVS_OFPRR_NONE) - 1, 0),
10497 AP_PAIR( 6, OAM_ROLE_STATUS, (1 << OFPCRR_N_REASONS) - 1, 0),
10498 AP_PAIR( 8, OAM_TABLE_STATUS, OFPTR_BITS, 0),
10499 AP_PAIR(10, OAM_REQUESTFORWARD, (1 << OFPRFR_N_REASONS) - 1, 0),
10500};
10501
10502#define FOR_EACH_ASYNC_PROP(VAR) \
10503 for (const struct ofp14_async_prop *VAR = async_props; \
10504 VAR < &async_props[ARRAY_SIZE(async_props)]; VAR++)
10505
10506static const struct ofp14_async_prop *
10507get_ofp14_async_config_prop_by_prop_type(uint64_t prop_type)
10508{
10509 FOR_EACH_ASYNC_PROP (ap) {
10510 if (prop_type == ap->prop_type) {
10511 return ap;
10512 }
10513 }
10514 return NULL;
10515}
10516
10517static const struct ofp14_async_prop *
10518get_ofp14_async_config_prop_by_oam(enum ofputil_async_msg_type oam,
10519 bool master)
10520{
10521 FOR_EACH_ASYNC_PROP (ap) {
10522 if (ap->oam == oam && ap->master == master) {
10523 return ap;
10524 }
10525 }
10526 return NULL;
10527}
10528
10529static uint32_t
10530ofp14_async_prop_allowed(const struct ofp14_async_prop *prop,
10531 enum ofp_version version)
10532{
10533 return version >= OFP14_VERSION ? prop->allowed14 : prop->allowed10;
10534}
10535
10536static ovs_be32
10537encode_async_mask(const struct ofputil_async_cfg *src,
10538 const struct ofp14_async_prop *ap,
10539 enum ofp_version version)
10540{
10541 uint32_t mask = ap->master ? src->master[ap->oam] : src->slave[ap->oam];
10542 return htonl(mask & ofp14_async_prop_allowed(ap, version));
10543}
10544
10545static enum ofperr
10546decode_async_mask(ovs_be32 src,
10547 const struct ofp14_async_prop *ap, enum ofp_version version,
10548 bool loose, struct ofputil_async_cfg *dst)
10549{
10550 uint32_t mask = ntohl(src);
10551 uint32_t allowed = ofp14_async_prop_allowed(ap, version);
10552 if (mask & ~allowed) {
10553 OFPPROP_LOG(&bad_ofmsg_rl, loose,
10554 "bad value %#x for %s (allowed mask %#x)",
10555 mask, ofputil_async_msg_type_to_string(ap->oam),
10556 allowed);
10557 mask &= allowed;
10558 if (!loose) {
10559 return OFPERR_OFPACFC_INVALID;
10560 }
10561 }
10562
9bfe9334
BP
10563 if (ap->oam == OAM_PACKET_IN) {
10564 if (mask & (1u << OFPR_NO_MATCH)) {
10565 mask |= 1u << OFPR_EXPLICIT_MISS;
10566 if (version < OFP13_VERSION) {
10567 mask |= 1u << OFPR_IMPLICIT_MISS;
10568 }
10569 }
10570 }
10571
8fd0bb60
BP
10572 uint32_t *array = ap->master ? dst->master : dst->slave;
10573 array[ap->oam] = mask;
10574 return 0;
10575}
10576
10577static enum ofperr
10578parse_async_tlv(const struct ofpbuf *property,
10579 const struct ofp14_async_prop *ap,
10580 struct ofputil_async_cfg *ac,
10581 enum ofp_version version, bool loose)
10582{
10583 enum ofperr error;
10584 ovs_be32 mask;
10585
10586 error = ofpprop_parse_be32(property, &mask);
10587 if (error) {
10588 return error;
10589 }
10590
10591 if (ofpprop_is_experimenter(ap->prop_type)) {
10592 /* For experimenter properties, whether a property is for the master or
10593 * slave role is indicated by both 'type' and 'exp_type' in struct
10594 * ofp_prop_experimenter. Check that these are consistent. */
10595 const struct ofp_prop_experimenter *ope = property->data;
10596 bool should_be_master = ope->type == htons(0xffff);
10597 if (should_be_master != ap->master) {
10598 VLOG_WARN_RL(&bad_ofmsg_rl, "async property type %#"PRIx16" "
10599 "indicates %s role but exp_type %"PRIu32" indicates "
10600 "%s role",
10601 ntohs(ope->type),
10602 should_be_master ? "master" : "slave",
10603 ntohl(ope->exp_type),
10604 ap->master ? "master" : "slave");
10605 return OFPERR_OFPBPC_BAD_EXP_TYPE;
10606 }
10607 }
10608
10609 return decode_async_mask(mask, ap, version, loose, ac);
10610}
10611
10612static void
10613decode_legacy_async_masks(const ovs_be32 masks[2],
10614 enum ofputil_async_msg_type oam,
10615 enum ofp_version version,
10616 struct ofputil_async_cfg *dst)
10617{
10618 for (int i = 0; i < 2; i++) {
10619 bool master = i == 0;
10620 const struct ofp14_async_prop *ap
10621 = get_ofp14_async_config_prop_by_oam(oam, master);
10622 decode_async_mask(masks[i], ap, version, true, dst);
10623 }
10624}
10625
98090482 10626/* Decodes the OpenFlow "set async config" request and "get async config
a930d4c5 10627 * reply" message in '*oh' into an abstract form in 'ac'.
98090482 10628 *
5876b4db
BP
10629 * Some versions of the "set async config" request change only some of the
10630 * settings and leave the others alone. This function uses 'basis' as the
10631 * initial state for decoding these. Other versions of the request change all
10632 * the settings; this function ignores 'basis' when decoding these.
10633 *
98090482
NR
10634 * If 'loose' is true, this function ignores properties and values that it does
10635 * not understand, as a controller would want to do when interpreting
10636 * capabilities provided by a switch. If 'loose' is false, this function
10637 * treats unknown properties and values as an error, as a switch would want to
10638 * do when interpreting a configuration request made by a controller.
10639 *
d18cc1ee
AA
10640 * Returns 0 if successful, otherwise an OFPERR_* value.
10641 *
10642 * Returns error code OFPERR_OFPACFC_INVALID if the value of mask is not in
10643 * the valid range of mask.
10644 *
10645 * Returns error code OFPERR_OFPACFC_UNSUPPORTED if the configuration is not
10646 * supported.*/
98090482 10647enum ofperr
a930d4c5 10648ofputil_decode_set_async_config(const struct ofp_header *oh, bool loose,
5876b4db 10649 const struct ofputil_async_cfg *basis,
a930d4c5 10650 struct ofputil_async_cfg *ac)
98090482 10651{
0a2869d5
BP
10652 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
10653 enum ofpraw raw = ofpraw_pull_assert(&b);
98090482
NR
10654
10655 if (raw == OFPRAW_OFPT13_SET_ASYNC ||
10656 raw == OFPRAW_NXT_SET_ASYNC_CONFIG ||
10657 raw == OFPRAW_OFPT13_GET_ASYNC_REPLY) {
10658 const struct nx_async_config *msg = ofpmsg_body(oh);
10659
5876b4db 10660 *ac = OFPUTIL_ASYNC_CFG_INIT;
8fd0bb60
BP
10661 decode_legacy_async_masks(msg->packet_in_mask, OAM_PACKET_IN,
10662 oh->version, ac);
10663 decode_legacy_async_masks(msg->port_status_mask, OAM_PORT_STATUS,
10664 oh->version, ac);
10665 decode_legacy_async_masks(msg->flow_removed_mask, OAM_FLOW_REMOVED,
10666 oh->version, ac);
98090482 10667 } else if (raw == OFPRAW_OFPT14_SET_ASYNC ||
af7bc161
BP
10668 raw == OFPRAW_OFPT14_GET_ASYNC_REPLY ||
10669 raw == OFPRAW_NXT_SET_ASYNC_CONFIG2) {
5876b4db 10670 *ac = *basis;
98090482 10671 while (b.size > 0) {
98090482
NR
10672 struct ofpbuf property;
10673 enum ofperr error;
34a543e3 10674 uint64_t type;
98090482 10675
34a543e3 10676 error = ofpprop_pull__(&b, &property, 8, 0xfffe, &type);
98090482
NR
10677 if (error) {
10678 return error;
10679 }
10680
8fd0bb60
BP
10681 const struct ofp14_async_prop *ap
10682 = get_ofp14_async_config_prop_by_prop_type(type);
10683 error = (ap
10684 ? parse_async_tlv(&property, ap, ac, oh->version, loose)
10685 : OFPPROP_UNKNOWN(loose, "async config", type));
b611d3ac 10686 if (error) {
8fd0bb60
BP
10687 /* Most messages use OFPBPC_BAD_TYPE but async has its own (who
10688 * knows why, it's OpenFlow. */
10689 if (error == OFPERR_OFPBPC_BAD_TYPE) {
10690 error = OFPERR_OFPACFC_UNSUPPORTED;
d18cc1ee 10691 }
98090482
NR
10692 return error;
10693 }
10694 }
10695 } else {
10696 return OFPERR_OFPBRC_BAD_VERSION;
10697 }
10698 return 0;
10699}
10700
8fd0bb60
BP
10701static void
10702encode_legacy_async_masks(const struct ofputil_async_cfg *ac,
10703 enum ofputil_async_msg_type oam,
10704 enum ofp_version version,
10705 ovs_be32 masks[2])
98090482 10706{
8fd0bb60
BP
10707 for (int i = 0; i < 2; i++) {
10708 bool master = i == 0;
10709 const struct ofp14_async_prop *ap
10710 = get_ofp14_async_config_prop_by_oam(oam, master);
10711 masks[i] = encode_async_mask(ac, ap, version);
10712 }
10713}
98090482 10714
8fd0bb60
BP
10715static void
10716ofputil_put_async_config__(const struct ofputil_async_cfg *ac,
10717 struct ofpbuf *buf, bool tlv,
10718 enum ofp_version version, uint32_t oams)
10719{
10720 if (!tlv) {
10721 struct nx_async_config *msg = ofpbuf_put_zeros(buf, sizeof *msg);
10722 encode_legacy_async_masks(ac, OAM_PACKET_IN, version,
10723 msg->packet_in_mask);
10724 encode_legacy_async_masks(ac, OAM_PORT_STATUS, version,
10725 msg->port_status_mask);
10726 encode_legacy_async_masks(ac, OAM_FLOW_REMOVED, version,
10727 msg->flow_removed_mask);
10728 } else {
10729 FOR_EACH_ASYNC_PROP (ap) {
10730 if (oams & (1u << ap->oam)) {
10731 size_t ofs = buf->size;
10732 ofpprop_put_be32(buf, ap->prop_type,
10733 encode_async_mask(ac, ap, version));
10734
10735 /* For experimenter properties, we need to use type 0xfffe for
10736 * master and 0xffff for slaves. */
10737 if (ofpprop_is_experimenter(ap->prop_type)) {
10738 struct ofp_prop_experimenter *ope
10739 = ofpbuf_at_assert(buf, ofs, sizeof *ope);
10740 ope->type = ap->master ? htons(0xffff) : htons(0xfffe);
10741 }
10742 }
98090482 10743 }
98090482 10744 }
98090482
NR
10745}
10746
8fd0bb60
BP
10747/* Encodes and returns a reply to the OFPT_GET_ASYNC_REQUEST in 'oh' that
10748 * states that the asynchronous message configuration is 'ac'. */
98090482 10749struct ofpbuf *
af7bc161
BP
10750ofputil_encode_get_async_reply(const struct ofp_header *oh,
10751 const struct ofputil_async_cfg *ac)
98090482
NR
10752{
10753 struct ofpbuf *buf;
98090482 10754
8fd0bb60
BP
10755 enum ofpraw raw = (oh->version < OFP14_VERSION
10756 ? OFPRAW_OFPT13_GET_ASYNC_REPLY
10757 : OFPRAW_OFPT14_GET_ASYNC_REPLY);
10758 struct ofpbuf *reply = ofpraw_alloc_reply(raw, oh, 0);
10759 ofputil_put_async_config__(ac, reply,
10760 raw == OFPRAW_OFPT14_GET_ASYNC_REPLY,
10761 oh->version, UINT32_MAX);
10762 return reply;
98090482
NR
10763
10764 return buf;
10765}
a930d4c5 10766
af7bc161
BP
10767/* Encodes and returns a message, in a format appropriate for OpenFlow version
10768 * 'ofp_version', that sets the asynchronous message configuration to 'ac'.
10769 *
10770 * Specify 'oams' as a bitmap of OAM_* that indicate the asynchronous messages
10771 * to configure. OF1.0 through OF1.3 can't natively configure a subset of
10772 * messages, so more messages than requested may be configured. OF1.0 through
10773 * OF1.3 also can't configure OVS extension OAM_* values, so if 'oam' includes
10774 * any extensions then this function encodes an Open vSwitch extension message
10775 * that does support configuring OVS extension OAM_*. */
10776struct ofpbuf *
10777ofputil_encode_set_async_config(const struct ofputil_async_cfg *ac,
10778 uint32_t oams, enum ofp_version ofp_version)
10779{
10780 enum ofpraw raw = (ofp_version >= OFP14_VERSION ? OFPRAW_OFPT14_SET_ASYNC
10781 : oams & OAM_EXTENSIONS ? OFPRAW_NXT_SET_ASYNC_CONFIG2
10782 : ofp_version >= OFP13_VERSION ? OFPRAW_OFPT13_SET_ASYNC
10783 : OFPRAW_NXT_SET_ASYNC_CONFIG);
10784 struct ofpbuf *request = ofpraw_alloc(raw, ofp_version, 0);
10785 ofputil_put_async_config__(ac, request,
10786 (raw == OFPRAW_OFPT14_SET_ASYNC ||
10787 raw == OFPRAW_NXT_SET_ASYNC_CONFIG2),
10788 ofp_version, oams);
10789 return request;
10790}
10791
a930d4c5
BP
10792struct ofputil_async_cfg
10793ofputil_async_cfg_default(enum ofp_version version)
10794{
9bfe9334
BP
10795 /* We enable all of the OF1.4 reasons regardless of 'version' because the
10796 * reasons added in OF1.4 just are just refinements of the OFPR_ACTION
10797 * introduced in OF1.0, breaking it into more specific categories. When we
10798 * encode these for earlier OpenFlow versions, we translate them into
10799 * OFPR_ACTION. */
10800 uint32_t pin = OFPR14_BITS & ~(1u << OFPR_INVALID_TTL);
10801 pin |= 1u << OFPR_EXPLICIT_MISS;
10802 if (version <= OFP12_VERSION) {
10803 pin |= 1u << OFPR_IMPLICIT_MISS;
10804 }
10805
a930d4c5 10806 return (struct ofputil_async_cfg) {
9bfe9334 10807 .master[OAM_PACKET_IN] = pin,
a930d4c5
BP
10808
10809 .master[OAM_FLOW_REMOVED]
10810 = (version >= OFP14_VERSION ? OFPRR14_BITS : OFPRR10_BITS),
10811
10812 .master[OAM_PORT_STATUS] = OFPPR_BITS,
10813 .slave[OAM_PORT_STATUS] = OFPPR_BITS,
10814 };
10815}
6c6eedc5
SJ
10816
10817static void
10818ofputil_put_ofp14_table_desc(const struct ofputil_table_desc *td,
10819 struct ofpbuf *b, enum ofp_version version)
10820{
10821 struct ofp14_table_desc *otd;
10822 struct ofp14_table_mod_prop_vacancy *otv;
10823 size_t start_otd;
10824
10825 start_otd = b->size;
10826 ofpbuf_put_zeros(b, sizeof *otd);
10827
10828 ofpprop_put_u32(b, OFPTMPT14_EVICTION, td->eviction_flags);
10829
10830 otv = ofpbuf_put_zeros(b, sizeof *otv);
10831 otv->type = htons(OFPTMPT14_VACANCY);
10832 otv->length = htons(sizeof *otv);
10833 otv->vacancy_down = td->table_vacancy.vacancy_down;
10834 otv->vacancy_up = td->table_vacancy.vacancy_up;
10835 otv->vacancy = td->table_vacancy.vacancy;
10836
10837 otd = ofpbuf_at_assert(b, start_otd, sizeof *otd);
10838 otd->length = htons(b->size - start_otd);
10839 otd->table_id = td->table_id;
10840 otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
10841 td->eviction, td->vacancy,
10842 version);
10843}
10844
10845/* Converts the abstract form of a "table status" message in '*ts' into an
10846 * OpenFlow message suitable for 'protocol', and returns that encoded form in
10847 * a buffer owned by the caller. */
10848struct ofpbuf *
10849ofputil_encode_table_status(const struct ofputil_table_status *ts,
10850 enum ofputil_protocol protocol)
10851{
10852 enum ofp_version version;
10853 struct ofpbuf *b;
10854
10855 version = ofputil_protocol_to_ofp_version(protocol);
10856 if (version >= OFP14_VERSION) {
10857 enum ofpraw raw;
10858 struct ofp14_table_status *ots;
10859
10860 raw = OFPRAW_OFPT14_TABLE_STATUS;
10861 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
10862 ots = ofpbuf_put_zeros(b, sizeof *ots);
10863 ots->reason = ts->reason;
10864 ofputil_put_ofp14_table_desc(&ts->desc, b, version);
10865 ofpmsg_update_length(b);
10866 return b;
10867 } else {
10868 return NULL;
10869 }
10870}
10871
10872/* Decodes the OpenFlow "table status" message in '*ots' into an abstract form
10873 * in '*ts'. Returns 0 if successful, otherwise an OFPERR_* value. */
10874enum ofperr
10875ofputil_decode_table_status(const struct ofp_header *oh,
10876 struct ofputil_table_status *ts)
10877{
10878 const struct ofp14_table_status *ots;
10879 struct ofpbuf b;
10880 enum ofperr error;
10881 enum ofpraw raw;
10882
10883 ofpbuf_use_const(&b, oh, ntohs(oh->length));
10884 raw = ofpraw_pull_assert(&b);
10885 ots = ofpbuf_pull(&b, sizeof *ots);
10886
10887 if (raw == OFPRAW_OFPT14_TABLE_STATUS) {
10888 if (ots->reason != OFPTR_VACANCY_DOWN
10889 && ots->reason != OFPTR_VACANCY_UP) {
10890 return OFPERR_OFPBPC_BAD_VALUE;
10891 }
10892 ts->reason = ots->reason;
10893
10894 error = ofputil_decode_table_desc(&b, &ts->desc, oh->version);
10895 return error;
10896 } else {
10897 return OFPERR_OFPBRC_BAD_VERSION;
10898 }
10899
10900 return 0;
10901}