]> git.proxmox.com Git - ovs.git/blame - lib/ofp-util.c
dpif-netdev: Fix use-after-free in dpif_netdev_recv.
[ovs.git] / lib / ofp-util.c
CommitLineData
fa37b408 1/*
e0edde6f 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
fa37b408
BP
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <config.h>
18#include "ofp-print.h"
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>
3b6a2571 25#include "autopath.h"
daff3353 26#include "bundle.h"
10a24935 27#include "byte-order.h"
d8ae4d67 28#include "classifier.h"
dc4762ed 29#include "dynamic-string.h"
75a75043 30#include "learn.h"
816fd533 31#include "meta-flow.h"
9e1fd49b 32#include "multipath.h"
6c038611 33#include "netdev.h"
b6c9e612 34#include "nx-match.h"
dc4762ed 35#include "ofp-errors.h"
fa37b408
BP
36#include "ofp-util.h"
37#include "ofpbuf.h"
38#include "packets.h"
39#include "random.h"
4ffd1b43 40#include "unaligned.h"
e41a9130 41#include "type-props.h"
5136ce49 42#include "vlog.h"
fa37b408 43
d98e6007 44VLOG_DEFINE_THIS_MODULE(ofp_util);
fa37b408
BP
45
46/* Rate limit for OpenFlow message parse errors. These always indicate a bug
47 * in the peer and so there's not much point in showing a lot of them. */
48static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
49
0596e897
BP
50/* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
51 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
52 * is wildcarded.
53 *
54 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
55 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
56 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
57 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
58 * wildcarded. */
59ovs_be32
60ofputil_wcbits_to_netmask(int wcbits)
61{
62 wcbits &= 0x3f;
63 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
64}
65
66/* Given the IP netmask 'netmask', returns the number of bits of the IP address
aad29cd1
BP
67 * that it wildcards, that is, the number of 0-bits in 'netmask'. 'netmask'
68 * must be a CIDR netmask (see ip_is_cidr()). */
0596e897
BP
69int
70ofputil_netmask_to_wcbits(ovs_be32 netmask)
71{
aad29cd1 72 return 32 - ip_count_cidr_bits(netmask);
0596e897
BP
73}
74
d8ae4d67
BP
75/* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
76 * name. */
77#define WC_INVARIANT_LIST \
78 WC_INVARIANT_BIT(IN_PORT) \
d8ae4d67 79 WC_INVARIANT_BIT(DL_TYPE) \
73f33563 80 WC_INVARIANT_BIT(NW_PROTO)
d8ae4d67
BP
81
82/* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
83 * actually have the same names and values. */
84#define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME);
85 WC_INVARIANT_LIST
86#undef WC_INVARIANT_BIT
87
88/* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
89 * OR'd together. */
eeba8e4f 90static const flow_wildcards_t WC_INVARIANTS = 0
d8ae4d67
BP
91#define WC_INVARIANT_BIT(NAME) | FWW_##NAME
92 WC_INVARIANT_LIST
93#undef WC_INVARIANT_BIT
eeba8e4f 94;
d8ae4d67 95
eb6f28db
BP
96/* Converts the wildcard in 'ofpfw' into a flow_wildcards in 'wc' for use in
97 * struct cls_rule. It is the caller's responsibility to handle the special
98 * case where the flow match's dl_vlan is set to OFP_VLAN_NONE. */
7286b1e1 99void
eb6f28db 100ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *wc)
d8ae4d67 101{
73c0ce34 102 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 11);
a877206f 103
d8ae4d67 104 /* Initialize most of rule->wc. */
f9ba8dad 105 flow_wildcards_init_catchall(wc);
eeba8e4f 106 wc->wildcards = (OVS_FORCE flow_wildcards_t) ofpfw & WC_INVARIANTS;
bad68a99
JP
107
108 /* Wildcard fields that aren't defined by ofp_match or tun_id. */
2486e66a 109 wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_NW_ECN | FWW_NW_TTL
47284b1f 110 | FWW_IPV6_LABEL);
bad68a99 111
2486e66a
JP
112 if (ofpfw & OFPFW_NW_TOS) {
113 /* OpenFlow 1.0 defines a TOS wildcard, but it's much later in
114 * the enum than we can use. */
115 wc->wildcards |= FWW_NW_DSCP;
d8ae4d67 116 }
7257b535 117
d8ae4d67
BP
118 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
119 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
120
73f33563
BP
121 if (!(ofpfw & OFPFW_TP_SRC)) {
122 wc->tp_src_mask = htons(UINT16_MAX);
123 }
124 if (!(ofpfw & OFPFW_TP_DST)) {
125 wc->tp_dst_mask = htons(UINT16_MAX);
126 }
127
73c0ce34
JS
128 if (!(ofpfw & OFPFW_DL_SRC)) {
129 memset(wc->dl_src_mask, 0xff, ETH_ADDR_LEN);
130 }
131 if (!(ofpfw & OFPFW_DL_DST)) {
132 memset(wc->dl_dst_mask, 0xff, ETH_ADDR_LEN);
d8ae4d67
BP
133 }
134
eb6f28db
BP
135 /* VLAN TCI mask. */
136 if (!(ofpfw & OFPFW_DL_VLAN_PCP)) {
137 wc->vlan_tci_mask |= htons(VLAN_PCP_MASK | VLAN_CFI);
138 }
139 if (!(ofpfw & OFPFW_DL_VLAN)) {
140 wc->vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
141 }
142}
143
144/* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
145 * 'priority'. */
146void
147ofputil_cls_rule_from_match(const struct ofp_match *match,
148 unsigned int priority, struct cls_rule *rule)
149{
150 uint32_t ofpfw = ntohl(match->wildcards) & OFPFW_ALL;
151
152 /* Initialize rule->priority, rule->wc. */
153 rule->priority = !ofpfw ? UINT16_MAX : priority;
154 ofputil_wildcard_from_openflow(ofpfw, &rule->wc);
155
66642cb4 156 /* Initialize most of rule->flow. */
d8ae4d67
BP
157 rule->flow.nw_src = match->nw_src;
158 rule->flow.nw_dst = match->nw_dst;
abe529af 159 rule->flow.in_port = ntohs(match->in_port);
36956a7d 160 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
d8ae4d67
BP
161 rule->flow.tp_src = match->tp_src;
162 rule->flow.tp_dst = match->tp_dst;
163 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
164 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
eadef313 165 rule->flow.nw_tos = match->nw_tos & IP_DSCP_MASK;
d8ae4d67
BP
166 rule->flow.nw_proto = match->nw_proto;
167
66642cb4 168 /* Translate VLANs. */
47271d0d
BP
169 if (!(ofpfw & OFPFW_DL_VLAN) && match->dl_vlan == htons(OFP_VLAN_NONE)) {
170 /* Match only packets without 802.1Q header.
171 *
172 * When OFPFW_DL_VLAN_PCP is wildcarded, this is obviously correct.
173 *
174 * If OFPFW_DL_VLAN_PCP is matched, the flow match is contradictory,
175 * because we can't have a specific PCP without an 802.1Q header.
176 * However, older versions of OVS treated this as matching packets
177 * withut an 802.1Q header, so we do here too. */
66642cb4 178 rule->flow.vlan_tci = htons(0);
eb6f28db 179 rule->wc.vlan_tci_mask = htons(0xffff);
47271d0d
BP
180 } else {
181 ovs_be16 vid, pcp, tci;
182
47271d0d
BP
183 vid = match->dl_vlan & htons(VLAN_VID_MASK);
184 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
185 tci = vid | pcp | htons(VLAN_CFI);
eb6f28db 186 rule->flow.vlan_tci = tci & rule->wc.vlan_tci_mask;
66642cb4
BP
187 }
188
d8ae4d67
BP
189 /* Clean up. */
190 cls_rule_zero_wildcarded_fields(rule);
191}
192
b78f6b77 193/* Convert 'rule' into the OpenFlow match structure 'match'. */
d8ae4d67 194void
b78f6b77 195ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match)
d8ae4d67
BP
196{
197 const struct flow_wildcards *wc = &rule->wc;
eeba8e4f 198 uint32_t ofpfw;
d8ae4d67 199
66642cb4 200 /* Figure out most OpenFlow wildcards. */
eeba8e4f 201 ofpfw = (OVS_FORCE uint32_t) (wc->wildcards & WC_INVARIANTS);
d8ae4d67
BP
202 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
203 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
2486e66a 204 if (wc->wildcards & FWW_NW_DSCP) {
d8ae4d67
BP
205 ofpfw |= OFPFW_NW_TOS;
206 }
73f33563
BP
207 if (!wc->tp_src_mask) {
208 ofpfw |= OFPFW_TP_SRC;
209 }
210 if (!wc->tp_dst_mask) {
211 ofpfw |= OFPFW_TP_DST;
212 }
73c0ce34
JS
213 if (eth_addr_is_zero(wc->dl_src_mask)) {
214 ofpfw |= OFPFW_DL_SRC;
215 }
216 if (eth_addr_is_zero(wc->dl_dst_mask)) {
217 ofpfw |= OFPFW_DL_DST;
218 }
ff9d3826 219
66642cb4
BP
220 /* Translate VLANs. */
221 match->dl_vlan = htons(0);
222 match->dl_vlan_pcp = 0;
223 if (rule->wc.vlan_tci_mask == htons(0)) {
224 ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
225 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
226 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
227 match->dl_vlan = htons(OFP_VLAN_NONE);
228 } else {
229 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
230 ofpfw |= OFPFW_DL_VLAN;
231 } else {
232 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
233 }
234
235 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
236 ofpfw |= OFPFW_DL_VLAN_PCP;
237 } else {
238 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
239 }
240 }
241
242 /* Compose most of the match structure. */
d8ae4d67 243 match->wildcards = htonl(ofpfw);
abe529af 244 match->in_port = htons(rule->flow.in_port);
d8ae4d67
BP
245 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
246 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
36956a7d 247 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
d8ae4d67
BP
248 match->nw_src = rule->flow.nw_src;
249 match->nw_dst = rule->flow.nw_dst;
eadef313 250 match->nw_tos = rule->flow.nw_tos & IP_DSCP_MASK;
d8ae4d67
BP
251 match->nw_proto = rule->flow.nw_proto;
252 match->tp_src = rule->flow.tp_src;
253 match->tp_dst = rule->flow.tp_dst;
254 memset(match->pad1, '\0', sizeof match->pad1);
255 memset(match->pad2, '\0', sizeof match->pad2);
256}
257
36956a7d
BP
258/* Given a 'dl_type' value in the format used in struct flow, returns the
259 * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */
260ovs_be16
261ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
262{
263 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
264 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
265 : flow_dl_type);
266}
267
268/* Given a 'dl_type' value in the format used in an OpenFlow ofp_match
269 * structure, returns the corresponding 'dl_type' value for use in struct
270 * flow. */
271ovs_be16
272ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
273{
274 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
275 ? htons(FLOW_DL_TYPE_NONE)
276 : ofp_dl_type);
277}
278
72fae175 279/* Returns a transaction ID to use for an outgoing OpenFlow message. */
44381c1b 280static ovs_be32
fa37b408
BP
281alloc_xid(void)
282{
72fae175 283 static uint32_t next_xid = 1;
44381c1b 284 return htonl(next_xid++);
fa37b408 285}
d1e2cf21
BP
286\f
287/* Basic parsing of OpenFlow messages. */
fa37b408 288
d1e2cf21
BP
289struct ofputil_msg_type {
290 enum ofputil_msg_code code; /* OFPUTIL_*. */
a1893da1 291 uint8_t ofp_version; /* An OpenFlow version or 0 for "any". */
d1e2cf21
BP
292 uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
293 const char *name; /* e.g. "OFPT_FLOW_REMOVED". */
294 unsigned int min_size; /* Minimum total message size in bytes. */
295 /* 0 if 'min_size' is the exact size that the message must be. Otherwise,
296 * the message may exceed 'min_size' by an even multiple of this value. */
297 unsigned int extra_multiple;
298};
299
5a020ef3
BP
300/* Represents a malformed OpenFlow message. */
301static const struct ofputil_msg_type ofputil_invalid_type = {
a1893da1 302 OFPUTIL_MSG_INVALID, 0, 0, "OFPUTIL_MSG_INVALID", 0, 0
5a020ef3
BP
303};
304
d1e2cf21
BP
305struct ofputil_msg_category {
306 const char *name; /* e.g. "OpenFlow message" */
307 const struct ofputil_msg_type *types;
308 size_t n_types;
90bf1e07 309 enum ofperr missing_error; /* Error value for missing type. */
d1e2cf21
BP
310};
311
90bf1e07 312static enum ofperr
5a020ef3 313ofputil_check_length(const struct ofputil_msg_type *type, unsigned int size)
d1e2cf21
BP
314{
315 switch (type->extra_multiple) {
316 case 0:
317 if (size != type->min_size) {
5a020ef3 318 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
d1e2cf21 319 "length %u (expected length %u)",
5a020ef3 320 type->name, size, type->min_size);
90bf1e07 321 return OFPERR_OFPBRC_BAD_LEN;
d1e2cf21 322 }
5a020ef3 323 return 0;
d1e2cf21
BP
324
325 case 1:
326 if (size < type->min_size) {
5a020ef3 327 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
d1e2cf21 328 "length %u (expected length at least %u bytes)",
5a020ef3 329 type->name, size, type->min_size);
90bf1e07 330 return OFPERR_OFPBRC_BAD_LEN;
d1e2cf21 331 }
5a020ef3 332 return 0;
d1e2cf21
BP
333
334 default:
335 if (size < type->min_size
336 || (size - type->min_size) % type->extra_multiple) {
5a020ef3 337 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
d1e2cf21
BP
338 "length %u (must be exactly %u bytes or longer "
339 "by an integer multiple of %u bytes)",
5a020ef3 340 type->name, size,
d1e2cf21 341 type->min_size, type->extra_multiple);
90bf1e07 342 return OFPERR_OFPBRC_BAD_LEN;
d1e2cf21 343 }
5a020ef3 344 return 0;
d1e2cf21
BP
345 }
346}
347
90bf1e07 348static enum ofperr
d1e2cf21 349ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
a1893da1 350 uint8_t version, uint32_t value,
d1e2cf21
BP
351 const struct ofputil_msg_type **typep)
352{
353 const struct ofputil_msg_type *type;
354
355 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
a1893da1
BP
356 if (type->value == value
357 && (!type->ofp_version || version == type->ofp_version)) {
d1e2cf21
BP
358 *typep = type;
359 return 0;
360 }
361 }
362
5c47debb 363 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
d1e2cf21
BP
364 cat->name, value);
365 return cat->missing_error;
366}
367
90bf1e07 368static enum ofperr
5a020ef3 369ofputil_decode_vendor(const struct ofp_header *oh, size_t length,
d1e2cf21
BP
370 const struct ofputil_msg_type **typep)
371{
372 static const struct ofputil_msg_type nxt_messages[] = {
a1893da1 373 { OFPUTIL_NXT_ROLE_REQUEST, OFP10_VERSION,
d1e2cf21
BP
374 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
375 sizeof(struct nx_role_request), 0 },
376
a1893da1 377 { OFPUTIL_NXT_ROLE_REPLY, OFP10_VERSION,
d1e2cf21
BP
378 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
379 sizeof(struct nx_role_request), 0 },
380
a1893da1 381 { OFPUTIL_NXT_SET_FLOW_FORMAT, OFP10_VERSION,
d1e2cf21 382 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
73dbf4ab 383 sizeof(struct nx_set_flow_format), 0 },
d1e2cf21 384
a1893da1 385 { OFPUTIL_NXT_SET_PACKET_IN_FORMAT, OFP10_VERSION,
54834960 386 NXT_SET_PACKET_IN_FORMAT, "NXT_SET_PACKET_IN_FORMAT",
73dbf4ab 387 sizeof(struct nx_set_packet_in_format), 0 },
54834960 388
a1893da1 389 { OFPUTIL_NXT_PACKET_IN, OFP10_VERSION,
54834960 390 NXT_PACKET_IN, "NXT_PACKET_IN",
73dbf4ab 391 sizeof(struct nx_packet_in), 1 },
54834960 392
a1893da1 393 { OFPUTIL_NXT_FLOW_MOD, OFP10_VERSION,
d1e2cf21
BP
394 NXT_FLOW_MOD, "NXT_FLOW_MOD",
395 sizeof(struct nx_flow_mod), 8 },
396
a1893da1 397 { OFPUTIL_NXT_FLOW_REMOVED, OFP10_VERSION,
d1e2cf21
BP
398 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
399 sizeof(struct nx_flow_removed), 8 },
d1e9b9bf 400
a1893da1 401 { OFPUTIL_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION,
d1e9b9bf 402 NXT_FLOW_MOD_TABLE_ID, "NXT_FLOW_MOD_TABLE_ID",
73dbf4ab 403 sizeof(struct nx_flow_mod_table_id), 0 },
f27f2134
BP
404
405 { OFPUTIL_NXT_FLOW_AGE, OFP10_VERSION,
406 NXT_FLOW_AGE, "NXT_FLOW_AGE",
407 sizeof(struct nicira_header), 0 },
80d5aefd
BP
408
409 { OFPUTIL_NXT_SET_ASYNC_CONFIG, OFP10_VERSION,
410 NXT_SET_ASYNC_CONFIG, "NXT_SET_ASYNC_CONFIG",
411 sizeof(struct nx_async_config), 0 },
a7349929
BP
412
413 { OFPUTIL_NXT_SET_CONTROLLER_ID, OFP10_VERSION,
414 NXT_SET_CONTROLLER_ID, "NXT_SET_CONTROLLER_ID",
415 sizeof(struct nx_controller_id), 0 },
d1e2cf21
BP
416 };
417
418 static const struct ofputil_msg_category nxt_category = {
419 "Nicira extension message",
420 nxt_messages, ARRAY_SIZE(nxt_messages),
90bf1e07 421 OFPERR_OFPBRC_BAD_SUBTYPE
d1e2cf21
BP
422 };
423
424 const struct ofp_vendor_header *ovh;
425 const struct nicira_header *nh;
426
5a020ef3
BP
427 if (length < sizeof(struct ofp_vendor_header)) {
428 if (length == ntohs(oh->length)) {
429 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor message");
430 }
90bf1e07 431 return OFPERR_OFPBRC_BAD_LEN;
5a020ef3
BP
432 }
433
d1e2cf21
BP
434 ovh = (const struct ofp_vendor_header *) oh;
435 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
436 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
437 "vendor %"PRIx32, ntohl(ovh->vendor));
90bf1e07 438 return OFPERR_OFPBRC_BAD_VENDOR;
d1e2cf21
BP
439 }
440
5a020ef3
BP
441 if (length < sizeof(struct nicira_header)) {
442 if (length == ntohs(oh->length)) {
443 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
444 "length %u (expected at least %zu)",
445 ntohs(ovh->header.length),
446 sizeof(struct nicira_header));
447 }
90bf1e07 448 return OFPERR_OFPBRC_BAD_LEN;
d1e2cf21
BP
449 }
450
451 nh = (const struct nicira_header *) oh;
a1893da1
BP
452 return ofputil_lookup_openflow_message(&nxt_category, oh->version,
453 ntohl(nh->subtype), typep);
d1e2cf21
BP
454}
455
90bf1e07 456static enum ofperr
5a020ef3 457check_nxstats_msg(const struct ofp_header *oh, size_t length)
d1e2cf21 458{
28c8bad1 459 const struct ofp_stats_msg *osm = (const struct ofp_stats_msg *) oh;
d1e2cf21
BP
460 ovs_be32 vendor;
461
5a020ef3
BP
462 if (length < sizeof(struct ofp_vendor_stats_msg)) {
463 if (length == ntohs(oh->length)) {
464 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor stats message");
465 }
90bf1e07 466 return OFPERR_OFPBRC_BAD_LEN;
5a020ef3
BP
467 }
468
03cd3493 469 memcpy(&vendor, osm + 1, sizeof vendor);
d1e2cf21
BP
470 if (vendor != htonl(NX_VENDOR_ID)) {
471 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
472 "unknown vendor %"PRIx32, ntohl(vendor));
90bf1e07 473 return OFPERR_OFPBRC_BAD_VENDOR;
d1e2cf21
BP
474 }
475
5a020ef3
BP
476 if (length < sizeof(struct nicira_stats_msg)) {
477 if (length == ntohs(osm->header.length)) {
478 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
479 }
90bf1e07 480 return OFPERR_OFPBRC_BAD_LEN;
d1e2cf21
BP
481 }
482
483 return 0;
484}
485
90bf1e07 486static enum ofperr
5a020ef3 487ofputil_decode_nxst_request(const struct ofp_header *oh, size_t length,
d1e2cf21
BP
488 const struct ofputil_msg_type **typep)
489{
490 static const struct ofputil_msg_type nxst_requests[] = {
a1893da1 491 { OFPUTIL_NXST_FLOW_REQUEST, OFP10_VERSION,
d1e2cf21
BP
492 NXST_FLOW, "NXST_FLOW request",
493 sizeof(struct nx_flow_stats_request), 8 },
494
a1893da1 495 { OFPUTIL_NXST_AGGREGATE_REQUEST, OFP10_VERSION,
d1e2cf21
BP
496 NXST_AGGREGATE, "NXST_AGGREGATE request",
497 sizeof(struct nx_aggregate_stats_request), 8 },
498 };
499
500 static const struct ofputil_msg_category nxst_request_category = {
08717852 501 "Nicira extension statistics request",
d1e2cf21 502 nxst_requests, ARRAY_SIZE(nxst_requests),
90bf1e07 503 OFPERR_OFPBRC_BAD_SUBTYPE
d1e2cf21
BP
504 };
505
506 const struct nicira_stats_msg *nsm;
90bf1e07 507 enum ofperr error;
d1e2cf21 508
5a020ef3 509 error = check_nxstats_msg(oh, length);
d1e2cf21
BP
510 if (error) {
511 return error;
512 }
513
514 nsm = (struct nicira_stats_msg *) oh;
a1893da1 515 return ofputil_lookup_openflow_message(&nxst_request_category, oh->version,
5a020ef3 516 ntohl(nsm->subtype), typep);
d1e2cf21
BP
517}
518
90bf1e07 519static enum ofperr
5a020ef3 520ofputil_decode_nxst_reply(const struct ofp_header *oh, size_t length,
d1e2cf21
BP
521 const struct ofputil_msg_type **typep)
522{
523 static const struct ofputil_msg_type nxst_replies[] = {
a1893da1 524 { OFPUTIL_NXST_FLOW_REPLY, OFP10_VERSION,
d1e2cf21
BP
525 NXST_FLOW, "NXST_FLOW reply",
526 sizeof(struct nicira_stats_msg), 8 },
527
a1893da1 528 { OFPUTIL_NXST_AGGREGATE_REPLY, OFP10_VERSION,
d1e2cf21
BP
529 NXST_AGGREGATE, "NXST_AGGREGATE reply",
530 sizeof(struct nx_aggregate_stats_reply), 0 },
531 };
532
533 static const struct ofputil_msg_category nxst_reply_category = {
08717852 534 "Nicira extension statistics reply",
d1e2cf21 535 nxst_replies, ARRAY_SIZE(nxst_replies),
90bf1e07 536 OFPERR_OFPBRC_BAD_SUBTYPE
d1e2cf21
BP
537 };
538
539 const struct nicira_stats_msg *nsm;
90bf1e07 540 enum ofperr error;
d1e2cf21 541
5a020ef3 542 error = check_nxstats_msg(oh, length);
d1e2cf21
BP
543 if (error) {
544 return error;
545 }
546
547 nsm = (struct nicira_stats_msg *) oh;
a1893da1 548 return ofputil_lookup_openflow_message(&nxst_reply_category, oh->version,
5a020ef3
BP
549 ntohl(nsm->subtype), typep);
550}
551
90bf1e07 552static enum ofperr
5a020ef3
BP
553check_stats_msg(const struct ofp_header *oh, size_t length)
554{
555 if (length < sizeof(struct ofp_stats_msg)) {
556 if (length == ntohs(oh->length)) {
557 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated stats message");
558 }
90bf1e07 559 return OFPERR_OFPBRC_BAD_LEN;
5a020ef3
BP
560 }
561
562 return 0;
d1e2cf21
BP
563}
564
90bf1e07 565static enum ofperr
5a020ef3 566ofputil_decode_ofpst_request(const struct ofp_header *oh, size_t length,
d1e2cf21
BP
567 const struct ofputil_msg_type **typep)
568{
d1e2cf21 569 static const struct ofputil_msg_type ofpst_requests[] = {
a1893da1 570 { OFPUTIL_OFPST_DESC_REQUEST, OFP10_VERSION,
d1e2cf21 571 OFPST_DESC, "OFPST_DESC request",
63f2140a 572 sizeof(struct ofp_stats_msg), 0 },
d1e2cf21 573
a1893da1 574 { OFPUTIL_OFPST_FLOW_REQUEST, OFP10_VERSION,
d1e2cf21 575 OFPST_FLOW, "OFPST_FLOW request",
63f2140a 576 sizeof(struct ofp_flow_stats_request), 0 },
d1e2cf21 577
a1893da1 578 { OFPUTIL_OFPST_AGGREGATE_REQUEST, OFP10_VERSION,
d1e2cf21 579 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
63f2140a 580 sizeof(struct ofp_flow_stats_request), 0 },
d1e2cf21 581
a1893da1 582 { OFPUTIL_OFPST_TABLE_REQUEST, OFP10_VERSION,
d1e2cf21 583 OFPST_TABLE, "OFPST_TABLE request",
63f2140a 584 sizeof(struct ofp_stats_msg), 0 },
d1e2cf21 585
a1893da1 586 { OFPUTIL_OFPST_PORT_REQUEST, OFP10_VERSION,
d1e2cf21 587 OFPST_PORT, "OFPST_PORT request",
63f2140a 588 sizeof(struct ofp_port_stats_request), 0 },
d1e2cf21 589
a1893da1 590 { OFPUTIL_OFPST_QUEUE_REQUEST, OFP10_VERSION,
d1e2cf21 591 OFPST_QUEUE, "OFPST_QUEUE request",
63f2140a 592 sizeof(struct ofp_queue_stats_request), 0 },
d1e2cf21 593
2be393ed
JP
594 { OFPUTIL_OFPST_PORT_DESC_REQUEST, OFP10_VERSION,
595 OFPST_PORT_DESC, "OFPST_PORT_DESC request",
596 sizeof(struct ofp_stats_msg), 0 },
597
a1893da1 598 { 0, 0,
d1e2cf21 599 OFPST_VENDOR, "OFPST_VENDOR request",
63f2140a 600 sizeof(struct ofp_vendor_stats_msg), 1 },
d1e2cf21
BP
601 };
602
603 static const struct ofputil_msg_category ofpst_request_category = {
604 "OpenFlow statistics",
605 ofpst_requests, ARRAY_SIZE(ofpst_requests),
90bf1e07 606 OFPERR_OFPBRC_BAD_STAT
d1e2cf21
BP
607 };
608
28c8bad1 609 const struct ofp_stats_msg *request = (const struct ofp_stats_msg *) oh;
90bf1e07 610 enum ofperr error;
d1e2cf21 611
5a020ef3
BP
612 error = check_stats_msg(oh, length);
613 if (error) {
614 return error;
615 }
616
d1e2cf21 617 error = ofputil_lookup_openflow_message(&ofpst_request_category,
a1893da1
BP
618 oh->version, ntohs(request->type),
619 typep);
28c8bad1 620 if (!error && request->type == htons(OFPST_VENDOR)) {
5a020ef3 621 error = ofputil_decode_nxst_request(oh, length, typep);
d1e2cf21
BP
622 }
623 return error;
624}
625
90bf1e07 626static enum ofperr
5a020ef3 627ofputil_decode_ofpst_reply(const struct ofp_header *oh, size_t length,
d1e2cf21
BP
628 const struct ofputil_msg_type **typep)
629{
d1e2cf21 630 static const struct ofputil_msg_type ofpst_replies[] = {
a1893da1 631 { OFPUTIL_OFPST_DESC_REPLY, OFP10_VERSION,
d1e2cf21 632 OFPST_DESC, "OFPST_DESC reply",
63f2140a 633 sizeof(struct ofp_desc_stats), 0 },
d1e2cf21 634
a1893da1 635 { OFPUTIL_OFPST_FLOW_REPLY, OFP10_VERSION,
d1e2cf21 636 OFPST_FLOW, "OFPST_FLOW reply",
63f2140a 637 sizeof(struct ofp_stats_msg), 1 },
d1e2cf21 638
a1893da1 639 { OFPUTIL_OFPST_AGGREGATE_REPLY, OFP10_VERSION,
d1e2cf21 640 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
63f2140a 641 sizeof(struct ofp_aggregate_stats_reply), 0 },
d1e2cf21 642
a1893da1 643 { OFPUTIL_OFPST_TABLE_REPLY, OFP10_VERSION,
d1e2cf21 644 OFPST_TABLE, "OFPST_TABLE reply",
63f2140a 645 sizeof(struct ofp_stats_msg), sizeof(struct ofp_table_stats) },
d1e2cf21 646
a1893da1 647 { OFPUTIL_OFPST_PORT_REPLY, OFP10_VERSION,
d1e2cf21 648 OFPST_PORT, "OFPST_PORT reply",
63f2140a 649 sizeof(struct ofp_stats_msg), sizeof(struct ofp_port_stats) },
d1e2cf21 650
a1893da1 651 { OFPUTIL_OFPST_QUEUE_REPLY, OFP10_VERSION,
d1e2cf21 652 OFPST_QUEUE, "OFPST_QUEUE reply",
63f2140a 653 sizeof(struct ofp_stats_msg), sizeof(struct ofp_queue_stats) },
d1e2cf21 654
2be393ed
JP
655 { OFPUTIL_OFPST_PORT_DESC_REPLY, OFP10_VERSION,
656 OFPST_PORT_DESC, "OFPST_PORT_DESC reply",
657 sizeof(struct ofp_stats_msg), sizeof(struct ofp10_phy_port) },
658
a1893da1 659 { 0, 0,
d1e2cf21 660 OFPST_VENDOR, "OFPST_VENDOR reply",
63f2140a 661 sizeof(struct ofp_vendor_stats_msg), 1 },
d1e2cf21
BP
662 };
663
664 static const struct ofputil_msg_category ofpst_reply_category = {
665 "OpenFlow statistics",
666 ofpst_replies, ARRAY_SIZE(ofpst_replies),
90bf1e07 667 OFPERR_OFPBRC_BAD_STAT
d1e2cf21
BP
668 };
669
28c8bad1 670 const struct ofp_stats_msg *reply = (const struct ofp_stats_msg *) oh;
90bf1e07 671 enum ofperr error;
d1e2cf21 672
5a020ef3
BP
673 error = check_stats_msg(oh, length);
674 if (error) {
675 return error;
676 }
677
a1893da1 678 error = ofputil_lookup_openflow_message(&ofpst_reply_category, oh->version,
5a020ef3 679 ntohs(reply->type), typep);
28c8bad1 680 if (!error && reply->type == htons(OFPST_VENDOR)) {
5a020ef3 681 error = ofputil_decode_nxst_reply(oh, length, typep);
d1e2cf21
BP
682 }
683 return error;
684}
685
90bf1e07 686static enum ofperr
5a020ef3
BP
687ofputil_decode_msg_type__(const struct ofp_header *oh, size_t length,
688 const struct ofputil_msg_type **typep)
d1e2cf21
BP
689{
690 static const struct ofputil_msg_type ofpt_messages[] = {
a1893da1 691 { OFPUTIL_OFPT_HELLO, OFP10_VERSION,
d1e2cf21
BP
692 OFPT_HELLO, "OFPT_HELLO",
693 sizeof(struct ofp_hello), 1 },
694
90bf1e07 695 { OFPUTIL_OFPT_ERROR, 0,
d1e2cf21
BP
696 OFPT_ERROR, "OFPT_ERROR",
697 sizeof(struct ofp_error_msg), 1 },
698
a1893da1 699 { OFPUTIL_OFPT_ECHO_REQUEST, OFP10_VERSION,
d1e2cf21
BP
700 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
701 sizeof(struct ofp_header), 1 },
702
a1893da1 703 { OFPUTIL_OFPT_ECHO_REPLY, OFP10_VERSION,
d1e2cf21
BP
704 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
705 sizeof(struct ofp_header), 1 },
706
a1893da1 707 { OFPUTIL_OFPT_FEATURES_REQUEST, OFP10_VERSION,
d1e2cf21
BP
708 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
709 sizeof(struct ofp_header), 0 },
710
a1893da1 711 { OFPUTIL_OFPT_FEATURES_REPLY, OFP10_VERSION,
d1e2cf21 712 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
9e1fd49b
BP
713 sizeof(struct ofp_switch_features), sizeof(struct ofp10_phy_port) },
714 { OFPUTIL_OFPT_FEATURES_REPLY, OFP11_VERSION,
715 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
716 sizeof(struct ofp_switch_features), sizeof(struct ofp11_port) },
d1e2cf21 717
a1893da1 718 { OFPUTIL_OFPT_GET_CONFIG_REQUEST, OFP10_VERSION,
d1e2cf21
BP
719 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
720 sizeof(struct ofp_header), 0 },
721
a1893da1 722 { OFPUTIL_OFPT_GET_CONFIG_REPLY, OFP10_VERSION,
d1e2cf21
BP
723 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
724 sizeof(struct ofp_switch_config), 0 },
725
a1893da1 726 { OFPUTIL_OFPT_SET_CONFIG, OFP10_VERSION,
d1e2cf21
BP
727 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
728 sizeof(struct ofp_switch_config), 0 },
729
a1893da1 730 { OFPUTIL_OFPT_PACKET_IN, OFP10_VERSION,
d1e2cf21
BP
731 OFPT_PACKET_IN, "OFPT_PACKET_IN",
732 offsetof(struct ofp_packet_in, data), 1 },
733
a1893da1 734 { OFPUTIL_OFPT_FLOW_REMOVED, OFP10_VERSION,
d1e2cf21
BP
735 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
736 sizeof(struct ofp_flow_removed), 0 },
737
a1893da1 738 { OFPUTIL_OFPT_PORT_STATUS, OFP10_VERSION,
d1e2cf21 739 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
9e1fd49b
BP
740 sizeof(struct ofp_port_status) + sizeof(struct ofp10_phy_port), 0 },
741 { OFPUTIL_OFPT_PORT_STATUS, OFP11_VERSION,
742 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
743 sizeof(struct ofp_port_status) + sizeof(struct ofp11_port), 0 },
d1e2cf21 744
a1893da1 745 { OFPUTIL_OFPT_PACKET_OUT, OFP10_VERSION,
5293a2e1 746 OFPT10_PACKET_OUT, "OFPT_PACKET_OUT",
d1e2cf21
BP
747 sizeof(struct ofp_packet_out), 1 },
748
a1893da1 749 { OFPUTIL_OFPT_FLOW_MOD, OFP10_VERSION,
5293a2e1 750 OFPT10_FLOW_MOD, "OFPT_FLOW_MOD",
d1e2cf21
BP
751 sizeof(struct ofp_flow_mod), 1 },
752
a1893da1 753 { OFPUTIL_OFPT_PORT_MOD, OFP10_VERSION,
5293a2e1 754 OFPT10_PORT_MOD, "OFPT_PORT_MOD",
9e1fd49b
BP
755 sizeof(struct ofp10_port_mod), 0 },
756 { OFPUTIL_OFPT_PORT_MOD, OFP11_VERSION,
757 OFPT11_PORT_MOD, "OFPT_PORT_MOD",
758 sizeof(struct ofp11_port_mod), 0 },
d1e2cf21 759
a1893da1 760 { 0, OFP10_VERSION,
5293a2e1 761 OFPT10_STATS_REQUEST, "OFPT_STATS_REQUEST",
28c8bad1 762 sizeof(struct ofp_stats_msg), 1 },
d1e2cf21 763
a1893da1 764 { 0, OFP10_VERSION,
5293a2e1 765 OFPT10_STATS_REPLY, "OFPT_STATS_REPLY",
28c8bad1 766 sizeof(struct ofp_stats_msg), 1 },
d1e2cf21 767
a1893da1 768 { OFPUTIL_OFPT_BARRIER_REQUEST, OFP10_VERSION,
5293a2e1 769 OFPT10_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
d1e2cf21
BP
770 sizeof(struct ofp_header), 0 },
771
a1893da1 772 { OFPUTIL_OFPT_BARRIER_REPLY, OFP10_VERSION,
5293a2e1 773 OFPT10_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
d1e2cf21
BP
774 sizeof(struct ofp_header), 0 },
775
a1893da1 776 { 0, 0,
d1e2cf21
BP
777 OFPT_VENDOR, "OFPT_VENDOR",
778 sizeof(struct ofp_vendor_header), 1 },
779 };
780
781 static const struct ofputil_msg_category ofpt_category = {
782 "OpenFlow message",
783 ofpt_messages, ARRAY_SIZE(ofpt_messages),
90bf1e07 784 OFPERR_OFPBRC_BAD_TYPE
d1e2cf21
BP
785 };
786
90bf1e07 787 enum ofperr error;
d1e2cf21 788
a1893da1
BP
789 error = ofputil_lookup_openflow_message(&ofpt_category, oh->version,
790 oh->type, typep);
d1e2cf21 791 if (!error) {
7887679b
BP
792 switch ((oh->version << 8) | oh->type) {
793 case (OFP10_VERSION << 8) | OFPT_VENDOR:
794 case (OFP11_VERSION << 8) | OFPT_VENDOR:
5a020ef3 795 error = ofputil_decode_vendor(oh, length, typep);
d1e2cf21
BP
796 break;
797
7887679b
BP
798 case (OFP10_VERSION << 8) | OFPT10_STATS_REQUEST:
799 case (OFP11_VERSION << 8) | OFPT11_STATS_REQUEST:
5a020ef3 800 error = ofputil_decode_ofpst_request(oh, length, typep);
d1e2cf21
BP
801 break;
802
7887679b
BP
803 case (OFP10_VERSION << 8) | OFPT10_STATS_REPLY:
804 case (OFP11_VERSION << 8) | OFPT11_STATS_REPLY:
5a020ef3 805 error = ofputil_decode_ofpst_reply(oh, length, typep);
d1e2cf21
BP
806
807 default:
808 break;
809 }
810 }
5a020ef3
BP
811 return error;
812}
813
90bf1e07
BP
814/* Decodes the message type represented by 'oh'. Returns 0 if successful or an
815 * OpenFlow error code on failure. Either way, stores in '*typep' a type
816 * structure that can be inspected with the ofputil_msg_type_*() functions.
5a020ef3
BP
817 *
818 * oh->length must indicate the correct length of the message (and must be at
819 * least sizeof(struct ofp_header)).
820 *
821 * Success indicates that 'oh' is at least as long as the minimum-length
822 * message of its type. */
90bf1e07 823enum ofperr
5a020ef3
BP
824ofputil_decode_msg_type(const struct ofp_header *oh,
825 const struct ofputil_msg_type **typep)
826{
827 size_t length = ntohs(oh->length);
90bf1e07 828 enum ofperr error;
5a020ef3
BP
829
830 error = ofputil_decode_msg_type__(oh, length, typep);
831 if (!error) {
832 error = ofputil_check_length(*typep, length);
833 }
d1e2cf21 834 if (error) {
5a020ef3
BP
835 *typep = &ofputil_invalid_type;
836 }
837 return error;
838}
d1e2cf21 839
5a020ef3
BP
840/* Decodes the message type represented by 'oh', of which only the first
841 * 'length' bytes are available. Returns 0 if successful or an OpenFlow error
90bf1e07
BP
842 * code on failure. Either way, stores in '*typep' a type structure that can
843 * be inspected with the ofputil_msg_type_*() functions. */
844enum ofperr
5a020ef3
BP
845ofputil_decode_msg_type_partial(const struct ofp_header *oh, size_t length,
846 const struct ofputil_msg_type **typep)
847{
90bf1e07 848 enum ofperr error;
5a020ef3
BP
849
850 error = (length >= sizeof *oh
851 ? ofputil_decode_msg_type__(oh, length, typep)
90bf1e07 852 : OFPERR_OFPBRC_BAD_LEN);
5a020ef3 853 if (error) {
d1e2cf21
BP
854 *typep = &ofputil_invalid_type;
855 }
856 return error;
857}
858
859/* Returns an OFPUTIL_* message type code for 'type'. */
860enum ofputil_msg_code
861ofputil_msg_type_code(const struct ofputil_msg_type *type)
862{
863 return type->code;
864}
2e4f5fcf 865\f
27527aa0 866/* Protocols. */
7fa91113 867
27527aa0
BP
868struct proto_abbrev {
869 enum ofputil_protocol protocol;
870 const char *name;
871};
872
873/* Most users really don't care about some of the differences between
874 * protocols. These abbreviations help with that. */
875static const struct proto_abbrev proto_abbrevs[] = {
876 { OFPUTIL_P_ANY, "any" },
877 { OFPUTIL_P_OF10_ANY, "OpenFlow10" },
878 { OFPUTIL_P_NXM_ANY, "NXM" },
879};
880#define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
881
882enum ofputil_protocol ofputil_flow_dump_protocols[] = {
883 OFPUTIL_P_NXM,
884 OFPUTIL_P_OF10,
885};
886size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
887
888/* Returns the ofputil_protocol that is initially in effect on an OpenFlow
889 * connection that has negotiated the given 'version'. 'version' should
890 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
891 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
892 * outside the valid range. */
893enum ofputil_protocol
894ofputil_protocol_from_ofp_version(int version)
895{
896 switch (version) {
87ea5e5e 897 case OFP10_VERSION: return OFPUTIL_P_OF10;
27527aa0
BP
898 default: return 0;
899 }
900}
901
9e1fd49b
BP
902/* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION or
903 * OFP11_VERSION) that corresponds to 'protocol'. */
904uint8_t
905ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
906{
907 switch (protocol) {
908 case OFPUTIL_P_OF10:
909 case OFPUTIL_P_OF10_TID:
910 case OFPUTIL_P_NXM:
911 case OFPUTIL_P_NXM_TID:
912 return OFP10_VERSION;
913 }
914
915 NOT_REACHED();
916}
917
27527aa0
BP
918/* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
919 * otherwise. */
7fa91113 920bool
27527aa0 921ofputil_protocol_is_valid(enum ofputil_protocol protocol)
7fa91113 922{
27527aa0
BP
923 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
924}
925
926/* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
927 * extension turned on or off if 'enable' is true or false, respectively.
928 *
929 * This extension is only useful for protocols whose "standard" version does
930 * not allow specific tables to be modified. In particular, this is true of
931 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
932 * specifies a table ID and so there is no need for such an extension. When
933 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
934 * extension, this function just returns its 'protocol' argument unchanged
935 * regardless of the value of 'enable'. */
936enum ofputil_protocol
937ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
938{
939 switch (protocol) {
940 case OFPUTIL_P_OF10:
941 case OFPUTIL_P_OF10_TID:
942 return enable ? OFPUTIL_P_OF10_TID : OFPUTIL_P_OF10;
943
944 case OFPUTIL_P_NXM:
945 case OFPUTIL_P_NXM_TID:
946 return enable ? OFPUTIL_P_NXM_TID : OFPUTIL_P_NXM;
947
948 default:
949 NOT_REACHED();
7fa91113 950 }
27527aa0 951}
7fa91113 952
27527aa0
BP
953/* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
954 * some extension to a standard protocol version, the return value is the
955 * standard version of that protocol without any extension. If 'protocol' is a
956 * standard protocol version, returns 'protocol' unchanged. */
957enum ofputil_protocol
958ofputil_protocol_to_base(enum ofputil_protocol protocol)
959{
960 return ofputil_protocol_set_tid(protocol, false);
7fa91113
BP
961}
962
27527aa0
BP
963/* Returns 'new_base' with any extensions taken from 'cur'. */
964enum ofputil_protocol
965ofputil_protocol_set_base(enum ofputil_protocol cur,
966 enum ofputil_protocol new_base)
7fa91113 967{
27527aa0
BP
968 bool tid = (cur & OFPUTIL_P_TID) != 0;
969
970 switch (new_base) {
971 case OFPUTIL_P_OF10:
972 case OFPUTIL_P_OF10_TID:
973 return ofputil_protocol_set_tid(OFPUTIL_P_OF10, tid);
974
975 case OFPUTIL_P_NXM:
976 case OFPUTIL_P_NXM_TID:
977 return ofputil_protocol_set_tid(OFPUTIL_P_NXM, tid);
978
7fa91113
BP
979 default:
980 NOT_REACHED();
981 }
982}
983
27527aa0
BP
984/* Returns a string form of 'protocol', if a simple form exists (that is, if
985 * 'protocol' is either a single protocol or it is a combination of protocols
986 * that have a single abbreviation). Otherwise, returns NULL. */
987const char *
988ofputil_protocol_to_string(enum ofputil_protocol protocol)
88ca35ee 989{
27527aa0
BP
990 const struct proto_abbrev *p;
991
992 /* Use a "switch" statement for single-bit names so that we get a compiler
993 * warning if we forget any. */
994 switch (protocol) {
995 case OFPUTIL_P_NXM:
996 return "NXM-table_id";
997
998 case OFPUTIL_P_NXM_TID:
999 return "NXM+table_id";
1000
1001 case OFPUTIL_P_OF10:
1002 return "OpenFlow10-table_id";
1003
1004 case OFPUTIL_P_OF10_TID:
1005 return "OpenFlow10+table_id";
1006 }
1007
1008 /* Check abbreviations. */
1009 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1010 if (protocol == p->protocol) {
1011 return p->name;
1012 }
1013 }
1014
1015 return NULL;
1016}
1017
1018/* Returns a string that represents 'protocols'. The return value might be a
1019 * comma-separated list if 'protocols' doesn't have a simple name. The return
1020 * value is "none" if 'protocols' is 0.
1021 *
1022 * The caller must free the returned string (with free()). */
1023char *
1024ofputil_protocols_to_string(enum ofputil_protocol protocols)
1025{
1026 struct ds s;
1027
1028 assert(!(protocols & ~OFPUTIL_P_ANY));
1029 if (protocols == 0) {
1030 return xstrdup("none");
1031 }
1032
1033 ds_init(&s);
1034 while (protocols) {
1035 const struct proto_abbrev *p;
1036 int i;
1037
1038 if (s.length) {
1039 ds_put_char(&s, ',');
1040 }
1041
1042 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1043 if ((protocols & p->protocol) == p->protocol) {
1044 ds_put_cstr(&s, p->name);
1045 protocols &= ~p->protocol;
1046 goto match;
1047 }
1048 }
1049
1050 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1051 enum ofputil_protocol bit = 1u << i;
1052
1053 if (protocols & bit) {
1054 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
1055 protocols &= ~bit;
1056 goto match;
1057 }
1058 }
1059 NOT_REACHED();
1060
1061 match: ;
1062 }
1063 return ds_steal_cstr(&s);
1064}
1065
1066static enum ofputil_protocol
1067ofputil_protocol_from_string__(const char *s, size_t n)
1068{
1069 const struct proto_abbrev *p;
1070 int i;
1071
1072 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1073 enum ofputil_protocol bit = 1u << i;
1074 const char *name = ofputil_protocol_to_string(bit);
1075
1076 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
1077 return bit;
1078 }
1079 }
1080
1081 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1082 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
1083 return p->protocol;
1084 }
1085 }
1086
1087 return 0;
1088}
1089
1090/* Returns the nonempty set of protocols represented by 's', which can be a
1091 * single protocol name or abbreviation or a comma-separated list of them.
1092 *
1093 * Aborts the program with an error message if 's' is invalid. */
1094enum ofputil_protocol
1095ofputil_protocols_from_string(const char *s)
1096{
1097 const char *orig_s = s;
1098 enum ofputil_protocol protocols;
1099
1100 protocols = 0;
1101 while (*s) {
1102 enum ofputil_protocol p;
1103 size_t n;
1104
1105 n = strcspn(s, ",");
1106 if (n == 0) {
1107 s++;
1108 continue;
1109 }
1110
1111 p = ofputil_protocol_from_string__(s, n);
1112 if (!p) {
1113 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
1114 }
1115 protocols |= p;
1116
1117 s += n;
1118 }
1119
1120 if (!protocols) {
1121 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
1122 }
1123 return protocols;
88ca35ee
BP
1124}
1125
54834960
EJ
1126bool
1127ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1128{
1129 switch (packet_in_format) {
1130 case NXPIF_OPENFLOW10:
1131 case NXPIF_NXM:
1132 return true;
1133 }
1134
1135 return false;
1136}
1137
1138const char *
1139ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1140{
1141 switch (packet_in_format) {
1142 case NXPIF_OPENFLOW10:
1143 return "openflow10";
1144 case NXPIF_NXM:
1145 return "nxm";
1146 default:
1147 NOT_REACHED();
1148 }
1149}
1150
1151int
1152ofputil_packet_in_format_from_string(const char *s)
1153{
1154 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
1155 : !strcmp(s, "nxm") ? NXPIF_NXM
1156 : -1);
1157}
1158
88ca35ee
BP
1159static bool
1160regs_fully_wildcarded(const struct flow_wildcards *wc)
1161{
1162 int i;
1163
1164 for (i = 0; i < FLOW_N_REGS; i++) {
1165 if (wc->reg_masks[i] != 0) {
1166 return false;
1167 }
1168 }
1169 return true;
1170}
1171
27527aa0
BP
1172/* Returns a bit-mask of ofputil_protocols that can be used for sending 'rule'
1173 * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs,
1174 * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to
1175 * use OpenFlow 1.0 protocol for backward compatibility. */
1176enum ofputil_protocol
1177ofputil_usable_protocols(const struct cls_rule *rule)
8368c090
BP
1178{
1179 const struct flow_wildcards *wc = &rule->wc;
8368c090 1180
73c0ce34 1181 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 11);
a877206f 1182
73c0ce34
JS
1183 /* NXM and OF1.1+ supports bitwise matching on ethernet addresses. */
1184 if (!eth_mask_is_exact(wc->dl_src_mask)
1185 && !eth_addr_is_zero(wc->dl_src_mask)) {
1186 return OFPUTIL_P_NXM_ANY;
1187 }
1188 if (!eth_mask_is_exact(wc->dl_dst_mask)
1189 && !eth_addr_is_zero(wc->dl_dst_mask)) {
27527aa0 1190 return OFPUTIL_P_NXM_ANY;
8368c090
BP
1191 }
1192
bad68a99
JP
1193 /* Only NXM supports matching ARP hardware addresses. */
1194 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
27527aa0 1195 return OFPUTIL_P_NXM_ANY;
bad68a99
JP
1196 }
1197
d31f1109
JP
1198 /* Only NXM supports matching IPv6 traffic. */
1199 if (!(wc->wildcards & FWW_DL_TYPE)
1200 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
27527aa0 1201 return OFPUTIL_P_NXM_ANY;
d31f1109
JP
1202 }
1203
8368c090
BP
1204 /* Only NXM supports matching registers. */
1205 if (!regs_fully_wildcarded(wc)) {
27527aa0 1206 return OFPUTIL_P_NXM_ANY;
8368c090
BP
1207 }
1208
b78f6b77
BP
1209 /* Only NXM supports matching tun_id. */
1210 if (wc->tun_id_mask != htonll(0)) {
27527aa0 1211 return OFPUTIL_P_NXM_ANY;
8368c090
BP
1212 }
1213
7257b535 1214 /* Only NXM supports matching fragments. */
eadef313 1215 if (wc->nw_frag_mask) {
27527aa0 1216 return OFPUTIL_P_NXM_ANY;
7257b535
BP
1217 }
1218
fa8223b7
JP
1219 /* Only NXM supports matching IPv6 flow label. */
1220 if (!(wc->wildcards & FWW_IPV6_LABEL)) {
27527aa0 1221 return OFPUTIL_P_NXM_ANY;
fa8223b7
JP
1222 }
1223
530180fd 1224 /* Only NXM supports matching IP ECN bits. */
2486e66a 1225 if (!(wc->wildcards & FWW_NW_ECN)) {
27527aa0 1226 return OFPUTIL_P_NXM_ANY;
530180fd
JP
1227 }
1228
a61680c6
JP
1229 /* Only NXM supports matching IP TTL/hop limit. */
1230 if (!(wc->wildcards & FWW_NW_TTL)) {
27527aa0 1231 return OFPUTIL_P_NXM_ANY;
a61680c6
JP
1232 }
1233
73f33563
BP
1234 /* Only NXM supports bitwise matching on transport port. */
1235 if ((wc->tp_src_mask && wc->tp_src_mask != htons(UINT16_MAX)) ||
1236 (wc->tp_dst_mask && wc->tp_dst_mask != htons(UINT16_MAX))) {
27527aa0 1237 return OFPUTIL_P_NXM_ANY;
73f33563
BP
1238 }
1239
8368c090 1240 /* Other formats can express this rule. */
27527aa0
BP
1241 return OFPUTIL_P_ANY;
1242}
1243
1244/* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1245 * protocol is 'current', at least partly transitions the protocol to 'want'.
1246 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1247 * connection if the switch processes the returned message correctly. (If
1248 * '*next != want' then the caller will have to iterate.)
1249 *
1250 * If 'current == want', returns NULL and stores 'current' in '*next'. */
1251struct ofpbuf *
1252ofputil_encode_set_protocol(enum ofputil_protocol current,
1253 enum ofputil_protocol want,
1254 enum ofputil_protocol *next)
1255{
1256 enum ofputil_protocol cur_base, want_base;
1257 bool cur_tid, want_tid;
1258
1259 cur_base = ofputil_protocol_to_base(current);
1260 want_base = ofputil_protocol_to_base(want);
1261 if (cur_base != want_base) {
1262 *next = ofputil_protocol_set_base(current, want_base);
1263
1264 switch (want_base) {
1265 case OFPUTIL_P_NXM:
1266 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1267
1268 case OFPUTIL_P_OF10:
1269 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1270
1271 case OFPUTIL_P_OF10_TID:
1272 case OFPUTIL_P_NXM_TID:
1273 NOT_REACHED();
1274 }
1275 }
1276
1277 cur_tid = (current & OFPUTIL_P_TID) != 0;
1278 want_tid = (want & OFPUTIL_P_TID) != 0;
1279 if (cur_tid != want_tid) {
1280 *next = ofputil_protocol_set_tid(current, want_tid);
1281 return ofputil_make_flow_mod_table_id(want_tid);
1282 }
1283
1284 assert(current == want);
1285
1286 *next = current;
1287 return NULL;
88ca35ee
BP
1288}
1289
27527aa0
BP
1290/* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1291 * format to 'nxff'. */
88ca35ee 1292struct ofpbuf *
27527aa0 1293ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
88ca35ee 1294{
73dbf4ab 1295 struct nx_set_flow_format *sff;
88ca35ee
BP
1296 struct ofpbuf *msg;
1297
27527aa0
BP
1298 assert(ofputil_nx_flow_format_is_valid(nxff));
1299
b78f6b77 1300 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
27527aa0 1301 sff->format = htonl(nxff);
88ca35ee
BP
1302
1303 return msg;
1304}
1305
27527aa0
BP
1306/* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1307 * otherwise. */
1308enum ofputil_protocol
1309ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1310{
1311 switch (flow_format) {
1312 case NXFF_OPENFLOW10:
1313 return OFPUTIL_P_OF10;
1314
1315 case NXFF_NXM:
1316 return OFPUTIL_P_NXM;
1317
1318 default:
1319 return 0;
1320 }
1321}
1322
1323/* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1324bool
1325ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1326{
1327 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1328}
1329
1330/* Returns a string version of 'flow_format', which must be a valid NXFF_*
1331 * value. */
1332const char *
1333ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1334{
1335 switch (flow_format) {
1336 case NXFF_OPENFLOW10:
1337 return "openflow10";
1338 case NXFF_NXM:
1339 return "nxm";
1340 default:
1341 NOT_REACHED();
1342 }
1343}
1344
54834960
EJ
1345struct ofpbuf *
1346ofputil_make_set_packet_in_format(enum nx_packet_in_format packet_in_format)
1347{
73dbf4ab 1348 struct nx_set_packet_in_format *spif;
54834960
EJ
1349 struct ofpbuf *msg;
1350
1351 spif = make_nxmsg(sizeof *spif, NXT_SET_PACKET_IN_FORMAT, &msg);
1352 spif->format = htonl(packet_in_format);
1353
1354 return msg;
1355}
1356
6c1491fb
BP
1357/* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1358 * extension on or off (according to 'flow_mod_table_id'). */
1359struct ofpbuf *
1360ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1361{
73dbf4ab 1362 struct nx_flow_mod_table_id *nfmti;
6c1491fb
BP
1363 struct ofpbuf *msg;
1364
1365 nfmti = make_nxmsg(sizeof *nfmti, NXT_FLOW_MOD_TABLE_ID, &msg);
1366 nfmti->set = flow_mod_table_id;
1367 return msg;
1368}
1369
7fa91113
BP
1370/* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1371 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1372 * code.
1373 *
2e4f5fcf 1374 * Does not validate the flow_mod actions. */
90bf1e07 1375enum ofperr
a9a2da38 1376ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
27527aa0
BP
1377 const struct ofp_header *oh,
1378 enum ofputil_protocol protocol)
2e4f5fcf
BP
1379{
1380 const struct ofputil_msg_type *type;
6c1491fb 1381 uint16_t command;
2e4f5fcf
BP
1382 struct ofpbuf b;
1383
2013493b 1384 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2e4f5fcf
BP
1385
1386 ofputil_decode_msg_type(oh, &type);
1387 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
1388 /* Standard OpenFlow flow_mod. */
2e4f5fcf 1389 const struct ofp_flow_mod *ofm;
1c0b7503 1390 uint16_t priority;
90bf1e07 1391 enum ofperr error;
2e4f5fcf
BP
1392
1393 /* Dissect the message. */
bbc32a88 1394 ofm = ofpbuf_pull(&b, sizeof *ofm);
2e4f5fcf
BP
1395 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
1396 if (error) {
1397 return error;
1398 }
1399
1c0b7503
BP
1400 /* Set priority based on original wildcards. Normally we'd allow
1401 * ofputil_cls_rule_from_match() to do this for us, but
b459a924 1402 * ofputil_normalize_rule() can put wildcards where the original flow
1c0b7503
BP
1403 * didn't have them. */
1404 priority = ntohs(ofm->priority);
1405 if (!(ofm->match.wildcards & htonl(OFPFW_ALL))) {
1406 priority = UINT16_MAX;
1407 }
1408
b459a924
BP
1409 /* Translate the rule. */
1410 ofputil_cls_rule_from_match(&ofm->match, priority, &fm->cr);
27527aa0 1411 ofputil_normalize_rule(&fm->cr);
2e4f5fcf
BP
1412
1413 /* Translate the message. */
6c1491fb 1414 command = ntohs(ofm->command);
623e1caf
JP
1415 fm->cookie = htonll(0);
1416 fm->cookie_mask = htonll(0);
1417 fm->new_cookie = ofm->cookie;
2e4f5fcf
BP
1418 fm->idle_timeout = ntohs(ofm->idle_timeout);
1419 fm->hard_timeout = ntohs(ofm->hard_timeout);
1420 fm->buffer_id = ntohl(ofm->buffer_id);
1421 fm->out_port = ntohs(ofm->out_port);
1422 fm->flags = ntohs(ofm->flags);
1423 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
1424 /* Nicira extended flow_mod. */
1425 const struct nx_flow_mod *nfm;
90bf1e07 1426 enum ofperr error;
2e4f5fcf
BP
1427
1428 /* Dissect the message. */
bbc32a88 1429 nfm = ofpbuf_pull(&b, sizeof *nfm);
2e4f5fcf 1430 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
e729e793 1431 &fm->cr, &fm->cookie, &fm->cookie_mask);
2e4f5fcf
BP
1432 if (error) {
1433 return error;
1434 }
1435 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
1436 if (error) {
1437 return error;
1438 }
1439
1440 /* Translate the message. */
6c1491fb 1441 command = ntohs(nfm->command);
623e1caf
JP
1442 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1443 /* Flow additions may only set a new cookie, not match an
1444 * existing cookie. */
1445 return OFPERR_NXBRC_NXM_INVALID;
e729e793 1446 }
623e1caf 1447 fm->new_cookie = nfm->cookie;
2e4f5fcf
BP
1448 fm->idle_timeout = ntohs(nfm->idle_timeout);
1449 fm->hard_timeout = ntohs(nfm->hard_timeout);
1450 fm->buffer_id = ntohl(nfm->buffer_id);
1451 fm->out_port = ntohs(nfm->out_port);
1452 fm->flags = ntohs(nfm->flags);
1453 } else {
1454 NOT_REACHED();
1455 }
1456
27527aa0 1457 if (protocol & OFPUTIL_P_TID) {
6c1491fb
BP
1458 fm->command = command & 0xff;
1459 fm->table_id = command >> 8;
1460 } else {
1461 fm->command = command;
1462 fm->table_id = 0xff;
1463 }
1464
2e4f5fcf
BP
1465 return 0;
1466}
1467
1468/* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
6cbe2c0f 1469 * 'protocol' and returns the message. */
2e4f5fcf 1470struct ofpbuf *
a9a2da38 1471ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
27527aa0 1472 enum ofputil_protocol protocol)
2e4f5fcf
BP
1473{
1474 size_t actions_len = fm->n_actions * sizeof *fm->actions;
27527aa0
BP
1475 struct ofp_flow_mod *ofm;
1476 struct nx_flow_mod *nfm;
2e4f5fcf 1477 struct ofpbuf *msg;
6c1491fb 1478 uint16_t command;
27527aa0 1479 int match_len;
6c1491fb 1480
27527aa0 1481 command = (protocol & OFPUTIL_P_TID
6c1491fb
BP
1482 ? (fm->command & 0xff) | (fm->table_id << 8)
1483 : fm->command);
2e4f5fcf 1484
27527aa0
BP
1485 switch (protocol) {
1486 case OFPUTIL_P_OF10:
1487 case OFPUTIL_P_OF10_TID:
2e4f5fcf 1488 msg = ofpbuf_new(sizeof *ofm + actions_len);
5293a2e1 1489 ofm = put_openflow(sizeof *ofm, OFPT10_FLOW_MOD, msg);
b78f6b77 1490 ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
623e1caf 1491 ofm->cookie = fm->new_cookie;
05411977 1492 ofm->command = htons(command);
2e4f5fcf
BP
1493 ofm->idle_timeout = htons(fm->idle_timeout);
1494 ofm->hard_timeout = htons(fm->hard_timeout);
1495 ofm->priority = htons(fm->cr.priority);
1496 ofm->buffer_id = htonl(fm->buffer_id);
1497 ofm->out_port = htons(fm->out_port);
1498 ofm->flags = htons(fm->flags);
27527aa0 1499 break;
2e4f5fcf 1500
27527aa0
BP
1501 case OFPUTIL_P_NXM:
1502 case OFPUTIL_P_NXM_TID:
2e4f5fcf
BP
1503 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
1504 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
2e4f5fcf 1505 nfm = msg->data;
6c1491fb 1506 nfm->command = htons(command);
623e1caf
JP
1507 nfm->cookie = fm->new_cookie;
1508 match_len = nx_put_match(msg, &fm->cr, fm->cookie, fm->cookie_mask);
2e4f5fcf
BP
1509 nfm->idle_timeout = htons(fm->idle_timeout);
1510 nfm->hard_timeout = htons(fm->hard_timeout);
1511 nfm->priority = htons(fm->cr.priority);
1512 nfm->buffer_id = htonl(fm->buffer_id);
1513 nfm->out_port = htons(fm->out_port);
1514 nfm->flags = htons(fm->flags);
1515 nfm->match_len = htons(match_len);
27527aa0
BP
1516 break;
1517
1518 default:
2e4f5fcf
BP
1519 NOT_REACHED();
1520 }
1521
1522 ofpbuf_put(msg, fm->actions, actions_len);
1523 update_openflow_length(msg);
1524 return msg;
1525}
1526
27527aa0
BP
1527/* Returns a bitmask with a 1-bit for each protocol that could be used to
1528 * send all of the 'n_fm's flow table modification requests in 'fms', and a
1529 * 0-bit for each protocol that is inadequate.
1530 *
1531 * (The return value will have at least one 1-bit.) */
1532enum ofputil_protocol
1533ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms,
1534 size_t n_fms)
1535{
1536 enum ofputil_protocol usable_protocols;
1537 size_t i;
1538
1539 usable_protocols = OFPUTIL_P_ANY;
1540 for (i = 0; i < n_fms; i++) {
1541 const struct ofputil_flow_mod *fm = &fms[i];
1542
1543 usable_protocols &= ofputil_usable_protocols(&fm->cr);
1544 if (fm->table_id != 0xff) {
1545 usable_protocols &= OFPUTIL_P_TID;
1546 }
623e1caf
JP
1547
1548 /* Matching of the cookie is only supported through NXM. */
1549 if (fm->cookie_mask != htonll(0)) {
27527aa0
BP
1550 usable_protocols &= OFPUTIL_P_NXM_ANY;
1551 }
1552 }
1553 assert(usable_protocols);
1554
1555 return usable_protocols;
1556}
1557
90bf1e07 1558static enum ofperr
81d1ea94 1559ofputil_decode_ofpst_flow_request(struct ofputil_flow_stats_request *fsr,
2e4f5fcf 1560 const struct ofp_header *oh,
2e4f5fcf
BP
1561 bool aggregate)
1562{
63f2140a
BP
1563 const struct ofp_flow_stats_request *ofsr =
1564 (const struct ofp_flow_stats_request *) oh;
2e4f5fcf
BP
1565
1566 fsr->aggregate = aggregate;
b78f6b77 1567 ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
2e4f5fcf
BP
1568 fsr->out_port = ntohs(ofsr->out_port);
1569 fsr->table_id = ofsr->table_id;
e729e793 1570 fsr->cookie = fsr->cookie_mask = htonll(0);
2e4f5fcf
BP
1571
1572 return 0;
1573}
1574
90bf1e07 1575static enum ofperr
81d1ea94 1576ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
2e4f5fcf
BP
1577 const struct ofp_header *oh,
1578 bool aggregate)
1579{
1580 const struct nx_flow_stats_request *nfsr;
1581 struct ofpbuf b;
90bf1e07 1582 enum ofperr error;
2e4f5fcf 1583
2013493b 1584 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2e4f5fcf 1585
bbc32a88 1586 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
e729e793
JP
1587 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match,
1588 &fsr->cookie, &fsr->cookie_mask);
2e4f5fcf
BP
1589 if (error) {
1590 return error;
1591 }
1592 if (b.size) {
90bf1e07 1593 return OFPERR_OFPBRC_BAD_LEN;
2e4f5fcf
BP
1594 }
1595
1596 fsr->aggregate = aggregate;
1597 fsr->out_port = ntohs(nfsr->out_port);
1598 fsr->table_id = nfsr->table_id;
1599
1600 return 0;
1601}
1602
1603/* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
b78f6b77
BP
1604 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1605 * successful, otherwise an OpenFlow error code. */
90bf1e07 1606enum ofperr
81d1ea94 1607ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
b78f6b77 1608 const struct ofp_header *oh)
2e4f5fcf
BP
1609{
1610 const struct ofputil_msg_type *type;
1611 struct ofpbuf b;
1612 int code;
1613
2013493b 1614 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2e4f5fcf
BP
1615
1616 ofputil_decode_msg_type(oh, &type);
1617 code = ofputil_msg_type_code(type);
1618 switch (code) {
1619 case OFPUTIL_OFPST_FLOW_REQUEST:
b78f6b77 1620 return ofputil_decode_ofpst_flow_request(fsr, oh, false);
2e4f5fcf
BP
1621
1622 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
b78f6b77 1623 return ofputil_decode_ofpst_flow_request(fsr, oh, true);
2e4f5fcf
BP
1624
1625 case OFPUTIL_NXST_FLOW_REQUEST:
1626 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1627
1628 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1629 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1630
1631 default:
1632 /* Hey, the caller lied. */
1633 NOT_REACHED();
1634 }
1635}
1636
1637/* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
4ffd1b43 1638 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
27527aa0 1639 * 'protocol', and returns the message. */
2e4f5fcf 1640struct ofpbuf *
81d1ea94 1641ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
27527aa0 1642 enum ofputil_protocol protocol)
2e4f5fcf
BP
1643{
1644 struct ofpbuf *msg;
1645
27527aa0
BP
1646 switch (protocol) {
1647 case OFPUTIL_P_OF10:
1648 case OFPUTIL_P_OF10_TID: {
2e4f5fcf
BP
1649 struct ofp_flow_stats_request *ofsr;
1650 int type;
1651
2e4f5fcf 1652 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
63f2140a 1653 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, 0, &msg);
b78f6b77 1654 ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
2e4f5fcf
BP
1655 ofsr->table_id = fsr->table_id;
1656 ofsr->out_port = htons(fsr->out_port);
27527aa0
BP
1657 break;
1658 }
1659
1660 case OFPUTIL_P_NXM:
1661 case OFPUTIL_P_NXM_TID: {
2e4f5fcf
BP
1662 struct nx_flow_stats_request *nfsr;
1663 int match_len;
9fb7fa87 1664 int subtype;
2e4f5fcf 1665
9fb7fa87 1666 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
63f2140a 1667 ofputil_make_stats_request(sizeof *nfsr, OFPST_VENDOR, subtype, &msg);
e729e793
JP
1668 match_len = nx_put_match(msg, &fsr->match,
1669 fsr->cookie, fsr->cookie_mask);
2e4f5fcf
BP
1670
1671 nfsr = msg->data;
1672 nfsr->out_port = htons(fsr->out_port);
1673 nfsr->match_len = htons(match_len);
1674 nfsr->table_id = fsr->table_id;
27527aa0
BP
1675 break;
1676 }
1677
1678 default:
2e4f5fcf
BP
1679 NOT_REACHED();
1680 }
1681
1682 return msg;
1683}
d1e2cf21 1684
27527aa0
BP
1685/* Returns a bitmask with a 1-bit for each protocol that could be used to
1686 * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate.
1687 *
1688 * (The return value will have at least one 1-bit.) */
1689enum ofputil_protocol
1690ofputil_flow_stats_request_usable_protocols(
1691 const struct ofputil_flow_stats_request *fsr)
1692{
1693 enum ofputil_protocol usable_protocols;
1694
1695 usable_protocols = ofputil_usable_protocols(&fsr->match);
1696 if (fsr->cookie_mask != htonll(0)) {
1697 usable_protocols &= OFPUTIL_P_NXM_ANY;
1698 }
1699 return usable_protocols;
1700}
1701
4ffd1b43 1702/* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
b78f6b77 1703 * ofputil_flow_stats in 'fs'.
4ffd1b43
BP
1704 *
1705 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1706 * OpenFlow message. Calling this function multiple times for a single 'msg'
1707 * iterates through the replies. The caller must initially leave 'msg''s layer
1708 * pointers null and not modify them between calls.
1709 *
f27f2134
BP
1710 * Most switches don't send the values needed to populate fs->idle_age and
1711 * fs->hard_age, so those members will usually be set to 0. If the switch from
1712 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1713 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1714 * 'idle_age' and 'hard_age' members in 'fs'.
1715 *
4ffd1b43
BP
1716 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1717 * otherwise a positive errno value. */
1718int
1719ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
f27f2134
BP
1720 struct ofpbuf *msg,
1721 bool flow_age_extension)
4ffd1b43
BP
1722{
1723 const struct ofputil_msg_type *type;
1724 int code;
1725
1726 ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1727 code = ofputil_msg_type_code(type);
1728 if (!msg->l2) {
1729 msg->l2 = msg->data;
1730 if (code == OFPUTIL_OFPST_FLOW_REPLY) {
28c8bad1 1731 ofpbuf_pull(msg, sizeof(struct ofp_stats_msg));
4ffd1b43
BP
1732 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1733 ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1734 } else {
1735 NOT_REACHED();
1736 }
1737 }
1738
1739 if (!msg->size) {
1740 return EOF;
1741 } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1742 const struct ofp_flow_stats *ofs;
1743 size_t length;
1744
1745 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1746 if (!ofs) {
1747 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1748 "bytes at end", msg->size);
1749 return EINVAL;
1750 }
1751
1752 length = ntohs(ofs->length);
1753 if (length < sizeof *ofs) {
1754 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1755 "length %zu", length);
1756 return EINVAL;
1757 }
1758
1759 if (ofputil_pull_actions(msg, length - sizeof *ofs,
1760 &fs->actions, &fs->n_actions)) {
1761 return EINVAL;
1762 }
1763
1764 fs->cookie = get_32aligned_be64(&ofs->cookie);
1765 ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
b78f6b77 1766 &fs->rule);
4ffd1b43
BP
1767 fs->table_id = ofs->table_id;
1768 fs->duration_sec = ntohl(ofs->duration_sec);
1769 fs->duration_nsec = ntohl(ofs->duration_nsec);
1770 fs->idle_timeout = ntohs(ofs->idle_timeout);
1771 fs->hard_timeout = ntohs(ofs->hard_timeout);
f27f2134
BP
1772 fs->idle_age = -1;
1773 fs->hard_age = -1;
4ffd1b43
BP
1774 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1775 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1776 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1777 const struct nx_flow_stats *nfs;
1778 size_t match_len, length;
1779
1780 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1781 if (!nfs) {
1782 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1783 "bytes at end", msg->size);
1784 return EINVAL;
1785 }
1786
1787 length = ntohs(nfs->length);
1788 match_len = ntohs(nfs->match_len);
1789 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1790 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1791 "claims invalid length %zu", match_len, length);
1792 return EINVAL;
1793 }
e729e793
JP
1794 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule,
1795 NULL, NULL)) {
4ffd1b43
BP
1796 return EINVAL;
1797 }
1798
1799 if (ofputil_pull_actions(msg,
1800 length - sizeof *nfs - ROUND_UP(match_len, 8),
1801 &fs->actions, &fs->n_actions)) {
1802 return EINVAL;
1803 }
1804
1805 fs->cookie = nfs->cookie;
1806 fs->table_id = nfs->table_id;
1807 fs->duration_sec = ntohl(nfs->duration_sec);
1808 fs->duration_nsec = ntohl(nfs->duration_nsec);
1809 fs->idle_timeout = ntohs(nfs->idle_timeout);
1810 fs->hard_timeout = ntohs(nfs->hard_timeout);
f27f2134
BP
1811 fs->idle_age = -1;
1812 fs->hard_age = -1;
1813 if (flow_age_extension) {
1814 if (nfs->idle_age) {
1815 fs->idle_age = ntohs(nfs->idle_age) - 1;
1816 }
1817 if (nfs->hard_age) {
1818 fs->hard_age = ntohs(nfs->hard_age) - 1;
1819 }
1820 }
4ffd1b43
BP
1821 fs->packet_count = ntohll(nfs->packet_count);
1822 fs->byte_count = ntohll(nfs->byte_count);
1823 } else {
1824 NOT_REACHED();
1825 }
1826
1827 return 0;
1828}
1829
5e9d0469
BP
1830/* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1831 *
1832 * We use this in situations where OVS internally uses UINT64_MAX to mean
1833 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1834static uint64_t
1835unknown_to_zero(uint64_t count)
1836{
1837 return count != UINT64_MAX ? count : 0;
1838}
1839
349adfb2
BP
1840/* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1841 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1842 * have been initialized with ofputil_start_stats_reply(). */
1843void
1844ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1845 struct list *replies)
1846{
1847 size_t act_len = fs->n_actions * sizeof *fs->actions;
1848 const struct ofp_stats_msg *osm;
1849
1850 osm = ofpbuf_from_list(list_back(replies))->data;
1851 if (osm->type == htons(OFPST_FLOW)) {
1852 size_t len = offsetof(struct ofp_flow_stats, actions) + act_len;
1853 struct ofp_flow_stats *ofs;
1854
1855 ofs = ofputil_append_stats_reply(len, replies);
1856 ofs->length = htons(len);
1857 ofs->table_id = fs->table_id;
1858 ofs->pad = 0;
1859 ofputil_cls_rule_to_match(&fs->rule, &ofs->match);
1860 ofs->duration_sec = htonl(fs->duration_sec);
1861 ofs->duration_nsec = htonl(fs->duration_nsec);
1862 ofs->priority = htons(fs->rule.priority);
1863 ofs->idle_timeout = htons(fs->idle_timeout);
1864 ofs->hard_timeout = htons(fs->hard_timeout);
1865 memset(ofs->pad2, 0, sizeof ofs->pad2);
1866 put_32aligned_be64(&ofs->cookie, fs->cookie);
5e9d0469
BP
1867 put_32aligned_be64(&ofs->packet_count,
1868 htonll(unknown_to_zero(fs->packet_count)));
1869 put_32aligned_be64(&ofs->byte_count,
1870 htonll(unknown_to_zero(fs->byte_count)));
349adfb2
BP
1871 memcpy(ofs->actions, fs->actions, act_len);
1872 } else if (osm->type == htons(OFPST_VENDOR)) {
1873 struct nx_flow_stats *nfs;
1874 struct ofpbuf *msg;
1875 size_t start_len;
1876
1877 msg = ofputil_reserve_stats_reply(
1878 sizeof *nfs + NXM_MAX_LEN + act_len, replies);
1879 start_len = msg->size;
1880
1881 nfs = ofpbuf_put_uninit(msg, sizeof *nfs);
1882 nfs->table_id = fs->table_id;
1883 nfs->pad = 0;
1884 nfs->duration_sec = htonl(fs->duration_sec);
1885 nfs->duration_nsec = htonl(fs->duration_nsec);
1886 nfs->priority = htons(fs->rule.priority);
1887 nfs->idle_timeout = htons(fs->idle_timeout);
1888 nfs->hard_timeout = htons(fs->hard_timeout);
f27f2134
BP
1889 nfs->idle_age = htons(fs->idle_age < 0 ? 0
1890 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
1891 : UINT16_MAX);
1892 nfs->hard_age = htons(fs->hard_age < 0 ? 0
1893 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
1894 : UINT16_MAX);
e729e793 1895 nfs->match_len = htons(nx_put_match(msg, &fs->rule, 0, 0));
349adfb2
BP
1896 nfs->cookie = fs->cookie;
1897 nfs->packet_count = htonll(fs->packet_count);
1898 nfs->byte_count = htonll(fs->byte_count);
1899 ofpbuf_put(msg, fs->actions, act_len);
1900 nfs->length = htons(msg->size - start_len);
1901 } else {
1902 NOT_REACHED();
1903 }
1904}
1905
76c93b22 1906/* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
27527aa0 1907 * NXST_AGGREGATE reply according to 'protocol', and returns the message. */
76c93b22
BP
1908struct ofpbuf *
1909ofputil_encode_aggregate_stats_reply(
1910 const struct ofputil_aggregate_stats *stats,
1911 const struct ofp_stats_msg *request)
1912{
1913 struct ofpbuf *msg;
1914
1915 if (request->type == htons(OFPST_AGGREGATE)) {
1916 struct ofp_aggregate_stats_reply *asr;
1917
1918 asr = ofputil_make_stats_reply(sizeof *asr, request, &msg);
5e9d0469
BP
1919 put_32aligned_be64(&asr->packet_count,
1920 htonll(unknown_to_zero(stats->packet_count)));
1921 put_32aligned_be64(&asr->byte_count,
1922 htonll(unknown_to_zero(stats->byte_count)));
76c93b22
BP
1923 asr->flow_count = htonl(stats->flow_count);
1924 } else if (request->type == htons(OFPST_VENDOR)) {
1925 struct nx_aggregate_stats_reply *nasr;
1926
1927 nasr = ofputil_make_stats_reply(sizeof *nasr, request, &msg);
1928 assert(nasr->nsm.subtype == htonl(NXST_AGGREGATE));
1929 nasr->packet_count = htonll(stats->packet_count);
1930 nasr->byte_count = htonll(stats->byte_count);
1931 nasr->flow_count = htonl(stats->flow_count);
1932 } else {
1933 NOT_REACHED();
1934 }
1935
1936 return msg;
1937}
1938
b78f6b77
BP
1939/* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1940 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1941 * an OpenFlow error code. */
90bf1e07 1942enum ofperr
9b045a0c 1943ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
b78f6b77 1944 const struct ofp_header *oh)
9b045a0c
BP
1945{
1946 const struct ofputil_msg_type *type;
1947 enum ofputil_msg_code code;
1948
1949 ofputil_decode_msg_type(oh, &type);
1950 code = ofputil_msg_type_code(type);
1951 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1952 const struct ofp_flow_removed *ofr;
1953
1954 ofr = (const struct ofp_flow_removed *) oh;
1955 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
b78f6b77 1956 &fr->rule);
9b045a0c
BP
1957 fr->cookie = ofr->cookie;
1958 fr->reason = ofr->reason;
1959 fr->duration_sec = ntohl(ofr->duration_sec);
1960 fr->duration_nsec = ntohl(ofr->duration_nsec);
1961 fr->idle_timeout = ntohs(ofr->idle_timeout);
1962 fr->packet_count = ntohll(ofr->packet_count);
1963 fr->byte_count = ntohll(ofr->byte_count);
1964 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1965 struct nx_flow_removed *nfr;
1966 struct ofpbuf b;
1967 int error;
1968
1969 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1970
1971 nfr = ofpbuf_pull(&b, sizeof *nfr);
1972 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
e729e793 1973 &fr->rule, NULL, NULL);
9b045a0c
BP
1974 if (error) {
1975 return error;
1976 }
1977 if (b.size) {
90bf1e07 1978 return OFPERR_OFPBRC_BAD_LEN;
9b045a0c
BP
1979 }
1980
1981 fr->cookie = nfr->cookie;
1982 fr->reason = nfr->reason;
1983 fr->duration_sec = ntohl(nfr->duration_sec);
1984 fr->duration_nsec = ntohl(nfr->duration_nsec);
1985 fr->idle_timeout = ntohs(nfr->idle_timeout);
1986 fr->packet_count = ntohll(nfr->packet_count);
1987 fr->byte_count = ntohll(nfr->byte_count);
1988 } else {
1989 NOT_REACHED();
1990 }
1991
1992 return 0;
1993}
1994
588cd7b5 1995/* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
27527aa0 1996 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
588cd7b5
BP
1997 * message. */
1998struct ofpbuf *
1999ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
27527aa0 2000 enum ofputil_protocol protocol)
588cd7b5
BP
2001{
2002 struct ofpbuf *msg;
2003
27527aa0
BP
2004 switch (protocol) {
2005 case OFPUTIL_P_OF10:
2006 case OFPUTIL_P_OF10_TID: {
588cd7b5
BP
2007 struct ofp_flow_removed *ofr;
2008
2009 ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
2010 &msg);
b78f6b77 2011 ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
7fb563b9 2012 ofr->cookie = fr->cookie;
588cd7b5
BP
2013 ofr->priority = htons(fr->rule.priority);
2014 ofr->reason = fr->reason;
2015 ofr->duration_sec = htonl(fr->duration_sec);
2016 ofr->duration_nsec = htonl(fr->duration_nsec);
2017 ofr->idle_timeout = htons(fr->idle_timeout);
5e9d0469
BP
2018 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
2019 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
27527aa0
BP
2020 break;
2021 }
2022
2023 case OFPUTIL_P_NXM:
2024 case OFPUTIL_P_NXM_TID: {
588cd7b5
BP
2025 struct nx_flow_removed *nfr;
2026 int match_len;
2027
2028 make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
e729e793 2029 match_len = nx_put_match(msg, &fr->rule, 0, 0);
588cd7b5
BP
2030
2031 nfr = msg->data;
2032 nfr->cookie = fr->cookie;
2033 nfr->priority = htons(fr->rule.priority);
2034 nfr->reason = fr->reason;
2035 nfr->duration_sec = htonl(fr->duration_sec);
2036 nfr->duration_nsec = htonl(fr->duration_nsec);
2037 nfr->idle_timeout = htons(fr->idle_timeout);
2038 nfr->match_len = htons(match_len);
2039 nfr->packet_count = htonll(fr->packet_count);
2040 nfr->byte_count = htonll(fr->byte_count);
27527aa0
BP
2041 break;
2042 }
2043
2044 default:
588cd7b5
BP
2045 NOT_REACHED();
2046 }
2047
2048 return msg;
2049}
2050
65120a8a
EJ
2051int
2052ofputil_decode_packet_in(struct ofputil_packet_in *pin,
2053 const struct ofp_header *oh)
2054{
2055 const struct ofputil_msg_type *type;
2056 enum ofputil_msg_code code;
2057
2058 ofputil_decode_msg_type(oh, &type);
2059 code = ofputil_msg_type_code(type);
2060 memset(pin, 0, sizeof *pin);
2061
2062 if (code == OFPUTIL_OFPT_PACKET_IN) {
2063 const struct ofp_packet_in *opi = (const struct ofp_packet_in *) oh;
2064
2065 pin->packet = opi->data;
2066 pin->packet_len = ntohs(opi->header.length)
2067 - offsetof(struct ofp_packet_in, data);
2068
5d6c3af0 2069 pin->fmd.in_port = ntohs(opi->in_port);
65120a8a
EJ
2070 pin->reason = opi->reason;
2071 pin->buffer_id = ntohl(opi->buffer_id);
2072 pin->total_len = ntohs(opi->total_len);
54834960 2073 } else if (code == OFPUTIL_NXT_PACKET_IN) {
73dbf4ab 2074 const struct nx_packet_in *npi;
54834960
EJ
2075 struct cls_rule rule;
2076 struct ofpbuf b;
2077 int error;
2078
2079 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2080
2081 npi = ofpbuf_pull(&b, sizeof *npi);
2082 error = nx_pull_match_loose(&b, ntohs(npi->match_len), 0, &rule, NULL,
2083 NULL);
2084 if (error) {
2085 return error;
2086 }
2087
2088 if (!ofpbuf_try_pull(&b, 2)) {
90bf1e07 2089 return OFPERR_OFPBRC_BAD_LEN;
54834960
EJ
2090 }
2091
2092 pin->packet = b.data;
2093 pin->packet_len = b.size;
2094 pin->reason = npi->reason;
2095 pin->table_id = npi->table_id;
2096 pin->cookie = npi->cookie;
2097
2098 pin->fmd.in_port = rule.flow.in_port;
2099
2100 pin->fmd.tun_id = rule.flow.tun_id;
2101 pin->fmd.tun_id_mask = rule.wc.tun_id_mask;
2102
2103 memcpy(pin->fmd.regs, rule.flow.regs, sizeof pin->fmd.regs);
2104 memcpy(pin->fmd.reg_masks, rule.wc.reg_masks,
2105 sizeof pin->fmd.reg_masks);
2106
2107 pin->buffer_id = ntohl(npi->buffer_id);
2108 pin->total_len = ntohs(npi->total_len);
65120a8a
EJ
2109 } else {
2110 NOT_REACHED();
2111 }
2112
2113 return 0;
2114}
2115
54834960
EJ
2116/* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
2117 * in the format specified by 'packet_in_format'. */
ebb57021 2118struct ofpbuf *
54834960
EJ
2119ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
2120 enum nx_packet_in_format packet_in_format)
ebb57021 2121{
54834960
EJ
2122 size_t send_len = MIN(pin->send_len, pin->packet_len);
2123 struct ofpbuf *packet;
ebb57021
BP
2124
2125 /* Add OFPT_PACKET_IN. */
54834960
EJ
2126 if (packet_in_format == NXPIF_OPENFLOW10) {
2127 size_t header_len = offsetof(struct ofp_packet_in, data);
2128 struct ofp_packet_in *opi;
2129
2130 packet = ofpbuf_new(send_len + header_len);
2131 opi = ofpbuf_put_zeros(packet, header_len);
87ea5e5e 2132 opi->header.version = OFP10_VERSION;
54834960
EJ
2133 opi->header.type = OFPT_PACKET_IN;
2134 opi->total_len = htons(pin->total_len);
2135 opi->in_port = htons(pin->fmd.in_port);
2136 opi->reason = pin->reason;
2137 opi->buffer_id = htonl(pin->buffer_id);
2138
2139 ofpbuf_put(packet, pin->packet, send_len);
2140 } else if (packet_in_format == NXPIF_NXM) {
73dbf4ab 2141 struct nx_packet_in *npi;
54834960
EJ
2142 struct cls_rule rule;
2143 size_t match_len;
2144 size_t i;
2145
2146 /* Estimate of required PACKET_IN length includes the NPI header, space
2147 * for the match (2 times sizeof the metadata seems like enough), 2
2148 * bytes for padding, and the packet length. */
2149 packet = ofpbuf_new(sizeof *npi + sizeof(struct flow_metadata) * 2
2150 + 2 + send_len);
2151
2152 cls_rule_init_catchall(&rule, 0);
2153 cls_rule_set_tun_id_masked(&rule, pin->fmd.tun_id,
2154 pin->fmd.tun_id_mask);
2155
2156 for (i = 0; i < FLOW_N_REGS; i++) {
2157 cls_rule_set_reg_masked(&rule, i, pin->fmd.regs[i],
2158 pin->fmd.reg_masks[i]);
2159 }
2160
2161 cls_rule_set_in_port(&rule, pin->fmd.in_port);
2162
2163 ofpbuf_put_zeros(packet, sizeof *npi);
2164 match_len = nx_put_match(packet, &rule, 0, 0);
2165 ofpbuf_put_zeros(packet, 2);
2166 ofpbuf_put(packet, pin->packet, send_len);
2167
2168 npi = packet->data;
87ea5e5e 2169 npi->nxh.header.version = OFP10_VERSION;
54834960
EJ
2170 npi->nxh.header.type = OFPT_VENDOR;
2171 npi->nxh.vendor = htonl(NX_VENDOR_ID);
2172 npi->nxh.subtype = htonl(NXT_PACKET_IN);
2173
2174 npi->buffer_id = htonl(pin->buffer_id);
2175 npi->total_len = htons(pin->total_len);
2176 npi->reason = pin->reason;
2177 npi->table_id = pin->table_id;
2178 npi->cookie = pin->cookie;
2179 npi->match_len = htons(match_len);
2180 } else {
2181 NOT_REACHED();
2182 }
2183 update_openflow_length(packet);
2184
2185 return packet;
ebb57021
BP
2186}
2187
7c1a76a4
BP
2188const char *
2189ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
2190{
2191 static char s[INT_STRLEN(int) + 1];
2192
2193 switch (reason) {
2194 case OFPR_NO_MATCH:
2195 return "no_match";
2196 case OFPR_ACTION:
2197 return "action";
2198 case OFPR_INVALID_TTL:
2199 return "invalid_ttl";
2200
2201 case OFPR_N_REASONS:
2202 default:
2203 sprintf(s, "%d", (int) reason);
2204 return s;
2205 }
2206}
2207
2208bool
2209ofputil_packet_in_reason_from_string(const char *s,
2210 enum ofp_packet_in_reason *reason)
2211{
2212 int i;
2213
2214 for (i = 0; i < OFPR_N_REASONS; i++) {
2215 if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
2216 *reason = i;
2217 return true;
2218 }
2219 }
2220 return false;
2221}
2222
c6a93eb7
BP
2223enum ofperr
2224ofputil_decode_packet_out(struct ofputil_packet_out *po,
2225 const struct ofp_packet_out *opo)
2226{
2227 enum ofperr error;
2228 struct ofpbuf b;
2229
2230 po->buffer_id = ntohl(opo->buffer_id);
2231 po->in_port = ntohs(opo->in_port);
2232 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
751c7785 2233 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
c6a93eb7
BP
2234 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
2235 po->in_port);
2236 return OFPERR_NXBRC_BAD_IN_PORT;
2237 }
2238
2239 ofpbuf_use_const(&b, opo, ntohs(opo->header.length));
2240 ofpbuf_pull(&b, sizeof *opo);
2241
2242 error = ofputil_pull_actions(&b, ntohs(opo->actions_len),
2243 &po->actions, &po->n_actions);
2244 if (error) {
2245 return error;
2246 }
2247
2248 if (po->buffer_id == UINT32_MAX) {
2249 po->packet = b.data;
2250 po->packet_len = b.size;
2251 } else {
2252 po->packet = NULL;
2253 po->packet_len = 0;
2254 }
2255
2256 return 0;
2257}
6c038611
BP
2258\f
2259/* ofputil_phy_port */
2260
2261/* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
2262BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2263BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2264BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2265BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2266BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2267BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2268BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2269
2270/* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
9e1fd49b
BP
2271BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
2272BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
2273BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
2274BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
2275BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
6c038611 2276
9e1fd49b
BP
2277static enum netdev_features
2278netdev_port_features_from_ofp10(ovs_be32 ofp10_)
6c038611
BP
2279{
2280 uint32_t ofp10 = ntohl(ofp10_);
2281 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
2282}
2283
9e1fd49b
BP
2284static ovs_be32
2285netdev_port_features_to_ofp10(enum netdev_features features)
6c038611
BP
2286{
2287 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
2288}
c6a93eb7 2289
9e1fd49b
BP
2290BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2291BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2292BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2293BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2294BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2295BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2296BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2297BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
2298BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
2299BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
2300BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
2301BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
2302BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
2303BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
2304BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
2305BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
2306
2307static enum netdev_features
2308netdev_port_features_from_ofp11(ovs_be32 ofp11)
2309{
2310 return ntohl(ofp11) & 0xffff;
2311}
2312
2313static ovs_be32
2314netdev_port_features_to_ofp11(enum netdev_features features)
2315{
2316 return htonl(features & 0xffff);
2317}
2318
2319static enum ofperr
2320ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
2321 const struct ofp10_phy_port *opp)
2322{
2323 memset(pp, 0, sizeof *pp);
2324
2325 pp->port_no = ntohs(opp->port_no);
2326 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
2327 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
2328
2329 pp->config = ntohl(opp->config) & OFPPC10_ALL;
2330 pp->state = ntohl(opp->state) & OFPPS10_ALL;
2331
2332 pp->curr = netdev_port_features_from_ofp10(opp->curr);
2333 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
2334 pp->supported = netdev_port_features_from_ofp10(opp->supported);
2335 pp->peer = netdev_port_features_from_ofp10(opp->peer);
2336
2337 pp->curr_speed = netdev_features_to_bps(pp->curr) / 1000;
2338 pp->max_speed = netdev_features_to_bps(pp->supported) / 1000;
2339
2340 return 0;
2341}
2342
2343static enum ofperr
2344ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
2345 const struct ofp11_port *op)
2346{
2347 enum ofperr error;
2348
2349 memset(pp, 0, sizeof *pp);
2350
2351 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
2352 if (error) {
2353 return error;
2354 }
2355 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
2356 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
2357
2358 pp->config = ntohl(op->config) & OFPPC11_ALL;
2359 pp->state = ntohl(op->state) & OFPPC11_ALL;
2360
2361 pp->curr = netdev_port_features_from_ofp11(op->curr);
2362 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
2363 pp->supported = netdev_port_features_from_ofp11(op->supported);
2364 pp->peer = netdev_port_features_from_ofp11(op->peer);
2365
2366 pp->curr_speed = ntohl(op->curr_speed);
2367 pp->max_speed = ntohl(op->max_speed);
2368
2369 return 0;
2370}
2371
5b5a3a67
JP
2372static size_t
2373ofputil_get_phy_port_size(uint8_t ofp_version)
2374{
2375 return ofp_version == OFP10_VERSION ? sizeof(struct ofp10_phy_port)
2376 : sizeof(struct ofp11_port);
2377}
2378
9e1fd49b
BP
2379static void
2380ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
2381 struct ofp10_phy_port *opp)
2382{
2383 memset(opp, 0, sizeof *opp);
2384
2385 opp->port_no = htons(pp->port_no);
2386 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2387 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2388
2389 opp->config = htonl(pp->config & OFPPC10_ALL);
2390 opp->state = htonl(pp->state & OFPPS10_ALL);
2391
2392 opp->curr = netdev_port_features_to_ofp10(pp->curr);
2393 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
2394 opp->supported = netdev_port_features_to_ofp10(pp->supported);
2395 opp->peer = netdev_port_features_to_ofp10(pp->peer);
2396}
2397
2398static void
2399ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
2400 struct ofp11_port *op)
2401{
2402 memset(op, 0, sizeof *op);
2403
2404 op->port_no = ofputil_port_to_ofp11(pp->port_no);
2405 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2406 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2407
2408 op->config = htonl(pp->config & OFPPC11_ALL);
2409 op->state = htonl(pp->state & OFPPS11_ALL);
2410
2411 op->curr = netdev_port_features_to_ofp11(pp->curr);
2412 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
2413 op->supported = netdev_port_features_to_ofp11(pp->supported);
2414 op->peer = netdev_port_features_to_ofp11(pp->peer);
2415
2416 op->curr_speed = htonl(pp->curr_speed);
2417 op->max_speed = htonl(pp->max_speed);
2418}
2419
2420static void
2421ofputil_put_phy_port(uint8_t ofp_version, const struct ofputil_phy_port *pp,
2422 struct ofpbuf *b)
2423{
2424 if (ofp_version == OFP10_VERSION) {
2425 struct ofp10_phy_port *opp;
2426 if (b->size + sizeof *opp <= UINT16_MAX) {
2427 opp = ofpbuf_put_uninit(b, sizeof *opp);
2428 ofputil_encode_ofp10_phy_port(pp, opp);
2429 }
2430 } else {
2431 struct ofp11_port *op;
2432 if (b->size + sizeof *op <= UINT16_MAX) {
2433 op = ofpbuf_put_uninit(b, sizeof *op);
2434 ofputil_encode_ofp11_port(pp, op);
2435 }
2436 }
2437}
2be393ed
JP
2438
2439void
2440ofputil_append_port_desc_stats_reply(uint8_t ofp_version,
2441 const struct ofputil_phy_port *pp,
2442 struct list *replies)
2443{
2444 if (ofp_version == OFP10_VERSION) {
2445 struct ofp10_phy_port *opp;
2446
2447 opp = ofputil_append_stats_reply(sizeof *opp, replies);
2448 ofputil_encode_ofp10_phy_port(pp, opp);
2449 } else {
2450 struct ofp11_port *op;
2451
2452 op = ofputil_append_stats_reply(sizeof *op, replies);
2453 ofputil_encode_ofp11_port(pp, op);
2454 }
2455}
9e1fd49b
BP
2456\f
2457/* ofputil_switch_features */
2458
2459#define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
2460 OFPC_IP_REASM | OFPC_QUEUE_STATS | OFPC_ARP_MATCH_IP)
2461BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
2462BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
2463BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
2464BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
2465BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
2466BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
2467
2468struct ofputil_action_bit_translation {
2469 enum ofputil_action_bitmap ofputil_bit;
2470 int of_bit;
2471};
2472
2473static const struct ofputil_action_bit_translation of10_action_bits[] = {
2474 { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT },
2475 { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID },
2476 { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP },
2477 { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN },
2478 { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC },
2479 { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST },
2480 { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC },
2481 { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST },
2482 { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS },
2483 { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC },
2484 { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST },
2485 { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE },
2486 { 0, 0 },
2487};
2488
2489static const struct ofputil_action_bit_translation of11_action_bits[] = {
2490 { OFPUTIL_A_OUTPUT, OFPAT11_OUTPUT },
2491 { OFPUTIL_A_SET_VLAN_VID, OFPAT11_SET_VLAN_VID },
2492 { OFPUTIL_A_SET_VLAN_PCP, OFPAT11_SET_VLAN_PCP },
2493 { OFPUTIL_A_SET_DL_SRC, OFPAT11_SET_DL_SRC },
2494 { OFPUTIL_A_SET_DL_DST, OFPAT11_SET_DL_DST },
2495 { OFPUTIL_A_SET_NW_SRC, OFPAT11_SET_NW_SRC },
2496 { OFPUTIL_A_SET_NW_DST, OFPAT11_SET_NW_DST },
2497 { OFPUTIL_A_SET_NW_TOS, OFPAT11_SET_NW_TOS },
2498 { OFPUTIL_A_SET_NW_ECN, OFPAT11_SET_NW_ECN },
2499 { OFPUTIL_A_SET_TP_SRC, OFPAT11_SET_TP_SRC },
2500 { OFPUTIL_A_SET_TP_DST, OFPAT11_SET_TP_DST },
2501 { OFPUTIL_A_COPY_TTL_OUT, OFPAT11_COPY_TTL_OUT },
2502 { OFPUTIL_A_COPY_TTL_IN, OFPAT11_COPY_TTL_IN },
2503 { OFPUTIL_A_SET_MPLS_LABEL, OFPAT11_SET_MPLS_LABEL },
2504 { OFPUTIL_A_SET_MPLS_TC, OFPAT11_SET_MPLS_TC },
2505 { OFPUTIL_A_SET_MPLS_TTL, OFPAT11_SET_MPLS_TTL },
2506 { OFPUTIL_A_DEC_MPLS_TTL, OFPAT11_DEC_MPLS_TTL },
2507 { OFPUTIL_A_PUSH_VLAN, OFPAT11_PUSH_VLAN },
2508 { OFPUTIL_A_POP_VLAN, OFPAT11_POP_VLAN },
2509 { OFPUTIL_A_PUSH_MPLS, OFPAT11_PUSH_MPLS },
2510 { OFPUTIL_A_POP_MPLS, OFPAT11_POP_MPLS },
2511 { OFPUTIL_A_SET_QUEUE, OFPAT11_SET_QUEUE },
2512 { OFPUTIL_A_GROUP, OFPAT11_GROUP },
2513 { OFPUTIL_A_SET_NW_TTL, OFPAT11_SET_NW_TTL },
2514 { OFPUTIL_A_DEC_NW_TTL, OFPAT11_DEC_NW_TTL },
2515 { 0, 0 },
2516};
2517
2518static enum ofputil_action_bitmap
2519decode_action_bits(ovs_be32 of_actions,
2520 const struct ofputil_action_bit_translation *x)
2521{
2522 enum ofputil_action_bitmap ofputil_actions;
2523
2524 ofputil_actions = 0;
2525 for (; x->ofputil_bit; x++) {
2526 if (of_actions & htonl(1u << x->of_bit)) {
2527 ofputil_actions |= x->ofputil_bit;
2528 }
2529 }
2530 return ofputil_actions;
2531}
2532
2533/* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
2534 * abstract representation in '*features'. Initializes '*b' to iterate over
2535 * the OpenFlow port structures following 'osf' with later calls to
2be393ed 2536 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
9e1fd49b
BP
2537 * OFPERR_* value. */
2538enum ofperr
2539ofputil_decode_switch_features(const struct ofp_switch_features *osf,
2540 struct ofputil_switch_features *features,
2541 struct ofpbuf *b)
2542{
2543 ofpbuf_use_const(b, osf, ntohs(osf->header.length));
2544 ofpbuf_pull(b, sizeof *osf);
9e1fd49b
BP
2545
2546 features->datapath_id = ntohll(osf->datapath_id);
2547 features->n_buffers = ntohl(osf->n_buffers);
2548 features->n_tables = osf->n_tables;
2549
2550 features->capabilities = ntohl(osf->capabilities) & OFPC_COMMON;
9e1fd49b 2551
5b5a3a67
JP
2552 if (b->size % ofputil_get_phy_port_size(osf->header.version)) {
2553 return OFPERR_OFPBRC_BAD_LEN;
2554 }
2555
2556 if (osf->header.version == OFP10_VERSION) {
9e1fd49b
BP
2557 if (osf->capabilities & htonl(OFPC10_STP)) {
2558 features->capabilities |= OFPUTIL_C_STP;
2559 }
2560 features->actions = decode_action_bits(osf->actions, of10_action_bits);
2561 } else if (osf->header.version == OFP11_VERSION) {
9e1fd49b
BP
2562 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
2563 features->capabilities |= OFPUTIL_C_GROUP_STATS;
2564 }
2565 features->actions = decode_action_bits(osf->actions, of11_action_bits);
2566 } else {
2567 return OFPERR_OFPBRC_BAD_VERSION;
2568 }
2569
2570 return 0;
2571}
2572
347b7ac4
JP
2573/* Returns true if the maximum number of ports are in 'osf'. */
2574static bool
2575max_ports_in_features(const struct ofp_switch_features *osf)
2576{
5b5a3a67 2577 size_t pp_size = ofputil_get_phy_port_size(osf->header.version);
347b7ac4
JP
2578 return ntohs(osf->header.length) + pp_size > UINT16_MAX;
2579}
2580
2581/* Given a buffer 'b' that contains a Features Reply message, checks if
2582 * it contains the maximum number of ports that will fit. If so, it
2583 * returns true and removes the ports from the message. The caller
2584 * should then send an OFPST_PORT_DESC stats request to get the ports,
2585 * since the switch may have more ports than could be represented in the
2586 * Features Reply. Otherwise, returns false.
2587 */
2588bool
2589ofputil_switch_features_ports_trunc(struct ofpbuf *b)
2590{
2591 struct ofp_switch_features *osf = b->data;
2592
2593 if (max_ports_in_features(osf)) {
2594 /* Remove all the ports. */
2595 b->size = sizeof(*osf);
2596 update_openflow_length(b);
2597
2598 return true;
2599 }
2600
2601 return false;
2602}
2603
9e1fd49b
BP
2604static ovs_be32
2605encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
2606 const struct ofputil_action_bit_translation *x)
2607{
2608 uint32_t of_actions;
2609
2610 of_actions = 0;
2611 for (; x->ofputil_bit; x++) {
2612 if (ofputil_actions & x->ofputil_bit) {
2613 of_actions |= 1 << x->of_bit;
2614 }
2615 }
2616 return htonl(of_actions);
2617}
2618
2619/* Returns a buffer owned by the caller that encodes 'features' in the format
2620 * required by 'protocol' with the given 'xid'. The caller should append port
2621 * information to the buffer with subsequent calls to
2622 * ofputil_put_switch_features_port(). */
2623struct ofpbuf *
2624ofputil_encode_switch_features(const struct ofputil_switch_features *features,
2625 enum ofputil_protocol protocol, ovs_be32 xid)
2626{
2627 struct ofp_switch_features *osf;
2628 struct ofpbuf *b;
2629
2630 osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, xid, &b);
2631 osf->header.version = ofputil_protocol_to_ofp_version(protocol);
2632 osf->datapath_id = htonll(features->datapath_id);
2633 osf->n_buffers = htonl(features->n_buffers);
2634 osf->n_tables = features->n_tables;
2635
2636 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
2637 if (osf->header.version == OFP10_VERSION) {
2638 if (features->capabilities & OFPUTIL_C_STP) {
2639 osf->capabilities |= htonl(OFPC10_STP);
2640 }
2641 osf->actions = encode_action_bits(features->actions, of10_action_bits);
2642 } else {
2643 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
2644 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
2645 }
2646 osf->actions = encode_action_bits(features->actions, of11_action_bits);
2647 }
2648
2649 return b;
2650}
2651
2652/* Encodes 'pp' into the format required by the switch_features message already
2653 * in 'b', which should have been returned by ofputil_encode_switch_features(),
2654 * and appends the encoded version to 'b'. */
2655void
2656ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
2657 struct ofpbuf *b)
2658{
2659 const struct ofp_switch_features *osf = b->data;
2660
2661 ofputil_put_phy_port(osf->header.version, pp, b);
2662}
2663\f
2664/* ofputil_port_status */
2665
2666/* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
2667 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
2668enum ofperr
2669ofputil_decode_port_status(const struct ofp_port_status *ops,
2670 struct ofputil_port_status *ps)
2671{
2672 struct ofpbuf b;
2673 int retval;
2674
2675 if (ops->reason != OFPPR_ADD &&
2676 ops->reason != OFPPR_DELETE &&
2677 ops->reason != OFPPR_MODIFY) {
2678 return OFPERR_NXBRC_BAD_REASON;
2679 }
2680 ps->reason = ops->reason;
2681
2682 ofpbuf_use_const(&b, ops, ntohs(ops->header.length));
2683 ofpbuf_pull(&b, sizeof *ops);
2684 retval = ofputil_pull_phy_port(ops->header.version, &b, &ps->desc);
2685 assert(retval != EOF);
2686 return retval;
2687}
2688
2689/* Converts the abstract form of a "port status" message in '*ps' into an
2690 * OpenFlow message suitable for 'protocol', and returns that encoded form in
2691 * a buffer owned by the caller. */
2692struct ofpbuf *
2693ofputil_encode_port_status(const struct ofputil_port_status *ps,
2694 enum ofputil_protocol protocol)
2695{
2696 struct ofp_port_status *ops;
2697 struct ofpbuf *b;
2698
2699 b = ofpbuf_new(sizeof *ops + sizeof(struct ofp11_port));
2700 ops = put_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, htonl(0), b);
2701 ops->header.version = ofputil_protocol_to_ofp_version(protocol);
2702 ops->reason = ps->reason;
2703 ofputil_put_phy_port(ops->header.version, &ps->desc, b);
2704 update_openflow_length(b);
2705 return b;
2706}
2707\f
2708/* ofputil_port_mod */
2709
2710/* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
2711 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
2712enum ofperr
2713ofputil_decode_port_mod(const struct ofp_header *oh,
2714 struct ofputil_port_mod *pm)
2715{
2716 if (oh->version == OFP10_VERSION) {
2717 const struct ofp10_port_mod *opm = (const struct ofp10_port_mod *) oh;
2718
2719 if (oh->length != htons(sizeof *opm)) {
2720 return OFPERR_OFPBRC_BAD_LEN;
2721 }
2722
2723 pm->port_no = ntohs(opm->port_no);
2724 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2725 pm->config = ntohl(opm->config) & OFPPC10_ALL;
2726 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
2727 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
2728 } else if (oh->version == OFP11_VERSION) {
2729 const struct ofp11_port_mod *opm = (const struct ofp11_port_mod *) oh;
2730 enum ofperr error;
2731
2732 if (oh->length != htons(sizeof *opm)) {
2733 return OFPERR_OFPBRC_BAD_LEN;
2734 }
2735
2736 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
2737 if (error) {
2738 return error;
2739 }
2740
2741 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2742 pm->config = ntohl(opm->config) & OFPPC11_ALL;
2743 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
2744 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
2745 } else {
2746 return OFPERR_OFPBRC_BAD_VERSION;
2747 }
2748
2749 pm->config &= pm->mask;
2750 return 0;
2751}
2752
2753/* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
2754 * message suitable for 'protocol', and returns that encoded form in a buffer
2755 * owned by the caller. */
2756struct ofpbuf *
2757ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
2758 enum ofputil_protocol protocol)
2759{
2760 uint8_t ofp_version = ofputil_protocol_to_ofp_version(protocol);
2761 struct ofpbuf *b;
2762
2763 if (ofp_version == OFP10_VERSION) {
2764 struct ofp10_port_mod *opm;
2765
2766 opm = make_openflow(sizeof *opm, OFPT10_PORT_MOD, &b);
2767 opm->port_no = htons(pm->port_no);
2768 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2769 opm->config = htonl(pm->config & OFPPC10_ALL);
2770 opm->mask = htonl(pm->mask & OFPPC10_ALL);
2771 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
2772 } else if (ofp_version == OFP11_VERSION) {
2773 struct ofp11_port_mod *opm;
2774
2775 opm = make_openflow(sizeof *opm, OFPT11_PORT_MOD, &b);
2776 opm->port_no = htonl(pm->port_no);
2777 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2778 opm->config = htonl(pm->config & OFPPC11_ALL);
2779 opm->mask = htonl(pm->mask & OFPPC11_ALL);
2780 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
2781 } else {
2782 NOT_REACHED();
2783 }
2784
2785 return b;
2786}
2787
c6a93eb7
BP
2788struct ofpbuf *
2789ofputil_encode_packet_out(const struct ofputil_packet_out *po)
2790{
2791 struct ofp_packet_out *opo;
2792 size_t actions_len;
2793 struct ofpbuf *msg;
2794 size_t size;
2795
2796 actions_len = po->n_actions * sizeof *po->actions;
2797 size = sizeof *opo + actions_len;
2798 if (po->buffer_id == UINT32_MAX) {
2799 size += po->packet_len;
2800 }
2801
2802 msg = ofpbuf_new(size);
5293a2e1 2803 opo = put_openflow(sizeof *opo, OFPT10_PACKET_OUT, msg);
c6a93eb7
BP
2804 opo->buffer_id = htonl(po->buffer_id);
2805 opo->in_port = htons(po->in_port);
2806 opo->actions_len = htons(actions_len);
2807 ofpbuf_put(msg, po->actions, actions_len);
2808 if (po->buffer_id == UINT32_MAX) {
2809 ofpbuf_put(msg, po->packet, po->packet_len);
2810 }
2811 update_openflow_length(msg);
2812
2813 return msg;
2814}
2815
d1e2cf21
BP
2816/* Returns a string representing the message type of 'type'. The string is the
2817 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
2818 * messages, the constant is followed by "request" or "reply",
2819 * e.g. "OFPST_AGGREGATE reply". */
2820const char *
2821ofputil_msg_type_name(const struct ofputil_msg_type *type)
2822{
2823 return type->name;
2824}
2825\f
fa37b408
BP
2826/* Allocates and stores in '*bufferp' a new ofpbuf with a size of
2827 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
2828 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
2829 * zeroed.
2830 *
2831 * The caller is responsible for freeing '*bufferp' when it is no longer
2832 * needed.
2833 *
2834 * The OpenFlow header length is initially set to 'openflow_len'; if the
2835 * message is later extended, the length should be updated with
2836 * update_openflow_length() before sending.
2837 *
2838 * Returns the header. */
2839void *
2840make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
2841{
2842 *bufferp = ofpbuf_new(openflow_len);
2843 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
2844}
2845
0bd0c660
BP
2846/* Similar to make_openflow() but creates a Nicira vendor extension message
2847 * with the specific 'subtype'. 'subtype' should be in host byte order. */
2848void *
2849make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
2850{
2851 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
2852}
2853
fa37b408
BP
2854/* Allocates and stores in '*bufferp' a new ofpbuf with a size of
2855 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
2856 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
2857 * zeroed.
2858 *
2859 * The caller is responsible for freeing '*bufferp' when it is no longer
2860 * needed.
2861 *
2862 * The OpenFlow header length is initially set to 'openflow_len'; if the
2863 * message is later extended, the length should be updated with
2864 * update_openflow_length() before sending.
2865 *
2866 * Returns the header. */
2867void *
44381c1b 2868make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
fa37b408
BP
2869 struct ofpbuf **bufferp)
2870{
2871 *bufferp = ofpbuf_new(openflow_len);
2872 return put_openflow_xid(openflow_len, type, xid, *bufferp);
2873}
2874
0bd0c660
BP
2875/* Similar to make_openflow_xid() but creates a Nicira vendor extension message
2876 * with the specific 'subtype'. 'subtype' should be in host byte order. */
2877void *
44381c1b 2878make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
0bd0c660
BP
2879 struct ofpbuf **bufferp)
2880{
dfdfc8d4
BP
2881 *bufferp = ofpbuf_new(openflow_len);
2882 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
0bd0c660
BP
2883}
2884
fa37b408
BP
2885/* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
2886 * with the given 'type' and an arbitrary transaction id. Allocated bytes
2887 * beyond the header, if any, are zeroed.
2888 *
2889 * The OpenFlow header length is initially set to 'openflow_len'; if the
2890 * message is later extended, the length should be updated with
2891 * update_openflow_length() before sending.
2892 *
2893 * Returns the header. */
2894void *
2895put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
2896{
2897 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
2898}
2899
2900/* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
2901 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
2902 * the header, if any, are zeroed.
2903 *
2904 * The OpenFlow header length is initially set to 'openflow_len'; if the
2905 * message is later extended, the length should be updated with
2906 * update_openflow_length() before sending.
2907 *
2908 * Returns the header. */
2909void *
44381c1b 2910put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
fa37b408
BP
2911 struct ofpbuf *buffer)
2912{
2913 struct ofp_header *oh;
2914
2915 assert(openflow_len >= sizeof *oh);
2916 assert(openflow_len <= UINT16_MAX);
2917
2918 oh = ofpbuf_put_uninit(buffer, openflow_len);
87ea5e5e 2919 oh->version = OFP10_VERSION;
fa37b408
BP
2920 oh->type = type;
2921 oh->length = htons(openflow_len);
2922 oh->xid = xid;
2923 memset(oh + 1, 0, openflow_len - sizeof *oh);
2924 return oh;
2925}
2926
dfdfc8d4
BP
2927/* Similar to put_openflow() but append a Nicira vendor extension message with
2928 * the specific 'subtype'. 'subtype' should be in host byte order. */
2929void *
2930put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
2931{
2932 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
2933}
2934
2935/* Similar to put_openflow_xid() but append a Nicira vendor extension message
2936 * with the specific 'subtype'. 'subtype' should be in host byte order. */
2937void *
2938put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
2939 struct ofpbuf *buffer)
2940{
2941 struct nicira_header *nxh;
2942
2943 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
2944 nxh->vendor = htonl(NX_VENDOR_ID);
2945 nxh->subtype = htonl(subtype);
2946 return nxh;
2947}
2948
fa37b408
BP
2949/* Updates the 'length' field of the OpenFlow message in 'buffer' to
2950 * 'buffer->size'. */
2951void
d295e8e9 2952update_openflow_length(struct ofpbuf *buffer)
fa37b408
BP
2953{
2954 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
d295e8e9 2955 oh->length = htons(buffer->size);
fa37b408
BP
2956}
2957
63f2140a
BP
2958static void
2959put_stats__(ovs_be32 xid, uint8_t ofp_type,
2960 ovs_be16 ofpst_type, ovs_be32 nxst_subtype,
2961 struct ofpbuf *msg)
2962{
2963 if (ofpst_type == htons(OFPST_VENDOR)) {
2964 struct nicira_stats_msg *nsm;
2965
2966 nsm = put_openflow_xid(sizeof *nsm, ofp_type, xid, msg);
2967 nsm->vsm.osm.type = ofpst_type;
2968 nsm->vsm.vendor = htonl(NX_VENDOR_ID);
2969 nsm->subtype = nxst_subtype;
2970 } else {
2971 struct ofp_stats_msg *osm;
2972
2973 osm = put_openflow_xid(sizeof *osm, ofp_type, xid, msg);
2974 osm->type = ofpst_type;
2975 }
2976}
2977
2978/* Creates a statistics request message with total length 'openflow_len'
2979 * (including all headers) and the given 'ofpst_type', and stores the buffer
2980 * containing the new message in '*bufferp'. If 'ofpst_type' is OFPST_VENDOR
2981 * then 'nxst_subtype' is used as the Nicira vendor extension statistics
2982 * subtype (otherwise 'nxst_subtype' is ignored).
2983 *
2984 * Initializes bytes following the headers to all-bits-zero.
2985 *
2986 * Returns the first byte of the new message. */
dfdfc8d4 2987void *
63f2140a
BP
2988ofputil_make_stats_request(size_t openflow_len, uint16_t ofpst_type,
2989 uint32_t nxst_subtype, struct ofpbuf **bufferp)
dfdfc8d4 2990{
63f2140a
BP
2991 struct ofpbuf *msg;
2992
2993 msg = *bufferp = ofpbuf_new(openflow_len);
5293a2e1 2994 put_stats__(alloc_xid(), OFPT10_STATS_REQUEST,
63f2140a
BP
2995 htons(ofpst_type), htonl(nxst_subtype), msg);
2996 ofpbuf_padto(msg, openflow_len);
2997
2998 return msg->data;
2999}
3000
3001static void
3002put_stats_reply__(const struct ofp_stats_msg *request, struct ofpbuf *msg)
3003{
5293a2e1
BP
3004 assert(request->header.type == OFPT10_STATS_REQUEST ||
3005 request->header.type == OFPT10_STATS_REPLY);
3006 put_stats__(request->header.xid, OFPT10_STATS_REPLY, request->type,
63f2140a
BP
3007 (request->type != htons(OFPST_VENDOR)
3008 ? htonl(0)
3009 : ((const struct nicira_stats_msg *) request)->subtype),
3010 msg);
3011}
3012
3013/* Creates a statistics reply message with total length 'openflow_len'
3014 * (including all headers) and the same type (either a standard OpenFlow
3015 * statistics type or a Nicira extension type and subtype) as 'request', and
3016 * stores the buffer containing the new message in '*bufferp'.
3017 *
3018 * Initializes bytes following the headers to all-bits-zero.
3019 *
3020 * Returns the first byte of the new message. */
3021void *
3022ofputil_make_stats_reply(size_t openflow_len,
3023 const struct ofp_stats_msg *request,
3024 struct ofpbuf **bufferp)
3025{
3026 struct ofpbuf *msg;
3027
3028 msg = *bufferp = ofpbuf_new(openflow_len);
3029 put_stats_reply__(request, msg);
3030 ofpbuf_padto(msg, openflow_len);
3031
3032 return msg->data;
3033}
3034
3035/* Initializes 'replies' as a list of ofpbufs that will contain a series of
3036 * replies to 'request', which should be an OpenFlow or Nicira extension
3037 * statistics request. Initially 'replies' will have a single reply message
3038 * that has only a header. The functions ofputil_reserve_stats_reply() and
3039 * ofputil_append_stats_reply() may be used to add to the reply. */
3040void
3041ofputil_start_stats_reply(const struct ofp_stats_msg *request,
3042 struct list *replies)
3043{
3044 struct ofpbuf *msg;
3045
3046 msg = ofpbuf_new(1024);
3047 put_stats_reply__(request, msg);
3048
3049 list_init(replies);
3050 list_push_back(replies, &msg->list_node);
3051}
3052
3053/* Prepares to append up to 'len' bytes to the series of statistics replies in
3054 * 'replies', which should have been initialized with
3055 * ofputil_start_stats_reply(). Returns an ofpbuf with at least 'len' bytes of
3056 * tailroom. (The 'len' bytes have not actually be allocated; the caller must
3057 * do so with e.g. ofpbuf_put_uninit().) */
3058struct ofpbuf *
3059ofputil_reserve_stats_reply(size_t len, struct list *replies)
3060{
3061 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
3062 struct ofp_stats_msg *osm = msg->data;
3063
3064 if (msg->size + len <= UINT16_MAX) {
3065 ofpbuf_prealloc_tailroom(msg, len);
3066 } else {
3067 osm->flags |= htons(OFPSF_REPLY_MORE);
3068
3069 msg = ofpbuf_new(MAX(1024, sizeof(struct nicira_stats_msg) + len));
3070 put_stats_reply__(osm, msg);
3071 list_push_back(replies, &msg->list_node);
3072 }
3073 return msg;
dfdfc8d4
BP
3074}
3075
63f2140a
BP
3076/* Appends 'len' bytes to the series of statistics replies in 'replies', and
3077 * returns the first byte. */
dfdfc8d4 3078void *
63f2140a 3079ofputil_append_stats_reply(size_t len, struct list *replies)
dfdfc8d4 3080{
63f2140a 3081 return ofpbuf_put_uninit(ofputil_reserve_stats_reply(len, replies), len);
dfdfc8d4
BP
3082}
3083
03cd3493 3084/* Returns the first byte past the ofp_stats_msg header in 'oh'. */
d1e2cf21
BP
3085const void *
3086ofputil_stats_body(const struct ofp_header *oh)
3087{
5293a2e1 3088 assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY);
28c8bad1 3089 return (const struct ofp_stats_msg *) oh + 1;
d1e2cf21
BP
3090}
3091
03cd3493 3092/* Returns the number of bytes past the ofp_stats_msg header in 'oh'. */
d1e2cf21
BP
3093size_t
3094ofputil_stats_body_len(const struct ofp_header *oh)
3095{
5293a2e1 3096 assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY);
28c8bad1 3097 return ntohs(oh->length) - sizeof(struct ofp_stats_msg);
d1e2cf21
BP
3098}
3099
03cd3493 3100/* Returns the first byte past the nicira_stats_msg header in 'oh'. */
c6430da5
BP
3101const void *
3102ofputil_nxstats_body(const struct ofp_header *oh)
3103{
5293a2e1 3104 assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY);
c6430da5
BP
3105 return ((const struct nicira_stats_msg *) oh) + 1;
3106}
3107
03cd3493 3108/* Returns the number of bytes past the nicira_stats_msg header in 'oh'. */
c6430da5
BP
3109size_t
3110ofputil_nxstats_body_len(const struct ofp_header *oh)
3111{
5293a2e1 3112 assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY);
c6430da5
BP
3113 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
3114}
3115
fa37b408 3116struct ofpbuf *
daa68e9f
BP
3117make_flow_mod(uint16_t command, const struct cls_rule *rule,
3118 size_t actions_len)
fa37b408
BP
3119{
3120 struct ofp_flow_mod *ofm;
3121 size_t size = sizeof *ofm + actions_len;
3122 struct ofpbuf *out = ofpbuf_new(size);
3123 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
87ea5e5e 3124 ofm->header.version = OFP10_VERSION;
5293a2e1 3125 ofm->header.type = OFPT10_FLOW_MOD;
fa37b408
BP
3126 ofm->header.length = htons(size);
3127 ofm->cookie = 0;
daa68e9f 3128 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
b78f6b77 3129 ofputil_cls_rule_to_match(rule, &ofm->match);
fa37b408
BP
3130 ofm->command = htons(command);
3131 return out;
3132}
3133
3134struct ofpbuf *
daa68e9f 3135make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
fa37b408
BP
3136 uint16_t idle_timeout, size_t actions_len)
3137{
daa68e9f 3138 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
fa37b408
BP
3139 struct ofp_flow_mod *ofm = out->data;
3140 ofm->idle_timeout = htons(idle_timeout);
3141 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
3142 ofm->buffer_id = htonl(buffer_id);
3143 return out;
3144}
3145
3146struct ofpbuf *
daa68e9f 3147make_del_flow(const struct cls_rule *rule)
fa37b408 3148{
daa68e9f 3149 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
fa37b408
BP
3150 struct ofp_flow_mod *ofm = out->data;
3151 ofm->out_port = htons(OFPP_NONE);
3152 return out;
3153}
3154
3155struct ofpbuf *
daa68e9f 3156make_add_simple_flow(const struct cls_rule *rule,
fa37b408
BP
3157 uint32_t buffer_id, uint16_t out_port,
3158 uint16_t idle_timeout)
3159{
81f3cad4
BP
3160 if (out_port != OFPP_NONE) {
3161 struct ofp_action_output *oao;
3162 struct ofpbuf *buffer;
3163
daa68e9f 3164 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
08f94c0e 3165 ofputil_put_OFPAT10_OUTPUT(buffer)->port = htons(out_port);
81f3cad4
BP
3166 return buffer;
3167 } else {
daa68e9f 3168 return make_add_flow(rule, buffer_id, idle_timeout, 0);
81f3cad4 3169 }
fa37b408
BP
3170}
3171
3172struct ofpbuf *
3173make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
3174 const struct ofpbuf *payload, int max_send_len)
3175{
3176 struct ofp_packet_in *opi;
3177 struct ofpbuf *buf;
3178 int send_len;
3179
3180 send_len = MIN(max_send_len, payload->size);
3181 buf = ofpbuf_new(sizeof *opi + send_len);
3182 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
3183 OFPT_PACKET_IN, 0, buf);
3184 opi->buffer_id = htonl(buffer_id);
3185 opi->total_len = htons(payload->size);
3186 opi->in_port = htons(in_port);
3187 opi->reason = reason;
3188 ofpbuf_put(buf, payload->data, send_len);
3189 update_openflow_length(buf);
3190
3191 return buf;
3192}
3193
fa37b408
BP
3194/* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
3195struct ofpbuf *
3196make_echo_request(void)
3197{
3198 struct ofp_header *rq;
3199 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
3200 rq = ofpbuf_put_uninit(out, sizeof *rq);
87ea5e5e 3201 rq->version = OFP10_VERSION;
fa37b408
BP
3202 rq->type = OFPT_ECHO_REQUEST;
3203 rq->length = htons(sizeof *rq);
44381c1b 3204 rq->xid = htonl(0);
fa37b408
BP
3205 return out;
3206}
3207
3208/* Creates and returns an OFPT_ECHO_REPLY message matching the
3209 * OFPT_ECHO_REQUEST message in 'rq'. */
3210struct ofpbuf *
3211make_echo_reply(const struct ofp_header *rq)
3212{
3213 size_t size = ntohs(rq->length);
3214 struct ofpbuf *out = ofpbuf_new(size);
3215 struct ofp_header *reply = ofpbuf_put(out, rq, size);
3216 reply->type = OFPT_ECHO_REPLY;
3217 return out;
3218}
3219
efb80167
BP
3220struct ofpbuf *
3221ofputil_encode_barrier_request(void)
3222{
3223 struct ofpbuf *msg;
3224
5293a2e1 3225 make_openflow(sizeof(struct ofp_header), OFPT10_BARRIER_REQUEST, &msg);
efb80167
BP
3226 return msg;
3227}
3228
7257b535
BP
3229const char *
3230ofputil_frag_handling_to_string(enum ofp_config_flags flags)
3231{
3232 switch (flags & OFPC_FRAG_MASK) {
3233 case OFPC_FRAG_NORMAL: return "normal";
3234 case OFPC_FRAG_DROP: return "drop";
3235 case OFPC_FRAG_REASM: return "reassemble";
3236 case OFPC_FRAG_NX_MATCH: return "nx-match";
3237 }
3238
3239 NOT_REACHED();
3240}
3241
3242bool
3243ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
3244{
3245 if (!strcasecmp(s, "normal")) {
3246 *flags = OFPC_FRAG_NORMAL;
3247 } else if (!strcasecmp(s, "drop")) {
3248 *flags = OFPC_FRAG_DROP;
3249 } else if (!strcasecmp(s, "reassemble")) {
3250 *flags = OFPC_FRAG_REASM;
3251 } else if (!strcasecmp(s, "nx-match")) {
3252 *flags = OFPC_FRAG_NX_MATCH;
3253 } else {
3254 return false;
3255 }
3256 return true;
3257}
3258
7b7503ea
BP
3259/* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
3260 * port number and stores the latter in '*ofp10_port', for the purpose of
3261 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
3262 * otherwise an OFPERR_* number.
3263 *
3264 * See the definition of OFP11_MAX for an explanation of the mapping. */
3265enum ofperr
3266ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port)
3267{
3268 uint32_t ofp11_port_h = ntohl(ofp11_port);
3269
3270 if (ofp11_port_h < OFPP_MAX) {
3271 *ofp10_port = ofp11_port_h;
3272 return 0;
3273 } else if (ofp11_port_h >= OFPP11_MAX) {
3274 *ofp10_port = ofp11_port_h - OFPP11_OFFSET;
3275 return 0;
3276 } else {
3277 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
3278 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
3279 ofp11_port_h, OFPP_MAX - 1,
3280 (uint32_t) OFPP11_MAX, UINT32_MAX);
3281 return OFPERR_OFPBAC_BAD_OUT_PORT;
3282 }
3283}
3284
3285/* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
3286 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
3287 *
3288 * See the definition of OFP11_MAX for an explanation of the mapping. */
3289ovs_be32
3290ofputil_port_to_ofp11(uint16_t ofp10_port)
3291{
3292 return htonl(ofp10_port < OFPP_MAX
3293 ? ofp10_port
3294 : ofp10_port + OFPP11_OFFSET);
3295}
3296
08f94c0e 3297/* Checks that 'port' is a valid output port for the OFPAT10_OUTPUT action, given
c1c9c9c4 3298 * that the switch will never have more than 'max_ports' ports. Returns 0 if
90bf1e07
BP
3299 * 'port' is valid, otherwise an OpenFlow return code. */
3300enum ofperr
77410139 3301ofputil_check_output_port(uint16_t port, int max_ports)
fa37b408
BP
3302{
3303 switch (port) {
3304 case OFPP_IN_PORT:
3305 case OFPP_TABLE:
3306 case OFPP_NORMAL:
3307 case OFPP_FLOOD:
3308 case OFPP_ALL:
3309 case OFPP_CONTROLLER:
0d193212 3310 case OFPP_NONE:
fa37b408
BP
3311 case OFPP_LOCAL:
3312 return 0;
3313
3314 default:
c1c9c9c4 3315 if (port < max_ports) {
fa37b408
BP
3316 return 0;
3317 }
90bf1e07 3318 return OFPERR_OFPBAC_BAD_OUT_PORT;
fa37b408
BP
3319 }
3320}
3321
39dc9082
BP
3322#define OFPUTIL_NAMED_PORTS \
3323 OFPUTIL_NAMED_PORT(IN_PORT) \
3324 OFPUTIL_NAMED_PORT(TABLE) \
3325 OFPUTIL_NAMED_PORT(NORMAL) \
3326 OFPUTIL_NAMED_PORT(FLOOD) \
3327 OFPUTIL_NAMED_PORT(ALL) \
3328 OFPUTIL_NAMED_PORT(CONTROLLER) \
3329 OFPUTIL_NAMED_PORT(LOCAL) \
3330 OFPUTIL_NAMED_PORT(NONE)
3331
3332/* Checks whether 's' is the string representation of an OpenFlow port number,
3333 * either as an integer or a string name (e.g. "LOCAL"). If it is, stores the
3334 * number in '*port' and returns true. Otherwise, returns false. */
3335bool
3336ofputil_port_from_string(const char *name, uint16_t *port)
3337{
3338 struct pair {
3339 const char *name;
3340 uint16_t value;
3341 };
3342 static const struct pair pairs[] = {
3343#define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
3344 OFPUTIL_NAMED_PORTS
3345#undef OFPUTIL_NAMED_PORT
3346 };
3347 static const int n_pairs = ARRAY_SIZE(pairs);
3348 int i;
3349
3350 if (str_to_int(name, 0, &i) && i >= 0 && i < UINT16_MAX) {
3351 *port = i;
3352 return true;
3353 }
3354
3355 for (i = 0; i < n_pairs; i++) {
3356 if (!strcasecmp(name, pairs[i].name)) {
3357 *port = pairs[i].value;
3358 return true;
3359 }
3360 }
3361 return false;
3362}
3363
3364/* Appends to 's' a string representation of the OpenFlow port number 'port'.
3365 * Most ports' string representation is just the port number, but for special
3366 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
3367void
3368ofputil_format_port(uint16_t port, struct ds *s)
3369{
3370 const char *name;
3371
3372 switch (port) {
3373#define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
3374 OFPUTIL_NAMED_PORTS
3375#undef OFPUTIL_NAMED_PORT
3376
3377 default:
3378 ds_put_format(s, "%"PRIu16, port);
3379 return;
3380 }
3381 ds_put_cstr(s, name);
3382}
3383
2be393ed
JP
3384/* Given a buffer 'b' that contains an array of OpenFlow ports of type
3385 * 'ofp_version', tries to pull the first element from the array. If
3386 * successful, initializes '*pp' with an abstract representation of the
3387 * port and returns 0. If no ports remain to be decoded, returns EOF.
3388 * On an error, returns a positive OFPERR_* value. */
3389int
3390ofputil_pull_phy_port(uint8_t ofp_version, struct ofpbuf *b,
3391 struct ofputil_phy_port *pp)
3392{
3393 if (ofp_version == OFP10_VERSION) {
3394 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
3395 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
3396 } else {
3397 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
3398 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
3399 }
3400}
3401
3402/* Given a buffer 'b' that contains an array of OpenFlow ports of type
3403 * 'ofp_version', returns the number of elements. */
3404size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *b)
3405{
5b5a3a67 3406 return b->size / ofputil_get_phy_port_size(ofp_version);
2be393ed
JP
3407}
3408
90bf1e07 3409static enum ofperr
29901626
BP
3410check_resubmit_table(const struct nx_action_resubmit *nar)
3411{
3412 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
90bf1e07 3413 return OFPERR_OFPBAC_BAD_ARGUMENT;
29901626
BP
3414 }
3415 return 0;
3416}
3417
90bf1e07 3418static enum ofperr
f694937d
EJ
3419check_output_reg(const struct nx_action_output_reg *naor,
3420 const struct flow *flow)
3421{
816fd533 3422 struct mf_subfield src;
f694937d
EJ
3423 size_t i;
3424
3425 for (i = 0; i < sizeof naor->zero; i++) {
3426 if (naor->zero[i]) {
90bf1e07 3427 return OFPERR_OFPBAC_BAD_ARGUMENT;
f694937d
EJ
3428 }
3429 }
3430
816fd533
BP
3431 nxm_decode(&src, naor->src, naor->ofs_nbits);
3432 return mf_check_src(&src, flow);
f694937d
EJ
3433}
3434
90bf1e07 3435enum ofperr
38f2e360
BP
3436validate_actions(const union ofp_action *actions, size_t n_actions,
3437 const struct flow *flow, int max_ports)
c1c9c9c4 3438{
38f2e360
BP
3439 const union ofp_action *a;
3440 size_t left;
c1c9c9c4 3441
38f2e360 3442 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
90bf1e07 3443 enum ofperr error;
38f2e360 3444 uint16_t port;
38f2e360 3445 int code;
c1c9c9c4 3446
38f2e360
BP
3447 code = ofputil_decode_action(a);
3448 if (code < 0) {
38f2e360 3449 error = -code;
38f2e360
BP
3450 VLOG_WARN_RL(&bad_ofmsg_rl,
3451 "action decoding error at offset %td (%s)",
90bf1e07 3452 (a - actions) * sizeof *a, ofperr_get_name(error));
fa37b408 3453
38f2e360
BP
3454 return error;
3455 }
fa37b408 3456
38f2e360
BP
3457 error = 0;
3458 switch ((enum ofputil_action_code) code) {
08f94c0e 3459 case OFPUTIL_OFPAT10_OUTPUT:
77410139
EJ
3460 error = ofputil_check_output_port(ntohs(a->output.port),
3461 max_ports);
38f2e360 3462 break;
e41a9130 3463
08f94c0e 3464 case OFPUTIL_OFPAT10_SET_VLAN_VID:
38f2e360 3465 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
90bf1e07 3466 error = OFPERR_OFPBAC_BAD_ARGUMENT;
38f2e360
BP
3467 }
3468 break;
96fc46e8 3469
08f94c0e 3470 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
38f2e360 3471 if (a->vlan_pcp.vlan_pcp & ~7) {
90bf1e07 3472 error = OFPERR_OFPBAC_BAD_ARGUMENT;
38f2e360
BP
3473 }
3474 break;
96fc46e8 3475
08f94c0e 3476 case OFPUTIL_OFPAT10_ENQUEUE:
38f2e360 3477 port = ntohs(((const struct ofp_action_enqueue *) a)->port);
82172632
EJ
3478 if (port >= max_ports && port != OFPP_IN_PORT
3479 && port != OFPP_LOCAL) {
90bf1e07 3480 error = OFPERR_OFPBAC_BAD_OUT_PORT;
38f2e360
BP
3481 }
3482 break;
96fc46e8 3483
38f2e360
BP
3484 case OFPUTIL_NXAST_REG_MOVE:
3485 error = nxm_check_reg_move((const struct nx_action_reg_move *) a,
3486 flow);
3487 break;
96fc46e8 3488
38f2e360
BP
3489 case OFPUTIL_NXAST_REG_LOAD:
3490 error = nxm_check_reg_load((const struct nx_action_reg_load *) a,
3491 flow);
3492 break;
b9298d3f 3493
38f2e360 3494 case OFPUTIL_NXAST_MULTIPATH:
43edca57
EJ
3495 error = multipath_check((const struct nx_action_multipath *) a,
3496 flow);
38f2e360
BP
3497 break;
3498
3499 case OFPUTIL_NXAST_AUTOPATH:
43edca57
EJ
3500 error = autopath_check((const struct nx_action_autopath *) a,
3501 flow);
38f2e360
BP
3502 break;
3503
daff3353 3504 case OFPUTIL_NXAST_BUNDLE:
a368bb53 3505 case OFPUTIL_NXAST_BUNDLE_LOAD:
daff3353 3506 error = bundle_check((const struct nx_action_bundle *) a,
a368bb53 3507 max_ports, flow);
daff3353
EJ
3508 break;
3509
f694937d
EJ
3510 case OFPUTIL_NXAST_OUTPUT_REG:
3511 error = check_output_reg((const struct nx_action_output_reg *) a,
3512 flow);
3513 break;
3514
29901626
BP
3515 case OFPUTIL_NXAST_RESUBMIT_TABLE:
3516 error = check_resubmit_table(
3517 (const struct nx_action_resubmit *) a);
3518 break;
3519
75a75043
BP
3520 case OFPUTIL_NXAST_LEARN:
3521 error = learn_check((const struct nx_action_learn *) a, flow);
3522 break;
3523
a7349929
BP
3524 case OFPUTIL_NXAST_CONTROLLER:
3525 if (((const struct nx_action_controller *) a)->zero) {
3526 error = OFPERR_NXBAC_MUST_BE_ZERO;
3527 }
3528 break;
3529
08f94c0e
BP
3530 case OFPUTIL_OFPAT10_STRIP_VLAN:
3531 case OFPUTIL_OFPAT10_SET_NW_SRC:
3532 case OFPUTIL_OFPAT10_SET_NW_DST:
3533 case OFPUTIL_OFPAT10_SET_NW_TOS:
3534 case OFPUTIL_OFPAT10_SET_TP_SRC:
3535 case OFPUTIL_OFPAT10_SET_TP_DST:
3536 case OFPUTIL_OFPAT10_SET_DL_SRC:
3537 case OFPUTIL_OFPAT10_SET_DL_DST:
38f2e360
BP
3538 case OFPUTIL_NXAST_RESUBMIT:
3539 case OFPUTIL_NXAST_SET_TUNNEL:
3540 case OFPUTIL_NXAST_SET_QUEUE:
3541 case OFPUTIL_NXAST_POP_QUEUE:
3542 case OFPUTIL_NXAST_NOTE:
3543 case OFPUTIL_NXAST_SET_TUNNEL64:
848e8809 3544 case OFPUTIL_NXAST_EXIT:
f0fd1a17 3545 case OFPUTIL_NXAST_DEC_TTL:
0e553d9c 3546 case OFPUTIL_NXAST_FIN_TIMEOUT:
38f2e360 3547 break;
53ddd40a 3548 }
53ddd40a 3549
3b6a2571 3550 if (error) {
38f2e360 3551 VLOG_WARN_RL(&bad_ofmsg_rl, "bad action at offset %td (%s)",
90bf1e07 3552 (a - actions) * sizeof *a, ofperr_get_name(error));
3b6a2571
EJ
3553 return error;
3554 }
fa37b408 3555 }
38f2e360
BP
3556 if (left) {
3557 VLOG_WARN_RL(&bad_ofmsg_rl, "bad action format at offset %zu",
3558 (n_actions - left) * sizeof *a);
90bf1e07 3559 return OFPERR_OFPBAC_BAD_LEN;
38f2e360
BP
3560 }
3561 return 0;
fa37b408
BP
3562}
3563
51cb6e0c
BP
3564struct ofputil_action {
3565 int code;
38f2e360
BP
3566 unsigned int min_len;
3567 unsigned int max_len;
3568};
27bcf966 3569
51cb6e0c 3570static const struct ofputil_action action_bad_type
90bf1e07 3571 = { -OFPERR_OFPBAC_BAD_TYPE, 0, UINT_MAX };
51cb6e0c 3572static const struct ofputil_action action_bad_len
90bf1e07 3573 = { -OFPERR_OFPBAC_BAD_LEN, 0, UINT_MAX };
51cb6e0c 3574static const struct ofputil_action action_bad_vendor
90bf1e07 3575 = { -OFPERR_OFPBAC_BAD_VENDOR, 0, UINT_MAX };
27bcf966 3576
51cb6e0c 3577static const struct ofputil_action *
38f2e360
BP
3578ofputil_decode_ofpat_action(const union ofp_action *a)
3579{
08f94c0e 3580 enum ofp10_action_type type = ntohs(a->type);
fa37b408 3581
51cb6e0c 3582 switch (type) {
08f94c0e 3583#define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
51cb6e0c
BP
3584 case ENUM: { \
3585 static const struct ofputil_action action = { \
e23ae585
BP
3586 OFPUTIL_##ENUM, \
3587 sizeof(struct STRUCT), \
3588 sizeof(struct STRUCT) \
51cb6e0c
BP
3589 }; \
3590 return &action; \
3591 }
e23ae585 3592#include "ofp-util.def"
51cb6e0c 3593
08f94c0e 3594 case OFPAT10_VENDOR:
51cb6e0c
BP
3595 default:
3596 return &action_bad_type;
38f2e360
BP
3597 }
3598}
fa37b408 3599
51cb6e0c 3600static const struct ofputil_action *
38f2e360
BP
3601ofputil_decode_nxast_action(const union ofp_action *a)
3602{
51cb6e0c
BP
3603 const struct nx_action_header *nah = (const struct nx_action_header *) a;
3604 enum nx_action_subtype subtype = ntohs(nah->subtype);
3605
3606 switch (subtype) {
e23ae585
BP
3607#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3608 case ENUM: { \
3609 static const struct ofputil_action action = { \
3610 OFPUTIL_##ENUM, \
3611 sizeof(struct STRUCT), \
3612 EXTENSIBLE ? UINT_MAX : sizeof(struct STRUCT) \
3613 }; \
3614 return &action; \
38f2e360 3615 }
e23ae585 3616#include "ofp-util.def"
51cb6e0c
BP
3617
3618 case NXAST_SNAT__OBSOLETE:
3619 case NXAST_DROP_SPOOFED_ARP__OBSOLETE:
3620 default:
3621 return &action_bad_type;
fa37b408
BP
3622 }
3623}
3624
08f94c0e 3625/* Parses 'a' to determine its type. Returns a nonnegative OFPUTIL_OFPAT10_* or
90bf1e07
BP
3626 * OFPUTIL_NXAST_* constant if successful, otherwise a negative OFPERR_* error
3627 * code.
38f2e360
BP
3628 *
3629 * The caller must have already verified that 'a''s length is correct (that is,
3630 * a->header.len is nonzero and a multiple of sizeof(union ofp_action) and no
3631 * longer than the amount of space allocated to 'a').
3632 *
3633 * This function verifies that 'a''s length is correct for the type of action
3634 * that it represents. */
fa37b408 3635int
38f2e360 3636ofputil_decode_action(const union ofp_action *a)
fa37b408 3637{
51cb6e0c
BP
3638 const struct ofputil_action *action;
3639 uint16_t len = ntohs(a->header.len);
3640
08f94c0e 3641 if (a->type != htons(OFPAT10_VENDOR)) {
51cb6e0c 3642 action = ofputil_decode_ofpat_action(a);
38f2e360 3643 } else {
51cb6e0c
BP
3644 switch (ntohl(a->vendor.vendor)) {
3645 case NX_VENDOR_ID:
3646 if (len < sizeof(struct nx_action_header)) {
90bf1e07 3647 return -OFPERR_OFPBAC_BAD_LEN;
51cb6e0c
BP
3648 }
3649 action = ofputil_decode_nxast_action(a);
3650 break;
3651 default:
3652 action = &action_bad_vendor;
3653 break;
3654 }
38f2e360 3655 }
51cb6e0c
BP
3656
3657 return (len >= action->min_len && len <= action->max_len
3658 ? action->code
90bf1e07 3659 : -OFPERR_OFPBAC_BAD_LEN);
38f2e360 3660}
fa37b408 3661
08f94c0e 3662/* Parses 'a' and returns its type as an OFPUTIL_OFPAT10_* or OFPUTIL_NXAST_*
38f2e360
BP
3663 * constant. The caller must have already validated that 'a' is a valid action
3664 * understood by Open vSwitch (e.g. by a previous successful call to
3665 * ofputil_decode_action()). */
3666enum ofputil_action_code
3667ofputil_decode_action_unsafe(const union ofp_action *a)
3668{
51cb6e0c
BP
3669 const struct ofputil_action *action;
3670
08f94c0e 3671 if (a->type != htons(OFPAT10_VENDOR)) {
51cb6e0c 3672 action = ofputil_decode_ofpat_action(a);
38f2e360 3673 } else {
51cb6e0c 3674 action = ofputil_decode_nxast_action(a);
fa37b408 3675 }
51cb6e0c
BP
3676
3677 return action->code;
fa37b408
BP
3678}
3679
e23ae585 3680/* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
08f94c0e 3681 * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1 if
e23ae585
BP
3682 * 'name' is not the name of any action.
3683 *
3684 * ofp-util.def lists the mapping from names to action. */
3685int
3686ofputil_action_code_from_name(const char *name)
3687{
3688 static const char *names[OFPUTIL_N_ACTIONS] = {
08f94c0e 3689#define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME,
e23ae585
BP
3690#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
3691#include "ofp-util.def"
3692 };
3693
3694 const char **p;
3695
3696 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
3697 if (*p && !strcasecmp(name, *p)) {
3698 return p - names;
3699 }
3700 }
3701 return -1;
3702}
3703
93996add
BP
3704/* Appends an action of the type specified by 'code' to 'buf' and returns the
3705 * action. Initializes the parts of 'action' that identify it as having type
3706 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
3707 * have variable length, the length used and cleared is that of struct
3708 * <STRUCT>. */
3709void *
3710ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
3711{
3712 switch (code) {
08f94c0e 3713#define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
93996add
BP
3714 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3715#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3716 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3717#include "ofp-util.def"
3718 }
3719 NOT_REACHED();
3720}
3721
08f94c0e 3722#define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
93996add
BP
3723 void \
3724 ofputil_init_##ENUM(struct STRUCT *s) \
3725 { \
3726 memset(s, 0, sizeof *s); \
3727 s->type = htons(ENUM); \
3728 s->len = htons(sizeof *s); \
3729 } \
3730 \
3731 struct STRUCT * \
3732 ofputil_put_##ENUM(struct ofpbuf *buf) \
3733 { \
3734 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3735 ofputil_init_##ENUM(s); \
3736 return s; \
3737 }
3738#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3739 void \
3740 ofputil_init_##ENUM(struct STRUCT *s) \
3741 { \
3742 memset(s, 0, sizeof *s); \
08f94c0e 3743 s->type = htons(OFPAT10_VENDOR); \
93996add
BP
3744 s->len = htons(sizeof *s); \
3745 s->vendor = htonl(NX_VENDOR_ID); \
3746 s->subtype = htons(ENUM); \
3747 } \
3748 \
3749 struct STRUCT * \
3750 ofputil_put_##ENUM(struct ofpbuf *buf) \
3751 { \
3752 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3753 ofputil_init_##ENUM(s); \
3754 return s; \
3755 }
3756#include "ofp-util.def"
3757
dbba996b 3758/* Returns true if 'action' outputs to 'port', false otherwise. */
c1c9c9c4 3759bool
dbba996b 3760action_outputs_to_port(const union ofp_action *action, ovs_be16 port)
c1c9c9c4 3761{
145fd580
BP
3762 switch (ofputil_decode_action(action)) {
3763 case OFPUTIL_OFPAT10_OUTPUT:
c1c9c9c4 3764 return action->output.port == port;
145fd580 3765 case OFPUTIL_OFPAT10_ENQUEUE:
c1c9c9c4 3766 return ((const struct ofp_action_enqueue *) action)->port == port;
145fd580
BP
3767 case OFPUTIL_NXAST_CONTROLLER:
3768 return port == htons(OFPP_CONTROLLER);
c1c9c9c4
BP
3769 default:
3770 return false;
3771 }
3772}
3773
b459a924
BP
3774/* "Normalizes" the wildcards in 'rule'. That means:
3775 *
3776 * 1. If the type of level N is known, then only the valid fields for that
3777 * level may be specified. For example, ARP does not have a TOS field,
3778 * so nw_tos must be wildcarded if 'rule' specifies an ARP flow.
3779 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
3780 * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an
3781 * IPv4 flow.
3782 *
3783 * 2. If the type of level N is not known (or not understood by Open
3784 * vSwitch), then no fields at all for that level may be specified. For
3785 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
3786 * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an
3787 * SCTP flow.
27527aa0 3788 */
b459a924 3789void
27527aa0 3790ofputil_normalize_rule(struct cls_rule *rule)
b459a924
BP
3791{
3792 enum {
3793 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
3794 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
3795 MAY_NW_PROTO = 1 << 2, /* nw_proto */
a61680c6 3796 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
b459a924
BP
3797 MAY_ARP_SHA = 1 << 4, /* arp_sha */
3798 MAY_ARP_THA = 1 << 5, /* arp_tha */
d78477ec 3799 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
b459a924
BP
3800 MAY_ND_TARGET = 1 << 7 /* nd_target */
3801 } may_match;
3802
3803 struct flow_wildcards wc;
3804
3805 /* Figure out what fields may be matched. */
3806 if (rule->flow.dl_type == htons(ETH_TYPE_IP)) {
9e44d715 3807 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
b459a924
BP
3808 if (rule->flow.nw_proto == IPPROTO_TCP ||
3809 rule->flow.nw_proto == IPPROTO_UDP ||
3810 rule->flow.nw_proto == IPPROTO_ICMP) {
3811 may_match |= MAY_TP_ADDR;
3812 }
27527aa0 3813 } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)) {
d78477ec 3814 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
b459a924
BP
3815 if (rule->flow.nw_proto == IPPROTO_TCP ||
3816 rule->flow.nw_proto == IPPROTO_UDP) {
3817 may_match |= MAY_TP_ADDR;
3818 } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) {
3819 may_match |= MAY_TP_ADDR;
3820 if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
3821 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
3822 } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
3823 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
3824 }
3825 }
3826 } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) {
27527aa0 3827 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
1c0b7503 3828 } else {
b459a924
BP
3829 may_match = 0;
3830 }
3831
3832 /* Clear the fields that may not be matched. */
3833 wc = rule->wc;
3834 if (!(may_match & MAY_NW_ADDR)) {
3835 wc.nw_src_mask = wc.nw_dst_mask = htonl(0);
3836 }
3837 if (!(may_match & MAY_TP_ADDR)) {
73f33563 3838 wc.tp_src_mask = wc.tp_dst_mask = htons(0);
b459a924
BP
3839 }
3840 if (!(may_match & MAY_NW_PROTO)) {
3841 wc.wildcards |= FWW_NW_PROTO;
3842 }
9e44d715 3843 if (!(may_match & MAY_IPVx)) {
2486e66a
JP
3844 wc.wildcards |= FWW_NW_DSCP;
3845 wc.wildcards |= FWW_NW_ECN;
a61680c6 3846 wc.wildcards |= FWW_NW_TTL;
b459a924
BP
3847 }
3848 if (!(may_match & MAY_ARP_SHA)) {
3849 wc.wildcards |= FWW_ARP_SHA;
3850 }
3851 if (!(may_match & MAY_ARP_THA)) {
3852 wc.wildcards |= FWW_ARP_THA;
3853 }
d78477ec 3854 if (!(may_match & MAY_IPV6)) {
b459a924 3855 wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any;
fa8223b7 3856 wc.wildcards |= FWW_IPV6_LABEL;
b459a924
BP
3857 }
3858 if (!(may_match & MAY_ND_TARGET)) {
47284b1f 3859 wc.nd_target_mask = in6addr_any;
b459a924
BP
3860 }
3861
3862 /* Log any changes. */
3863 if (!flow_wildcards_equal(&wc, &rule->wc)) {
3864 bool log = !VLOG_DROP_INFO(&bad_ofmsg_rl);
3865 char *pre = log ? cls_rule_to_string(rule) : NULL;
3866
3867 rule->wc = wc;
3868 cls_rule_zero_wildcarded_fields(rule);
3869
3870 if (log) {
3871 char *post = cls_rule_to_string(rule);
3872 VLOG_INFO("normalization changed ofp_match, details:");
3873 VLOG_INFO(" pre: %s", pre);
3874 VLOG_INFO("post: %s", post);
3875 free(pre);
3876 free(post);
3877 }
fa37b408 3878 }
3f09c339 3879}
26c112c2 3880
3052b0c5
BP
3881/* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
3882 * successful, otherwise an OpenFlow error.
3883 *
3884 * If successful, the first action is stored in '*actionsp' and the number of
3885 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
3886 * are stored, respectively.
3887 *
3888 * This function does not check that the actions are valid (the caller should
3889 * do so, with validate_actions()). The caller is also responsible for making
3890 * sure that 'b->data' is initially aligned appropriately for "union
3891 * ofp_action". */
90bf1e07 3892enum ofperr
3052b0c5
BP
3893ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
3894 union ofp_action **actionsp, size_t *n_actionsp)
3895{
69b6be19 3896 if (actions_len % OFP_ACTION_ALIGN != 0) {
f4350529
BP
3897 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
3898 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
3052b0c5
BP
3899 goto error;
3900 }
3901
3902 *actionsp = ofpbuf_try_pull(b, actions_len);
3903 if (*actionsp == NULL) {
f4350529
BP
3904 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
3905 "exceeds remaining message length (%zu)",
3906 actions_len, b->size);
3052b0c5
BP
3907 goto error;
3908 }
3909
69b6be19 3910 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
3052b0c5
BP
3911 return 0;
3912
3913error:
3914 *actionsp = NULL;
3915 *n_actionsp = 0;
90bf1e07 3916 return OFPERR_OFPBRC_BAD_LEN;
3052b0c5 3917}
18ddadc2
BP
3918
3919bool
3920ofputil_actions_equal(const union ofp_action *a, size_t n_a,
3921 const union ofp_action *b, size_t n_b)
3922{
3923 return n_a == n_b && (!n_a || !memcmp(a, b, n_a * sizeof *a));
3924}
3925
3926union ofp_action *
3927ofputil_actions_clone(const union ofp_action *actions, size_t n)
3928{
3929 return n ? xmemdup(actions, n * sizeof *actions) : NULL;
3930}
0ff22822
BP
3931
3932/* Parses a key or a key-value pair from '*stringp'.
3933 *
3934 * On success: Stores the key into '*keyp'. Stores the value, if present, into
3935 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
3936 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
3937 * are substrings of '*stringp' created by replacing some of its bytes by null
3938 * terminators. Returns true.
3939 *
3940 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
3941 * NULL and returns false. */
3942bool
3943ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
3944{
3945 char *pos, *key, *value;
3946 size_t key_len;
3947
3948 pos = *stringp;
3949 pos += strspn(pos, ", \t\r\n");
3950 if (*pos == '\0') {
3951 *keyp = *valuep = NULL;
3952 return false;
3953 }
3954
3955 key = pos;
3956 key_len = strcspn(pos, ":=(, \t\r\n");
3957 if (key[key_len] == ':' || key[key_len] == '=') {
3958 /* The value can be separated by a colon. */
3959 size_t value_len;
3960
3961 value = key + key_len + 1;
3962 value_len = strcspn(value, ", \t\r\n");
3963 pos = value + value_len + (value[value_len] != '\0');
3964 value[value_len] = '\0';
3965 } else if (key[key_len] == '(') {
3966 /* The value can be surrounded by balanced parentheses. The outermost
3967 * set of parentheses is removed. */
3968 int level = 1;
3969 size_t value_len;
3970
3971 value = key + key_len + 1;
3972 for (value_len = 0; level > 0; value_len++) {
3973 switch (value[value_len]) {
3974 case '\0':
33cadc50
BP
3975 level = 0;
3976 break;
0ff22822
BP
3977
3978 case '(':
3979 level++;
3980 break;
3981
3982 case ')':
3983 level--;
3984 break;
3985 }
3986 }
3987 value[value_len - 1] = '\0';
3988 pos = value + value_len;
3989 } else {
3990 /* There might be no value at all. */
3991 value = key + key_len; /* Will become the empty string below. */
3992 pos = key + key_len + (key[key_len] != '\0');
3993 }
3994 key[key_len] = '\0';
3995
3996 *stringp = pos;
3997 *keyp = key;
3998 *valuep = value;
3999 return true;
4000}