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