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