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