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