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