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