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