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