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 = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
142 : ntohs(match->in_port));
143 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
144 rule->flow.tp_src = match->tp_src;
145 rule->flow.tp_dst = match->tp_dst;
146 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
147 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
148 rule->flow.nw_tos = match->nw_tos;
149 rule->flow.nw_proto = match->nw_proto;
151 /* Translate VLANs. */
152 vid = match->dl_vlan & htons(VLAN_VID_MASK);
153 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
154 switch (ofpfw & (OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP)) {
155 case OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP:
156 /* Wildcard everything. */
157 rule->flow.vlan_tci = htons(0);
158 rule->wc.vlan_tci_mask = htons(0);
161 case OFPFW_DL_VLAN_PCP:
162 if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
163 /* Match only packets without 802.1Q header. */
164 rule->flow.vlan_tci = htons(0);
165 rule->wc.vlan_tci_mask = htons(0xffff);
167 /* Wildcard PCP, specific VID. */
168 rule->flow.vlan_tci = vid | htons(VLAN_CFI);
169 rule->wc.vlan_tci_mask = htons(VLAN_VID_MASK | VLAN_CFI);
174 /* Wildcard VID, specific PCP. */
175 rule->flow.vlan_tci = pcp | htons(VLAN_CFI);
176 rule->wc.vlan_tci_mask = htons(VLAN_PCP_MASK | VLAN_CFI);
180 if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
181 /* This case is odd, since we can't have a specific PCP without an
182 * 802.1Q header. However, older versions of OVS treated this as
183 * matching packets withut an 802.1Q header, so we do here too. */
184 rule->flow.vlan_tci = htons(0);
185 rule->wc.vlan_tci_mask = htons(0xffff);
187 /* Specific VID and PCP. */
188 rule->flow.vlan_tci = vid | pcp | htons(VLAN_CFI);
189 rule->wc.vlan_tci_mask = htons(0xffff);
195 cls_rule_zero_wildcarded_fields(rule);
198 /* Convert 'rule' into the OpenFlow match structure 'match'. */
200 ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match)
202 const struct flow_wildcards *wc = &rule->wc;
205 /* Figure out most OpenFlow wildcards. */
206 ofpfw = wc->wildcards & WC_INVARIANTS;
207 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
208 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
209 if (wc->wildcards & FWW_NW_TOS) {
210 ofpfw |= OFPFW_NW_TOS;
213 /* Translate VLANs. */
214 match->dl_vlan = htons(0);
215 match->dl_vlan_pcp = 0;
216 if (rule->wc.vlan_tci_mask == htons(0)) {
217 ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
218 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
219 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
220 match->dl_vlan = htons(OFP_VLAN_NONE);
222 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
223 ofpfw |= OFPFW_DL_VLAN;
225 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
228 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
229 ofpfw |= OFPFW_DL_VLAN_PCP;
231 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
235 /* Compose most of the match structure. */
236 match->wildcards = htonl(ofpfw);
237 match->in_port = htons(rule->flow.in_port == ODPP_LOCAL ? OFPP_LOCAL
238 : rule->flow.in_port);
239 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
240 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
241 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
242 match->nw_src = rule->flow.nw_src;
243 match->nw_dst = rule->flow.nw_dst;
244 match->nw_tos = rule->flow.nw_tos;
245 match->nw_proto = rule->flow.nw_proto;
246 match->tp_src = rule->flow.tp_src;
247 match->tp_dst = rule->flow.tp_dst;
248 memset(match->pad1, '\0', sizeof match->pad1);
249 memset(match->pad2, '\0', sizeof match->pad2);
252 /* Given a 'dl_type' value in the format used in struct flow, returns the
253 * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */
255 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
257 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
258 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
262 /* Given a 'dl_type' value in the format used in an OpenFlow ofp_match
263 * structure, returns the corresponding 'dl_type' value for use in struct
266 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
268 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
269 ? htons(FLOW_DL_TYPE_NONE)
273 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
277 static uint32_t next_xid = 1;
278 return htonl(next_xid++);
281 /* Basic parsing of OpenFlow messages. */
283 struct ofputil_msg_type {
284 enum ofputil_msg_code code; /* OFPUTIL_*. */
285 uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
286 const char *name; /* e.g. "OFPT_FLOW_REMOVED". */
287 unsigned int min_size; /* Minimum total message size in bytes. */
288 /* 0 if 'min_size' is the exact size that the message must be. Otherwise,
289 * the message may exceed 'min_size' by an even multiple of this value. */
290 unsigned int extra_multiple;
293 struct ofputil_msg_category {
294 const char *name; /* e.g. "OpenFlow message" */
295 const struct ofputil_msg_type *types;
297 int missing_error; /* ofp_mkerr() value for missing type. */
301 ofputil_length_ok(const struct ofputil_msg_category *cat,
302 const struct ofputil_msg_type *type,
305 switch (type->extra_multiple) {
307 if (size != type->min_size) {
308 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
309 "length %u (expected length %u)",
310 cat->name, type->name, size, type->min_size);
316 if (size < type->min_size) {
317 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
318 "length %u (expected length at least %u bytes)",
319 cat->name, type->name, size, type->min_size);
325 if (size < type->min_size
326 || (size - type->min_size) % type->extra_multiple) {
327 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
328 "length %u (must be exactly %u bytes or longer "
329 "by an integer multiple of %u bytes)",
330 cat->name, type->name, size,
331 type->min_size, type->extra_multiple);
339 ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
340 uint32_t value, unsigned int size,
341 const struct ofputil_msg_type **typep)
343 const struct ofputil_msg_type *type;
345 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
346 if (type->value == value) {
347 if (!ofputil_length_ok(cat, type, size)) {
348 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
355 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
357 return cat->missing_error;
361 ofputil_decode_vendor(const struct ofp_header *oh,
362 const struct ofputil_msg_type **typep)
364 static const struct ofputil_msg_type nxt_messages[] = {
365 { OFPUTIL_NXT_ROLE_REQUEST,
366 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
367 sizeof(struct nx_role_request), 0 },
369 { OFPUTIL_NXT_ROLE_REPLY,
370 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
371 sizeof(struct nx_role_request), 0 },
373 { OFPUTIL_NXT_SET_FLOW_FORMAT,
374 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
375 sizeof(struct nxt_set_flow_format), 0 },
377 { OFPUTIL_NXT_FLOW_MOD,
378 NXT_FLOW_MOD, "NXT_FLOW_MOD",
379 sizeof(struct nx_flow_mod), 8 },
381 { OFPUTIL_NXT_FLOW_REMOVED,
382 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
383 sizeof(struct nx_flow_removed), 8 },
386 static const struct ofputil_msg_category nxt_category = {
387 "Nicira extension message",
388 nxt_messages, ARRAY_SIZE(nxt_messages),
389 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
392 const struct ofp_vendor_header *ovh;
393 const struct nicira_header *nh;
395 ovh = (const struct ofp_vendor_header *) oh;
396 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
397 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
398 "vendor %"PRIx32, ntohl(ovh->vendor));
399 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
402 if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
403 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
404 "length %u (expected at least %zu)",
405 ntohs(ovh->header.length), sizeof(struct nicira_header));
406 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
409 nh = (const struct nicira_header *) oh;
410 return ofputil_lookup_openflow_message(&nxt_category, ntohl(nh->subtype),
411 ntohs(oh->length), typep);
415 check_nxstats_msg(const struct ofp_header *oh)
417 const struct ofp_stats_request *osr;
420 osr = (const struct ofp_stats_request *) oh;
422 memcpy(&vendor, osr->body, sizeof vendor);
423 if (vendor != htonl(NX_VENDOR_ID)) {
424 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
425 "unknown vendor %"PRIx32, ntohl(vendor));
426 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
429 if (ntohs(osr->header.length) < sizeof(struct nicira_stats_msg)) {
430 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
431 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
438 ofputil_decode_nxst_request(const struct ofp_header *oh,
439 const struct ofputil_msg_type **typep)
441 static const struct ofputil_msg_type nxst_requests[] = {
442 { OFPUTIL_NXST_FLOW_REQUEST,
443 NXST_FLOW, "NXST_FLOW request",
444 sizeof(struct nx_flow_stats_request), 8 },
446 { OFPUTIL_NXST_AGGREGATE_REQUEST,
447 NXST_AGGREGATE, "NXST_AGGREGATE request",
448 sizeof(struct nx_aggregate_stats_request), 8 },
451 static const struct ofputil_msg_category nxst_request_category = {
452 "Nicira extension statistics request",
453 nxst_requests, ARRAY_SIZE(nxst_requests),
454 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
457 const struct nicira_stats_msg *nsm;
460 error = check_nxstats_msg(oh);
465 nsm = (struct nicira_stats_msg *) oh;
466 return ofputil_lookup_openflow_message(&nxst_request_category,
468 ntohs(oh->length), typep);
472 ofputil_decode_nxst_reply(const struct ofp_header *oh,
473 const struct ofputil_msg_type **typep)
475 static const struct ofputil_msg_type nxst_replies[] = {
476 { OFPUTIL_NXST_FLOW_REPLY,
477 NXST_FLOW, "NXST_FLOW reply",
478 sizeof(struct nicira_stats_msg), 8 },
480 { OFPUTIL_NXST_AGGREGATE_REPLY,
481 NXST_AGGREGATE, "NXST_AGGREGATE reply",
482 sizeof(struct nx_aggregate_stats_reply), 0 },
485 static const struct ofputil_msg_category nxst_reply_category = {
486 "Nicira extension statistics reply",
487 nxst_replies, ARRAY_SIZE(nxst_replies),
488 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
491 const struct nicira_stats_msg *nsm;
494 error = check_nxstats_msg(oh);
499 nsm = (struct nicira_stats_msg *) oh;
500 return ofputil_lookup_openflow_message(&nxst_reply_category,
502 ntohs(oh->length), typep);
506 ofputil_decode_ofpst_request(const struct ofp_header *oh,
507 const struct ofputil_msg_type **typep)
509 enum { OSR_SIZE = sizeof(struct ofp_stats_request) };
510 static const struct ofputil_msg_type ofpst_requests[] = {
511 { OFPUTIL_OFPST_DESC_REQUEST,
512 OFPST_DESC, "OFPST_DESC request",
515 { OFPUTIL_OFPST_FLOW_REQUEST,
516 OFPST_FLOW, "OFPST_FLOW request",
517 OSR_SIZE + sizeof(struct ofp_flow_stats_request), 0 },
519 { OFPUTIL_OFPST_AGGREGATE_REQUEST,
520 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
521 OSR_SIZE + sizeof(struct ofp_aggregate_stats_request), 0 },
523 { OFPUTIL_OFPST_TABLE_REQUEST,
524 OFPST_TABLE, "OFPST_TABLE request",
527 { OFPUTIL_OFPST_PORT_REQUEST,
528 OFPST_PORT, "OFPST_PORT request",
529 OSR_SIZE + sizeof(struct ofp_port_stats_request), 0 },
531 { OFPUTIL_OFPST_QUEUE_REQUEST,
532 OFPST_QUEUE, "OFPST_QUEUE request",
533 OSR_SIZE + sizeof(struct ofp_queue_stats_request), 0 },
536 OFPST_VENDOR, "OFPST_VENDOR request",
537 OSR_SIZE + sizeof(uint32_t), 1 },
540 static const struct ofputil_msg_category ofpst_request_category = {
541 "OpenFlow statistics",
542 ofpst_requests, ARRAY_SIZE(ofpst_requests),
543 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
546 const struct ofp_stats_request *osr;
549 osr = (const struct ofp_stats_request *) oh;
550 error = ofputil_lookup_openflow_message(&ofpst_request_category,
552 ntohs(oh->length), typep);
553 if (!error && osr->type == htons(OFPST_VENDOR)) {
554 error = ofputil_decode_nxst_request(oh, typep);
560 ofputil_decode_ofpst_reply(const struct ofp_header *oh,
561 const struct ofputil_msg_type **typep)
563 enum { OSR_SIZE = sizeof(struct ofp_stats_reply) };
564 static const struct ofputil_msg_type ofpst_replies[] = {
565 { OFPUTIL_OFPST_DESC_REPLY,
566 OFPST_DESC, "OFPST_DESC reply",
567 OSR_SIZE + sizeof(struct ofp_desc_stats), 0 },
569 { OFPUTIL_OFPST_FLOW_REPLY,
570 OFPST_FLOW, "OFPST_FLOW reply",
573 { OFPUTIL_OFPST_AGGREGATE_REPLY,
574 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
575 OSR_SIZE + sizeof(struct ofp_aggregate_stats_reply), 0 },
577 { OFPUTIL_OFPST_TABLE_REPLY,
578 OFPST_TABLE, "OFPST_TABLE reply",
579 OSR_SIZE, sizeof(struct ofp_table_stats) },
581 { OFPUTIL_OFPST_PORT_REPLY,
582 OFPST_PORT, "OFPST_PORT reply",
583 OSR_SIZE, sizeof(struct ofp_port_stats) },
585 { OFPUTIL_OFPST_QUEUE_REPLY,
586 OFPST_QUEUE, "OFPST_QUEUE reply",
587 OSR_SIZE, sizeof(struct ofp_queue_stats) },
590 OFPST_VENDOR, "OFPST_VENDOR reply",
591 OSR_SIZE + sizeof(uint32_t), 1 },
594 static const struct ofputil_msg_category ofpst_reply_category = {
595 "OpenFlow statistics",
596 ofpst_replies, ARRAY_SIZE(ofpst_replies),
597 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
600 const struct ofp_stats_reply *osr = (const struct ofp_stats_reply *) oh;
603 error = ofputil_lookup_openflow_message(&ofpst_reply_category,
605 ntohs(oh->length), typep);
606 if (!error && osr->type == htons(OFPST_VENDOR)) {
607 error = ofputil_decode_nxst_reply(oh, typep);
612 /* Decodes the message type represented by 'oh'. Returns 0 if successful or
613 * an OpenFlow error code constructed with ofp_mkerr() on failure. Either
614 * way, stores in '*typep' a type structure that can be inspected with the
615 * ofputil_msg_type_*() functions.
617 * oh->length must indicate the correct length of the message (and must be at
618 * least sizeof(struct ofp_header)).
620 * Success indicates that 'oh' is at least as long as the minimum-length
621 * message of its type. */
623 ofputil_decode_msg_type(const struct ofp_header *oh,
624 const struct ofputil_msg_type **typep)
626 static const struct ofputil_msg_type ofpt_messages[] = {
627 { OFPUTIL_OFPT_HELLO,
628 OFPT_HELLO, "OFPT_HELLO",
629 sizeof(struct ofp_hello), 1 },
631 { OFPUTIL_OFPT_ERROR,
632 OFPT_ERROR, "OFPT_ERROR",
633 sizeof(struct ofp_error_msg), 1 },
635 { OFPUTIL_OFPT_ECHO_REQUEST,
636 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
637 sizeof(struct ofp_header), 1 },
639 { OFPUTIL_OFPT_ECHO_REPLY,
640 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
641 sizeof(struct ofp_header), 1 },
643 { OFPUTIL_OFPT_FEATURES_REQUEST,
644 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
645 sizeof(struct ofp_header), 0 },
647 { OFPUTIL_OFPT_FEATURES_REPLY,
648 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
649 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
651 { OFPUTIL_OFPT_GET_CONFIG_REQUEST,
652 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
653 sizeof(struct ofp_header), 0 },
655 { OFPUTIL_OFPT_GET_CONFIG_REPLY,
656 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
657 sizeof(struct ofp_switch_config), 0 },
659 { OFPUTIL_OFPT_SET_CONFIG,
660 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
661 sizeof(struct ofp_switch_config), 0 },
663 { OFPUTIL_OFPT_PACKET_IN,
664 OFPT_PACKET_IN, "OFPT_PACKET_IN",
665 offsetof(struct ofp_packet_in, data), 1 },
667 { OFPUTIL_OFPT_FLOW_REMOVED,
668 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
669 sizeof(struct ofp_flow_removed), 0 },
671 { OFPUTIL_OFPT_PORT_STATUS,
672 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
673 sizeof(struct ofp_port_status), 0 },
675 { OFPUTIL_OFPT_PACKET_OUT,
676 OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
677 sizeof(struct ofp_packet_out), 1 },
679 { OFPUTIL_OFPT_FLOW_MOD,
680 OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
681 sizeof(struct ofp_flow_mod), 1 },
683 { OFPUTIL_OFPT_PORT_MOD,
684 OFPT_PORT_MOD, "OFPT_PORT_MOD",
685 sizeof(struct ofp_port_mod), 0 },
688 OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
689 sizeof(struct ofp_stats_request), 1 },
692 OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
693 sizeof(struct ofp_stats_reply), 1 },
695 { OFPUTIL_OFPT_BARRIER_REQUEST,
696 OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
697 sizeof(struct ofp_header), 0 },
699 { OFPUTIL_OFPT_BARRIER_REPLY,
700 OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
701 sizeof(struct ofp_header), 0 },
704 OFPT_VENDOR, "OFPT_VENDOR",
705 sizeof(struct ofp_vendor_header), 1 },
708 static const struct ofputil_msg_category ofpt_category = {
710 ofpt_messages, ARRAY_SIZE(ofpt_messages),
711 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE)
716 error = ofputil_lookup_openflow_message(&ofpt_category, oh->type,
717 ntohs(oh->length), typep);
721 error = ofputil_decode_vendor(oh, typep);
724 case OFPT_STATS_REQUEST:
725 error = ofputil_decode_ofpst_request(oh, typep);
728 case OFPT_STATS_REPLY:
729 error = ofputil_decode_ofpst_reply(oh, typep);
736 static const struct ofputil_msg_type ofputil_invalid_type = {
738 0, "OFPUTIL_INVALID",
742 *typep = &ofputil_invalid_type;
747 /* Returns an OFPUTIL_* message type code for 'type'. */
748 enum ofputil_msg_code
749 ofputil_msg_type_code(const struct ofputil_msg_type *type)
757 ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
759 switch (flow_format) {
760 case NXFF_OPENFLOW10:
769 ofputil_flow_format_to_string(enum nx_flow_format flow_format)
771 switch (flow_format) {
772 case NXFF_OPENFLOW10:
782 ofputil_flow_format_from_string(const char *s)
784 return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
785 : !strcmp(s, "nxm") ? NXFF_NXM
790 regs_fully_wildcarded(const struct flow_wildcards *wc)
794 for (i = 0; i < FLOW_N_REGS; i++) {
795 if (wc->reg_masks[i] != 0) {
802 /* Returns the minimum nx_flow_format to use for sending 'rule' to a switch
803 * (e.g. to add or remove a flow). Only NXM can handle tunnel IDs, registers,
804 * or fixing the Ethernet multicast bit. Otherwise, it's better to use
805 * NXFF_OPENFLOW10 for backward compatibility. */
807 ofputil_min_flow_format(const struct cls_rule *rule)
809 const struct flow_wildcards *wc = &rule->wc;
811 /* Only NXM supports separately wildcards the Ethernet multicast bit. */
812 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
816 /* Only NXM supports matching ARP hardware addresses. */
817 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
821 /* Only NXM supports matching IPv6 traffic. */
822 if (!(wc->wildcards & FWW_DL_TYPE)
823 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
827 /* Only NXM supports matching registers. */
828 if (!regs_fully_wildcarded(wc)) {
832 /* Only NXM supports matching tun_id. */
833 if (wc->tun_id_mask != htonll(0)) {
837 /* Other formats can express this rule. */
838 return NXFF_OPENFLOW10;
841 /* Returns an OpenFlow message that can be used to set the flow format to
844 ofputil_make_set_flow_format(enum nx_flow_format flow_format)
846 struct nxt_set_flow_format *sff;
849 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
850 sff->format = htonl(flow_format);
855 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
856 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
859 * Does not validate the flow_mod actions. */
861 ofputil_decode_flow_mod(struct flow_mod *fm, const struct ofp_header *oh)
863 const struct ofputil_msg_type *type;
866 ofpbuf_use_const(&b, oh, ntohs(oh->length));
868 ofputil_decode_msg_type(oh, &type);
869 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
870 /* Standard OpenFlow flow_mod. */
871 struct ofp_match match, orig_match;
872 const struct ofp_flow_mod *ofm;
875 /* Dissect the message. */
876 ofm = ofpbuf_pull(&b, sizeof *ofm);
877 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
882 /* Normalize ofm->match. If normalization actually changes anything,
883 * then log the differences. */
885 match.pad1[0] = match.pad2[0] = 0;
887 normalize_match(&match);
888 if (memcmp(&match, &orig_match, sizeof orig_match)) {
889 if (!VLOG_DROP_INFO(&bad_ofmsg_rl)) {
890 char *old = ofp_match_to_literal_string(&orig_match);
891 char *new = ofp_match_to_literal_string(&match);
892 VLOG_INFO("normalization changed ofp_match, details:");
893 VLOG_INFO(" pre: %s", old);
894 VLOG_INFO("post: %s", new);
900 /* Translate the message. */
901 ofputil_cls_rule_from_match(&match, ntohs(ofm->priority), &fm->cr);
902 fm->cookie = ofm->cookie;
903 fm->command = ntohs(ofm->command);
904 fm->idle_timeout = ntohs(ofm->idle_timeout);
905 fm->hard_timeout = ntohs(ofm->hard_timeout);
906 fm->buffer_id = ntohl(ofm->buffer_id);
907 fm->out_port = ntohs(ofm->out_port);
908 fm->flags = ntohs(ofm->flags);
909 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
910 /* Nicira extended flow_mod. */
911 const struct nx_flow_mod *nfm;
914 /* Dissect the message. */
915 nfm = ofpbuf_pull(&b, sizeof *nfm);
916 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
921 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
926 /* Translate the message. */
927 fm->cookie = nfm->cookie;
928 fm->command = ntohs(nfm->command);
929 fm->idle_timeout = ntohs(nfm->idle_timeout);
930 fm->hard_timeout = ntohs(nfm->hard_timeout);
931 fm->buffer_id = ntohl(nfm->buffer_id);
932 fm->out_port = ntohs(nfm->out_port);
933 fm->flags = ntohs(nfm->flags);
941 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
942 * 'flow_format' and returns the message. */
944 ofputil_encode_flow_mod(const struct flow_mod *fm,
945 enum nx_flow_format flow_format)
947 size_t actions_len = fm->n_actions * sizeof *fm->actions;
950 if (flow_format == NXFF_OPENFLOW10) {
951 struct ofp_flow_mod *ofm;
953 msg = ofpbuf_new(sizeof *ofm + actions_len);
954 ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
955 ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
956 ofm->cookie = fm->cookie;
957 ofm->command = htons(fm->command);
958 ofm->idle_timeout = htons(fm->idle_timeout);
959 ofm->hard_timeout = htons(fm->hard_timeout);
960 ofm->priority = htons(fm->cr.priority);
961 ofm->buffer_id = htonl(fm->buffer_id);
962 ofm->out_port = htons(fm->out_port);
963 ofm->flags = htons(fm->flags);
964 } else if (flow_format == NXFF_NXM) {
965 struct nx_flow_mod *nfm;
968 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
969 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
970 match_len = nx_put_match(msg, &fm->cr);
973 nfm->cookie = fm->cookie;
974 nfm->command = htons(fm->command);
975 nfm->idle_timeout = htons(fm->idle_timeout);
976 nfm->hard_timeout = htons(fm->hard_timeout);
977 nfm->priority = htons(fm->cr.priority);
978 nfm->buffer_id = htonl(fm->buffer_id);
979 nfm->out_port = htons(fm->out_port);
980 nfm->flags = htons(fm->flags);
981 nfm->match_len = htons(match_len);
986 ofpbuf_put(msg, fm->actions, actions_len);
987 update_openflow_length(msg);
992 ofputil_decode_ofpst_flow_request(struct flow_stats_request *fsr,
993 const struct ofp_header *oh,
996 const struct ofp_flow_stats_request *ofsr = ofputil_stats_body(oh);
998 fsr->aggregate = aggregate;
999 ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
1000 fsr->out_port = ntohs(ofsr->out_port);
1001 fsr->table_id = ofsr->table_id;
1007 ofputil_decode_nxst_flow_request(struct flow_stats_request *fsr,
1008 const struct ofp_header *oh,
1011 const struct nx_flow_stats_request *nfsr;
1015 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1017 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1018 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match);
1023 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1026 fsr->aggregate = aggregate;
1027 fsr->out_port = ntohs(nfsr->out_port);
1028 fsr->table_id = nfsr->table_id;
1033 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1034 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1035 * successful, otherwise an OpenFlow error code. */
1037 ofputil_decode_flow_stats_request(struct flow_stats_request *fsr,
1038 const struct ofp_header *oh)
1040 const struct ofputil_msg_type *type;
1044 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1046 ofputil_decode_msg_type(oh, &type);
1047 code = ofputil_msg_type_code(type);
1049 case OFPUTIL_OFPST_FLOW_REQUEST:
1050 return ofputil_decode_ofpst_flow_request(fsr, oh, false);
1052 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1053 return ofputil_decode_ofpst_flow_request(fsr, oh, true);
1055 case OFPUTIL_NXST_FLOW_REQUEST:
1056 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1058 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1059 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1062 /* Hey, the caller lied. */
1067 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1068 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1069 * 'flow_format', and returns the message. */
1071 ofputil_encode_flow_stats_request(const struct flow_stats_request *fsr,
1072 enum nx_flow_format flow_format)
1076 if (flow_format == NXFF_OPENFLOW10) {
1077 struct ofp_flow_stats_request *ofsr;
1080 BUILD_ASSERT_DECL(sizeof(struct ofp_flow_stats_request)
1081 == sizeof(struct ofp_aggregate_stats_request));
1083 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1084 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, &msg);
1085 ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
1086 ofsr->table_id = fsr->table_id;
1087 ofsr->out_port = htons(fsr->out_port);
1088 } else if (flow_format == NXFF_NXM) {
1089 struct nx_flow_stats_request *nfsr;
1093 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1094 ofputil_make_nxstats_request(sizeof *nfsr, subtype, &msg);
1095 match_len = nx_put_match(msg, &fsr->match);
1098 nfsr->out_port = htons(fsr->out_port);
1099 nfsr->match_len = htons(match_len);
1100 nfsr->table_id = fsr->table_id;
1108 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1109 * ofputil_flow_stats in 'fs'.
1111 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1112 * OpenFlow message. Calling this function multiple times for a single 'msg'
1113 * iterates through the replies. The caller must initially leave 'msg''s layer
1114 * pointers null and not modify them between calls.
1116 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1117 * otherwise a positive errno value. */
1119 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1122 const struct ofputil_msg_type *type;
1125 ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1126 code = ofputil_msg_type_code(type);
1128 msg->l2 = msg->data;
1129 if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1130 ofpbuf_pull(msg, sizeof(struct ofp_stats_reply));
1131 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1132 ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1140 } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1141 const struct ofp_flow_stats *ofs;
1144 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1146 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1147 "bytes at end", msg->size);
1151 length = ntohs(ofs->length);
1152 if (length < sizeof *ofs) {
1153 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1154 "length %zu", length);
1158 if (ofputil_pull_actions(msg, length - sizeof *ofs,
1159 &fs->actions, &fs->n_actions)) {
1163 fs->cookie = get_32aligned_be64(&ofs->cookie);
1164 ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
1166 fs->table_id = ofs->table_id;
1167 fs->duration_sec = ntohl(ofs->duration_sec);
1168 fs->duration_nsec = ntohl(ofs->duration_nsec);
1169 fs->idle_timeout = ntohs(ofs->idle_timeout);
1170 fs->hard_timeout = ntohs(ofs->hard_timeout);
1171 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1172 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1173 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1174 const struct nx_flow_stats *nfs;
1175 size_t match_len, length;
1177 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1179 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1180 "bytes at end", msg->size);
1184 length = ntohs(nfs->length);
1185 match_len = ntohs(nfs->match_len);
1186 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1187 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1188 "claims invalid length %zu", match_len, length);
1191 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule)) {
1195 if (ofputil_pull_actions(msg,
1196 length - sizeof *nfs - ROUND_UP(match_len, 8),
1197 &fs->actions, &fs->n_actions)) {
1201 fs->cookie = nfs->cookie;
1202 fs->table_id = nfs->table_id;
1203 fs->duration_sec = ntohl(nfs->duration_sec);
1204 fs->duration_nsec = ntohl(nfs->duration_nsec);
1205 fs->idle_timeout = ntohs(nfs->idle_timeout);
1206 fs->hard_timeout = ntohs(nfs->hard_timeout);
1207 fs->packet_count = ntohll(nfs->packet_count);
1208 fs->byte_count = ntohll(nfs->byte_count);
1216 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1217 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1218 * an OpenFlow error code. */
1220 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1221 const struct ofp_header *oh)
1223 const struct ofputil_msg_type *type;
1224 enum ofputil_msg_code code;
1226 ofputil_decode_msg_type(oh, &type);
1227 code = ofputil_msg_type_code(type);
1228 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1229 const struct ofp_flow_removed *ofr;
1231 ofr = (const struct ofp_flow_removed *) oh;
1232 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
1234 fr->cookie = ofr->cookie;
1235 fr->reason = ofr->reason;
1236 fr->duration_sec = ntohl(ofr->duration_sec);
1237 fr->duration_nsec = ntohl(ofr->duration_nsec);
1238 fr->idle_timeout = ntohs(ofr->idle_timeout);
1239 fr->packet_count = ntohll(ofr->packet_count);
1240 fr->byte_count = ntohll(ofr->byte_count);
1241 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1242 struct nx_flow_removed *nfr;
1246 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1248 nfr = ofpbuf_pull(&b, sizeof *nfr);
1249 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1255 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1258 fr->cookie = nfr->cookie;
1259 fr->reason = nfr->reason;
1260 fr->duration_sec = ntohl(nfr->duration_sec);
1261 fr->duration_nsec = ntohl(nfr->duration_nsec);
1262 fr->idle_timeout = ntohs(nfr->idle_timeout);
1263 fr->packet_count = ntohll(nfr->packet_count);
1264 fr->byte_count = ntohll(nfr->byte_count);
1272 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1273 * NXT_FLOW_REMOVED message 'oh' according to 'flow_format', and returns the
1276 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1277 enum nx_flow_format flow_format)
1281 if (flow_format == NXFF_OPENFLOW10) {
1282 struct ofp_flow_removed *ofr;
1284 ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
1286 ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
1287 ofr->priority = htons(fr->rule.priority);
1288 ofr->reason = fr->reason;
1289 ofr->duration_sec = htonl(fr->duration_sec);
1290 ofr->duration_nsec = htonl(fr->duration_nsec);
1291 ofr->idle_timeout = htons(fr->idle_timeout);
1292 ofr->packet_count = htonll(fr->packet_count);
1293 ofr->byte_count = htonll(fr->byte_count);
1294 } else if (flow_format == NXFF_NXM) {
1295 struct nx_flow_removed *nfr;
1298 make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
1299 match_len = nx_put_match(msg, &fr->rule);
1302 nfr->cookie = fr->cookie;
1303 nfr->priority = htons(fr->rule.priority);
1304 nfr->reason = fr->reason;
1305 nfr->duration_sec = htonl(fr->duration_sec);
1306 nfr->duration_nsec = htonl(fr->duration_nsec);
1307 nfr->idle_timeout = htons(fr->idle_timeout);
1308 nfr->match_len = htons(match_len);
1309 nfr->packet_count = htonll(fr->packet_count);
1310 nfr->byte_count = htonll(fr->byte_count);
1318 /* Converts abstract ofputil_packet_in 'pin' into an OFPT_PACKET_IN message
1319 * and returns the message.
1321 * If 'rw_packet' is NULL, the caller takes ownership of the newly allocated
1324 * If 'rw_packet' is nonnull, then it must contain the same data as
1325 * pin->packet. 'rw_packet' is allowed to be the same ofpbuf as pin->packet.
1326 * It is modified in-place into an OFPT_PACKET_IN message according to 'pin',
1327 * and then ofputil_encode_packet_in() returns 'rw_packet'. If 'rw_packet' has
1328 * enough headroom to insert a "struct ofp_packet_in", this is more efficient
1329 * than ofputil_encode_packet_in() because it does not copy the packet
1332 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
1333 struct ofpbuf *rw_packet)
1335 int total_len = pin->packet->size;
1336 struct ofp_packet_in *opi;
1339 if (pin->send_len < rw_packet->size) {
1340 rw_packet->size = pin->send_len;
1343 rw_packet = ofpbuf_clone_data_with_headroom(
1344 pin->packet->data, MIN(pin->send_len, pin->packet->size),
1345 offsetof(struct ofp_packet_in, data));
1348 /* Add OFPT_PACKET_IN. */
1349 opi = ofpbuf_push_zeros(rw_packet, offsetof(struct ofp_packet_in, data));
1350 opi->header.version = OFP_VERSION;
1351 opi->header.type = OFPT_PACKET_IN;
1352 opi->total_len = htons(total_len);
1353 opi->in_port = htons(pin->in_port);
1354 opi->reason = pin->reason;
1355 opi->buffer_id = htonl(pin->buffer_id);
1356 update_openflow_length(rw_packet);
1361 /* Returns a string representing the message type of 'type'. The string is the
1362 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
1363 * messages, the constant is followed by "request" or "reply",
1364 * e.g. "OFPST_AGGREGATE reply". */
1366 ofputil_msg_type_name(const struct ofputil_msg_type *type)
1371 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1372 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1373 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
1376 * The caller is responsible for freeing '*bufferp' when it is no longer
1379 * The OpenFlow header length is initially set to 'openflow_len'; if the
1380 * message is later extended, the length should be updated with
1381 * update_openflow_length() before sending.
1383 * Returns the header. */
1385 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1387 *bufferp = ofpbuf_new(openflow_len);
1388 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1391 /* Similar to make_openflow() but creates a Nicira vendor extension message
1392 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1394 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1396 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
1399 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1400 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1401 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
1404 * The caller is responsible for freeing '*bufferp' when it is no longer
1407 * The OpenFlow header length is initially set to 'openflow_len'; if the
1408 * message is later extended, the length should be updated with
1409 * update_openflow_length() before sending.
1411 * Returns the header. */
1413 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1414 struct ofpbuf **bufferp)
1416 *bufferp = ofpbuf_new(openflow_len);
1417 return put_openflow_xid(openflow_len, type, xid, *bufferp);
1420 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
1421 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1423 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1424 struct ofpbuf **bufferp)
1426 *bufferp = ofpbuf_new(openflow_len);
1427 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
1430 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1431 * with the given 'type' and an arbitrary transaction id. Allocated bytes
1432 * beyond the header, if any, are zeroed.
1434 * The OpenFlow header length is initially set to 'openflow_len'; if the
1435 * message is later extended, the length should be updated with
1436 * update_openflow_length() before sending.
1438 * Returns the header. */
1440 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1442 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
1445 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1446 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
1447 * the header, if any, are zeroed.
1449 * The OpenFlow header length is initially set to 'openflow_len'; if the
1450 * message is later extended, the length should be updated with
1451 * update_openflow_length() before sending.
1453 * Returns the header. */
1455 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1456 struct ofpbuf *buffer)
1458 struct ofp_header *oh;
1460 assert(openflow_len >= sizeof *oh);
1461 assert(openflow_len <= UINT16_MAX);
1463 oh = ofpbuf_put_uninit(buffer, openflow_len);
1464 oh->version = OFP_VERSION;
1466 oh->length = htons(openflow_len);
1468 memset(oh + 1, 0, openflow_len - sizeof *oh);
1472 /* Similar to put_openflow() but append a Nicira vendor extension message with
1473 * the specific 'subtype'. 'subtype' should be in host byte order. */
1475 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
1477 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
1480 /* Similar to put_openflow_xid() but append a Nicira vendor extension message
1481 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1483 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1484 struct ofpbuf *buffer)
1486 struct nicira_header *nxh;
1488 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
1489 nxh->vendor = htonl(NX_VENDOR_ID);
1490 nxh->subtype = htonl(subtype);
1494 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
1495 * 'buffer->size'. */
1497 update_openflow_length(struct ofpbuf *buffer)
1499 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
1500 oh->length = htons(buffer->size);
1503 /* Creates an ofp_stats_request with the given 'type' and 'body_len' bytes of
1504 * space allocated for the 'body' member. Returns the first byte of the 'body'
1507 ofputil_make_stats_request(size_t body_len, uint16_t type,
1508 struct ofpbuf **bufferp)
1510 struct ofp_stats_request *osr;
1511 osr = make_openflow((offsetof(struct ofp_stats_request, body)
1512 + body_len), OFPT_STATS_REQUEST, bufferp);
1513 osr->type = htons(type);
1514 osr->flags = htons(0);
1518 /* Creates a stats request message with Nicira as vendor and the given
1519 * 'subtype', of total length 'openflow_len'. Returns the message. */
1521 ofputil_make_nxstats_request(size_t openflow_len, uint32_t subtype,
1522 struct ofpbuf **bufferp)
1524 struct nicira_stats_msg *nsm;
1526 nsm = make_openflow(openflow_len, OFPT_STATS_REQUEST, bufferp);
1527 nsm->type = htons(OFPST_VENDOR);
1528 nsm->flags = htons(0);
1529 nsm->vendor = htonl(NX_VENDOR_ID);
1530 nsm->subtype = htonl(subtype);
1534 /* Returns the first byte of the 'body' member of the ofp_stats_request or
1535 * ofp_stats_reply in 'oh'. */
1537 ofputil_stats_body(const struct ofp_header *oh)
1539 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1540 return ((const struct ofp_stats_request *) oh)->body;
1543 /* Returns the length of the 'body' member of the ofp_stats_request or
1544 * ofp_stats_reply in 'oh'. */
1546 ofputil_stats_body_len(const struct ofp_header *oh)
1548 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1549 return ntohs(oh->length) - sizeof(struct ofp_stats_request);
1552 /* Returns the first byte of the body of the nicira_stats_msg in 'oh'. */
1554 ofputil_nxstats_body(const struct ofp_header *oh)
1556 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1557 return ((const struct nicira_stats_msg *) oh) + 1;
1560 /* Returns the length of the body of the nicira_stats_msg in 'oh'. */
1562 ofputil_nxstats_body_len(const struct ofp_header *oh)
1564 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1565 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
1569 make_flow_mod(uint16_t command, const struct cls_rule *rule,
1572 struct ofp_flow_mod *ofm;
1573 size_t size = sizeof *ofm + actions_len;
1574 struct ofpbuf *out = ofpbuf_new(size);
1575 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
1576 ofm->header.version = OFP_VERSION;
1577 ofm->header.type = OFPT_FLOW_MOD;
1578 ofm->header.length = htons(size);
1580 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
1581 ofputil_cls_rule_to_match(rule, &ofm->match);
1582 ofm->command = htons(command);
1587 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
1588 uint16_t idle_timeout, size_t actions_len)
1590 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
1591 struct ofp_flow_mod *ofm = out->data;
1592 ofm->idle_timeout = htons(idle_timeout);
1593 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
1594 ofm->buffer_id = htonl(buffer_id);
1599 make_del_flow(const struct cls_rule *rule)
1601 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
1602 struct ofp_flow_mod *ofm = out->data;
1603 ofm->out_port = htons(OFPP_NONE);
1608 make_add_simple_flow(const struct cls_rule *rule,
1609 uint32_t buffer_id, uint16_t out_port,
1610 uint16_t idle_timeout)
1612 if (out_port != OFPP_NONE) {
1613 struct ofp_action_output *oao;
1614 struct ofpbuf *buffer;
1616 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
1617 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
1618 oao->type = htons(OFPAT_OUTPUT);
1619 oao->len = htons(sizeof *oao);
1620 oao->port = htons(out_port);
1623 return make_add_flow(rule, buffer_id, idle_timeout, 0);
1628 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
1629 const struct ofpbuf *payload, int max_send_len)
1631 struct ofp_packet_in *opi;
1635 send_len = MIN(max_send_len, payload->size);
1636 buf = ofpbuf_new(sizeof *opi + send_len);
1637 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
1638 OFPT_PACKET_IN, 0, buf);
1639 opi->buffer_id = htonl(buffer_id);
1640 opi->total_len = htons(payload->size);
1641 opi->in_port = htons(in_port);
1642 opi->reason = reason;
1643 ofpbuf_put(buf, payload->data, send_len);
1644 update_openflow_length(buf);
1650 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1652 const struct ofp_action_header *actions, size_t n_actions)
1654 size_t actions_len = n_actions * sizeof *actions;
1655 struct ofp_packet_out *opo;
1656 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
1657 struct ofpbuf *out = ofpbuf_new(size);
1659 opo = ofpbuf_put_uninit(out, sizeof *opo);
1660 opo->header.version = OFP_VERSION;
1661 opo->header.type = OFPT_PACKET_OUT;
1662 opo->header.length = htons(size);
1663 opo->header.xid = htonl(0);
1664 opo->buffer_id = htonl(buffer_id);
1665 opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
1666 opo->actions_len = htons(actions_len);
1667 ofpbuf_put(out, actions, actions_len);
1669 ofpbuf_put(out, packet->data, packet->size);
1675 make_unbuffered_packet_out(const struct ofpbuf *packet,
1676 uint16_t in_port, uint16_t out_port)
1678 struct ofp_action_output action;
1679 action.type = htons(OFPAT_OUTPUT);
1680 action.len = htons(sizeof action);
1681 action.port = htons(out_port);
1682 return make_packet_out(packet, UINT32_MAX, in_port,
1683 (struct ofp_action_header *) &action, 1);
1687 make_buffered_packet_out(uint32_t buffer_id,
1688 uint16_t in_port, uint16_t out_port)
1690 if (out_port != OFPP_NONE) {
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(NULL, buffer_id, in_port,
1696 (struct ofp_action_header *) &action, 1);
1698 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
1702 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1704 make_echo_request(void)
1706 struct ofp_header *rq;
1707 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1708 rq = ofpbuf_put_uninit(out, sizeof *rq);
1709 rq->version = OFP_VERSION;
1710 rq->type = OFPT_ECHO_REQUEST;
1711 rq->length = htons(sizeof *rq);
1716 /* Creates and returns an OFPT_ECHO_REPLY message matching the
1717 * OFPT_ECHO_REQUEST message in 'rq'. */
1719 make_echo_reply(const struct ofp_header *rq)
1721 size_t size = ntohs(rq->length);
1722 struct ofpbuf *out = ofpbuf_new(size);
1723 struct ofp_header *reply = ofpbuf_put(out, rq, size);
1724 reply->type = OFPT_ECHO_REPLY;
1728 /* Converts the members of 'opp' from host to network byte order. */
1730 hton_ofp_phy_port(struct ofp_phy_port *opp)
1732 opp->port_no = htons(opp->port_no);
1733 opp->config = htonl(opp->config);
1734 opp->state = htonl(opp->state);
1735 opp->curr = htonl(opp->curr);
1736 opp->advertised = htonl(opp->advertised);
1737 opp->supported = htonl(opp->supported);
1738 opp->peer = htonl(opp->peer);
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_vid.vlan_vid & ~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' (which must be in network byte
1993 * order), false otherwise. */
1995 action_outputs_to_port(const union ofp_action *action, uint16_t port)
1997 switch (ntohs(action->type)) {
1999 return action->output.port == port;
2001 return ((const struct ofp_action_enqueue *) action)->port == port;
2007 /* The set of actions must either come from a trusted source or have been
2008 * previously validated with validate_actions(). */
2009 const union ofp_action *
2010 actions_first(struct actions_iterator *iter,
2011 const union ofp_action *oa, size_t n_actions)
2014 iter->end = oa + n_actions;
2015 return actions_next(iter);
2018 const union ofp_action *
2019 actions_next(struct actions_iterator *iter)
2021 if (iter->pos != iter->end) {
2022 const union ofp_action *a = iter->pos;
2023 unsigned int len = ntohs(a->header.len);
2024 iter->pos += len / OFP_ACTION_ALIGN;
2032 normalize_match(struct ofp_match *m)
2034 enum { OFPFW_NW = (OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO
2036 enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
2039 wc = ntohl(m->wildcards) & OFPFW_ALL;
2040 if (wc & OFPFW_DL_TYPE) {
2043 /* Can't sensibly match on network or transport headers if the
2044 * data link type is unknown. */
2045 wc |= OFPFW_NW | OFPFW_TP;
2046 m->nw_src = m->nw_dst = m->nw_proto = m->nw_tos = 0;
2047 m->tp_src = m->tp_dst = 0;
2048 } else if (m->dl_type == htons(ETH_TYPE_IP)) {
2049 if (wc & OFPFW_NW_PROTO) {
2052 /* Can't sensibly match on transport headers if the network
2053 * protocol is unknown. */
2055 m->tp_src = m->tp_dst = 0;
2056 } else if (m->nw_proto == IPPROTO_TCP ||
2057 m->nw_proto == IPPROTO_UDP ||
2058 m->nw_proto == IPPROTO_ICMP) {
2059 if (wc & OFPFW_TP_SRC) {
2062 if (wc & OFPFW_TP_DST) {
2066 /* Transport layer fields will always be extracted as zeros, so we
2067 * can do an exact-match on those values. */
2069 m->tp_src = m->tp_dst = 0;
2071 if (wc & OFPFW_NW_SRC_MASK) {
2072 m->nw_src &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_SRC_SHIFT);
2074 if (wc & OFPFW_NW_DST_MASK) {
2075 m->nw_dst &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_DST_SHIFT);
2077 if (wc & OFPFW_NW_TOS) {
2080 m->nw_tos &= IP_DSCP_MASK;
2082 } else if (m->dl_type == htons(ETH_TYPE_ARP)) {
2083 if (wc & OFPFW_NW_PROTO) {
2086 if (wc & OFPFW_NW_SRC_MASK) {
2087 m->nw_src &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_SRC_SHIFT);
2089 if (wc & OFPFW_NW_DST_MASK) {
2090 m->nw_dst &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_DST_SHIFT);
2092 m->tp_src = m->tp_dst = m->nw_tos = 0;
2093 } else if (m->dl_type == htons(ETH_TYPE_IPV6)) {
2094 /* Don't normalize IPv6 traffic, since OpenFlow doesn't have a
2095 * way to express it. */
2097 /* Network and transport layer fields will always be extracted as
2098 * zeros, so we can do an exact-match on those values. */
2099 wc &= ~(OFPFW_NW | OFPFW_TP);
2100 m->nw_proto = m->nw_src = m->nw_dst = m->nw_tos = 0;
2101 m->tp_src = m->tp_dst = 0;
2103 if (wc & OFPFW_DL_SRC) {
2104 memset(m->dl_src, 0, sizeof m->dl_src);
2106 if (wc & OFPFW_DL_DST) {
2107 memset(m->dl_dst, 0, sizeof m->dl_dst);
2109 m->wildcards = htonl(wc);
2112 /* Returns a string that describes 'match' in a very literal way, without
2113 * interpreting its contents except in a very basic fashion. The returned
2114 * string is intended to be fixed-length, so that it is easy to see differences
2115 * between two such strings if one is put above another. This is useful for
2116 * describing changes made by normalize_match().
2118 * The caller must free the returned string (with free()). */
2120 ofp_match_to_literal_string(const struct ofp_match *match)
2122 return xasprintf("wildcards=%#10"PRIx32" "
2123 " in_port=%5"PRId16" "
2124 " dl_src="ETH_ADDR_FMT" "
2125 " dl_dst="ETH_ADDR_FMT" "
2126 " dl_vlan=%5"PRId16" "
2127 " dl_vlan_pcp=%3"PRId8" "
2128 " dl_type=%#6"PRIx16" "
2129 " nw_tos=%#4"PRIx8" "
2130 " nw_proto=%#4"PRIx16" "
2131 " nw_src=%#10"PRIx32" "
2132 " nw_dst=%#10"PRIx32" "
2133 " tp_src=%5"PRId16" "
2135 ntohl(match->wildcards),
2136 ntohs(match->in_port),
2137 ETH_ADDR_ARGS(match->dl_src),
2138 ETH_ADDR_ARGS(match->dl_dst),
2139 ntohs(match->dl_vlan),
2141 ntohs(match->dl_type),
2144 ntohl(match->nw_src),
2145 ntohl(match->nw_dst),
2146 ntohs(match->tp_src),
2147 ntohs(match->tp_dst));
2151 vendor_code_to_id(uint8_t code)
2154 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
2156 #undef OFPUTIL_VENDOR
2163 vendor_id_to_code(uint32_t id)
2166 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case VENDOR_ID: return NAME;
2168 #undef OFPUTIL_VENDOR
2174 /* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
2175 * information taken from 'error', whose encoding must be as described in the
2176 * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
2177 * oh->xid as its transaction ID, and it will include up to the first 64 bytes
2180 * Returns NULL if 'error' is not an OpenFlow error code. */
2182 ofputil_encode_error_msg(int error, const struct ofp_header *oh)
2184 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2194 if (!is_ofp_error(error)) {
2195 /* We format 'error' with strerror() here since it seems likely to be
2196 * a system errno value. */
2197 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
2198 error, strerror(error));
2205 len = ntohs(oh->length);
2215 vendor = get_ofp_err_vendor(error);
2216 type = get_ofp_err_type(error);
2217 code = get_ofp_err_code(error);
2218 if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
2219 struct ofp_error_msg *oem;
2221 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
2222 oem->type = htons(type);
2223 oem->code = htons(code);
2225 struct ofp_error_msg *oem;
2226 struct nx_vendor_error *nve;
2229 vendor_id = vendor_code_to_id(vendor);
2230 if (vendor_id == UINT32_MAX) {
2231 VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
2236 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
2237 OFPT_ERROR, xid, &buf);
2238 oem->type = htons(NXET_VENDOR);
2239 oem->code = htons(NXVC_VENDOR_ERROR);
2241 nve = (struct nx_vendor_error *)oem->data;
2242 nve->vendor = htonl(vendor_id);
2243 nve->type = htons(type);
2244 nve->code = htons(code);
2249 ofpbuf_put(buf, data, len);
2255 /* Decodes 'oh', which should be an OpenFlow OFPT_ERROR message, and returns an
2256 * Open vSwitch internal error code in the format described in the large
2257 * comment in ofp-util.h.
2259 * If 'payload_ofs' is nonnull, on success '*payload_ofs' is set to the offset
2260 * to the payload starting from 'oh' and on failure it is set to 0. */
2262 ofputil_decode_error_msg(const struct ofp_header *oh, size_t *payload_ofs)
2264 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2266 const struct ofp_error_msg *oem;
2267 uint16_t type, code;
2274 if (oh->type != OFPT_ERROR) {
2278 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2279 oem = ofpbuf_try_pull(&b, sizeof *oem);
2284 type = ntohs(oem->type);
2285 code = ntohs(oem->code);
2286 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
2287 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
2292 vendor = vendor_id_to_code(ntohl(nve->vendor));
2294 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
2295 ntohl(nve->vendor));
2298 type = ntohs(nve->type);
2299 code = ntohs(nve->code);
2301 vendor = OFPUTIL_VENDOR_OPENFLOW;
2305 VLOG_WARN_RL(&rl, "error contains type %"PRIu16" greater than "
2306 "supported maximum value 1023", type);
2311 *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
2313 return ofp_mkerr_vendor(vendor, type, code);
2317 ofputil_format_error(struct ds *s, int error)
2319 if (is_errno(error)) {
2320 ds_put_cstr(s, strerror(error));
2322 uint16_t type = get_ofp_err_type(error);
2323 uint16_t code = get_ofp_err_code(error);
2324 const char *type_s = ofp_error_type_to_string(type);
2325 const char *code_s = ofp_error_code_to_string(type, code);
2327 ds_put_format(s, "type ");
2329 ds_put_cstr(s, type_s);
2331 ds_put_format(s, "%"PRIu16, type);
2334 ds_put_cstr(s, ", code ");
2336 ds_put_cstr(s, code_s);
2338 ds_put_format(s, "%"PRIu16, code);
2344 ofputil_error_to_string(int error)
2346 struct ds s = DS_EMPTY_INITIALIZER;
2347 ofputil_format_error(&s, error);
2348 return ds_steal_cstr(&s);
2351 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
2352 * successful, otherwise an OpenFlow error.
2354 * If successful, the first action is stored in '*actionsp' and the number of
2355 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
2356 * are stored, respectively.
2358 * This function does not check that the actions are valid (the caller should
2359 * do so, with validate_actions()). The caller is also responsible for making
2360 * sure that 'b->data' is initially aligned appropriately for "union
2363 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2364 union ofp_action **actionsp, size_t *n_actionsp)
2366 if (actions_len % OFP_ACTION_ALIGN != 0) {
2367 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2368 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
2372 *actionsp = ofpbuf_try_pull(b, actions_len);
2373 if (*actionsp == NULL) {
2374 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2375 "exceeds remaining message length (%zu)",
2376 actions_len, b->size);
2380 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
2386 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);