2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "ofp-print.h"
21 #include <netinet/icmp6.h>
24 #include "byte-order.h"
25 #include "classifier.h"
26 #include "dynamic-string.h"
27 #include "multipath.h"
29 #include "ofp-errors.h"
34 #include "unaligned.h"
35 #include "type-props.h"
38 VLOG_DEFINE_THIS_MODULE(ofp_util);
40 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
41 * in the peer and so there's not much point in showing a lot of them. */
42 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
44 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
45 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
48 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
49 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
50 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
51 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
54 ofputil_wcbits_to_netmask(int wcbits)
57 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
60 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
61 * that it wildcards. 'netmask' must be a CIDR netmask (see ip_is_cidr()). */
63 ofputil_netmask_to_wcbits(ovs_be32 netmask)
65 assert(ip_is_cidr(netmask));
67 return netmask == htonl(0) ? 32 : __builtin_ctz(ntohl(netmask));
71 for (wcbits = 32; netmask; wcbits--) {
72 netmask &= netmask - 1;
79 /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
81 #define WC_INVARIANT_LIST \
82 WC_INVARIANT_BIT(IN_PORT) \
83 WC_INVARIANT_BIT(DL_SRC) \
84 WC_INVARIANT_BIT(DL_DST) \
85 WC_INVARIANT_BIT(DL_TYPE) \
86 WC_INVARIANT_BIT(NW_PROTO) \
87 WC_INVARIANT_BIT(TP_SRC) \
88 WC_INVARIANT_BIT(TP_DST)
90 /* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
91 * actually have the same names and values. */
92 #define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME);
94 #undef WC_INVARIANT_BIT
96 /* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
98 static const flow_wildcards_t WC_INVARIANTS = 0
99 #define WC_INVARIANT_BIT(NAME) | FWW_##NAME
101 #undef WC_INVARIANT_BIT
104 /* Converts the wildcard in 'ofpfw' into a flow_wildcards in 'wc' for use in
105 * struct cls_rule. It is the caller's responsibility to handle the special
106 * case where the flow match's dl_vlan is set to OFP_VLAN_NONE. */
108 ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *wc)
110 /* Initialize most of rule->wc. */
111 flow_wildcards_init_catchall(wc);
112 wc->wildcards = (OVS_FORCE flow_wildcards_t) ofpfw & WC_INVARIANTS;
114 /* Wildcard fields that aren't defined by ofp_match or tun_id. */
115 wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_ND_TARGET);
117 if (ofpfw & OFPFW_NW_TOS) {
118 wc->wildcards |= FWW_NW_TOS;
120 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
121 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
123 if (ofpfw & OFPFW_DL_DST) {
124 /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
125 * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST
126 * and FWW_ETH_MCAST. */
127 wc->wildcards |= FWW_ETH_MCAST;
131 if (!(ofpfw & OFPFW_DL_VLAN_PCP)) {
132 wc->vlan_tci_mask |= htons(VLAN_PCP_MASK | VLAN_CFI);
134 if (!(ofpfw & OFPFW_DL_VLAN)) {
135 wc->vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
139 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
142 ofputil_cls_rule_from_match(const struct ofp_match *match,
143 unsigned int priority, struct cls_rule *rule)
145 uint32_t ofpfw = ntohl(match->wildcards) & OFPFW_ALL;
147 /* Initialize rule->priority, rule->wc. */
148 rule->priority = !ofpfw ? UINT16_MAX : priority;
149 ofputil_wildcard_from_openflow(ofpfw, &rule->wc);
151 /* Initialize most of rule->flow. */
152 rule->flow.nw_src = match->nw_src;
153 rule->flow.nw_dst = match->nw_dst;
154 rule->flow.in_port = ntohs(match->in_port);
155 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
156 rule->flow.tp_src = match->tp_src;
157 rule->flow.tp_dst = match->tp_dst;
158 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
159 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
160 rule->flow.nw_tos = match->nw_tos;
161 rule->flow.nw_proto = match->nw_proto;
163 /* Translate VLANs. */
164 if (!(ofpfw & OFPFW_DL_VLAN) && match->dl_vlan == htons(OFP_VLAN_NONE)) {
165 /* Match only packets without 802.1Q header.
167 * When OFPFW_DL_VLAN_PCP is wildcarded, this is obviously correct.
169 * If OFPFW_DL_VLAN_PCP is matched, the flow match is contradictory,
170 * because we can't have a specific PCP without an 802.1Q header.
171 * However, older versions of OVS treated this as matching packets
172 * withut an 802.1Q header, so we do here too. */
173 rule->flow.vlan_tci = htons(0);
174 rule->wc.vlan_tci_mask = htons(0xffff);
176 ovs_be16 vid, pcp, tci;
178 vid = match->dl_vlan & htons(VLAN_VID_MASK);
179 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
180 tci = vid | pcp | htons(VLAN_CFI);
181 rule->flow.vlan_tci = tci & rule->wc.vlan_tci_mask;
185 cls_rule_zero_wildcarded_fields(rule);
188 /* Convert 'rule' into the OpenFlow match structure 'match'. */
190 ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match)
192 const struct flow_wildcards *wc = &rule->wc;
195 /* Figure out most OpenFlow wildcards. */
196 ofpfw = (OVS_FORCE uint32_t) (wc->wildcards & WC_INVARIANTS);
197 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
198 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
199 if (wc->wildcards & FWW_NW_TOS) {
200 ofpfw |= OFPFW_NW_TOS;
203 /* Translate VLANs. */
204 match->dl_vlan = htons(0);
205 match->dl_vlan_pcp = 0;
206 if (rule->wc.vlan_tci_mask == htons(0)) {
207 ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
208 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
209 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
210 match->dl_vlan = htons(OFP_VLAN_NONE);
212 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
213 ofpfw |= OFPFW_DL_VLAN;
215 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
218 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
219 ofpfw |= OFPFW_DL_VLAN_PCP;
221 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
225 /* Compose most of the match structure. */
226 match->wildcards = htonl(ofpfw);
227 match->in_port = htons(rule->flow.in_port);
228 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
229 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
230 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
231 match->nw_src = rule->flow.nw_src;
232 match->nw_dst = rule->flow.nw_dst;
233 match->nw_tos = rule->flow.nw_tos;
234 match->nw_proto = rule->flow.nw_proto;
235 match->tp_src = rule->flow.tp_src;
236 match->tp_dst = rule->flow.tp_dst;
237 memset(match->pad1, '\0', sizeof match->pad1);
238 memset(match->pad2, '\0', sizeof match->pad2);
241 /* Given a 'dl_type' value in the format used in struct flow, returns the
242 * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */
244 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
246 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
247 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
251 /* Given a 'dl_type' value in the format used in an OpenFlow ofp_match
252 * structure, returns the corresponding 'dl_type' value for use in struct
255 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
257 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
258 ? htons(FLOW_DL_TYPE_NONE)
262 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
266 static uint32_t next_xid = 1;
267 return htonl(next_xid++);
270 /* Basic parsing of OpenFlow messages. */
272 struct ofputil_msg_type {
273 enum ofputil_msg_code code; /* OFPUTIL_*. */
274 uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
275 const char *name; /* e.g. "OFPT_FLOW_REMOVED". */
276 unsigned int min_size; /* Minimum total message size in bytes. */
277 /* 0 if 'min_size' is the exact size that the message must be. Otherwise,
278 * the message may exceed 'min_size' by an even multiple of this value. */
279 unsigned int extra_multiple;
282 struct ofputil_msg_category {
283 const char *name; /* e.g. "OpenFlow message" */
284 const struct ofputil_msg_type *types;
286 int missing_error; /* ofp_mkerr() value for missing type. */
290 ofputil_length_ok(const struct ofputil_msg_category *cat,
291 const struct ofputil_msg_type *type,
294 switch (type->extra_multiple) {
296 if (size != type->min_size) {
297 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
298 "length %u (expected length %u)",
299 cat->name, type->name, size, type->min_size);
305 if (size < type->min_size) {
306 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
307 "length %u (expected length at least %u bytes)",
308 cat->name, type->name, size, type->min_size);
314 if (size < type->min_size
315 || (size - type->min_size) % type->extra_multiple) {
316 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
317 "length %u (must be exactly %u bytes or longer "
318 "by an integer multiple of %u bytes)",
319 cat->name, type->name, size,
320 type->min_size, type->extra_multiple);
328 ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
329 uint32_t value, unsigned int size,
330 const struct ofputil_msg_type **typep)
332 const struct ofputil_msg_type *type;
334 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
335 if (type->value == value) {
336 if (!ofputil_length_ok(cat, type, size)) {
337 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
344 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
346 return cat->missing_error;
350 ofputil_decode_vendor(const struct ofp_header *oh,
351 const struct ofputil_msg_type **typep)
353 BUILD_ASSERT_DECL(sizeof(struct nxt_set_flow_format)
354 != sizeof(struct nxt_flow_mod_table_id));
356 static const struct ofputil_msg_type nxt_messages[] = {
357 { OFPUTIL_NXT_ROLE_REQUEST,
358 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
359 sizeof(struct nx_role_request), 0 },
361 { OFPUTIL_NXT_ROLE_REPLY,
362 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
363 sizeof(struct nx_role_request), 0 },
365 { OFPUTIL_NXT_SET_FLOW_FORMAT,
366 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
367 sizeof(struct nxt_set_flow_format), 0 },
369 { OFPUTIL_NXT_FLOW_MOD,
370 NXT_FLOW_MOD, "NXT_FLOW_MOD",
371 sizeof(struct nx_flow_mod), 8 },
373 { OFPUTIL_NXT_FLOW_REMOVED,
374 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
375 sizeof(struct nx_flow_removed), 8 },
377 { OFPUTIL_NXT_FLOW_MOD_TABLE_ID,
378 NXT_FLOW_MOD_TABLE_ID, "NXT_FLOW_MOD_TABLE_ID",
379 sizeof(struct nxt_flow_mod_table_id), 0 },
382 static const struct ofputil_msg_category nxt_category = {
383 "Nicira extension message",
384 nxt_messages, ARRAY_SIZE(nxt_messages),
385 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
388 const struct ofp_vendor_header *ovh;
389 const struct nicira_header *nh;
391 ovh = (const struct ofp_vendor_header *) oh;
392 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
393 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
394 "vendor %"PRIx32, ntohl(ovh->vendor));
395 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
398 if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
399 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
400 "length %u (expected at least %zu)",
401 ntohs(ovh->header.length), sizeof(struct nicira_header));
402 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
405 nh = (const struct nicira_header *) oh;
406 return ofputil_lookup_openflow_message(&nxt_category, ntohl(nh->subtype),
407 ntohs(oh->length), typep);
411 check_nxstats_msg(const struct ofp_header *oh)
413 const struct ofp_stats_msg *osm = (const struct ofp_stats_msg *) oh;
416 memcpy(&vendor, osm + 1, sizeof vendor);
417 if (vendor != htonl(NX_VENDOR_ID)) {
418 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
419 "unknown vendor %"PRIx32, ntohl(vendor));
420 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
423 if (ntohs(osm->header.length) < sizeof(struct nicira_stats_msg)) {
424 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
425 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
432 ofputil_decode_nxst_request(const struct ofp_header *oh,
433 const struct ofputil_msg_type **typep)
435 static const struct ofputil_msg_type nxst_requests[] = {
436 { OFPUTIL_NXST_FLOW_REQUEST,
437 NXST_FLOW, "NXST_FLOW request",
438 sizeof(struct nx_flow_stats_request), 8 },
440 { OFPUTIL_NXST_AGGREGATE_REQUEST,
441 NXST_AGGREGATE, "NXST_AGGREGATE request",
442 sizeof(struct nx_aggregate_stats_request), 8 },
445 static const struct ofputil_msg_category nxst_request_category = {
446 "Nicira extension statistics request",
447 nxst_requests, ARRAY_SIZE(nxst_requests),
448 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
451 const struct nicira_stats_msg *nsm;
454 error = check_nxstats_msg(oh);
459 nsm = (struct nicira_stats_msg *) oh;
460 return ofputil_lookup_openflow_message(&nxst_request_category,
462 ntohs(oh->length), typep);
466 ofputil_decode_nxst_reply(const struct ofp_header *oh,
467 const struct ofputil_msg_type **typep)
469 static const struct ofputil_msg_type nxst_replies[] = {
470 { OFPUTIL_NXST_FLOW_REPLY,
471 NXST_FLOW, "NXST_FLOW reply",
472 sizeof(struct nicira_stats_msg), 8 },
474 { OFPUTIL_NXST_AGGREGATE_REPLY,
475 NXST_AGGREGATE, "NXST_AGGREGATE reply",
476 sizeof(struct nx_aggregate_stats_reply), 0 },
479 static const struct ofputil_msg_category nxst_reply_category = {
480 "Nicira extension statistics reply",
481 nxst_replies, ARRAY_SIZE(nxst_replies),
482 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
485 const struct nicira_stats_msg *nsm;
488 error = check_nxstats_msg(oh);
493 nsm = (struct nicira_stats_msg *) oh;
494 return ofputil_lookup_openflow_message(&nxst_reply_category,
496 ntohs(oh->length), typep);
500 ofputil_decode_ofpst_request(const struct ofp_header *oh,
501 const struct ofputil_msg_type **typep)
503 static const struct ofputil_msg_type ofpst_requests[] = {
504 { OFPUTIL_OFPST_DESC_REQUEST,
505 OFPST_DESC, "OFPST_DESC request",
506 sizeof(struct ofp_stats_msg), 0 },
508 { OFPUTIL_OFPST_FLOW_REQUEST,
509 OFPST_FLOW, "OFPST_FLOW request",
510 sizeof(struct ofp_flow_stats_request), 0 },
512 { OFPUTIL_OFPST_AGGREGATE_REQUEST,
513 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
514 sizeof(struct ofp_flow_stats_request), 0 },
516 { OFPUTIL_OFPST_TABLE_REQUEST,
517 OFPST_TABLE, "OFPST_TABLE request",
518 sizeof(struct ofp_stats_msg), 0 },
520 { OFPUTIL_OFPST_PORT_REQUEST,
521 OFPST_PORT, "OFPST_PORT request",
522 sizeof(struct ofp_port_stats_request), 0 },
524 { OFPUTIL_OFPST_QUEUE_REQUEST,
525 OFPST_QUEUE, "OFPST_QUEUE request",
526 sizeof(struct ofp_queue_stats_request), 0 },
529 OFPST_VENDOR, "OFPST_VENDOR request",
530 sizeof(struct ofp_vendor_stats_msg), 1 },
533 static const struct ofputil_msg_category ofpst_request_category = {
534 "OpenFlow statistics",
535 ofpst_requests, ARRAY_SIZE(ofpst_requests),
536 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
539 const struct ofp_stats_msg *request = (const struct ofp_stats_msg *) oh;
542 error = ofputil_lookup_openflow_message(&ofpst_request_category,
543 ntohs(request->type),
544 ntohs(oh->length), typep);
545 if (!error && request->type == htons(OFPST_VENDOR)) {
546 error = ofputil_decode_nxst_request(oh, typep);
552 ofputil_decode_ofpst_reply(const struct ofp_header *oh,
553 const struct ofputil_msg_type **typep)
555 static const struct ofputil_msg_type ofpst_replies[] = {
556 { OFPUTIL_OFPST_DESC_REPLY,
557 OFPST_DESC, "OFPST_DESC reply",
558 sizeof(struct ofp_desc_stats), 0 },
560 { OFPUTIL_OFPST_FLOW_REPLY,
561 OFPST_FLOW, "OFPST_FLOW reply",
562 sizeof(struct ofp_stats_msg), 1 },
564 { OFPUTIL_OFPST_AGGREGATE_REPLY,
565 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
566 sizeof(struct ofp_aggregate_stats_reply), 0 },
568 { OFPUTIL_OFPST_TABLE_REPLY,
569 OFPST_TABLE, "OFPST_TABLE reply",
570 sizeof(struct ofp_stats_msg), sizeof(struct ofp_table_stats) },
572 { OFPUTIL_OFPST_PORT_REPLY,
573 OFPST_PORT, "OFPST_PORT reply",
574 sizeof(struct ofp_stats_msg), sizeof(struct ofp_port_stats) },
576 { OFPUTIL_OFPST_QUEUE_REPLY,
577 OFPST_QUEUE, "OFPST_QUEUE reply",
578 sizeof(struct ofp_stats_msg), sizeof(struct ofp_queue_stats) },
581 OFPST_VENDOR, "OFPST_VENDOR reply",
582 sizeof(struct ofp_vendor_stats_msg), 1 },
585 static const struct ofputil_msg_category ofpst_reply_category = {
586 "OpenFlow statistics",
587 ofpst_replies, ARRAY_SIZE(ofpst_replies),
588 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
591 const struct ofp_stats_msg *reply = (const struct ofp_stats_msg *) oh;
594 error = ofputil_lookup_openflow_message(&ofpst_reply_category,
596 ntohs(oh->length), typep);
597 if (!error && reply->type == htons(OFPST_VENDOR)) {
598 error = ofputil_decode_nxst_reply(oh, typep);
603 /* Decodes the message type represented by 'oh'. Returns 0 if successful or
604 * an OpenFlow error code constructed with ofp_mkerr() on failure. Either
605 * way, stores in '*typep' a type structure that can be inspected with the
606 * ofputil_msg_type_*() functions.
608 * oh->length must indicate the correct length of the message (and must be at
609 * least sizeof(struct ofp_header)).
611 * Success indicates that 'oh' is at least as long as the minimum-length
612 * message of its type. */
614 ofputil_decode_msg_type(const struct ofp_header *oh,
615 const struct ofputil_msg_type **typep)
617 static const struct ofputil_msg_type ofpt_messages[] = {
618 { OFPUTIL_OFPT_HELLO,
619 OFPT_HELLO, "OFPT_HELLO",
620 sizeof(struct ofp_hello), 1 },
622 { OFPUTIL_OFPT_ERROR,
623 OFPT_ERROR, "OFPT_ERROR",
624 sizeof(struct ofp_error_msg), 1 },
626 { OFPUTIL_OFPT_ECHO_REQUEST,
627 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
628 sizeof(struct ofp_header), 1 },
630 { OFPUTIL_OFPT_ECHO_REPLY,
631 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
632 sizeof(struct ofp_header), 1 },
634 { OFPUTIL_OFPT_FEATURES_REQUEST,
635 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
636 sizeof(struct ofp_header), 0 },
638 { OFPUTIL_OFPT_FEATURES_REPLY,
639 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
640 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
642 { OFPUTIL_OFPT_GET_CONFIG_REQUEST,
643 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
644 sizeof(struct ofp_header), 0 },
646 { OFPUTIL_OFPT_GET_CONFIG_REPLY,
647 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
648 sizeof(struct ofp_switch_config), 0 },
650 { OFPUTIL_OFPT_SET_CONFIG,
651 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
652 sizeof(struct ofp_switch_config), 0 },
654 { OFPUTIL_OFPT_PACKET_IN,
655 OFPT_PACKET_IN, "OFPT_PACKET_IN",
656 offsetof(struct ofp_packet_in, data), 1 },
658 { OFPUTIL_OFPT_FLOW_REMOVED,
659 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
660 sizeof(struct ofp_flow_removed), 0 },
662 { OFPUTIL_OFPT_PORT_STATUS,
663 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
664 sizeof(struct ofp_port_status), 0 },
666 { OFPUTIL_OFPT_PACKET_OUT,
667 OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
668 sizeof(struct ofp_packet_out), 1 },
670 { OFPUTIL_OFPT_FLOW_MOD,
671 OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
672 sizeof(struct ofp_flow_mod), 1 },
674 { OFPUTIL_OFPT_PORT_MOD,
675 OFPT_PORT_MOD, "OFPT_PORT_MOD",
676 sizeof(struct ofp_port_mod), 0 },
679 OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
680 sizeof(struct ofp_stats_msg), 1 },
683 OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
684 sizeof(struct ofp_stats_msg), 1 },
686 { OFPUTIL_OFPT_BARRIER_REQUEST,
687 OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
688 sizeof(struct ofp_header), 0 },
690 { OFPUTIL_OFPT_BARRIER_REPLY,
691 OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
692 sizeof(struct ofp_header), 0 },
695 OFPT_VENDOR, "OFPT_VENDOR",
696 sizeof(struct ofp_vendor_header), 1 },
699 static const struct ofputil_msg_category ofpt_category = {
701 ofpt_messages, ARRAY_SIZE(ofpt_messages),
702 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE)
707 error = ofputil_lookup_openflow_message(&ofpt_category, oh->type,
708 ntohs(oh->length), typep);
712 error = ofputil_decode_vendor(oh, typep);
715 case OFPT_STATS_REQUEST:
716 error = ofputil_decode_ofpst_request(oh, typep);
719 case OFPT_STATS_REPLY:
720 error = ofputil_decode_ofpst_reply(oh, typep);
727 static const struct ofputil_msg_type ofputil_invalid_type = {
729 0, "OFPUTIL_INVALID",
733 *typep = &ofputil_invalid_type;
738 /* Returns an OFPUTIL_* message type code for 'type'. */
739 enum ofputil_msg_code
740 ofputil_msg_type_code(const struct ofputil_msg_type *type)
748 ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
750 switch (flow_format) {
751 case NXFF_OPENFLOW10:
760 ofputil_flow_format_to_string(enum nx_flow_format flow_format)
762 switch (flow_format) {
763 case NXFF_OPENFLOW10:
773 ofputil_flow_format_from_string(const char *s)
775 return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
776 : !strcmp(s, "nxm") ? NXFF_NXM
781 regs_fully_wildcarded(const struct flow_wildcards *wc)
785 for (i = 0; i < FLOW_N_REGS; i++) {
786 if (wc->reg_masks[i] != 0) {
793 /* Returns the minimum nx_flow_format to use for sending 'rule' to a switch
794 * (e.g. to add or remove a flow). Only NXM can handle tunnel IDs, registers,
795 * or fixing the Ethernet multicast bit. Otherwise, it's better to use
796 * NXFF_OPENFLOW10 for backward compatibility. */
798 ofputil_min_flow_format(const struct cls_rule *rule)
800 const struct flow_wildcards *wc = &rule->wc;
802 /* Only NXM supports separately wildcards the Ethernet multicast bit. */
803 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
807 /* Only NXM supports matching ARP hardware addresses. */
808 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
812 /* Only NXM supports matching IPv6 traffic. */
813 if (!(wc->wildcards & FWW_DL_TYPE)
814 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
818 /* Only NXM supports matching registers. */
819 if (!regs_fully_wildcarded(wc)) {
823 /* Only NXM supports matching tun_id. */
824 if (wc->tun_id_mask != htonll(0)) {
828 /* Other formats can express this rule. */
829 return NXFF_OPENFLOW10;
832 /* Returns an OpenFlow message that can be used to set the flow format to
835 ofputil_make_set_flow_format(enum nx_flow_format flow_format)
837 struct nxt_set_flow_format *sff;
840 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
841 sff->format = htonl(flow_format);
846 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
847 * extension on or off (according to 'flow_mod_table_id'). */
849 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
851 struct nxt_flow_mod_table_id *nfmti;
854 nfmti = make_nxmsg(sizeof *nfmti, NXT_FLOW_MOD_TABLE_ID, &msg);
855 nfmti->set = flow_mod_table_id;
859 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
860 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
863 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
864 * enabled, false otherwise.
866 * Does not validate the flow_mod actions. */
868 ofputil_decode_flow_mod(struct flow_mod *fm, const struct ofp_header *oh,
869 bool flow_mod_table_id)
871 const struct ofputil_msg_type *type;
875 ofpbuf_use_const(&b, oh, ntohs(oh->length));
877 ofputil_decode_msg_type(oh, &type);
878 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
879 /* Standard OpenFlow flow_mod. */
880 const struct ofp_flow_mod *ofm;
884 /* Dissect the message. */
885 ofm = ofpbuf_pull(&b, sizeof *ofm);
886 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
891 /* Set priority based on original wildcards. Normally we'd allow
892 * ofputil_cls_rule_from_match() to do this for us, but
893 * ofputil_normalize_rule() can put wildcards where the original flow
894 * didn't have them. */
895 priority = ntohs(ofm->priority);
896 if (!(ofm->match.wildcards & htonl(OFPFW_ALL))) {
897 priority = UINT16_MAX;
900 /* Translate the rule. */
901 ofputil_cls_rule_from_match(&ofm->match, priority, &fm->cr);
902 ofputil_normalize_rule(&fm->cr, NXFF_OPENFLOW10);
904 /* Translate the message. */
905 fm->cookie = ofm->cookie;
906 command = ntohs(ofm->command);
907 fm->idle_timeout = ntohs(ofm->idle_timeout);
908 fm->hard_timeout = ntohs(ofm->hard_timeout);
909 fm->buffer_id = ntohl(ofm->buffer_id);
910 fm->out_port = ntohs(ofm->out_port);
911 fm->flags = ntohs(ofm->flags);
912 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
913 /* Nicira extended flow_mod. */
914 const struct nx_flow_mod *nfm;
917 /* Dissect the message. */
918 nfm = ofpbuf_pull(&b, sizeof *nfm);
919 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
924 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
929 /* Translate the message. */
930 fm->cookie = nfm->cookie;
931 command = ntohs(nfm->command);
932 fm->idle_timeout = ntohs(nfm->idle_timeout);
933 fm->hard_timeout = ntohs(nfm->hard_timeout);
934 fm->buffer_id = ntohl(nfm->buffer_id);
935 fm->out_port = ntohs(nfm->out_port);
936 fm->flags = ntohs(nfm->flags);
941 if (flow_mod_table_id) {
942 fm->command = command & 0xff;
943 fm->table_id = command >> 8;
945 fm->command = command;
952 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
953 * 'flow_format' and returns the message.
955 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
956 * enabled, false otherwise. */
958 ofputil_encode_flow_mod(const struct flow_mod *fm,
959 enum nx_flow_format flow_format,
960 bool flow_mod_table_id)
962 size_t actions_len = fm->n_actions * sizeof *fm->actions;
966 command = (flow_mod_table_id
967 ? (fm->command & 0xff) | (fm->table_id << 8)
970 if (flow_format == NXFF_OPENFLOW10) {
971 struct ofp_flow_mod *ofm;
973 msg = ofpbuf_new(sizeof *ofm + actions_len);
974 ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
975 ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
976 ofm->cookie = fm->cookie;
977 ofm->command = htons(fm->command);
978 ofm->idle_timeout = htons(fm->idle_timeout);
979 ofm->hard_timeout = htons(fm->hard_timeout);
980 ofm->priority = htons(fm->cr.priority);
981 ofm->buffer_id = htonl(fm->buffer_id);
982 ofm->out_port = htons(fm->out_port);
983 ofm->flags = htons(fm->flags);
984 } else if (flow_format == NXFF_NXM) {
985 struct nx_flow_mod *nfm;
988 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
989 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
990 match_len = nx_put_match(msg, &fm->cr);
993 nfm->cookie = fm->cookie;
994 nfm->command = htons(command);
995 nfm->idle_timeout = htons(fm->idle_timeout);
996 nfm->hard_timeout = htons(fm->hard_timeout);
997 nfm->priority = htons(fm->cr.priority);
998 nfm->buffer_id = htonl(fm->buffer_id);
999 nfm->out_port = htons(fm->out_port);
1000 nfm->flags = htons(fm->flags);
1001 nfm->match_len = htons(match_len);
1006 ofpbuf_put(msg, fm->actions, actions_len);
1007 update_openflow_length(msg);
1012 ofputil_decode_ofpst_flow_request(struct flow_stats_request *fsr,
1013 const struct ofp_header *oh,
1016 const struct ofp_flow_stats_request *ofsr =
1017 (const struct ofp_flow_stats_request *) oh;
1019 fsr->aggregate = aggregate;
1020 ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
1021 fsr->out_port = ntohs(ofsr->out_port);
1022 fsr->table_id = ofsr->table_id;
1028 ofputil_decode_nxst_flow_request(struct flow_stats_request *fsr,
1029 const struct ofp_header *oh,
1032 const struct nx_flow_stats_request *nfsr;
1036 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1038 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1039 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match);
1044 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1047 fsr->aggregate = aggregate;
1048 fsr->out_port = ntohs(nfsr->out_port);
1049 fsr->table_id = nfsr->table_id;
1054 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1055 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1056 * successful, otherwise an OpenFlow error code. */
1058 ofputil_decode_flow_stats_request(struct flow_stats_request *fsr,
1059 const struct ofp_header *oh)
1061 const struct ofputil_msg_type *type;
1065 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1067 ofputil_decode_msg_type(oh, &type);
1068 code = ofputil_msg_type_code(type);
1070 case OFPUTIL_OFPST_FLOW_REQUEST:
1071 return ofputil_decode_ofpst_flow_request(fsr, oh, false);
1073 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1074 return ofputil_decode_ofpst_flow_request(fsr, oh, true);
1076 case OFPUTIL_NXST_FLOW_REQUEST:
1077 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1079 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1080 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1083 /* Hey, the caller lied. */
1088 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1089 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1090 * 'flow_format', and returns the message. */
1092 ofputil_encode_flow_stats_request(const struct flow_stats_request *fsr,
1093 enum nx_flow_format flow_format)
1097 if (flow_format == NXFF_OPENFLOW10) {
1098 struct ofp_flow_stats_request *ofsr;
1101 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1102 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, 0, &msg);
1103 ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
1104 ofsr->table_id = fsr->table_id;
1105 ofsr->out_port = htons(fsr->out_port);
1106 } else if (flow_format == NXFF_NXM) {
1107 struct nx_flow_stats_request *nfsr;
1111 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1112 ofputil_make_stats_request(sizeof *nfsr, OFPST_VENDOR, subtype, &msg);
1113 match_len = nx_put_match(msg, &fsr->match);
1116 nfsr->out_port = htons(fsr->out_port);
1117 nfsr->match_len = htons(match_len);
1118 nfsr->table_id = fsr->table_id;
1126 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1127 * ofputil_flow_stats in 'fs'.
1129 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1130 * OpenFlow message. Calling this function multiple times for a single 'msg'
1131 * iterates through the replies. The caller must initially leave 'msg''s layer
1132 * pointers null and not modify them between calls.
1134 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1135 * otherwise a positive errno value. */
1137 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1140 const struct ofputil_msg_type *type;
1143 ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1144 code = ofputil_msg_type_code(type);
1146 msg->l2 = msg->data;
1147 if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1148 ofpbuf_pull(msg, sizeof(struct ofp_stats_msg));
1149 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1150 ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1158 } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1159 const struct ofp_flow_stats *ofs;
1162 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1164 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1165 "bytes at end", msg->size);
1169 length = ntohs(ofs->length);
1170 if (length < sizeof *ofs) {
1171 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1172 "length %zu", length);
1176 if (ofputil_pull_actions(msg, length - sizeof *ofs,
1177 &fs->actions, &fs->n_actions)) {
1181 fs->cookie = get_32aligned_be64(&ofs->cookie);
1182 ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
1184 fs->table_id = ofs->table_id;
1185 fs->duration_sec = ntohl(ofs->duration_sec);
1186 fs->duration_nsec = ntohl(ofs->duration_nsec);
1187 fs->idle_timeout = ntohs(ofs->idle_timeout);
1188 fs->hard_timeout = ntohs(ofs->hard_timeout);
1189 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1190 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1191 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1192 const struct nx_flow_stats *nfs;
1193 size_t match_len, length;
1195 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1197 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1198 "bytes at end", msg->size);
1202 length = ntohs(nfs->length);
1203 match_len = ntohs(nfs->match_len);
1204 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1205 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1206 "claims invalid length %zu", match_len, length);
1209 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule)) {
1213 if (ofputil_pull_actions(msg,
1214 length - sizeof *nfs - ROUND_UP(match_len, 8),
1215 &fs->actions, &fs->n_actions)) {
1219 fs->cookie = nfs->cookie;
1220 fs->table_id = nfs->table_id;
1221 fs->duration_sec = ntohl(nfs->duration_sec);
1222 fs->duration_nsec = ntohl(nfs->duration_nsec);
1223 fs->idle_timeout = ntohs(nfs->idle_timeout);
1224 fs->hard_timeout = ntohs(nfs->hard_timeout);
1225 fs->packet_count = ntohll(nfs->packet_count);
1226 fs->byte_count = ntohll(nfs->byte_count);
1234 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1235 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1236 * an OpenFlow error code. */
1238 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1239 const struct ofp_header *oh)
1241 const struct ofputil_msg_type *type;
1242 enum ofputil_msg_code code;
1244 ofputil_decode_msg_type(oh, &type);
1245 code = ofputil_msg_type_code(type);
1246 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1247 const struct ofp_flow_removed *ofr;
1249 ofr = (const struct ofp_flow_removed *) oh;
1250 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
1252 fr->cookie = ofr->cookie;
1253 fr->reason = ofr->reason;
1254 fr->duration_sec = ntohl(ofr->duration_sec);
1255 fr->duration_nsec = ntohl(ofr->duration_nsec);
1256 fr->idle_timeout = ntohs(ofr->idle_timeout);
1257 fr->packet_count = ntohll(ofr->packet_count);
1258 fr->byte_count = ntohll(ofr->byte_count);
1259 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1260 struct nx_flow_removed *nfr;
1264 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1266 nfr = ofpbuf_pull(&b, sizeof *nfr);
1267 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1273 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1276 fr->cookie = nfr->cookie;
1277 fr->reason = nfr->reason;
1278 fr->duration_sec = ntohl(nfr->duration_sec);
1279 fr->duration_nsec = ntohl(nfr->duration_nsec);
1280 fr->idle_timeout = ntohs(nfr->idle_timeout);
1281 fr->packet_count = ntohll(nfr->packet_count);
1282 fr->byte_count = ntohll(nfr->byte_count);
1290 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1291 * NXT_FLOW_REMOVED message 'oh' according to 'flow_format', and returns the
1294 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1295 enum nx_flow_format flow_format)
1299 if (flow_format == NXFF_OPENFLOW10) {
1300 struct ofp_flow_removed *ofr;
1302 ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
1304 ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
1305 ofr->cookie = fr->cookie;
1306 ofr->priority = htons(fr->rule.priority);
1307 ofr->reason = fr->reason;
1308 ofr->duration_sec = htonl(fr->duration_sec);
1309 ofr->duration_nsec = htonl(fr->duration_nsec);
1310 ofr->idle_timeout = htons(fr->idle_timeout);
1311 ofr->packet_count = htonll(fr->packet_count);
1312 ofr->byte_count = htonll(fr->byte_count);
1313 } else if (flow_format == NXFF_NXM) {
1314 struct nx_flow_removed *nfr;
1317 make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
1318 match_len = nx_put_match(msg, &fr->rule);
1321 nfr->cookie = fr->cookie;
1322 nfr->priority = htons(fr->rule.priority);
1323 nfr->reason = fr->reason;
1324 nfr->duration_sec = htonl(fr->duration_sec);
1325 nfr->duration_nsec = htonl(fr->duration_nsec);
1326 nfr->idle_timeout = htons(fr->idle_timeout);
1327 nfr->match_len = htons(match_len);
1328 nfr->packet_count = htonll(fr->packet_count);
1329 nfr->byte_count = htonll(fr->byte_count);
1337 /* Converts abstract ofputil_packet_in 'pin' into an OFPT_PACKET_IN message
1338 * and returns the message.
1340 * If 'rw_packet' is NULL, the caller takes ownership of the newly allocated
1343 * If 'rw_packet' is nonnull, then it must contain the same data as
1344 * pin->packet. 'rw_packet' is allowed to be the same ofpbuf as pin->packet.
1345 * It is modified in-place into an OFPT_PACKET_IN message according to 'pin',
1346 * and then ofputil_encode_packet_in() returns 'rw_packet'. If 'rw_packet' has
1347 * enough headroom to insert a "struct ofp_packet_in", this is more efficient
1348 * than ofputil_encode_packet_in() because it does not copy the packet
1351 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
1352 struct ofpbuf *rw_packet)
1354 int total_len = pin->packet->size;
1355 struct ofp_packet_in *opi;
1358 if (pin->send_len < rw_packet->size) {
1359 rw_packet->size = pin->send_len;
1362 rw_packet = ofpbuf_clone_data_with_headroom(
1363 pin->packet->data, MIN(pin->send_len, pin->packet->size),
1364 offsetof(struct ofp_packet_in, data));
1367 /* Add OFPT_PACKET_IN. */
1368 opi = ofpbuf_push_zeros(rw_packet, offsetof(struct ofp_packet_in, data));
1369 opi->header.version = OFP_VERSION;
1370 opi->header.type = OFPT_PACKET_IN;
1371 opi->total_len = htons(total_len);
1372 opi->in_port = htons(pin->in_port);
1373 opi->reason = pin->reason;
1374 opi->buffer_id = htonl(pin->buffer_id);
1375 update_openflow_length(rw_packet);
1380 /* Returns a string representing the message type of 'type'. The string is the
1381 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
1382 * messages, the constant is followed by "request" or "reply",
1383 * e.g. "OFPST_AGGREGATE reply". */
1385 ofputil_msg_type_name(const struct ofputil_msg_type *type)
1390 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1391 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1392 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
1395 * The caller is responsible for freeing '*bufferp' when it is no longer
1398 * The OpenFlow header length is initially set to 'openflow_len'; if the
1399 * message is later extended, the length should be updated with
1400 * update_openflow_length() before sending.
1402 * Returns the header. */
1404 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1406 *bufferp = ofpbuf_new(openflow_len);
1407 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1410 /* Similar to make_openflow() but creates a Nicira vendor extension message
1411 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1413 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1415 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
1418 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1419 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1420 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
1423 * The caller is responsible for freeing '*bufferp' when it is no longer
1426 * The OpenFlow header length is initially set to 'openflow_len'; if the
1427 * message is later extended, the length should be updated with
1428 * update_openflow_length() before sending.
1430 * Returns the header. */
1432 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1433 struct ofpbuf **bufferp)
1435 *bufferp = ofpbuf_new(openflow_len);
1436 return put_openflow_xid(openflow_len, type, xid, *bufferp);
1439 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
1440 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1442 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1443 struct ofpbuf **bufferp)
1445 *bufferp = ofpbuf_new(openflow_len);
1446 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
1449 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1450 * with the given 'type' and an arbitrary transaction id. Allocated bytes
1451 * beyond the header, if any, are zeroed.
1453 * The OpenFlow header length is initially set to 'openflow_len'; if the
1454 * message is later extended, the length should be updated with
1455 * update_openflow_length() before sending.
1457 * Returns the header. */
1459 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1461 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
1464 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1465 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
1466 * the header, if any, are zeroed.
1468 * The OpenFlow header length is initially set to 'openflow_len'; if the
1469 * message is later extended, the length should be updated with
1470 * update_openflow_length() before sending.
1472 * Returns the header. */
1474 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1475 struct ofpbuf *buffer)
1477 struct ofp_header *oh;
1479 assert(openflow_len >= sizeof *oh);
1480 assert(openflow_len <= UINT16_MAX);
1482 oh = ofpbuf_put_uninit(buffer, openflow_len);
1483 oh->version = OFP_VERSION;
1485 oh->length = htons(openflow_len);
1487 memset(oh + 1, 0, openflow_len - sizeof *oh);
1491 /* Similar to put_openflow() but append a Nicira vendor extension message with
1492 * the specific 'subtype'. 'subtype' should be in host byte order. */
1494 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
1496 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
1499 /* Similar to put_openflow_xid() but append a Nicira vendor extension message
1500 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1502 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1503 struct ofpbuf *buffer)
1505 struct nicira_header *nxh;
1507 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
1508 nxh->vendor = htonl(NX_VENDOR_ID);
1509 nxh->subtype = htonl(subtype);
1513 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
1514 * 'buffer->size'. */
1516 update_openflow_length(struct ofpbuf *buffer)
1518 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
1519 oh->length = htons(buffer->size);
1523 put_stats__(ovs_be32 xid, uint8_t ofp_type,
1524 ovs_be16 ofpst_type, ovs_be32 nxst_subtype,
1527 if (ofpst_type == htons(OFPST_VENDOR)) {
1528 struct nicira_stats_msg *nsm;
1530 nsm = put_openflow_xid(sizeof *nsm, ofp_type, xid, msg);
1531 nsm->vsm.osm.type = ofpst_type;
1532 nsm->vsm.vendor = htonl(NX_VENDOR_ID);
1533 nsm->subtype = nxst_subtype;
1535 struct ofp_stats_msg *osm;
1537 osm = put_openflow_xid(sizeof *osm, ofp_type, xid, msg);
1538 osm->type = ofpst_type;
1542 /* Creates a statistics request message with total length 'openflow_len'
1543 * (including all headers) and the given 'ofpst_type', and stores the buffer
1544 * containing the new message in '*bufferp'. If 'ofpst_type' is OFPST_VENDOR
1545 * then 'nxst_subtype' is used as the Nicira vendor extension statistics
1546 * subtype (otherwise 'nxst_subtype' is ignored).
1548 * Initializes bytes following the headers to all-bits-zero.
1550 * Returns the first byte of the new message. */
1552 ofputil_make_stats_request(size_t openflow_len, uint16_t ofpst_type,
1553 uint32_t nxst_subtype, struct ofpbuf **bufferp)
1557 msg = *bufferp = ofpbuf_new(openflow_len);
1558 put_stats__(alloc_xid(), OFPT_STATS_REQUEST,
1559 htons(ofpst_type), htonl(nxst_subtype), msg);
1560 ofpbuf_padto(msg, openflow_len);
1566 put_stats_reply__(const struct ofp_stats_msg *request, struct ofpbuf *msg)
1568 assert(request->header.type == OFPT_STATS_REQUEST ||
1569 request->header.type == OFPT_STATS_REPLY);
1570 put_stats__(request->header.xid, OFPT_STATS_REPLY, request->type,
1571 (request->type != htons(OFPST_VENDOR)
1573 : ((const struct nicira_stats_msg *) request)->subtype),
1577 /* Creates a statistics reply message with total length 'openflow_len'
1578 * (including all headers) and the same type (either a standard OpenFlow
1579 * statistics type or a Nicira extension type and subtype) as 'request', and
1580 * stores the buffer containing the new message in '*bufferp'.
1582 * Initializes bytes following the headers to all-bits-zero.
1584 * Returns the first byte of the new message. */
1586 ofputil_make_stats_reply(size_t openflow_len,
1587 const struct ofp_stats_msg *request,
1588 struct ofpbuf **bufferp)
1592 msg = *bufferp = ofpbuf_new(openflow_len);
1593 put_stats_reply__(request, msg);
1594 ofpbuf_padto(msg, openflow_len);
1599 /* Initializes 'replies' as a list of ofpbufs that will contain a series of
1600 * replies to 'request', which should be an OpenFlow or Nicira extension
1601 * statistics request. Initially 'replies' will have a single reply message
1602 * that has only a header. The functions ofputil_reserve_stats_reply() and
1603 * ofputil_append_stats_reply() may be used to add to the reply. */
1605 ofputil_start_stats_reply(const struct ofp_stats_msg *request,
1606 struct list *replies)
1610 msg = ofpbuf_new(1024);
1611 put_stats_reply__(request, msg);
1614 list_push_back(replies, &msg->list_node);
1617 /* Prepares to append up to 'len' bytes to the series of statistics replies in
1618 * 'replies', which should have been initialized with
1619 * ofputil_start_stats_reply(). Returns an ofpbuf with at least 'len' bytes of
1620 * tailroom. (The 'len' bytes have not actually be allocated; the caller must
1621 * do so with e.g. ofpbuf_put_uninit().) */
1623 ofputil_reserve_stats_reply(size_t len, struct list *replies)
1625 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
1626 struct ofp_stats_msg *osm = msg->data;
1628 if (msg->size + len <= UINT16_MAX) {
1629 ofpbuf_prealloc_tailroom(msg, len);
1631 osm->flags |= htons(OFPSF_REPLY_MORE);
1633 msg = ofpbuf_new(MAX(1024, sizeof(struct nicira_stats_msg) + len));
1634 put_stats_reply__(osm, msg);
1635 list_push_back(replies, &msg->list_node);
1640 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
1641 * returns the first byte. */
1643 ofputil_append_stats_reply(size_t len, struct list *replies)
1645 return ofpbuf_put_uninit(ofputil_reserve_stats_reply(len, replies), len);
1648 /* Returns the first byte past the ofp_stats_msg header in 'oh'. */
1650 ofputil_stats_body(const struct ofp_header *oh)
1652 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1653 return (const struct ofp_stats_msg *) oh + 1;
1656 /* Returns the number of bytes past the ofp_stats_msg header in 'oh'. */
1658 ofputil_stats_body_len(const struct ofp_header *oh)
1660 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1661 return ntohs(oh->length) - sizeof(struct ofp_stats_msg);
1664 /* Returns the first byte past the nicira_stats_msg header in 'oh'. */
1666 ofputil_nxstats_body(const struct ofp_header *oh)
1668 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1669 return ((const struct nicira_stats_msg *) oh) + 1;
1672 /* Returns the number of bytes past the nicira_stats_msg header in 'oh'. */
1674 ofputil_nxstats_body_len(const struct ofp_header *oh)
1676 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1677 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
1681 make_flow_mod(uint16_t command, const struct cls_rule *rule,
1684 struct ofp_flow_mod *ofm;
1685 size_t size = sizeof *ofm + actions_len;
1686 struct ofpbuf *out = ofpbuf_new(size);
1687 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
1688 ofm->header.version = OFP_VERSION;
1689 ofm->header.type = OFPT_FLOW_MOD;
1690 ofm->header.length = htons(size);
1692 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
1693 ofputil_cls_rule_to_match(rule, &ofm->match);
1694 ofm->command = htons(command);
1699 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
1700 uint16_t idle_timeout, size_t actions_len)
1702 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
1703 struct ofp_flow_mod *ofm = out->data;
1704 ofm->idle_timeout = htons(idle_timeout);
1705 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
1706 ofm->buffer_id = htonl(buffer_id);
1711 make_del_flow(const struct cls_rule *rule)
1713 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
1714 struct ofp_flow_mod *ofm = out->data;
1715 ofm->out_port = htons(OFPP_NONE);
1720 make_add_simple_flow(const struct cls_rule *rule,
1721 uint32_t buffer_id, uint16_t out_port,
1722 uint16_t idle_timeout)
1724 if (out_port != OFPP_NONE) {
1725 struct ofp_action_output *oao;
1726 struct ofpbuf *buffer;
1728 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
1729 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
1730 oao->type = htons(OFPAT_OUTPUT);
1731 oao->len = htons(sizeof *oao);
1732 oao->port = htons(out_port);
1735 return make_add_flow(rule, buffer_id, idle_timeout, 0);
1740 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
1741 const struct ofpbuf *payload, int max_send_len)
1743 struct ofp_packet_in *opi;
1747 send_len = MIN(max_send_len, payload->size);
1748 buf = ofpbuf_new(sizeof *opi + send_len);
1749 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
1750 OFPT_PACKET_IN, 0, buf);
1751 opi->buffer_id = htonl(buffer_id);
1752 opi->total_len = htons(payload->size);
1753 opi->in_port = htons(in_port);
1754 opi->reason = reason;
1755 ofpbuf_put(buf, payload->data, send_len);
1756 update_openflow_length(buf);
1762 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1764 const struct ofp_action_header *actions, size_t n_actions)
1766 size_t actions_len = n_actions * sizeof *actions;
1767 struct ofp_packet_out *opo;
1768 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
1769 struct ofpbuf *out = ofpbuf_new(size);
1771 opo = ofpbuf_put_uninit(out, sizeof *opo);
1772 opo->header.version = OFP_VERSION;
1773 opo->header.type = OFPT_PACKET_OUT;
1774 opo->header.length = htons(size);
1775 opo->header.xid = htonl(0);
1776 opo->buffer_id = htonl(buffer_id);
1777 opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
1778 opo->actions_len = htons(actions_len);
1779 ofpbuf_put(out, actions, actions_len);
1781 ofpbuf_put(out, packet->data, packet->size);
1787 make_unbuffered_packet_out(const struct ofpbuf *packet,
1788 uint16_t in_port, uint16_t out_port)
1790 struct ofp_action_output action;
1791 action.type = htons(OFPAT_OUTPUT);
1792 action.len = htons(sizeof action);
1793 action.port = htons(out_port);
1794 return make_packet_out(packet, UINT32_MAX, in_port,
1795 (struct ofp_action_header *) &action, 1);
1799 make_buffered_packet_out(uint32_t buffer_id,
1800 uint16_t in_port, uint16_t out_port)
1802 if (out_port != OFPP_NONE) {
1803 struct ofp_action_output action;
1804 action.type = htons(OFPAT_OUTPUT);
1805 action.len = htons(sizeof action);
1806 action.port = htons(out_port);
1807 return make_packet_out(NULL, buffer_id, in_port,
1808 (struct ofp_action_header *) &action, 1);
1810 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
1814 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1816 make_echo_request(void)
1818 struct ofp_header *rq;
1819 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1820 rq = ofpbuf_put_uninit(out, sizeof *rq);
1821 rq->version = OFP_VERSION;
1822 rq->type = OFPT_ECHO_REQUEST;
1823 rq->length = htons(sizeof *rq);
1828 /* Creates and returns an OFPT_ECHO_REPLY message matching the
1829 * OFPT_ECHO_REQUEST message in 'rq'. */
1831 make_echo_reply(const struct ofp_header *rq)
1833 size_t size = ntohs(rq->length);
1834 struct ofpbuf *out = ofpbuf_new(size);
1835 struct ofp_header *reply = ofpbuf_put(out, rq, size);
1836 reply->type = OFPT_ECHO_REPLY;
1841 check_action_exact_len(const union ofp_action *a, unsigned int len,
1842 unsigned int required_len)
1844 if (len != required_len) {
1845 VLOG_WARN_RL(&bad_ofmsg_rl, "action %"PRIu16" has invalid length "
1846 "%"PRIu16" (must be %u)\n",
1847 ntohs(a->type), ntohs(a->header.len), required_len);
1848 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1854 check_nx_action_exact_len(const struct nx_action_header *a,
1855 unsigned int len, unsigned int required_len)
1857 if (len != required_len) {
1858 VLOG_WARN_RL(&bad_ofmsg_rl,
1859 "Nicira action %"PRIu16" has invalid length %"PRIu16" "
1861 ntohs(a->subtype), ntohs(a->len), required_len);
1862 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1867 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
1868 * that the switch will never have more than 'max_ports' ports. Returns 0 if
1869 * 'port' is valid, otherwise an ofp_mkerr() return code. */
1871 check_output_port(uint16_t port, int max_ports)
1879 case OFPP_CONTROLLER:
1884 if (port < max_ports) {
1887 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
1888 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1892 /* Checks that 'action' is a valid OFPAT_ENQUEUE action, given that the switch
1893 * will never have more than 'max_ports' ports. Returns 0 if 'port' is valid,
1894 * otherwise an ofp_mkerr() return code. */
1896 check_enqueue_action(const union ofp_action *a, unsigned int len,
1899 const struct ofp_action_enqueue *oae;
1903 error = check_action_exact_len(a, len, 16);
1908 oae = (const struct ofp_action_enqueue *) a;
1909 port = ntohs(oae->port);
1910 if (port < max_ports || port == OFPP_IN_PORT) {
1913 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown enqueue port %x", port);
1914 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1918 check_nicira_action(const union ofp_action *a, unsigned int len,
1919 const struct flow *flow)
1921 const struct nx_action_header *nah;
1926 VLOG_WARN_RL(&bad_ofmsg_rl,
1927 "Nicira vendor action only %u bytes", len);
1928 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1930 nah = (const struct nx_action_header *) a;
1932 subtype = ntohs(nah->subtype);
1933 if (subtype > TYPE_MAXIMUM(enum nx_action_subtype)) {
1934 /* This is necessary because enum nx_action_subtype may be an
1935 * 8-bit type, so the cast below throws away the top 8 bits. */
1936 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1939 switch ((enum nx_action_subtype) subtype) {
1940 case NXAST_RESUBMIT:
1941 case NXAST_SET_TUNNEL:
1942 case NXAST_SET_QUEUE:
1943 case NXAST_POP_QUEUE:
1944 return check_nx_action_exact_len(nah, len, 16);
1946 case NXAST_REG_MOVE:
1947 error = check_nx_action_exact_len(nah, len,
1948 sizeof(struct nx_action_reg_move));
1952 return nxm_check_reg_move((const struct nx_action_reg_move *) a, flow);
1954 case NXAST_REG_LOAD:
1955 error = check_nx_action_exact_len(nah, len,
1956 sizeof(struct nx_action_reg_load));
1960 return nxm_check_reg_load((const struct nx_action_reg_load *) a, flow);
1965 case NXAST_SET_TUNNEL64:
1966 return check_nx_action_exact_len(
1967 nah, len, sizeof(struct nx_action_set_tunnel64));
1969 case NXAST_MULTIPATH:
1970 error = check_nx_action_exact_len(
1971 nah, len, sizeof(struct nx_action_multipath));
1975 return multipath_check((const struct nx_action_multipath *) a);
1977 case NXAST_AUTOPATH:
1978 error = check_nx_action_exact_len(
1979 nah, len, sizeof(struct nx_action_autopath));
1983 return autopath_check((const struct nx_action_autopath *) a);
1985 case NXAST_SNAT__OBSOLETE:
1986 case NXAST_DROP_SPOOFED_ARP__OBSOLETE:
1988 VLOG_WARN_RL(&bad_ofmsg_rl,
1989 "unknown Nicira vendor action subtype %d", subtype);
1990 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1995 check_action(const union ofp_action *a, unsigned int len,
1996 const struct flow *flow, int max_ports)
1998 enum ofp_action_type type = ntohs(a->type);
2003 error = check_action_exact_len(a, len, 8);
2007 return check_output_port(ntohs(a->output.port), max_ports);
2009 case OFPAT_SET_VLAN_VID:
2010 error = check_action_exact_len(a, len, 8);
2014 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
2015 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
2019 case OFPAT_SET_VLAN_PCP:
2020 error = check_action_exact_len(a, len, 8);
2024 if (a->vlan_pcp.vlan_pcp & ~7) {
2025 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
2029 case OFPAT_STRIP_VLAN:
2030 case OFPAT_SET_NW_SRC:
2031 case OFPAT_SET_NW_DST:
2032 case OFPAT_SET_NW_TOS:
2033 case OFPAT_SET_TP_SRC:
2034 case OFPAT_SET_TP_DST:
2035 return check_action_exact_len(a, len, 8);
2037 case OFPAT_SET_DL_SRC:
2038 case OFPAT_SET_DL_DST:
2039 return check_action_exact_len(a, len, 16);
2042 return (a->vendor.vendor == htonl(NX_VENDOR_ID)
2043 ? check_nicira_action(a, len, flow)
2044 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
2047 return check_enqueue_action(a, len, max_ports);
2050 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %d", (int) type);
2051 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
2056 validate_actions(const union ofp_action *actions, size_t n_actions,
2057 const struct flow *flow, int max_ports)
2061 for (i = 0; i < n_actions; ) {
2062 const union ofp_action *a = &actions[i];
2063 unsigned int len = ntohs(a->header.len);
2064 unsigned int n_slots = len / OFP_ACTION_ALIGN;
2065 unsigned int slots_left = &actions[n_actions] - a;
2068 if (n_slots > slots_left) {
2069 VLOG_WARN_RL(&bad_ofmsg_rl,
2070 "action requires %u slots but only %u remain",
2071 n_slots, slots_left);
2072 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
2074 VLOG_WARN_RL(&bad_ofmsg_rl, "action has invalid length 0");
2075 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
2076 } else if (len % OFP_ACTION_ALIGN) {
2077 VLOG_WARN_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
2078 "of %d", len, OFP_ACTION_ALIGN);
2079 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
2082 error = check_action(a, len, flow, max_ports);
2091 /* Returns true if 'action' outputs to 'port', false otherwise. */
2093 action_outputs_to_port(const union ofp_action *action, ovs_be16 port)
2095 switch (ntohs(action->type)) {
2097 return action->output.port == port;
2099 return ((const struct ofp_action_enqueue *) action)->port == port;
2105 /* The set of actions must either come from a trusted source or have been
2106 * previously validated with validate_actions(). */
2107 const union ofp_action *
2108 actions_first(struct actions_iterator *iter,
2109 const union ofp_action *oa, size_t n_actions)
2112 iter->end = oa + n_actions;
2113 return actions_next(iter);
2116 const union ofp_action *
2117 actions_next(struct actions_iterator *iter)
2119 if (iter->pos != iter->end) {
2120 const union ofp_action *a = iter->pos;
2121 unsigned int len = ntohs(a->header.len);
2122 iter->pos += len / OFP_ACTION_ALIGN;
2129 /* "Normalizes" the wildcards in 'rule'. That means:
2131 * 1. If the type of level N is known, then only the valid fields for that
2132 * level may be specified. For example, ARP does not have a TOS field,
2133 * so nw_tos must be wildcarded if 'rule' specifies an ARP flow.
2134 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
2135 * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an
2138 * 2. If the type of level N is not known (or not understood by Open
2139 * vSwitch), then no fields at all for that level may be specified. For
2140 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
2141 * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an
2144 * 'flow_format' specifies the format of the flow as received or as intended to
2145 * be sent. This is important for IPv6 and ARP, for which NXM supports more
2146 * detailed matching. */
2148 ofputil_normalize_rule(struct cls_rule *rule, enum nx_flow_format flow_format)
2151 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
2152 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
2153 MAY_NW_PROTO = 1 << 2, /* nw_proto */
2154 MAY_NW_TOS = 1 << 3, /* nw_tos */
2155 MAY_ARP_SHA = 1 << 4, /* arp_sha */
2156 MAY_ARP_THA = 1 << 5, /* arp_tha */
2157 MAY_IPV6_ADDR = 1 << 6, /* ipv6_src, ipv6_dst */
2158 MAY_ND_TARGET = 1 << 7 /* nd_target */
2161 struct flow_wildcards wc;
2163 /* Figure out what fields may be matched. */
2164 if (rule->flow.dl_type == htons(ETH_TYPE_IP)) {
2165 may_match = MAY_NW_PROTO | MAY_NW_TOS | MAY_NW_ADDR;
2166 if (rule->flow.nw_proto == IPPROTO_TCP ||
2167 rule->flow.nw_proto == IPPROTO_UDP ||
2168 rule->flow.nw_proto == IPPROTO_ICMP) {
2169 may_match |= MAY_TP_ADDR;
2171 } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)
2172 && flow_format == NXFF_NXM) {
2173 may_match = MAY_NW_PROTO | MAY_NW_TOS | MAY_IPV6_ADDR;
2174 if (rule->flow.nw_proto == IPPROTO_TCP ||
2175 rule->flow.nw_proto == IPPROTO_UDP) {
2176 may_match |= MAY_TP_ADDR;
2177 } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) {
2178 may_match |= MAY_TP_ADDR;
2179 if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
2180 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
2181 } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
2182 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
2185 } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) {
2186 may_match = MAY_NW_PROTO | MAY_NW_ADDR;
2187 if (flow_format == NXFF_NXM) {
2188 may_match |= MAY_ARP_SHA | MAY_ARP_THA;
2194 /* Clear the fields that may not be matched. */
2196 if (!(may_match & MAY_NW_ADDR)) {
2197 wc.nw_src_mask = wc.nw_dst_mask = htonl(0);
2199 if (!(may_match & MAY_TP_ADDR)) {
2200 wc.wildcards |= FWW_TP_SRC | FWW_TP_DST;
2202 if (!(may_match & MAY_NW_PROTO)) {
2203 wc.wildcards |= FWW_NW_PROTO;
2205 if (!(may_match & MAY_NW_TOS)) {
2206 wc.wildcards |= FWW_NW_TOS;
2208 if (!(may_match & MAY_ARP_SHA)) {
2209 wc.wildcards |= FWW_ARP_SHA;
2211 if (!(may_match & MAY_ARP_THA)) {
2212 wc.wildcards |= FWW_ARP_THA;
2214 if (!(may_match & MAY_IPV6_ADDR)) {
2215 wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any;
2217 if (!(may_match & MAY_ND_TARGET)) {
2218 wc.wildcards |= FWW_ND_TARGET;
2221 /* Log any changes. */
2222 if (!flow_wildcards_equal(&wc, &rule->wc)) {
2223 bool log = !VLOG_DROP_INFO(&bad_ofmsg_rl);
2224 char *pre = log ? cls_rule_to_string(rule) : NULL;
2227 cls_rule_zero_wildcarded_fields(rule);
2230 char *post = cls_rule_to_string(rule);
2231 VLOG_INFO("normalization changed ofp_match, details:");
2232 VLOG_INFO(" pre: %s", pre);
2233 VLOG_INFO("post: %s", post);
2241 vendor_code_to_id(uint8_t code)
2244 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
2246 #undef OFPUTIL_VENDOR
2253 vendor_id_to_code(uint32_t id)
2256 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case VENDOR_ID: return NAME;
2258 #undef OFPUTIL_VENDOR
2264 /* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
2265 * information taken from 'error', whose encoding must be as described in the
2266 * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
2267 * oh->xid as its transaction ID, and it will include up to the first 64 bytes
2270 * Returns NULL if 'error' is not an OpenFlow error code. */
2272 ofputil_encode_error_msg(int error, const struct ofp_header *oh)
2274 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2284 if (!is_ofp_error(error)) {
2285 /* We format 'error' with strerror() here since it seems likely to be
2286 * a system errno value. */
2287 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
2288 error, strerror(error));
2295 len = ntohs(oh->length);
2305 vendor = get_ofp_err_vendor(error);
2306 type = get_ofp_err_type(error);
2307 code = get_ofp_err_code(error);
2308 if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
2309 struct ofp_error_msg *oem;
2311 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
2312 oem->type = htons(type);
2313 oem->code = htons(code);
2315 struct ofp_error_msg *oem;
2316 struct nx_vendor_error *nve;
2319 vendor_id = vendor_code_to_id(vendor);
2320 if (vendor_id == UINT32_MAX) {
2321 VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
2326 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
2327 OFPT_ERROR, xid, &buf);
2328 oem->type = htons(NXET_VENDOR);
2329 oem->code = htons(NXVC_VENDOR_ERROR);
2331 nve = (struct nx_vendor_error *)oem->data;
2332 nve->vendor = htonl(vendor_id);
2333 nve->type = htons(type);
2334 nve->code = htons(code);
2339 ofpbuf_put(buf, data, len);
2345 /* Decodes 'oh', which should be an OpenFlow OFPT_ERROR message, and returns an
2346 * Open vSwitch internal error code in the format described in the large
2347 * comment in ofp-util.h.
2349 * If 'payload_ofs' is nonnull, on success '*payload_ofs' is set to the offset
2350 * to the payload starting from 'oh' and on failure it is set to 0. */
2352 ofputil_decode_error_msg(const struct ofp_header *oh, size_t *payload_ofs)
2354 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2356 const struct ofp_error_msg *oem;
2357 uint16_t type, code;
2364 if (oh->type != OFPT_ERROR) {
2368 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2369 oem = ofpbuf_try_pull(&b, sizeof *oem);
2374 type = ntohs(oem->type);
2375 code = ntohs(oem->code);
2376 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
2377 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
2382 vendor = vendor_id_to_code(ntohl(nve->vendor));
2384 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
2385 ntohl(nve->vendor));
2388 type = ntohs(nve->type);
2389 code = ntohs(nve->code);
2391 vendor = OFPUTIL_VENDOR_OPENFLOW;
2395 VLOG_WARN_RL(&rl, "error contains type %"PRIu16" greater than "
2396 "supported maximum value 1023", type);
2401 *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
2403 return ofp_mkerr_vendor(vendor, type, code);
2407 ofputil_format_error(struct ds *s, int error)
2409 if (is_errno(error)) {
2410 ds_put_cstr(s, strerror(error));
2412 uint16_t type = get_ofp_err_type(error);
2413 uint16_t code = get_ofp_err_code(error);
2414 const char *type_s = ofp_error_type_to_string(type);
2415 const char *code_s = ofp_error_code_to_string(type, code);
2417 ds_put_format(s, "type ");
2419 ds_put_cstr(s, type_s);
2421 ds_put_format(s, "%"PRIu16, type);
2424 ds_put_cstr(s, ", code ");
2426 ds_put_cstr(s, code_s);
2428 ds_put_format(s, "%"PRIu16, code);
2434 ofputil_error_to_string(int error)
2436 struct ds s = DS_EMPTY_INITIALIZER;
2437 ofputil_format_error(&s, error);
2438 return ds_steal_cstr(&s);
2441 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
2442 * successful, otherwise an OpenFlow error.
2444 * If successful, the first action is stored in '*actionsp' and the number of
2445 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
2446 * are stored, respectively.
2448 * This function does not check that the actions are valid (the caller should
2449 * do so, with validate_actions()). The caller is also responsible for making
2450 * sure that 'b->data' is initially aligned appropriately for "union
2453 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2454 union ofp_action **actionsp, size_t *n_actionsp)
2456 if (actions_len % OFP_ACTION_ALIGN != 0) {
2457 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2458 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
2462 *actionsp = ofpbuf_try_pull(b, actions_len);
2463 if (*actionsp == NULL) {
2464 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2465 "exceeds remaining message length (%zu)",
2466 actions_len, b->size);
2470 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
2476 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);