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