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