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