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