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