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