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"
23 #include "byte-order.h"
24 #include "classifier.h"
25 #include "dynamic-string.h"
26 #include "multipath.h"
28 #include "ofp-errors.h"
33 #include "unaligned.h"
34 #include "type-props.h"
37 VLOG_DEFINE_THIS_MODULE(ofp_util);
39 static void normalize_match(struct ofp_match *);
41 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
42 * in the peer and so there's not much point in showing a lot of them. */
43 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
45 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
46 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
49 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
50 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
51 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
52 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
55 ofputil_wcbits_to_netmask(int wcbits)
58 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
61 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
62 * that it wildcards. 'netmask' must be a CIDR netmask (see ip_is_cidr()). */
64 ofputil_netmask_to_wcbits(ovs_be32 netmask)
66 assert(ip_is_cidr(netmask));
68 return netmask == htonl(0) ? 32 : __builtin_ctz(ntohl(netmask));
72 for (wcbits = 32; netmask; wcbits--) {
73 netmask &= netmask - 1;
80 /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
82 #define WC_INVARIANT_LIST \
83 WC_INVARIANT_BIT(IN_PORT) \
84 WC_INVARIANT_BIT(DL_SRC) \
85 WC_INVARIANT_BIT(DL_DST) \
86 WC_INVARIANT_BIT(DL_TYPE) \
87 WC_INVARIANT_BIT(NW_PROTO) \
88 WC_INVARIANT_BIT(TP_SRC) \
89 WC_INVARIANT_BIT(TP_DST)
91 /* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
92 * actually have the same names and values. */
93 #define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME);
95 #undef WC_INVARIANT_BIT
97 /* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
101 #define WC_INVARIANT_BIT(NAME) | FWW_##NAME
103 #undef WC_INVARIANT_BIT
106 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
109 ofputil_cls_rule_from_match(const struct ofp_match *match,
110 unsigned int priority, struct cls_rule *rule)
112 struct flow_wildcards *wc = &rule->wc;
116 /* Initialize rule->priority. */
117 ofpfw = ntohl(match->wildcards) & OFPFW_ALL;
118 rule->priority = !ofpfw ? UINT16_MAX : priority;
120 /* Initialize most of rule->wc. */
121 flow_wildcards_init_catchall(wc);
122 wc->wildcards = ofpfw & WC_INVARIANTS;
124 /* Wildcard fields that aren't defined by ofp_match or tun_id. */
125 wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_ND_TARGET);
127 if (ofpfw & OFPFW_NW_TOS) {
128 wc->wildcards |= FWW_NW_TOS;
130 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
131 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
133 if (ofpfw & OFPFW_DL_DST) {
134 /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
135 * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST
136 * and FWW_ETH_MCAST. */
137 wc->wildcards |= FWW_ETH_MCAST;
140 /* Initialize most of rule->flow. */
141 rule->flow.nw_src = match->nw_src;
142 rule->flow.nw_dst = match->nw_dst;
143 rule->flow.in_port = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
144 : ntohs(match->in_port));
145 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
146 rule->flow.tp_src = match->tp_src;
147 rule->flow.tp_dst = match->tp_dst;
148 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
149 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
150 rule->flow.nw_tos = match->nw_tos;
151 rule->flow.nw_proto = match->nw_proto;
153 /* Translate VLANs. */
154 vid = match->dl_vlan & htons(VLAN_VID_MASK);
155 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
156 switch (ofpfw & (OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP)) {
157 case OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP:
158 /* Wildcard everything. */
159 rule->flow.vlan_tci = htons(0);
160 rule->wc.vlan_tci_mask = htons(0);
163 case OFPFW_DL_VLAN_PCP:
164 if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
165 /* Match only packets without 802.1Q header. */
166 rule->flow.vlan_tci = htons(0);
167 rule->wc.vlan_tci_mask = htons(0xffff);
169 /* Wildcard PCP, specific VID. */
170 rule->flow.vlan_tci = vid | htons(VLAN_CFI);
171 rule->wc.vlan_tci_mask = htons(VLAN_VID_MASK | VLAN_CFI);
176 /* Wildcard VID, specific PCP. */
177 rule->flow.vlan_tci = pcp | htons(VLAN_CFI);
178 rule->wc.vlan_tci_mask = htons(VLAN_PCP_MASK | VLAN_CFI);
182 if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
183 /* This case is odd, since we can't have a specific PCP without an
184 * 802.1Q header. However, older versions of OVS treated this as
185 * matching packets withut an 802.1Q header, so we do here too. */
186 rule->flow.vlan_tci = htons(0);
187 rule->wc.vlan_tci_mask = htons(0xffff);
189 /* Specific VID and PCP. */
190 rule->flow.vlan_tci = vid | pcp | htons(VLAN_CFI);
191 rule->wc.vlan_tci_mask = htons(0xffff);
197 cls_rule_zero_wildcarded_fields(rule);
200 /* Convert 'rule' into the OpenFlow match structure 'match'. */
202 ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match)
204 const struct flow_wildcards *wc = &rule->wc;
207 /* Figure out most OpenFlow wildcards. */
208 ofpfw = wc->wildcards & WC_INVARIANTS;
209 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
210 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
211 if (wc->wildcards & FWW_NW_TOS) {
212 ofpfw |= OFPFW_NW_TOS;
215 /* Translate VLANs. */
216 match->dl_vlan = htons(0);
217 match->dl_vlan_pcp = 0;
218 if (rule->wc.vlan_tci_mask == htons(0)) {
219 ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
220 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
221 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
222 match->dl_vlan = htons(OFP_VLAN_NONE);
224 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
225 ofpfw |= OFPFW_DL_VLAN;
227 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
230 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
231 ofpfw |= OFPFW_DL_VLAN_PCP;
233 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
237 /* Compose most of the match structure. */
238 match->wildcards = htonl(ofpfw);
239 match->in_port = htons(rule->flow.in_port == ODPP_LOCAL ? OFPP_LOCAL
240 : rule->flow.in_port);
241 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
242 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
243 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
244 match->nw_src = rule->flow.nw_src;
245 match->nw_dst = rule->flow.nw_dst;
246 match->nw_tos = rule->flow.nw_tos;
247 match->nw_proto = rule->flow.nw_proto;
248 match->tp_src = rule->flow.tp_src;
249 match->tp_dst = rule->flow.tp_dst;
250 memset(match->pad1, '\0', sizeof match->pad1);
251 memset(match->pad2, '\0', sizeof match->pad2);
254 /* Given a 'dl_type' value in the format used in struct flow, returns the
255 * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */
257 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
259 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
260 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
264 /* Given a 'dl_type' value in the format used in an OpenFlow ofp_match
265 * structure, returns the corresponding 'dl_type' value for use in struct
268 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
270 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
271 ? htons(FLOW_DL_TYPE_NONE)
275 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
279 static uint32_t next_xid = 1;
280 return htonl(next_xid++);
283 /* Basic parsing of OpenFlow messages. */
285 struct ofputil_msg_type {
286 enum ofputil_msg_code code; /* OFPUTIL_*. */
287 uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
288 const char *name; /* e.g. "OFPT_FLOW_REMOVED". */
289 unsigned int min_size; /* Minimum total message size in bytes. */
290 /* 0 if 'min_size' is the exact size that the message must be. Otherwise,
291 * the message may exceed 'min_size' by an even multiple of this value. */
292 unsigned int extra_multiple;
295 struct ofputil_msg_category {
296 const char *name; /* e.g. "OpenFlow message" */
297 const struct ofputil_msg_type *types;
299 int missing_error; /* ofp_mkerr() value for missing type. */
303 ofputil_length_ok(const struct ofputil_msg_category *cat,
304 const struct ofputil_msg_type *type,
307 switch (type->extra_multiple) {
309 if (size != type->min_size) {
310 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
311 "length %u (expected length %u)",
312 cat->name, type->name, size, type->min_size);
318 if (size < type->min_size) {
319 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
320 "length %u (expected length at least %u bytes)",
321 cat->name, type->name, size, type->min_size);
327 if (size < type->min_size
328 || (size - type->min_size) % type->extra_multiple) {
329 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
330 "length %u (must be exactly %u bytes or longer "
331 "by an integer multiple of %u bytes)",
332 cat->name, type->name, size,
333 type->min_size, type->extra_multiple);
341 ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
342 uint32_t value, unsigned int size,
343 const struct ofputil_msg_type **typep)
345 const struct ofputil_msg_type *type;
347 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
348 if (type->value == value) {
349 if (!ofputil_length_ok(cat, type, size)) {
350 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
357 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
359 return cat->missing_error;
363 ofputil_decode_vendor(const struct ofp_header *oh,
364 const struct ofputil_msg_type **typep)
366 static const struct ofputil_msg_type nxt_messages[] = {
367 { OFPUTIL_NXT_ROLE_REQUEST,
368 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
369 sizeof(struct nx_role_request), 0 },
371 { OFPUTIL_NXT_ROLE_REPLY,
372 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
373 sizeof(struct nx_role_request), 0 },
375 { OFPUTIL_NXT_SET_FLOW_FORMAT,
376 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
377 sizeof(struct nxt_set_flow_format), 0 },
379 { OFPUTIL_NXT_FLOW_MOD,
380 NXT_FLOW_MOD, "NXT_FLOW_MOD",
381 sizeof(struct nx_flow_mod), 8 },
383 { OFPUTIL_NXT_FLOW_REMOVED,
384 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
385 sizeof(struct nx_flow_removed), 8 },
388 static const struct ofputil_msg_category nxt_category = {
389 "Nicira extension message",
390 nxt_messages, ARRAY_SIZE(nxt_messages),
391 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
394 const struct ofp_vendor_header *ovh;
395 const struct nicira_header *nh;
397 ovh = (const struct ofp_vendor_header *) oh;
398 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
399 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
400 "vendor %"PRIx32, ntohl(ovh->vendor));
401 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
404 if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
405 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
406 "length %u (expected at least %zu)",
407 ntohs(ovh->header.length), sizeof(struct nicira_header));
408 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
411 nh = (const struct nicira_header *) oh;
412 return ofputil_lookup_openflow_message(&nxt_category, ntohl(nh->subtype),
413 ntohs(oh->length), typep);
417 check_nxstats_msg(const struct ofp_header *oh)
419 const struct ofp_stats_request *osr;
422 osr = (const struct ofp_stats_request *) oh;
424 memcpy(&vendor, osr->body, sizeof vendor);
425 if (vendor != htonl(NX_VENDOR_ID)) {
426 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
427 "unknown vendor %"PRIx32, ntohl(vendor));
428 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
431 if (ntohs(osr->header.length) < sizeof(struct nicira_stats_msg)) {
432 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
433 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
440 ofputil_decode_nxst_request(const struct ofp_header *oh,
441 const struct ofputil_msg_type **typep)
443 static const struct ofputil_msg_type nxst_requests[] = {
444 { OFPUTIL_NXST_FLOW_REQUEST,
445 NXST_FLOW, "NXST_FLOW request",
446 sizeof(struct nx_flow_stats_request), 8 },
448 { OFPUTIL_NXST_AGGREGATE_REQUEST,
449 NXST_AGGREGATE, "NXST_AGGREGATE request",
450 sizeof(struct nx_aggregate_stats_request), 8 },
453 static const struct ofputil_msg_category nxst_request_category = {
454 "Nicira extension statistics request",
455 nxst_requests, ARRAY_SIZE(nxst_requests),
456 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
459 const struct nicira_stats_msg *nsm;
462 error = check_nxstats_msg(oh);
467 nsm = (struct nicira_stats_msg *) oh;
468 return ofputil_lookup_openflow_message(&nxst_request_category,
470 ntohs(oh->length), typep);
474 ofputil_decode_nxst_reply(const struct ofp_header *oh,
475 const struct ofputil_msg_type **typep)
477 static const struct ofputil_msg_type nxst_replies[] = {
478 { OFPUTIL_NXST_FLOW_REPLY,
479 NXST_FLOW, "NXST_FLOW reply",
480 sizeof(struct nicira_stats_msg), 8 },
482 { OFPUTIL_NXST_AGGREGATE_REPLY,
483 NXST_AGGREGATE, "NXST_AGGREGATE reply",
484 sizeof(struct nx_aggregate_stats_reply), 0 },
487 static const struct ofputil_msg_category nxst_reply_category = {
488 "Nicira extension statistics reply",
489 nxst_replies, ARRAY_SIZE(nxst_replies),
490 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
493 const struct nicira_stats_msg *nsm;
496 error = check_nxstats_msg(oh);
501 nsm = (struct nicira_stats_msg *) oh;
502 return ofputil_lookup_openflow_message(&nxst_reply_category,
504 ntohs(oh->length), typep);
508 ofputil_decode_ofpst_request(const struct ofp_header *oh,
509 const struct ofputil_msg_type **typep)
511 enum { OSR_SIZE = sizeof(struct ofp_stats_request) };
512 static const struct ofputil_msg_type ofpst_requests[] = {
513 { OFPUTIL_OFPST_DESC_REQUEST,
514 OFPST_DESC, "OFPST_DESC request",
517 { OFPUTIL_OFPST_FLOW_REQUEST,
518 OFPST_FLOW, "OFPST_FLOW request",
519 OSR_SIZE + sizeof(struct ofp_flow_stats_request), 0 },
521 { OFPUTIL_OFPST_AGGREGATE_REQUEST,
522 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
523 OSR_SIZE + sizeof(struct ofp_aggregate_stats_request), 0 },
525 { OFPUTIL_OFPST_TABLE_REQUEST,
526 OFPST_TABLE, "OFPST_TABLE request",
529 { OFPUTIL_OFPST_PORT_REQUEST,
530 OFPST_PORT, "OFPST_PORT request",
531 OSR_SIZE + sizeof(struct ofp_port_stats_request), 0 },
533 { OFPUTIL_OFPST_QUEUE_REQUEST,
534 OFPST_QUEUE, "OFPST_QUEUE request",
535 OSR_SIZE + sizeof(struct ofp_queue_stats_request), 0 },
538 OFPST_VENDOR, "OFPST_VENDOR request",
539 OSR_SIZE + sizeof(uint32_t), 1 },
542 static const struct ofputil_msg_category ofpst_request_category = {
543 "OpenFlow statistics",
544 ofpst_requests, ARRAY_SIZE(ofpst_requests),
545 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
548 const struct ofp_stats_request *osr;
551 osr = (const struct ofp_stats_request *) oh;
552 error = ofputil_lookup_openflow_message(&ofpst_request_category,
554 ntohs(oh->length), typep);
555 if (!error && osr->type == htons(OFPST_VENDOR)) {
556 error = ofputil_decode_nxst_request(oh, typep);
562 ofputil_decode_ofpst_reply(const struct ofp_header *oh,
563 const struct ofputil_msg_type **typep)
565 enum { OSR_SIZE = sizeof(struct ofp_stats_reply) };
566 static const struct ofputil_msg_type ofpst_replies[] = {
567 { OFPUTIL_OFPST_DESC_REPLY,
568 OFPST_DESC, "OFPST_DESC reply",
569 OSR_SIZE + sizeof(struct ofp_desc_stats), 0 },
571 { OFPUTIL_OFPST_FLOW_REPLY,
572 OFPST_FLOW, "OFPST_FLOW reply",
575 { OFPUTIL_OFPST_AGGREGATE_REPLY,
576 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
577 OSR_SIZE + sizeof(struct ofp_aggregate_stats_reply), 0 },
579 { OFPUTIL_OFPST_TABLE_REPLY,
580 OFPST_TABLE, "OFPST_TABLE reply",
581 OSR_SIZE, sizeof(struct ofp_table_stats) },
583 { OFPUTIL_OFPST_PORT_REPLY,
584 OFPST_PORT, "OFPST_PORT reply",
585 OSR_SIZE, sizeof(struct ofp_port_stats) },
587 { OFPUTIL_OFPST_QUEUE_REPLY,
588 OFPST_QUEUE, "OFPST_QUEUE reply",
589 OSR_SIZE, sizeof(struct ofp_queue_stats) },
592 OFPST_VENDOR, "OFPST_VENDOR reply",
593 OSR_SIZE + sizeof(uint32_t), 1 },
596 static const struct ofputil_msg_category ofpst_reply_category = {
597 "OpenFlow statistics",
598 ofpst_replies, ARRAY_SIZE(ofpst_replies),
599 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
602 const struct ofp_stats_reply *osr = (const struct ofp_stats_reply *) oh;
605 error = ofputil_lookup_openflow_message(&ofpst_reply_category,
607 ntohs(oh->length), typep);
608 if (!error && osr->type == htons(OFPST_VENDOR)) {
609 error = ofputil_decode_nxst_reply(oh, typep);
614 /* Decodes the message type represented by 'oh'. Returns 0 if successful or
615 * an OpenFlow error code constructed with ofp_mkerr() on failure. Either
616 * way, stores in '*typep' a type structure that can be inspected with the
617 * ofputil_msg_type_*() functions.
619 * oh->length must indicate the correct length of the message (and must be at
620 * least sizeof(struct ofp_header)).
622 * Success indicates that 'oh' is at least as long as the minimum-length
623 * message of its type. */
625 ofputil_decode_msg_type(const struct ofp_header *oh,
626 const struct ofputil_msg_type **typep)
628 static const struct ofputil_msg_type ofpt_messages[] = {
629 { OFPUTIL_OFPT_HELLO,
630 OFPT_HELLO, "OFPT_HELLO",
631 sizeof(struct ofp_hello), 1 },
633 { OFPUTIL_OFPT_ERROR,
634 OFPT_ERROR, "OFPT_ERROR",
635 sizeof(struct ofp_error_msg), 1 },
637 { OFPUTIL_OFPT_ECHO_REQUEST,
638 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
639 sizeof(struct ofp_header), 1 },
641 { OFPUTIL_OFPT_ECHO_REPLY,
642 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
643 sizeof(struct ofp_header), 1 },
645 { OFPUTIL_OFPT_FEATURES_REQUEST,
646 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
647 sizeof(struct ofp_header), 0 },
649 { OFPUTIL_OFPT_FEATURES_REPLY,
650 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
651 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
653 { OFPUTIL_OFPT_GET_CONFIG_REQUEST,
654 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
655 sizeof(struct ofp_header), 0 },
657 { OFPUTIL_OFPT_GET_CONFIG_REPLY,
658 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
659 sizeof(struct ofp_switch_config), 0 },
661 { OFPUTIL_OFPT_SET_CONFIG,
662 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
663 sizeof(struct ofp_switch_config), 0 },
665 { OFPUTIL_OFPT_PACKET_IN,
666 OFPT_PACKET_IN, "OFPT_PACKET_IN",
667 offsetof(struct ofp_packet_in, data), 1 },
669 { OFPUTIL_OFPT_FLOW_REMOVED,
670 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
671 sizeof(struct ofp_flow_removed), 0 },
673 { OFPUTIL_OFPT_PORT_STATUS,
674 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
675 sizeof(struct ofp_port_status), 0 },
677 { OFPUTIL_OFPT_PACKET_OUT,
678 OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
679 sizeof(struct ofp_packet_out), 1 },
681 { OFPUTIL_OFPT_FLOW_MOD,
682 OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
683 sizeof(struct ofp_flow_mod), 1 },
685 { OFPUTIL_OFPT_PORT_MOD,
686 OFPT_PORT_MOD, "OFPT_PORT_MOD",
687 sizeof(struct ofp_port_mod), 0 },
690 OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
691 sizeof(struct ofp_stats_request), 1 },
694 OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
695 sizeof(struct ofp_stats_reply), 1 },
697 { OFPUTIL_OFPT_BARRIER_REQUEST,
698 OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
699 sizeof(struct ofp_header), 0 },
701 { OFPUTIL_OFPT_BARRIER_REPLY,
702 OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
703 sizeof(struct ofp_header), 0 },
706 OFPT_VENDOR, "OFPT_VENDOR",
707 sizeof(struct ofp_vendor_header), 1 },
710 static const struct ofputil_msg_category ofpt_category = {
712 ofpt_messages, ARRAY_SIZE(ofpt_messages),
713 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE)
718 error = ofputil_lookup_openflow_message(&ofpt_category, oh->type,
719 ntohs(oh->length), typep);
723 error = ofputil_decode_vendor(oh, typep);
726 case OFPT_STATS_REQUEST:
727 error = ofputil_decode_ofpst_request(oh, typep);
730 case OFPT_STATS_REPLY:
731 error = ofputil_decode_ofpst_reply(oh, typep);
738 static const struct ofputil_msg_type ofputil_invalid_type = {
740 0, "OFPUTIL_INVALID",
744 *typep = &ofputil_invalid_type;
749 /* Returns an OFPUTIL_* message type code for 'type'. */
750 enum ofputil_msg_code
751 ofputil_msg_type_code(const struct ofputil_msg_type *type)
759 ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
761 switch (flow_format) {
762 case NXFF_OPENFLOW10:
771 ofputil_flow_format_to_string(enum nx_flow_format flow_format)
773 switch (flow_format) {
774 case NXFF_OPENFLOW10:
784 ofputil_flow_format_from_string(const char *s)
786 return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
787 : !strcmp(s, "nxm") ? NXFF_NXM
792 regs_fully_wildcarded(const struct flow_wildcards *wc)
796 for (i = 0; i < FLOW_N_REGS; i++) {
797 if (wc->reg_masks[i] != 0) {
804 /* Returns the minimum nx_flow_format to use for sending 'rule' to a switch
805 * (e.g. to add or remove a flow). Only NXM can handle tunnel IDs, registers,
806 * or fixing the Ethernet multicast bit. Otherwise, it's better to use
807 * NXFF_OPENFLOW10 for backward compatibility. */
809 ofputil_min_flow_format(const struct cls_rule *rule)
811 const struct flow_wildcards *wc = &rule->wc;
813 /* Only NXM supports separately wildcards the Ethernet multicast bit. */
814 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
818 /* Only NXM supports matching ARP hardware addresses. */
819 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
823 /* Only NXM supports matching IPv6 traffic. */
824 if (!(wc->wildcards & FWW_DL_TYPE)
825 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
829 /* Only NXM supports matching registers. */
830 if (!regs_fully_wildcarded(wc)) {
834 /* Only NXM supports matching tun_id. */
835 if (wc->tun_id_mask != htonll(0)) {
839 /* Other formats can express this rule. */
840 return NXFF_OPENFLOW10;
843 /* Returns an OpenFlow message that can be used to set the flow format to
846 ofputil_make_set_flow_format(enum nx_flow_format flow_format)
848 struct nxt_set_flow_format *sff;
851 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
852 sff->format = htonl(flow_format);
857 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
858 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
861 * Does not validate the flow_mod actions. */
863 ofputil_decode_flow_mod(struct flow_mod *fm, const struct ofp_header *oh)
865 const struct ofputil_msg_type *type;
868 ofpbuf_use_const(&b, oh, ntohs(oh->length));
870 ofputil_decode_msg_type(oh, &type);
871 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
872 /* Standard OpenFlow flow_mod. */
873 struct ofp_match match, orig_match;
874 const struct ofp_flow_mod *ofm;
877 /* Dissect the message. */
878 ofm = ofpbuf_pull(&b, sizeof *ofm);
879 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
884 /* Normalize ofm->match. If normalization actually changes anything,
885 * then log the differences. */
887 match.pad1[0] = match.pad2[0] = 0;
889 normalize_match(&match);
890 if (memcmp(&match, &orig_match, sizeof orig_match)) {
891 if (!VLOG_DROP_INFO(&bad_ofmsg_rl)) {
892 char *old = ofp_match_to_literal_string(&orig_match);
893 char *new = ofp_match_to_literal_string(&match);
894 VLOG_INFO("normalization changed ofp_match, details:");
895 VLOG_INFO(" pre: %s", old);
896 VLOG_INFO("post: %s", new);
902 /* Translate the message. */
903 ofputil_cls_rule_from_match(&match, ntohs(ofm->priority), &fm->cr);
904 fm->cookie = ofm->cookie;
905 fm->command = ntohs(ofm->command);
906 fm->idle_timeout = ntohs(ofm->idle_timeout);
907 fm->hard_timeout = ntohs(ofm->hard_timeout);
908 fm->buffer_id = ntohl(ofm->buffer_id);
909 fm->out_port = ntohs(ofm->out_port);
910 fm->flags = ntohs(ofm->flags);
911 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
912 /* Nicira extended flow_mod. */
913 const struct nx_flow_mod *nfm;
916 /* Dissect the message. */
917 nfm = ofpbuf_pull(&b, sizeof *nfm);
918 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
923 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
928 /* Translate the message. */
929 fm->cookie = nfm->cookie;
930 fm->command = ntohs(nfm->command);
931 fm->idle_timeout = ntohs(nfm->idle_timeout);
932 fm->hard_timeout = ntohs(nfm->hard_timeout);
933 fm->buffer_id = ntohl(nfm->buffer_id);
934 fm->out_port = ntohs(nfm->out_port);
935 fm->flags = ntohs(nfm->flags);
943 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
944 * 'flow_format' and returns the message. */
946 ofputil_encode_flow_mod(const struct flow_mod *fm,
947 enum nx_flow_format flow_format)
949 size_t actions_len = fm->n_actions * sizeof *fm->actions;
952 if (flow_format == NXFF_OPENFLOW10) {
953 struct ofp_flow_mod *ofm;
955 msg = ofpbuf_new(sizeof *ofm + actions_len);
956 ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
957 ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
958 ofm->cookie = fm->cookie;
959 ofm->command = htons(fm->command);
960 ofm->idle_timeout = htons(fm->idle_timeout);
961 ofm->hard_timeout = htons(fm->hard_timeout);
962 ofm->priority = htons(fm->cr.priority);
963 ofm->buffer_id = htonl(fm->buffer_id);
964 ofm->out_port = htons(fm->out_port);
965 ofm->flags = htons(fm->flags);
966 } else if (flow_format == NXFF_NXM) {
967 struct nx_flow_mod *nfm;
970 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
971 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
972 match_len = nx_put_match(msg, &fm->cr);
975 nfm->cookie = fm->cookie;
976 nfm->command = htons(fm->command);
977 nfm->idle_timeout = htons(fm->idle_timeout);
978 nfm->hard_timeout = htons(fm->hard_timeout);
979 nfm->priority = htons(fm->cr.priority);
980 nfm->buffer_id = htonl(fm->buffer_id);
981 nfm->out_port = htons(fm->out_port);
982 nfm->flags = htons(fm->flags);
983 nfm->match_len = htons(match_len);
988 ofpbuf_put(msg, fm->actions, actions_len);
989 update_openflow_length(msg);
994 ofputil_decode_ofpst_flow_request(struct flow_stats_request *fsr,
995 const struct ofp_header *oh,
998 const struct ofp_flow_stats_request *ofsr = ofputil_stats_body(oh);
1000 fsr->aggregate = aggregate;
1001 ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
1002 fsr->out_port = ntohs(ofsr->out_port);
1003 fsr->table_id = ofsr->table_id;
1009 ofputil_decode_nxst_flow_request(struct flow_stats_request *fsr,
1010 const struct ofp_header *oh,
1013 const struct nx_flow_stats_request *nfsr;
1017 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1019 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1020 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match);
1025 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1028 fsr->aggregate = aggregate;
1029 fsr->out_port = ntohs(nfsr->out_port);
1030 fsr->table_id = nfsr->table_id;
1035 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1036 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1037 * successful, otherwise an OpenFlow error code. */
1039 ofputil_decode_flow_stats_request(struct flow_stats_request *fsr,
1040 const struct ofp_header *oh)
1042 const struct ofputil_msg_type *type;
1046 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1048 ofputil_decode_msg_type(oh, &type);
1049 code = ofputil_msg_type_code(type);
1051 case OFPUTIL_OFPST_FLOW_REQUEST:
1052 return ofputil_decode_ofpst_flow_request(fsr, oh, false);
1054 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1055 return ofputil_decode_ofpst_flow_request(fsr, oh, true);
1057 case OFPUTIL_NXST_FLOW_REQUEST:
1058 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1060 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1061 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1064 /* Hey, the caller lied. */
1069 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1070 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1071 * 'flow_format', and returns the message. */
1073 ofputil_encode_flow_stats_request(const struct flow_stats_request *fsr,
1074 enum nx_flow_format flow_format)
1078 if (flow_format == NXFF_OPENFLOW10) {
1079 struct ofp_flow_stats_request *ofsr;
1082 BUILD_ASSERT_DECL(sizeof(struct ofp_flow_stats_request)
1083 == sizeof(struct ofp_aggregate_stats_request));
1085 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1086 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, &msg);
1087 ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
1088 ofsr->table_id = fsr->table_id;
1089 ofsr->out_port = htons(fsr->out_port);
1090 } else if (flow_format == NXFF_NXM) {
1091 struct nx_flow_stats_request *nfsr;
1095 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1096 ofputil_make_nxstats_request(sizeof *nfsr, subtype, &msg);
1097 match_len = nx_put_match(msg, &fsr->match);
1100 nfsr->out_port = htons(fsr->out_port);
1101 nfsr->match_len = htons(match_len);
1102 nfsr->table_id = fsr->table_id;
1110 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1111 * ofputil_flow_stats in 'fs'.
1113 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1114 * OpenFlow message. Calling this function multiple times for a single 'msg'
1115 * iterates through the replies. The caller must initially leave 'msg''s layer
1116 * pointers null and not modify them between calls.
1118 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1119 * otherwise a positive errno value. */
1121 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1124 const struct ofputil_msg_type *type;
1127 ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1128 code = ofputil_msg_type_code(type);
1130 msg->l2 = msg->data;
1131 if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1132 ofpbuf_pull(msg, sizeof(struct ofp_stats_reply));
1133 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1134 ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1142 } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1143 const struct ofp_flow_stats *ofs;
1146 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1148 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1149 "bytes at end", msg->size);
1153 length = ntohs(ofs->length);
1154 if (length < sizeof *ofs) {
1155 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1156 "length %zu", length);
1160 if (ofputil_pull_actions(msg, length - sizeof *ofs,
1161 &fs->actions, &fs->n_actions)) {
1165 fs->cookie = get_32aligned_be64(&ofs->cookie);
1166 ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
1168 fs->table_id = ofs->table_id;
1169 fs->duration_sec = ntohl(ofs->duration_sec);
1170 fs->duration_nsec = ntohl(ofs->duration_nsec);
1171 fs->idle_timeout = ntohs(ofs->idle_timeout);
1172 fs->hard_timeout = ntohs(ofs->hard_timeout);
1173 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1174 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1175 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1176 const struct nx_flow_stats *nfs;
1177 size_t match_len, length;
1179 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1181 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1182 "bytes at end", msg->size);
1186 length = ntohs(nfs->length);
1187 match_len = ntohs(nfs->match_len);
1188 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1189 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1190 "claims invalid length %zu", match_len, length);
1193 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule)) {
1197 if (ofputil_pull_actions(msg,
1198 length - sizeof *nfs - ROUND_UP(match_len, 8),
1199 &fs->actions, &fs->n_actions)) {
1203 fs->cookie = nfs->cookie;
1204 fs->table_id = nfs->table_id;
1205 fs->duration_sec = ntohl(nfs->duration_sec);
1206 fs->duration_nsec = ntohl(nfs->duration_nsec);
1207 fs->idle_timeout = ntohs(nfs->idle_timeout);
1208 fs->hard_timeout = ntohs(nfs->hard_timeout);
1209 fs->packet_count = ntohll(nfs->packet_count);
1210 fs->byte_count = ntohll(nfs->byte_count);
1218 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1219 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1220 * an OpenFlow error code. */
1222 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1223 const struct ofp_header *oh)
1225 const struct ofputil_msg_type *type;
1226 enum ofputil_msg_code code;
1228 ofputil_decode_msg_type(oh, &type);
1229 code = ofputil_msg_type_code(type);
1230 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1231 const struct ofp_flow_removed *ofr;
1233 ofr = (const struct ofp_flow_removed *) oh;
1234 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
1236 fr->cookie = ofr->cookie;
1237 fr->reason = ofr->reason;
1238 fr->duration_sec = ntohl(ofr->duration_sec);
1239 fr->duration_nsec = ntohl(ofr->duration_nsec);
1240 fr->idle_timeout = ntohs(ofr->idle_timeout);
1241 fr->packet_count = ntohll(ofr->packet_count);
1242 fr->byte_count = ntohll(ofr->byte_count);
1243 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1244 struct nx_flow_removed *nfr;
1248 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1250 nfr = ofpbuf_pull(&b, sizeof *nfr);
1251 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1257 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1260 fr->cookie = nfr->cookie;
1261 fr->reason = nfr->reason;
1262 fr->duration_sec = ntohl(nfr->duration_sec);
1263 fr->duration_nsec = ntohl(nfr->duration_nsec);
1264 fr->idle_timeout = ntohs(nfr->idle_timeout);
1265 fr->packet_count = ntohll(nfr->packet_count);
1266 fr->byte_count = ntohll(nfr->byte_count);
1274 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1275 * NXT_FLOW_REMOVED message 'oh' according to 'flow_format', and returns the
1278 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1279 enum nx_flow_format flow_format)
1283 if (flow_format == NXFF_OPENFLOW10) {
1284 struct ofp_flow_removed *ofr;
1286 ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
1288 ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
1289 ofr->priority = htons(fr->rule.priority);
1290 ofr->reason = fr->reason;
1291 ofr->duration_sec = htonl(fr->duration_sec);
1292 ofr->duration_nsec = htonl(fr->duration_nsec);
1293 ofr->idle_timeout = htons(fr->idle_timeout);
1294 ofr->packet_count = htonll(fr->packet_count);
1295 ofr->byte_count = htonll(fr->byte_count);
1296 } else if (flow_format == NXFF_NXM) {
1297 struct nx_flow_removed *nfr;
1300 make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
1301 match_len = nx_put_match(msg, &fr->rule);
1304 nfr->cookie = fr->cookie;
1305 nfr->priority = htons(fr->rule.priority);
1306 nfr->reason = fr->reason;
1307 nfr->duration_sec = htonl(fr->duration_sec);
1308 nfr->duration_nsec = htonl(fr->duration_nsec);
1309 nfr->idle_timeout = htons(fr->idle_timeout);
1310 nfr->match_len = htons(match_len);
1311 nfr->packet_count = htonll(fr->packet_count);
1312 nfr->byte_count = htonll(fr->byte_count);
1320 /* Converts abstract ofputil_packet_in 'pin' into an OFPT_PACKET_IN message
1321 * and returns the message.
1323 * If 'rw_packet' is NULL, the caller takes ownership of the newly allocated
1326 * If 'rw_packet' is nonnull, then it must contain the same data as
1327 * pin->packet. 'rw_packet' is allowed to be the same ofpbuf as pin->packet.
1328 * It is modified in-place into an OFPT_PACKET_IN message according to 'pin',
1329 * and then ofputil_encode_packet_in() returns 'rw_packet'. If 'rw_packet' has
1330 * enough headroom to insert a "struct ofp_packet_in", this is more efficient
1331 * than ofputil_encode_packet_in() because it does not copy the packet
1334 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
1335 struct ofpbuf *rw_packet)
1337 int total_len = pin->packet->size;
1338 struct ofp_packet_in *opi;
1341 if (pin->send_len < rw_packet->size) {
1342 rw_packet->size = pin->send_len;
1345 rw_packet = ofpbuf_clone_data_with_headroom(
1346 pin->packet->data, MIN(pin->send_len, pin->packet->size),
1347 offsetof(struct ofp_packet_in, data));
1350 /* Add OFPT_PACKET_IN. */
1351 opi = ofpbuf_push_zeros(rw_packet, offsetof(struct ofp_packet_in, data));
1352 opi->header.version = OFP_VERSION;
1353 opi->header.type = OFPT_PACKET_IN;
1354 opi->total_len = htons(total_len);
1355 opi->in_port = htons(pin->in_port);
1356 opi->reason = pin->reason;
1357 opi->buffer_id = htonl(pin->buffer_id);
1358 update_openflow_length(rw_packet);
1363 /* Returns a string representing the message type of 'type'. The string is the
1364 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
1365 * messages, the constant is followed by "request" or "reply",
1366 * e.g. "OFPST_AGGREGATE reply". */
1368 ofputil_msg_type_name(const struct ofputil_msg_type *type)
1373 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1374 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1375 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
1378 * The caller is responsible for freeing '*bufferp' when it is no longer
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 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1389 *bufferp = ofpbuf_new(openflow_len);
1390 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1393 /* Similar to make_openflow() but creates a Nicira vendor extension message
1394 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1396 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1398 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
1401 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1402 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1403 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
1406 * The caller is responsible for freeing '*bufferp' when it is no longer
1409 * The OpenFlow header length is initially set to 'openflow_len'; if the
1410 * message is later extended, the length should be updated with
1411 * update_openflow_length() before sending.
1413 * Returns the header. */
1415 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1416 struct ofpbuf **bufferp)
1418 *bufferp = ofpbuf_new(openflow_len);
1419 return put_openflow_xid(openflow_len, type, xid, *bufferp);
1422 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
1423 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1425 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1426 struct ofpbuf **bufferp)
1428 *bufferp = ofpbuf_new(openflow_len);
1429 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
1432 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1433 * with the given 'type' and an arbitrary transaction id. Allocated bytes
1434 * beyond the header, if any, are zeroed.
1436 * The OpenFlow header length is initially set to 'openflow_len'; if the
1437 * message is later extended, the length should be updated with
1438 * update_openflow_length() before sending.
1440 * Returns the header. */
1442 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1444 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
1447 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1448 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
1449 * the header, if any, are zeroed.
1451 * The OpenFlow header length is initially set to 'openflow_len'; if the
1452 * message is later extended, the length should be updated with
1453 * update_openflow_length() before sending.
1455 * Returns the header. */
1457 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1458 struct ofpbuf *buffer)
1460 struct ofp_header *oh;
1462 assert(openflow_len >= sizeof *oh);
1463 assert(openflow_len <= UINT16_MAX);
1465 oh = ofpbuf_put_uninit(buffer, openflow_len);
1466 oh->version = OFP_VERSION;
1468 oh->length = htons(openflow_len);
1470 memset(oh + 1, 0, openflow_len - sizeof *oh);
1474 /* Similar to put_openflow() but append a Nicira vendor extension message with
1475 * the specific 'subtype'. 'subtype' should be in host byte order. */
1477 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
1479 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
1482 /* Similar to put_openflow_xid() but append a Nicira vendor extension message
1483 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1485 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1486 struct ofpbuf *buffer)
1488 struct nicira_header *nxh;
1490 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
1491 nxh->vendor = htonl(NX_VENDOR_ID);
1492 nxh->subtype = htonl(subtype);
1496 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
1497 * 'buffer->size'. */
1499 update_openflow_length(struct ofpbuf *buffer)
1501 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
1502 oh->length = htons(buffer->size);
1505 /* Creates an ofp_stats_request with the given 'type' and 'body_len' bytes of
1506 * space allocated for the 'body' member. Returns the first byte of the 'body'
1509 ofputil_make_stats_request(size_t body_len, uint16_t type,
1510 struct ofpbuf **bufferp)
1512 struct ofp_stats_request *osr;
1513 osr = make_openflow((offsetof(struct ofp_stats_request, body)
1514 + body_len), OFPT_STATS_REQUEST, bufferp);
1515 osr->type = htons(type);
1516 osr->flags = htons(0);
1520 /* Creates a stats request message with Nicira as vendor and the given
1521 * 'subtype', of total length 'openflow_len'. Returns the message. */
1523 ofputil_make_nxstats_request(size_t openflow_len, uint32_t subtype,
1524 struct ofpbuf **bufferp)
1526 struct nicira_stats_msg *nsm;
1528 nsm = make_openflow(openflow_len, OFPT_STATS_REQUEST, bufferp);
1529 nsm->type = htons(OFPST_VENDOR);
1530 nsm->flags = htons(0);
1531 nsm->vendor = htonl(NX_VENDOR_ID);
1532 nsm->subtype = htonl(subtype);
1536 /* Returns the first byte of the 'body' member of the ofp_stats_request or
1537 * ofp_stats_reply in 'oh'. */
1539 ofputil_stats_body(const struct ofp_header *oh)
1541 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1542 return ((const struct ofp_stats_request *) oh)->body;
1545 /* Returns the length of the 'body' member of the ofp_stats_request or
1546 * ofp_stats_reply in 'oh'. */
1548 ofputil_stats_body_len(const struct ofp_header *oh)
1550 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1551 return ntohs(oh->length) - sizeof(struct ofp_stats_request);
1554 /* Returns the first byte of the body of the nicira_stats_msg in 'oh'. */
1556 ofputil_nxstats_body(const struct ofp_header *oh)
1558 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1559 return ((const struct nicira_stats_msg *) oh) + 1;
1562 /* Returns the length of the body of the nicira_stats_msg in 'oh'. */
1564 ofputil_nxstats_body_len(const struct ofp_header *oh)
1566 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1567 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
1571 make_flow_mod(uint16_t command, const struct cls_rule *rule,
1574 struct ofp_flow_mod *ofm;
1575 size_t size = sizeof *ofm + actions_len;
1576 struct ofpbuf *out = ofpbuf_new(size);
1577 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
1578 ofm->header.version = OFP_VERSION;
1579 ofm->header.type = OFPT_FLOW_MOD;
1580 ofm->header.length = htons(size);
1582 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
1583 ofputil_cls_rule_to_match(rule, &ofm->match);
1584 ofm->command = htons(command);
1589 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
1590 uint16_t idle_timeout, size_t actions_len)
1592 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
1593 struct ofp_flow_mod *ofm = out->data;
1594 ofm->idle_timeout = htons(idle_timeout);
1595 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
1596 ofm->buffer_id = htonl(buffer_id);
1601 make_del_flow(const struct cls_rule *rule)
1603 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
1604 struct ofp_flow_mod *ofm = out->data;
1605 ofm->out_port = htons(OFPP_NONE);
1610 make_add_simple_flow(const struct cls_rule *rule,
1611 uint32_t buffer_id, uint16_t out_port,
1612 uint16_t idle_timeout)
1614 if (out_port != OFPP_NONE) {
1615 struct ofp_action_output *oao;
1616 struct ofpbuf *buffer;
1618 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
1619 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
1620 oao->type = htons(OFPAT_OUTPUT);
1621 oao->len = htons(sizeof *oao);
1622 oao->port = htons(out_port);
1625 return make_add_flow(rule, buffer_id, idle_timeout, 0);
1630 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
1631 const struct ofpbuf *payload, int max_send_len)
1633 struct ofp_packet_in *opi;
1637 send_len = MIN(max_send_len, payload->size);
1638 buf = ofpbuf_new(sizeof *opi + send_len);
1639 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
1640 OFPT_PACKET_IN, 0, buf);
1641 opi->buffer_id = htonl(buffer_id);
1642 opi->total_len = htons(payload->size);
1643 opi->in_port = htons(in_port);
1644 opi->reason = reason;
1645 ofpbuf_put(buf, payload->data, send_len);
1646 update_openflow_length(buf);
1652 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1654 const struct ofp_action_header *actions, size_t n_actions)
1656 size_t actions_len = n_actions * sizeof *actions;
1657 struct ofp_packet_out *opo;
1658 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
1659 struct ofpbuf *out = ofpbuf_new(size);
1661 opo = ofpbuf_put_uninit(out, sizeof *opo);
1662 opo->header.version = OFP_VERSION;
1663 opo->header.type = OFPT_PACKET_OUT;
1664 opo->header.length = htons(size);
1665 opo->header.xid = htonl(0);
1666 opo->buffer_id = htonl(buffer_id);
1667 opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
1668 opo->actions_len = htons(actions_len);
1669 ofpbuf_put(out, actions, actions_len);
1671 ofpbuf_put(out, packet->data, packet->size);
1677 make_unbuffered_packet_out(const struct ofpbuf *packet,
1678 uint16_t in_port, uint16_t out_port)
1680 struct ofp_action_output action;
1681 action.type = htons(OFPAT_OUTPUT);
1682 action.len = htons(sizeof action);
1683 action.port = htons(out_port);
1684 return make_packet_out(packet, UINT32_MAX, in_port,
1685 (struct ofp_action_header *) &action, 1);
1689 make_buffered_packet_out(uint32_t buffer_id,
1690 uint16_t in_port, uint16_t out_port)
1692 if (out_port != OFPP_NONE) {
1693 struct ofp_action_output action;
1694 action.type = htons(OFPAT_OUTPUT);
1695 action.len = htons(sizeof action);
1696 action.port = htons(out_port);
1697 return make_packet_out(NULL, buffer_id, in_port,
1698 (struct ofp_action_header *) &action, 1);
1700 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
1704 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1706 make_echo_request(void)
1708 struct ofp_header *rq;
1709 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1710 rq = ofpbuf_put_uninit(out, sizeof *rq);
1711 rq->version = OFP_VERSION;
1712 rq->type = OFPT_ECHO_REQUEST;
1713 rq->length = htons(sizeof *rq);
1718 /* Creates and returns an OFPT_ECHO_REPLY message matching the
1719 * OFPT_ECHO_REQUEST message in 'rq'. */
1721 make_echo_reply(const struct ofp_header *rq)
1723 size_t size = ntohs(rq->length);
1724 struct ofpbuf *out = ofpbuf_new(size);
1725 struct ofp_header *reply = ofpbuf_put(out, rq, size);
1726 reply->type = OFPT_ECHO_REPLY;
1730 /* Converts the members of 'opp' from host to network byte order. */
1732 hton_ofp_phy_port(struct ofp_phy_port *opp)
1734 opp->port_no = htons(opp->port_no);
1735 opp->config = htonl(opp->config);
1736 opp->state = htonl(opp->state);
1737 opp->curr = htonl(opp->curr);
1738 opp->advertised = htonl(opp->advertised);
1739 opp->supported = htonl(opp->supported);
1740 opp->peer = htonl(opp->peer);
1744 check_action_exact_len(const union ofp_action *a, unsigned int len,
1745 unsigned int required_len)
1747 if (len != required_len) {
1748 VLOG_WARN_RL(&bad_ofmsg_rl, "action %"PRIu16" has invalid length "
1749 "%"PRIu16" (must be %u)\n",
1750 ntohs(a->type), ntohs(a->header.len), required_len);
1751 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1757 check_nx_action_exact_len(const struct nx_action_header *a,
1758 unsigned int len, unsigned int required_len)
1760 if (len != required_len) {
1761 VLOG_WARN_RL(&bad_ofmsg_rl,
1762 "Nicira action %"PRIu16" has invalid length %"PRIu16" "
1764 ntohs(a->subtype), ntohs(a->len), required_len);
1765 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1770 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
1771 * that the switch will never have more than 'max_ports' ports. Returns 0 if
1772 * 'port' is valid, otherwise an ofp_mkerr() return code. */
1774 check_output_port(uint16_t port, int max_ports)
1782 case OFPP_CONTROLLER:
1787 if (port < max_ports) {
1790 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
1791 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1795 /* Checks that 'action' is a valid OFPAT_ENQUEUE action, given that the switch
1796 * will never have more than 'max_ports' ports. Returns 0 if 'port' is valid,
1797 * otherwise an ofp_mkerr() return code. */
1799 check_enqueue_action(const union ofp_action *a, unsigned int len,
1802 const struct ofp_action_enqueue *oae;
1806 error = check_action_exact_len(a, len, 16);
1811 oae = (const struct ofp_action_enqueue *) a;
1812 port = ntohs(oae->port);
1813 if (port < max_ports || port == OFPP_IN_PORT) {
1816 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown enqueue port %x", port);
1817 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1821 check_nicira_action(const union ofp_action *a, unsigned int len,
1822 const struct flow *flow)
1824 const struct nx_action_header *nah;
1829 VLOG_WARN_RL(&bad_ofmsg_rl,
1830 "Nicira vendor action only %u bytes", len);
1831 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1833 nah = (const struct nx_action_header *) a;
1835 subtype = ntohs(nah->subtype);
1836 if (subtype > TYPE_MAXIMUM(enum nx_action_subtype)) {
1837 /* This is necessary because enum nx_action_subtype may be an
1838 * 8-bit type, so the cast below throws away the top 8 bits. */
1839 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1842 switch ((enum nx_action_subtype) subtype) {
1843 case NXAST_RESUBMIT:
1844 case NXAST_SET_TUNNEL:
1845 case NXAST_DROP_SPOOFED_ARP:
1846 case NXAST_SET_QUEUE:
1847 case NXAST_POP_QUEUE:
1848 return check_nx_action_exact_len(nah, len, 16);
1850 case NXAST_REG_MOVE:
1851 error = check_nx_action_exact_len(nah, len,
1852 sizeof(struct nx_action_reg_move));
1856 return nxm_check_reg_move((const struct nx_action_reg_move *) a, flow);
1858 case NXAST_REG_LOAD:
1859 error = check_nx_action_exact_len(nah, len,
1860 sizeof(struct nx_action_reg_load));
1864 return nxm_check_reg_load((const struct nx_action_reg_load *) a, flow);
1869 case NXAST_SET_TUNNEL64:
1870 return check_nx_action_exact_len(
1871 nah, len, sizeof(struct nx_action_set_tunnel64));
1873 case NXAST_MULTIPATH:
1874 error = check_nx_action_exact_len(
1875 nah, len, sizeof(struct nx_action_multipath));
1879 return multipath_check((const struct nx_action_multipath *) a);
1881 case NXAST_AUTOPATH:
1882 error = check_nx_action_exact_len(
1883 nah, len, sizeof(struct nx_action_autopath));
1887 return autopath_check((const struct nx_action_autopath *) a);
1889 case NXAST_SNAT__OBSOLETE:
1891 VLOG_WARN_RL(&bad_ofmsg_rl,
1892 "unknown Nicira vendor action subtype %d", subtype);
1893 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1898 check_action(const union ofp_action *a, unsigned int len,
1899 const struct flow *flow, int max_ports)
1901 enum ofp_action_type type = ntohs(a->type);
1906 error = check_action_exact_len(a, len, 8);
1910 return check_output_port(ntohs(a->output.port), max_ports);
1912 case OFPAT_SET_VLAN_VID:
1913 error = check_action_exact_len(a, len, 8);
1917 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1918 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1922 case OFPAT_SET_VLAN_PCP:
1923 error = check_action_exact_len(a, len, 8);
1927 if (a->vlan_pcp.vlan_pcp & ~7) {
1928 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1932 case OFPAT_STRIP_VLAN:
1933 case OFPAT_SET_NW_SRC:
1934 case OFPAT_SET_NW_DST:
1935 case OFPAT_SET_NW_TOS:
1936 case OFPAT_SET_TP_SRC:
1937 case OFPAT_SET_TP_DST:
1938 return check_action_exact_len(a, len, 8);
1940 case OFPAT_SET_DL_SRC:
1941 case OFPAT_SET_DL_DST:
1942 return check_action_exact_len(a, len, 16);
1945 return (a->vendor.vendor == htonl(NX_VENDOR_ID)
1946 ? check_nicira_action(a, len, flow)
1947 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
1950 return check_enqueue_action(a, len, max_ports);
1953 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %d", (int) type);
1954 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
1959 validate_actions(const union ofp_action *actions, size_t n_actions,
1960 const struct flow *flow, int max_ports)
1964 for (i = 0; i < n_actions; ) {
1965 const union ofp_action *a = &actions[i];
1966 unsigned int len = ntohs(a->header.len);
1967 unsigned int n_slots = len / OFP_ACTION_ALIGN;
1968 unsigned int slots_left = &actions[n_actions] - a;
1971 if (n_slots > slots_left) {
1972 VLOG_WARN_RL(&bad_ofmsg_rl,
1973 "action requires %u slots but only %u remain",
1974 n_slots, slots_left);
1975 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1977 VLOG_WARN_RL(&bad_ofmsg_rl, "action has invalid length 0");
1978 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1979 } else if (len % OFP_ACTION_ALIGN) {
1980 VLOG_WARN_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
1981 "of %d", len, OFP_ACTION_ALIGN);
1982 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1985 error = check_action(a, len, flow, max_ports);
1994 /* Returns true if 'action' outputs to 'port' (which must be in network byte
1995 * order), false otherwise. */
1997 action_outputs_to_port(const union ofp_action *action, uint16_t port)
1999 switch (ntohs(action->type)) {
2001 return action->output.port == port;
2003 return ((const struct ofp_action_enqueue *) action)->port == port;
2009 /* The set of actions must either come from a trusted source or have been
2010 * previously validated with validate_actions(). */
2011 const union ofp_action *
2012 actions_first(struct actions_iterator *iter,
2013 const union ofp_action *oa, size_t n_actions)
2016 iter->end = oa + n_actions;
2017 return actions_next(iter);
2020 const union ofp_action *
2021 actions_next(struct actions_iterator *iter)
2023 if (iter->pos != iter->end) {
2024 const union ofp_action *a = iter->pos;
2025 unsigned int len = ntohs(a->header.len);
2026 iter->pos += len / OFP_ACTION_ALIGN;
2034 normalize_match(struct ofp_match *m)
2036 enum { OFPFW_NW = (OFPFW_NW_SRC_ALL | OFPFW_NW_DST_ALL | OFPFW_NW_PROTO
2038 enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
2041 wc = ntohl(m->wildcards) & OFPFW_ALL;
2042 if (wc & OFPFW_DL_TYPE) {
2043 /* Can't sensibly match on network or transport headers if the
2044 * data link type is unknown. */
2045 wc |= OFPFW_NW | OFPFW_TP;
2046 } else if (m->dl_type == htons(ETH_TYPE_IP)) {
2047 if (wc & OFPFW_NW_PROTO) {
2048 /* Can't sensibly match on transport headers if the network
2049 * protocol is unknown. */
2051 } else if (m->nw_proto != IPPROTO_TCP &&
2052 m->nw_proto != IPPROTO_UDP &&
2053 m->nw_proto != IPPROTO_ICMP) {
2054 /* Transport layer fields will always be extracted as zeros, so we
2055 * can do an exact-match on those values. */
2057 m->tp_src = m->tp_dst = 0;
2059 } else if (m->dl_type != htons(ETH_TYPE_ARP) &&
2060 m->dl_type != htons(ETH_TYPE_IPV6)) {
2061 /* Network and transport layer fields will always be extracted as
2062 * zeros, so we can do an exact-match on those values. */
2063 wc &= ~(OFPFW_NW | OFPFW_TP);
2064 m->nw_proto = m->nw_src = m->nw_dst = m->nw_tos = 0;
2065 m->tp_src = m->tp_dst = 0;
2067 m->wildcards = htonl(wc);
2070 /* Returns a string that describes 'match' in a very literal way, without
2071 * interpreting its contents except in a very basic fashion. The returned
2072 * string is intended to be fixed-length, so that it is easy to see differences
2073 * between two such strings if one is put above another. This is useful for
2074 * describing changes made by normalize_match().
2076 * The caller must free the returned string (with free()). */
2078 ofp_match_to_literal_string(const struct ofp_match *match)
2080 return xasprintf("wildcards=%#10"PRIx32" "
2081 " in_port=%5"PRId16" "
2082 " dl_src="ETH_ADDR_FMT" "
2083 " dl_dst="ETH_ADDR_FMT" "
2084 " dl_vlan=%5"PRId16" "
2085 " dl_vlan_pcp=%3"PRId8" "
2086 " dl_type=%#6"PRIx16" "
2087 " nw_tos=%#4"PRIx8" "
2088 " nw_proto=%#4"PRIx16" "
2089 " nw_src=%#10"PRIx32" "
2090 " nw_dst=%#10"PRIx32" "
2091 " tp_src=%5"PRId16" "
2093 ntohl(match->wildcards),
2094 ntohs(match->in_port),
2095 ETH_ADDR_ARGS(match->dl_src),
2096 ETH_ADDR_ARGS(match->dl_dst),
2097 ntohs(match->dl_vlan),
2099 ntohs(match->dl_type),
2102 ntohl(match->nw_src),
2103 ntohl(match->nw_dst),
2104 ntohs(match->tp_src),
2105 ntohs(match->tp_dst));
2109 vendor_code_to_id(uint8_t code)
2112 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
2114 #undef OFPUTIL_VENDOR
2121 vendor_id_to_code(uint32_t id)
2124 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case VENDOR_ID: return NAME;
2126 #undef OFPUTIL_VENDOR
2132 /* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
2133 * information taken from 'error', whose encoding must be as described in the
2134 * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
2135 * oh->xid as its transaction ID, and it will include up to the first 64 bytes
2138 * Returns NULL if 'error' is not an OpenFlow error code. */
2140 ofputil_encode_error_msg(int error, const struct ofp_header *oh)
2142 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2152 if (!is_ofp_error(error)) {
2153 /* We format 'error' with strerror() here since it seems likely to be
2154 * a system errno value. */
2155 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
2156 error, strerror(error));
2163 len = ntohs(oh->length);
2173 vendor = get_ofp_err_vendor(error);
2174 type = get_ofp_err_type(error);
2175 code = get_ofp_err_code(error);
2176 if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
2177 struct ofp_error_msg *oem;
2179 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
2180 oem->type = htons(type);
2181 oem->code = htons(code);
2183 struct ofp_error_msg *oem;
2184 struct nx_vendor_error *nve;
2187 vendor_id = vendor_code_to_id(vendor);
2188 if (vendor_id == UINT32_MAX) {
2189 VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
2194 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
2195 OFPT_ERROR, xid, &buf);
2196 oem->type = htons(NXET_VENDOR);
2197 oem->code = htons(NXVC_VENDOR_ERROR);
2199 nve = (struct nx_vendor_error *)oem->data;
2200 nve->vendor = htonl(vendor_id);
2201 nve->type = htons(type);
2202 nve->code = htons(code);
2207 ofpbuf_put(buf, data, len);
2213 /* Decodes 'oh', which should be an OpenFlow OFPT_ERROR message, and returns an
2214 * Open vSwitch internal error code in the format described in the large
2215 * comment in ofp-util.h.
2217 * If 'payload_ofs' is nonnull, on success '*payload_ofs' is set to the offset
2218 * to the payload starting from 'oh' and on failure it is set to 0. */
2220 ofputil_decode_error_msg(const struct ofp_header *oh, size_t *payload_ofs)
2222 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2224 const struct ofp_error_msg *oem;
2225 uint16_t type, code;
2232 if (oh->type != OFPT_ERROR) {
2236 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2237 oem = ofpbuf_try_pull(&b, sizeof *oem);
2242 type = ntohs(oem->type);
2243 code = ntohs(oem->code);
2244 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
2245 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
2250 vendor = vendor_id_to_code(ntohl(nve->vendor));
2252 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
2253 ntohl(nve->vendor));
2256 type = ntohs(nve->type);
2257 code = ntohs(nve->code);
2259 vendor = OFPUTIL_VENDOR_OPENFLOW;
2263 VLOG_WARN_RL(&rl, "error contains type %"PRIu16" greater than "
2264 "supported maximum value 1023", type);
2269 *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
2271 return ofp_mkerr_vendor(vendor, type, code);
2275 ofputil_format_error(struct ds *s, int error)
2277 if (is_errno(error)) {
2278 ds_put_cstr(s, strerror(error));
2280 uint16_t type = get_ofp_err_type(error);
2281 uint16_t code = get_ofp_err_code(error);
2282 const char *type_s = ofp_error_type_to_string(type);
2283 const char *code_s = ofp_error_code_to_string(type, code);
2285 ds_put_format(s, "type ");
2287 ds_put_cstr(s, type_s);
2289 ds_put_format(s, "%"PRIu16, type);
2292 ds_put_cstr(s, ", code ");
2294 ds_put_cstr(s, code_s);
2296 ds_put_format(s, "%"PRIu16, code);
2302 ofputil_error_to_string(int error)
2304 struct ds s = DS_EMPTY_INITIALIZER;
2305 ofputil_format_error(&s, error);
2306 return ds_steal_cstr(&s);
2309 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
2310 * successful, otherwise an OpenFlow error.
2312 * If successful, the first action is stored in '*actionsp' and the number of
2313 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
2314 * are stored, respectively.
2316 * This function does not check that the actions are valid (the caller should
2317 * do so, with validate_actions()). The caller is also responsible for making
2318 * sure that 'b->data' is initially aligned appropriately for "union
2321 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2322 union ofp_action **actionsp, size_t *n_actionsp)
2324 if (actions_len % OFP_ACTION_ALIGN != 0) {
2325 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2326 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
2330 *actionsp = ofpbuf_try_pull(b, actions_len);
2331 if (*actionsp == NULL) {
2332 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2333 "exceeds remaining message length (%zu)",
2334 actions_len, b->size);
2338 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
2344 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);