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