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 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
40 * in the peer and so there's not much point in showing a lot of them. */
41 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
43 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
44 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
47 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
48 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
49 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
50 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
53 ofputil_wcbits_to_netmask(int wcbits)
56 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
59 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
60 * that it wildcards. 'netmask' must be a CIDR netmask (see ip_is_cidr()). */
62 ofputil_netmask_to_wcbits(ovs_be32 netmask)
64 assert(ip_is_cidr(netmask));
66 return netmask == htonl(0) ? 32 : __builtin_ctz(ntohl(netmask));
70 for (wcbits = 32; netmask; wcbits--) {
71 netmask &= netmask - 1;
78 /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
80 #define WC_INVARIANT_LIST \
81 WC_INVARIANT_BIT(IN_PORT) \
82 WC_INVARIANT_BIT(DL_SRC) \
83 WC_INVARIANT_BIT(DL_DST) \
84 WC_INVARIANT_BIT(DL_TYPE) \
85 WC_INVARIANT_BIT(NW_PROTO) \
86 WC_INVARIANT_BIT(TP_SRC) \
87 WC_INVARIANT_BIT(TP_DST)
89 /* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
90 * actually have the same names and values. */
91 #define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME);
93 #undef WC_INVARIANT_BIT
95 /* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
99 #define WC_INVARIANT_BIT(NAME) | FWW_##NAME
101 #undef WC_INVARIANT_BIT
104 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
107 ofputil_cls_rule_from_match(const struct ofp_match *match,
108 unsigned int priority, struct cls_rule *rule)
110 struct flow_wildcards *wc = &rule->wc;
114 /* Initialize rule->priority. */
115 ofpfw = ntohl(match->wildcards) & OFPFW_ALL;
116 rule->priority = !ofpfw ? UINT16_MAX : priority;
118 /* Initialize most of rule->wc. */
119 flow_wildcards_init_catchall(wc);
120 wc->wildcards = ofpfw & WC_INVARIANTS;
122 /* Wildcard fields that aren't defined by ofp_match or tun_id. */
123 wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_ND_TARGET);
125 if (ofpfw & OFPFW_NW_TOS) {
126 wc->wildcards |= FWW_NW_TOS;
128 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
129 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
131 if (ofpfw & OFPFW_DL_DST) {
132 /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
133 * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST
134 * and FWW_ETH_MCAST. */
135 wc->wildcards |= FWW_ETH_MCAST;
138 /* Initialize most of rule->flow. */
139 rule->flow.nw_src = match->nw_src;
140 rule->flow.nw_dst = match->nw_dst;
141 rule->flow.in_port = ntohs(match->in_port);
142 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
143 rule->flow.tp_src = match->tp_src;
144 rule->flow.tp_dst = match->tp_dst;
145 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
146 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
147 rule->flow.nw_tos = match->nw_tos;
148 rule->flow.nw_proto = match->nw_proto;
150 /* Translate VLANs. */
151 vid = match->dl_vlan & htons(VLAN_VID_MASK);
152 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
153 switch (ofpfw & (OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP)) {
154 case OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP:
155 /* Wildcard everything. */
156 rule->flow.vlan_tci = htons(0);
157 rule->wc.vlan_tci_mask = htons(0);
160 case OFPFW_DL_VLAN_PCP:
161 if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
162 /* Match only packets without 802.1Q header. */
163 rule->flow.vlan_tci = htons(0);
164 rule->wc.vlan_tci_mask = htons(0xffff);
166 /* Wildcard PCP, specific VID. */
167 rule->flow.vlan_tci = vid | htons(VLAN_CFI);
168 rule->wc.vlan_tci_mask = htons(VLAN_VID_MASK | VLAN_CFI);
173 /* Wildcard VID, specific PCP. */
174 rule->flow.vlan_tci = pcp | htons(VLAN_CFI);
175 rule->wc.vlan_tci_mask = htons(VLAN_PCP_MASK | VLAN_CFI);
179 if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
180 /* This case is odd, since we can't have a specific PCP without an
181 * 802.1Q header. However, older versions of OVS treated this as
182 * matching packets withut an 802.1Q header, so we do here too. */
183 rule->flow.vlan_tci = htons(0);
184 rule->wc.vlan_tci_mask = htons(0xffff);
186 /* Specific VID and PCP. */
187 rule->flow.vlan_tci = vid | pcp | htons(VLAN_CFI);
188 rule->wc.vlan_tci_mask = htons(0xffff);
194 cls_rule_zero_wildcarded_fields(rule);
197 /* Convert 'rule' into the OpenFlow match structure 'match'. */
199 ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match)
201 const struct flow_wildcards *wc = &rule->wc;
204 /* Figure out most OpenFlow wildcards. */
205 ofpfw = wc->wildcards & WC_INVARIANTS;
206 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
207 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
208 if (wc->wildcards & FWW_NW_TOS) {
209 ofpfw |= OFPFW_NW_TOS;
212 /* Translate VLANs. */
213 match->dl_vlan = htons(0);
214 match->dl_vlan_pcp = 0;
215 if (rule->wc.vlan_tci_mask == htons(0)) {
216 ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
217 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
218 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
219 match->dl_vlan = htons(OFP_VLAN_NONE);
221 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
222 ofpfw |= OFPFW_DL_VLAN;
224 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
227 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
228 ofpfw |= OFPFW_DL_VLAN_PCP;
230 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
234 /* Compose most of the match structure. */
235 match->wildcards = htonl(ofpfw);
236 match->in_port = htons(rule->flow.in_port);
237 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
238 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
239 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
240 match->nw_src = rule->flow.nw_src;
241 match->nw_dst = rule->flow.nw_dst;
242 match->nw_tos = rule->flow.nw_tos;
243 match->nw_proto = rule->flow.nw_proto;
244 match->tp_src = rule->flow.tp_src;
245 match->tp_dst = rule->flow.tp_dst;
246 memset(match->pad1, '\0', sizeof match->pad1);
247 memset(match->pad2, '\0', sizeof match->pad2);
250 /* Given a 'dl_type' value in the format used in struct flow, returns the
251 * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */
253 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
255 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
256 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
260 /* Given a 'dl_type' value in the format used in an OpenFlow ofp_match
261 * structure, returns the corresponding 'dl_type' value for use in struct
264 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
266 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
267 ? htons(FLOW_DL_TYPE_NONE)
271 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
275 static uint32_t next_xid = 1;
276 return htonl(next_xid++);
279 /* Basic parsing of OpenFlow messages. */
281 struct ofputil_msg_type {
282 enum ofputil_msg_code code; /* OFPUTIL_*. */
283 uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
284 const char *name; /* e.g. "OFPT_FLOW_REMOVED". */
285 unsigned int min_size; /* Minimum total message size in bytes. */
286 /* 0 if 'min_size' is the exact size that the message must be. Otherwise,
287 * the message may exceed 'min_size' by an even multiple of this value. */
288 unsigned int extra_multiple;
291 struct ofputil_msg_category {
292 const char *name; /* e.g. "OpenFlow message" */
293 const struct ofputil_msg_type *types;
295 int missing_error; /* ofp_mkerr() value for missing type. */
299 ofputil_length_ok(const struct ofputil_msg_category *cat,
300 const struct ofputil_msg_type *type,
303 switch (type->extra_multiple) {
305 if (size != type->min_size) {
306 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
307 "length %u (expected length %u)",
308 cat->name, type->name, size, type->min_size);
314 if (size < type->min_size) {
315 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
316 "length %u (expected length at least %u bytes)",
317 cat->name, type->name, size, type->min_size);
323 if (size < type->min_size
324 || (size - type->min_size) % type->extra_multiple) {
325 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
326 "length %u (must be exactly %u bytes or longer "
327 "by an integer multiple of %u bytes)",
328 cat->name, type->name, size,
329 type->min_size, type->extra_multiple);
337 ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
338 uint32_t value, unsigned int size,
339 const struct ofputil_msg_type **typep)
341 const struct ofputil_msg_type *type;
343 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
344 if (type->value == value) {
345 if (!ofputil_length_ok(cat, type, size)) {
346 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
353 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
355 return cat->missing_error;
359 ofputil_decode_vendor(const struct ofp_header *oh,
360 const struct ofputil_msg_type **typep)
362 BUILD_ASSERT_DECL(sizeof(struct nxt_set_flow_format)
363 != sizeof(struct nxt_flow_mod_table_id));
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;
412 if (nh->subtype == htonl(NXT_FLOW_MOD_TABLE_ID)
413 && oh->length == htons(sizeof(struct nxt_flow_mod_table_id))) {
414 /* NXT_SET_FLOW_FORMAT and NXT_FLOW_MOD_TABLE_ID accidentally have the
415 * same value but different lengths. ofputil_lookup_openflow_message()
416 * doesn't support this case, so special case it here. */
417 static const struct ofputil_msg_type nxt_flow_mod_table_id =
418 { OFPUTIL_NXT_FLOW_MOD_TABLE_ID,
419 NXT_FLOW_MOD_TABLE_ID, "NXT_FLOW_MOD_TABLE_ID",
420 sizeof(struct nxt_flow_mod_table_id), 0 };
422 *typep = &nxt_flow_mod_table_id;
426 return ofputil_lookup_openflow_message(&nxt_category, ntohl(nh->subtype),
427 ntohs(oh->length), typep);
431 check_nxstats_msg(const struct ofp_header *oh)
433 const struct ofp_stats_request *osr;
436 osr = (const struct ofp_stats_request *) oh;
438 memcpy(&vendor, osr->body, sizeof vendor);
439 if (vendor != htonl(NX_VENDOR_ID)) {
440 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
441 "unknown vendor %"PRIx32, ntohl(vendor));
442 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
445 if (ntohs(osr->header.length) < sizeof(struct nicira_stats_msg)) {
446 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
447 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
454 ofputil_decode_nxst_request(const struct ofp_header *oh,
455 const struct ofputil_msg_type **typep)
457 static const struct ofputil_msg_type nxst_requests[] = {
458 { OFPUTIL_NXST_FLOW_REQUEST,
459 NXST_FLOW, "NXST_FLOW request",
460 sizeof(struct nx_flow_stats_request), 8 },
462 { OFPUTIL_NXST_AGGREGATE_REQUEST,
463 NXST_AGGREGATE, "NXST_AGGREGATE request",
464 sizeof(struct nx_aggregate_stats_request), 8 },
467 static const struct ofputil_msg_category nxst_request_category = {
468 "Nicira extension statistics request",
469 nxst_requests, ARRAY_SIZE(nxst_requests),
470 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
473 const struct nicira_stats_msg *nsm;
476 error = check_nxstats_msg(oh);
481 nsm = (struct nicira_stats_msg *) oh;
482 return ofputil_lookup_openflow_message(&nxst_request_category,
484 ntohs(oh->length), typep);
488 ofputil_decode_nxst_reply(const struct ofp_header *oh,
489 const struct ofputil_msg_type **typep)
491 static const struct ofputil_msg_type nxst_replies[] = {
492 { OFPUTIL_NXST_FLOW_REPLY,
493 NXST_FLOW, "NXST_FLOW reply",
494 sizeof(struct nicira_stats_msg), 8 },
496 { OFPUTIL_NXST_AGGREGATE_REPLY,
497 NXST_AGGREGATE, "NXST_AGGREGATE reply",
498 sizeof(struct nx_aggregate_stats_reply), 0 },
501 static const struct ofputil_msg_category nxst_reply_category = {
502 "Nicira extension statistics reply",
503 nxst_replies, ARRAY_SIZE(nxst_replies),
504 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
507 const struct nicira_stats_msg *nsm;
510 error = check_nxstats_msg(oh);
515 nsm = (struct nicira_stats_msg *) oh;
516 return ofputil_lookup_openflow_message(&nxst_reply_category,
518 ntohs(oh->length), typep);
522 ofputil_decode_ofpst_request(const struct ofp_header *oh,
523 const struct ofputil_msg_type **typep)
525 enum { OSR_SIZE = sizeof(struct ofp_stats_request) };
526 static const struct ofputil_msg_type ofpst_requests[] = {
527 { OFPUTIL_OFPST_DESC_REQUEST,
528 OFPST_DESC, "OFPST_DESC request",
531 { OFPUTIL_OFPST_FLOW_REQUEST,
532 OFPST_FLOW, "OFPST_FLOW request",
533 OSR_SIZE + sizeof(struct ofp_flow_stats_request), 0 },
535 { OFPUTIL_OFPST_AGGREGATE_REQUEST,
536 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
537 OSR_SIZE + sizeof(struct ofp_aggregate_stats_request), 0 },
539 { OFPUTIL_OFPST_TABLE_REQUEST,
540 OFPST_TABLE, "OFPST_TABLE request",
543 { OFPUTIL_OFPST_PORT_REQUEST,
544 OFPST_PORT, "OFPST_PORT request",
545 OSR_SIZE + sizeof(struct ofp_port_stats_request), 0 },
547 { OFPUTIL_OFPST_QUEUE_REQUEST,
548 OFPST_QUEUE, "OFPST_QUEUE request",
549 OSR_SIZE + sizeof(struct ofp_queue_stats_request), 0 },
552 OFPST_VENDOR, "OFPST_VENDOR request",
553 OSR_SIZE + sizeof(uint32_t), 1 },
556 static const struct ofputil_msg_category ofpst_request_category = {
557 "OpenFlow statistics",
558 ofpst_requests, ARRAY_SIZE(ofpst_requests),
559 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
562 const struct ofp_stats_request *osr;
565 osr = (const struct ofp_stats_request *) oh;
566 error = ofputil_lookup_openflow_message(&ofpst_request_category,
568 ntohs(oh->length), typep);
569 if (!error && osr->type == htons(OFPST_VENDOR)) {
570 error = ofputil_decode_nxst_request(oh, typep);
576 ofputil_decode_ofpst_reply(const struct ofp_header *oh,
577 const struct ofputil_msg_type **typep)
579 enum { OSR_SIZE = sizeof(struct ofp_stats_reply) };
580 static const struct ofputil_msg_type ofpst_replies[] = {
581 { OFPUTIL_OFPST_DESC_REPLY,
582 OFPST_DESC, "OFPST_DESC reply",
583 OSR_SIZE + sizeof(struct ofp_desc_stats), 0 },
585 { OFPUTIL_OFPST_FLOW_REPLY,
586 OFPST_FLOW, "OFPST_FLOW reply",
589 { OFPUTIL_OFPST_AGGREGATE_REPLY,
590 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
591 OSR_SIZE + sizeof(struct ofp_aggregate_stats_reply), 0 },
593 { OFPUTIL_OFPST_TABLE_REPLY,
594 OFPST_TABLE, "OFPST_TABLE reply",
595 OSR_SIZE, sizeof(struct ofp_table_stats) },
597 { OFPUTIL_OFPST_PORT_REPLY,
598 OFPST_PORT, "OFPST_PORT reply",
599 OSR_SIZE, sizeof(struct ofp_port_stats) },
601 { OFPUTIL_OFPST_QUEUE_REPLY,
602 OFPST_QUEUE, "OFPST_QUEUE reply",
603 OSR_SIZE, sizeof(struct ofp_queue_stats) },
606 OFPST_VENDOR, "OFPST_VENDOR reply",
607 OSR_SIZE + sizeof(uint32_t), 1 },
610 static const struct ofputil_msg_category ofpst_reply_category = {
611 "OpenFlow statistics",
612 ofpst_replies, ARRAY_SIZE(ofpst_replies),
613 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
616 const struct ofp_stats_reply *osr = (const struct ofp_stats_reply *) oh;
619 error = ofputil_lookup_openflow_message(&ofpst_reply_category,
621 ntohs(oh->length), typep);
622 if (!error && osr->type == htons(OFPST_VENDOR)) {
623 error = ofputil_decode_nxst_reply(oh, typep);
628 /* Decodes the message type represented by 'oh'. Returns 0 if successful or
629 * an OpenFlow error code constructed with ofp_mkerr() on failure. Either
630 * way, stores in '*typep' a type structure that can be inspected with the
631 * ofputil_msg_type_*() functions.
633 * oh->length must indicate the correct length of the message (and must be at
634 * least sizeof(struct ofp_header)).
636 * Success indicates that 'oh' is at least as long as the minimum-length
637 * message of its type. */
639 ofputil_decode_msg_type(const struct ofp_header *oh,
640 const struct ofputil_msg_type **typep)
642 static const struct ofputil_msg_type ofpt_messages[] = {
643 { OFPUTIL_OFPT_HELLO,
644 OFPT_HELLO, "OFPT_HELLO",
645 sizeof(struct ofp_hello), 1 },
647 { OFPUTIL_OFPT_ERROR,
648 OFPT_ERROR, "OFPT_ERROR",
649 sizeof(struct ofp_error_msg), 1 },
651 { OFPUTIL_OFPT_ECHO_REQUEST,
652 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
653 sizeof(struct ofp_header), 1 },
655 { OFPUTIL_OFPT_ECHO_REPLY,
656 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
657 sizeof(struct ofp_header), 1 },
659 { OFPUTIL_OFPT_FEATURES_REQUEST,
660 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
661 sizeof(struct ofp_header), 0 },
663 { OFPUTIL_OFPT_FEATURES_REPLY,
664 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
665 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
667 { OFPUTIL_OFPT_GET_CONFIG_REQUEST,
668 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
669 sizeof(struct ofp_header), 0 },
671 { OFPUTIL_OFPT_GET_CONFIG_REPLY,
672 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
673 sizeof(struct ofp_switch_config), 0 },
675 { OFPUTIL_OFPT_SET_CONFIG,
676 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
677 sizeof(struct ofp_switch_config), 0 },
679 { OFPUTIL_OFPT_PACKET_IN,
680 OFPT_PACKET_IN, "OFPT_PACKET_IN",
681 offsetof(struct ofp_packet_in, data), 1 },
683 { OFPUTIL_OFPT_FLOW_REMOVED,
684 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
685 sizeof(struct ofp_flow_removed), 0 },
687 { OFPUTIL_OFPT_PORT_STATUS,
688 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
689 sizeof(struct ofp_port_status), 0 },
691 { OFPUTIL_OFPT_PACKET_OUT,
692 OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
693 sizeof(struct ofp_packet_out), 1 },
695 { OFPUTIL_OFPT_FLOW_MOD,
696 OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
697 sizeof(struct ofp_flow_mod), 1 },
699 { OFPUTIL_OFPT_PORT_MOD,
700 OFPT_PORT_MOD, "OFPT_PORT_MOD",
701 sizeof(struct ofp_port_mod), 0 },
704 OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
705 sizeof(struct ofp_stats_request), 1 },
708 OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
709 sizeof(struct ofp_stats_reply), 1 },
711 { OFPUTIL_OFPT_BARRIER_REQUEST,
712 OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
713 sizeof(struct ofp_header), 0 },
715 { OFPUTIL_OFPT_BARRIER_REPLY,
716 OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
717 sizeof(struct ofp_header), 0 },
720 OFPT_VENDOR, "OFPT_VENDOR",
721 sizeof(struct ofp_vendor_header), 1 },
724 static const struct ofputil_msg_category ofpt_category = {
726 ofpt_messages, ARRAY_SIZE(ofpt_messages),
727 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE)
732 error = ofputil_lookup_openflow_message(&ofpt_category, oh->type,
733 ntohs(oh->length), typep);
737 error = ofputil_decode_vendor(oh, typep);
740 case OFPT_STATS_REQUEST:
741 error = ofputil_decode_ofpst_request(oh, typep);
744 case OFPT_STATS_REPLY:
745 error = ofputil_decode_ofpst_reply(oh, typep);
752 static const struct ofputil_msg_type ofputil_invalid_type = {
754 0, "OFPUTIL_INVALID",
758 *typep = &ofputil_invalid_type;
763 /* Returns an OFPUTIL_* message type code for 'type'. */
764 enum ofputil_msg_code
765 ofputil_msg_type_code(const struct ofputil_msg_type *type)
773 ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
775 switch (flow_format) {
776 case NXFF_OPENFLOW10:
785 ofputil_flow_format_to_string(enum nx_flow_format flow_format)
787 switch (flow_format) {
788 case NXFF_OPENFLOW10:
798 ofputil_flow_format_from_string(const char *s)
800 return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
801 : !strcmp(s, "nxm") ? NXFF_NXM
806 regs_fully_wildcarded(const struct flow_wildcards *wc)
810 for (i = 0; i < FLOW_N_REGS; i++) {
811 if (wc->reg_masks[i] != 0) {
818 /* Returns the minimum nx_flow_format to use for sending 'rule' to a switch
819 * (e.g. to add or remove a flow). Only NXM can handle tunnel IDs, registers,
820 * or fixing the Ethernet multicast bit. Otherwise, it's better to use
821 * NXFF_OPENFLOW10 for backward compatibility. */
823 ofputil_min_flow_format(const struct cls_rule *rule)
825 const struct flow_wildcards *wc = &rule->wc;
827 /* Only NXM supports separately wildcards the Ethernet multicast bit. */
828 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
832 /* Only NXM supports matching ARP hardware addresses. */
833 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
837 /* Only NXM supports matching IPv6 traffic. */
838 if (!(wc->wildcards & FWW_DL_TYPE)
839 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
843 /* Only NXM supports matching registers. */
844 if (!regs_fully_wildcarded(wc)) {
848 /* Only NXM supports matching tun_id. */
849 if (wc->tun_id_mask != htonll(0)) {
853 /* Other formats can express this rule. */
854 return NXFF_OPENFLOW10;
857 /* Returns an OpenFlow message that can be used to set the flow format to
860 ofputil_make_set_flow_format(enum nx_flow_format flow_format)
862 struct nxt_set_flow_format *sff;
865 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
866 sff->format = htonl(flow_format);
871 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
872 * extension on or off (according to 'flow_mod_table_id'). */
874 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
876 struct nxt_flow_mod_table_id *nfmti;
879 nfmti = make_nxmsg(sizeof *nfmti, NXT_FLOW_MOD_TABLE_ID, &msg);
880 nfmti->set = flow_mod_table_id;
884 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
885 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
888 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
889 * enabled, false otherwise.
891 * Does not validate the flow_mod actions. */
893 ofputil_decode_flow_mod(struct flow_mod *fm, const struct ofp_header *oh,
894 bool flow_mod_table_id)
896 const struct ofputil_msg_type *type;
900 ofpbuf_use_const(&b, oh, ntohs(oh->length));
902 ofputil_decode_msg_type(oh, &type);
903 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
904 /* Standard OpenFlow flow_mod. */
905 struct ofp_match match, orig_match;
906 const struct ofp_flow_mod *ofm;
909 /* Dissect the message. */
910 ofm = ofpbuf_pull(&b, sizeof *ofm);
911 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
916 /* Normalize ofm->match. If normalization actually changes anything,
917 * then log the differences. */
919 match.pad1[0] = match.pad2[0] = 0;
921 normalize_match(&match);
922 if (memcmp(&match, &orig_match, sizeof orig_match)) {
923 if (!VLOG_DROP_INFO(&bad_ofmsg_rl)) {
924 char *old = ofp_match_to_literal_string(&orig_match);
925 char *new = ofp_match_to_literal_string(&match);
926 VLOG_INFO("normalization changed ofp_match, details:");
927 VLOG_INFO(" pre: %s", old);
928 VLOG_INFO("post: %s", new);
934 /* Translate the message. */
935 ofputil_cls_rule_from_match(&match, ntohs(ofm->priority), &fm->cr);
936 fm->cookie = ofm->cookie;
937 command = ntohs(ofm->command);
938 fm->idle_timeout = ntohs(ofm->idle_timeout);
939 fm->hard_timeout = ntohs(ofm->hard_timeout);
940 fm->buffer_id = ntohl(ofm->buffer_id);
941 fm->out_port = ntohs(ofm->out_port);
942 fm->flags = ntohs(ofm->flags);
943 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
944 /* Nicira extended flow_mod. */
945 const struct nx_flow_mod *nfm;
948 /* Dissect the message. */
949 nfm = ofpbuf_pull(&b, sizeof *nfm);
950 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
955 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
960 /* Translate the message. */
961 fm->cookie = nfm->cookie;
962 command = ntohs(nfm->command);
963 fm->idle_timeout = ntohs(nfm->idle_timeout);
964 fm->hard_timeout = ntohs(nfm->hard_timeout);
965 fm->buffer_id = ntohl(nfm->buffer_id);
966 fm->out_port = ntohs(nfm->out_port);
967 fm->flags = ntohs(nfm->flags);
972 if (flow_mod_table_id) {
973 fm->command = command & 0xff;
974 fm->table_id = command >> 8;
976 fm->command = command;
983 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
984 * 'flow_format' and returns the message.
986 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
987 * enabled, false otherwise. */
989 ofputil_encode_flow_mod(const struct flow_mod *fm,
990 enum nx_flow_format flow_format,
991 bool flow_mod_table_id)
993 size_t actions_len = fm->n_actions * sizeof *fm->actions;
997 command = (flow_mod_table_id
998 ? (fm->command & 0xff) | (fm->table_id << 8)
1001 if (flow_format == NXFF_OPENFLOW10) {
1002 struct ofp_flow_mod *ofm;
1004 msg = ofpbuf_new(sizeof *ofm + actions_len);
1005 ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
1006 ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
1007 ofm->cookie = fm->cookie;
1008 ofm->command = htons(fm->command);
1009 ofm->idle_timeout = htons(fm->idle_timeout);
1010 ofm->hard_timeout = htons(fm->hard_timeout);
1011 ofm->priority = htons(fm->cr.priority);
1012 ofm->buffer_id = htonl(fm->buffer_id);
1013 ofm->out_port = htons(fm->out_port);
1014 ofm->flags = htons(fm->flags);
1015 } else if (flow_format == NXFF_NXM) {
1016 struct nx_flow_mod *nfm;
1019 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
1020 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
1021 match_len = nx_put_match(msg, &fm->cr);
1024 nfm->cookie = fm->cookie;
1025 nfm->command = htons(command);
1026 nfm->idle_timeout = htons(fm->idle_timeout);
1027 nfm->hard_timeout = htons(fm->hard_timeout);
1028 nfm->priority = htons(fm->cr.priority);
1029 nfm->buffer_id = htonl(fm->buffer_id);
1030 nfm->out_port = htons(fm->out_port);
1031 nfm->flags = htons(fm->flags);
1032 nfm->match_len = htons(match_len);
1037 ofpbuf_put(msg, fm->actions, actions_len);
1038 update_openflow_length(msg);
1043 ofputil_decode_ofpst_flow_request(struct flow_stats_request *fsr,
1044 const struct ofp_header *oh,
1047 const struct ofp_flow_stats_request *ofsr = ofputil_stats_body(oh);
1049 fsr->aggregate = aggregate;
1050 ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
1051 fsr->out_port = ntohs(ofsr->out_port);
1052 fsr->table_id = ofsr->table_id;
1058 ofputil_decode_nxst_flow_request(struct flow_stats_request *fsr,
1059 const struct ofp_header *oh,
1062 const struct nx_flow_stats_request *nfsr;
1066 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1068 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1069 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match);
1074 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1077 fsr->aggregate = aggregate;
1078 fsr->out_port = ntohs(nfsr->out_port);
1079 fsr->table_id = nfsr->table_id;
1084 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1085 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1086 * successful, otherwise an OpenFlow error code. */
1088 ofputil_decode_flow_stats_request(struct flow_stats_request *fsr,
1089 const struct ofp_header *oh)
1091 const struct ofputil_msg_type *type;
1095 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1097 ofputil_decode_msg_type(oh, &type);
1098 code = ofputil_msg_type_code(type);
1100 case OFPUTIL_OFPST_FLOW_REQUEST:
1101 return ofputil_decode_ofpst_flow_request(fsr, oh, false);
1103 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1104 return ofputil_decode_ofpst_flow_request(fsr, oh, true);
1106 case OFPUTIL_NXST_FLOW_REQUEST:
1107 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1109 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1110 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1113 /* Hey, the caller lied. */
1118 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1119 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1120 * 'flow_format', and returns the message. */
1122 ofputil_encode_flow_stats_request(const struct flow_stats_request *fsr,
1123 enum nx_flow_format flow_format)
1127 if (flow_format == NXFF_OPENFLOW10) {
1128 struct ofp_flow_stats_request *ofsr;
1131 BUILD_ASSERT_DECL(sizeof(struct ofp_flow_stats_request)
1132 == sizeof(struct ofp_aggregate_stats_request));
1134 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1135 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, &msg);
1136 ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
1137 ofsr->table_id = fsr->table_id;
1138 ofsr->out_port = htons(fsr->out_port);
1139 } else if (flow_format == NXFF_NXM) {
1140 struct nx_flow_stats_request *nfsr;
1144 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1145 ofputil_make_nxstats_request(sizeof *nfsr, subtype, &msg);
1146 match_len = nx_put_match(msg, &fsr->match);
1149 nfsr->out_port = htons(fsr->out_port);
1150 nfsr->match_len = htons(match_len);
1151 nfsr->table_id = fsr->table_id;
1159 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1160 * ofputil_flow_stats in 'fs'.
1162 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1163 * OpenFlow message. Calling this function multiple times for a single 'msg'
1164 * iterates through the replies. The caller must initially leave 'msg''s layer
1165 * pointers null and not modify them between calls.
1167 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1168 * otherwise a positive errno value. */
1170 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1173 const struct ofputil_msg_type *type;
1176 ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1177 code = ofputil_msg_type_code(type);
1179 msg->l2 = msg->data;
1180 if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1181 ofpbuf_pull(msg, sizeof(struct ofp_stats_reply));
1182 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1183 ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1191 } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1192 const struct ofp_flow_stats *ofs;
1195 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1197 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1198 "bytes at end", msg->size);
1202 length = ntohs(ofs->length);
1203 if (length < sizeof *ofs) {
1204 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1205 "length %zu", length);
1209 if (ofputil_pull_actions(msg, length - sizeof *ofs,
1210 &fs->actions, &fs->n_actions)) {
1214 fs->cookie = get_32aligned_be64(&ofs->cookie);
1215 ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
1217 fs->table_id = ofs->table_id;
1218 fs->duration_sec = ntohl(ofs->duration_sec);
1219 fs->duration_nsec = ntohl(ofs->duration_nsec);
1220 fs->idle_timeout = ntohs(ofs->idle_timeout);
1221 fs->hard_timeout = ntohs(ofs->hard_timeout);
1222 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1223 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1224 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1225 const struct nx_flow_stats *nfs;
1226 size_t match_len, length;
1228 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1230 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1231 "bytes at end", msg->size);
1235 length = ntohs(nfs->length);
1236 match_len = ntohs(nfs->match_len);
1237 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1238 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1239 "claims invalid length %zu", match_len, length);
1242 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule)) {
1246 if (ofputil_pull_actions(msg,
1247 length - sizeof *nfs - ROUND_UP(match_len, 8),
1248 &fs->actions, &fs->n_actions)) {
1252 fs->cookie = nfs->cookie;
1253 fs->table_id = nfs->table_id;
1254 fs->duration_sec = ntohl(nfs->duration_sec);
1255 fs->duration_nsec = ntohl(nfs->duration_nsec);
1256 fs->idle_timeout = ntohs(nfs->idle_timeout);
1257 fs->hard_timeout = ntohs(nfs->hard_timeout);
1258 fs->packet_count = ntohll(nfs->packet_count);
1259 fs->byte_count = ntohll(nfs->byte_count);
1267 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1268 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1269 * an OpenFlow error code. */
1271 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1272 const struct ofp_header *oh)
1274 const struct ofputil_msg_type *type;
1275 enum ofputil_msg_code code;
1277 ofputil_decode_msg_type(oh, &type);
1278 code = ofputil_msg_type_code(type);
1279 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1280 const struct ofp_flow_removed *ofr;
1282 ofr = (const struct ofp_flow_removed *) oh;
1283 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
1285 fr->cookie = ofr->cookie;
1286 fr->reason = ofr->reason;
1287 fr->duration_sec = ntohl(ofr->duration_sec);
1288 fr->duration_nsec = ntohl(ofr->duration_nsec);
1289 fr->idle_timeout = ntohs(ofr->idle_timeout);
1290 fr->packet_count = ntohll(ofr->packet_count);
1291 fr->byte_count = ntohll(ofr->byte_count);
1292 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1293 struct nx_flow_removed *nfr;
1297 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1299 nfr = ofpbuf_pull(&b, sizeof *nfr);
1300 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1306 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1309 fr->cookie = nfr->cookie;
1310 fr->reason = nfr->reason;
1311 fr->duration_sec = ntohl(nfr->duration_sec);
1312 fr->duration_nsec = ntohl(nfr->duration_nsec);
1313 fr->idle_timeout = ntohs(nfr->idle_timeout);
1314 fr->packet_count = ntohll(nfr->packet_count);
1315 fr->byte_count = ntohll(nfr->byte_count);
1323 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1324 * NXT_FLOW_REMOVED message 'oh' according to 'flow_format', and returns the
1327 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1328 enum nx_flow_format flow_format)
1332 if (flow_format == NXFF_OPENFLOW10) {
1333 struct ofp_flow_removed *ofr;
1335 ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
1337 ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
1338 ofr->priority = htons(fr->rule.priority);
1339 ofr->reason = fr->reason;
1340 ofr->duration_sec = htonl(fr->duration_sec);
1341 ofr->duration_nsec = htonl(fr->duration_nsec);
1342 ofr->idle_timeout = htons(fr->idle_timeout);
1343 ofr->packet_count = htonll(fr->packet_count);
1344 ofr->byte_count = htonll(fr->byte_count);
1345 } else if (flow_format == NXFF_NXM) {
1346 struct nx_flow_removed *nfr;
1349 make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
1350 match_len = nx_put_match(msg, &fr->rule);
1353 nfr->cookie = fr->cookie;
1354 nfr->priority = htons(fr->rule.priority);
1355 nfr->reason = fr->reason;
1356 nfr->duration_sec = htonl(fr->duration_sec);
1357 nfr->duration_nsec = htonl(fr->duration_nsec);
1358 nfr->idle_timeout = htons(fr->idle_timeout);
1359 nfr->match_len = htons(match_len);
1360 nfr->packet_count = htonll(fr->packet_count);
1361 nfr->byte_count = htonll(fr->byte_count);
1369 /* Converts abstract ofputil_packet_in 'pin' into an OFPT_PACKET_IN message
1370 * and returns the message.
1372 * If 'rw_packet' is NULL, the caller takes ownership of the newly allocated
1375 * If 'rw_packet' is nonnull, then it must contain the same data as
1376 * pin->packet. 'rw_packet' is allowed to be the same ofpbuf as pin->packet.
1377 * It is modified in-place into an OFPT_PACKET_IN message according to 'pin',
1378 * and then ofputil_encode_packet_in() returns 'rw_packet'. If 'rw_packet' has
1379 * enough headroom to insert a "struct ofp_packet_in", this is more efficient
1380 * than ofputil_encode_packet_in() because it does not copy the packet
1383 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
1384 struct ofpbuf *rw_packet)
1386 int total_len = pin->packet->size;
1387 struct ofp_packet_in *opi;
1390 if (pin->send_len < rw_packet->size) {
1391 rw_packet->size = pin->send_len;
1394 rw_packet = ofpbuf_clone_data_with_headroom(
1395 pin->packet->data, MIN(pin->send_len, pin->packet->size),
1396 offsetof(struct ofp_packet_in, data));
1399 /* Add OFPT_PACKET_IN. */
1400 opi = ofpbuf_push_zeros(rw_packet, offsetof(struct ofp_packet_in, data));
1401 opi->header.version = OFP_VERSION;
1402 opi->header.type = OFPT_PACKET_IN;
1403 opi->total_len = htons(total_len);
1404 opi->in_port = htons(pin->in_port);
1405 opi->reason = pin->reason;
1406 opi->buffer_id = htonl(pin->buffer_id);
1407 update_openflow_length(rw_packet);
1412 /* Returns a string representing the message type of 'type'. The string is the
1413 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
1414 * messages, the constant is followed by "request" or "reply",
1415 * e.g. "OFPST_AGGREGATE reply". */
1417 ofputil_msg_type_name(const struct ofputil_msg_type *type)
1422 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1423 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1424 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
1427 * The caller is responsible for freeing '*bufferp' when it is no longer
1430 * The OpenFlow header length is initially set to 'openflow_len'; if the
1431 * message is later extended, the length should be updated with
1432 * update_openflow_length() before sending.
1434 * Returns the header. */
1436 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1438 *bufferp = ofpbuf_new(openflow_len);
1439 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1442 /* Similar to make_openflow() but creates a Nicira vendor extension message
1443 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1445 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1447 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
1450 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1451 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1452 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
1455 * The caller is responsible for freeing '*bufferp' when it is no longer
1458 * The OpenFlow header length is initially set to 'openflow_len'; if the
1459 * message is later extended, the length should be updated with
1460 * update_openflow_length() before sending.
1462 * Returns the header. */
1464 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1465 struct ofpbuf **bufferp)
1467 *bufferp = ofpbuf_new(openflow_len);
1468 return put_openflow_xid(openflow_len, type, xid, *bufferp);
1471 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
1472 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1474 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1475 struct ofpbuf **bufferp)
1477 *bufferp = ofpbuf_new(openflow_len);
1478 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
1481 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1482 * with the given 'type' and an arbitrary transaction id. Allocated bytes
1483 * beyond the header, if any, are zeroed.
1485 * The OpenFlow header length is initially set to 'openflow_len'; if the
1486 * message is later extended, the length should be updated with
1487 * update_openflow_length() before sending.
1489 * Returns the header. */
1491 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1493 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
1496 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1497 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
1498 * the header, if any, are zeroed.
1500 * The OpenFlow header length is initially set to 'openflow_len'; if the
1501 * message is later extended, the length should be updated with
1502 * update_openflow_length() before sending.
1504 * Returns the header. */
1506 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1507 struct ofpbuf *buffer)
1509 struct ofp_header *oh;
1511 assert(openflow_len >= sizeof *oh);
1512 assert(openflow_len <= UINT16_MAX);
1514 oh = ofpbuf_put_uninit(buffer, openflow_len);
1515 oh->version = OFP_VERSION;
1517 oh->length = htons(openflow_len);
1519 memset(oh + 1, 0, openflow_len - sizeof *oh);
1523 /* Similar to put_openflow() but append a Nicira vendor extension message with
1524 * the specific 'subtype'. 'subtype' should be in host byte order. */
1526 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
1528 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
1531 /* Similar to put_openflow_xid() but append a Nicira vendor extension message
1532 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1534 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1535 struct ofpbuf *buffer)
1537 struct nicira_header *nxh;
1539 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
1540 nxh->vendor = htonl(NX_VENDOR_ID);
1541 nxh->subtype = htonl(subtype);
1545 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
1546 * 'buffer->size'. */
1548 update_openflow_length(struct ofpbuf *buffer)
1550 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
1551 oh->length = htons(buffer->size);
1554 /* Creates an ofp_stats_request with the given 'type' and 'body_len' bytes of
1555 * space allocated for the 'body' member. Returns the first byte of the 'body'
1558 ofputil_make_stats_request(size_t body_len, uint16_t type,
1559 struct ofpbuf **bufferp)
1561 struct ofp_stats_request *osr;
1562 osr = make_openflow((offsetof(struct ofp_stats_request, body)
1563 + body_len), OFPT_STATS_REQUEST, bufferp);
1564 osr->type = htons(type);
1565 osr->flags = htons(0);
1569 /* Creates a stats request message with Nicira as vendor and the given
1570 * 'subtype', of total length 'openflow_len'. Returns the message. */
1572 ofputil_make_nxstats_request(size_t openflow_len, uint32_t subtype,
1573 struct ofpbuf **bufferp)
1575 struct nicira_stats_msg *nsm;
1577 nsm = make_openflow(openflow_len, OFPT_STATS_REQUEST, bufferp);
1578 nsm->type = htons(OFPST_VENDOR);
1579 nsm->flags = htons(0);
1580 nsm->vendor = htonl(NX_VENDOR_ID);
1581 nsm->subtype = htonl(subtype);
1585 /* Returns the first byte of the 'body' member of the ofp_stats_request or
1586 * ofp_stats_reply in 'oh'. */
1588 ofputil_stats_body(const struct ofp_header *oh)
1590 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1591 return ((const struct ofp_stats_request *) oh)->body;
1594 /* Returns the length of the 'body' member of the ofp_stats_request or
1595 * ofp_stats_reply in 'oh'. */
1597 ofputil_stats_body_len(const struct ofp_header *oh)
1599 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1600 return ntohs(oh->length) - sizeof(struct ofp_stats_request);
1603 /* Returns the first byte of the body of the nicira_stats_msg in 'oh'. */
1605 ofputil_nxstats_body(const struct ofp_header *oh)
1607 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1608 return ((const struct nicira_stats_msg *) oh) + 1;
1611 /* Returns the length of the body of the nicira_stats_msg in 'oh'. */
1613 ofputil_nxstats_body_len(const struct ofp_header *oh)
1615 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1616 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
1620 make_flow_mod(uint16_t command, const struct cls_rule *rule,
1623 struct ofp_flow_mod *ofm;
1624 size_t size = sizeof *ofm + actions_len;
1625 struct ofpbuf *out = ofpbuf_new(size);
1626 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
1627 ofm->header.version = OFP_VERSION;
1628 ofm->header.type = OFPT_FLOW_MOD;
1629 ofm->header.length = htons(size);
1631 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
1632 ofputil_cls_rule_to_match(rule, &ofm->match);
1633 ofm->command = htons(command);
1638 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
1639 uint16_t idle_timeout, size_t actions_len)
1641 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
1642 struct ofp_flow_mod *ofm = out->data;
1643 ofm->idle_timeout = htons(idle_timeout);
1644 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
1645 ofm->buffer_id = htonl(buffer_id);
1650 make_del_flow(const struct cls_rule *rule)
1652 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
1653 struct ofp_flow_mod *ofm = out->data;
1654 ofm->out_port = htons(OFPP_NONE);
1659 make_add_simple_flow(const struct cls_rule *rule,
1660 uint32_t buffer_id, uint16_t out_port,
1661 uint16_t idle_timeout)
1663 if (out_port != OFPP_NONE) {
1664 struct ofp_action_output *oao;
1665 struct ofpbuf *buffer;
1667 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
1668 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
1669 oao->type = htons(OFPAT_OUTPUT);
1670 oao->len = htons(sizeof *oao);
1671 oao->port = htons(out_port);
1674 return make_add_flow(rule, buffer_id, idle_timeout, 0);
1679 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
1680 const struct ofpbuf *payload, int max_send_len)
1682 struct ofp_packet_in *opi;
1686 send_len = MIN(max_send_len, payload->size);
1687 buf = ofpbuf_new(sizeof *opi + send_len);
1688 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
1689 OFPT_PACKET_IN, 0, buf);
1690 opi->buffer_id = htonl(buffer_id);
1691 opi->total_len = htons(payload->size);
1692 opi->in_port = htons(in_port);
1693 opi->reason = reason;
1694 ofpbuf_put(buf, payload->data, send_len);
1695 update_openflow_length(buf);
1701 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1703 const struct ofp_action_header *actions, size_t n_actions)
1705 size_t actions_len = n_actions * sizeof *actions;
1706 struct ofp_packet_out *opo;
1707 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
1708 struct ofpbuf *out = ofpbuf_new(size);
1710 opo = ofpbuf_put_uninit(out, sizeof *opo);
1711 opo->header.version = OFP_VERSION;
1712 opo->header.type = OFPT_PACKET_OUT;
1713 opo->header.length = htons(size);
1714 opo->header.xid = htonl(0);
1715 opo->buffer_id = htonl(buffer_id);
1716 opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
1717 opo->actions_len = htons(actions_len);
1718 ofpbuf_put(out, actions, actions_len);
1720 ofpbuf_put(out, packet->data, packet->size);
1726 make_unbuffered_packet_out(const struct ofpbuf *packet,
1727 uint16_t in_port, uint16_t out_port)
1729 struct ofp_action_output action;
1730 action.type = htons(OFPAT_OUTPUT);
1731 action.len = htons(sizeof action);
1732 action.port = htons(out_port);
1733 return make_packet_out(packet, UINT32_MAX, in_port,
1734 (struct ofp_action_header *) &action, 1);
1738 make_buffered_packet_out(uint32_t buffer_id,
1739 uint16_t in_port, uint16_t out_port)
1741 if (out_port != OFPP_NONE) {
1742 struct ofp_action_output action;
1743 action.type = htons(OFPAT_OUTPUT);
1744 action.len = htons(sizeof action);
1745 action.port = htons(out_port);
1746 return make_packet_out(NULL, buffer_id, in_port,
1747 (struct ofp_action_header *) &action, 1);
1749 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
1753 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1755 make_echo_request(void)
1757 struct ofp_header *rq;
1758 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1759 rq = ofpbuf_put_uninit(out, sizeof *rq);
1760 rq->version = OFP_VERSION;
1761 rq->type = OFPT_ECHO_REQUEST;
1762 rq->length = htons(sizeof *rq);
1767 /* Creates and returns an OFPT_ECHO_REPLY message matching the
1768 * OFPT_ECHO_REQUEST message in 'rq'. */
1770 make_echo_reply(const struct ofp_header *rq)
1772 size_t size = ntohs(rq->length);
1773 struct ofpbuf *out = ofpbuf_new(size);
1774 struct ofp_header *reply = ofpbuf_put(out, rq, size);
1775 reply->type = OFPT_ECHO_REPLY;
1780 check_action_exact_len(const union ofp_action *a, unsigned int len,
1781 unsigned int required_len)
1783 if (len != required_len) {
1784 VLOG_WARN_RL(&bad_ofmsg_rl, "action %"PRIu16" has invalid length "
1785 "%"PRIu16" (must be %u)\n",
1786 ntohs(a->type), ntohs(a->header.len), required_len);
1787 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1793 check_nx_action_exact_len(const struct nx_action_header *a,
1794 unsigned int len, unsigned int required_len)
1796 if (len != required_len) {
1797 VLOG_WARN_RL(&bad_ofmsg_rl,
1798 "Nicira action %"PRIu16" has invalid length %"PRIu16" "
1800 ntohs(a->subtype), ntohs(a->len), required_len);
1801 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1806 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
1807 * that the switch will never have more than 'max_ports' ports. Returns 0 if
1808 * 'port' is valid, otherwise an ofp_mkerr() return code. */
1810 check_output_port(uint16_t port, int max_ports)
1818 case OFPP_CONTROLLER:
1823 if (port < max_ports) {
1826 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
1827 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1831 /* Checks that 'action' is a valid OFPAT_ENQUEUE action, given that the switch
1832 * will never have more than 'max_ports' ports. Returns 0 if 'port' is valid,
1833 * otherwise an ofp_mkerr() return code. */
1835 check_enqueue_action(const union ofp_action *a, unsigned int len,
1838 const struct ofp_action_enqueue *oae;
1842 error = check_action_exact_len(a, len, 16);
1847 oae = (const struct ofp_action_enqueue *) a;
1848 port = ntohs(oae->port);
1849 if (port < max_ports || port == OFPP_IN_PORT) {
1852 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown enqueue port %x", port);
1853 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1857 check_nicira_action(const union ofp_action *a, unsigned int len,
1858 const struct flow *flow)
1860 const struct nx_action_header *nah;
1865 VLOG_WARN_RL(&bad_ofmsg_rl,
1866 "Nicira vendor action only %u bytes", len);
1867 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1869 nah = (const struct nx_action_header *) a;
1871 subtype = ntohs(nah->subtype);
1872 if (subtype > TYPE_MAXIMUM(enum nx_action_subtype)) {
1873 /* This is necessary because enum nx_action_subtype may be an
1874 * 8-bit type, so the cast below throws away the top 8 bits. */
1875 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1878 switch ((enum nx_action_subtype) subtype) {
1879 case NXAST_RESUBMIT:
1880 case NXAST_SET_TUNNEL:
1881 case NXAST_DROP_SPOOFED_ARP:
1882 case NXAST_SET_QUEUE:
1883 case NXAST_POP_QUEUE:
1884 return check_nx_action_exact_len(nah, len, 16);
1886 case NXAST_REG_MOVE:
1887 error = check_nx_action_exact_len(nah, len,
1888 sizeof(struct nx_action_reg_move));
1892 return nxm_check_reg_move((const struct nx_action_reg_move *) a, flow);
1894 case NXAST_REG_LOAD:
1895 error = check_nx_action_exact_len(nah, len,
1896 sizeof(struct nx_action_reg_load));
1900 return nxm_check_reg_load((const struct nx_action_reg_load *) a, flow);
1905 case NXAST_SET_TUNNEL64:
1906 return check_nx_action_exact_len(
1907 nah, len, sizeof(struct nx_action_set_tunnel64));
1909 case NXAST_MULTIPATH:
1910 error = check_nx_action_exact_len(
1911 nah, len, sizeof(struct nx_action_multipath));
1915 return multipath_check((const struct nx_action_multipath *) a);
1917 case NXAST_AUTOPATH:
1918 error = check_nx_action_exact_len(
1919 nah, len, sizeof(struct nx_action_autopath));
1923 return autopath_check((const struct nx_action_autopath *) a);
1925 case NXAST_SNAT__OBSOLETE:
1927 VLOG_WARN_RL(&bad_ofmsg_rl,
1928 "unknown Nicira vendor action subtype %d", subtype);
1929 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1934 check_action(const union ofp_action *a, unsigned int len,
1935 const struct flow *flow, int max_ports)
1937 enum ofp_action_type type = ntohs(a->type);
1942 error = check_action_exact_len(a, len, 8);
1946 return check_output_port(ntohs(a->output.port), max_ports);
1948 case OFPAT_SET_VLAN_VID:
1949 error = check_action_exact_len(a, len, 8);
1953 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1954 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1958 case OFPAT_SET_VLAN_PCP:
1959 error = check_action_exact_len(a, len, 8);
1963 if (a->vlan_pcp.vlan_pcp & ~7) {
1964 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1968 case OFPAT_STRIP_VLAN:
1969 case OFPAT_SET_NW_SRC:
1970 case OFPAT_SET_NW_DST:
1971 case OFPAT_SET_NW_TOS:
1972 case OFPAT_SET_TP_SRC:
1973 case OFPAT_SET_TP_DST:
1974 return check_action_exact_len(a, len, 8);
1976 case OFPAT_SET_DL_SRC:
1977 case OFPAT_SET_DL_DST:
1978 return check_action_exact_len(a, len, 16);
1981 return (a->vendor.vendor == htonl(NX_VENDOR_ID)
1982 ? check_nicira_action(a, len, flow)
1983 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
1986 return check_enqueue_action(a, len, max_ports);
1989 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %d", (int) type);
1990 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
1995 validate_actions(const union ofp_action *actions, size_t n_actions,
1996 const struct flow *flow, int max_ports)
2000 for (i = 0; i < n_actions; ) {
2001 const union ofp_action *a = &actions[i];
2002 unsigned int len = ntohs(a->header.len);
2003 unsigned int n_slots = len / OFP_ACTION_ALIGN;
2004 unsigned int slots_left = &actions[n_actions] - a;
2007 if (n_slots > slots_left) {
2008 VLOG_WARN_RL(&bad_ofmsg_rl,
2009 "action requires %u slots but only %u remain",
2010 n_slots, slots_left);
2011 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
2013 VLOG_WARN_RL(&bad_ofmsg_rl, "action has invalid length 0");
2014 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
2015 } else if (len % OFP_ACTION_ALIGN) {
2016 VLOG_WARN_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
2017 "of %d", len, OFP_ACTION_ALIGN);
2018 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
2021 error = check_action(a, len, flow, max_ports);
2030 /* Returns true if 'action' outputs to 'port', false otherwise. */
2032 action_outputs_to_port(const union ofp_action *action, ovs_be16 port)
2034 switch (ntohs(action->type)) {
2036 return action->output.port == port;
2038 return ((const struct ofp_action_enqueue *) action)->port == port;
2044 /* The set of actions must either come from a trusted source or have been
2045 * previously validated with validate_actions(). */
2046 const union ofp_action *
2047 actions_first(struct actions_iterator *iter,
2048 const union ofp_action *oa, size_t n_actions)
2051 iter->end = oa + n_actions;
2052 return actions_next(iter);
2055 const union ofp_action *
2056 actions_next(struct actions_iterator *iter)
2058 if (iter->pos != iter->end) {
2059 const union ofp_action *a = iter->pos;
2060 unsigned int len = ntohs(a->header.len);
2061 iter->pos += len / OFP_ACTION_ALIGN;
2069 normalize_match(struct ofp_match *m)
2071 enum { OFPFW_NW = (OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO
2073 enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
2076 wc = ntohl(m->wildcards) & OFPFW_ALL;
2077 if (wc & OFPFW_DL_TYPE) {
2080 /* Can't sensibly match on network or transport headers if the
2081 * data link type is unknown. */
2082 wc |= OFPFW_NW | OFPFW_TP;
2083 m->nw_src = m->nw_dst = m->nw_proto = m->nw_tos = 0;
2084 m->tp_src = m->tp_dst = 0;
2085 } else if (m->dl_type == htons(ETH_TYPE_IP)) {
2086 if (wc & OFPFW_NW_PROTO) {
2089 /* Can't sensibly match on transport headers if the network
2090 * protocol is unknown. */
2092 m->tp_src = m->tp_dst = 0;
2093 } else if (m->nw_proto == IPPROTO_TCP ||
2094 m->nw_proto == IPPROTO_UDP ||
2095 m->nw_proto == IPPROTO_ICMP) {
2096 if (wc & OFPFW_TP_SRC) {
2099 if (wc & OFPFW_TP_DST) {
2103 /* Transport layer fields will always be extracted as zeros, so we
2104 * can do an exact-match on those values. */
2106 m->tp_src = m->tp_dst = 0;
2108 if (wc & OFPFW_NW_SRC_MASK) {
2109 m->nw_src &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_SRC_SHIFT);
2111 if (wc & OFPFW_NW_DST_MASK) {
2112 m->nw_dst &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_DST_SHIFT);
2114 if (wc & OFPFW_NW_TOS) {
2117 m->nw_tos &= IP_DSCP_MASK;
2119 } else if (m->dl_type == htons(ETH_TYPE_ARP)) {
2120 if (wc & OFPFW_NW_PROTO) {
2123 if (wc & OFPFW_NW_SRC_MASK) {
2124 m->nw_src &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_SRC_SHIFT);
2126 if (wc & OFPFW_NW_DST_MASK) {
2127 m->nw_dst &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_DST_SHIFT);
2129 m->tp_src = m->tp_dst = m->nw_tos = 0;
2130 } else if (m->dl_type == htons(ETH_TYPE_IPV6)) {
2131 /* Don't normalize IPv6 traffic, since OpenFlow doesn't have a
2132 * way to express it. */
2134 /* Network and transport layer fields will always be extracted as
2135 * zeros, so we can do an exact-match on those values. */
2136 wc &= ~(OFPFW_NW | OFPFW_TP);
2137 m->nw_proto = m->nw_src = m->nw_dst = m->nw_tos = 0;
2138 m->tp_src = m->tp_dst = 0;
2140 if (wc & OFPFW_DL_SRC) {
2141 memset(m->dl_src, 0, sizeof m->dl_src);
2143 if (wc & OFPFW_DL_DST) {
2144 memset(m->dl_dst, 0, sizeof m->dl_dst);
2146 m->wildcards = htonl(wc);
2149 /* Returns a string that describes 'match' in a very literal way, without
2150 * interpreting its contents except in a very basic fashion. The returned
2151 * string is intended to be fixed-length, so that it is easy to see differences
2152 * between two such strings if one is put above another. This is useful for
2153 * describing changes made by normalize_match().
2155 * The caller must free the returned string (with free()). */
2157 ofp_match_to_literal_string(const struct ofp_match *match)
2159 return xasprintf("wildcards=%#10"PRIx32" "
2160 " in_port=%5"PRId16" "
2161 " dl_src="ETH_ADDR_FMT" "
2162 " dl_dst="ETH_ADDR_FMT" "
2163 " dl_vlan=%5"PRId16" "
2164 " dl_vlan_pcp=%3"PRId8" "
2165 " dl_type=%#6"PRIx16" "
2166 " nw_tos=%#4"PRIx8" "
2167 " nw_proto=%#4"PRIx16" "
2168 " nw_src=%#10"PRIx32" "
2169 " nw_dst=%#10"PRIx32" "
2170 " tp_src=%5"PRId16" "
2172 ntohl(match->wildcards),
2173 ntohs(match->in_port),
2174 ETH_ADDR_ARGS(match->dl_src),
2175 ETH_ADDR_ARGS(match->dl_dst),
2176 ntohs(match->dl_vlan),
2178 ntohs(match->dl_type),
2181 ntohl(match->nw_src),
2182 ntohl(match->nw_dst),
2183 ntohs(match->tp_src),
2184 ntohs(match->tp_dst));
2188 vendor_code_to_id(uint8_t code)
2191 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
2193 #undef OFPUTIL_VENDOR
2200 vendor_id_to_code(uint32_t id)
2203 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case VENDOR_ID: return NAME;
2205 #undef OFPUTIL_VENDOR
2211 /* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
2212 * information taken from 'error', whose encoding must be as described in the
2213 * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
2214 * oh->xid as its transaction ID, and it will include up to the first 64 bytes
2217 * Returns NULL if 'error' is not an OpenFlow error code. */
2219 ofputil_encode_error_msg(int error, const struct ofp_header *oh)
2221 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2231 if (!is_ofp_error(error)) {
2232 /* We format 'error' with strerror() here since it seems likely to be
2233 * a system errno value. */
2234 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
2235 error, strerror(error));
2242 len = ntohs(oh->length);
2252 vendor = get_ofp_err_vendor(error);
2253 type = get_ofp_err_type(error);
2254 code = get_ofp_err_code(error);
2255 if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
2256 struct ofp_error_msg *oem;
2258 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
2259 oem->type = htons(type);
2260 oem->code = htons(code);
2262 struct ofp_error_msg *oem;
2263 struct nx_vendor_error *nve;
2266 vendor_id = vendor_code_to_id(vendor);
2267 if (vendor_id == UINT32_MAX) {
2268 VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
2273 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
2274 OFPT_ERROR, xid, &buf);
2275 oem->type = htons(NXET_VENDOR);
2276 oem->code = htons(NXVC_VENDOR_ERROR);
2278 nve = (struct nx_vendor_error *)oem->data;
2279 nve->vendor = htonl(vendor_id);
2280 nve->type = htons(type);
2281 nve->code = htons(code);
2286 ofpbuf_put(buf, data, len);
2292 /* Decodes 'oh', which should be an OpenFlow OFPT_ERROR message, and returns an
2293 * Open vSwitch internal error code in the format described in the large
2294 * comment in ofp-util.h.
2296 * If 'payload_ofs' is nonnull, on success '*payload_ofs' is set to the offset
2297 * to the payload starting from 'oh' and on failure it is set to 0. */
2299 ofputil_decode_error_msg(const struct ofp_header *oh, size_t *payload_ofs)
2301 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2303 const struct ofp_error_msg *oem;
2304 uint16_t type, code;
2311 if (oh->type != OFPT_ERROR) {
2315 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2316 oem = ofpbuf_try_pull(&b, sizeof *oem);
2321 type = ntohs(oem->type);
2322 code = ntohs(oem->code);
2323 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
2324 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
2329 vendor = vendor_id_to_code(ntohl(nve->vendor));
2331 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
2332 ntohl(nve->vendor));
2335 type = ntohs(nve->type);
2336 code = ntohs(nve->code);
2338 vendor = OFPUTIL_VENDOR_OPENFLOW;
2342 VLOG_WARN_RL(&rl, "error contains type %"PRIu16" greater than "
2343 "supported maximum value 1023", type);
2348 *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
2350 return ofp_mkerr_vendor(vendor, type, code);
2354 ofputil_format_error(struct ds *s, int error)
2356 if (is_errno(error)) {
2357 ds_put_cstr(s, strerror(error));
2359 uint16_t type = get_ofp_err_type(error);
2360 uint16_t code = get_ofp_err_code(error);
2361 const char *type_s = ofp_error_type_to_string(type);
2362 const char *code_s = ofp_error_code_to_string(type, code);
2364 ds_put_format(s, "type ");
2366 ds_put_cstr(s, type_s);
2368 ds_put_format(s, "%"PRIu16, type);
2371 ds_put_cstr(s, ", code ");
2373 ds_put_cstr(s, code_s);
2375 ds_put_format(s, "%"PRIu16, code);
2381 ofputil_error_to_string(int error)
2383 struct ds s = DS_EMPTY_INITIALIZER;
2384 ofputil_format_error(&s, error);
2385 return ds_steal_cstr(&s);
2388 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
2389 * successful, otherwise an OpenFlow error.
2391 * If successful, the first action is stored in '*actionsp' and the number of
2392 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
2393 * are stored, respectively.
2395 * This function does not check that the actions are valid (the caller should
2396 * do so, with validate_actions()). The caller is also responsible for making
2397 * sure that 'b->data' is initially aligned appropriately for "union
2400 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2401 union ofp_action **actionsp, size_t *n_actionsp)
2403 if (actions_len % OFP_ACTION_ALIGN != 0) {
2404 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2405 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
2409 *actionsp = ofpbuf_try_pull(b, actions_len);
2410 if (*actionsp == NULL) {
2411 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2412 "exceeds remaining message length (%zu)",
2413 actions_len, b->size);
2417 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
2423 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);