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