]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ofp-util.c
openflow: Merge ofp_stats_request and ofp_stats_reply.
[mirror_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>
b459a924 21#include <netinet/icmp6.h>
fa37b408 22#include <stdlib.h>
3b6a2571 23#include "autopath.h"
10a24935 24#include "byte-order.h"
d8ae4d67 25#include "classifier.h"
dc4762ed 26#include "dynamic-string.h"
53ddd40a 27#include "multipath.h"
b6c9e612 28#include "nx-match.h"
dc4762ed 29#include "ofp-errors.h"
fa37b408
BP
30#include "ofp-util.h"
31#include "ofpbuf.h"
32#include "packets.h"
33#include "random.h"
4ffd1b43 34#include "unaligned.h"
e41a9130 35#include "type-props.h"
5136ce49 36#include "vlog.h"
fa37b408 37
d98e6007 38VLOG_DEFINE_THIS_MODULE(ofp_util);
fa37b408
BP
39
40/* Rate limit for OpenFlow message parse errors. These always indicate a bug
41 * in the peer and so there's not much point in showing a lot of them. */
42static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
43
0596e897
BP
44/* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
45 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
46 * is wildcarded.
47 *
48 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
49 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
50 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
51 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
52 * wildcarded. */
53ovs_be32
54ofputil_wcbits_to_netmask(int wcbits)
55{
56 wcbits &= 0x3f;
57 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
58}
59
60/* Given the IP netmask 'netmask', returns the number of bits of the IP address
61 * that it wildcards. 'netmask' must be a CIDR netmask (see ip_is_cidr()). */
62int
63ofputil_netmask_to_wcbits(ovs_be32 netmask)
64{
65 assert(ip_is_cidr(netmask));
66#if __GNUC__ >= 4
67 return netmask == htonl(0) ? 32 : __builtin_ctz(ntohl(netmask));
68#else
69 int wcbits;
70
71 for (wcbits = 32; netmask; wcbits--) {
72 netmask &= netmask - 1;
73 }
74
75 return wcbits;
76#endif
77}
78
d8ae4d67
BP
79/* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
80 * name. */
81#define WC_INVARIANT_LIST \
82 WC_INVARIANT_BIT(IN_PORT) \
d8ae4d67
BP
83 WC_INVARIANT_BIT(DL_SRC) \
84 WC_INVARIANT_BIT(DL_DST) \
85 WC_INVARIANT_BIT(DL_TYPE) \
86 WC_INVARIANT_BIT(NW_PROTO) \
87 WC_INVARIANT_BIT(TP_SRC) \
88 WC_INVARIANT_BIT(TP_DST)
89
90/* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
91 * actually have the same names and values. */
92#define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME);
93 WC_INVARIANT_LIST
94#undef WC_INVARIANT_BIT
95
96/* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
97 * OR'd together. */
eeba8e4f 98static const flow_wildcards_t WC_INVARIANTS = 0
d8ae4d67
BP
99#define WC_INVARIANT_BIT(NAME) | FWW_##NAME
100 WC_INVARIANT_LIST
101#undef WC_INVARIANT_BIT
eeba8e4f 102;
d8ae4d67 103
eb6f28db
BP
104/* Converts the wildcard in 'ofpfw' into a flow_wildcards in 'wc' for use in
105 * struct cls_rule. It is the caller's responsibility to handle the special
106 * case where the flow match's dl_vlan is set to OFP_VLAN_NONE. */
7286b1e1 107void
eb6f28db 108ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *wc)
d8ae4d67 109{
d8ae4d67 110 /* Initialize most of rule->wc. */
f9ba8dad 111 flow_wildcards_init_catchall(wc);
eeba8e4f 112 wc->wildcards = (OVS_FORCE flow_wildcards_t) ofpfw & WC_INVARIANTS;
bad68a99
JP
113
114 /* Wildcard fields that aren't defined by ofp_match or tun_id. */
685a51a5 115 wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_ND_TARGET);
bad68a99 116
d8ae4d67
BP
117 if (ofpfw & OFPFW_NW_TOS) {
118 wc->wildcards |= FWW_NW_TOS;
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);
160 rule->flow.nw_tos = match->nw_tos;
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;
d8ae4d67
BP
199 if (wc->wildcards & FWW_NW_TOS) {
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;
233 match->nw_tos = rule->flow.nw_tos;
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
282struct ofputil_msg_category {
283 const char *name; /* e.g. "OpenFlow message" */
284 const struct ofputil_msg_type *types;
285 size_t n_types;
286 int missing_error; /* ofp_mkerr() value for missing type. */
287};
288
289static bool
290ofputil_length_ok(const struct ofputil_msg_category *cat,
291 const struct ofputil_msg_type *type,
292 unsigned int size)
293{
294 switch (type->extra_multiple) {
295 case 0:
296 if (size != type->min_size) {
297 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
298 "length %u (expected length %u)",
299 cat->name, type->name, size, type->min_size);
300 return false;
301 }
302 return true;
303
304 case 1:
305 if (size < type->min_size) {
306 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
307 "length %u (expected length at least %u bytes)",
308 cat->name, type->name, size, type->min_size);
309 return false;
310 }
311 return true;
312
313 default:
314 if (size < type->min_size
315 || (size - type->min_size) % type->extra_multiple) {
316 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
317 "length %u (must be exactly %u bytes or longer "
318 "by an integer multiple of %u bytes)",
319 cat->name, type->name, size,
320 type->min_size, type->extra_multiple);
321 return false;
322 }
323 return true;
324 }
325}
326
327static int
328ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
329 uint32_t value, unsigned int size,
330 const struct ofputil_msg_type **typep)
331{
332 const struct ofputil_msg_type *type;
333
334 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
335 if (type->value == value) {
336 if (!ofputil_length_ok(cat, type, size)) {
337 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
338 }
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
350ofputil_decode_vendor(const struct ofp_header *oh,
351 const struct ofputil_msg_type **typep)
352{
6c1491fb
BP
353 BUILD_ASSERT_DECL(sizeof(struct nxt_set_flow_format)
354 != sizeof(struct nxt_flow_mod_table_id));
355
d1e2cf21 356 static const struct ofputil_msg_type nxt_messages[] = {
d1e2cf21
BP
357 { OFPUTIL_NXT_ROLE_REQUEST,
358 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
359 sizeof(struct nx_role_request), 0 },
360
361 { OFPUTIL_NXT_ROLE_REPLY,
362 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
363 sizeof(struct nx_role_request), 0 },
364
365 { OFPUTIL_NXT_SET_FLOW_FORMAT,
366 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
367 sizeof(struct nxt_set_flow_format), 0 },
368
369 { OFPUTIL_NXT_FLOW_MOD,
370 NXT_FLOW_MOD, "NXT_FLOW_MOD",
371 sizeof(struct nx_flow_mod), 8 },
372
373 { OFPUTIL_NXT_FLOW_REMOVED,
374 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
375 sizeof(struct nx_flow_removed), 8 },
d1e9b9bf
BP
376
377 { OFPUTIL_NXT_FLOW_MOD_TABLE_ID,
378 NXT_FLOW_MOD_TABLE_ID, "NXT_FLOW_MOD_TABLE_ID",
379 sizeof(struct nxt_flow_mod_table_id), 0 },
d1e2cf21
BP
380 };
381
382 static const struct ofputil_msg_category nxt_category = {
383 "Nicira extension message",
384 nxt_messages, ARRAY_SIZE(nxt_messages),
385 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
386 };
387
388 const struct ofp_vendor_header *ovh;
389 const struct nicira_header *nh;
390
391 ovh = (const struct ofp_vendor_header *) oh;
392 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
393 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
394 "vendor %"PRIx32, ntohl(ovh->vendor));
395 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
396 }
397
398 if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
399 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
400 "length %u (expected at least %zu)",
401 ntohs(ovh->header.length), sizeof(struct nicira_header));
402 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
403 }
404
405 nh = (const struct nicira_header *) oh;
406 return ofputil_lookup_openflow_message(&nxt_category, ntohl(nh->subtype),
407 ntohs(oh->length), typep);
408}
409
410static int
411check_nxstats_msg(const struct ofp_header *oh)
412{
28c8bad1 413 const struct ofp_stats_msg *osm = (const struct ofp_stats_msg *) oh;
d1e2cf21
BP
414 ovs_be32 vendor;
415
28c8bad1 416 memcpy(&vendor, osm->body, sizeof vendor);
d1e2cf21
BP
417 if (vendor != htonl(NX_VENDOR_ID)) {
418 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
419 "unknown vendor %"PRIx32, ntohl(vendor));
420 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
421 }
422
28c8bad1 423 if (ntohs(osm->header.length) < sizeof(struct nicira_stats_msg)) {
d1e2cf21
BP
424 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
425 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
426 }
427
428 return 0;
429}
430
431static int
432ofputil_decode_nxst_request(const struct ofp_header *oh,
433 const struct ofputil_msg_type **typep)
434{
435 static const struct ofputil_msg_type nxst_requests[] = {
436 { OFPUTIL_NXST_FLOW_REQUEST,
437 NXST_FLOW, "NXST_FLOW request",
438 sizeof(struct nx_flow_stats_request), 8 },
439
440 { OFPUTIL_NXST_AGGREGATE_REQUEST,
441 NXST_AGGREGATE, "NXST_AGGREGATE request",
442 sizeof(struct nx_aggregate_stats_request), 8 },
443 };
444
445 static const struct ofputil_msg_category nxst_request_category = {
08717852 446 "Nicira extension statistics request",
d1e2cf21
BP
447 nxst_requests, ARRAY_SIZE(nxst_requests),
448 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
449 };
450
451 const struct nicira_stats_msg *nsm;
452 int error;
453
454 error = check_nxstats_msg(oh);
455 if (error) {
456 return error;
457 }
458
459 nsm = (struct nicira_stats_msg *) oh;
460 return ofputil_lookup_openflow_message(&nxst_request_category,
461 ntohl(nsm->subtype),
462 ntohs(oh->length), typep);
463}
464
465static int
466ofputil_decode_nxst_reply(const struct ofp_header *oh,
467 const struct ofputil_msg_type **typep)
468{
469 static const struct ofputil_msg_type nxst_replies[] = {
470 { OFPUTIL_NXST_FLOW_REPLY,
471 NXST_FLOW, "NXST_FLOW reply",
472 sizeof(struct nicira_stats_msg), 8 },
473
474 { OFPUTIL_NXST_AGGREGATE_REPLY,
475 NXST_AGGREGATE, "NXST_AGGREGATE reply",
476 sizeof(struct nx_aggregate_stats_reply), 0 },
477 };
478
479 static const struct ofputil_msg_category nxst_reply_category = {
08717852 480 "Nicira extension statistics reply",
d1e2cf21
BP
481 nxst_replies, ARRAY_SIZE(nxst_replies),
482 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
483 };
484
485 const struct nicira_stats_msg *nsm;
486 int error;
487
488 error = check_nxstats_msg(oh);
489 if (error) {
490 return error;
491 }
492
493 nsm = (struct nicira_stats_msg *) oh;
494 return ofputil_lookup_openflow_message(&nxst_reply_category,
495 ntohl(nsm->subtype),
496 ntohs(oh->length), typep);
497}
498
499static int
500ofputil_decode_ofpst_request(const struct ofp_header *oh,
501 const struct ofputil_msg_type **typep)
502{
28c8bad1 503 enum { OSM_SIZE = sizeof(struct ofp_stats_msg) };
d1e2cf21
BP
504 static const struct ofputil_msg_type ofpst_requests[] = {
505 { OFPUTIL_OFPST_DESC_REQUEST,
506 OFPST_DESC, "OFPST_DESC request",
28c8bad1 507 OSM_SIZE, 0 },
d1e2cf21
BP
508
509 { OFPUTIL_OFPST_FLOW_REQUEST,
510 OFPST_FLOW, "OFPST_FLOW request",
28c8bad1 511 OSM_SIZE + sizeof(struct ofp_flow_stats_request), 0 },
d1e2cf21
BP
512
513 { OFPUTIL_OFPST_AGGREGATE_REQUEST,
514 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
28c8bad1 515 OSM_SIZE + sizeof(struct ofp_aggregate_stats_request), 0 },
d1e2cf21
BP
516
517 { OFPUTIL_OFPST_TABLE_REQUEST,
518 OFPST_TABLE, "OFPST_TABLE request",
28c8bad1 519 OSM_SIZE, 0 },
d1e2cf21
BP
520
521 { OFPUTIL_OFPST_PORT_REQUEST,
522 OFPST_PORT, "OFPST_PORT request",
28c8bad1 523 OSM_SIZE + sizeof(struct ofp_port_stats_request), 0 },
d1e2cf21
BP
524
525 { OFPUTIL_OFPST_QUEUE_REQUEST,
526 OFPST_QUEUE, "OFPST_QUEUE request",
28c8bad1 527 OSM_SIZE + sizeof(struct ofp_queue_stats_request), 0 },
d1e2cf21
BP
528
529 { 0,
530 OFPST_VENDOR, "OFPST_VENDOR request",
28c8bad1 531 OSM_SIZE + sizeof(uint32_t), 1 },
d1e2cf21
BP
532 };
533
534 static const struct ofputil_msg_category ofpst_request_category = {
535 "OpenFlow statistics",
536 ofpst_requests, ARRAY_SIZE(ofpst_requests),
537 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
538 };
539
28c8bad1 540 const struct ofp_stats_msg *request = (const struct ofp_stats_msg *) oh;
d1e2cf21
BP
541 int error;
542
d1e2cf21 543 error = ofputil_lookup_openflow_message(&ofpst_request_category,
28c8bad1 544 ntohs(request->type),
d1e2cf21 545 ntohs(oh->length), typep);
28c8bad1 546 if (!error && request->type == htons(OFPST_VENDOR)) {
d1e2cf21
BP
547 error = ofputil_decode_nxst_request(oh, typep);
548 }
549 return error;
550}
551
552static int
553ofputil_decode_ofpst_reply(const struct ofp_header *oh,
554 const struct ofputil_msg_type **typep)
555{
28c8bad1 556 enum { OSM_SIZE = sizeof(struct ofp_stats_msg) };
d1e2cf21
BP
557 static const struct ofputil_msg_type ofpst_replies[] = {
558 { OFPUTIL_OFPST_DESC_REPLY,
559 OFPST_DESC, "OFPST_DESC reply",
28c8bad1 560 OSM_SIZE + sizeof(struct ofp_desc_stats), 0 },
d1e2cf21
BP
561
562 { OFPUTIL_OFPST_FLOW_REPLY,
563 OFPST_FLOW, "OFPST_FLOW reply",
28c8bad1 564 OSM_SIZE, 1 },
d1e2cf21
BP
565
566 { OFPUTIL_OFPST_AGGREGATE_REPLY,
567 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
28c8bad1 568 OSM_SIZE + sizeof(struct ofp_aggregate_stats_reply), 0 },
d1e2cf21
BP
569
570 { OFPUTIL_OFPST_TABLE_REPLY,
571 OFPST_TABLE, "OFPST_TABLE reply",
28c8bad1 572 OSM_SIZE, sizeof(struct ofp_table_stats) },
d1e2cf21
BP
573
574 { OFPUTIL_OFPST_PORT_REPLY,
575 OFPST_PORT, "OFPST_PORT reply",
28c8bad1 576 OSM_SIZE, sizeof(struct ofp_port_stats) },
d1e2cf21
BP
577
578 { OFPUTIL_OFPST_QUEUE_REPLY,
579 OFPST_QUEUE, "OFPST_QUEUE reply",
28c8bad1 580 OSM_SIZE, sizeof(struct ofp_queue_stats) },
d1e2cf21
BP
581
582 { 0,
583 OFPST_VENDOR, "OFPST_VENDOR reply",
28c8bad1 584 OSM_SIZE + sizeof(uint32_t), 1 },
d1e2cf21
BP
585 };
586
587 static const struct ofputil_msg_category ofpst_reply_category = {
588 "OpenFlow statistics",
589 ofpst_replies, ARRAY_SIZE(ofpst_replies),
590 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
591 };
592
28c8bad1 593 const struct ofp_stats_msg *reply = (const struct ofp_stats_msg *) oh;
d1e2cf21
BP
594 int error;
595
596 error = ofputil_lookup_openflow_message(&ofpst_reply_category,
28c8bad1 597 ntohs(reply->type),
d1e2cf21 598 ntohs(oh->length), typep);
28c8bad1 599 if (!error && reply->type == htons(OFPST_VENDOR)) {
d1e2cf21
BP
600 error = ofputil_decode_nxst_reply(oh, typep);
601 }
602 return error;
603}
604
605/* Decodes the message type represented by 'oh'. Returns 0 if successful or
606 * an OpenFlow error code constructed with ofp_mkerr() on failure. Either
607 * way, stores in '*typep' a type structure that can be inspected with the
608 * ofputil_msg_type_*() functions.
609 *
610 * oh->length must indicate the correct length of the message (and must be at
611 * least sizeof(struct ofp_header)).
612 *
613 * Success indicates that 'oh' is at least as long as the minimum-length
614 * message of its type. */
615int
616ofputil_decode_msg_type(const struct ofp_header *oh,
617 const struct ofputil_msg_type **typep)
618{
619 static const struct ofputil_msg_type ofpt_messages[] = {
620 { OFPUTIL_OFPT_HELLO,
621 OFPT_HELLO, "OFPT_HELLO",
622 sizeof(struct ofp_hello), 1 },
623
624 { OFPUTIL_OFPT_ERROR,
625 OFPT_ERROR, "OFPT_ERROR",
626 sizeof(struct ofp_error_msg), 1 },
627
628 { OFPUTIL_OFPT_ECHO_REQUEST,
629 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
630 sizeof(struct ofp_header), 1 },
631
632 { OFPUTIL_OFPT_ECHO_REPLY,
633 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
634 sizeof(struct ofp_header), 1 },
635
636 { OFPUTIL_OFPT_FEATURES_REQUEST,
637 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
638 sizeof(struct ofp_header), 0 },
639
640 { OFPUTIL_OFPT_FEATURES_REPLY,
641 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
642 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
643
644 { OFPUTIL_OFPT_GET_CONFIG_REQUEST,
645 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
646 sizeof(struct ofp_header), 0 },
647
648 { OFPUTIL_OFPT_GET_CONFIG_REPLY,
649 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
650 sizeof(struct ofp_switch_config), 0 },
651
652 { OFPUTIL_OFPT_SET_CONFIG,
653 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
654 sizeof(struct ofp_switch_config), 0 },
655
656 { OFPUTIL_OFPT_PACKET_IN,
657 OFPT_PACKET_IN, "OFPT_PACKET_IN",
658 offsetof(struct ofp_packet_in, data), 1 },
659
660 { OFPUTIL_OFPT_FLOW_REMOVED,
661 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
662 sizeof(struct ofp_flow_removed), 0 },
663
664 { OFPUTIL_OFPT_PORT_STATUS,
665 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
666 sizeof(struct ofp_port_status), 0 },
667
668 { OFPUTIL_OFPT_PACKET_OUT,
669 OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
670 sizeof(struct ofp_packet_out), 1 },
671
672 { OFPUTIL_OFPT_FLOW_MOD,
673 OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
674 sizeof(struct ofp_flow_mod), 1 },
675
676 { OFPUTIL_OFPT_PORT_MOD,
677 OFPT_PORT_MOD, "OFPT_PORT_MOD",
678 sizeof(struct ofp_port_mod), 0 },
679
680 { 0,
681 OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
28c8bad1 682 sizeof(struct ofp_stats_msg), 1 },
d1e2cf21
BP
683
684 { 0,
685 OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
28c8bad1 686 sizeof(struct ofp_stats_msg), 1 },
d1e2cf21
BP
687
688 { OFPUTIL_OFPT_BARRIER_REQUEST,
689 OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
690 sizeof(struct ofp_header), 0 },
691
692 { OFPUTIL_OFPT_BARRIER_REPLY,
693 OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
694 sizeof(struct ofp_header), 0 },
695
696 { 0,
697 OFPT_VENDOR, "OFPT_VENDOR",
698 sizeof(struct ofp_vendor_header), 1 },
699 };
700
701 static const struct ofputil_msg_category ofpt_category = {
702 "OpenFlow message",
703 ofpt_messages, ARRAY_SIZE(ofpt_messages),
704 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE)
705 };
706
707 int error;
708
709 error = ofputil_lookup_openflow_message(&ofpt_category, oh->type,
710 ntohs(oh->length), typep);
711 if (!error) {
712 switch (oh->type) {
713 case OFPT_VENDOR:
714 error = ofputil_decode_vendor(oh, typep);
715 break;
716
717 case OFPT_STATS_REQUEST:
718 error = ofputil_decode_ofpst_request(oh, typep);
719 break;
720
721 case OFPT_STATS_REPLY:
722 error = ofputil_decode_ofpst_reply(oh, typep);
723
724 default:
725 break;
726 }
727 }
728 if (error) {
729 static const struct ofputil_msg_type ofputil_invalid_type = {
730 OFPUTIL_INVALID,
731 0, "OFPUTIL_INVALID",
732 0, 0
733 };
734
735 *typep = &ofputil_invalid_type;
736 }
737 return error;
738}
739
740/* Returns an OFPUTIL_* message type code for 'type'. */
741enum ofputil_msg_code
742ofputil_msg_type_code(const struct ofputil_msg_type *type)
743{
744 return type->code;
745}
2e4f5fcf 746\f
7fa91113
BP
747/* Flow formats. */
748
749bool
750ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
751{
752 switch (flow_format) {
753 case NXFF_OPENFLOW10:
7fa91113
BP
754 case NXFF_NXM:
755 return true;
756 }
757
758 return false;
759}
760
761const char *
762ofputil_flow_format_to_string(enum nx_flow_format flow_format)
763{
764 switch (flow_format) {
765 case NXFF_OPENFLOW10:
766 return "openflow10";
7fa91113
BP
767 case NXFF_NXM:
768 return "nxm";
769 default:
770 NOT_REACHED();
771 }
772}
773
88ca35ee
BP
774int
775ofputil_flow_format_from_string(const char *s)
776{
777 return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
88ca35ee
BP
778 : !strcmp(s, "nxm") ? NXFF_NXM
779 : -1);
780}
781
782static bool
783regs_fully_wildcarded(const struct flow_wildcards *wc)
784{
785 int i;
786
787 for (i = 0; i < FLOW_N_REGS; i++) {
788 if (wc->reg_masks[i] != 0) {
789 return false;
790 }
791 }
792 return true;
793}
794
b78f6b77
BP
795/* Returns the minimum nx_flow_format to use for sending 'rule' to a switch
796 * (e.g. to add or remove a flow). Only NXM can handle tunnel IDs, registers,
797 * or fixing the Ethernet multicast bit. Otherwise, it's better to use
798 * NXFF_OPENFLOW10 for backward compatibility. */
799enum nx_flow_format
800ofputil_min_flow_format(const struct cls_rule *rule)
8368c090
BP
801{
802 const struct flow_wildcards *wc = &rule->wc;
8368c090
BP
803
804 /* Only NXM supports separately wildcards the Ethernet multicast bit. */
805 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
b78f6b77 806 return NXFF_NXM;
8368c090
BP
807 }
808
bad68a99
JP
809 /* Only NXM supports matching ARP hardware addresses. */
810 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
b78f6b77 811 return NXFF_NXM;
bad68a99
JP
812 }
813
d31f1109
JP
814 /* Only NXM supports matching IPv6 traffic. */
815 if (!(wc->wildcards & FWW_DL_TYPE)
816 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
b78f6b77 817 return NXFF_NXM;
d31f1109
JP
818 }
819
8368c090
BP
820 /* Only NXM supports matching registers. */
821 if (!regs_fully_wildcarded(wc)) {
b78f6b77 822 return NXFF_NXM;
8368c090
BP
823 }
824
b78f6b77
BP
825 /* Only NXM supports matching tun_id. */
826 if (wc->tun_id_mask != htonll(0)) {
827 return NXFF_NXM;
8368c090
BP
828 }
829
830 /* Other formats can express this rule. */
b78f6b77 831 return NXFF_OPENFLOW10;
88ca35ee
BP
832}
833
834/* Returns an OpenFlow message that can be used to set the flow format to
835 * 'flow_format'. */
836struct ofpbuf *
837ofputil_make_set_flow_format(enum nx_flow_format flow_format)
838{
b78f6b77 839 struct nxt_set_flow_format *sff;
88ca35ee
BP
840 struct ofpbuf *msg;
841
b78f6b77
BP
842 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
843 sff->format = htonl(flow_format);
88ca35ee
BP
844
845 return msg;
846}
847
6c1491fb
BP
848/* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
849 * extension on or off (according to 'flow_mod_table_id'). */
850struct ofpbuf *
851ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
852{
853 struct nxt_flow_mod_table_id *nfmti;
854 struct ofpbuf *msg;
855
856 nfmti = make_nxmsg(sizeof *nfmti, NXT_FLOW_MOD_TABLE_ID, &msg);
857 nfmti->set = flow_mod_table_id;
858 return msg;
859}
860
7fa91113
BP
861/* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
862 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
863 * code.
864 *
6c1491fb
BP
865 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
866 * enabled, false otherwise.
867 *
2e4f5fcf
BP
868 * Does not validate the flow_mod actions. */
869int
870ofputil_decode_flow_mod(struct flow_mod *fm, const struct ofp_header *oh,
6c1491fb 871 bool flow_mod_table_id)
2e4f5fcf
BP
872{
873 const struct ofputil_msg_type *type;
6c1491fb 874 uint16_t command;
2e4f5fcf
BP
875 struct ofpbuf b;
876
2013493b 877 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2e4f5fcf
BP
878
879 ofputil_decode_msg_type(oh, &type);
880 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
881 /* Standard OpenFlow flow_mod. */
2e4f5fcf 882 const struct ofp_flow_mod *ofm;
1c0b7503 883 uint16_t priority;
2e4f5fcf
BP
884 int error;
885
886 /* Dissect the message. */
bbc32a88 887 ofm = ofpbuf_pull(&b, sizeof *ofm);
2e4f5fcf
BP
888 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
889 if (error) {
890 return error;
891 }
892
1c0b7503
BP
893 /* Set priority based on original wildcards. Normally we'd allow
894 * ofputil_cls_rule_from_match() to do this for us, but
b459a924 895 * ofputil_normalize_rule() can put wildcards where the original flow
1c0b7503
BP
896 * didn't have them. */
897 priority = ntohs(ofm->priority);
898 if (!(ofm->match.wildcards & htonl(OFPFW_ALL))) {
899 priority = UINT16_MAX;
900 }
901
b459a924
BP
902 /* Translate the rule. */
903 ofputil_cls_rule_from_match(&ofm->match, priority, &fm->cr);
904 ofputil_normalize_rule(&fm->cr, NXFF_OPENFLOW10);
2e4f5fcf
BP
905
906 /* Translate the message. */
2e4f5fcf 907 fm->cookie = ofm->cookie;
6c1491fb 908 command = ntohs(ofm->command);
2e4f5fcf
BP
909 fm->idle_timeout = ntohs(ofm->idle_timeout);
910 fm->hard_timeout = ntohs(ofm->hard_timeout);
911 fm->buffer_id = ntohl(ofm->buffer_id);
912 fm->out_port = ntohs(ofm->out_port);
913 fm->flags = ntohs(ofm->flags);
914 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
915 /* Nicira extended flow_mod. */
916 const struct nx_flow_mod *nfm;
917 int error;
918
919 /* Dissect the message. */
bbc32a88 920 nfm = ofpbuf_pull(&b, sizeof *nfm);
2e4f5fcf
BP
921 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
922 &fm->cr);
923 if (error) {
924 return error;
925 }
926 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
927 if (error) {
928 return error;
929 }
930
931 /* Translate the message. */
932 fm->cookie = nfm->cookie;
6c1491fb 933 command = ntohs(nfm->command);
2e4f5fcf
BP
934 fm->idle_timeout = ntohs(nfm->idle_timeout);
935 fm->hard_timeout = ntohs(nfm->hard_timeout);
936 fm->buffer_id = ntohl(nfm->buffer_id);
937 fm->out_port = ntohs(nfm->out_port);
938 fm->flags = ntohs(nfm->flags);
939 } else {
940 NOT_REACHED();
941 }
942
6c1491fb
BP
943 if (flow_mod_table_id) {
944 fm->command = command & 0xff;
945 fm->table_id = command >> 8;
946 } else {
947 fm->command = command;
948 fm->table_id = 0xff;
949 }
950
2e4f5fcf
BP
951 return 0;
952}
953
954/* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
6c1491fb
BP
955 * 'flow_format' and returns the message.
956 *
957 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
958 * enabled, false otherwise. */
2e4f5fcf
BP
959struct ofpbuf *
960ofputil_encode_flow_mod(const struct flow_mod *fm,
6c1491fb
BP
961 enum nx_flow_format flow_format,
962 bool flow_mod_table_id)
2e4f5fcf
BP
963{
964 size_t actions_len = fm->n_actions * sizeof *fm->actions;
965 struct ofpbuf *msg;
6c1491fb
BP
966 uint16_t command;
967
968 command = (flow_mod_table_id
969 ? (fm->command & 0xff) | (fm->table_id << 8)
970 : fm->command);
2e4f5fcf 971
b78f6b77 972 if (flow_format == NXFF_OPENFLOW10) {
2e4f5fcf
BP
973 struct ofp_flow_mod *ofm;
974
975 msg = ofpbuf_new(sizeof *ofm + actions_len);
976 ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
b78f6b77
BP
977 ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
978 ofm->cookie = fm->cookie;
2e4f5fcf
BP
979 ofm->command = htons(fm->command);
980 ofm->idle_timeout = htons(fm->idle_timeout);
981 ofm->hard_timeout = htons(fm->hard_timeout);
982 ofm->priority = htons(fm->cr.priority);
983 ofm->buffer_id = htonl(fm->buffer_id);
984 ofm->out_port = htons(fm->out_port);
985 ofm->flags = htons(fm->flags);
986 } else if (flow_format == NXFF_NXM) {
987 struct nx_flow_mod *nfm;
988 int match_len;
989
990 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
991 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
992 match_len = nx_put_match(msg, &fm->cr);
993
994 nfm = msg->data;
995 nfm->cookie = fm->cookie;
6c1491fb 996 nfm->command = htons(command);
2e4f5fcf
BP
997 nfm->idle_timeout = htons(fm->idle_timeout);
998 nfm->hard_timeout = htons(fm->hard_timeout);
999 nfm->priority = htons(fm->cr.priority);
1000 nfm->buffer_id = htonl(fm->buffer_id);
1001 nfm->out_port = htons(fm->out_port);
1002 nfm->flags = htons(fm->flags);
1003 nfm->match_len = htons(match_len);
1004 } else {
1005 NOT_REACHED();
1006 }
1007
1008 ofpbuf_put(msg, fm->actions, actions_len);
1009 update_openflow_length(msg);
1010 return msg;
1011}
1012
1013static int
1014ofputil_decode_ofpst_flow_request(struct flow_stats_request *fsr,
1015 const struct ofp_header *oh,
2e4f5fcf
BP
1016 bool aggregate)
1017{
1018 const struct ofp_flow_stats_request *ofsr = ofputil_stats_body(oh);
1019
1020 fsr->aggregate = aggregate;
b78f6b77 1021 ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
2e4f5fcf
BP
1022 fsr->out_port = ntohs(ofsr->out_port);
1023 fsr->table_id = ofsr->table_id;
1024
1025 return 0;
1026}
1027
1028static int
1029ofputil_decode_nxst_flow_request(struct flow_stats_request *fsr,
1030 const struct ofp_header *oh,
1031 bool aggregate)
1032{
1033 const struct nx_flow_stats_request *nfsr;
1034 struct ofpbuf b;
1035 int error;
1036
2013493b 1037 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2e4f5fcf 1038
bbc32a88 1039 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
2e4f5fcf
BP
1040 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match);
1041 if (error) {
1042 return error;
1043 }
1044 if (b.size) {
1045 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1046 }
1047
1048 fsr->aggregate = aggregate;
1049 fsr->out_port = ntohs(nfsr->out_port);
1050 fsr->table_id = nfsr->table_id;
1051
1052 return 0;
1053}
1054
1055/* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
b78f6b77
BP
1056 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1057 * successful, otherwise an OpenFlow error code. */
2e4f5fcf
BP
1058int
1059ofputil_decode_flow_stats_request(struct flow_stats_request *fsr,
b78f6b77 1060 const struct ofp_header *oh)
2e4f5fcf
BP
1061{
1062 const struct ofputil_msg_type *type;
1063 struct ofpbuf b;
1064 int code;
1065
2013493b 1066 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2e4f5fcf
BP
1067
1068 ofputil_decode_msg_type(oh, &type);
1069 code = ofputil_msg_type_code(type);
1070 switch (code) {
1071 case OFPUTIL_OFPST_FLOW_REQUEST:
b78f6b77 1072 return ofputil_decode_ofpst_flow_request(fsr, oh, false);
2e4f5fcf
BP
1073
1074 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
b78f6b77 1075 return ofputil_decode_ofpst_flow_request(fsr, oh, true);
2e4f5fcf
BP
1076
1077 case OFPUTIL_NXST_FLOW_REQUEST:
1078 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1079
1080 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1081 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1082
1083 default:
1084 /* Hey, the caller lied. */
1085 NOT_REACHED();
1086 }
1087}
1088
1089/* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
4ffd1b43 1090 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2e4f5fcf
BP
1091 * 'flow_format', and returns the message. */
1092struct ofpbuf *
1093ofputil_encode_flow_stats_request(const struct flow_stats_request *fsr,
1094 enum nx_flow_format flow_format)
1095{
1096 struct ofpbuf *msg;
1097
b78f6b77 1098 if (flow_format == NXFF_OPENFLOW10) {
2e4f5fcf
BP
1099 struct ofp_flow_stats_request *ofsr;
1100 int type;
1101
1102 BUILD_ASSERT_DECL(sizeof(struct ofp_flow_stats_request)
1103 == sizeof(struct ofp_aggregate_stats_request));
1104
1105 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1106 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, &msg);
b78f6b77 1107 ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
2e4f5fcf
BP
1108 ofsr->table_id = fsr->table_id;
1109 ofsr->out_port = htons(fsr->out_port);
1110 } else if (flow_format == NXFF_NXM) {
1111 struct nx_flow_stats_request *nfsr;
1112 int match_len;
9fb7fa87 1113 int subtype;
2e4f5fcf 1114
9fb7fa87
BP
1115 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1116 ofputil_make_nxstats_request(sizeof *nfsr, subtype, &msg);
2e4f5fcf
BP
1117 match_len = nx_put_match(msg, &fsr->match);
1118
1119 nfsr = msg->data;
1120 nfsr->out_port = htons(fsr->out_port);
1121 nfsr->match_len = htons(match_len);
1122 nfsr->table_id = fsr->table_id;
1123 } else {
1124 NOT_REACHED();
1125 }
1126
1127 return msg;
1128}
d1e2cf21 1129
4ffd1b43 1130/* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
b78f6b77 1131 * ofputil_flow_stats in 'fs'.
4ffd1b43
BP
1132 *
1133 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1134 * OpenFlow message. Calling this function multiple times for a single 'msg'
1135 * iterates through the replies. The caller must initially leave 'msg''s layer
1136 * pointers null and not modify them between calls.
1137 *
1138 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1139 * otherwise a positive errno value. */
1140int
1141ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
b78f6b77 1142 struct ofpbuf *msg)
4ffd1b43
BP
1143{
1144 const struct ofputil_msg_type *type;
1145 int code;
1146
1147 ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1148 code = ofputil_msg_type_code(type);
1149 if (!msg->l2) {
1150 msg->l2 = msg->data;
1151 if (code == OFPUTIL_OFPST_FLOW_REPLY) {
28c8bad1 1152 ofpbuf_pull(msg, sizeof(struct ofp_stats_msg));
4ffd1b43
BP
1153 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1154 ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1155 } else {
1156 NOT_REACHED();
1157 }
1158 }
1159
1160 if (!msg->size) {
1161 return EOF;
1162 } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1163 const struct ofp_flow_stats *ofs;
1164 size_t length;
1165
1166 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1167 if (!ofs) {
1168 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1169 "bytes at end", msg->size);
1170 return EINVAL;
1171 }
1172
1173 length = ntohs(ofs->length);
1174 if (length < sizeof *ofs) {
1175 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1176 "length %zu", length);
1177 return EINVAL;
1178 }
1179
1180 if (ofputil_pull_actions(msg, length - sizeof *ofs,
1181 &fs->actions, &fs->n_actions)) {
1182 return EINVAL;
1183 }
1184
1185 fs->cookie = get_32aligned_be64(&ofs->cookie);
1186 ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
b78f6b77 1187 &fs->rule);
4ffd1b43
BP
1188 fs->table_id = ofs->table_id;
1189 fs->duration_sec = ntohl(ofs->duration_sec);
1190 fs->duration_nsec = ntohl(ofs->duration_nsec);
1191 fs->idle_timeout = ntohs(ofs->idle_timeout);
1192 fs->hard_timeout = ntohs(ofs->hard_timeout);
1193 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1194 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1195 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1196 const struct nx_flow_stats *nfs;
1197 size_t match_len, length;
1198
1199 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1200 if (!nfs) {
1201 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1202 "bytes at end", msg->size);
1203 return EINVAL;
1204 }
1205
1206 length = ntohs(nfs->length);
1207 match_len = ntohs(nfs->match_len);
1208 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1209 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1210 "claims invalid length %zu", match_len, length);
1211 return EINVAL;
1212 }
1213 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule)) {
1214 return EINVAL;
1215 }
1216
1217 if (ofputil_pull_actions(msg,
1218 length - sizeof *nfs - ROUND_UP(match_len, 8),
1219 &fs->actions, &fs->n_actions)) {
1220 return EINVAL;
1221 }
1222
1223 fs->cookie = nfs->cookie;
1224 fs->table_id = nfs->table_id;
1225 fs->duration_sec = ntohl(nfs->duration_sec);
1226 fs->duration_nsec = ntohl(nfs->duration_nsec);
1227 fs->idle_timeout = ntohs(nfs->idle_timeout);
1228 fs->hard_timeout = ntohs(nfs->hard_timeout);
1229 fs->packet_count = ntohll(nfs->packet_count);
1230 fs->byte_count = ntohll(nfs->byte_count);
1231 } else {
1232 NOT_REACHED();
1233 }
1234
1235 return 0;
1236}
1237
b78f6b77
BP
1238/* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1239 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1240 * an OpenFlow error code. */
9b045a0c
BP
1241int
1242ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
b78f6b77 1243 const struct ofp_header *oh)
9b045a0c
BP
1244{
1245 const struct ofputil_msg_type *type;
1246 enum ofputil_msg_code code;
1247
1248 ofputil_decode_msg_type(oh, &type);
1249 code = ofputil_msg_type_code(type);
1250 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1251 const struct ofp_flow_removed *ofr;
1252
1253 ofr = (const struct ofp_flow_removed *) oh;
1254 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
b78f6b77 1255 &fr->rule);
9b045a0c
BP
1256 fr->cookie = ofr->cookie;
1257 fr->reason = ofr->reason;
1258 fr->duration_sec = ntohl(ofr->duration_sec);
1259 fr->duration_nsec = ntohl(ofr->duration_nsec);
1260 fr->idle_timeout = ntohs(ofr->idle_timeout);
1261 fr->packet_count = ntohll(ofr->packet_count);
1262 fr->byte_count = ntohll(ofr->byte_count);
1263 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1264 struct nx_flow_removed *nfr;
1265 struct ofpbuf b;
1266 int error;
1267
1268 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1269
1270 nfr = ofpbuf_pull(&b, sizeof *nfr);
1271 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1272 &fr->rule);
1273 if (error) {
1274 return error;
1275 }
1276 if (b.size) {
1277 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1278 }
1279
1280 fr->cookie = nfr->cookie;
1281 fr->reason = nfr->reason;
1282 fr->duration_sec = ntohl(nfr->duration_sec);
1283 fr->duration_nsec = ntohl(nfr->duration_nsec);
1284 fr->idle_timeout = ntohs(nfr->idle_timeout);
1285 fr->packet_count = ntohll(nfr->packet_count);
1286 fr->byte_count = ntohll(nfr->byte_count);
1287 } else {
1288 NOT_REACHED();
1289 }
1290
1291 return 0;
1292}
1293
588cd7b5
BP
1294/* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1295 * NXT_FLOW_REMOVED message 'oh' according to 'flow_format', and returns the
1296 * message. */
1297struct ofpbuf *
1298ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1299 enum nx_flow_format flow_format)
1300{
1301 struct ofpbuf *msg;
1302
b78f6b77 1303 if (flow_format == NXFF_OPENFLOW10) {
588cd7b5
BP
1304 struct ofp_flow_removed *ofr;
1305
1306 ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
1307 &msg);
b78f6b77 1308 ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
7fb563b9 1309 ofr->cookie = fr->cookie;
588cd7b5
BP
1310 ofr->priority = htons(fr->rule.priority);
1311 ofr->reason = fr->reason;
1312 ofr->duration_sec = htonl(fr->duration_sec);
1313 ofr->duration_nsec = htonl(fr->duration_nsec);
1314 ofr->idle_timeout = htons(fr->idle_timeout);
1315 ofr->packet_count = htonll(fr->packet_count);
1316 ofr->byte_count = htonll(fr->byte_count);
1317 } else if (flow_format == NXFF_NXM) {
1318 struct nx_flow_removed *nfr;
1319 int match_len;
1320
1321 make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
1322 match_len = nx_put_match(msg, &fr->rule);
1323
1324 nfr = msg->data;
1325 nfr->cookie = fr->cookie;
1326 nfr->priority = htons(fr->rule.priority);
1327 nfr->reason = fr->reason;
1328 nfr->duration_sec = htonl(fr->duration_sec);
1329 nfr->duration_nsec = htonl(fr->duration_nsec);
1330 nfr->idle_timeout = htons(fr->idle_timeout);
1331 nfr->match_len = htons(match_len);
1332 nfr->packet_count = htonll(fr->packet_count);
1333 nfr->byte_count = htonll(fr->byte_count);
1334 } else {
1335 NOT_REACHED();
1336 }
1337
1338 return msg;
1339}
1340
ebb57021
BP
1341/* Converts abstract ofputil_packet_in 'pin' into an OFPT_PACKET_IN message
1342 * and returns the message.
1343 *
1344 * If 'rw_packet' is NULL, the caller takes ownership of the newly allocated
1345 * returned ofpbuf.
1346 *
1347 * If 'rw_packet' is nonnull, then it must contain the same data as
1348 * pin->packet. 'rw_packet' is allowed to be the same ofpbuf as pin->packet.
1349 * It is modified in-place into an OFPT_PACKET_IN message according to 'pin',
1350 * and then ofputil_encode_packet_in() returns 'rw_packet'. If 'rw_packet' has
1351 * enough headroom to insert a "struct ofp_packet_in", this is more efficient
1352 * than ofputil_encode_packet_in() because it does not copy the packet
1353 * payload. */
1354struct ofpbuf *
1355ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
1356 struct ofpbuf *rw_packet)
1357{
1358 int total_len = pin->packet->size;
1359 struct ofp_packet_in *opi;
1360
1361 if (rw_packet) {
1362 if (pin->send_len < rw_packet->size) {
1363 rw_packet->size = pin->send_len;
1364 }
1365 } else {
1366 rw_packet = ofpbuf_clone_data_with_headroom(
1367 pin->packet->data, MIN(pin->send_len, pin->packet->size),
1368 offsetof(struct ofp_packet_in, data));
1369 }
1370
1371 /* Add OFPT_PACKET_IN. */
1372 opi = ofpbuf_push_zeros(rw_packet, offsetof(struct ofp_packet_in, data));
1373 opi->header.version = OFP_VERSION;
1374 opi->header.type = OFPT_PACKET_IN;
1375 opi->total_len = htons(total_len);
1376 opi->in_port = htons(pin->in_port);
1377 opi->reason = pin->reason;
1378 opi->buffer_id = htonl(pin->buffer_id);
1379 update_openflow_length(rw_packet);
1380
1381 return rw_packet;
1382}
1383
d1e2cf21
BP
1384/* Returns a string representing the message type of 'type'. The string is the
1385 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
1386 * messages, the constant is followed by "request" or "reply",
1387 * e.g. "OFPST_AGGREGATE reply". */
1388const char *
1389ofputil_msg_type_name(const struct ofputil_msg_type *type)
1390{
1391 return type->name;
1392}
1393\f
fa37b408
BP
1394/* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1395 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1396 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
1397 * zeroed.
1398 *
1399 * The caller is responsible for freeing '*bufferp' when it is no longer
1400 * needed.
1401 *
1402 * The OpenFlow header length is initially set to 'openflow_len'; if the
1403 * message is later extended, the length should be updated with
1404 * update_openflow_length() before sending.
1405 *
1406 * Returns the header. */
1407void *
1408make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1409{
1410 *bufferp = ofpbuf_new(openflow_len);
1411 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1412}
1413
0bd0c660
BP
1414/* Similar to make_openflow() but creates a Nicira vendor extension message
1415 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1416void *
1417make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1418{
1419 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
1420}
1421
fa37b408
BP
1422/* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1423 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1424 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
1425 * zeroed.
1426 *
1427 * The caller is responsible for freeing '*bufferp' when it is no longer
1428 * needed.
1429 *
1430 * The OpenFlow header length is initially set to 'openflow_len'; if the
1431 * message is later extended, the length should be updated with
1432 * update_openflow_length() before sending.
1433 *
1434 * Returns the header. */
1435void *
44381c1b 1436make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
fa37b408
BP
1437 struct ofpbuf **bufferp)
1438{
1439 *bufferp = ofpbuf_new(openflow_len);
1440 return put_openflow_xid(openflow_len, type, xid, *bufferp);
1441}
1442
0bd0c660
BP
1443/* Similar to make_openflow_xid() but creates a Nicira vendor extension message
1444 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1445void *
44381c1b 1446make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
0bd0c660
BP
1447 struct ofpbuf **bufferp)
1448{
dfdfc8d4
BP
1449 *bufferp = ofpbuf_new(openflow_len);
1450 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
0bd0c660
BP
1451}
1452
fa37b408
BP
1453/* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1454 * with the given 'type' and an arbitrary transaction id. Allocated bytes
1455 * beyond the header, if any, are zeroed.
1456 *
1457 * The OpenFlow header length is initially set to 'openflow_len'; if the
1458 * message is later extended, the length should be updated with
1459 * update_openflow_length() before sending.
1460 *
1461 * Returns the header. */
1462void *
1463put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1464{
1465 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
1466}
1467
1468/* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1469 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
1470 * the header, if any, are zeroed.
1471 *
1472 * The OpenFlow header length is initially set to 'openflow_len'; if the
1473 * message is later extended, the length should be updated with
1474 * update_openflow_length() before sending.
1475 *
1476 * Returns the header. */
1477void *
44381c1b 1478put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
fa37b408
BP
1479 struct ofpbuf *buffer)
1480{
1481 struct ofp_header *oh;
1482
1483 assert(openflow_len >= sizeof *oh);
1484 assert(openflow_len <= UINT16_MAX);
1485
1486 oh = ofpbuf_put_uninit(buffer, openflow_len);
1487 oh->version = OFP_VERSION;
1488 oh->type = type;
1489 oh->length = htons(openflow_len);
1490 oh->xid = xid;
1491 memset(oh + 1, 0, openflow_len - sizeof *oh);
1492 return oh;
1493}
1494
dfdfc8d4
BP
1495/* Similar to put_openflow() but append a Nicira vendor extension message with
1496 * the specific 'subtype'. 'subtype' should be in host byte order. */
1497void *
1498put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
1499{
1500 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
1501}
1502
1503/* Similar to put_openflow_xid() but append a Nicira vendor extension message
1504 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1505void *
1506put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1507 struct ofpbuf *buffer)
1508{
1509 struct nicira_header *nxh;
1510
1511 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
1512 nxh->vendor = htonl(NX_VENDOR_ID);
1513 nxh->subtype = htonl(subtype);
1514 return nxh;
1515}
1516
fa37b408
BP
1517/* Updates the 'length' field of the OpenFlow message in 'buffer' to
1518 * 'buffer->size'. */
1519void
d295e8e9 1520update_openflow_length(struct ofpbuf *buffer)
fa37b408
BP
1521{
1522 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
d295e8e9 1523 oh->length = htons(buffer->size);
fa37b408
BP
1524}
1525
28c8bad1
BP
1526/* Creates an ofp_stats_msg with the given 'type' and 'body_len' bytes of space
1527 * allocated for the 'body' member. Returns the first byte of the 'body'
dfdfc8d4
BP
1528 * member. */
1529void *
1530ofputil_make_stats_request(size_t body_len, uint16_t type,
1531 struct ofpbuf **bufferp)
1532{
28c8bad1
BP
1533 struct ofp_stats_msg *request;
1534 request = make_openflow(offsetof(struct ofp_stats_msg, body)
1535 + body_len, OFPT_STATS_REQUEST, bufferp);
1536 request->type = htons(type);
1537 request->flags = htons(0);
1538 return request + 1;
dfdfc8d4
BP
1539}
1540
1541/* Creates a stats request message with Nicira as vendor and the given
1542 * 'subtype', of total length 'openflow_len'. Returns the message. */
1543void *
1544ofputil_make_nxstats_request(size_t openflow_len, uint32_t subtype,
1545 struct ofpbuf **bufferp)
1546{
1547 struct nicira_stats_msg *nsm;
1548
1549 nsm = make_openflow(openflow_len, OFPT_STATS_REQUEST, bufferp);
1550 nsm->type = htons(OFPST_VENDOR);
1551 nsm->flags = htons(0);
1552 nsm->vendor = htonl(NX_VENDOR_ID);
1553 nsm->subtype = htonl(subtype);
1554 return nsm;
1555}
1556
28c8bad1 1557/* Returns the first byte of the body of the ofp_stats_msg in 'oh'. */
d1e2cf21
BP
1558const void *
1559ofputil_stats_body(const struct ofp_header *oh)
1560{
1561 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
28c8bad1 1562 return (const struct ofp_stats_msg *) oh + 1;
d1e2cf21
BP
1563}
1564
28c8bad1 1565/* Returns the length of the body of the ofp_stats_msg in 'oh'. */
d1e2cf21
BP
1566size_t
1567ofputil_stats_body_len(const struct ofp_header *oh)
1568{
1569 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
28c8bad1 1570 return ntohs(oh->length) - sizeof(struct ofp_stats_msg);
d1e2cf21
BP
1571}
1572
c6430da5
BP
1573/* Returns the first byte of the body of the nicira_stats_msg in 'oh'. */
1574const void *
1575ofputil_nxstats_body(const struct ofp_header *oh)
1576{
1577 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1578 return ((const struct nicira_stats_msg *) oh) + 1;
1579}
1580
1581/* Returns the length of the body of the nicira_stats_msg in 'oh'. */
1582size_t
1583ofputil_nxstats_body_len(const struct ofp_header *oh)
1584{
1585 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1586 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
1587}
1588
fa37b408 1589struct ofpbuf *
daa68e9f
BP
1590make_flow_mod(uint16_t command, const struct cls_rule *rule,
1591 size_t actions_len)
fa37b408
BP
1592{
1593 struct ofp_flow_mod *ofm;
1594 size_t size = sizeof *ofm + actions_len;
1595 struct ofpbuf *out = ofpbuf_new(size);
1596 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
1597 ofm->header.version = OFP_VERSION;
1598 ofm->header.type = OFPT_FLOW_MOD;
1599 ofm->header.length = htons(size);
1600 ofm->cookie = 0;
daa68e9f 1601 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
b78f6b77 1602 ofputil_cls_rule_to_match(rule, &ofm->match);
fa37b408
BP
1603 ofm->command = htons(command);
1604 return out;
1605}
1606
1607struct ofpbuf *
daa68e9f 1608make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
fa37b408
BP
1609 uint16_t idle_timeout, size_t actions_len)
1610{
daa68e9f 1611 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
fa37b408
BP
1612 struct ofp_flow_mod *ofm = out->data;
1613 ofm->idle_timeout = htons(idle_timeout);
1614 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
1615 ofm->buffer_id = htonl(buffer_id);
1616 return out;
1617}
1618
1619struct ofpbuf *
daa68e9f 1620make_del_flow(const struct cls_rule *rule)
fa37b408 1621{
daa68e9f 1622 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
fa37b408
BP
1623 struct ofp_flow_mod *ofm = out->data;
1624 ofm->out_port = htons(OFPP_NONE);
1625 return out;
1626}
1627
1628struct ofpbuf *
daa68e9f 1629make_add_simple_flow(const struct cls_rule *rule,
fa37b408
BP
1630 uint32_t buffer_id, uint16_t out_port,
1631 uint16_t idle_timeout)
1632{
81f3cad4
BP
1633 if (out_port != OFPP_NONE) {
1634 struct ofp_action_output *oao;
1635 struct ofpbuf *buffer;
1636
daa68e9f 1637 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
81f3cad4
BP
1638 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
1639 oao->type = htons(OFPAT_OUTPUT);
1640 oao->len = htons(sizeof *oao);
1641 oao->port = htons(out_port);
1642 return buffer;
1643 } else {
daa68e9f 1644 return make_add_flow(rule, buffer_id, idle_timeout, 0);
81f3cad4 1645 }
fa37b408
BP
1646}
1647
1648struct ofpbuf *
1649make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
1650 const struct ofpbuf *payload, int max_send_len)
1651{
1652 struct ofp_packet_in *opi;
1653 struct ofpbuf *buf;
1654 int send_len;
1655
1656 send_len = MIN(max_send_len, payload->size);
1657 buf = ofpbuf_new(sizeof *opi + send_len);
1658 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
1659 OFPT_PACKET_IN, 0, buf);
1660 opi->buffer_id = htonl(buffer_id);
1661 opi->total_len = htons(payload->size);
1662 opi->in_port = htons(in_port);
1663 opi->reason = reason;
1664 ofpbuf_put(buf, payload->data, send_len);
1665 update_openflow_length(buf);
1666
1667 return buf;
1668}
1669
1670struct ofpbuf *
1671make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1672 uint16_t in_port,
1673 const struct ofp_action_header *actions, size_t n_actions)
1674{
1675 size_t actions_len = n_actions * sizeof *actions;
1676 struct ofp_packet_out *opo;
1677 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
1678 struct ofpbuf *out = ofpbuf_new(size);
1679
1680 opo = ofpbuf_put_uninit(out, sizeof *opo);
1681 opo->header.version = OFP_VERSION;
1682 opo->header.type = OFPT_PACKET_OUT;
1683 opo->header.length = htons(size);
1684 opo->header.xid = htonl(0);
1685 opo->buffer_id = htonl(buffer_id);
1686 opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
1687 opo->actions_len = htons(actions_len);
1688 ofpbuf_put(out, actions, actions_len);
1689 if (packet) {
1690 ofpbuf_put(out, packet->data, packet->size);
1691 }
1692 return out;
1693}
1694
1695struct ofpbuf *
1696make_unbuffered_packet_out(const struct ofpbuf *packet,
1697 uint16_t in_port, uint16_t out_port)
1698{
1699 struct ofp_action_output action;
1700 action.type = htons(OFPAT_OUTPUT);
1701 action.len = htons(sizeof action);
1702 action.port = htons(out_port);
1703 return make_packet_out(packet, UINT32_MAX, in_port,
1704 (struct ofp_action_header *) &action, 1);
1705}
1706
1707struct ofpbuf *
1708make_buffered_packet_out(uint32_t buffer_id,
1709 uint16_t in_port, uint16_t out_port)
1710{
81f3cad4
BP
1711 if (out_port != OFPP_NONE) {
1712 struct ofp_action_output action;
1713 action.type = htons(OFPAT_OUTPUT);
1714 action.len = htons(sizeof action);
1715 action.port = htons(out_port);
1716 return make_packet_out(NULL, buffer_id, in_port,
1717 (struct ofp_action_header *) &action, 1);
1718 } else {
1719 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
1720 }
fa37b408
BP
1721}
1722
1723/* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1724struct ofpbuf *
1725make_echo_request(void)
1726{
1727 struct ofp_header *rq;
1728 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1729 rq = ofpbuf_put_uninit(out, sizeof *rq);
1730 rq->version = OFP_VERSION;
1731 rq->type = OFPT_ECHO_REQUEST;
1732 rq->length = htons(sizeof *rq);
44381c1b 1733 rq->xid = htonl(0);
fa37b408
BP
1734 return out;
1735}
1736
1737/* Creates and returns an OFPT_ECHO_REPLY message matching the
1738 * OFPT_ECHO_REQUEST message in 'rq'. */
1739struct ofpbuf *
1740make_echo_reply(const struct ofp_header *rq)
1741{
1742 size_t size = ntohs(rq->length);
1743 struct ofpbuf *out = ofpbuf_new(size);
1744 struct ofp_header *reply = ofpbuf_put(out, rq, size);
1745 reply->type = OFPT_ECHO_REPLY;
1746 return out;
1747}
1748
fa37b408
BP
1749static int
1750check_action_exact_len(const union ofp_action *a, unsigned int len,
1751 unsigned int required_len)
1752{
1753 if (len != required_len) {
35c4693a
BP
1754 VLOG_WARN_RL(&bad_ofmsg_rl, "action %"PRIu16" has invalid length "
1755 "%"PRIu16" (must be %u)\n",
1756 ntohs(a->type), ntohs(a->header.len), required_len);
fa37b408
BP
1757 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1758 }
1759 return 0;
1760}
1761
abcf9134
BP
1762static int
1763check_nx_action_exact_len(const struct nx_action_header *a,
1764 unsigned int len, unsigned int required_len)
1765{
1766 if (len != required_len) {
1767 VLOG_WARN_RL(&bad_ofmsg_rl,
1768 "Nicira action %"PRIu16" has invalid length %"PRIu16" "
1769 "(must be %u)\n",
1770 ntohs(a->subtype), ntohs(a->len), required_len);
1771 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1772 }
1773 return 0;
1774}
1775
c1c9c9c4
BP
1776/* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
1777 * that the switch will never have more than 'max_ports' ports. Returns 0 if
1778 * 'port' is valid, otherwise an ofp_mkerr() return code. */
fa37b408 1779static int
c1c9c9c4 1780check_output_port(uint16_t port, int max_ports)
fa37b408
BP
1781{
1782 switch (port) {
1783 case OFPP_IN_PORT:
1784 case OFPP_TABLE:
1785 case OFPP_NORMAL:
1786 case OFPP_FLOOD:
1787 case OFPP_ALL:
1788 case OFPP_CONTROLLER:
1789 case OFPP_LOCAL:
1790 return 0;
1791
1792 default:
c1c9c9c4 1793 if (port < max_ports) {
fa37b408
BP
1794 return 0;
1795 }
1796 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
1797 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1798 }
1799}
1800
c1c9c9c4
BP
1801/* Checks that 'action' is a valid OFPAT_ENQUEUE action, given that the switch
1802 * will never have more than 'max_ports' ports. Returns 0 if 'port' is valid,
1803 * otherwise an ofp_mkerr() return code. */
1804static int
1805check_enqueue_action(const union ofp_action *a, unsigned int len,
1806 int max_ports)
1807{
1808 const struct ofp_action_enqueue *oae;
1809 uint16_t port;
1810 int error;
1811
1812 error = check_action_exact_len(a, len, 16);
1813 if (error) {
1814 return error;
1815 }
1816
1817 oae = (const struct ofp_action_enqueue *) a;
1818 port = ntohs(oae->port);
1819 if (port < max_ports || port == OFPP_IN_PORT) {
1820 return 0;
1821 }
1822 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown enqueue port %x", port);
1823 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1824}
1825
fa37b408 1826static int
b6c9e612
BP
1827check_nicira_action(const union ofp_action *a, unsigned int len,
1828 const struct flow *flow)
fa37b408
BP
1829{
1830 const struct nx_action_header *nah;
5160ab34 1831 int subtype;
b6c9e612 1832 int error;
fa37b408
BP
1833
1834 if (len < 16) {
f4350529
BP
1835 VLOG_WARN_RL(&bad_ofmsg_rl,
1836 "Nicira vendor action only %u bytes", len);
fa37b408
BP
1837 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1838 }
1839 nah = (const struct nx_action_header *) a;
1840
e41a9130
BP
1841 subtype = ntohs(nah->subtype);
1842 if (subtype > TYPE_MAXIMUM(enum nx_action_subtype)) {
5160ab34 1843 /* This is necessary because enum nx_action_subtype may be an
e41a9130
BP
1844 * 8-bit type, so the cast below throws away the top 8 bits. */
1845 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1846 }
1847
1848 switch ((enum nx_action_subtype) subtype) {
fa37b408
BP
1849 case NXAST_RESUBMIT:
1850 case NXAST_SET_TUNNEL:
eedc0097
JP
1851 case NXAST_SET_QUEUE:
1852 case NXAST_POP_QUEUE:
abcf9134 1853 return check_nx_action_exact_len(nah, len, 16);
96fc46e8 1854
b6c9e612 1855 case NXAST_REG_MOVE:
abcf9134
BP
1856 error = check_nx_action_exact_len(nah, len,
1857 sizeof(struct nx_action_reg_move));
b6c9e612
BP
1858 if (error) {
1859 return error;
1860 }
1861 return nxm_check_reg_move((const struct nx_action_reg_move *) a, flow);
96fc46e8 1862
b6c9e612 1863 case NXAST_REG_LOAD:
abcf9134
BP
1864 error = check_nx_action_exact_len(nah, len,
1865 sizeof(struct nx_action_reg_load));
b6c9e612
BP
1866 if (error) {
1867 return error;
1868 }
1869 return nxm_check_reg_load((const struct nx_action_reg_load *) a, flow);
96fc46e8
BP
1870
1871 case NXAST_NOTE:
1872 return 0;
1873
b9298d3f 1874 case NXAST_SET_TUNNEL64:
abcf9134
BP
1875 return check_nx_action_exact_len(
1876 nah, len, sizeof(struct nx_action_set_tunnel64));
b9298d3f 1877
53ddd40a 1878 case NXAST_MULTIPATH:
abcf9134
BP
1879 error = check_nx_action_exact_len(
1880 nah, len, sizeof(struct nx_action_multipath));
53ddd40a
BP
1881 if (error) {
1882 return error;
1883 }
1884 return multipath_check((const struct nx_action_multipath *) a);
1885
3b6a2571
EJ
1886 case NXAST_AUTOPATH:
1887 error = check_nx_action_exact_len(
1888 nah, len, sizeof(struct nx_action_autopath));
1889 if (error) {
1890 return error;
1891 }
1892 return autopath_check((const struct nx_action_autopath *) a);
1893
e41a9130 1894 case NXAST_SNAT__OBSOLETE:
6c222e55 1895 case NXAST_DROP_SPOOFED_ARP__OBSOLETE:
fa37b408 1896 default:
abcf9134 1897 VLOG_WARN_RL(&bad_ofmsg_rl,
5160ab34 1898 "unknown Nicira vendor action subtype %d", subtype);
fa37b408
BP
1899 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1900 }
1901}
1902
1903static int
b6c9e612
BP
1904check_action(const union ofp_action *a, unsigned int len,
1905 const struct flow *flow, int max_ports)
fa37b408 1906{
e41a9130 1907 enum ofp_action_type type = ntohs(a->type);
fa37b408
BP
1908 int error;
1909
e41a9130 1910 switch (type) {
fa37b408 1911 case OFPAT_OUTPUT:
c1c9c9c4
BP
1912 error = check_action_exact_len(a, len, 8);
1913 if (error) {
1914 return error;
1915 }
1916 return check_output_port(ntohs(a->output.port), max_ports);
fa37b408
BP
1917
1918 case OFPAT_SET_VLAN_VID:
27bcf966
BP
1919 error = check_action_exact_len(a, len, 8);
1920 if (error) {
1921 return error;
1922 }
1923 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1924 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1925 }
1926 return 0;
1927
fa37b408 1928 case OFPAT_SET_VLAN_PCP:
27bcf966
BP
1929 error = check_action_exact_len(a, len, 8);
1930 if (error) {
1931 return error;
1932 }
d93ca2a0 1933 if (a->vlan_pcp.vlan_pcp & ~7) {
27bcf966
BP
1934 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1935 }
1936 return 0;
1937
fa37b408
BP
1938 case OFPAT_STRIP_VLAN:
1939 case OFPAT_SET_NW_SRC:
1940 case OFPAT_SET_NW_DST:
1941 case OFPAT_SET_NW_TOS:
1942 case OFPAT_SET_TP_SRC:
1943 case OFPAT_SET_TP_DST:
1944 return check_action_exact_len(a, len, 8);
1945
1946 case OFPAT_SET_DL_SRC:
1947 case OFPAT_SET_DL_DST:
1948 return check_action_exact_len(a, len, 16);
1949
1950 case OFPAT_VENDOR:
1951 return (a->vendor.vendor == htonl(NX_VENDOR_ID)
b6c9e612 1952 ? check_nicira_action(a, len, flow)
fa37b408
BP
1953 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
1954
c1c9c9c4
BP
1955 case OFPAT_ENQUEUE:
1956 return check_enqueue_action(a, len, max_ports);
1957
fa37b408 1958 default:
e41a9130 1959 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %d", (int) type);
fa37b408
BP
1960 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
1961 }
1962}
1963
1964int
1965validate_actions(const union ofp_action *actions, size_t n_actions,
b6c9e612 1966 const struct flow *flow, int max_ports)
fa37b408 1967{
3dffcf07 1968 size_t i;
fa37b408 1969
3dffcf07
BP
1970 for (i = 0; i < n_actions; ) {
1971 const union ofp_action *a = &actions[i];
fa37b408 1972 unsigned int len = ntohs(a->header.len);
69b6be19 1973 unsigned int n_slots = len / OFP_ACTION_ALIGN;
fa37b408
BP
1974 unsigned int slots_left = &actions[n_actions] - a;
1975 int error;
1976
1977 if (n_slots > slots_left) {
f4350529
BP
1978 VLOG_WARN_RL(&bad_ofmsg_rl,
1979 "action requires %u slots but only %u remain",
1980 n_slots, slots_left);
fa37b408
BP
1981 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1982 } else if (!len) {
f4350529 1983 VLOG_WARN_RL(&bad_ofmsg_rl, "action has invalid length 0");
fa37b408 1984 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
69b6be19 1985 } else if (len % OFP_ACTION_ALIGN) {
f4350529
BP
1986 VLOG_WARN_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
1987 "of %d", len, OFP_ACTION_ALIGN);
fa37b408
BP
1988 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1989 }
1990
b6c9e612 1991 error = check_action(a, len, flow, max_ports);
fa37b408
BP
1992 if (error) {
1993 return error;
1994 }
3dffcf07 1995 i += n_slots;
fa37b408
BP
1996 }
1997 return 0;
1998}
1999
dbba996b 2000/* Returns true if 'action' outputs to 'port', false otherwise. */
c1c9c9c4 2001bool
dbba996b 2002action_outputs_to_port(const union ofp_action *action, ovs_be16 port)
c1c9c9c4
BP
2003{
2004 switch (ntohs(action->type)) {
2005 case OFPAT_OUTPUT:
2006 return action->output.port == port;
2007 case OFPAT_ENQUEUE:
2008 return ((const struct ofp_action_enqueue *) action)->port == port;
2009 default:
2010 return false;
2011 }
2012}
2013
fa37b408
BP
2014/* The set of actions must either come from a trusted source or have been
2015 * previously validated with validate_actions(). */
2016const union ofp_action *
2017actions_first(struct actions_iterator *iter,
2018 const union ofp_action *oa, size_t n_actions)
2019{
2020 iter->pos = oa;
2021 iter->end = oa + n_actions;
2022 return actions_next(iter);
2023}
2024
2025const union ofp_action *
2026actions_next(struct actions_iterator *iter)
2027{
3dffcf07 2028 if (iter->pos != iter->end) {
fa37b408
BP
2029 const union ofp_action *a = iter->pos;
2030 unsigned int len = ntohs(a->header.len);
69b6be19 2031 iter->pos += len / OFP_ACTION_ALIGN;
fa37b408
BP
2032 return a;
2033 } else {
2034 return NULL;
2035 }
2036}
2037
b459a924
BP
2038/* "Normalizes" the wildcards in 'rule'. That means:
2039 *
2040 * 1. If the type of level N is known, then only the valid fields for that
2041 * level may be specified. For example, ARP does not have a TOS field,
2042 * so nw_tos must be wildcarded if 'rule' specifies an ARP flow.
2043 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
2044 * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an
2045 * IPv4 flow.
2046 *
2047 * 2. If the type of level N is not known (or not understood by Open
2048 * vSwitch), then no fields at all for that level may be specified. For
2049 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
2050 * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an
2051 * SCTP flow.
2052 *
2053 * 'flow_format' specifies the format of the flow as received or as intended to
2054 * be sent. This is important for IPv6 and ARP, for which NXM supports more
2055 * detailed matching. */
2056void
2057ofputil_normalize_rule(struct cls_rule *rule, enum nx_flow_format flow_format)
2058{
2059 enum {
2060 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
2061 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
2062 MAY_NW_PROTO = 1 << 2, /* nw_proto */
2063 MAY_NW_TOS = 1 << 3, /* nw_tos */
2064 MAY_ARP_SHA = 1 << 4, /* arp_sha */
2065 MAY_ARP_THA = 1 << 5, /* arp_tha */
2066 MAY_IPV6_ADDR = 1 << 6, /* ipv6_src, ipv6_dst */
2067 MAY_ND_TARGET = 1 << 7 /* nd_target */
2068 } may_match;
2069
2070 struct flow_wildcards wc;
2071
2072 /* Figure out what fields may be matched. */
2073 if (rule->flow.dl_type == htons(ETH_TYPE_IP)) {
2074 may_match = MAY_NW_PROTO | MAY_NW_TOS | MAY_NW_ADDR;
2075 if (rule->flow.nw_proto == IPPROTO_TCP ||
2076 rule->flow.nw_proto == IPPROTO_UDP ||
2077 rule->flow.nw_proto == IPPROTO_ICMP) {
2078 may_match |= MAY_TP_ADDR;
2079 }
2080 } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)
2081 && flow_format == NXFF_NXM) {
2082 may_match = MAY_NW_PROTO | MAY_NW_TOS | MAY_IPV6_ADDR;
2083 if (rule->flow.nw_proto == IPPROTO_TCP ||
2084 rule->flow.nw_proto == IPPROTO_UDP) {
2085 may_match |= MAY_TP_ADDR;
2086 } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) {
2087 may_match |= MAY_TP_ADDR;
2088 if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
2089 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
2090 } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
2091 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
2092 }
2093 }
2094 } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) {
2095 may_match = MAY_NW_PROTO | MAY_NW_ADDR;
2096 if (flow_format == NXFF_NXM) {
2097 may_match |= MAY_ARP_SHA | MAY_ARP_THA;
fa37b408 2098 }
1c0b7503 2099 } else {
b459a924
BP
2100 may_match = 0;
2101 }
2102
2103 /* Clear the fields that may not be matched. */
2104 wc = rule->wc;
2105 if (!(may_match & MAY_NW_ADDR)) {
2106 wc.nw_src_mask = wc.nw_dst_mask = htonl(0);
2107 }
2108 if (!(may_match & MAY_TP_ADDR)) {
2109 wc.wildcards |= FWW_TP_SRC | FWW_TP_DST;
2110 }
2111 if (!(may_match & MAY_NW_PROTO)) {
2112 wc.wildcards |= FWW_NW_PROTO;
2113 }
2114 if (!(may_match & MAY_NW_TOS)) {
2115 wc.wildcards |= FWW_NW_TOS;
2116 }
2117 if (!(may_match & MAY_ARP_SHA)) {
2118 wc.wildcards |= FWW_ARP_SHA;
2119 }
2120 if (!(may_match & MAY_ARP_THA)) {
2121 wc.wildcards |= FWW_ARP_THA;
2122 }
2123 if (!(may_match & MAY_IPV6_ADDR)) {
2124 wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any;
2125 }
2126 if (!(may_match & MAY_ND_TARGET)) {
2127 wc.wildcards |= FWW_ND_TARGET;
2128 }
2129
2130 /* Log any changes. */
2131 if (!flow_wildcards_equal(&wc, &rule->wc)) {
2132 bool log = !VLOG_DROP_INFO(&bad_ofmsg_rl);
2133 char *pre = log ? cls_rule_to_string(rule) : NULL;
2134
2135 rule->wc = wc;
2136 cls_rule_zero_wildcarded_fields(rule);
2137
2138 if (log) {
2139 char *post = cls_rule_to_string(rule);
2140 VLOG_INFO("normalization changed ofp_match, details:");
2141 VLOG_INFO(" pre: %s", pre);
2142 VLOG_INFO("post: %s", post);
2143 free(pre);
2144 free(post);
2145 }
fa37b408 2146 }
3f09c339 2147}
26c112c2
BP
2148
2149static uint32_t
2150vendor_code_to_id(uint8_t code)
2151{
2152 switch (code) {
2153#define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
3a75cda9
BP
2154 OFPUTIL_VENDORS
2155#undef OFPUTIL_VENDOR
26c112c2
BP
2156 default:
2157 return UINT32_MAX;
2158 }
2159}
2160
dc4762ed
BP
2161static int
2162vendor_id_to_code(uint32_t id)
2163{
2164 switch (id) {
2165#define OFPUTIL_VENDOR(NAME, VENDOR_ID) case VENDOR_ID: return NAME;
2166 OFPUTIL_VENDORS
2167#undef OFPUTIL_VENDOR
2168 default:
2169 return -1;
2170 }
2171}
2172
26c112c2
BP
2173/* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
2174 * information taken from 'error', whose encoding must be as described in the
2175 * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
2176 * oh->xid as its transaction ID, and it will include up to the first 64 bytes
2177 * of 'oh'.
2178 *
2179 * Returns NULL if 'error' is not an OpenFlow error code. */
2180struct ofpbuf *
dc4762ed 2181ofputil_encode_error_msg(int error, const struct ofp_header *oh)
26c112c2
BP
2182{
2183 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2184
2185 struct ofpbuf *buf;
2186 const void *data;
2187 size_t len;
2188 uint8_t vendor;
2189 uint16_t type;
2190 uint16_t code;
44381c1b 2191 ovs_be32 xid;
26c112c2
BP
2192
2193 if (!is_ofp_error(error)) {
2194 /* We format 'error' with strerror() here since it seems likely to be
2195 * a system errno value. */
2196 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
2197 error, strerror(error));
2198 return NULL;
2199 }
2200
2201 if (oh) {
2202 xid = oh->xid;
2203 data = oh;
2204 len = ntohs(oh->length);
2205 if (len > 64) {
2206 len = 64;
2207 }
2208 } else {
2209 xid = 0;
2210 data = NULL;
2211 len = 0;
2212 }
2213
2214 vendor = get_ofp_err_vendor(error);
2215 type = get_ofp_err_type(error);
2216 code = get_ofp_err_code(error);
2217 if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
2218 struct ofp_error_msg *oem;
2219
2220 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
2221 oem->type = htons(type);
2222 oem->code = htons(code);
2223 } else {
2224 struct ofp_error_msg *oem;
217f48c6 2225 struct nx_vendor_error *nve;
26c112c2
BP
2226 uint32_t vendor_id;
2227
2228 vendor_id = vendor_code_to_id(vendor);
2229 if (vendor_id == UINT32_MAX) {
2230 VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
2231 error, vendor);
2232 return NULL;
2233 }
2234
217f48c6 2235 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
26c112c2
BP
2236 OFPT_ERROR, xid, &buf);
2237 oem->type = htons(NXET_VENDOR);
2238 oem->code = htons(NXVC_VENDOR_ERROR);
2239
17764fb2 2240 nve = (struct nx_vendor_error *)oem->data;
217f48c6
BP
2241 nve->vendor = htonl(vendor_id);
2242 nve->type = htons(type);
2243 nve->code = htons(code);
26c112c2
BP
2244 }
2245
2246 if (len) {
59edb09c 2247 buf->size -= len;
26c112c2
BP
2248 ofpbuf_put(buf, data, len);
2249 }
2250
2251 return buf;
2252}
3052b0c5 2253
dc4762ed
BP
2254/* Decodes 'oh', which should be an OpenFlow OFPT_ERROR message, and returns an
2255 * Open vSwitch internal error code in the format described in the large
2256 * comment in ofp-util.h.
2257 *
2258 * If 'payload_ofs' is nonnull, on success '*payload_ofs' is set to the offset
2259 * to the payload starting from 'oh' and on failure it is set to 0. */
2260int
2261ofputil_decode_error_msg(const struct ofp_header *oh, size_t *payload_ofs)
2262{
2263 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2264
2265 const struct ofp_error_msg *oem;
2266 uint16_t type, code;
2267 struct ofpbuf b;
2268 int vendor;
2269
2270 if (payload_ofs) {
2271 *payload_ofs = 0;
2272 }
2273 if (oh->type != OFPT_ERROR) {
2274 return EPROTO;
2275 }
2276
2277 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2278 oem = ofpbuf_try_pull(&b, sizeof *oem);
2279 if (!oem) {
2280 return EPROTO;
2281 }
2282
2283 type = ntohs(oem->type);
2284 code = ntohs(oem->code);
2285 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
2286 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
2287 if (!nve) {
2288 return EPROTO;
2289 }
2290
2291 vendor = vendor_id_to_code(ntohl(nve->vendor));
2292 if (vendor < 0) {
2293 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
2294 ntohl(nve->vendor));
2295 return EPROTO;
2296 }
2297 type = ntohs(nve->type);
2298 code = ntohs(nve->code);
2299 } else {
2300 vendor = OFPUTIL_VENDOR_OPENFLOW;
2301 }
2302
2303 if (type >= 1024) {
2304 VLOG_WARN_RL(&rl, "error contains type %"PRIu16" greater than "
2305 "supported maximum value 1023", type);
2306 return EPROTO;
2307 }
2308
2309 if (payload_ofs) {
2310 *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
2311 }
2312 return ofp_mkerr_vendor(vendor, type, code);
2313}
2314
2315void
2316ofputil_format_error(struct ds *s, int error)
2317{
2318 if (is_errno(error)) {
2319 ds_put_cstr(s, strerror(error));
2320 } else {
2321 uint16_t type = get_ofp_err_type(error);
2322 uint16_t code = get_ofp_err_code(error);
2323 const char *type_s = ofp_error_type_to_string(type);
2324 const char *code_s = ofp_error_code_to_string(type, code);
2325
2326 ds_put_format(s, "type ");
2327 if (type_s) {
2328 ds_put_cstr(s, type_s);
2329 } else {
2330 ds_put_format(s, "%"PRIu16, type);
2331 }
2332
2333 ds_put_cstr(s, ", code ");
2334 if (code_s) {
2335 ds_put_cstr(s, code_s);
2336 } else {
2337 ds_put_format(s, "%"PRIu16, code);
2338 }
2339 }
2340}
2341
2342char *
2343ofputil_error_to_string(int error)
2344{
2345 struct ds s = DS_EMPTY_INITIALIZER;
2346 ofputil_format_error(&s, error);
2347 return ds_steal_cstr(&s);
2348}
2349
3052b0c5
BP
2350/* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
2351 * successful, otherwise an OpenFlow error.
2352 *
2353 * If successful, the first action is stored in '*actionsp' and the number of
2354 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
2355 * are stored, respectively.
2356 *
2357 * This function does not check that the actions are valid (the caller should
2358 * do so, with validate_actions()). The caller is also responsible for making
2359 * sure that 'b->data' is initially aligned appropriately for "union
2360 * ofp_action". */
2361int
2362ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2363 union ofp_action **actionsp, size_t *n_actionsp)
2364{
69b6be19 2365 if (actions_len % OFP_ACTION_ALIGN != 0) {
f4350529
BP
2366 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2367 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
3052b0c5
BP
2368 goto error;
2369 }
2370
2371 *actionsp = ofpbuf_try_pull(b, actions_len);
2372 if (*actionsp == NULL) {
f4350529
BP
2373 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2374 "exceeds remaining message length (%zu)",
2375 actions_len, b->size);
3052b0c5
BP
2376 goto error;
2377 }
2378
69b6be19 2379 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
3052b0c5
BP
2380 return 0;
2381
2382error:
2383 *actionsp = NULL;
2384 *n_actionsp = 0;
2385 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2386}