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