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"
22 #include "byte-order.h"
23 #include "classifier.h"
24 #include "dynamic-string.h"
25 #include "multipath.h"
27 #include "ofp-errors.h"
32 #include "type-props.h"
35 VLOG_DEFINE_THIS_MODULE(ofp_util);
37 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
38 * in the peer and so there's not much point in showing a lot of them. */
39 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
41 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
42 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
45 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
46 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
47 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
48 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
51 ofputil_wcbits_to_netmask(int wcbits)
54 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
57 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
58 * that it wildcards. 'netmask' must be a CIDR netmask (see ip_is_cidr()). */
60 ofputil_netmask_to_wcbits(ovs_be32 netmask)
62 assert(ip_is_cidr(netmask));
64 return netmask == htonl(0) ? 32 : __builtin_ctz(ntohl(netmask));
68 for (wcbits = 32; netmask; wcbits--) {
69 netmask &= netmask - 1;
76 /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
78 #define WC_INVARIANT_LIST \
79 WC_INVARIANT_BIT(IN_PORT) \
80 WC_INVARIANT_BIT(DL_SRC) \
81 WC_INVARIANT_BIT(DL_DST) \
82 WC_INVARIANT_BIT(DL_TYPE) \
83 WC_INVARIANT_BIT(NW_PROTO) \
84 WC_INVARIANT_BIT(TP_SRC) \
85 WC_INVARIANT_BIT(TP_DST)
87 /* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
88 * actually have the same names and values. */
89 #define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME);
91 #undef WC_INVARIANT_BIT
93 /* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
97 #define WC_INVARIANT_BIT(NAME) | FWW_##NAME
99 #undef WC_INVARIANT_BIT
102 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
105 * 'flow_format' must either NXFF_OPENFLOW10 or NXFF_TUN_ID_FROM_COOKIE. In
106 * the latter case only, 'flow''s tun_id field will be taken from the high bits
107 * of 'cookie', if 'match''s wildcards do not indicate that tun_id is
110 ofputil_cls_rule_from_match(const struct ofp_match *match,
111 unsigned int priority,
112 enum nx_flow_format flow_format,
113 ovs_be64 cookie, struct cls_rule *rule)
115 struct flow_wildcards *wc = &rule->wc;
119 /* Initialize rule->priority. */
120 ofpfw = ntohl(match->wildcards);
121 ofpfw &= flow_format == NXFF_TUN_ID_FROM_COOKIE ? OVSFW_ALL : OFPFW_ALL;
122 rule->priority = !ofpfw ? UINT16_MAX : priority;
124 /* Initialize most of rule->wc. */
125 flow_wildcards_init_catchall(wc);
126 wc->wildcards = ofpfw & WC_INVARIANTS;
128 /* Wildcard fields that aren't defined by ofp_match or tun_id. */
129 wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_ND_TARGET);
131 if (ofpfw & OFPFW_NW_TOS) {
132 wc->wildcards |= FWW_NW_TOS;
134 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
135 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
137 if (flow_format == NXFF_TUN_ID_FROM_COOKIE && !(ofpfw & NXFW_TUN_ID)) {
138 rule->flow.tun_id = htonll(ntohll(cookie) >> 32);
141 if (ofpfw & OFPFW_DL_DST) {
142 /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
143 * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST
144 * and FWW_ETH_MCAST. */
145 wc->wildcards |= FWW_ETH_MCAST;
148 /* Initialize most of rule->flow. */
149 rule->flow.nw_src = match->nw_src;
150 rule->flow.nw_dst = match->nw_dst;
151 rule->flow.in_port = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
152 : ntohs(match->in_port));
153 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
154 rule->flow.tp_src = match->tp_src;
155 rule->flow.tp_dst = match->tp_dst;
156 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
157 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
158 rule->flow.nw_tos = match->nw_tos;
159 rule->flow.nw_proto = match->nw_proto;
161 /* Translate VLANs. */
162 vid = match->dl_vlan & htons(VLAN_VID_MASK);
163 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
164 switch (ofpfw & (OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP)) {
165 case OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP:
166 /* Wildcard everything. */
167 rule->flow.vlan_tci = htons(0);
168 rule->wc.vlan_tci_mask = htons(0);
171 case OFPFW_DL_VLAN_PCP:
172 if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
173 /* Match only packets without 802.1Q header. */
174 rule->flow.vlan_tci = htons(0);
175 rule->wc.vlan_tci_mask = htons(0xffff);
177 /* Wildcard PCP, specific VID. */
178 rule->flow.vlan_tci = vid | htons(VLAN_CFI);
179 rule->wc.vlan_tci_mask = htons(VLAN_VID_MASK | VLAN_CFI);
184 /* Wildcard VID, specific PCP. */
185 rule->flow.vlan_tci = pcp | htons(VLAN_CFI);
186 rule->wc.vlan_tci_mask = htons(VLAN_PCP_MASK | VLAN_CFI);
190 if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
191 /* This case is odd, since we can't have a specific PCP without an
192 * 802.1Q header. However, older versions of OVS treated this as
193 * matching packets withut an 802.1Q header, so we do here too. */
194 rule->flow.vlan_tci = htons(0);
195 rule->wc.vlan_tci_mask = htons(0xffff);
197 /* Specific VID and PCP. */
198 rule->flow.vlan_tci = vid | pcp | htons(VLAN_CFI);
199 rule->wc.vlan_tci_mask = htons(0xffff);
205 cls_rule_zero_wildcarded_fields(rule);
208 /* Convert 'rule' into the OpenFlow match structure 'match'. 'flow_format'
209 * must either NXFF_OPENFLOW10 or NXFF_TUN_ID_FROM_COOKIE.
211 * The NXFF_TUN_ID_FROM_COOKIE flow format requires modifying the flow cookie.
212 * This function can help with that, if 'cookie_out' is nonnull. For
213 * NXFF_OPENFLOW10, or if the tunnel ID is wildcarded, 'cookie_in' will be
214 * copied directly to '*cookie_out'. For NXFF_TUN_ID_FROM_COOKIE when tunnel
215 * ID is matched, 'cookie_in' will be modified appropriately before setting
219 ofputil_cls_rule_to_match(const struct cls_rule *rule,
220 enum nx_flow_format flow_format,
221 struct ofp_match *match,
222 ovs_be64 cookie_in, ovs_be64 *cookie_out)
224 const struct flow_wildcards *wc = &rule->wc;
227 /* Figure out most OpenFlow wildcards. */
228 ofpfw = wc->wildcards & WC_INVARIANTS;
229 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
230 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
231 if (wc->wildcards & FWW_NW_TOS) {
232 ofpfw |= OFPFW_NW_TOS;
236 if (flow_format == NXFF_TUN_ID_FROM_COOKIE) {
237 if (wc->tun_id_mask == htonll(0)) {
238 ofpfw |= NXFW_TUN_ID;
240 uint32_t cookie_lo = ntohll(cookie_in);
241 uint32_t cookie_hi = ntohll(rule->flow.tun_id);
242 cookie_in = htonll(cookie_lo | ((uint64_t) cookie_hi << 32));
246 *cookie_out = cookie_in;
249 /* Translate VLANs. */
250 match->dl_vlan = htons(0);
251 match->dl_vlan_pcp = 0;
252 if (rule->wc.vlan_tci_mask == htons(0)) {
253 ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
254 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
255 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
256 match->dl_vlan = htons(OFP_VLAN_NONE);
258 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
259 ofpfw |= OFPFW_DL_VLAN;
261 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
264 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
265 ofpfw |= OFPFW_DL_VLAN_PCP;
267 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
271 /* Compose most of the match structure. */
272 match->wildcards = htonl(ofpfw);
273 match->in_port = htons(rule->flow.in_port == ODPP_LOCAL ? OFPP_LOCAL
274 : rule->flow.in_port);
275 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
276 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
277 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
278 match->nw_src = rule->flow.nw_src;
279 match->nw_dst = rule->flow.nw_dst;
280 match->nw_tos = rule->flow.nw_tos;
281 match->nw_proto = rule->flow.nw_proto;
282 match->tp_src = rule->flow.tp_src;
283 match->tp_dst = rule->flow.tp_dst;
284 memset(match->pad1, '\0', sizeof match->pad1);
285 memset(match->pad2, '\0', sizeof match->pad2);
288 /* Given a 'dl_type' value in the format used in struct flow, returns the
289 * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */
291 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
293 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
294 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
298 /* Given a 'dl_type' value in the format used in an OpenFlow ofp_match
299 * structure, returns the corresponding 'dl_type' value for use in struct
302 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
304 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
305 ? htons(FLOW_DL_TYPE_NONE)
309 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
313 static uint32_t next_xid = 1;
314 return htonl(next_xid++);
317 /* Basic parsing of OpenFlow messages. */
319 struct ofputil_msg_type {
320 enum ofputil_msg_code code; /* OFPUTIL_*. */
321 uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
322 const char *name; /* e.g. "OFPT_FLOW_REMOVED". */
323 unsigned int min_size; /* Minimum total message size in bytes. */
324 /* 0 if 'min_size' is the exact size that the message must be. Otherwise,
325 * the message may exceed 'min_size' by an even multiple of this value. */
326 unsigned int extra_multiple;
329 struct ofputil_msg_category {
330 const char *name; /* e.g. "OpenFlow message" */
331 const struct ofputil_msg_type *types;
333 int missing_error; /* ofp_mkerr() value for missing type. */
337 ofputil_length_ok(const struct ofputil_msg_category *cat,
338 const struct ofputil_msg_type *type,
341 switch (type->extra_multiple) {
343 if (size != type->min_size) {
344 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
345 "length %u (expected length %u)",
346 cat->name, type->name, size, type->min_size);
352 if (size < type->min_size) {
353 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
354 "length %u (expected length at least %u bytes)",
355 cat->name, type->name, size, type->min_size);
361 if (size < type->min_size
362 || (size - type->min_size) % type->extra_multiple) {
363 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
364 "length %u (must be exactly %u bytes or longer "
365 "by an integer multiple of %u bytes)",
366 cat->name, type->name, size,
367 type->min_size, type->extra_multiple);
375 ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
376 uint32_t value, unsigned int size,
377 const struct ofputil_msg_type **typep)
379 const struct ofputil_msg_type *type;
381 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
382 if (type->value == value) {
383 if (!ofputil_length_ok(cat, type, size)) {
384 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
391 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
393 return cat->missing_error;
397 ofputil_decode_vendor(const struct ofp_header *oh,
398 const struct ofputil_msg_type **typep)
400 static const struct ofputil_msg_type nxt_messages[] = {
401 { OFPUTIL_NXT_STATUS_REQUEST,
402 NXT_STATUS_REQUEST, "NXT_STATUS_REQUEST",
403 sizeof(struct nicira_header), 1 },
405 { OFPUTIL_NXT_STATUS_REPLY,
406 NXT_STATUS_REPLY, "NXT_STATUS_REPLY",
407 sizeof(struct nicira_header), 1 },
409 { OFPUTIL_NXT_TUN_ID_FROM_COOKIE,
410 NXT_TUN_ID_FROM_COOKIE, "NXT_TUN_ID_FROM_COOKIE",
411 sizeof(struct nxt_tun_id_cookie), 0 },
413 { OFPUTIL_NXT_ROLE_REQUEST,
414 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
415 sizeof(struct nx_role_request), 0 },
417 { OFPUTIL_NXT_ROLE_REPLY,
418 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
419 sizeof(struct nx_role_request), 0 },
421 { OFPUTIL_NXT_SET_FLOW_FORMAT,
422 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
423 sizeof(struct nxt_set_flow_format), 0 },
425 { OFPUTIL_NXT_FLOW_MOD,
426 NXT_FLOW_MOD, "NXT_FLOW_MOD",
427 sizeof(struct nx_flow_mod), 8 },
429 { OFPUTIL_NXT_FLOW_REMOVED,
430 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
431 sizeof(struct nx_flow_removed), 8 },
434 static const struct ofputil_msg_category nxt_category = {
435 "Nicira extension message",
436 nxt_messages, ARRAY_SIZE(nxt_messages),
437 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
440 const struct ofp_vendor_header *ovh;
441 const struct nicira_header *nh;
443 ovh = (const struct ofp_vendor_header *) oh;
444 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
445 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
446 "vendor %"PRIx32, ntohl(ovh->vendor));
447 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
450 if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
451 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
452 "length %u (expected at least %zu)",
453 ntohs(ovh->header.length), sizeof(struct nicira_header));
454 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
457 nh = (const struct nicira_header *) oh;
458 return ofputil_lookup_openflow_message(&nxt_category, ntohl(nh->subtype),
459 ntohs(oh->length), typep);
463 check_nxstats_msg(const struct ofp_header *oh)
465 const struct ofp_stats_request *osr;
468 osr = (const struct ofp_stats_request *) oh;
470 memcpy(&vendor, osr->body, sizeof vendor);
471 if (vendor != htonl(NX_VENDOR_ID)) {
472 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
473 "unknown vendor %"PRIx32, ntohl(vendor));
474 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
477 if (ntohs(osr->header.length) < sizeof(struct nicira_stats_msg)) {
478 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
479 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
486 ofputil_decode_nxst_request(const struct ofp_header *oh,
487 const struct ofputil_msg_type **typep)
489 static const struct ofputil_msg_type nxst_requests[] = {
490 { OFPUTIL_NXST_FLOW_REQUEST,
491 NXST_FLOW, "NXST_FLOW request",
492 sizeof(struct nx_flow_stats_request), 8 },
494 { OFPUTIL_NXST_AGGREGATE_REQUEST,
495 NXST_AGGREGATE, "NXST_AGGREGATE request",
496 sizeof(struct nx_aggregate_stats_request), 8 },
499 static const struct ofputil_msg_category nxst_request_category = {
500 "Nicira extension statistics request",
501 nxst_requests, ARRAY_SIZE(nxst_requests),
502 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
505 const struct nicira_stats_msg *nsm;
508 error = check_nxstats_msg(oh);
513 nsm = (struct nicira_stats_msg *) oh;
514 return ofputil_lookup_openflow_message(&nxst_request_category,
516 ntohs(oh->length), typep);
520 ofputil_decode_nxst_reply(const struct ofp_header *oh,
521 const struct ofputil_msg_type **typep)
523 static const struct ofputil_msg_type nxst_replies[] = {
524 { OFPUTIL_NXST_FLOW_REPLY,
525 NXST_FLOW, "NXST_FLOW reply",
526 sizeof(struct nicira_stats_msg), 8 },
528 { OFPUTIL_NXST_AGGREGATE_REPLY,
529 NXST_AGGREGATE, "NXST_AGGREGATE reply",
530 sizeof(struct nx_aggregate_stats_reply), 0 },
533 static const struct ofputil_msg_category nxst_reply_category = {
534 "Nicira extension statistics reply",
535 nxst_replies, ARRAY_SIZE(nxst_replies),
536 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
539 const struct nicira_stats_msg *nsm;
542 error = check_nxstats_msg(oh);
547 nsm = (struct nicira_stats_msg *) oh;
548 return ofputil_lookup_openflow_message(&nxst_reply_category,
550 ntohs(oh->length), typep);
554 ofputil_decode_ofpst_request(const struct ofp_header *oh,
555 const struct ofputil_msg_type **typep)
557 enum { OSR_SIZE = sizeof(struct ofp_stats_request) };
558 static const struct ofputil_msg_type ofpst_requests[] = {
559 { OFPUTIL_OFPST_DESC_REQUEST,
560 OFPST_DESC, "OFPST_DESC request",
563 { OFPUTIL_OFPST_FLOW_REQUEST,
564 OFPST_FLOW, "OFPST_FLOW request",
565 OSR_SIZE + sizeof(struct ofp_flow_stats_request), 0 },
567 { OFPUTIL_OFPST_AGGREGATE_REQUEST,
568 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
569 OSR_SIZE + sizeof(struct ofp_aggregate_stats_request), 0 },
571 { OFPUTIL_OFPST_TABLE_REQUEST,
572 OFPST_TABLE, "OFPST_TABLE request",
575 { OFPUTIL_OFPST_PORT_REQUEST,
576 OFPST_PORT, "OFPST_PORT request",
577 OSR_SIZE + sizeof(struct ofp_port_stats_request), 0 },
579 { OFPUTIL_OFPST_QUEUE_REQUEST,
580 OFPST_QUEUE, "OFPST_QUEUE request",
581 OSR_SIZE + sizeof(struct ofp_queue_stats_request), 0 },
584 OFPST_VENDOR, "OFPST_VENDOR request",
585 OSR_SIZE + sizeof(uint32_t), 1 },
588 static const struct ofputil_msg_category ofpst_request_category = {
589 "OpenFlow statistics",
590 ofpst_requests, ARRAY_SIZE(ofpst_requests),
591 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
594 const struct ofp_stats_request *osr;
597 osr = (const struct ofp_stats_request *) oh;
598 error = ofputil_lookup_openflow_message(&ofpst_request_category,
600 ntohs(oh->length), typep);
601 if (!error && osr->type == htons(OFPST_VENDOR)) {
602 error = ofputil_decode_nxst_request(oh, typep);
608 ofputil_decode_ofpst_reply(const struct ofp_header *oh,
609 const struct ofputil_msg_type **typep)
611 enum { OSR_SIZE = sizeof(struct ofp_stats_reply) };
612 static const struct ofputil_msg_type ofpst_replies[] = {
613 { OFPUTIL_OFPST_DESC_REPLY,
614 OFPST_DESC, "OFPST_DESC reply",
615 OSR_SIZE + sizeof(struct ofp_desc_stats), 0 },
617 { OFPUTIL_OFPST_FLOW_REPLY,
618 OFPST_FLOW, "OFPST_FLOW reply",
621 { OFPUTIL_OFPST_AGGREGATE_REPLY,
622 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
623 OSR_SIZE + sizeof(struct ofp_aggregate_stats_reply), 0 },
625 { OFPUTIL_OFPST_TABLE_REPLY,
626 OFPST_TABLE, "OFPST_TABLE reply",
627 OSR_SIZE, sizeof(struct ofp_table_stats) },
629 { OFPUTIL_OFPST_PORT_REPLY,
630 OFPST_PORT, "OFPST_PORT reply",
631 OSR_SIZE, sizeof(struct ofp_port_stats) },
633 { OFPUTIL_OFPST_QUEUE_REPLY,
634 OFPST_QUEUE, "OFPST_QUEUE reply",
635 OSR_SIZE, sizeof(struct ofp_queue_stats) },
638 OFPST_VENDOR, "OFPST_VENDOR reply",
639 OSR_SIZE + sizeof(uint32_t), 1 },
642 static const struct ofputil_msg_category ofpst_reply_category = {
643 "OpenFlow statistics",
644 ofpst_replies, ARRAY_SIZE(ofpst_replies),
645 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
648 const struct ofp_stats_reply *osr = (const struct ofp_stats_reply *) oh;
651 error = ofputil_lookup_openflow_message(&ofpst_reply_category,
653 ntohs(oh->length), typep);
654 if (!error && osr->type == htons(OFPST_VENDOR)) {
655 error = ofputil_decode_nxst_reply(oh, typep);
660 /* Decodes the message type represented by 'oh'. Returns 0 if successful or
661 * an OpenFlow error code constructed with ofp_mkerr() on failure. Either
662 * way, stores in '*typep' a type structure that can be inspected with the
663 * ofputil_msg_type_*() functions.
665 * oh->length must indicate the correct length of the message (and must be at
666 * least sizeof(struct ofp_header)).
668 * Success indicates that 'oh' is at least as long as the minimum-length
669 * message of its type. */
671 ofputil_decode_msg_type(const struct ofp_header *oh,
672 const struct ofputil_msg_type **typep)
674 static const struct ofputil_msg_type ofpt_messages[] = {
675 { OFPUTIL_OFPT_HELLO,
676 OFPT_HELLO, "OFPT_HELLO",
677 sizeof(struct ofp_hello), 1 },
679 { OFPUTIL_OFPT_ERROR,
680 OFPT_ERROR, "OFPT_ERROR",
681 sizeof(struct ofp_error_msg), 1 },
683 { OFPUTIL_OFPT_ECHO_REQUEST,
684 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
685 sizeof(struct ofp_header), 1 },
687 { OFPUTIL_OFPT_ECHO_REPLY,
688 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
689 sizeof(struct ofp_header), 1 },
691 { OFPUTIL_OFPT_FEATURES_REQUEST,
692 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
693 sizeof(struct ofp_header), 0 },
695 { OFPUTIL_OFPT_FEATURES_REPLY,
696 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
697 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
699 { OFPUTIL_OFPT_GET_CONFIG_REQUEST,
700 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
701 sizeof(struct ofp_header), 0 },
703 { OFPUTIL_OFPT_GET_CONFIG_REPLY,
704 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
705 sizeof(struct ofp_switch_config), 0 },
707 { OFPUTIL_OFPT_SET_CONFIG,
708 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
709 sizeof(struct ofp_switch_config), 0 },
711 { OFPUTIL_OFPT_PACKET_IN,
712 OFPT_PACKET_IN, "OFPT_PACKET_IN",
713 offsetof(struct ofp_packet_in, data), 1 },
715 { OFPUTIL_OFPT_FLOW_REMOVED,
716 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
717 sizeof(struct ofp_flow_removed), 0 },
719 { OFPUTIL_OFPT_PORT_STATUS,
720 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
721 sizeof(struct ofp_port_status), 0 },
723 { OFPUTIL_OFPT_PACKET_OUT,
724 OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
725 sizeof(struct ofp_packet_out), 1 },
727 { OFPUTIL_OFPT_FLOW_MOD,
728 OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
729 sizeof(struct ofp_flow_mod), 1 },
731 { OFPUTIL_OFPT_PORT_MOD,
732 OFPT_PORT_MOD, "OFPT_PORT_MOD",
733 sizeof(struct ofp_port_mod), 0 },
736 OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
737 sizeof(struct ofp_stats_request), 1 },
740 OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
741 sizeof(struct ofp_stats_reply), 1 },
743 { OFPUTIL_OFPT_BARRIER_REQUEST,
744 OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
745 sizeof(struct ofp_header), 0 },
747 { OFPUTIL_OFPT_BARRIER_REPLY,
748 OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
749 sizeof(struct ofp_header), 0 },
752 OFPT_VENDOR, "OFPT_VENDOR",
753 sizeof(struct ofp_vendor_header), 1 },
756 static const struct ofputil_msg_category ofpt_category = {
758 ofpt_messages, ARRAY_SIZE(ofpt_messages),
759 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE)
764 error = ofputil_lookup_openflow_message(&ofpt_category, oh->type,
765 ntohs(oh->length), typep);
769 error = ofputil_decode_vendor(oh, typep);
772 case OFPT_STATS_REQUEST:
773 error = ofputil_decode_ofpst_request(oh, typep);
776 case OFPT_STATS_REPLY:
777 error = ofputil_decode_ofpst_reply(oh, typep);
784 static const struct ofputil_msg_type ofputil_invalid_type = {
786 0, "OFPUTIL_INVALID",
790 *typep = &ofputil_invalid_type;
795 /* Returns an OFPUTIL_* message type code for 'type'. */
796 enum ofputil_msg_code
797 ofputil_msg_type_code(const struct ofputil_msg_type *type)
805 ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
807 switch (flow_format) {
808 case NXFF_OPENFLOW10:
809 case NXFF_TUN_ID_FROM_COOKIE:
818 ofputil_flow_format_to_string(enum nx_flow_format flow_format)
820 switch (flow_format) {
821 case NXFF_OPENFLOW10:
823 case NXFF_TUN_ID_FROM_COOKIE:
824 return "tun_id_from_cookie";
833 ofputil_flow_format_from_string(const char *s)
835 return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
836 : !strcmp(s, "tun_id_from_cookie") ? NXFF_TUN_ID_FROM_COOKIE
837 : !strcmp(s, "nxm") ? NXFF_NXM
842 regs_fully_wildcarded(const struct flow_wildcards *wc)
846 for (i = 0; i < FLOW_N_REGS; i++) {
847 if (wc->reg_masks[i] != 0) {
855 is_nxm_required(const struct cls_rule *rule, bool cookie_support,
858 const struct flow_wildcards *wc = &rule->wc;
861 /* Only NXM supports separately wildcards the Ethernet multicast bit. */
862 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
866 /* Only NXM supports matching ARP hardware addresses. */
867 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
871 /* Only NXM supports matching IPv6 traffic. */
872 if (!(wc->wildcards & FWW_DL_TYPE)
873 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
877 /* Only NXM supports matching registers. */
878 if (!regs_fully_wildcarded(wc)) {
882 switch (wc->tun_id_mask) {
883 case CONSTANT_HTONLL(0):
884 /* Other formats can fully wildcard tun_id. */
887 case CONSTANT_HTONLL(UINT64_MAX):
888 /* Only NXM supports matching tunnel ID, unless there is a cookie and
889 * the top 32 bits of the cookie are the desired tunnel ID value. */
890 cookie_hi = htonl(ntohll(cookie) >> 32);
892 || (cookie_hi && cookie_hi != ntohll(rule->flow.tun_id))) {
898 /* Only NXM supports partial matches on tunnel ID. */
902 /* Other formats can express this rule. */
906 /* Returns the minimum nx_flow_format to use for sending 'rule' to a switch
907 * (e.g. to add or remove a flow). 'cookie_support' should be true if the
908 * command to be sent includes a flow cookie (as OFPT_FLOW_MOD does, for
909 * example) or false if the command does not (OFPST_FLOW and OFPST_AGGREGATE do
910 * not, for example). If 'cookie_support' is true, then 'cookie' should be the
911 * cookie to be sent; otherwise its value is ignored.
913 * The "best" flow format is chosen on this basis:
915 * - It must be capable of expressing the rule. NXFF_OPENFLOW10 flows can't
916 * handle tunnel IDs. NXFF_TUN_ID_FROM_COOKIE flows can't handle registers
917 * or fixing the Ethernet multicast bit, and can't handle tunnel IDs that
918 * conflict with the high 32 bits of the cookie or commands that don't
921 * - Otherwise, the chosen format should be as backward compatible as
922 * possible. (NXFF_OPENFLOW10 is more backward compatible than
923 * NXFF_TUN_ID_FROM_COOKIE, which is more backward compatible than
927 ofputil_min_flow_format(const struct cls_rule *rule, bool cookie_support,
930 if (is_nxm_required(rule, cookie_support, cookie)) {
932 } else if (rule->wc.tun_id_mask != htonll(0)) {
933 return NXFF_TUN_ID_FROM_COOKIE;
935 return NXFF_OPENFLOW10;
939 /* Returns an OpenFlow message that can be used to set the flow format to
942 ofputil_make_set_flow_format(enum nx_flow_format flow_format)
946 if (flow_format == NXFF_OPENFLOW10
947 || flow_format == NXFF_TUN_ID_FROM_COOKIE) {
948 struct nxt_tun_id_cookie *tic;
950 tic = make_nxmsg(sizeof *tic, NXT_TUN_ID_FROM_COOKIE, &msg);
951 tic->set = flow_format == NXFF_TUN_ID_FROM_COOKIE;
953 struct nxt_set_flow_format *sff;
955 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
956 sff->format = htonl(flow_format);
962 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
963 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
966 * For OFPT_FLOW_MOD messages, 'flow_format' should be the current flow format
967 * at the time when the message was received. Otherwise 'flow_format' is
970 * Does not validate the flow_mod actions. */
972 ofputil_decode_flow_mod(struct flow_mod *fm, const struct ofp_header *oh,
973 enum nx_flow_format flow_format)
975 const struct ofputil_msg_type *type;
978 ofpbuf_use_const(&b, oh, ntohs(oh->length));
980 ofputil_decode_msg_type(oh, &type);
981 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
982 /* Standard OpenFlow flow_mod. */
983 struct ofp_match match, orig_match;
984 const struct ofp_flow_mod *ofm;
987 /* Dissect the message. */
988 ofm = ofpbuf_pull(&b, sizeof *ofm);
989 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
994 /* Normalize ofm->match. If normalization actually changes anything,
995 * then log the differences. */
997 match.pad1[0] = match.pad2[0] = 0;
999 normalize_match(&match);
1000 if (memcmp(&match, &orig_match, sizeof orig_match)) {
1001 if (!VLOG_DROP_INFO(&bad_ofmsg_rl)) {
1002 char *old = ofp_match_to_literal_string(&orig_match);
1003 char *new = ofp_match_to_literal_string(&match);
1004 VLOG_INFO("normalization changed ofp_match, details:");
1005 VLOG_INFO(" pre: %s", old);
1006 VLOG_INFO("post: %s", new);
1012 /* Translate the message. */
1013 ofputil_cls_rule_from_match(&match, ntohs(ofm->priority), flow_format,
1014 ofm->cookie, &fm->cr);
1015 fm->cookie = ofm->cookie;
1016 fm->command = ntohs(ofm->command);
1017 fm->idle_timeout = ntohs(ofm->idle_timeout);
1018 fm->hard_timeout = ntohs(ofm->hard_timeout);
1019 fm->buffer_id = ntohl(ofm->buffer_id);
1020 fm->out_port = ntohs(ofm->out_port);
1021 fm->flags = ntohs(ofm->flags);
1022 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
1023 /* Nicira extended flow_mod. */
1024 const struct nx_flow_mod *nfm;
1027 /* Dissect the message. */
1028 nfm = ofpbuf_pull(&b, sizeof *nfm);
1029 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
1034 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
1039 /* Translate the message. */
1040 fm->cookie = nfm->cookie;
1041 fm->command = ntohs(nfm->command);
1042 fm->idle_timeout = ntohs(nfm->idle_timeout);
1043 fm->hard_timeout = ntohs(nfm->hard_timeout);
1044 fm->buffer_id = ntohl(nfm->buffer_id);
1045 fm->out_port = ntohs(nfm->out_port);
1046 fm->flags = ntohs(nfm->flags);
1054 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1055 * 'flow_format' and returns the message. */
1057 ofputil_encode_flow_mod(const struct flow_mod *fm,
1058 enum nx_flow_format flow_format)
1060 size_t actions_len = fm->n_actions * sizeof *fm->actions;
1063 if (flow_format == NXFF_OPENFLOW10
1064 || flow_format == NXFF_TUN_ID_FROM_COOKIE) {
1065 struct ofp_flow_mod *ofm;
1067 msg = ofpbuf_new(sizeof *ofm + actions_len);
1068 ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
1069 ofputil_cls_rule_to_match(&fm->cr, flow_format, &ofm->match,
1070 fm->cookie, &ofm->cookie);
1071 ofm->command = htons(fm->command);
1072 ofm->idle_timeout = htons(fm->idle_timeout);
1073 ofm->hard_timeout = htons(fm->hard_timeout);
1074 ofm->priority = htons(fm->cr.priority);
1075 ofm->buffer_id = htonl(fm->buffer_id);
1076 ofm->out_port = htons(fm->out_port);
1077 ofm->flags = htons(fm->flags);
1078 } else if (flow_format == NXFF_NXM) {
1079 struct nx_flow_mod *nfm;
1082 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
1083 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
1084 match_len = nx_put_match(msg, &fm->cr);
1087 nfm->cookie = fm->cookie;
1088 nfm->command = htons(fm->command);
1089 nfm->idle_timeout = htons(fm->idle_timeout);
1090 nfm->hard_timeout = htons(fm->hard_timeout);
1091 nfm->priority = htons(fm->cr.priority);
1092 nfm->buffer_id = htonl(fm->buffer_id);
1093 nfm->out_port = htons(fm->out_port);
1094 nfm->flags = htons(fm->flags);
1095 nfm->match_len = htons(match_len);
1100 ofpbuf_put(msg, fm->actions, actions_len);
1101 update_openflow_length(msg);
1106 ofputil_decode_ofpst_flow_request(struct flow_stats_request *fsr,
1107 const struct ofp_header *oh,
1108 enum nx_flow_format flow_format,
1111 const struct ofp_flow_stats_request *ofsr = ofputil_stats_body(oh);
1113 fsr->aggregate = aggregate;
1114 ofputil_cls_rule_from_match(&ofsr->match, 0, flow_format, 0, &fsr->match);
1115 fsr->out_port = ntohs(ofsr->out_port);
1116 fsr->table_id = ofsr->table_id;
1122 ofputil_decode_nxst_flow_request(struct flow_stats_request *fsr,
1123 const struct ofp_header *oh,
1126 const struct nx_flow_stats_request *nfsr;
1130 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1132 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1133 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match);
1138 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1141 fsr->aggregate = aggregate;
1142 fsr->out_port = ntohs(nfsr->out_port);
1143 fsr->table_id = nfsr->table_id;
1148 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1149 * message 'oh', received when the current flow format was 'flow_format', into
1150 * an abstract flow_stats_request in 'fsr'. Returns 0 if successful, otherwise
1151 * an OpenFlow error code.
1153 * For OFPST_FLOW and OFPST_AGGREGATE messages, 'flow_format' should be the
1154 * current flow format at the time when the message was received. Otherwise
1155 * 'flow_format' is ignored. */
1157 ofputil_decode_flow_stats_request(struct flow_stats_request *fsr,
1158 const struct ofp_header *oh,
1159 enum nx_flow_format flow_format)
1161 const struct ofputil_msg_type *type;
1165 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1167 ofputil_decode_msg_type(oh, &type);
1168 code = ofputil_msg_type_code(type);
1170 case OFPUTIL_OFPST_FLOW_REQUEST:
1171 return ofputil_decode_ofpst_flow_request(fsr, oh, flow_format, false);
1173 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1174 return ofputil_decode_ofpst_flow_request(fsr, oh, flow_format, true);
1176 case OFPUTIL_NXST_FLOW_REQUEST:
1177 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1179 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1180 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1183 /* Hey, the caller lied. */
1188 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1189 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE message 'oh' according to
1190 * 'flow_format', and returns the message. */
1192 ofputil_encode_flow_stats_request(const struct flow_stats_request *fsr,
1193 enum nx_flow_format flow_format)
1197 if (flow_format == NXFF_OPENFLOW10
1198 || flow_format == NXFF_TUN_ID_FROM_COOKIE) {
1199 struct ofp_flow_stats_request *ofsr;
1202 BUILD_ASSERT_DECL(sizeof(struct ofp_flow_stats_request)
1203 == sizeof(struct ofp_aggregate_stats_request));
1205 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1206 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, &msg);
1207 ofputil_cls_rule_to_match(&fsr->match, flow_format, &ofsr->match,
1209 ofsr->table_id = fsr->table_id;
1210 ofsr->out_port = htons(fsr->out_port);
1211 } else if (flow_format == NXFF_NXM) {
1212 struct nx_flow_stats_request *nfsr;
1216 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1217 ofputil_make_nxstats_request(sizeof *nfsr, subtype, &msg);
1218 match_len = nx_put_match(msg, &fsr->match);
1221 nfsr->out_port = htons(fsr->out_port);
1222 nfsr->match_len = htons(match_len);
1223 nfsr->table_id = fsr->table_id;
1231 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh', received
1232 * when the current flow format was 'flow_format', into an abstract
1233 * ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise an
1234 * OpenFlow error code.
1236 * For OFPT_FLOW_REMOVED messages, 'flow_format' should be the current flow
1237 * format at the time when the message was received. Otherwise 'flow_format'
1240 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1241 const struct ofp_header *oh,
1242 enum nx_flow_format flow_format)
1244 const struct ofputil_msg_type *type;
1245 enum ofputil_msg_code code;
1247 ofputil_decode_msg_type(oh, &type);
1248 code = ofputil_msg_type_code(type);
1249 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1250 const struct ofp_flow_removed *ofr;
1252 ofr = (const struct ofp_flow_removed *) oh;
1253 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
1254 flow_format, ofr->cookie, &fr->rule);
1255 fr->cookie = ofr->cookie;
1256 fr->reason = ofr->reason;
1257 fr->duration_sec = ntohl(ofr->duration_sec);
1258 fr->duration_nsec = ntohl(ofr->duration_nsec);
1259 fr->idle_timeout = ntohs(ofr->idle_timeout);
1260 fr->packet_count = ntohll(ofr->packet_count);
1261 fr->byte_count = ntohll(ofr->byte_count);
1262 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1263 struct nx_flow_removed *nfr;
1267 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1269 nfr = ofpbuf_pull(&b, sizeof *nfr);
1270 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1276 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1279 fr->cookie = nfr->cookie;
1280 fr->reason = nfr->reason;
1281 fr->duration_sec = ntohl(nfr->duration_sec);
1282 fr->duration_nsec = ntohl(nfr->duration_nsec);
1283 fr->idle_timeout = ntohs(nfr->idle_timeout);
1284 fr->packet_count = ntohll(nfr->packet_count);
1285 fr->byte_count = ntohll(nfr->byte_count);
1293 /* Returns a string representing the message type of 'type'. The string is the
1294 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
1295 * messages, the constant is followed by "request" or "reply",
1296 * e.g. "OFPST_AGGREGATE reply". */
1298 ofputil_msg_type_name(const struct ofputil_msg_type *type)
1303 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1304 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1305 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
1308 * The caller is responsible for freeing '*bufferp' when it is no longer
1311 * The OpenFlow header length is initially set to 'openflow_len'; if the
1312 * message is later extended, the length should be updated with
1313 * update_openflow_length() before sending.
1315 * Returns the header. */
1317 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1319 *bufferp = ofpbuf_new(openflow_len);
1320 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1323 /* Similar to make_openflow() but creates a Nicira vendor extension message
1324 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1326 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1328 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
1331 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1332 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1333 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
1336 * The caller is responsible for freeing '*bufferp' when it is no longer
1339 * The OpenFlow header length is initially set to 'openflow_len'; if the
1340 * message is later extended, the length should be updated with
1341 * update_openflow_length() before sending.
1343 * Returns the header. */
1345 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1346 struct ofpbuf **bufferp)
1348 *bufferp = ofpbuf_new(openflow_len);
1349 return put_openflow_xid(openflow_len, type, xid, *bufferp);
1352 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
1353 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1355 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1356 struct ofpbuf **bufferp)
1358 *bufferp = ofpbuf_new(openflow_len);
1359 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
1362 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1363 * with the given 'type' and an arbitrary transaction id. Allocated bytes
1364 * beyond the header, if any, are zeroed.
1366 * The OpenFlow header length is initially set to 'openflow_len'; if the
1367 * message is later extended, the length should be updated with
1368 * update_openflow_length() before sending.
1370 * Returns the header. */
1372 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1374 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
1377 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1378 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
1379 * the header, if any, are zeroed.
1381 * The OpenFlow header length is initially set to 'openflow_len'; if the
1382 * message is later extended, the length should be updated with
1383 * update_openflow_length() before sending.
1385 * Returns the header. */
1387 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1388 struct ofpbuf *buffer)
1390 struct ofp_header *oh;
1392 assert(openflow_len >= sizeof *oh);
1393 assert(openflow_len <= UINT16_MAX);
1395 oh = ofpbuf_put_uninit(buffer, openflow_len);
1396 oh->version = OFP_VERSION;
1398 oh->length = htons(openflow_len);
1400 memset(oh + 1, 0, openflow_len - sizeof *oh);
1404 /* Similar to put_openflow() but append a Nicira vendor extension message with
1405 * the specific 'subtype'. 'subtype' should be in host byte order. */
1407 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
1409 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
1412 /* Similar to put_openflow_xid() but append a Nicira vendor extension message
1413 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1415 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1416 struct ofpbuf *buffer)
1418 struct nicira_header *nxh;
1420 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
1421 nxh->vendor = htonl(NX_VENDOR_ID);
1422 nxh->subtype = htonl(subtype);
1426 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
1427 * 'buffer->size'. */
1429 update_openflow_length(struct ofpbuf *buffer)
1431 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
1432 oh->length = htons(buffer->size);
1435 /* Creates an ofp_stats_request with the given 'type' and 'body_len' bytes of
1436 * space allocated for the 'body' member. Returns the first byte of the 'body'
1439 ofputil_make_stats_request(size_t body_len, uint16_t type,
1440 struct ofpbuf **bufferp)
1442 struct ofp_stats_request *osr;
1443 osr = make_openflow((offsetof(struct ofp_stats_request, body)
1444 + body_len), OFPT_STATS_REQUEST, bufferp);
1445 osr->type = htons(type);
1446 osr->flags = htons(0);
1450 /* Creates a stats request message with Nicira as vendor and the given
1451 * 'subtype', of total length 'openflow_len'. Returns the message. */
1453 ofputil_make_nxstats_request(size_t openflow_len, uint32_t subtype,
1454 struct ofpbuf **bufferp)
1456 struct nicira_stats_msg *nsm;
1458 nsm = make_openflow(openflow_len, OFPT_STATS_REQUEST, bufferp);
1459 nsm->type = htons(OFPST_VENDOR);
1460 nsm->flags = htons(0);
1461 nsm->vendor = htonl(NX_VENDOR_ID);
1462 nsm->subtype = htonl(subtype);
1466 /* Returns the first byte of the 'body' member of the ofp_stats_request or
1467 * ofp_stats_reply in 'oh'. */
1469 ofputil_stats_body(const struct ofp_header *oh)
1471 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1472 return ((const struct ofp_stats_request *) oh)->body;
1475 /* Returns the length of the 'body' member of the ofp_stats_request or
1476 * ofp_stats_reply in 'oh'. */
1478 ofputil_stats_body_len(const struct ofp_header *oh)
1480 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1481 return ntohs(oh->length) - sizeof(struct ofp_stats_request);
1484 /* Returns the first byte of the body of the nicira_stats_msg in 'oh'. */
1486 ofputil_nxstats_body(const struct ofp_header *oh)
1488 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1489 return ((const struct nicira_stats_msg *) oh) + 1;
1492 /* Returns the length of the body of the nicira_stats_msg in 'oh'. */
1494 ofputil_nxstats_body_len(const struct ofp_header *oh)
1496 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1497 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
1501 make_flow_mod(uint16_t command, const struct cls_rule *rule,
1504 struct ofp_flow_mod *ofm;
1505 size_t size = sizeof *ofm + actions_len;
1506 struct ofpbuf *out = ofpbuf_new(size);
1507 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
1508 ofm->header.version = OFP_VERSION;
1509 ofm->header.type = OFPT_FLOW_MOD;
1510 ofm->header.length = htons(size);
1512 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
1513 ofputil_cls_rule_to_match(rule, NXFF_OPENFLOW10, &ofm->match, 0, NULL);
1514 ofm->command = htons(command);
1519 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
1520 uint16_t idle_timeout, size_t actions_len)
1522 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
1523 struct ofp_flow_mod *ofm = out->data;
1524 ofm->idle_timeout = htons(idle_timeout);
1525 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
1526 ofm->buffer_id = htonl(buffer_id);
1531 make_del_flow(const struct cls_rule *rule)
1533 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
1534 struct ofp_flow_mod *ofm = out->data;
1535 ofm->out_port = htons(OFPP_NONE);
1540 make_add_simple_flow(const struct cls_rule *rule,
1541 uint32_t buffer_id, uint16_t out_port,
1542 uint16_t idle_timeout)
1544 if (out_port != OFPP_NONE) {
1545 struct ofp_action_output *oao;
1546 struct ofpbuf *buffer;
1548 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
1549 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
1550 oao->type = htons(OFPAT_OUTPUT);
1551 oao->len = htons(sizeof *oao);
1552 oao->port = htons(out_port);
1555 return make_add_flow(rule, buffer_id, idle_timeout, 0);
1560 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
1561 const struct ofpbuf *payload, int max_send_len)
1563 struct ofp_packet_in *opi;
1567 send_len = MIN(max_send_len, payload->size);
1568 buf = ofpbuf_new(sizeof *opi + send_len);
1569 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
1570 OFPT_PACKET_IN, 0, buf);
1571 opi->buffer_id = htonl(buffer_id);
1572 opi->total_len = htons(payload->size);
1573 opi->in_port = htons(in_port);
1574 opi->reason = reason;
1575 ofpbuf_put(buf, payload->data, send_len);
1576 update_openflow_length(buf);
1582 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1584 const struct ofp_action_header *actions, size_t n_actions)
1586 size_t actions_len = n_actions * sizeof *actions;
1587 struct ofp_packet_out *opo;
1588 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
1589 struct ofpbuf *out = ofpbuf_new(size);
1591 opo = ofpbuf_put_uninit(out, sizeof *opo);
1592 opo->header.version = OFP_VERSION;
1593 opo->header.type = OFPT_PACKET_OUT;
1594 opo->header.length = htons(size);
1595 opo->header.xid = htonl(0);
1596 opo->buffer_id = htonl(buffer_id);
1597 opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
1598 opo->actions_len = htons(actions_len);
1599 ofpbuf_put(out, actions, actions_len);
1601 ofpbuf_put(out, packet->data, packet->size);
1607 make_unbuffered_packet_out(const struct ofpbuf *packet,
1608 uint16_t in_port, uint16_t out_port)
1610 struct ofp_action_output action;
1611 action.type = htons(OFPAT_OUTPUT);
1612 action.len = htons(sizeof action);
1613 action.port = htons(out_port);
1614 return make_packet_out(packet, UINT32_MAX, in_port,
1615 (struct ofp_action_header *) &action, 1);
1619 make_buffered_packet_out(uint32_t buffer_id,
1620 uint16_t in_port, uint16_t out_port)
1622 if (out_port != OFPP_NONE) {
1623 struct ofp_action_output action;
1624 action.type = htons(OFPAT_OUTPUT);
1625 action.len = htons(sizeof action);
1626 action.port = htons(out_port);
1627 return make_packet_out(NULL, buffer_id, in_port,
1628 (struct ofp_action_header *) &action, 1);
1630 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
1634 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1636 make_echo_request(void)
1638 struct ofp_header *rq;
1639 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1640 rq = ofpbuf_put_uninit(out, sizeof *rq);
1641 rq->version = OFP_VERSION;
1642 rq->type = OFPT_ECHO_REQUEST;
1643 rq->length = htons(sizeof *rq);
1648 /* Creates and returns an OFPT_ECHO_REPLY message matching the
1649 * OFPT_ECHO_REQUEST message in 'rq'. */
1651 make_echo_reply(const struct ofp_header *rq)
1653 size_t size = ntohs(rq->length);
1654 struct ofpbuf *out = ofpbuf_new(size);
1655 struct ofp_header *reply = ofpbuf_put(out, rq, size);
1656 reply->type = OFPT_ECHO_REPLY;
1660 const struct ofp_flow_stats *
1661 flow_stats_first(struct flow_stats_iterator *iter,
1662 const struct ofp_stats_reply *osr)
1664 iter->pos = osr->body;
1665 iter->end = osr->body + (ntohs(osr->header.length)
1666 - offsetof(struct ofp_stats_reply, body));
1667 return flow_stats_next(iter);
1670 const struct ofp_flow_stats *
1671 flow_stats_next(struct flow_stats_iterator *iter)
1673 ptrdiff_t bytes_left = iter->end - iter->pos;
1674 const struct ofp_flow_stats *fs;
1677 if (bytes_left < sizeof *fs) {
1678 if (bytes_left != 0) {
1679 VLOG_WARN_RL(&bad_ofmsg_rl,
1680 "%td leftover bytes in flow stats reply", bytes_left);
1685 fs = (const void *) iter->pos;
1686 length = ntohs(fs->length);
1687 if (length < sizeof *fs) {
1688 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu is shorter than "
1689 "min %zu", length, sizeof *fs);
1691 } else if (length > bytes_left) {
1692 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu but only %td "
1693 "bytes left", length, bytes_left);
1695 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1696 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu has %zu bytes "
1697 "left over in final action", length,
1698 (length - sizeof *fs) % sizeof fs->actions[0]);
1701 iter->pos += length;
1706 check_action_exact_len(const union ofp_action *a, unsigned int len,
1707 unsigned int required_len)
1709 if (len != required_len) {
1710 VLOG_WARN_RL(&bad_ofmsg_rl, "action %"PRIu16" has invalid length "
1711 "%"PRIu16" (must be %u)\n",
1712 ntohs(a->type), ntohs(a->header.len), required_len);
1713 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1719 check_nx_action_exact_len(const struct nx_action_header *a,
1720 unsigned int len, unsigned int required_len)
1722 if (len != required_len) {
1723 VLOG_WARN_RL(&bad_ofmsg_rl,
1724 "Nicira action %"PRIu16" has invalid length %"PRIu16" "
1726 ntohs(a->subtype), ntohs(a->len), required_len);
1727 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1732 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
1733 * that the switch will never have more than 'max_ports' ports. Returns 0 if
1734 * 'port' is valid, otherwise an ofp_mkerr() return code. */
1736 check_output_port(uint16_t port, int max_ports)
1744 case OFPP_CONTROLLER:
1749 if (port < max_ports) {
1752 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
1753 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1757 /* Checks that 'action' is a valid OFPAT_ENQUEUE action, given that the switch
1758 * will never have more than 'max_ports' ports. Returns 0 if 'port' is valid,
1759 * otherwise an ofp_mkerr() return code. */
1761 check_enqueue_action(const union ofp_action *a, unsigned int len,
1764 const struct ofp_action_enqueue *oae;
1768 error = check_action_exact_len(a, len, 16);
1773 oae = (const struct ofp_action_enqueue *) a;
1774 port = ntohs(oae->port);
1775 if (port < max_ports || port == OFPP_IN_PORT) {
1778 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown enqueue port %x", port);
1779 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1783 check_nicira_action(const union ofp_action *a, unsigned int len,
1784 const struct flow *flow)
1786 const struct nx_action_header *nah;
1791 VLOG_WARN_RL(&bad_ofmsg_rl,
1792 "Nicira vendor action only %u bytes", len);
1793 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1795 nah = (const struct nx_action_header *) a;
1797 subtype = ntohs(nah->subtype);
1798 if (subtype > TYPE_MAXIMUM(enum nx_action_subtype)) {
1799 /* This is necessary because enum nx_action_subtype is probably an
1800 * 8-bit type, so the cast below throws away the top 8 bits. */
1801 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1804 switch ((enum nx_action_subtype) subtype) {
1805 case NXAST_RESUBMIT:
1806 case NXAST_SET_TUNNEL:
1807 case NXAST_DROP_SPOOFED_ARP:
1808 case NXAST_SET_QUEUE:
1809 case NXAST_POP_QUEUE:
1810 return check_nx_action_exact_len(nah, len, 16);
1812 case NXAST_REG_MOVE:
1813 error = check_nx_action_exact_len(nah, len,
1814 sizeof(struct nx_action_reg_move));
1818 return nxm_check_reg_move((const struct nx_action_reg_move *) a, flow);
1820 case NXAST_REG_LOAD:
1821 error = check_nx_action_exact_len(nah, len,
1822 sizeof(struct nx_action_reg_load));
1826 return nxm_check_reg_load((const struct nx_action_reg_load *) a, flow);
1831 case NXAST_SET_TUNNEL64:
1832 return check_nx_action_exact_len(
1833 nah, len, sizeof(struct nx_action_set_tunnel64));
1835 case NXAST_MULTIPATH:
1836 error = check_nx_action_exact_len(
1837 nah, len, sizeof(struct nx_action_multipath));
1841 return multipath_check((const struct nx_action_multipath *) a);
1843 case NXAST_SNAT__OBSOLETE:
1845 VLOG_WARN_RL(&bad_ofmsg_rl,
1846 "unknown Nicira vendor action subtype %"PRIu16,
1847 ntohs(nah->subtype));
1848 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1853 check_action(const union ofp_action *a, unsigned int len,
1854 const struct flow *flow, int max_ports)
1856 enum ofp_action_type type = ntohs(a->type);
1861 error = check_action_exact_len(a, len, 8);
1865 return check_output_port(ntohs(a->output.port), max_ports);
1867 case OFPAT_SET_VLAN_VID:
1868 error = check_action_exact_len(a, len, 8);
1872 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1873 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1877 case OFPAT_SET_VLAN_PCP:
1878 error = check_action_exact_len(a, len, 8);
1882 if (a->vlan_vid.vlan_vid & ~7) {
1883 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1887 case OFPAT_STRIP_VLAN:
1888 case OFPAT_SET_NW_SRC:
1889 case OFPAT_SET_NW_DST:
1890 case OFPAT_SET_NW_TOS:
1891 case OFPAT_SET_TP_SRC:
1892 case OFPAT_SET_TP_DST:
1893 return check_action_exact_len(a, len, 8);
1895 case OFPAT_SET_DL_SRC:
1896 case OFPAT_SET_DL_DST:
1897 return check_action_exact_len(a, len, 16);
1900 return (a->vendor.vendor == htonl(NX_VENDOR_ID)
1901 ? check_nicira_action(a, len, flow)
1902 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
1905 return check_enqueue_action(a, len, max_ports);
1908 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %d", (int) type);
1909 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
1914 validate_actions(const union ofp_action *actions, size_t n_actions,
1915 const struct flow *flow, int max_ports)
1919 for (i = 0; i < n_actions; ) {
1920 const union ofp_action *a = &actions[i];
1921 unsigned int len = ntohs(a->header.len);
1922 unsigned int n_slots = len / OFP_ACTION_ALIGN;
1923 unsigned int slots_left = &actions[n_actions] - a;
1926 if (n_slots > slots_left) {
1927 VLOG_WARN_RL(&bad_ofmsg_rl,
1928 "action requires %u slots but only %u remain",
1929 n_slots, slots_left);
1930 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1932 VLOG_WARN_RL(&bad_ofmsg_rl, "action has invalid length 0");
1933 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1934 } else if (len % OFP_ACTION_ALIGN) {
1935 VLOG_WARN_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
1936 "of %d", len, OFP_ACTION_ALIGN);
1937 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1940 error = check_action(a, len, flow, max_ports);
1949 /* Returns true if 'action' outputs to 'port' (which must be in network byte
1950 * order), false otherwise. */
1952 action_outputs_to_port(const union ofp_action *action, uint16_t port)
1954 switch (ntohs(action->type)) {
1956 return action->output.port == port;
1958 return ((const struct ofp_action_enqueue *) action)->port == port;
1964 /* The set of actions must either come from a trusted source or have been
1965 * previously validated with validate_actions(). */
1966 const union ofp_action *
1967 actions_first(struct actions_iterator *iter,
1968 const union ofp_action *oa, size_t n_actions)
1971 iter->end = oa + n_actions;
1972 return actions_next(iter);
1975 const union ofp_action *
1976 actions_next(struct actions_iterator *iter)
1978 if (iter->pos != iter->end) {
1979 const union ofp_action *a = iter->pos;
1980 unsigned int len = ntohs(a->header.len);
1981 iter->pos += len / OFP_ACTION_ALIGN;
1989 normalize_match(struct ofp_match *m)
1991 enum { OFPFW_NW = (OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO
1993 enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
1996 wc = ntohl(m->wildcards) & OVSFW_ALL;
1997 if (wc & OFPFW_DL_TYPE) {
2000 /* Can't sensibly match on network or transport headers if the
2001 * data link type is unknown. */
2002 wc |= OFPFW_NW | OFPFW_TP;
2003 m->nw_src = m->nw_dst = m->nw_proto = m->nw_tos = 0;
2004 m->tp_src = m->tp_dst = 0;
2005 } else if (m->dl_type == htons(ETH_TYPE_IP)) {
2006 if (wc & OFPFW_NW_PROTO) {
2009 /* Can't sensibly match on transport headers if the network
2010 * protocol is unknown. */
2012 m->tp_src = m->tp_dst = 0;
2013 } else if (m->nw_proto == IPPROTO_TCP ||
2014 m->nw_proto == IPPROTO_UDP ||
2015 m->nw_proto == IPPROTO_ICMP) {
2016 if (wc & OFPFW_TP_SRC) {
2019 if (wc & OFPFW_TP_DST) {
2023 /* Transport layer fields will always be extracted as zeros, so we
2024 * can do an exact-match on those values. */
2026 m->tp_src = m->tp_dst = 0;
2028 if (wc & OFPFW_NW_SRC_MASK) {
2029 m->nw_src &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_SRC_SHIFT);
2031 if (wc & OFPFW_NW_DST_MASK) {
2032 m->nw_dst &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_DST_SHIFT);
2034 if (wc & OFPFW_NW_TOS) {
2037 m->nw_tos &= IP_DSCP_MASK;
2039 } else if (m->dl_type == htons(ETH_TYPE_ARP)) {
2040 if (wc & OFPFW_NW_PROTO) {
2043 if (wc & OFPFW_NW_SRC_MASK) {
2044 m->nw_src &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_SRC_SHIFT);
2046 if (wc & OFPFW_NW_DST_MASK) {
2047 m->nw_dst &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_DST_SHIFT);
2049 m->tp_src = m->tp_dst = m->nw_tos = 0;
2050 } else if (m->dl_type == htons(ETH_TYPE_IPV6)) {
2051 /* Don't normalize IPv6 traffic, since OpenFlow doesn't have a
2052 * way to express it. */
2054 /* Network and transport layer fields will always be extracted as
2055 * zeros, so we can do an exact-match on those values. */
2056 wc &= ~(OFPFW_NW | OFPFW_TP);
2057 m->nw_proto = m->nw_src = m->nw_dst = m->nw_tos = 0;
2058 m->tp_src = m->tp_dst = 0;
2060 if (wc & OFPFW_DL_SRC) {
2061 memset(m->dl_src, 0, sizeof m->dl_src);
2063 if (wc & OFPFW_DL_DST) {
2064 memset(m->dl_dst, 0, sizeof m->dl_dst);
2066 m->wildcards = htonl(wc);
2069 /* Returns a string that describes 'match' in a very literal way, without
2070 * interpreting its contents except in a very basic fashion. The returned
2071 * string is intended to be fixed-length, so that it is easy to see differences
2072 * between two such strings if one is put above another. This is useful for
2073 * describing changes made by normalize_match().
2075 * The caller must free the returned string (with free()). */
2077 ofp_match_to_literal_string(const struct ofp_match *match)
2079 return xasprintf("wildcards=%#10"PRIx32" "
2080 " in_port=%5"PRId16" "
2081 " dl_src="ETH_ADDR_FMT" "
2082 " dl_dst="ETH_ADDR_FMT" "
2083 " dl_vlan=%5"PRId16" "
2084 " dl_vlan_pcp=%3"PRId8" "
2085 " dl_type=%#6"PRIx16" "
2086 " nw_tos=%#4"PRIx8" "
2087 " nw_proto=%#4"PRIx16" "
2088 " nw_src=%#10"PRIx32" "
2089 " nw_dst=%#10"PRIx32" "
2090 " tp_src=%5"PRId16" "
2092 ntohl(match->wildcards),
2093 ntohs(match->in_port),
2094 ETH_ADDR_ARGS(match->dl_src),
2095 ETH_ADDR_ARGS(match->dl_dst),
2096 ntohs(match->dl_vlan),
2098 ntohs(match->dl_type),
2101 ntohl(match->nw_src),
2102 ntohl(match->nw_dst),
2103 ntohs(match->tp_src),
2104 ntohs(match->tp_dst));
2108 vendor_code_to_id(uint8_t code)
2111 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
2113 #undef OFPUTIL_VENDOR
2120 vendor_id_to_code(uint32_t id)
2123 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case VENDOR_ID: return NAME;
2125 #undef OFPUTIL_VENDOR
2131 /* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
2132 * information taken from 'error', whose encoding must be as described in the
2133 * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
2134 * oh->xid as its transaction ID, and it will include up to the first 64 bytes
2137 * Returns NULL if 'error' is not an OpenFlow error code. */
2139 ofputil_encode_error_msg(int error, const struct ofp_header *oh)
2141 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2151 if (!is_ofp_error(error)) {
2152 /* We format 'error' with strerror() here since it seems likely to be
2153 * a system errno value. */
2154 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
2155 error, strerror(error));
2162 len = ntohs(oh->length);
2172 vendor = get_ofp_err_vendor(error);
2173 type = get_ofp_err_type(error);
2174 code = get_ofp_err_code(error);
2175 if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
2176 struct ofp_error_msg *oem;
2178 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
2179 oem->type = htons(type);
2180 oem->code = htons(code);
2182 struct ofp_error_msg *oem;
2183 struct nx_vendor_error *nve;
2186 vendor_id = vendor_code_to_id(vendor);
2187 if (vendor_id == UINT32_MAX) {
2188 VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
2193 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
2194 OFPT_ERROR, xid, &buf);
2195 oem->type = htons(NXET_VENDOR);
2196 oem->code = htons(NXVC_VENDOR_ERROR);
2198 nve = (struct nx_vendor_error *)oem->data;
2199 nve->vendor = htonl(vendor_id);
2200 nve->type = htons(type);
2201 nve->code = htons(code);
2206 ofpbuf_put(buf, data, len);
2212 /* Decodes 'oh', which should be an OpenFlow OFPT_ERROR message, and returns an
2213 * Open vSwitch internal error code in the format described in the large
2214 * comment in ofp-util.h.
2216 * If 'payload_ofs' is nonnull, on success '*payload_ofs' is set to the offset
2217 * to the payload starting from 'oh' and on failure it is set to 0. */
2219 ofputil_decode_error_msg(const struct ofp_header *oh, size_t *payload_ofs)
2221 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2223 const struct ofp_error_msg *oem;
2224 uint16_t type, code;
2231 if (oh->type != OFPT_ERROR) {
2235 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2236 oem = ofpbuf_try_pull(&b, sizeof *oem);
2241 type = ntohs(oem->type);
2242 code = ntohs(oem->code);
2243 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
2244 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
2249 vendor = vendor_id_to_code(ntohl(nve->vendor));
2251 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
2252 ntohl(nve->vendor));
2255 type = ntohs(nve->type);
2256 code = ntohs(nve->code);
2258 vendor = OFPUTIL_VENDOR_OPENFLOW;
2262 VLOG_WARN_RL(&rl, "error contains type %"PRIu16" greater than "
2263 "supported maximum value 1023", type);
2268 *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
2270 return ofp_mkerr_vendor(vendor, type, code);
2274 ofputil_format_error(struct ds *s, int error)
2276 if (is_errno(error)) {
2277 ds_put_cstr(s, strerror(error));
2279 uint16_t type = get_ofp_err_type(error);
2280 uint16_t code = get_ofp_err_code(error);
2281 const char *type_s = ofp_error_type_to_string(type);
2282 const char *code_s = ofp_error_code_to_string(type, code);
2284 ds_put_format(s, "type ");
2286 ds_put_cstr(s, type_s);
2288 ds_put_format(s, "%"PRIu16, type);
2291 ds_put_cstr(s, ", code ");
2293 ds_put_cstr(s, code_s);
2295 ds_put_format(s, "%"PRIu16, code);
2301 ofputil_error_to_string(int error)
2303 struct ds s = DS_EMPTY_INITIALIZER;
2304 ofputil_format_error(&s, error);
2305 return ds_steal_cstr(&s);
2308 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
2309 * successful, otherwise an OpenFlow error.
2311 * If successful, the first action is stored in '*actionsp' and the number of
2312 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
2313 * are stored, respectively.
2315 * This function does not check that the actions are valid (the caller should
2316 * do so, with validate_actions()). The caller is also responsible for making
2317 * sure that 'b->data' is initially aligned appropriately for "union
2320 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2321 union ofp_action **actionsp, size_t *n_actionsp)
2323 if (actions_len % OFP_ACTION_ALIGN != 0) {
2324 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2325 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
2329 *actionsp = ofpbuf_try_pull(b, actions_len);
2330 if (*actionsp == NULL) {
2331 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2332 "exceeds remaining message length (%zu)",
2333 actions_len, b->size);
2337 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
2343 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);