2 * Copyright (c) 2008, 2009, 2010 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"
21 #include "byte-order.h"
22 #include "classifier.h"
23 #include "multipath.h"
29 #include "type-props.h"
32 VLOG_DEFINE_THIS_MODULE(ofp_util);
34 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
35 * in the peer and so there's not much point in showing a lot of them. */
36 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
38 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
39 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
42 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
43 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
44 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
45 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
48 ofputil_wcbits_to_netmask(int wcbits)
51 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
54 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
55 * that it wildcards. 'netmask' must be a CIDR netmask (see ip_is_cidr()). */
57 ofputil_netmask_to_wcbits(ovs_be32 netmask)
59 assert(ip_is_cidr(netmask));
61 return netmask == htonl(0) ? 32 : __builtin_ctz(ntohl(netmask));
65 for (wcbits = 32; netmask; wcbits--) {
66 netmask &= netmask - 1;
73 /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
75 #define WC_INVARIANT_LIST \
76 WC_INVARIANT_BIT(IN_PORT) \
77 WC_INVARIANT_BIT(DL_SRC) \
78 WC_INVARIANT_BIT(DL_DST) \
79 WC_INVARIANT_BIT(DL_TYPE) \
80 WC_INVARIANT_BIT(NW_PROTO) \
81 WC_INVARIANT_BIT(TP_SRC) \
82 WC_INVARIANT_BIT(TP_DST)
84 /* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
85 * actually have the same names and values. */
86 #define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME);
88 #undef WC_INVARIANT_BIT
90 /* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
94 #define WC_INVARIANT_BIT(NAME) | FWW_##NAME
96 #undef WC_INVARIANT_BIT
99 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
102 * 'flow_format' must either NXFF_OPENFLOW10 or NXFF_TUN_ID_FROM_COOKIE. In
103 * the latter case only, 'flow''s tun_id field will be taken from the high bits
104 * of 'cookie', if 'match''s wildcards do not indicate that tun_id is
107 ofputil_cls_rule_from_match(const struct ofp_match *match,
108 unsigned int priority,
109 enum nx_flow_format flow_format,
110 ovs_be64 cookie, struct cls_rule *rule)
112 struct flow_wildcards *wc = &rule->wc;
116 /* Initialize rule->priority. */
117 ofpfw = ntohl(match->wildcards);
118 ofpfw &= flow_format == NXFF_TUN_ID_FROM_COOKIE ? OVSFW_ALL : OFPFW_ALL;
119 rule->priority = !ofpfw ? UINT16_MAX : priority;
121 /* Initialize most of rule->wc. */
122 flow_wildcards_init_catchall(wc);
123 wc->wildcards = ofpfw & WC_INVARIANTS;
124 if (ofpfw & OFPFW_NW_TOS) {
125 wc->wildcards |= FWW_NW_TOS;
127 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
128 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
130 if (flow_format == NXFF_TUN_ID_FROM_COOKIE && !(ofpfw & NXFW_TUN_ID)) {
131 rule->flow.tun_id = htonll(ntohll(cookie) >> 32);
133 wc->wildcards |= FWW_TUN_ID;
134 rule->flow.tun_id = htonll(0);
137 if (ofpfw & OFPFW_DL_DST) {
138 /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
139 * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST
140 * and FWW_ETH_MCAST. */
141 wc->wildcards |= FWW_ETH_MCAST;
144 /* Initialize most of rule->flow. */
145 rule->flow.nw_src = match->nw_src;
146 rule->flow.nw_dst = match->nw_dst;
147 rule->flow.in_port = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
148 : ntohs(match->in_port));
149 rule->flow.dl_type = match->dl_type;
150 rule->flow.tp_src = match->tp_src;
151 rule->flow.tp_dst = match->tp_dst;
152 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
153 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
154 rule->flow.nw_tos = match->nw_tos;
155 rule->flow.nw_proto = match->nw_proto;
157 /* Translate VLANs. */
158 vid = match->dl_vlan & htons(VLAN_VID_MASK);
159 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
160 switch (ofpfw & (OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP)) {
161 case OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP:
162 /* Wildcard everything. */
163 rule->flow.vlan_tci = htons(0);
164 rule->wc.vlan_tci_mask = htons(0);
167 case OFPFW_DL_VLAN_PCP:
168 if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
169 /* Match only packets without 802.1Q header. */
170 rule->flow.vlan_tci = htons(0);
171 rule->wc.vlan_tci_mask = htons(0xffff);
173 /* Wildcard PCP, specific VID. */
174 rule->flow.vlan_tci = vid | htons(VLAN_CFI);
175 rule->wc.vlan_tci_mask = htons(VLAN_VID_MASK | VLAN_CFI);
180 /* Wildcard VID, specific PCP. */
181 rule->flow.vlan_tci = pcp | htons(VLAN_CFI);
182 rule->wc.vlan_tci_mask = htons(VLAN_PCP_MASK | VLAN_CFI);
186 if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
187 /* This case is odd, since we can't have a specific PCP without an
188 * 802.1Q header. However, older versions of OVS treated this as
189 * matching packets withut an 802.1Q header, so we do here too. */
190 rule->flow.vlan_tci = htons(0);
191 rule->wc.vlan_tci_mask = htons(0xffff);
193 /* Specific VID and PCP. */
194 rule->flow.vlan_tci = vid | pcp | htons(VLAN_CFI);
195 rule->wc.vlan_tci_mask = htons(0xffff);
201 cls_rule_zero_wildcarded_fields(rule);
204 /* Convert 'rule' into the OpenFlow match structure 'match'. 'flow_format'
205 * must either NXFF_OPENFLOW10 or NXFF_TUN_ID_FROM_COOKIE.
207 * The NXFF_TUN_ID_FROM_COOKIE flow format requires modifying the flow cookie.
208 * This function can help with that, if 'cookie_out' is nonnull. For
209 * NXFF_OPENFLOW10, or if the tunnel ID is wildcarded, 'cookie_in' will be
210 * copied directly to '*cookie_out'. For NXFF_TUN_ID_FROM_COOKIE when tunnel
211 * ID is matched, 'cookie_in' will be modified appropriately before setting
215 ofputil_cls_rule_to_match(const struct cls_rule *rule,
216 enum nx_flow_format flow_format,
217 struct ofp_match *match,
218 ovs_be64 cookie_in, ovs_be64 *cookie_out)
220 const struct flow_wildcards *wc = &rule->wc;
223 /* Figure out most OpenFlow wildcards. */
224 ofpfw = wc->wildcards & WC_INVARIANTS;
225 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
226 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
227 if (wc->wildcards & FWW_NW_TOS) {
228 ofpfw |= OFPFW_NW_TOS;
232 if (flow_format == NXFF_TUN_ID_FROM_COOKIE) {
233 if (wc->wildcards & FWW_TUN_ID) {
234 ofpfw |= NXFW_TUN_ID;
236 uint32_t cookie_lo = ntohll(cookie_in);
237 uint32_t cookie_hi = ntohll(rule->flow.tun_id);
238 cookie_in = htonll(cookie_lo | ((uint64_t) cookie_hi << 32));
242 *cookie_out = cookie_in;
245 /* Translate VLANs. */
246 match->dl_vlan = htons(0);
247 match->dl_vlan_pcp = 0;
248 if (rule->wc.vlan_tci_mask == htons(0)) {
249 ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
250 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
251 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
252 match->dl_vlan = htons(OFP_VLAN_NONE);
254 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
255 ofpfw |= OFPFW_DL_VLAN;
257 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
260 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
261 ofpfw |= OFPFW_DL_VLAN_PCP;
263 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
267 /* Compose most of the match structure. */
268 match->wildcards = htonl(ofpfw);
269 match->in_port = htons(rule->flow.in_port == ODPP_LOCAL ? OFPP_LOCAL
270 : rule->flow.in_port);
271 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
272 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
273 match->dl_type = rule->flow.dl_type;
274 match->nw_src = rule->flow.nw_src;
275 match->nw_dst = rule->flow.nw_dst;
276 match->nw_tos = rule->flow.nw_tos;
277 match->nw_proto = rule->flow.nw_proto;
278 match->tp_src = rule->flow.tp_src;
279 match->tp_dst = rule->flow.tp_dst;
280 memset(match->pad1, '\0', sizeof match->pad1);
281 memset(match->pad2, '\0', sizeof match->pad2);
284 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
288 static uint32_t next_xid = 1;
289 return htonl(next_xid++);
292 /* Basic parsing of OpenFlow messages. */
294 struct ofputil_msg_type {
295 enum ofputil_msg_code code; /* OFPUTIL_*. */
296 uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
297 const char *name; /* e.g. "OFPT_FLOW_REMOVED". */
298 unsigned int min_size; /* Minimum total message size in bytes. */
299 /* 0 if 'min_size' is the exact size that the message must be. Otherwise,
300 * the message may exceed 'min_size' by an even multiple of this value. */
301 unsigned int extra_multiple;
304 struct ofputil_msg_category {
305 const char *name; /* e.g. "OpenFlow message" */
306 const struct ofputil_msg_type *types;
308 int missing_error; /* ofp_mkerr() value for missing type. */
312 ofputil_length_ok(const struct ofputil_msg_category *cat,
313 const struct ofputil_msg_type *type,
316 switch (type->extra_multiple) {
318 if (size != type->min_size) {
319 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
320 "length %u (expected length %u)",
321 cat->name, type->name, size, type->min_size);
327 if (size < type->min_size) {
328 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
329 "length %u (expected length at least %u bytes)",
330 cat->name, type->name, size, type->min_size);
336 if (size < type->min_size
337 || (size - type->min_size) % type->extra_multiple) {
338 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
339 "length %u (must be exactly %u bytes or longer "
340 "by an integer multiple of %u bytes)",
341 cat->name, type->name, size,
342 type->min_size, type->extra_multiple);
350 ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
351 uint32_t value, unsigned int size,
352 const struct ofputil_msg_type **typep)
354 const struct ofputil_msg_type *type;
356 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
357 if (type->value == value) {
358 if (!ofputil_length_ok(cat, type, size)) {
359 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
366 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
368 return cat->missing_error;
372 ofputil_decode_vendor(const struct ofp_header *oh,
373 const struct ofputil_msg_type **typep)
375 static const struct ofputil_msg_type nxt_messages[] = {
376 { OFPUTIL_NXT_STATUS_REQUEST,
377 NXT_STATUS_REQUEST, "NXT_STATUS_REQUEST",
378 sizeof(struct nicira_header), 1 },
380 { OFPUTIL_NXT_STATUS_REPLY,
381 NXT_STATUS_REPLY, "NXT_STATUS_REPLY",
382 sizeof(struct nicira_header), 1 },
384 { OFPUTIL_NXT_TUN_ID_FROM_COOKIE,
385 NXT_TUN_ID_FROM_COOKIE, "NXT_TUN_ID_FROM_COOKIE",
386 sizeof(struct nxt_tun_id_cookie), 0 },
388 { OFPUTIL_NXT_ROLE_REQUEST,
389 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
390 sizeof(struct nx_role_request), 0 },
392 { OFPUTIL_NXT_ROLE_REPLY,
393 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
394 sizeof(struct nx_role_request), 0 },
396 { OFPUTIL_NXT_SET_FLOW_FORMAT,
397 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
398 sizeof(struct nxt_set_flow_format), 0 },
400 { OFPUTIL_NXT_FLOW_MOD,
401 NXT_FLOW_MOD, "NXT_FLOW_MOD",
402 sizeof(struct nx_flow_mod), 8 },
404 { OFPUTIL_NXT_FLOW_REMOVED,
405 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
406 sizeof(struct nx_flow_removed), 8 },
409 static const struct ofputil_msg_category nxt_category = {
410 "Nicira extension message",
411 nxt_messages, ARRAY_SIZE(nxt_messages),
412 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
415 const struct ofp_vendor_header *ovh;
416 const struct nicira_header *nh;
418 ovh = (const struct ofp_vendor_header *) oh;
419 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
420 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
421 "vendor %"PRIx32, ntohl(ovh->vendor));
422 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
425 if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
426 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
427 "length %u (expected at least %zu)",
428 ntohs(ovh->header.length), sizeof(struct nicira_header));
429 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
432 nh = (const struct nicira_header *) oh;
433 return ofputil_lookup_openflow_message(&nxt_category, ntohl(nh->subtype),
434 ntohs(oh->length), typep);
438 check_nxstats_msg(const struct ofp_header *oh)
440 const struct ofp_stats_request *osr;
443 osr = (const struct ofp_stats_request *) oh;
445 memcpy(&vendor, osr->body, sizeof vendor);
446 if (vendor != htonl(NX_VENDOR_ID)) {
447 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
448 "unknown vendor %"PRIx32, ntohl(vendor));
449 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
452 if (ntohs(osr->header.length) < sizeof(struct nicira_stats_msg)) {
453 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
454 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
461 ofputil_decode_nxst_request(const struct ofp_header *oh,
462 const struct ofputil_msg_type **typep)
464 static const struct ofputil_msg_type nxst_requests[] = {
465 { OFPUTIL_NXST_FLOW_REQUEST,
466 NXST_FLOW, "NXST_FLOW request",
467 sizeof(struct nx_flow_stats_request), 8 },
469 { OFPUTIL_NXST_AGGREGATE_REQUEST,
470 NXST_AGGREGATE, "NXST_AGGREGATE request",
471 sizeof(struct nx_aggregate_stats_request), 8 },
474 static const struct ofputil_msg_category nxst_request_category = {
475 "Nicira extension statistics request",
476 nxst_requests, ARRAY_SIZE(nxst_requests),
477 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
480 const struct nicira_stats_msg *nsm;
483 error = check_nxstats_msg(oh);
488 nsm = (struct nicira_stats_msg *) oh;
489 return ofputil_lookup_openflow_message(&nxst_request_category,
491 ntohs(oh->length), typep);
495 ofputil_decode_nxst_reply(const struct ofp_header *oh,
496 const struct ofputil_msg_type **typep)
498 static const struct ofputil_msg_type nxst_replies[] = {
499 { OFPUTIL_NXST_FLOW_REPLY,
500 NXST_FLOW, "NXST_FLOW reply",
501 sizeof(struct nicira_stats_msg), 8 },
503 { OFPUTIL_NXST_AGGREGATE_REPLY,
504 NXST_AGGREGATE, "NXST_AGGREGATE reply",
505 sizeof(struct nx_aggregate_stats_reply), 0 },
508 static const struct ofputil_msg_category nxst_reply_category = {
509 "Nicira extension statistics reply",
510 nxst_replies, ARRAY_SIZE(nxst_replies),
511 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
514 const struct nicira_stats_msg *nsm;
517 error = check_nxstats_msg(oh);
522 nsm = (struct nicira_stats_msg *) oh;
523 return ofputil_lookup_openflow_message(&nxst_reply_category,
525 ntohs(oh->length), typep);
529 ofputil_decode_ofpst_request(const struct ofp_header *oh,
530 const struct ofputil_msg_type **typep)
532 enum { OSR_SIZE = sizeof(struct ofp_stats_request) };
533 static const struct ofputil_msg_type ofpst_requests[] = {
534 { OFPUTIL_OFPST_DESC_REQUEST,
535 OFPST_DESC, "OFPST_DESC request",
538 { OFPUTIL_OFPST_FLOW_REQUEST,
539 OFPST_FLOW, "OFPST_FLOW request",
540 OSR_SIZE + sizeof(struct ofp_flow_stats_request), 0 },
542 { OFPUTIL_OFPST_AGGREGATE_REQUEST,
543 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
544 OSR_SIZE + sizeof(struct ofp_aggregate_stats_request), 0 },
546 { OFPUTIL_OFPST_TABLE_REQUEST,
547 OFPST_TABLE, "OFPST_TABLE request",
550 { OFPUTIL_OFPST_PORT_REQUEST,
551 OFPST_PORT, "OFPST_PORT request",
552 OSR_SIZE + sizeof(struct ofp_port_stats_request), 0 },
554 { OFPUTIL_OFPST_QUEUE_REQUEST,
555 OFPST_QUEUE, "OFPST_QUEUE request",
556 OSR_SIZE + sizeof(struct ofp_queue_stats_request), 0 },
559 OFPST_VENDOR, "OFPST_VENDOR request",
560 OSR_SIZE + sizeof(uint32_t), 1 },
563 static const struct ofputil_msg_category ofpst_request_category = {
564 "OpenFlow statistics",
565 ofpst_requests, ARRAY_SIZE(ofpst_requests),
566 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
569 const struct ofp_stats_request *osr;
572 osr = (const struct ofp_stats_request *) oh;
573 error = ofputil_lookup_openflow_message(&ofpst_request_category,
575 ntohs(oh->length), typep);
576 if (!error && osr->type == htons(OFPST_VENDOR)) {
577 error = ofputil_decode_nxst_request(oh, typep);
583 ofputil_decode_ofpst_reply(const struct ofp_header *oh,
584 const struct ofputil_msg_type **typep)
586 enum { OSR_SIZE = sizeof(struct ofp_stats_reply) };
587 static const struct ofputil_msg_type ofpst_replies[] = {
588 { OFPUTIL_OFPST_DESC_REPLY,
589 OFPST_DESC, "OFPST_DESC reply",
590 OSR_SIZE + sizeof(struct ofp_desc_stats), 0 },
592 { OFPUTIL_OFPST_FLOW_REPLY,
593 OFPST_FLOW, "OFPST_FLOW reply",
596 { OFPUTIL_OFPST_AGGREGATE_REPLY,
597 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
598 OSR_SIZE + sizeof(struct ofp_aggregate_stats_reply), 0 },
600 { OFPUTIL_OFPST_TABLE_REPLY,
601 OFPST_TABLE, "OFPST_TABLE reply",
602 OSR_SIZE, sizeof(struct ofp_table_stats) },
604 { OFPUTIL_OFPST_PORT_REPLY,
605 OFPST_PORT, "OFPST_PORT reply",
606 OSR_SIZE, sizeof(struct ofp_port_stats) },
608 { OFPUTIL_OFPST_QUEUE_REPLY,
609 OFPST_QUEUE, "OFPST_QUEUE reply",
610 OSR_SIZE, sizeof(struct ofp_queue_stats) },
613 OFPST_VENDOR, "OFPST_VENDOR reply",
614 OSR_SIZE + sizeof(uint32_t), 1 },
617 static const struct ofputil_msg_category ofpst_reply_category = {
618 "OpenFlow statistics",
619 ofpst_replies, ARRAY_SIZE(ofpst_replies),
620 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
623 const struct ofp_stats_reply *osr = (const struct ofp_stats_reply *) oh;
626 error = ofputil_lookup_openflow_message(&ofpst_reply_category,
628 ntohs(oh->length), typep);
629 if (!error && osr->type == htons(OFPST_VENDOR)) {
630 error = ofputil_decode_nxst_reply(oh, typep);
635 /* Decodes the message type represented by 'oh'. Returns 0 if successful or
636 * an OpenFlow error code constructed with ofp_mkerr() on failure. Either
637 * way, stores in '*typep' a type structure that can be inspected with the
638 * ofputil_msg_type_*() functions.
640 * oh->length must indicate the correct length of the message (and must be at
641 * least sizeof(struct ofp_header)).
643 * Success indicates that 'oh' is at least as long as the minimum-length
644 * message of its type. */
646 ofputil_decode_msg_type(const struct ofp_header *oh,
647 const struct ofputil_msg_type **typep)
649 static const struct ofputil_msg_type ofpt_messages[] = {
650 { OFPUTIL_OFPT_HELLO,
651 OFPT_HELLO, "OFPT_HELLO",
652 sizeof(struct ofp_hello), 1 },
654 { OFPUTIL_OFPT_ERROR,
655 OFPT_ERROR, "OFPT_ERROR",
656 sizeof(struct ofp_error_msg), 1 },
658 { OFPUTIL_OFPT_ECHO_REQUEST,
659 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
660 sizeof(struct ofp_header), 1 },
662 { OFPUTIL_OFPT_ECHO_REPLY,
663 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
664 sizeof(struct ofp_header), 1 },
666 { OFPUTIL_OFPT_FEATURES_REQUEST,
667 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
668 sizeof(struct ofp_header), 0 },
670 { OFPUTIL_OFPT_FEATURES_REPLY,
671 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
672 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
674 { OFPUTIL_OFPT_GET_CONFIG_REQUEST,
675 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
676 sizeof(struct ofp_header), 0 },
678 { OFPUTIL_OFPT_GET_CONFIG_REPLY,
679 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
680 sizeof(struct ofp_switch_config), 0 },
682 { OFPUTIL_OFPT_SET_CONFIG,
683 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
684 sizeof(struct ofp_switch_config), 0 },
686 { OFPUTIL_OFPT_PACKET_IN,
687 OFPT_PACKET_IN, "OFPT_PACKET_IN",
688 offsetof(struct ofp_packet_in, data), 1 },
690 { OFPUTIL_OFPT_FLOW_REMOVED,
691 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
692 sizeof(struct ofp_flow_removed), 0 },
694 { OFPUTIL_OFPT_PORT_STATUS,
695 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
696 sizeof(struct ofp_port_status), 0 },
698 { OFPUTIL_OFPT_PACKET_OUT,
699 OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
700 sizeof(struct ofp_packet_out), 1 },
702 { OFPUTIL_OFPT_FLOW_MOD,
703 OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
704 sizeof(struct ofp_flow_mod), 1 },
706 { OFPUTIL_OFPT_PORT_MOD,
707 OFPT_PORT_MOD, "OFPT_PORT_MOD",
708 sizeof(struct ofp_port_mod), 0 },
711 OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
712 sizeof(struct ofp_stats_request), 1 },
715 OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
716 sizeof(struct ofp_stats_reply), 1 },
718 { OFPUTIL_OFPT_BARRIER_REQUEST,
719 OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
720 sizeof(struct ofp_header), 0 },
722 { OFPUTIL_OFPT_BARRIER_REPLY,
723 OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
724 sizeof(struct ofp_header), 0 },
727 OFPT_VENDOR, "OFPT_VENDOR",
728 sizeof(struct ofp_vendor_header), 1 },
731 static const struct ofputil_msg_category ofpt_category = {
733 ofpt_messages, ARRAY_SIZE(ofpt_messages),
734 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE)
739 error = ofputil_lookup_openflow_message(&ofpt_category, oh->type,
740 ntohs(oh->length), typep);
744 error = ofputil_decode_vendor(oh, typep);
747 case OFPT_STATS_REQUEST:
748 error = ofputil_decode_ofpst_request(oh, typep);
751 case OFPT_STATS_REPLY:
752 error = ofputil_decode_ofpst_reply(oh, typep);
759 static const struct ofputil_msg_type ofputil_invalid_type = {
761 0, "OFPUTIL_INVALID",
765 *typep = &ofputil_invalid_type;
770 /* Returns an OFPUTIL_* message type code for 'type'. */
771 enum ofputil_msg_code
772 ofputil_msg_type_code(const struct ofputil_msg_type *type)
780 ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
782 switch (flow_format) {
783 case NXFF_OPENFLOW10:
784 case NXFF_TUN_ID_FROM_COOKIE:
793 ofputil_flow_format_to_string(enum nx_flow_format flow_format)
795 switch (flow_format) {
796 case NXFF_OPENFLOW10:
798 case NXFF_TUN_ID_FROM_COOKIE:
799 return "tun_id_from_cookie";
808 ofputil_flow_format_from_string(const char *s)
810 return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
811 : !strcmp(s, "tun_id_from_cookie") ? NXFF_TUN_ID_FROM_COOKIE
812 : !strcmp(s, "nxm") ? NXFF_NXM
817 regs_fully_wildcarded(const struct flow_wildcards *wc)
821 for (i = 0; i < FLOW_N_REGS; i++) {
822 if (wc->reg_masks[i] != 0) {
829 /* Returns the minimum nx_flow_format to use for sending 'rule' to a switch
830 * (e.g. to add or remove a flow). 'cookie_support' should be true if the
831 * command to be sent includes a flow cookie (as OFPT_FLOW_MOD does, for
832 * example) or false if the command does not (OFPST_FLOW and OFPST_AGGREGATE do
833 * not, for example). If 'cookie_support' is true, then 'cookie' should be the
834 * cookie to be sent; otherwise its value is ignored.
836 * The "best" flow format is chosen on this basis:
838 * - It must be capable of expressing the rule. NXFF_OPENFLOW10 flows can't
839 * handle tunnel IDs. NXFF_TUN_ID_FROM_COOKIE flows can't handle registers
840 * or fixing the Ethernet multicast bit, and can't handle tunnel IDs that
841 * conflict with the high 32 bits of the cookie or commands that don't
844 * - Otherwise, the chosen format should be as backward compatible as
845 * possible. (NXFF_OPENFLOW10 is more backward compatible than
846 * NXFF_TUN_ID_FROM_COOKIE, which is more backward compatible than
850 ofputil_min_flow_format(const struct cls_rule *rule, bool cookie_support,
853 const struct flow_wildcards *wc = &rule->wc;
854 ovs_be32 cookie_hi = htonl(ntohll(cookie) >> 32);
856 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)
857 || !regs_fully_wildcarded(wc)
858 || (!(wc->wildcards & FWW_TUN_ID)
860 || (cookie_hi && cookie_hi != ntohll(rule->flow.tun_id))))) {
862 } else if (!(wc->wildcards & FWW_TUN_ID)) {
863 return NXFF_TUN_ID_FROM_COOKIE;
865 return NXFF_OPENFLOW10;
869 /* Returns an OpenFlow message that can be used to set the flow format to
872 ofputil_make_set_flow_format(enum nx_flow_format flow_format)
876 if (flow_format == NXFF_OPENFLOW10
877 || flow_format == NXFF_TUN_ID_FROM_COOKIE) {
878 struct nxt_tun_id_cookie *tic;
880 tic = make_nxmsg(sizeof *tic, NXT_TUN_ID_FROM_COOKIE, &msg);
881 tic->set = flow_format == NXFF_TUN_ID_FROM_COOKIE;
883 struct nxt_set_flow_format *sff;
885 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
886 sff->format = htonl(flow_format);
892 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
893 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
896 * For OFPT_FLOW_MOD messages, 'flow_format' should be the current flow format
897 * at the time when the message was received. Otherwise 'flow_format' is
900 * Does not validate the flow_mod actions. */
902 ofputil_decode_flow_mod(struct flow_mod *fm, const struct ofp_header *oh,
903 enum nx_flow_format flow_format)
905 const struct ofputil_msg_type *type;
908 ofpbuf_use_const(&b, oh, ntohs(oh->length));
910 ofputil_decode_msg_type(oh, &type);
911 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
912 /* Standard OpenFlow flow_mod. */
913 struct ofp_match match, orig_match;
914 const struct ofp_flow_mod *ofm;
917 /* Dissect the message. */
918 ofm = ofpbuf_pull(&b, sizeof *ofm);
919 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
924 /* Normalize ofm->match. If normalization actually changes anything,
925 * then log the differences. */
927 match.pad1[0] = match.pad2[0] = 0;
929 normalize_match(&match);
930 if (memcmp(&match, &orig_match, sizeof orig_match)) {
931 if (!VLOG_DROP_INFO(&bad_ofmsg_rl)) {
932 char *old = ofp_match_to_literal_string(&orig_match);
933 char *new = ofp_match_to_literal_string(&match);
934 VLOG_INFO("normalization changed ofp_match, details:");
935 VLOG_INFO(" pre: %s", old);
936 VLOG_INFO("post: %s", new);
942 /* Translate the message. */
943 ofputil_cls_rule_from_match(&match, ntohs(ofm->priority), flow_format,
944 ofm->cookie, &fm->cr);
945 fm->cookie = ofm->cookie;
946 fm->command = ntohs(ofm->command);
947 fm->idle_timeout = ntohs(ofm->idle_timeout);
948 fm->hard_timeout = ntohs(ofm->hard_timeout);
949 fm->buffer_id = ntohl(ofm->buffer_id);
950 fm->out_port = ntohs(ofm->out_port);
951 fm->flags = ntohs(ofm->flags);
952 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
953 /* Nicira extended flow_mod. */
954 const struct nx_flow_mod *nfm;
957 /* Dissect the message. */
958 nfm = ofpbuf_pull(&b, sizeof *nfm);
959 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
964 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
969 /* Translate the message. */
970 fm->cookie = nfm->cookie;
971 fm->command = ntohs(nfm->command);
972 fm->idle_timeout = ntohs(nfm->idle_timeout);
973 fm->hard_timeout = ntohs(nfm->hard_timeout);
974 fm->buffer_id = ntohl(nfm->buffer_id);
975 fm->out_port = ntohs(nfm->out_port);
976 fm->flags = ntohs(nfm->flags);
984 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
985 * 'flow_format' and returns the message. */
987 ofputil_encode_flow_mod(const struct flow_mod *fm,
988 enum nx_flow_format flow_format)
990 size_t actions_len = fm->n_actions * sizeof *fm->actions;
993 if (flow_format == NXFF_OPENFLOW10
994 || flow_format == NXFF_TUN_ID_FROM_COOKIE) {
995 struct ofp_flow_mod *ofm;
997 msg = ofpbuf_new(sizeof *ofm + actions_len);
998 ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
999 ofputil_cls_rule_to_match(&fm->cr, flow_format, &ofm->match,
1000 fm->cookie, &ofm->cookie);
1001 ofm->command = htons(fm->command);
1002 ofm->idle_timeout = htons(fm->idle_timeout);
1003 ofm->hard_timeout = htons(fm->hard_timeout);
1004 ofm->priority = htons(fm->cr.priority);
1005 ofm->buffer_id = htonl(fm->buffer_id);
1006 ofm->out_port = htons(fm->out_port);
1007 ofm->flags = htons(fm->flags);
1008 } else if (flow_format == NXFF_NXM) {
1009 struct nx_flow_mod *nfm;
1012 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
1013 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
1014 match_len = nx_put_match(msg, &fm->cr);
1017 nfm->cookie = fm->cookie;
1018 nfm->command = htons(fm->command);
1019 nfm->idle_timeout = htons(fm->idle_timeout);
1020 nfm->hard_timeout = htons(fm->hard_timeout);
1021 nfm->priority = htons(fm->cr.priority);
1022 nfm->buffer_id = htonl(fm->buffer_id);
1023 nfm->out_port = htons(fm->out_port);
1024 nfm->flags = htons(fm->flags);
1025 nfm->match_len = htons(match_len);
1030 ofpbuf_put(msg, fm->actions, actions_len);
1031 update_openflow_length(msg);
1036 ofputil_decode_ofpst_flow_request(struct flow_stats_request *fsr,
1037 const struct ofp_header *oh,
1038 enum nx_flow_format flow_format,
1041 const struct ofp_flow_stats_request *ofsr = ofputil_stats_body(oh);
1043 fsr->aggregate = aggregate;
1044 ofputil_cls_rule_from_match(&ofsr->match, 0, flow_format, 0, &fsr->match);
1045 fsr->out_port = ntohs(ofsr->out_port);
1046 fsr->table_id = ofsr->table_id;
1052 ofputil_decode_nxst_flow_request(struct flow_stats_request *fsr,
1053 const struct ofp_header *oh,
1056 const struct nx_flow_stats_request *nfsr;
1060 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1062 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1063 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match);
1068 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1071 fsr->aggregate = aggregate;
1072 fsr->out_port = ntohs(nfsr->out_port);
1073 fsr->table_id = nfsr->table_id;
1078 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1079 * message 'oh', received when the current flow format was 'flow_format', into
1080 * an abstract flow_stats_request in 'fsr'. Returns 0 if successful, otherwise
1081 * an OpenFlow error code.
1083 * For OFPST_FLOW and OFPST_AGGREGATE messages, 'flow_format' should be the
1084 * current flow format at the time when the message was received. Otherwise
1085 * 'flow_format' is ignored. */
1087 ofputil_decode_flow_stats_request(struct flow_stats_request *fsr,
1088 const struct ofp_header *oh,
1089 enum nx_flow_format flow_format)
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, flow_format, false);
1103 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1104 return ofputil_decode_ofpst_flow_request(fsr, oh, flow_format, 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 message '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 || flow_format == NXFF_TUN_ID_FROM_COOKIE) {
1129 struct ofp_flow_stats_request *ofsr;
1132 BUILD_ASSERT_DECL(sizeof(struct ofp_flow_stats_request)
1133 == sizeof(struct ofp_aggregate_stats_request));
1135 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1136 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, &msg);
1137 ofputil_cls_rule_to_match(&fsr->match, flow_format, &ofsr->match,
1139 ofsr->table_id = fsr->table_id;
1140 ofsr->out_port = htons(fsr->out_port);
1141 } else if (flow_format == NXFF_NXM) {
1142 struct nx_flow_stats_request *nfsr;
1146 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1147 ofputil_make_nxstats_request(sizeof *nfsr, subtype, &msg);
1148 match_len = nx_put_match(msg, &fsr->match);
1151 nfsr->out_port = htons(fsr->out_port);
1152 nfsr->match_len = htons(match_len);
1153 nfsr->table_id = fsr->table_id;
1161 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh', received
1162 * when the current flow format was 'flow_format', into an abstract
1163 * ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise an
1164 * OpenFlow error code.
1166 * For OFPT_FLOW_REMOVED messages, 'flow_format' should be the current flow
1167 * format at the time when the message was received. Otherwise 'flow_format'
1170 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1171 const struct ofp_header *oh,
1172 enum nx_flow_format flow_format)
1174 const struct ofputil_msg_type *type;
1175 enum ofputil_msg_code code;
1177 ofputil_decode_msg_type(oh, &type);
1178 code = ofputil_msg_type_code(type);
1179 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1180 const struct ofp_flow_removed *ofr;
1182 ofr = (const struct ofp_flow_removed *) oh;
1183 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
1184 flow_format, ofr->cookie, &fr->rule);
1185 fr->cookie = ofr->cookie;
1186 fr->reason = ofr->reason;
1187 fr->duration_sec = ntohl(ofr->duration_sec);
1188 fr->duration_nsec = ntohl(ofr->duration_nsec);
1189 fr->idle_timeout = ntohs(ofr->idle_timeout);
1190 fr->packet_count = ntohll(ofr->packet_count);
1191 fr->byte_count = ntohll(ofr->byte_count);
1192 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1193 struct nx_flow_removed *nfr;
1197 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1199 nfr = ofpbuf_pull(&b, sizeof *nfr);
1200 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1206 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1209 fr->cookie = nfr->cookie;
1210 fr->reason = nfr->reason;
1211 fr->duration_sec = ntohl(nfr->duration_sec);
1212 fr->duration_nsec = ntohl(nfr->duration_nsec);
1213 fr->idle_timeout = ntohs(nfr->idle_timeout);
1214 fr->packet_count = ntohll(nfr->packet_count);
1215 fr->byte_count = ntohll(nfr->byte_count);
1223 /* Returns a string representing the message type of 'type'. The string is the
1224 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
1225 * messages, the constant is followed by "request" or "reply",
1226 * e.g. "OFPST_AGGREGATE reply". */
1228 ofputil_msg_type_name(const struct ofputil_msg_type *type)
1233 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1234 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1235 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
1238 * The caller is responsible for freeing '*bufferp' when it is no longer
1241 * The OpenFlow header length is initially set to 'openflow_len'; if the
1242 * message is later extended, the length should be updated with
1243 * update_openflow_length() before sending.
1245 * Returns the header. */
1247 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1249 *bufferp = ofpbuf_new(openflow_len);
1250 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1253 /* Similar to make_openflow() but creates a Nicira vendor extension message
1254 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1256 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1258 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
1261 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1262 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1263 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
1266 * The caller is responsible for freeing '*bufferp' when it is no longer
1269 * The OpenFlow header length is initially set to 'openflow_len'; if the
1270 * message is later extended, the length should be updated with
1271 * update_openflow_length() before sending.
1273 * Returns the header. */
1275 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1276 struct ofpbuf **bufferp)
1278 *bufferp = ofpbuf_new(openflow_len);
1279 return put_openflow_xid(openflow_len, type, xid, *bufferp);
1282 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
1283 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1285 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1286 struct ofpbuf **bufferp)
1288 *bufferp = ofpbuf_new(openflow_len);
1289 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
1292 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1293 * with the given 'type' and an arbitrary transaction id. Allocated bytes
1294 * beyond the header, if any, are zeroed.
1296 * The OpenFlow header length is initially set to 'openflow_len'; if the
1297 * message is later extended, the length should be updated with
1298 * update_openflow_length() before sending.
1300 * Returns the header. */
1302 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1304 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
1307 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1308 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
1309 * the header, if any, are zeroed.
1311 * The OpenFlow header length is initially set to 'openflow_len'; if the
1312 * message is later extended, the length should be updated with
1313 * update_openflow_length() before sending.
1315 * Returns the header. */
1317 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1318 struct ofpbuf *buffer)
1320 struct ofp_header *oh;
1322 assert(openflow_len >= sizeof *oh);
1323 assert(openflow_len <= UINT16_MAX);
1325 oh = ofpbuf_put_uninit(buffer, openflow_len);
1326 oh->version = OFP_VERSION;
1328 oh->length = htons(openflow_len);
1330 memset(oh + 1, 0, openflow_len - sizeof *oh);
1334 /* Similar to put_openflow() but append a Nicira vendor extension message with
1335 * the specific 'subtype'. 'subtype' should be in host byte order. */
1337 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
1339 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
1342 /* Similar to put_openflow_xid() but append a Nicira vendor extension message
1343 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1345 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1346 struct ofpbuf *buffer)
1348 struct nicira_header *nxh;
1350 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
1351 nxh->vendor = htonl(NX_VENDOR_ID);
1352 nxh->subtype = htonl(subtype);
1356 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
1357 * 'buffer->size'. */
1359 update_openflow_length(struct ofpbuf *buffer)
1361 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
1362 oh->length = htons(buffer->size);
1365 /* Creates an ofp_stats_request with the given 'type' and 'body_len' bytes of
1366 * space allocated for the 'body' member. Returns the first byte of the 'body'
1369 ofputil_make_stats_request(size_t body_len, uint16_t type,
1370 struct ofpbuf **bufferp)
1372 struct ofp_stats_request *osr;
1373 osr = make_openflow((offsetof(struct ofp_stats_request, body)
1374 + body_len), OFPT_STATS_REQUEST, bufferp);
1375 osr->type = htons(type);
1376 osr->flags = htons(0);
1380 /* Creates a stats request message with Nicira as vendor and the given
1381 * 'subtype', of total length 'openflow_len'. Returns the message. */
1383 ofputil_make_nxstats_request(size_t openflow_len, uint32_t subtype,
1384 struct ofpbuf **bufferp)
1386 struct nicira_stats_msg *nsm;
1388 nsm = make_openflow(openflow_len, OFPT_STATS_REQUEST, bufferp);
1389 nsm->type = htons(OFPST_VENDOR);
1390 nsm->flags = htons(0);
1391 nsm->vendor = htonl(NX_VENDOR_ID);
1392 nsm->subtype = htonl(subtype);
1396 /* Returns the first byte of the 'body' member of the ofp_stats_request or
1397 * ofp_stats_reply in 'oh'. */
1399 ofputil_stats_body(const struct ofp_header *oh)
1401 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1402 return ((const struct ofp_stats_request *) oh)->body;
1405 /* Returns the length of the 'body' member of the ofp_stats_request or
1406 * ofp_stats_reply in 'oh'. */
1408 ofputil_stats_body_len(const struct ofp_header *oh)
1410 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1411 return ntohs(oh->length) - sizeof(struct ofp_stats_request);
1414 /* Returns the first byte of the body of the nicira_stats_msg in 'oh'. */
1416 ofputil_nxstats_body(const struct ofp_header *oh)
1418 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1419 return ((const struct nicira_stats_msg *) oh) + 1;
1422 /* Returns the length of the body of the nicira_stats_msg in 'oh'. */
1424 ofputil_nxstats_body_len(const struct ofp_header *oh)
1426 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1427 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
1431 make_flow_mod(uint16_t command, const struct cls_rule *rule,
1434 struct ofp_flow_mod *ofm;
1435 size_t size = sizeof *ofm + actions_len;
1436 struct ofpbuf *out = ofpbuf_new(size);
1437 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
1438 ofm->header.version = OFP_VERSION;
1439 ofm->header.type = OFPT_FLOW_MOD;
1440 ofm->header.length = htons(size);
1442 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
1443 ofputil_cls_rule_to_match(rule, NXFF_OPENFLOW10, &ofm->match, 0, NULL);
1444 ofm->command = htons(command);
1449 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
1450 uint16_t idle_timeout, size_t actions_len)
1452 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
1453 struct ofp_flow_mod *ofm = out->data;
1454 ofm->idle_timeout = htons(idle_timeout);
1455 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
1456 ofm->buffer_id = htonl(buffer_id);
1461 make_del_flow(const struct cls_rule *rule)
1463 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
1464 struct ofp_flow_mod *ofm = out->data;
1465 ofm->out_port = htons(OFPP_NONE);
1470 make_add_simple_flow(const struct cls_rule *rule,
1471 uint32_t buffer_id, uint16_t out_port,
1472 uint16_t idle_timeout)
1474 if (out_port != OFPP_NONE) {
1475 struct ofp_action_output *oao;
1476 struct ofpbuf *buffer;
1478 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
1479 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
1480 oao->type = htons(OFPAT_OUTPUT);
1481 oao->len = htons(sizeof *oao);
1482 oao->port = htons(out_port);
1485 return make_add_flow(rule, buffer_id, idle_timeout, 0);
1490 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
1491 const struct ofpbuf *payload, int max_send_len)
1493 struct ofp_packet_in *opi;
1497 send_len = MIN(max_send_len, payload->size);
1498 buf = ofpbuf_new(sizeof *opi + send_len);
1499 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
1500 OFPT_PACKET_IN, 0, buf);
1501 opi->buffer_id = htonl(buffer_id);
1502 opi->total_len = htons(payload->size);
1503 opi->in_port = htons(in_port);
1504 opi->reason = reason;
1505 ofpbuf_put(buf, payload->data, send_len);
1506 update_openflow_length(buf);
1512 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1514 const struct ofp_action_header *actions, size_t n_actions)
1516 size_t actions_len = n_actions * sizeof *actions;
1517 struct ofp_packet_out *opo;
1518 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
1519 struct ofpbuf *out = ofpbuf_new(size);
1521 opo = ofpbuf_put_uninit(out, sizeof *opo);
1522 opo->header.version = OFP_VERSION;
1523 opo->header.type = OFPT_PACKET_OUT;
1524 opo->header.length = htons(size);
1525 opo->header.xid = htonl(0);
1526 opo->buffer_id = htonl(buffer_id);
1527 opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
1528 opo->actions_len = htons(actions_len);
1529 ofpbuf_put(out, actions, actions_len);
1531 ofpbuf_put(out, packet->data, packet->size);
1537 make_unbuffered_packet_out(const struct ofpbuf *packet,
1538 uint16_t in_port, uint16_t out_port)
1540 struct ofp_action_output action;
1541 action.type = htons(OFPAT_OUTPUT);
1542 action.len = htons(sizeof action);
1543 action.port = htons(out_port);
1544 return make_packet_out(packet, UINT32_MAX, in_port,
1545 (struct ofp_action_header *) &action, 1);
1549 make_buffered_packet_out(uint32_t buffer_id,
1550 uint16_t in_port, uint16_t out_port)
1552 if (out_port != OFPP_NONE) {
1553 struct ofp_action_output action;
1554 action.type = htons(OFPAT_OUTPUT);
1555 action.len = htons(sizeof action);
1556 action.port = htons(out_port);
1557 return make_packet_out(NULL, buffer_id, in_port,
1558 (struct ofp_action_header *) &action, 1);
1560 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
1564 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1566 make_echo_request(void)
1568 struct ofp_header *rq;
1569 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1570 rq = ofpbuf_put_uninit(out, sizeof *rq);
1571 rq->version = OFP_VERSION;
1572 rq->type = OFPT_ECHO_REQUEST;
1573 rq->length = htons(sizeof *rq);
1578 /* Creates and returns an OFPT_ECHO_REPLY message matching the
1579 * OFPT_ECHO_REQUEST message in 'rq'. */
1581 make_echo_reply(const struct ofp_header *rq)
1583 size_t size = ntohs(rq->length);
1584 struct ofpbuf *out = ofpbuf_new(size);
1585 struct ofp_header *reply = ofpbuf_put(out, rq, size);
1586 reply->type = OFPT_ECHO_REPLY;
1590 const struct ofp_flow_stats *
1591 flow_stats_first(struct flow_stats_iterator *iter,
1592 const struct ofp_stats_reply *osr)
1594 iter->pos = osr->body;
1595 iter->end = osr->body + (ntohs(osr->header.length)
1596 - offsetof(struct ofp_stats_reply, body));
1597 return flow_stats_next(iter);
1600 const struct ofp_flow_stats *
1601 flow_stats_next(struct flow_stats_iterator *iter)
1603 ptrdiff_t bytes_left = iter->end - iter->pos;
1604 const struct ofp_flow_stats *fs;
1607 if (bytes_left < sizeof *fs) {
1608 if (bytes_left != 0) {
1609 VLOG_WARN_RL(&bad_ofmsg_rl,
1610 "%td leftover bytes in flow stats reply", bytes_left);
1615 fs = (const void *) iter->pos;
1616 length = ntohs(fs->length);
1617 if (length < sizeof *fs) {
1618 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu is shorter than "
1619 "min %zu", length, sizeof *fs);
1621 } else if (length > bytes_left) {
1622 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu but only %td "
1623 "bytes left", length, bytes_left);
1625 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1626 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu has %zu bytes "
1627 "left over in final action", length,
1628 (length - sizeof *fs) % sizeof fs->actions[0]);
1631 iter->pos += length;
1636 check_action_exact_len(const union ofp_action *a, unsigned int len,
1637 unsigned int required_len)
1639 if (len != required_len) {
1640 VLOG_WARN_RL(&bad_ofmsg_rl, "action %"PRIu16" has invalid length "
1641 "%"PRIu16" (must be %u)\n",
1642 ntohs(a->type), ntohs(a->header.len), required_len);
1643 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1649 check_nx_action_exact_len(const struct nx_action_header *a,
1650 unsigned int len, unsigned int required_len)
1652 if (len != required_len) {
1653 VLOG_WARN_RL(&bad_ofmsg_rl,
1654 "Nicira action %"PRIu16" has invalid length %"PRIu16" "
1656 ntohs(a->subtype), ntohs(a->len), required_len);
1657 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1662 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
1663 * that the switch will never have more than 'max_ports' ports. Returns 0 if
1664 * 'port' is valid, otherwise an ofp_mkerr() return code. */
1666 check_output_port(uint16_t port, int max_ports)
1674 case OFPP_CONTROLLER:
1679 if (port < max_ports) {
1682 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
1683 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1687 /* Checks that 'action' is a valid OFPAT_ENQUEUE action, given that the switch
1688 * will never have more than 'max_ports' ports. Returns 0 if 'port' is valid,
1689 * otherwise an ofp_mkerr() return code. */
1691 check_enqueue_action(const union ofp_action *a, unsigned int len,
1694 const struct ofp_action_enqueue *oae;
1698 error = check_action_exact_len(a, len, 16);
1703 oae = (const struct ofp_action_enqueue *) a;
1704 port = ntohs(oae->port);
1705 if (port < max_ports || port == OFPP_IN_PORT) {
1708 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown enqueue port %x", port);
1709 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1713 check_nicira_action(const union ofp_action *a, unsigned int len,
1714 const struct flow *flow)
1716 const struct nx_action_header *nah;
1721 VLOG_WARN_RL(&bad_ofmsg_rl,
1722 "Nicira vendor action only %u bytes", len);
1723 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1725 nah = (const struct nx_action_header *) a;
1727 subtype = ntohs(nah->subtype);
1728 if (subtype > TYPE_MAXIMUM(enum nx_action_subtype)) {
1729 /* This is necessary because enum nx_action_subtype is probably an
1730 * 8-bit type, so the cast below throws away the top 8 bits. */
1731 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1734 switch ((enum nx_action_subtype) subtype) {
1735 case NXAST_RESUBMIT:
1736 case NXAST_SET_TUNNEL:
1737 case NXAST_DROP_SPOOFED_ARP:
1738 case NXAST_SET_QUEUE:
1739 case NXAST_POP_QUEUE:
1740 return check_nx_action_exact_len(nah, len, 16);
1742 case NXAST_REG_MOVE:
1743 error = check_nx_action_exact_len(nah, len,
1744 sizeof(struct nx_action_reg_move));
1748 return nxm_check_reg_move((const struct nx_action_reg_move *) a, flow);
1750 case NXAST_REG_LOAD:
1751 error = check_nx_action_exact_len(nah, len,
1752 sizeof(struct nx_action_reg_load));
1756 return nxm_check_reg_load((const struct nx_action_reg_load *) a, flow);
1761 case NXAST_SET_TUNNEL64:
1762 return check_nx_action_exact_len(
1763 nah, len, sizeof(struct nx_action_set_tunnel64));
1765 case NXAST_MULTIPATH:
1766 error = check_nx_action_exact_len(
1767 nah, len, sizeof(struct nx_action_multipath));
1771 return multipath_check((const struct nx_action_multipath *) a);
1773 case NXAST_SNAT__OBSOLETE:
1775 VLOG_WARN_RL(&bad_ofmsg_rl,
1776 "unknown Nicira vendor action subtype %"PRIu16,
1777 ntohs(nah->subtype));
1778 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1783 check_action(const union ofp_action *a, unsigned int len,
1784 const struct flow *flow, int max_ports)
1786 enum ofp_action_type type = ntohs(a->type);
1791 error = check_action_exact_len(a, len, 8);
1795 return check_output_port(ntohs(a->output.port), max_ports);
1797 case OFPAT_SET_VLAN_VID:
1798 error = check_action_exact_len(a, len, 8);
1802 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1803 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1807 case OFPAT_SET_VLAN_PCP:
1808 error = check_action_exact_len(a, len, 8);
1812 if (a->vlan_vid.vlan_vid & ~7) {
1813 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1817 case OFPAT_STRIP_VLAN:
1818 case OFPAT_SET_NW_SRC:
1819 case OFPAT_SET_NW_DST:
1820 case OFPAT_SET_NW_TOS:
1821 case OFPAT_SET_TP_SRC:
1822 case OFPAT_SET_TP_DST:
1823 return check_action_exact_len(a, len, 8);
1825 case OFPAT_SET_DL_SRC:
1826 case OFPAT_SET_DL_DST:
1827 return check_action_exact_len(a, len, 16);
1830 return (a->vendor.vendor == htonl(NX_VENDOR_ID)
1831 ? check_nicira_action(a, len, flow)
1832 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
1835 return check_enqueue_action(a, len, max_ports);
1838 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %d", (int) type);
1839 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
1844 validate_actions(const union ofp_action *actions, size_t n_actions,
1845 const struct flow *flow, int max_ports)
1849 for (i = 0; i < n_actions; ) {
1850 const union ofp_action *a = &actions[i];
1851 unsigned int len = ntohs(a->header.len);
1852 unsigned int n_slots = len / OFP_ACTION_ALIGN;
1853 unsigned int slots_left = &actions[n_actions] - a;
1856 if (n_slots > slots_left) {
1857 VLOG_WARN_RL(&bad_ofmsg_rl,
1858 "action requires %u slots but only %u remain",
1859 n_slots, slots_left);
1860 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1862 VLOG_WARN_RL(&bad_ofmsg_rl, "action has invalid length 0");
1863 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1864 } else if (len % OFP_ACTION_ALIGN) {
1865 VLOG_WARN_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
1866 "of %d", len, OFP_ACTION_ALIGN);
1867 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1870 error = check_action(a, len, flow, max_ports);
1879 /* Returns true if 'action' outputs to 'port' (which must be in network byte
1880 * order), false otherwise. */
1882 action_outputs_to_port(const union ofp_action *action, uint16_t port)
1884 switch (ntohs(action->type)) {
1886 return action->output.port == port;
1888 return ((const struct ofp_action_enqueue *) action)->port == port;
1894 /* The set of actions must either come from a trusted source or have been
1895 * previously validated with validate_actions(). */
1896 const union ofp_action *
1897 actions_first(struct actions_iterator *iter,
1898 const union ofp_action *oa, size_t n_actions)
1901 iter->end = oa + n_actions;
1902 return actions_next(iter);
1905 const union ofp_action *
1906 actions_next(struct actions_iterator *iter)
1908 if (iter->pos != iter->end) {
1909 const union ofp_action *a = iter->pos;
1910 unsigned int len = ntohs(a->header.len);
1911 iter->pos += len / OFP_ACTION_ALIGN;
1919 normalize_match(struct ofp_match *m)
1921 enum { OFPFW_NW = (OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO
1923 enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
1926 wc = ntohl(m->wildcards) & OVSFW_ALL;
1927 if (wc & OFPFW_DL_TYPE) {
1930 /* Can't sensibly match on network or transport headers if the
1931 * data link type is unknown. */
1932 wc |= OFPFW_NW | OFPFW_TP;
1933 m->nw_src = m->nw_dst = m->nw_proto = m->nw_tos = 0;
1934 m->tp_src = m->tp_dst = 0;
1935 } else if (m->dl_type == htons(ETH_TYPE_IP)) {
1936 if (wc & OFPFW_NW_PROTO) {
1939 /* Can't sensibly match on transport headers if the network
1940 * protocol is unknown. */
1942 m->tp_src = m->tp_dst = 0;
1943 } else if (m->nw_proto == IPPROTO_TCP ||
1944 m->nw_proto == IPPROTO_UDP ||
1945 m->nw_proto == IPPROTO_ICMP) {
1946 if (wc & OFPFW_TP_SRC) {
1949 if (wc & OFPFW_TP_DST) {
1953 /* Transport layer fields will always be extracted as zeros, so we
1954 * can do an exact-match on those values. */
1956 m->tp_src = m->tp_dst = 0;
1958 if (wc & OFPFW_NW_SRC_MASK) {
1959 m->nw_src &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_SRC_SHIFT);
1961 if (wc & OFPFW_NW_DST_MASK) {
1962 m->nw_dst &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_DST_SHIFT);
1964 if (wc & OFPFW_NW_TOS) {
1967 m->nw_tos &= IP_DSCP_MASK;
1969 } else if (m->dl_type == htons(ETH_TYPE_ARP)) {
1970 if (wc & OFPFW_NW_PROTO) {
1973 if (wc & OFPFW_NW_SRC_MASK) {
1974 m->nw_src &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_SRC_SHIFT);
1976 if (wc & OFPFW_NW_DST_MASK) {
1977 m->nw_dst &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_DST_SHIFT);
1979 m->tp_src = m->tp_dst = m->nw_tos = 0;
1981 /* Network and transport layer fields will always be extracted as
1982 * zeros, so we can do an exact-match on those values. */
1983 wc &= ~(OFPFW_NW | OFPFW_TP);
1984 m->nw_proto = m->nw_src = m->nw_dst = m->nw_tos = 0;
1985 m->tp_src = m->tp_dst = 0;
1987 if (wc & OFPFW_DL_SRC) {
1988 memset(m->dl_src, 0, sizeof m->dl_src);
1990 if (wc & OFPFW_DL_DST) {
1991 memset(m->dl_dst, 0, sizeof m->dl_dst);
1993 m->wildcards = htonl(wc);
1996 /* Returns a string that describes 'match' in a very literal way, without
1997 * interpreting its contents except in a very basic fashion. The returned
1998 * string is intended to be fixed-length, so that it is easy to see differences
1999 * between two such strings if one is put above another. This is useful for
2000 * describing changes made by normalize_match().
2002 * The caller must free the returned string (with free()). */
2004 ofp_match_to_literal_string(const struct ofp_match *match)
2006 return xasprintf("wildcards=%#10"PRIx32" "
2007 " in_port=%5"PRId16" "
2008 " dl_src="ETH_ADDR_FMT" "
2009 " dl_dst="ETH_ADDR_FMT" "
2010 " dl_vlan=%5"PRId16" "
2011 " dl_vlan_pcp=%3"PRId8" "
2012 " dl_type=%#6"PRIx16" "
2013 " nw_tos=%#4"PRIx8" "
2014 " nw_proto=%#4"PRIx16" "
2015 " nw_src=%#10"PRIx32" "
2016 " nw_dst=%#10"PRIx32" "
2017 " tp_src=%5"PRId16" "
2019 ntohl(match->wildcards),
2020 ntohs(match->in_port),
2021 ETH_ADDR_ARGS(match->dl_src),
2022 ETH_ADDR_ARGS(match->dl_dst),
2023 ntohs(match->dl_vlan),
2025 ntohs(match->dl_type),
2028 ntohl(match->nw_src),
2029 ntohl(match->nw_dst),
2030 ntohs(match->tp_src),
2031 ntohs(match->tp_dst));
2035 vendor_code_to_id(uint8_t code)
2038 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
2040 #undef OFPUTIL_VENDOR
2046 /* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
2047 * information taken from 'error', whose encoding must be as described in the
2048 * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
2049 * oh->xid as its transaction ID, and it will include up to the first 64 bytes
2052 * Returns NULL if 'error' is not an OpenFlow error code. */
2054 make_ofp_error_msg(int error, const struct ofp_header *oh)
2056 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2066 if (!is_ofp_error(error)) {
2067 /* We format 'error' with strerror() here since it seems likely to be
2068 * a system errno value. */
2069 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
2070 error, strerror(error));
2077 len = ntohs(oh->length);
2087 vendor = get_ofp_err_vendor(error);
2088 type = get_ofp_err_type(error);
2089 code = get_ofp_err_code(error);
2090 if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
2091 struct ofp_error_msg *oem;
2093 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
2094 oem->type = htons(type);
2095 oem->code = htons(code);
2097 struct ofp_error_msg *oem;
2098 struct nx_vendor_error *nve;
2101 vendor_id = vendor_code_to_id(vendor);
2102 if (vendor_id == UINT32_MAX) {
2103 VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
2108 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
2109 OFPT_ERROR, xid, &buf);
2110 oem->type = htons(NXET_VENDOR);
2111 oem->code = htons(NXVC_VENDOR_ERROR);
2113 nve = (struct nx_vendor_error *)oem->data;
2114 nve->vendor = htonl(vendor_id);
2115 nve->type = htons(type);
2116 nve->code = htons(code);
2121 ofpbuf_put(buf, data, len);
2127 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
2128 * successful, otherwise an OpenFlow error.
2130 * If successful, the first action is stored in '*actionsp' and the number of
2131 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
2132 * are stored, respectively.
2134 * This function does not check that the actions are valid (the caller should
2135 * do so, with validate_actions()). The caller is also responsible for making
2136 * sure that 'b->data' is initially aligned appropriately for "union
2139 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2140 union ofp_action **actionsp, size_t *n_actionsp)
2142 if (actions_len % OFP_ACTION_ALIGN != 0) {
2143 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2144 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
2148 *actionsp = ofpbuf_try_pull(b, actions_len);
2149 if (*actionsp == NULL) {
2150 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2151 "exceeds remaining message length (%zu)",
2152 actions_len, b->size);
2156 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
2162 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);