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 ovs_be32 normalize_wildcards(const 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
99 static const flow_wildcards_t WC_INVARIANTS = 0
100 #define WC_INVARIANT_BIT(NAME) | FWW_##NAME
102 #undef WC_INVARIANT_BIT
105 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
108 ofputil_cls_rule_from_match(const struct ofp_match *match,
109 unsigned int priority, struct cls_rule *rule)
111 struct flow_wildcards *wc = &rule->wc;
115 /* Initialize rule->priority. */
116 ofpfw = ntohl(match->wildcards) & OFPFW_ALL;
117 rule->priority = !ofpfw ? UINT16_MAX : priority;
119 /* Initialize most of rule->wc. */
120 flow_wildcards_init_catchall(wc);
121 wc->wildcards = (OVS_FORCE flow_wildcards_t) ofpfw & WC_INVARIANTS;
123 /* Wildcard fields that aren't defined by ofp_match or tun_id. */
124 wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_ND_TARGET);
126 if (ofpfw & OFPFW_NW_TOS) {
127 wc->wildcards |= FWW_NW_TOS;
129 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
130 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
132 if (ofpfw & OFPFW_DL_DST) {
133 /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
134 * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST
135 * and FWW_ETH_MCAST. */
136 wc->wildcards |= FWW_ETH_MCAST;
139 /* Initialize most of rule->flow. */
140 rule->flow.nw_src = match->nw_src;
141 rule->flow.nw_dst = match->nw_dst;
142 rule->flow.in_port = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
143 : ntohs(match->in_port));
144 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
145 rule->flow.tp_src = match->tp_src;
146 rule->flow.tp_dst = match->tp_dst;
147 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
148 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
149 rule->flow.nw_tos = match->nw_tos;
150 rule->flow.nw_proto = match->nw_proto;
152 /* Translate VLANs. */
153 vid = match->dl_vlan & htons(VLAN_VID_MASK);
154 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
155 switch (ofpfw & (OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP)) {
156 case OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP:
157 /* Wildcard everything. */
158 rule->flow.vlan_tci = htons(0);
159 rule->wc.vlan_tci_mask = htons(0);
162 case OFPFW_DL_VLAN_PCP:
163 if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
164 /* Match only packets without 802.1Q header. */
165 rule->flow.vlan_tci = htons(0);
166 rule->wc.vlan_tci_mask = htons(0xffff);
168 /* Wildcard PCP, specific VID. */
169 rule->flow.vlan_tci = vid | htons(VLAN_CFI);
170 rule->wc.vlan_tci_mask = htons(VLAN_VID_MASK | VLAN_CFI);
175 /* Wildcard VID, specific PCP. */
176 rule->flow.vlan_tci = pcp | htons(VLAN_CFI);
177 rule->wc.vlan_tci_mask = htons(VLAN_PCP_MASK | VLAN_CFI);
181 if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
182 /* This case is odd, since we can't have a specific PCP without an
183 * 802.1Q header. However, older versions of OVS treated this as
184 * matching packets withut an 802.1Q header, so we do here too. */
185 rule->flow.vlan_tci = htons(0);
186 rule->wc.vlan_tci_mask = htons(0xffff);
188 /* Specific VID and PCP. */
189 rule->flow.vlan_tci = vid | pcp | htons(VLAN_CFI);
190 rule->wc.vlan_tci_mask = htons(0xffff);
196 cls_rule_zero_wildcarded_fields(rule);
199 /* Convert 'rule' into the OpenFlow match structure 'match'. */
201 ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match)
203 const struct flow_wildcards *wc = &rule->wc;
206 /* Figure out most OpenFlow wildcards. */
207 ofpfw = (OVS_FORCE uint32_t) (wc->wildcards & WC_INVARIANTS);
208 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
209 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
210 if (wc->wildcards & FWW_NW_TOS) {
211 ofpfw |= OFPFW_NW_TOS;
214 /* Translate VLANs. */
215 match->dl_vlan = htons(0);
216 match->dl_vlan_pcp = 0;
217 if (rule->wc.vlan_tci_mask == htons(0)) {
218 ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
219 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
220 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
221 match->dl_vlan = htons(OFP_VLAN_NONE);
223 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
224 ofpfw |= OFPFW_DL_VLAN;
226 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
229 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
230 ofpfw |= OFPFW_DL_VLAN_PCP;
232 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
236 /* Compose most of the match structure. */
237 match->wildcards = htonl(ofpfw);
238 match->in_port = htons(rule->flow.in_port == ODPP_LOCAL ? OFPP_LOCAL
239 : rule->flow.in_port);
240 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
241 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
242 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
243 match->nw_src = rule->flow.nw_src;
244 match->nw_dst = rule->flow.nw_dst;
245 match->nw_tos = rule->flow.nw_tos;
246 match->nw_proto = rule->flow.nw_proto;
247 match->tp_src = rule->flow.tp_src;
248 match->tp_dst = rule->flow.tp_dst;
249 memset(match->pad1, '\0', sizeof match->pad1);
250 memset(match->pad2, '\0', sizeof match->pad2);
253 /* Given a 'dl_type' value in the format used in struct flow, returns the
254 * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */
256 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
258 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
259 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
263 /* Given a 'dl_type' value in the format used in an OpenFlow ofp_match
264 * structure, returns the corresponding 'dl_type' value for use in struct
267 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
269 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
270 ? htons(FLOW_DL_TYPE_NONE)
274 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
278 static uint32_t next_xid = 1;
279 return htonl(next_xid++);
282 /* Basic parsing of OpenFlow messages. */
284 struct ofputil_msg_type {
285 enum ofputil_msg_code code; /* OFPUTIL_*. */
286 uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
287 const char *name; /* e.g. "OFPT_FLOW_REMOVED". */
288 unsigned int min_size; /* Minimum total message size in bytes. */
289 /* 0 if 'min_size' is the exact size that the message must be. Otherwise,
290 * the message may exceed 'min_size' by an even multiple of this value. */
291 unsigned int extra_multiple;
294 struct ofputil_msg_category {
295 const char *name; /* e.g. "OpenFlow message" */
296 const struct ofputil_msg_type *types;
298 int missing_error; /* ofp_mkerr() value for missing type. */
302 ofputil_length_ok(const struct ofputil_msg_category *cat,
303 const struct ofputil_msg_type *type,
306 switch (type->extra_multiple) {
308 if (size != type->min_size) {
309 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
310 "length %u (expected length %u)",
311 cat->name, type->name, size, type->min_size);
317 if (size < type->min_size) {
318 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
319 "length %u (expected length at least %u bytes)",
320 cat->name, type->name, size, type->min_size);
326 if (size < type->min_size
327 || (size - type->min_size) % type->extra_multiple) {
328 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
329 "length %u (must be exactly %u bytes or longer "
330 "by an integer multiple of %u bytes)",
331 cat->name, type->name, size,
332 type->min_size, type->extra_multiple);
340 ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
341 uint32_t value, unsigned int size,
342 const struct ofputil_msg_type **typep)
344 const struct ofputil_msg_type *type;
346 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
347 if (type->value == value) {
348 if (!ofputil_length_ok(cat, type, size)) {
349 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
356 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
358 return cat->missing_error;
362 ofputil_decode_vendor(const struct ofp_header *oh,
363 const struct ofputil_msg_type **typep)
365 static const struct ofputil_msg_type nxt_messages[] = {
366 { OFPUTIL_NXT_ROLE_REQUEST,
367 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
368 sizeof(struct nx_role_request), 0 },
370 { OFPUTIL_NXT_ROLE_REPLY,
371 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
372 sizeof(struct nx_role_request), 0 },
374 { OFPUTIL_NXT_SET_FLOW_FORMAT,
375 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
376 sizeof(struct nxt_set_flow_format), 0 },
378 { OFPUTIL_NXT_FLOW_MOD,
379 NXT_FLOW_MOD, "NXT_FLOW_MOD",
380 sizeof(struct nx_flow_mod), 8 },
382 { OFPUTIL_NXT_FLOW_REMOVED,
383 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
384 sizeof(struct nx_flow_removed), 8 },
387 static const struct ofputil_msg_category nxt_category = {
388 "Nicira extension message",
389 nxt_messages, ARRAY_SIZE(nxt_messages),
390 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
393 const struct ofp_vendor_header *ovh;
394 const struct nicira_header *nh;
396 ovh = (const struct ofp_vendor_header *) oh;
397 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
398 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
399 "vendor %"PRIx32, ntohl(ovh->vendor));
400 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
403 if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
404 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
405 "length %u (expected at least %zu)",
406 ntohs(ovh->header.length), sizeof(struct nicira_header));
407 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
410 nh = (const struct nicira_header *) oh;
411 return ofputil_lookup_openflow_message(&nxt_category, ntohl(nh->subtype),
412 ntohs(oh->length), typep);
416 check_nxstats_msg(const struct ofp_header *oh)
418 const struct ofp_stats_request *osr;
421 osr = (const struct ofp_stats_request *) oh;
423 memcpy(&vendor, osr->body, sizeof vendor);
424 if (vendor != htonl(NX_VENDOR_ID)) {
425 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
426 "unknown vendor %"PRIx32, ntohl(vendor));
427 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
430 if (ntohs(osr->header.length) < sizeof(struct nicira_stats_msg)) {
431 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
432 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
439 ofputil_decode_nxst_request(const struct ofp_header *oh,
440 const struct ofputil_msg_type **typep)
442 static const struct ofputil_msg_type nxst_requests[] = {
443 { OFPUTIL_NXST_FLOW_REQUEST,
444 NXST_FLOW, "NXST_FLOW request",
445 sizeof(struct nx_flow_stats_request), 8 },
447 { OFPUTIL_NXST_AGGREGATE_REQUEST,
448 NXST_AGGREGATE, "NXST_AGGREGATE request",
449 sizeof(struct nx_aggregate_stats_request), 8 },
452 static const struct ofputil_msg_category nxst_request_category = {
453 "Nicira extension statistics request",
454 nxst_requests, ARRAY_SIZE(nxst_requests),
455 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
458 const struct nicira_stats_msg *nsm;
461 error = check_nxstats_msg(oh);
466 nsm = (struct nicira_stats_msg *) oh;
467 return ofputil_lookup_openflow_message(&nxst_request_category,
469 ntohs(oh->length), typep);
473 ofputil_decode_nxst_reply(const struct ofp_header *oh,
474 const struct ofputil_msg_type **typep)
476 static const struct ofputil_msg_type nxst_replies[] = {
477 { OFPUTIL_NXST_FLOW_REPLY,
478 NXST_FLOW, "NXST_FLOW reply",
479 sizeof(struct nicira_stats_msg), 8 },
481 { OFPUTIL_NXST_AGGREGATE_REPLY,
482 NXST_AGGREGATE, "NXST_AGGREGATE reply",
483 sizeof(struct nx_aggregate_stats_reply), 0 },
486 static const struct ofputil_msg_category nxst_reply_category = {
487 "Nicira extension statistics reply",
488 nxst_replies, ARRAY_SIZE(nxst_replies),
489 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
492 const struct nicira_stats_msg *nsm;
495 error = check_nxstats_msg(oh);
500 nsm = (struct nicira_stats_msg *) oh;
501 return ofputil_lookup_openflow_message(&nxst_reply_category,
503 ntohs(oh->length), typep);
507 ofputil_decode_ofpst_request(const struct ofp_header *oh,
508 const struct ofputil_msg_type **typep)
510 enum { OSR_SIZE = sizeof(struct ofp_stats_request) };
511 static const struct ofputil_msg_type ofpst_requests[] = {
512 { OFPUTIL_OFPST_DESC_REQUEST,
513 OFPST_DESC, "OFPST_DESC request",
516 { OFPUTIL_OFPST_FLOW_REQUEST,
517 OFPST_FLOW, "OFPST_FLOW request",
518 OSR_SIZE + sizeof(struct ofp_flow_stats_request), 0 },
520 { OFPUTIL_OFPST_AGGREGATE_REQUEST,
521 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
522 OSR_SIZE + sizeof(struct ofp_aggregate_stats_request), 0 },
524 { OFPUTIL_OFPST_TABLE_REQUEST,
525 OFPST_TABLE, "OFPST_TABLE request",
528 { OFPUTIL_OFPST_PORT_REQUEST,
529 OFPST_PORT, "OFPST_PORT request",
530 OSR_SIZE + sizeof(struct ofp_port_stats_request), 0 },
532 { OFPUTIL_OFPST_QUEUE_REQUEST,
533 OFPST_QUEUE, "OFPST_QUEUE request",
534 OSR_SIZE + sizeof(struct ofp_queue_stats_request), 0 },
537 OFPST_VENDOR, "OFPST_VENDOR request",
538 OSR_SIZE + sizeof(uint32_t), 1 },
541 static const struct ofputil_msg_category ofpst_request_category = {
542 "OpenFlow statistics",
543 ofpst_requests, ARRAY_SIZE(ofpst_requests),
544 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
547 const struct ofp_stats_request *osr;
550 osr = (const struct ofp_stats_request *) oh;
551 error = ofputil_lookup_openflow_message(&ofpst_request_category,
553 ntohs(oh->length), typep);
554 if (!error && osr->type == htons(OFPST_VENDOR)) {
555 error = ofputil_decode_nxst_request(oh, typep);
561 ofputil_decode_ofpst_reply(const struct ofp_header *oh,
562 const struct ofputil_msg_type **typep)
564 enum { OSR_SIZE = sizeof(struct ofp_stats_reply) };
565 static const struct ofputil_msg_type ofpst_replies[] = {
566 { OFPUTIL_OFPST_DESC_REPLY,
567 OFPST_DESC, "OFPST_DESC reply",
568 OSR_SIZE + sizeof(struct ofp_desc_stats), 0 },
570 { OFPUTIL_OFPST_FLOW_REPLY,
571 OFPST_FLOW, "OFPST_FLOW reply",
574 { OFPUTIL_OFPST_AGGREGATE_REPLY,
575 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
576 OSR_SIZE + sizeof(struct ofp_aggregate_stats_reply), 0 },
578 { OFPUTIL_OFPST_TABLE_REPLY,
579 OFPST_TABLE, "OFPST_TABLE reply",
580 OSR_SIZE, sizeof(struct ofp_table_stats) },
582 { OFPUTIL_OFPST_PORT_REPLY,
583 OFPST_PORT, "OFPST_PORT reply",
584 OSR_SIZE, sizeof(struct ofp_port_stats) },
586 { OFPUTIL_OFPST_QUEUE_REPLY,
587 OFPST_QUEUE, "OFPST_QUEUE reply",
588 OSR_SIZE, sizeof(struct ofp_queue_stats) },
591 OFPST_VENDOR, "OFPST_VENDOR reply",
592 OSR_SIZE + sizeof(uint32_t), 1 },
595 static const struct ofputil_msg_category ofpst_reply_category = {
596 "OpenFlow statistics",
597 ofpst_replies, ARRAY_SIZE(ofpst_replies),
598 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
601 const struct ofp_stats_reply *osr = (const struct ofp_stats_reply *) oh;
604 error = ofputil_lookup_openflow_message(&ofpst_reply_category,
606 ntohs(oh->length), typep);
607 if (!error && osr->type == htons(OFPST_VENDOR)) {
608 error = ofputil_decode_nxst_reply(oh, typep);
613 /* Decodes the message type represented by 'oh'. Returns 0 if successful or
614 * an OpenFlow error code constructed with ofp_mkerr() on failure. Either
615 * way, stores in '*typep' a type structure that can be inspected with the
616 * ofputil_msg_type_*() functions.
618 * oh->length must indicate the correct length of the message (and must be at
619 * least sizeof(struct ofp_header)).
621 * Success indicates that 'oh' is at least as long as the minimum-length
622 * message of its type. */
624 ofputil_decode_msg_type(const struct ofp_header *oh,
625 const struct ofputil_msg_type **typep)
627 static const struct ofputil_msg_type ofpt_messages[] = {
628 { OFPUTIL_OFPT_HELLO,
629 OFPT_HELLO, "OFPT_HELLO",
630 sizeof(struct ofp_hello), 1 },
632 { OFPUTIL_OFPT_ERROR,
633 OFPT_ERROR, "OFPT_ERROR",
634 sizeof(struct ofp_error_msg), 1 },
636 { OFPUTIL_OFPT_ECHO_REQUEST,
637 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
638 sizeof(struct ofp_header), 1 },
640 { OFPUTIL_OFPT_ECHO_REPLY,
641 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
642 sizeof(struct ofp_header), 1 },
644 { OFPUTIL_OFPT_FEATURES_REQUEST,
645 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
646 sizeof(struct ofp_header), 0 },
648 { OFPUTIL_OFPT_FEATURES_REPLY,
649 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
650 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
652 { OFPUTIL_OFPT_GET_CONFIG_REQUEST,
653 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
654 sizeof(struct ofp_header), 0 },
656 { OFPUTIL_OFPT_GET_CONFIG_REPLY,
657 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
658 sizeof(struct ofp_switch_config), 0 },
660 { OFPUTIL_OFPT_SET_CONFIG,
661 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
662 sizeof(struct ofp_switch_config), 0 },
664 { OFPUTIL_OFPT_PACKET_IN,
665 OFPT_PACKET_IN, "OFPT_PACKET_IN",
666 offsetof(struct ofp_packet_in, data), 1 },
668 { OFPUTIL_OFPT_FLOW_REMOVED,
669 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
670 sizeof(struct ofp_flow_removed), 0 },
672 { OFPUTIL_OFPT_PORT_STATUS,
673 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
674 sizeof(struct ofp_port_status), 0 },
676 { OFPUTIL_OFPT_PACKET_OUT,
677 OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
678 sizeof(struct ofp_packet_out), 1 },
680 { OFPUTIL_OFPT_FLOW_MOD,
681 OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
682 sizeof(struct ofp_flow_mod), 1 },
684 { OFPUTIL_OFPT_PORT_MOD,
685 OFPT_PORT_MOD, "OFPT_PORT_MOD",
686 sizeof(struct ofp_port_mod), 0 },
689 OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
690 sizeof(struct ofp_stats_request), 1 },
693 OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
694 sizeof(struct ofp_stats_reply), 1 },
696 { OFPUTIL_OFPT_BARRIER_REQUEST,
697 OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
698 sizeof(struct ofp_header), 0 },
700 { OFPUTIL_OFPT_BARRIER_REPLY,
701 OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
702 sizeof(struct ofp_header), 0 },
705 OFPT_VENDOR, "OFPT_VENDOR",
706 sizeof(struct ofp_vendor_header), 1 },
709 static const struct ofputil_msg_category ofpt_category = {
711 ofpt_messages, ARRAY_SIZE(ofpt_messages),
712 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE)
717 error = ofputil_lookup_openflow_message(&ofpt_category, oh->type,
718 ntohs(oh->length), typep);
722 error = ofputil_decode_vendor(oh, typep);
725 case OFPT_STATS_REQUEST:
726 error = ofputil_decode_ofpst_request(oh, typep);
729 case OFPT_STATS_REPLY:
730 error = ofputil_decode_ofpst_reply(oh, typep);
737 static const struct ofputil_msg_type ofputil_invalid_type = {
739 0, "OFPUTIL_INVALID",
743 *typep = &ofputil_invalid_type;
748 /* Returns an OFPUTIL_* message type code for 'type'. */
749 enum ofputil_msg_code
750 ofputil_msg_type_code(const struct ofputil_msg_type *type)
758 ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
760 switch (flow_format) {
761 case NXFF_OPENFLOW10:
770 ofputil_flow_format_to_string(enum nx_flow_format flow_format)
772 switch (flow_format) {
773 case NXFF_OPENFLOW10:
783 ofputil_flow_format_from_string(const char *s)
785 return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
786 : !strcmp(s, "nxm") ? NXFF_NXM
791 regs_fully_wildcarded(const struct flow_wildcards *wc)
795 for (i = 0; i < FLOW_N_REGS; i++) {
796 if (wc->reg_masks[i] != 0) {
803 /* Returns the minimum nx_flow_format to use for sending 'rule' to a switch
804 * (e.g. to add or remove a flow). Only NXM can handle tunnel IDs, registers,
805 * or fixing the Ethernet multicast bit. Otherwise, it's better to use
806 * NXFF_OPENFLOW10 for backward compatibility. */
808 ofputil_min_flow_format(const struct cls_rule *rule)
810 const struct flow_wildcards *wc = &rule->wc;
812 /* Only NXM supports separately wildcards the Ethernet multicast bit. */
813 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
817 /* Only NXM supports matching ARP hardware addresses. */
818 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
822 /* Only NXM supports matching IPv6 traffic. */
823 if (!(wc->wildcards & FWW_DL_TYPE)
824 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
828 /* Only NXM supports matching registers. */
829 if (!regs_fully_wildcarded(wc)) {
833 /* Only NXM supports matching tun_id. */
834 if (wc->tun_id_mask != htonll(0)) {
838 /* Other formats can express this rule. */
839 return NXFF_OPENFLOW10;
842 /* Returns an OpenFlow message that can be used to set the flow format to
845 ofputil_make_set_flow_format(enum nx_flow_format flow_format)
847 struct nxt_set_flow_format *sff;
850 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
851 sff->format = htonl(flow_format);
856 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
857 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
860 * Does not validate the flow_mod actions. */
862 ofputil_decode_flow_mod(struct flow_mod *fm, const struct ofp_header *oh)
864 const struct ofputil_msg_type *type;
867 ofpbuf_use_const(&b, oh, ntohs(oh->length));
869 ofputil_decode_msg_type(oh, &type);
870 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
871 /* Standard OpenFlow flow_mod. */
872 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 /* Set priority based on original wildcards. Normally we'd allow
885 * ofputil_cls_rule_from_match() to do this for us, but
886 * normalize_wildcards() can put wildcards where the original flow
887 * didn't have them. */
888 priority = ntohs(ofm->priority);
889 if (!(ofm->match.wildcards & htonl(OFPFW_ALL))) {
890 priority = UINT16_MAX;
893 /* Normalize ofm->match. If normalization actually changes anything,
894 * then log the differences. */
895 wc = normalize_wildcards(&ofm->match);
896 if (wc == ofm->match.wildcards) {
897 ofputil_cls_rule_from_match(&ofm->match, priority, &fm->cr);
899 struct ofp_match match = ofm->match;
900 match.wildcards = wc;
901 ofputil_cls_rule_from_match(&match, priority, &fm->cr);
903 if (!VLOG_DROP_INFO(&bad_ofmsg_rl)) {
904 char *pre = ofp_match_to_string(&ofm->match, 1);
905 char *post = ofp_match_to_string(&match, 1);
906 VLOG_INFO("normalization changed ofp_match, details:");
907 VLOG_INFO(" pre: %s", pre);
908 VLOG_INFO("post: %s", post);
914 /* Translate the message. */
915 fm->cookie = ofm->cookie;
916 fm->command = ntohs(ofm->command);
917 fm->idle_timeout = ntohs(ofm->idle_timeout);
918 fm->hard_timeout = ntohs(ofm->hard_timeout);
919 fm->buffer_id = ntohl(ofm->buffer_id);
920 fm->out_port = ntohs(ofm->out_port);
921 fm->flags = ntohs(ofm->flags);
922 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
923 /* Nicira extended flow_mod. */
924 const struct nx_flow_mod *nfm;
927 /* Dissect the message. */
928 nfm = ofpbuf_pull(&b, sizeof *nfm);
929 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
934 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
939 /* Translate the message. */
940 fm->cookie = nfm->cookie;
941 fm->command = ntohs(nfm->command);
942 fm->idle_timeout = ntohs(nfm->idle_timeout);
943 fm->hard_timeout = ntohs(nfm->hard_timeout);
944 fm->buffer_id = ntohl(nfm->buffer_id);
945 fm->out_port = ntohs(nfm->out_port);
946 fm->flags = ntohs(nfm->flags);
954 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
955 * 'flow_format' and returns the message. */
957 ofputil_encode_flow_mod(const struct flow_mod *fm,
958 enum nx_flow_format flow_format)
960 size_t actions_len = fm->n_actions * sizeof *fm->actions;
963 if (flow_format == NXFF_OPENFLOW10) {
964 struct ofp_flow_mod *ofm;
966 msg = ofpbuf_new(sizeof *ofm + actions_len);
967 ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
968 ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
969 ofm->cookie = fm->cookie;
970 ofm->command = htons(fm->command);
971 ofm->idle_timeout = htons(fm->idle_timeout);
972 ofm->hard_timeout = htons(fm->hard_timeout);
973 ofm->priority = htons(fm->cr.priority);
974 ofm->buffer_id = htonl(fm->buffer_id);
975 ofm->out_port = htons(fm->out_port);
976 ofm->flags = htons(fm->flags);
977 } else if (flow_format == NXFF_NXM) {
978 struct nx_flow_mod *nfm;
981 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
982 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
983 match_len = nx_put_match(msg, &fm->cr);
986 nfm->cookie = fm->cookie;
987 nfm->command = htons(fm->command);
988 nfm->idle_timeout = htons(fm->idle_timeout);
989 nfm->hard_timeout = htons(fm->hard_timeout);
990 nfm->priority = htons(fm->cr.priority);
991 nfm->buffer_id = htonl(fm->buffer_id);
992 nfm->out_port = htons(fm->out_port);
993 nfm->flags = htons(fm->flags);
994 nfm->match_len = htons(match_len);
999 ofpbuf_put(msg, fm->actions, actions_len);
1000 update_openflow_length(msg);
1005 ofputil_decode_ofpst_flow_request(struct flow_stats_request *fsr,
1006 const struct ofp_header *oh,
1009 const struct ofp_flow_stats_request *ofsr = ofputil_stats_body(oh);
1011 fsr->aggregate = aggregate;
1012 ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
1013 fsr->out_port = ntohs(ofsr->out_port);
1014 fsr->table_id = ofsr->table_id;
1020 ofputil_decode_nxst_flow_request(struct flow_stats_request *fsr,
1021 const struct ofp_header *oh,
1024 const struct nx_flow_stats_request *nfsr;
1028 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1030 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1031 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match);
1036 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1039 fsr->aggregate = aggregate;
1040 fsr->out_port = ntohs(nfsr->out_port);
1041 fsr->table_id = nfsr->table_id;
1046 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1047 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1048 * successful, otherwise an OpenFlow error code. */
1050 ofputil_decode_flow_stats_request(struct flow_stats_request *fsr,
1051 const struct ofp_header *oh)
1053 const struct ofputil_msg_type *type;
1057 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1059 ofputil_decode_msg_type(oh, &type);
1060 code = ofputil_msg_type_code(type);
1062 case OFPUTIL_OFPST_FLOW_REQUEST:
1063 return ofputil_decode_ofpst_flow_request(fsr, oh, false);
1065 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1066 return ofputil_decode_ofpst_flow_request(fsr, oh, true);
1068 case OFPUTIL_NXST_FLOW_REQUEST:
1069 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1071 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1072 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1075 /* Hey, the caller lied. */
1080 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1081 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1082 * 'flow_format', and returns the message. */
1084 ofputil_encode_flow_stats_request(const struct flow_stats_request *fsr,
1085 enum nx_flow_format flow_format)
1089 if (flow_format == NXFF_OPENFLOW10) {
1090 struct ofp_flow_stats_request *ofsr;
1093 BUILD_ASSERT_DECL(sizeof(struct ofp_flow_stats_request)
1094 == sizeof(struct ofp_aggregate_stats_request));
1096 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1097 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, &msg);
1098 ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
1099 ofsr->table_id = fsr->table_id;
1100 ofsr->out_port = htons(fsr->out_port);
1101 } else if (flow_format == NXFF_NXM) {
1102 struct nx_flow_stats_request *nfsr;
1106 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1107 ofputil_make_nxstats_request(sizeof *nfsr, subtype, &msg);
1108 match_len = nx_put_match(msg, &fsr->match);
1111 nfsr->out_port = htons(fsr->out_port);
1112 nfsr->match_len = htons(match_len);
1113 nfsr->table_id = fsr->table_id;
1121 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1122 * ofputil_flow_stats in 'fs'.
1124 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1125 * OpenFlow message. Calling this function multiple times for a single 'msg'
1126 * iterates through the replies. The caller must initially leave 'msg''s layer
1127 * pointers null and not modify them between calls.
1129 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1130 * otherwise a positive errno value. */
1132 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1135 const struct ofputil_msg_type *type;
1138 ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1139 code = ofputil_msg_type_code(type);
1141 msg->l2 = msg->data;
1142 if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1143 ofpbuf_pull(msg, sizeof(struct ofp_stats_reply));
1144 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1145 ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1153 } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1154 const struct ofp_flow_stats *ofs;
1157 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1159 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1160 "bytes at end", msg->size);
1164 length = ntohs(ofs->length);
1165 if (length < sizeof *ofs) {
1166 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1167 "length %zu", length);
1171 if (ofputil_pull_actions(msg, length - sizeof *ofs,
1172 &fs->actions, &fs->n_actions)) {
1176 fs->cookie = get_32aligned_be64(&ofs->cookie);
1177 ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
1179 fs->table_id = ofs->table_id;
1180 fs->duration_sec = ntohl(ofs->duration_sec);
1181 fs->duration_nsec = ntohl(ofs->duration_nsec);
1182 fs->idle_timeout = ntohs(ofs->idle_timeout);
1183 fs->hard_timeout = ntohs(ofs->hard_timeout);
1184 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1185 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1186 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1187 const struct nx_flow_stats *nfs;
1188 size_t match_len, length;
1190 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1192 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1193 "bytes at end", msg->size);
1197 length = ntohs(nfs->length);
1198 match_len = ntohs(nfs->match_len);
1199 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1200 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1201 "claims invalid length %zu", match_len, length);
1204 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule)) {
1208 if (ofputil_pull_actions(msg,
1209 length - sizeof *nfs - ROUND_UP(match_len, 8),
1210 &fs->actions, &fs->n_actions)) {
1214 fs->cookie = nfs->cookie;
1215 fs->table_id = nfs->table_id;
1216 fs->duration_sec = ntohl(nfs->duration_sec);
1217 fs->duration_nsec = ntohl(nfs->duration_nsec);
1218 fs->idle_timeout = ntohs(nfs->idle_timeout);
1219 fs->hard_timeout = ntohs(nfs->hard_timeout);
1220 fs->packet_count = ntohll(nfs->packet_count);
1221 fs->byte_count = ntohll(nfs->byte_count);
1229 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1230 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1231 * an OpenFlow error code. */
1233 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1234 const struct ofp_header *oh)
1236 const struct ofputil_msg_type *type;
1237 enum ofputil_msg_code code;
1239 ofputil_decode_msg_type(oh, &type);
1240 code = ofputil_msg_type_code(type);
1241 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1242 const struct ofp_flow_removed *ofr;
1244 ofr = (const struct ofp_flow_removed *) oh;
1245 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
1247 fr->cookie = ofr->cookie;
1248 fr->reason = ofr->reason;
1249 fr->duration_sec = ntohl(ofr->duration_sec);
1250 fr->duration_nsec = ntohl(ofr->duration_nsec);
1251 fr->idle_timeout = ntohs(ofr->idle_timeout);
1252 fr->packet_count = ntohll(ofr->packet_count);
1253 fr->byte_count = ntohll(ofr->byte_count);
1254 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1255 struct nx_flow_removed *nfr;
1259 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1261 nfr = ofpbuf_pull(&b, sizeof *nfr);
1262 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1268 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1271 fr->cookie = nfr->cookie;
1272 fr->reason = nfr->reason;
1273 fr->duration_sec = ntohl(nfr->duration_sec);
1274 fr->duration_nsec = ntohl(nfr->duration_nsec);
1275 fr->idle_timeout = ntohs(nfr->idle_timeout);
1276 fr->packet_count = ntohll(nfr->packet_count);
1277 fr->byte_count = ntohll(nfr->byte_count);
1285 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1286 * NXT_FLOW_REMOVED message 'oh' according to 'flow_format', and returns the
1289 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1290 enum nx_flow_format flow_format)
1294 if (flow_format == NXFF_OPENFLOW10) {
1295 struct ofp_flow_removed *ofr;
1297 ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
1299 ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
1300 ofr->priority = htons(fr->rule.priority);
1301 ofr->reason = fr->reason;
1302 ofr->duration_sec = htonl(fr->duration_sec);
1303 ofr->duration_nsec = htonl(fr->duration_nsec);
1304 ofr->idle_timeout = htons(fr->idle_timeout);
1305 ofr->packet_count = htonll(fr->packet_count);
1306 ofr->byte_count = htonll(fr->byte_count);
1307 } else if (flow_format == NXFF_NXM) {
1308 struct nx_flow_removed *nfr;
1311 make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
1312 match_len = nx_put_match(msg, &fr->rule);
1315 nfr->cookie = fr->cookie;
1316 nfr->priority = htons(fr->rule.priority);
1317 nfr->reason = fr->reason;
1318 nfr->duration_sec = htonl(fr->duration_sec);
1319 nfr->duration_nsec = htonl(fr->duration_nsec);
1320 nfr->idle_timeout = htons(fr->idle_timeout);
1321 nfr->match_len = htons(match_len);
1322 nfr->packet_count = htonll(fr->packet_count);
1323 nfr->byte_count = htonll(fr->byte_count);
1331 /* Converts abstract ofputil_packet_in 'pin' into an OFPT_PACKET_IN message
1332 * and returns the message.
1334 * If 'rw_packet' is NULL, the caller takes ownership of the newly allocated
1337 * If 'rw_packet' is nonnull, then it must contain the same data as
1338 * pin->packet. 'rw_packet' is allowed to be the same ofpbuf as pin->packet.
1339 * It is modified in-place into an OFPT_PACKET_IN message according to 'pin',
1340 * and then ofputil_encode_packet_in() returns 'rw_packet'. If 'rw_packet' has
1341 * enough headroom to insert a "struct ofp_packet_in", this is more efficient
1342 * than ofputil_encode_packet_in() because it does not copy the packet
1345 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
1346 struct ofpbuf *rw_packet)
1348 int total_len = pin->packet->size;
1349 struct ofp_packet_in *opi;
1352 if (pin->send_len < rw_packet->size) {
1353 rw_packet->size = pin->send_len;
1356 rw_packet = ofpbuf_clone_data_with_headroom(
1357 pin->packet->data, MIN(pin->send_len, pin->packet->size),
1358 offsetof(struct ofp_packet_in, data));
1361 /* Add OFPT_PACKET_IN. */
1362 opi = ofpbuf_push_zeros(rw_packet, offsetof(struct ofp_packet_in, data));
1363 opi->header.version = OFP_VERSION;
1364 opi->header.type = OFPT_PACKET_IN;
1365 opi->total_len = htons(total_len);
1366 opi->in_port = htons(pin->in_port);
1367 opi->reason = pin->reason;
1368 opi->buffer_id = htonl(pin->buffer_id);
1369 update_openflow_length(rw_packet);
1374 /* Returns a string representing the message type of 'type'. The string is the
1375 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
1376 * messages, the constant is followed by "request" or "reply",
1377 * e.g. "OFPST_AGGREGATE reply". */
1379 ofputil_msg_type_name(const struct ofputil_msg_type *type)
1384 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1385 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1386 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
1389 * The caller is responsible for freeing '*bufferp' when it is no longer
1392 * The OpenFlow header length is initially set to 'openflow_len'; if the
1393 * message is later extended, the length should be updated with
1394 * update_openflow_length() before sending.
1396 * Returns the header. */
1398 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1400 *bufferp = ofpbuf_new(openflow_len);
1401 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1404 /* Similar to make_openflow() but creates a Nicira vendor extension message
1405 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1407 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1409 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
1412 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1413 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1414 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
1417 * The caller is responsible for freeing '*bufferp' when it is no longer
1420 * The OpenFlow header length is initially set to 'openflow_len'; if the
1421 * message is later extended, the length should be updated with
1422 * update_openflow_length() before sending.
1424 * Returns the header. */
1426 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1427 struct ofpbuf **bufferp)
1429 *bufferp = ofpbuf_new(openflow_len);
1430 return put_openflow_xid(openflow_len, type, xid, *bufferp);
1433 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
1434 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1436 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1437 struct ofpbuf **bufferp)
1439 *bufferp = ofpbuf_new(openflow_len);
1440 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
1443 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1444 * with the given 'type' and an arbitrary transaction id. Allocated bytes
1445 * beyond the header, if any, are zeroed.
1447 * The OpenFlow header length is initially set to 'openflow_len'; if the
1448 * message is later extended, the length should be updated with
1449 * update_openflow_length() before sending.
1451 * Returns the header. */
1453 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1455 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
1458 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1459 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
1460 * the header, if any, are zeroed.
1462 * The OpenFlow header length is initially set to 'openflow_len'; if the
1463 * message is later extended, the length should be updated with
1464 * update_openflow_length() before sending.
1466 * Returns the header. */
1468 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1469 struct ofpbuf *buffer)
1471 struct ofp_header *oh;
1473 assert(openflow_len >= sizeof *oh);
1474 assert(openflow_len <= UINT16_MAX);
1476 oh = ofpbuf_put_uninit(buffer, openflow_len);
1477 oh->version = OFP_VERSION;
1479 oh->length = htons(openflow_len);
1481 memset(oh + 1, 0, openflow_len - sizeof *oh);
1485 /* Similar to put_openflow() but append a Nicira vendor extension message with
1486 * the specific 'subtype'. 'subtype' should be in host byte order. */
1488 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
1490 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
1493 /* Similar to put_openflow_xid() but append a Nicira vendor extension message
1494 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1496 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1497 struct ofpbuf *buffer)
1499 struct nicira_header *nxh;
1501 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
1502 nxh->vendor = htonl(NX_VENDOR_ID);
1503 nxh->subtype = htonl(subtype);
1507 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
1508 * 'buffer->size'. */
1510 update_openflow_length(struct ofpbuf *buffer)
1512 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
1513 oh->length = htons(buffer->size);
1516 /* Creates an ofp_stats_request with the given 'type' and 'body_len' bytes of
1517 * space allocated for the 'body' member. Returns the first byte of the 'body'
1520 ofputil_make_stats_request(size_t body_len, uint16_t type,
1521 struct ofpbuf **bufferp)
1523 struct ofp_stats_request *osr;
1524 osr = make_openflow((offsetof(struct ofp_stats_request, body)
1525 + body_len), OFPT_STATS_REQUEST, bufferp);
1526 osr->type = htons(type);
1527 osr->flags = htons(0);
1531 /* Creates a stats request message with Nicira as vendor and the given
1532 * 'subtype', of total length 'openflow_len'. Returns the message. */
1534 ofputil_make_nxstats_request(size_t openflow_len, uint32_t subtype,
1535 struct ofpbuf **bufferp)
1537 struct nicira_stats_msg *nsm;
1539 nsm = make_openflow(openflow_len, OFPT_STATS_REQUEST, bufferp);
1540 nsm->type = htons(OFPST_VENDOR);
1541 nsm->flags = htons(0);
1542 nsm->vendor = htonl(NX_VENDOR_ID);
1543 nsm->subtype = htonl(subtype);
1547 /* Returns the first byte of the 'body' member of the ofp_stats_request or
1548 * ofp_stats_reply in 'oh'. */
1550 ofputil_stats_body(const struct ofp_header *oh)
1552 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1553 return ((const struct ofp_stats_request *) oh)->body;
1556 /* Returns the length of the 'body' member of the ofp_stats_request or
1557 * ofp_stats_reply in 'oh'. */
1559 ofputil_stats_body_len(const struct ofp_header *oh)
1561 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1562 return ntohs(oh->length) - sizeof(struct ofp_stats_request);
1565 /* Returns the first byte of the body of the nicira_stats_msg in 'oh'. */
1567 ofputil_nxstats_body(const struct ofp_header *oh)
1569 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1570 return ((const struct nicira_stats_msg *) oh) + 1;
1573 /* Returns the length of the body of the nicira_stats_msg in 'oh'. */
1575 ofputil_nxstats_body_len(const struct ofp_header *oh)
1577 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1578 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
1582 make_flow_mod(uint16_t command, const struct cls_rule *rule,
1585 struct ofp_flow_mod *ofm;
1586 size_t size = sizeof *ofm + actions_len;
1587 struct ofpbuf *out = ofpbuf_new(size);
1588 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
1589 ofm->header.version = OFP_VERSION;
1590 ofm->header.type = OFPT_FLOW_MOD;
1591 ofm->header.length = htons(size);
1593 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
1594 ofputil_cls_rule_to_match(rule, &ofm->match);
1595 ofm->command = htons(command);
1600 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
1601 uint16_t idle_timeout, size_t actions_len)
1603 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
1604 struct ofp_flow_mod *ofm = out->data;
1605 ofm->idle_timeout = htons(idle_timeout);
1606 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
1607 ofm->buffer_id = htonl(buffer_id);
1612 make_del_flow(const struct cls_rule *rule)
1614 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
1615 struct ofp_flow_mod *ofm = out->data;
1616 ofm->out_port = htons(OFPP_NONE);
1621 make_add_simple_flow(const struct cls_rule *rule,
1622 uint32_t buffer_id, uint16_t out_port,
1623 uint16_t idle_timeout)
1625 if (out_port != OFPP_NONE) {
1626 struct ofp_action_output *oao;
1627 struct ofpbuf *buffer;
1629 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
1630 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
1631 oao->type = htons(OFPAT_OUTPUT);
1632 oao->len = htons(sizeof *oao);
1633 oao->port = htons(out_port);
1636 return make_add_flow(rule, buffer_id, idle_timeout, 0);
1641 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
1642 const struct ofpbuf *payload, int max_send_len)
1644 struct ofp_packet_in *opi;
1648 send_len = MIN(max_send_len, payload->size);
1649 buf = ofpbuf_new(sizeof *opi + send_len);
1650 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
1651 OFPT_PACKET_IN, 0, buf);
1652 opi->buffer_id = htonl(buffer_id);
1653 opi->total_len = htons(payload->size);
1654 opi->in_port = htons(in_port);
1655 opi->reason = reason;
1656 ofpbuf_put(buf, payload->data, send_len);
1657 update_openflow_length(buf);
1663 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1665 const struct ofp_action_header *actions, size_t n_actions)
1667 size_t actions_len = n_actions * sizeof *actions;
1668 struct ofp_packet_out *opo;
1669 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
1670 struct ofpbuf *out = ofpbuf_new(size);
1672 opo = ofpbuf_put_uninit(out, sizeof *opo);
1673 opo->header.version = OFP_VERSION;
1674 opo->header.type = OFPT_PACKET_OUT;
1675 opo->header.length = htons(size);
1676 opo->header.xid = htonl(0);
1677 opo->buffer_id = htonl(buffer_id);
1678 opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
1679 opo->actions_len = htons(actions_len);
1680 ofpbuf_put(out, actions, actions_len);
1682 ofpbuf_put(out, packet->data, packet->size);
1688 make_unbuffered_packet_out(const struct ofpbuf *packet,
1689 uint16_t in_port, uint16_t out_port)
1691 struct ofp_action_output action;
1692 action.type = htons(OFPAT_OUTPUT);
1693 action.len = htons(sizeof action);
1694 action.port = htons(out_port);
1695 return make_packet_out(packet, UINT32_MAX, in_port,
1696 (struct ofp_action_header *) &action, 1);
1700 make_buffered_packet_out(uint32_t buffer_id,
1701 uint16_t in_port, uint16_t out_port)
1703 if (out_port != OFPP_NONE) {
1704 struct ofp_action_output action;
1705 action.type = htons(OFPAT_OUTPUT);
1706 action.len = htons(sizeof action);
1707 action.port = htons(out_port);
1708 return make_packet_out(NULL, buffer_id, in_port,
1709 (struct ofp_action_header *) &action, 1);
1711 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
1715 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1717 make_echo_request(void)
1719 struct ofp_header *rq;
1720 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1721 rq = ofpbuf_put_uninit(out, sizeof *rq);
1722 rq->version = OFP_VERSION;
1723 rq->type = OFPT_ECHO_REQUEST;
1724 rq->length = htons(sizeof *rq);
1729 /* Creates and returns an OFPT_ECHO_REPLY message matching the
1730 * OFPT_ECHO_REQUEST message in 'rq'. */
1732 make_echo_reply(const struct ofp_header *rq)
1734 size_t size = ntohs(rq->length);
1735 struct ofpbuf *out = ofpbuf_new(size);
1736 struct ofp_header *reply = ofpbuf_put(out, rq, size);
1737 reply->type = OFPT_ECHO_REPLY;
1742 check_action_exact_len(const union ofp_action *a, unsigned int len,
1743 unsigned int required_len)
1745 if (len != required_len) {
1746 VLOG_WARN_RL(&bad_ofmsg_rl, "action %"PRIu16" has invalid length "
1747 "%"PRIu16" (must be %u)\n",
1748 ntohs(a->type), ntohs(a->header.len), required_len);
1749 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1755 check_nx_action_exact_len(const struct nx_action_header *a,
1756 unsigned int len, unsigned int required_len)
1758 if (len != required_len) {
1759 VLOG_WARN_RL(&bad_ofmsg_rl,
1760 "Nicira action %"PRIu16" has invalid length %"PRIu16" "
1762 ntohs(a->subtype), ntohs(a->len), required_len);
1763 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1768 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
1769 * that the switch will never have more than 'max_ports' ports. Returns 0 if
1770 * 'port' is valid, otherwise an ofp_mkerr() return code. */
1772 check_output_port(uint16_t port, int max_ports)
1780 case OFPP_CONTROLLER:
1785 if (port < max_ports) {
1788 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
1789 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1793 /* Checks that 'action' is a valid OFPAT_ENQUEUE action, given that the switch
1794 * will never have more than 'max_ports' ports. Returns 0 if 'port' is valid,
1795 * otherwise an ofp_mkerr() return code. */
1797 check_enqueue_action(const union ofp_action *a, unsigned int len,
1800 const struct ofp_action_enqueue *oae;
1804 error = check_action_exact_len(a, len, 16);
1809 oae = (const struct ofp_action_enqueue *) a;
1810 port = ntohs(oae->port);
1811 if (port < max_ports || port == OFPP_IN_PORT) {
1814 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown enqueue port %x", port);
1815 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1819 check_nicira_action(const union ofp_action *a, unsigned int len,
1820 const struct flow *flow)
1822 const struct nx_action_header *nah;
1827 VLOG_WARN_RL(&bad_ofmsg_rl,
1828 "Nicira vendor action only %u bytes", len);
1829 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1831 nah = (const struct nx_action_header *) a;
1833 subtype = ntohs(nah->subtype);
1834 if (subtype > TYPE_MAXIMUM(enum nx_action_subtype)) {
1835 /* This is necessary because enum nx_action_subtype may be an
1836 * 8-bit type, so the cast below throws away the top 8 bits. */
1837 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1840 switch ((enum nx_action_subtype) subtype) {
1841 case NXAST_RESUBMIT:
1842 case NXAST_SET_TUNNEL:
1843 case NXAST_DROP_SPOOFED_ARP:
1844 case NXAST_SET_QUEUE:
1845 case NXAST_POP_QUEUE:
1846 return check_nx_action_exact_len(nah, len, 16);
1848 case NXAST_REG_MOVE:
1849 error = check_nx_action_exact_len(nah, len,
1850 sizeof(struct nx_action_reg_move));
1854 return nxm_check_reg_move((const struct nx_action_reg_move *) a, flow);
1856 case NXAST_REG_LOAD:
1857 error = check_nx_action_exact_len(nah, len,
1858 sizeof(struct nx_action_reg_load));
1862 return nxm_check_reg_load((const struct nx_action_reg_load *) a, flow);
1867 case NXAST_SET_TUNNEL64:
1868 return check_nx_action_exact_len(
1869 nah, len, sizeof(struct nx_action_set_tunnel64));
1871 case NXAST_MULTIPATH:
1872 error = check_nx_action_exact_len(
1873 nah, len, sizeof(struct nx_action_multipath));
1877 return multipath_check((const struct nx_action_multipath *) a);
1879 case NXAST_AUTOPATH:
1880 error = check_nx_action_exact_len(
1881 nah, len, sizeof(struct nx_action_autopath));
1885 return autopath_check((const struct nx_action_autopath *) a);
1887 case NXAST_SNAT__OBSOLETE:
1889 VLOG_WARN_RL(&bad_ofmsg_rl,
1890 "unknown Nicira vendor action subtype %d", subtype);
1891 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1896 check_action(const union ofp_action *a, unsigned int len,
1897 const struct flow *flow, int max_ports)
1899 enum ofp_action_type type = ntohs(a->type);
1904 error = check_action_exact_len(a, len, 8);
1908 return check_output_port(ntohs(a->output.port), max_ports);
1910 case OFPAT_SET_VLAN_VID:
1911 error = check_action_exact_len(a, len, 8);
1915 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1916 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1920 case OFPAT_SET_VLAN_PCP:
1921 error = check_action_exact_len(a, len, 8);
1925 if (a->vlan_pcp.vlan_pcp & ~7) {
1926 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1930 case OFPAT_STRIP_VLAN:
1931 case OFPAT_SET_NW_SRC:
1932 case OFPAT_SET_NW_DST:
1933 case OFPAT_SET_NW_TOS:
1934 case OFPAT_SET_TP_SRC:
1935 case OFPAT_SET_TP_DST:
1936 return check_action_exact_len(a, len, 8);
1938 case OFPAT_SET_DL_SRC:
1939 case OFPAT_SET_DL_DST:
1940 return check_action_exact_len(a, len, 16);
1943 return (a->vendor.vendor == htonl(NX_VENDOR_ID)
1944 ? check_nicira_action(a, len, flow)
1945 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
1948 return check_enqueue_action(a, len, max_ports);
1951 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %d", (int) type);
1952 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
1957 validate_actions(const union ofp_action *actions, size_t n_actions,
1958 const struct flow *flow, int max_ports)
1962 for (i = 0; i < n_actions; ) {
1963 const union ofp_action *a = &actions[i];
1964 unsigned int len = ntohs(a->header.len);
1965 unsigned int n_slots = len / OFP_ACTION_ALIGN;
1966 unsigned int slots_left = &actions[n_actions] - a;
1969 if (n_slots > slots_left) {
1970 VLOG_WARN_RL(&bad_ofmsg_rl,
1971 "action requires %u slots but only %u remain",
1972 n_slots, slots_left);
1973 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1975 VLOG_WARN_RL(&bad_ofmsg_rl, "action has invalid length 0");
1976 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1977 } else if (len % OFP_ACTION_ALIGN) {
1978 VLOG_WARN_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
1979 "of %d", len, OFP_ACTION_ALIGN);
1980 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1983 error = check_action(a, len, flow, max_ports);
1992 /* Returns true if 'action' outputs to 'port', false otherwise. */
1994 action_outputs_to_port(const union ofp_action *action, ovs_be16 port)
1996 switch (ntohs(action->type)) {
1998 return action->output.port == port;
2000 return ((const struct ofp_action_enqueue *) action)->port == port;
2006 /* The set of actions must either come from a trusted source or have been
2007 * previously validated with validate_actions(). */
2008 const union ofp_action *
2009 actions_first(struct actions_iterator *iter,
2010 const union ofp_action *oa, size_t n_actions)
2013 iter->end = oa + n_actions;
2014 return actions_next(iter);
2017 const union ofp_action *
2018 actions_next(struct actions_iterator *iter)
2020 if (iter->pos != iter->end) {
2021 const union ofp_action *a = iter->pos;
2022 unsigned int len = ntohs(a->header.len);
2023 iter->pos += len / OFP_ACTION_ALIGN;
2031 normalize_wildcards(const struct ofp_match *m)
2033 enum { OFPFW_NW = (OFPFW_NW_SRC_ALL | OFPFW_NW_DST_ALL | OFPFW_NW_PROTO
2035 enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
2039 if (wc & htonl(OFPFW_DL_TYPE)) {
2040 wc |= htonl(OFPFW_NW | OFPFW_TP);
2041 } else if (m->dl_type == htons(ETH_TYPE_IP)) {
2042 if (wc & htonl(OFPFW_NW_PROTO) || (m->nw_proto != IPPROTO_TCP &&
2043 m->nw_proto != IPPROTO_UDP &&
2044 m->nw_proto != IPPROTO_ICMP)) {
2045 wc |= htonl(OFPFW_TP);
2047 } else if (m->dl_type == htons(ETH_TYPE_ARP)) {
2048 wc |= htonl(OFPFW_TP);
2050 wc |= htonl(OFPFW_NW | OFPFW_TP);
2056 vendor_code_to_id(uint8_t code)
2059 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
2061 #undef OFPUTIL_VENDOR
2068 vendor_id_to_code(uint32_t id)
2071 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case VENDOR_ID: return NAME;
2073 #undef OFPUTIL_VENDOR
2079 /* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
2080 * information taken from 'error', whose encoding must be as described in the
2081 * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
2082 * oh->xid as its transaction ID, and it will include up to the first 64 bytes
2085 * Returns NULL if 'error' is not an OpenFlow error code. */
2087 ofputil_encode_error_msg(int error, const struct ofp_header *oh)
2089 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2099 if (!is_ofp_error(error)) {
2100 /* We format 'error' with strerror() here since it seems likely to be
2101 * a system errno value. */
2102 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
2103 error, strerror(error));
2110 len = ntohs(oh->length);
2120 vendor = get_ofp_err_vendor(error);
2121 type = get_ofp_err_type(error);
2122 code = get_ofp_err_code(error);
2123 if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
2124 struct ofp_error_msg *oem;
2126 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
2127 oem->type = htons(type);
2128 oem->code = htons(code);
2130 struct ofp_error_msg *oem;
2131 struct nx_vendor_error *nve;
2134 vendor_id = vendor_code_to_id(vendor);
2135 if (vendor_id == UINT32_MAX) {
2136 VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
2141 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
2142 OFPT_ERROR, xid, &buf);
2143 oem->type = htons(NXET_VENDOR);
2144 oem->code = htons(NXVC_VENDOR_ERROR);
2146 nve = (struct nx_vendor_error *)oem->data;
2147 nve->vendor = htonl(vendor_id);
2148 nve->type = htons(type);
2149 nve->code = htons(code);
2154 ofpbuf_put(buf, data, len);
2160 /* Decodes 'oh', which should be an OpenFlow OFPT_ERROR message, and returns an
2161 * Open vSwitch internal error code in the format described in the large
2162 * comment in ofp-util.h.
2164 * If 'payload_ofs' is nonnull, on success '*payload_ofs' is set to the offset
2165 * to the payload starting from 'oh' and on failure it is set to 0. */
2167 ofputil_decode_error_msg(const struct ofp_header *oh, size_t *payload_ofs)
2169 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2171 const struct ofp_error_msg *oem;
2172 uint16_t type, code;
2179 if (oh->type != OFPT_ERROR) {
2183 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2184 oem = ofpbuf_try_pull(&b, sizeof *oem);
2189 type = ntohs(oem->type);
2190 code = ntohs(oem->code);
2191 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
2192 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
2197 vendor = vendor_id_to_code(ntohl(nve->vendor));
2199 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
2200 ntohl(nve->vendor));
2203 type = ntohs(nve->type);
2204 code = ntohs(nve->code);
2206 vendor = OFPUTIL_VENDOR_OPENFLOW;
2210 VLOG_WARN_RL(&rl, "error contains type %"PRIu16" greater than "
2211 "supported maximum value 1023", type);
2216 *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
2218 return ofp_mkerr_vendor(vendor, type, code);
2222 ofputil_format_error(struct ds *s, int error)
2224 if (is_errno(error)) {
2225 ds_put_cstr(s, strerror(error));
2227 uint16_t type = get_ofp_err_type(error);
2228 uint16_t code = get_ofp_err_code(error);
2229 const char *type_s = ofp_error_type_to_string(type);
2230 const char *code_s = ofp_error_code_to_string(type, code);
2232 ds_put_format(s, "type ");
2234 ds_put_cstr(s, type_s);
2236 ds_put_format(s, "%"PRIu16, type);
2239 ds_put_cstr(s, ", code ");
2241 ds_put_cstr(s, code_s);
2243 ds_put_format(s, "%"PRIu16, code);
2249 ofputil_error_to_string(int error)
2251 struct ds s = DS_EMPTY_INITIALIZER;
2252 ofputil_format_error(&s, error);
2253 return ds_steal_cstr(&s);
2256 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
2257 * successful, otherwise an OpenFlow error.
2259 * If successful, the first action is stored in '*actionsp' and the number of
2260 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
2261 * are stored, respectively.
2263 * This function does not check that the actions are valid (the caller should
2264 * do so, with validate_actions()). The caller is also responsible for making
2265 * sure that 'b->data' is initially aligned appropriately for "union
2268 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2269 union ofp_action **actionsp, size_t *n_actionsp)
2271 if (actions_len % OFP_ACTION_ALIGN != 0) {
2272 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2273 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
2277 *actionsp = ofpbuf_try_pull(b, actions_len);
2278 if (*actionsp == NULL) {
2279 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2280 "exceeds remaining message length (%zu)",
2281 actions_len, b->size);
2285 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
2291 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);