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