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"
30 VLOG_DEFINE_THIS_MODULE(ofp_util);
32 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
33 * in the peer and so there's not much point in showing a lot of them. */
34 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
36 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
37 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
40 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
41 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
42 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
43 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
46 ofputil_wcbits_to_netmask(int wcbits)
49 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
52 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
53 * that it wildcards. 'netmask' must be a CIDR netmask (see ip_is_cidr()). */
55 ofputil_netmask_to_wcbits(ovs_be32 netmask)
57 assert(ip_is_cidr(netmask));
59 return netmask == htonl(0) ? 32 : __builtin_ctz(ntohl(netmask));
63 for (wcbits = 32; netmask; wcbits--) {
64 netmask &= netmask - 1;
71 /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
73 #define WC_INVARIANT_LIST \
74 WC_INVARIANT_BIT(IN_PORT) \
75 WC_INVARIANT_BIT(DL_SRC) \
76 WC_INVARIANT_BIT(DL_DST) \
77 WC_INVARIANT_BIT(DL_TYPE) \
78 WC_INVARIANT_BIT(NW_PROTO) \
79 WC_INVARIANT_BIT(TP_SRC) \
80 WC_INVARIANT_BIT(TP_DST)
82 /* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
83 * actually have the same names and values. */
84 #define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME);
86 #undef WC_INVARIANT_BIT
88 /* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
92 #define WC_INVARIANT_BIT(NAME) | FWW_##NAME
94 #undef WC_INVARIANT_BIT
97 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
100 * 'flow_format' must either NXFF_OPENFLOW10 or NXFF_TUN_ID_FROM_COOKIE. In
101 * the latter case only, 'flow''s tun_id field will be taken from the high bits
102 * of 'cookie', if 'match''s wildcards do not indicate that tun_id is
105 ofputil_cls_rule_from_match(const struct ofp_match *match,
106 unsigned int priority,
107 enum nx_flow_format flow_format,
108 uint64_t cookie, struct cls_rule *rule)
110 struct flow_wildcards *wc = &rule->wc;
114 /* Initialize rule->priority. */
115 ofpfw = ntohl(match->wildcards);
116 ofpfw &= flow_format == NXFF_TUN_ID_FROM_COOKIE ? OVSFW_ALL : OFPFW_ALL;
117 rule->priority = !ofpfw ? UINT16_MAX : priority;
119 /* Initialize most of rule->wc. */
120 flow_wildcards_init_catchall(wc);
121 wc->wildcards = ofpfw & WC_INVARIANTS;
122 if (ofpfw & OFPFW_NW_TOS) {
123 wc->wildcards |= FWW_NW_TOS;
125 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
126 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
128 if (flow_format == NXFF_TUN_ID_FROM_COOKIE && !(ofpfw & NXFW_TUN_ID)) {
129 rule->flow.tun_id = htonl(ntohll(cookie) >> 32);
131 wc->wildcards |= FWW_TUN_ID;
132 rule->flow.tun_id = 0;
135 if (ofpfw & OFPFW_DL_DST) {
136 /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
137 * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST
138 * and FWW_ETH_MCAST. */
139 wc->wildcards |= FWW_ETH_MCAST;
142 /* Initialize most of rule->flow. */
143 rule->flow.nw_src = match->nw_src;
144 rule->flow.nw_dst = match->nw_dst;
145 rule->flow.in_port = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
146 : ntohs(match->in_port));
147 rule->flow.dl_type = match->dl_type;
148 rule->flow.tp_src = match->tp_src;
149 rule->flow.tp_dst = match->tp_dst;
150 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
151 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
152 rule->flow.nw_tos = match->nw_tos;
153 rule->flow.nw_proto = match->nw_proto;
155 /* Translate VLANs. */
156 vid = match->dl_vlan & htons(VLAN_VID_MASK);
157 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
158 switch (ofpfw & (OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP)) {
159 case OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP:
160 /* Wildcard everything. */
161 rule->flow.vlan_tci = htons(0);
162 rule->wc.vlan_tci_mask = htons(0);
165 case OFPFW_DL_VLAN_PCP:
166 if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
167 /* Match only packets without 802.1Q header. */
168 rule->flow.vlan_tci = htons(0);
169 rule->wc.vlan_tci_mask = htons(0xffff);
171 /* Wildcard PCP, specific VID. */
172 rule->flow.vlan_tci = vid | htons(VLAN_CFI);
173 rule->wc.vlan_tci_mask = htons(VLAN_VID_MASK | VLAN_CFI);
178 /* Wildcard VID, specific PCP. */
179 rule->flow.vlan_tci = pcp | htons(VLAN_CFI);
180 rule->wc.vlan_tci_mask = htons(VLAN_PCP_MASK | VLAN_CFI);
184 if (match->dl_vlan == htons(OFP_VLAN_NONE)) {
185 /* This case is odd, since we can't have a specific PCP without an
186 * 802.1Q header. However, older versions of OVS treated this as
187 * matching packets withut an 802.1Q header, so we do here too. */
188 rule->flow.vlan_tci = htons(0);
189 rule->wc.vlan_tci_mask = htons(0xffff);
191 /* Specific VID and PCP. */
192 rule->flow.vlan_tci = vid | pcp | htons(VLAN_CFI);
193 rule->wc.vlan_tci_mask = htons(0xffff);
199 cls_rule_zero_wildcarded_fields(rule);
202 /* Extract 'flow' with 'wildcards' into the OpenFlow match structure
205 * 'flow_format' must either NXFF_OPENFLOW10 or NXFF_TUN_ID_FROM_COOKIE. In
206 * the latter case only, 'match''s NXFW_TUN_ID bit will be filled in; otherwise
207 * it is always set to 0. */
209 ofputil_cls_rule_to_match(const struct cls_rule *rule,
210 enum nx_flow_format flow_format,
211 struct ofp_match *match)
213 const struct flow_wildcards *wc = &rule->wc;
216 /* Figure out most OpenFlow wildcards. */
217 ofpfw = wc->wildcards & WC_INVARIANTS;
218 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
219 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
220 if (wc->wildcards & FWW_NW_TOS) {
221 ofpfw |= OFPFW_NW_TOS;
223 if (flow_format == NXFF_TUN_ID_FROM_COOKIE && wc->wildcards & FWW_TUN_ID) {
224 ofpfw |= NXFW_TUN_ID;
227 /* Translate VLANs. */
228 match->dl_vlan = htons(0);
229 match->dl_vlan_pcp = 0;
230 if (rule->wc.vlan_tci_mask == htons(0)) {
231 ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
232 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
233 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
234 match->dl_vlan = htons(OFP_VLAN_NONE);
236 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
237 ofpfw |= OFPFW_DL_VLAN;
239 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
242 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
243 ofpfw |= OFPFW_DL_VLAN_PCP;
245 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
249 /* Compose most of the match structure. */
250 match->wildcards = htonl(ofpfw);
251 match->in_port = htons(rule->flow.in_port == ODPP_LOCAL ? OFPP_LOCAL
252 : rule->flow.in_port);
253 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
254 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
255 match->dl_type = rule->flow.dl_type;
256 match->nw_src = rule->flow.nw_src;
257 match->nw_dst = rule->flow.nw_dst;
258 match->nw_tos = rule->flow.nw_tos;
259 match->nw_proto = rule->flow.nw_proto;
260 match->tp_src = rule->flow.tp_src;
261 match->tp_dst = rule->flow.tp_dst;
262 memset(match->pad1, '\0', sizeof match->pad1);
263 memset(match->pad2, '\0', sizeof match->pad2);
266 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
270 static uint32_t next_xid = 1;
271 return htonl(next_xid++);
274 /* Basic parsing of OpenFlow messages. */
276 struct ofputil_msg_type {
277 enum ofputil_msg_code code; /* OFPUTIL_*. */
278 uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
279 const char *name; /* e.g. "OFPT_FLOW_REMOVED". */
280 unsigned int min_size; /* Minimum total message size in bytes. */
281 /* 0 if 'min_size' is the exact size that the message must be. Otherwise,
282 * the message may exceed 'min_size' by an even multiple of this value. */
283 unsigned int extra_multiple;
286 struct ofputil_msg_category {
287 const char *name; /* e.g. "OpenFlow message" */
288 const struct ofputil_msg_type *types;
290 int missing_error; /* ofp_mkerr() value for missing type. */
294 ofputil_length_ok(const struct ofputil_msg_category *cat,
295 const struct ofputil_msg_type *type,
298 switch (type->extra_multiple) {
300 if (size != type->min_size) {
301 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
302 "length %u (expected length %u)",
303 cat->name, type->name, size, type->min_size);
309 if (size < type->min_size) {
310 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
311 "length %u (expected length at least %u bytes)",
312 cat->name, type->name, size, type->min_size);
318 if (size < type->min_size
319 || (size - type->min_size) % type->extra_multiple) {
320 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect "
321 "length %u (must be exactly %u bytes or longer "
322 "by an integer multiple of %u bytes)",
323 cat->name, type->name, size,
324 type->min_size, type->extra_multiple);
332 ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
333 uint32_t value, unsigned int size,
334 const struct ofputil_msg_type **typep)
336 const struct ofputil_msg_type *type;
338 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
339 if (type->value == value) {
340 if (!ofputil_length_ok(cat, type, size)) {
341 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
348 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %u",
350 return cat->missing_error;
354 ofputil_decode_vendor(const struct ofp_header *oh,
355 const struct ofputil_msg_type **typep)
357 static const struct ofputil_msg_type nxt_messages[] = {
358 { OFPUTIL_NXT_STATUS_REQUEST,
359 NXT_STATUS_REQUEST, "NXT_STATUS_REQUEST",
360 sizeof(struct nicira_header), 1 },
362 { OFPUTIL_NXT_STATUS_REPLY,
363 NXT_STATUS_REPLY, "NXT_STATUS_REPLY",
364 sizeof(struct nicira_header), 1 },
366 { OFPUTIL_NXT_TUN_ID_FROM_COOKIE,
367 NXT_TUN_ID_FROM_COOKIE, "NXT_TUN_ID_FROM_COOKIE",
368 sizeof(struct nxt_tun_id_cookie), 0 },
370 { OFPUTIL_NXT_ROLE_REQUEST,
371 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
372 sizeof(struct nx_role_request), 0 },
374 { OFPUTIL_NXT_ROLE_REPLY,
375 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
376 sizeof(struct nx_role_request), 0 },
378 { OFPUTIL_NXT_SET_FLOW_FORMAT,
379 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
380 sizeof(struct nxt_set_flow_format), 0 },
382 { OFPUTIL_NXT_FLOW_MOD,
383 NXT_FLOW_MOD, "NXT_FLOW_MOD",
384 sizeof(struct nx_flow_mod), 8 },
386 { OFPUTIL_NXT_FLOW_REMOVED,
387 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
388 sizeof(struct nx_flow_removed), 8 },
391 static const struct ofputil_msg_category nxt_category = {
392 "Nicira extension message",
393 nxt_messages, ARRAY_SIZE(nxt_messages),
394 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
397 const struct ofp_vendor_header *ovh;
398 const struct nicira_header *nh;
400 ovh = (const struct ofp_vendor_header *) oh;
401 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
402 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
403 "vendor %"PRIx32, ntohl(ovh->vendor));
404 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
407 if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
408 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
409 "length %u (expected at least %zu)",
410 ntohs(ovh->header.length), sizeof(struct nicira_header));
411 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
414 nh = (const struct nicira_header *) oh;
415 return ofputil_lookup_openflow_message(&nxt_category, ntohl(nh->subtype),
416 ntohs(oh->length), typep);
420 check_nxstats_msg(const struct ofp_header *oh)
422 const struct ofp_stats_request *osr;
425 osr = (const struct ofp_stats_request *) oh;
427 memcpy(&vendor, osr->body, sizeof vendor);
428 if (vendor != htonl(NX_VENDOR_ID)) {
429 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
430 "unknown vendor %"PRIx32, ntohl(vendor));
431 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
434 if (ntohs(osr->header.length) < sizeof(struct nicira_stats_msg)) {
435 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
436 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
443 ofputil_decode_nxst_request(const struct ofp_header *oh,
444 const struct ofputil_msg_type **typep)
446 static const struct ofputil_msg_type nxst_requests[] = {
447 { OFPUTIL_NXST_FLOW_REQUEST,
448 NXST_FLOW, "NXST_FLOW request",
449 sizeof(struct nx_flow_stats_request), 8 },
451 { OFPUTIL_NXST_AGGREGATE_REQUEST,
452 NXST_AGGREGATE, "NXST_AGGREGATE request",
453 sizeof(struct nx_aggregate_stats_request), 8 },
456 static const struct ofputil_msg_category nxst_request_category = {
457 "Nicira extension statistics",
458 nxst_requests, ARRAY_SIZE(nxst_requests),
459 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
462 const struct nicira_stats_msg *nsm;
465 error = check_nxstats_msg(oh);
470 nsm = (struct nicira_stats_msg *) oh;
471 return ofputil_lookup_openflow_message(&nxst_request_category,
473 ntohs(oh->length), typep);
477 ofputil_decode_nxst_reply(const struct ofp_header *oh,
478 const struct ofputil_msg_type **typep)
480 static const struct ofputil_msg_type nxst_replies[] = {
481 { OFPUTIL_NXST_FLOW_REPLY,
482 NXST_FLOW, "NXST_FLOW reply",
483 sizeof(struct nicira_stats_msg), 8 },
485 { OFPUTIL_NXST_AGGREGATE_REPLY,
486 NXST_AGGREGATE, "NXST_AGGREGATE reply",
487 sizeof(struct nx_aggregate_stats_reply), 0 },
490 static const struct ofputil_msg_category nxst_reply_category = {
491 "Nicira extension statistics",
492 nxst_replies, ARRAY_SIZE(nxst_replies),
493 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
496 const struct nicira_stats_msg *nsm;
499 error = check_nxstats_msg(oh);
504 nsm = (struct nicira_stats_msg *) oh;
505 return ofputil_lookup_openflow_message(&nxst_reply_category,
507 ntohs(oh->length), typep);
511 ofputil_decode_ofpst_request(const struct ofp_header *oh,
512 const struct ofputil_msg_type **typep)
514 enum { OSR_SIZE = sizeof(struct ofp_stats_request) };
515 static const struct ofputil_msg_type ofpst_requests[] = {
516 { OFPUTIL_OFPST_DESC_REQUEST,
517 OFPST_DESC, "OFPST_DESC request",
520 { OFPUTIL_OFPST_FLOW_REQUEST,
521 OFPST_FLOW, "OFPST_FLOW request",
522 OSR_SIZE + sizeof(struct ofp_flow_stats_request), 0 },
524 { OFPUTIL_OFPST_AGGREGATE_REQUEST,
525 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
526 OSR_SIZE + sizeof(struct ofp_aggregate_stats_request), 0 },
528 { OFPUTIL_OFPST_TABLE_REQUEST,
529 OFPST_TABLE, "OFPST_TABLE request",
532 { OFPUTIL_OFPST_PORT_REQUEST,
533 OFPST_PORT, "OFPST_PORT request",
534 OSR_SIZE + sizeof(struct ofp_port_stats_request), 0 },
536 { OFPUTIL_OFPST_QUEUE_REQUEST,
537 OFPST_QUEUE, "OFPST_QUEUE request",
538 OSR_SIZE + sizeof(struct ofp_queue_stats_request), 0 },
541 OFPST_VENDOR, "OFPST_VENDOR request",
542 OSR_SIZE + sizeof(uint32_t), 1 },
545 static const struct ofputil_msg_category ofpst_request_category = {
546 "OpenFlow statistics",
547 ofpst_requests, ARRAY_SIZE(ofpst_requests),
548 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
551 const struct ofp_stats_request *osr;
554 osr = (const struct ofp_stats_request *) oh;
555 error = ofputil_lookup_openflow_message(&ofpst_request_category,
557 ntohs(oh->length), typep);
558 if (!error && osr->type == htons(OFPST_VENDOR)) {
559 error = ofputil_decode_nxst_request(oh, typep);
565 ofputil_decode_ofpst_reply(const struct ofp_header *oh,
566 const struct ofputil_msg_type **typep)
568 enum { OSR_SIZE = sizeof(struct ofp_stats_reply) };
569 static const struct ofputil_msg_type ofpst_replies[] = {
570 { OFPUTIL_OFPST_DESC_REPLY,
571 OFPST_DESC, "OFPST_DESC reply",
572 OSR_SIZE + sizeof(struct ofp_desc_stats), 0 },
574 { OFPUTIL_OFPST_FLOW_REPLY,
575 OFPST_FLOW, "OFPST_FLOW reply",
578 { OFPUTIL_OFPST_AGGREGATE_REPLY,
579 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
580 OSR_SIZE + sizeof(struct ofp_aggregate_stats_reply), 0 },
582 { OFPUTIL_OFPST_TABLE_REPLY,
583 OFPST_TABLE, "OFPST_TABLE reply",
584 OSR_SIZE, sizeof(struct ofp_table_stats) },
586 { OFPUTIL_OFPST_PORT_REPLY,
587 OFPST_PORT, "OFPST_PORT reply",
588 OSR_SIZE, sizeof(struct ofp_port_stats) },
590 { OFPUTIL_OFPST_QUEUE_REPLY,
591 OFPST_QUEUE, "OFPST_QUEUE reply",
592 OSR_SIZE, sizeof(struct ofp_queue_stats) },
595 OFPST_VENDOR, "OFPST_VENDOR reply",
596 OSR_SIZE + sizeof(uint32_t), 1 },
599 static const struct ofputil_msg_category ofpst_reply_category = {
600 "OpenFlow statistics",
601 ofpst_replies, ARRAY_SIZE(ofpst_replies),
602 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
605 const struct ofp_stats_reply *osr = (const struct ofp_stats_reply *) oh;
608 error = ofputil_lookup_openflow_message(&ofpst_reply_category,
610 ntohs(oh->length), typep);
611 if (!error && osr->type == htons(OFPST_VENDOR)) {
612 error = ofputil_decode_nxst_reply(oh, typep);
617 /* Decodes the message type represented by 'oh'. Returns 0 if successful or
618 * an OpenFlow error code constructed with ofp_mkerr() on failure. Either
619 * way, stores in '*typep' a type structure that can be inspected with the
620 * ofputil_msg_type_*() functions.
622 * oh->length must indicate the correct length of the message (and must be at
623 * least sizeof(struct ofp_header)).
625 * Success indicates that 'oh' is at least as long as the minimum-length
626 * message of its type. */
628 ofputil_decode_msg_type(const struct ofp_header *oh,
629 const struct ofputil_msg_type **typep)
631 static const struct ofputil_msg_type ofpt_messages[] = {
632 { OFPUTIL_OFPT_HELLO,
633 OFPT_HELLO, "OFPT_HELLO",
634 sizeof(struct ofp_hello), 1 },
636 { OFPUTIL_OFPT_ERROR,
637 OFPT_ERROR, "OFPT_ERROR",
638 sizeof(struct ofp_error_msg), 1 },
640 { OFPUTIL_OFPT_ECHO_REQUEST,
641 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
642 sizeof(struct ofp_header), 1 },
644 { OFPUTIL_OFPT_ECHO_REPLY,
645 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
646 sizeof(struct ofp_header), 1 },
648 { OFPUTIL_OFPT_FEATURES_REQUEST,
649 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
650 sizeof(struct ofp_header), 0 },
652 { OFPUTIL_OFPT_FEATURES_REPLY,
653 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
654 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
656 { OFPUTIL_OFPT_GET_CONFIG_REQUEST,
657 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
658 sizeof(struct ofp_header), 0 },
660 { OFPUTIL_OFPT_GET_CONFIG_REPLY,
661 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
662 sizeof(struct ofp_switch_config), 0 },
664 { OFPUTIL_OFPT_SET_CONFIG,
665 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
666 sizeof(struct ofp_switch_config), 0 },
668 { OFPUTIL_OFPT_PACKET_IN,
669 OFPT_PACKET_IN, "OFPT_PACKET_IN",
670 offsetof(struct ofp_packet_in, data), 1 },
672 { OFPUTIL_OFPT_FLOW_REMOVED,
673 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
674 sizeof(struct ofp_flow_removed), 0 },
676 { OFPUTIL_OFPT_PORT_STATUS,
677 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
678 sizeof(struct ofp_port_status), 0 },
680 { OFPUTIL_OFPT_PACKET_OUT,
681 OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
682 sizeof(struct ofp_packet_out), 1 },
684 { OFPUTIL_OFPT_FLOW_MOD,
685 OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
686 sizeof(struct ofp_flow_mod), 1 },
688 { OFPUTIL_OFPT_PORT_MOD,
689 OFPT_PORT_MOD, "OFPT_PORT_MOD",
690 sizeof(struct ofp_port_mod), 0 },
693 OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
694 sizeof(struct ofp_stats_request), 1 },
697 OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
698 sizeof(struct ofp_stats_reply), 1 },
700 { OFPUTIL_OFPT_BARRIER_REQUEST,
701 OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
702 sizeof(struct ofp_header), 0 },
704 { OFPUTIL_OFPT_BARRIER_REPLY,
705 OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
706 sizeof(struct ofp_header), 0 },
709 OFPT_VENDOR, "OFPT_VENDOR",
710 sizeof(struct ofp_vendor_header), 1 },
713 static const struct ofputil_msg_category ofpt_category = {
715 ofpt_messages, ARRAY_SIZE(ofpt_messages),
716 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE)
721 error = ofputil_lookup_openflow_message(&ofpt_category, oh->type,
722 ntohs(oh->length), typep);
726 error = ofputil_decode_vendor(oh, typep);
729 case OFPT_STATS_REQUEST:
730 error = ofputil_decode_ofpst_request(oh, typep);
733 case OFPT_STATS_REPLY:
734 error = ofputil_decode_ofpst_reply(oh, typep);
741 static const struct ofputil_msg_type ofputil_invalid_type = {
743 0, "OFPUTIL_INVALID",
747 *typep = &ofputil_invalid_type;
752 /* Returns an OFPUTIL_* message type code for 'type'. */
753 enum ofputil_msg_code
754 ofputil_msg_type_code(const struct ofputil_msg_type *type)
762 ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
764 switch (flow_format) {
765 case NXFF_OPENFLOW10:
766 case NXFF_TUN_ID_FROM_COOKIE:
775 ofputil_flow_format_to_string(enum nx_flow_format flow_format)
777 switch (flow_format) {
778 case NXFF_OPENFLOW10:
780 case NXFF_TUN_ID_FROM_COOKIE:
781 return "tun_id_from_cookie";
790 ofputil_flow_format_from_string(const char *s)
792 return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
793 : !strcmp(s, "tun_id_from_cookie") ? NXFF_TUN_ID_FROM_COOKIE
794 : !strcmp(s, "nxm") ? NXFF_NXM
799 regs_fully_wildcarded(const struct flow_wildcards *wc)
803 for (i = 0; i < FLOW_N_REGS; i++) {
804 if (wc->reg_masks[i] != 0) {
811 /* Returns the minimum nx_flow_format to use for sending 'rule' to a switch
812 * (e.g. to add or remove a flow). 'cookie_support' should be true if the
813 * command to be sent includes a flow cookie (as OFPT_FLOW_MOD does, for
814 * example) or false if the command does not (OFPST_FLOW and OFPST_AGGREGATE do
815 * not, for example). If 'cookie_support' is true, then 'cookie' should be the
816 * cookie to be sent; otherwise its value is ignored.
818 * The "best" flow format is chosen on this basis:
820 * - It must be capable of expressing the rule. NXFF_OPENFLOW10 flows can't
821 * handle tunnel IDs. NXFF_TUN_ID_FROM_COOKIE flows can't handle registers
822 * or fixing the Ethernet multicast bit, and can't handle tunnel IDs that
823 * conflict with the high 32 bits of the cookie or commands that don't
826 * - Otherwise, the chosen format should be as backward compatible as
827 * possible. (NXFF_OPENFLOW10 is more backward compatible than
828 * NXFF_TUN_ID_FROM_COOKIE, which is more backward compatible than
832 ofputil_min_flow_format(const struct cls_rule *rule, bool cookie_support,
835 const struct flow_wildcards *wc = &rule->wc;
836 ovs_be32 cookie_hi = htonl(ntohll(cookie) >> 32);
838 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)
839 || !regs_fully_wildcarded(wc)
840 || (!(wc->wildcards & FWW_TUN_ID)
842 || (cookie_hi && cookie_hi != rule->flow.tun_id)))) {
844 } else if (!(wc->wildcards & FWW_TUN_ID)) {
845 return NXFF_TUN_ID_FROM_COOKIE;
847 return NXFF_OPENFLOW10;
851 /* Returns an OpenFlow message that can be used to set the flow format to
854 ofputil_make_set_flow_format(enum nx_flow_format flow_format)
858 if (flow_format == NXFF_OPENFLOW10
859 || flow_format == NXFF_TUN_ID_FROM_COOKIE) {
860 struct nxt_tun_id_cookie *tic;
862 tic = make_nxmsg(sizeof *tic, NXT_TUN_ID_FROM_COOKIE, &msg);
863 tic->set = flow_format == NXFF_TUN_ID_FROM_COOKIE;
865 struct nxt_set_flow_format *sff;
867 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
868 sff->format = htonl(flow_format);
874 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
875 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
878 * For OFPT_FLOW_MOD messages, 'flow_format' should be the current flow format
879 * at the time when the message was received. Otherwise 'flow_format' is
882 * Does not validate the flow_mod actions. */
884 ofputil_decode_flow_mod(struct flow_mod *fm, const struct ofp_header *oh,
885 enum nx_flow_format flow_format)
887 const struct ofputil_msg_type *type;
890 b.data = (void *) oh;
891 b.size = ntohs(oh->length);
893 ofputil_decode_msg_type(oh, &type);
894 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
895 /* Standard OpenFlow flow_mod. */
896 struct ofp_match match, orig_match;
897 const struct ofp_flow_mod *ofm;
900 /* Dissect the message. */
901 ofm = ofpbuf_try_pull(&b, sizeof *ofm);
903 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
905 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
910 /* Normalize ofm->match. If normalization actually changes anything,
911 * then log the differences. */
913 match.pad1[0] = match.pad2[0] = 0;
915 normalize_match(&match);
916 if (memcmp(&match, &orig_match, sizeof orig_match)) {
917 if (!VLOG_DROP_INFO(&bad_ofmsg_rl)) {
918 char *old = ofp_match_to_literal_string(&orig_match);
919 char *new = ofp_match_to_literal_string(&match);
920 VLOG_INFO("normalization changed ofp_match, details:");
921 VLOG_INFO(" pre: %s", old);
922 VLOG_INFO("post: %s", new);
928 /* Translate the message. */
929 ofputil_cls_rule_from_match(&match, ntohs(ofm->priority), flow_format,
930 ofm->cookie, &fm->cr);
931 fm->cookie = ofm->cookie;
932 fm->command = ntohs(ofm->command);
933 fm->idle_timeout = ntohs(ofm->idle_timeout);
934 fm->hard_timeout = ntohs(ofm->hard_timeout);
935 fm->buffer_id = ntohl(ofm->buffer_id);
936 fm->out_port = ntohs(ofm->out_port);
937 fm->flags = ntohs(ofm->flags);
938 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
939 /* Nicira extended flow_mod. */
940 const struct nx_flow_mod *nfm;
943 /* Dissect the message. */
944 nfm = ofpbuf_try_pull(&b, sizeof *nfm);
946 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
948 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
953 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
958 /* Translate the message. */
959 fm->cookie = nfm->cookie;
960 fm->command = ntohs(nfm->command);
961 fm->idle_timeout = ntohs(nfm->idle_timeout);
962 fm->hard_timeout = ntohs(nfm->hard_timeout);
963 fm->buffer_id = ntohl(nfm->buffer_id);
964 fm->out_port = ntohs(nfm->out_port);
965 fm->flags = ntohs(nfm->flags);
973 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
974 * 'flow_format' and returns the message. */
976 ofputil_encode_flow_mod(const struct flow_mod *fm,
977 enum nx_flow_format flow_format)
979 size_t actions_len = fm->n_actions * sizeof *fm->actions;
982 if (flow_format == NXFF_OPENFLOW10
983 || flow_format == NXFF_TUN_ID_FROM_COOKIE) {
984 struct ofp_flow_mod *ofm;
986 msg = ofpbuf_new(sizeof *ofm + actions_len);
987 ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
988 ofputil_cls_rule_to_match(&fm->cr, flow_format, &ofm->match);
989 if (flow_format != NXFF_TUN_ID_FROM_COOKIE
990 || fm->cr.wc.wildcards & FWW_TUN_ID) {
991 ofm->cookie = fm->cookie;
993 uint32_t cookie_lo = ntohll(fm->cookie);
994 uint32_t cookie_hi = ntohl(fm->cr.flow.tun_id);
995 ofm->cookie = htonll(cookie_lo | ((uint64_t) cookie_hi << 32));
997 ofm->command = htons(fm->command);
998 ofm->idle_timeout = htons(fm->idle_timeout);
999 ofm->hard_timeout = htons(fm->hard_timeout);
1000 ofm->priority = htons(fm->cr.priority);
1001 ofm->buffer_id = htonl(fm->buffer_id);
1002 ofm->out_port = htons(fm->out_port);
1003 ofm->flags = htons(fm->flags);
1004 } else if (flow_format == NXFF_NXM) {
1005 struct nx_flow_mod *nfm;
1008 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
1009 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
1010 match_len = nx_put_match(msg, &fm->cr);
1013 nfm->cookie = fm->cookie;
1014 nfm->command = htons(fm->command);
1015 nfm->idle_timeout = htons(fm->idle_timeout);
1016 nfm->hard_timeout = htons(fm->hard_timeout);
1017 nfm->priority = htons(fm->cr.priority);
1018 nfm->buffer_id = htonl(fm->buffer_id);
1019 nfm->out_port = htons(fm->out_port);
1020 nfm->flags = htons(fm->flags);
1021 nfm->match_len = htons(match_len);
1026 ofpbuf_put(msg, fm->actions, actions_len);
1027 update_openflow_length(msg);
1032 ofputil_decode_ofpst_flow_request(struct flow_stats_request *fsr,
1033 const struct ofp_header *oh,
1034 enum nx_flow_format flow_format,
1037 const struct ofp_flow_stats_request *ofsr = ofputil_stats_body(oh);
1039 fsr->aggregate = aggregate;
1040 ofputil_cls_rule_from_match(&ofsr->match, 0, flow_format, 0, &fsr->match);
1041 fsr->out_port = ntohs(ofsr->out_port);
1042 fsr->table_id = ofsr->table_id;
1048 ofputil_decode_nxst_flow_request(struct flow_stats_request *fsr,
1049 const struct ofp_header *oh,
1052 const struct nx_flow_stats_request *nfsr;
1056 b.data = (void *) oh;
1057 b.size = ntohs(oh->length);
1059 nfsr = ofpbuf_try_pull(&b, sizeof *nfsr);
1061 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
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 ofputil_decode_flow_stats_request(struct flow_stats_request *fsr,
1084 const struct ofp_header *oh,
1085 enum nx_flow_format flow_format)
1087 const struct ofputil_msg_type *type;
1091 b.data = (void *) oh;
1092 b.size = ntohs(oh->length);
1094 ofputil_decode_msg_type(oh, &type);
1095 code = ofputil_msg_type_code(type);
1097 case OFPUTIL_OFPST_FLOW_REQUEST:
1098 return ofputil_decode_ofpst_flow_request(fsr, oh, flow_format, false);
1100 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1101 return ofputil_decode_ofpst_flow_request(fsr, oh, flow_format, true);
1103 case OFPUTIL_NXST_FLOW_REQUEST:
1104 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1106 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1107 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1110 /* Hey, the caller lied. */
1115 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1116 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE message 'oh' according to
1117 * 'flow_format', and returns the message. */
1119 ofputil_encode_flow_stats_request(const struct flow_stats_request *fsr,
1120 enum nx_flow_format flow_format)
1124 if (flow_format == NXFF_OPENFLOW10
1125 || flow_format == NXFF_TUN_ID_FROM_COOKIE) {
1126 struct ofp_flow_stats_request *ofsr;
1129 BUILD_ASSERT_DECL(sizeof(struct ofp_flow_stats_request)
1130 == sizeof(struct ofp_aggregate_stats_request));
1132 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1133 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, &msg);
1134 ofputil_cls_rule_to_match(&fsr->match, flow_format, &ofsr->match);
1135 ofsr->table_id = fsr->table_id;
1136 ofsr->out_port = htons(fsr->out_port);
1137 } else if (flow_format == NXFF_NXM) {
1138 struct nx_flow_stats_request *nfsr;
1141 ofputil_make_nxstats_request(sizeof *nfsr, NXST_FLOW, &msg);
1142 match_len = nx_put_match(msg, &fsr->match);
1145 nfsr->out_port = htons(fsr->out_port);
1146 nfsr->match_len = htons(match_len);
1147 nfsr->table_id = fsr->table_id;
1155 /* Returns a string representing the message type of 'type'. The string is the
1156 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
1157 * messages, the constant is followed by "request" or "reply",
1158 * e.g. "OFPST_AGGREGATE reply". */
1160 ofputil_msg_type_name(const struct ofputil_msg_type *type)
1165 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1166 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1167 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
1170 * The caller is responsible for freeing '*bufferp' when it is no longer
1173 * The OpenFlow header length is initially set to 'openflow_len'; if the
1174 * message is later extended, the length should be updated with
1175 * update_openflow_length() before sending.
1177 * Returns the header. */
1179 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1181 *bufferp = ofpbuf_new(openflow_len);
1182 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1185 /* Similar to make_openflow() but creates a Nicira vendor extension message
1186 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1188 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1190 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
1193 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1194 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1195 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
1198 * The caller is responsible for freeing '*bufferp' when it is no longer
1201 * The OpenFlow header length is initially set to 'openflow_len'; if the
1202 * message is later extended, the length should be updated with
1203 * update_openflow_length() before sending.
1205 * Returns the header. */
1207 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1208 struct ofpbuf **bufferp)
1210 *bufferp = ofpbuf_new(openflow_len);
1211 return put_openflow_xid(openflow_len, type, xid, *bufferp);
1214 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
1215 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1217 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1218 struct ofpbuf **bufferp)
1220 *bufferp = ofpbuf_new(openflow_len);
1221 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
1224 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1225 * with the given 'type' and an arbitrary transaction id. Allocated bytes
1226 * beyond the header, if any, are zeroed.
1228 * The OpenFlow header length is initially set to 'openflow_len'; if the
1229 * message is later extended, the length should be updated with
1230 * update_openflow_length() before sending.
1232 * Returns the header. */
1234 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1236 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
1239 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1240 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
1241 * the header, if any, are zeroed.
1243 * The OpenFlow header length is initially set to 'openflow_len'; if the
1244 * message is later extended, the length should be updated with
1245 * update_openflow_length() before sending.
1247 * Returns the header. */
1249 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1250 struct ofpbuf *buffer)
1252 struct ofp_header *oh;
1254 assert(openflow_len >= sizeof *oh);
1255 assert(openflow_len <= UINT16_MAX);
1257 oh = ofpbuf_put_uninit(buffer, openflow_len);
1258 oh->version = OFP_VERSION;
1260 oh->length = htons(openflow_len);
1262 memset(oh + 1, 0, openflow_len - sizeof *oh);
1266 /* Similar to put_openflow() but append a Nicira vendor extension message with
1267 * the specific 'subtype'. 'subtype' should be in host byte order. */
1269 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
1271 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
1274 /* Similar to put_openflow_xid() but append a Nicira vendor extension message
1275 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1277 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1278 struct ofpbuf *buffer)
1280 struct nicira_header *nxh;
1282 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
1283 nxh->vendor = htonl(NX_VENDOR_ID);
1284 nxh->subtype = htonl(subtype);
1288 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
1289 * 'buffer->size'. */
1291 update_openflow_length(struct ofpbuf *buffer)
1293 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
1294 oh->length = htons(buffer->size);
1297 /* Creates an ofp_stats_request with the given 'type' and 'body_len' bytes of
1298 * space allocated for the 'body' member. Returns the first byte of the 'body'
1301 ofputil_make_stats_request(size_t body_len, uint16_t type,
1302 struct ofpbuf **bufferp)
1304 struct ofp_stats_request *osr;
1305 osr = make_openflow((offsetof(struct ofp_stats_request, body)
1306 + body_len), OFPT_STATS_REQUEST, bufferp);
1307 osr->type = htons(type);
1308 osr->flags = htons(0);
1312 /* Creates a stats request message with Nicira as vendor and the given
1313 * 'subtype', of total length 'openflow_len'. Returns the message. */
1315 ofputil_make_nxstats_request(size_t openflow_len, uint32_t subtype,
1316 struct ofpbuf **bufferp)
1318 struct nicira_stats_msg *nsm;
1320 nsm = make_openflow(openflow_len, OFPT_STATS_REQUEST, bufferp);
1321 nsm->type = htons(OFPST_VENDOR);
1322 nsm->flags = htons(0);
1323 nsm->vendor = htonl(NX_VENDOR_ID);
1324 nsm->subtype = htonl(subtype);
1328 /* Returns the first byte of the 'body' member of the ofp_stats_request or
1329 * ofp_stats_reply in 'oh'. */
1331 ofputil_stats_body(const struct ofp_header *oh)
1333 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1334 return ((const struct ofp_stats_request *) oh)->body;
1337 /* Returns the length of the 'body' member of the ofp_stats_request or
1338 * ofp_stats_reply in 'oh'. */
1340 ofputil_stats_body_len(const struct ofp_header *oh)
1342 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1343 return ntohs(oh->length) - sizeof(struct ofp_stats_request);
1347 make_flow_mod(uint16_t command, const struct cls_rule *rule,
1350 struct ofp_flow_mod *ofm;
1351 size_t size = sizeof *ofm + actions_len;
1352 struct ofpbuf *out = ofpbuf_new(size);
1353 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
1354 ofm->header.version = OFP_VERSION;
1355 ofm->header.type = OFPT_FLOW_MOD;
1356 ofm->header.length = htons(size);
1358 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
1359 ofputil_cls_rule_to_match(rule, NXFF_OPENFLOW10, &ofm->match);
1360 ofm->command = htons(command);
1365 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
1366 uint16_t idle_timeout, size_t actions_len)
1368 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
1369 struct ofp_flow_mod *ofm = out->data;
1370 ofm->idle_timeout = htons(idle_timeout);
1371 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
1372 ofm->buffer_id = htonl(buffer_id);
1377 make_del_flow(const struct cls_rule *rule)
1379 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
1380 struct ofp_flow_mod *ofm = out->data;
1381 ofm->out_port = htons(OFPP_NONE);
1386 make_add_simple_flow(const struct cls_rule *rule,
1387 uint32_t buffer_id, uint16_t out_port,
1388 uint16_t idle_timeout)
1390 if (out_port != OFPP_NONE) {
1391 struct ofp_action_output *oao;
1392 struct ofpbuf *buffer;
1394 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
1395 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
1396 oao->type = htons(OFPAT_OUTPUT);
1397 oao->len = htons(sizeof *oao);
1398 oao->port = htons(out_port);
1401 return make_add_flow(rule, buffer_id, idle_timeout, 0);
1406 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
1407 const struct ofpbuf *payload, int max_send_len)
1409 struct ofp_packet_in *opi;
1413 send_len = MIN(max_send_len, payload->size);
1414 buf = ofpbuf_new(sizeof *opi + send_len);
1415 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
1416 OFPT_PACKET_IN, 0, buf);
1417 opi->buffer_id = htonl(buffer_id);
1418 opi->total_len = htons(payload->size);
1419 opi->in_port = htons(in_port);
1420 opi->reason = reason;
1421 ofpbuf_put(buf, payload->data, send_len);
1422 update_openflow_length(buf);
1428 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1430 const struct ofp_action_header *actions, size_t n_actions)
1432 size_t actions_len = n_actions * sizeof *actions;
1433 struct ofp_packet_out *opo;
1434 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
1435 struct ofpbuf *out = ofpbuf_new(size);
1437 opo = ofpbuf_put_uninit(out, sizeof *opo);
1438 opo->header.version = OFP_VERSION;
1439 opo->header.type = OFPT_PACKET_OUT;
1440 opo->header.length = htons(size);
1441 opo->header.xid = htonl(0);
1442 opo->buffer_id = htonl(buffer_id);
1443 opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
1444 opo->actions_len = htons(actions_len);
1445 ofpbuf_put(out, actions, actions_len);
1447 ofpbuf_put(out, packet->data, packet->size);
1453 make_unbuffered_packet_out(const struct ofpbuf *packet,
1454 uint16_t in_port, uint16_t out_port)
1456 struct ofp_action_output action;
1457 action.type = htons(OFPAT_OUTPUT);
1458 action.len = htons(sizeof action);
1459 action.port = htons(out_port);
1460 return make_packet_out(packet, UINT32_MAX, in_port,
1461 (struct ofp_action_header *) &action, 1);
1465 make_buffered_packet_out(uint32_t buffer_id,
1466 uint16_t in_port, uint16_t out_port)
1468 if (out_port != OFPP_NONE) {
1469 struct ofp_action_output action;
1470 action.type = htons(OFPAT_OUTPUT);
1471 action.len = htons(sizeof action);
1472 action.port = htons(out_port);
1473 return make_packet_out(NULL, buffer_id, in_port,
1474 (struct ofp_action_header *) &action, 1);
1476 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
1480 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1482 make_echo_request(void)
1484 struct ofp_header *rq;
1485 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1486 rq = ofpbuf_put_uninit(out, sizeof *rq);
1487 rq->version = OFP_VERSION;
1488 rq->type = OFPT_ECHO_REQUEST;
1489 rq->length = htons(sizeof *rq);
1494 /* Creates and returns an OFPT_ECHO_REPLY message matching the
1495 * OFPT_ECHO_REQUEST message in 'rq'. */
1497 make_echo_reply(const struct ofp_header *rq)
1499 size_t size = ntohs(rq->length);
1500 struct ofpbuf *out = ofpbuf_new(size);
1501 struct ofp_header *reply = ofpbuf_put(out, rq, size);
1502 reply->type = OFPT_ECHO_REPLY;
1506 const struct ofp_flow_stats *
1507 flow_stats_first(struct flow_stats_iterator *iter,
1508 const struct ofp_stats_reply *osr)
1510 iter->pos = osr->body;
1511 iter->end = osr->body + (ntohs(osr->header.length)
1512 - offsetof(struct ofp_stats_reply, body));
1513 return flow_stats_next(iter);
1516 const struct ofp_flow_stats *
1517 flow_stats_next(struct flow_stats_iterator *iter)
1519 ptrdiff_t bytes_left = iter->end - iter->pos;
1520 const struct ofp_flow_stats *fs;
1523 if (bytes_left < sizeof *fs) {
1524 if (bytes_left != 0) {
1525 VLOG_WARN_RL(&bad_ofmsg_rl,
1526 "%td leftover bytes in flow stats reply", bytes_left);
1531 fs = (const void *) iter->pos;
1532 length = ntohs(fs->length);
1533 if (length < sizeof *fs) {
1534 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu is shorter than "
1535 "min %zu", length, sizeof *fs);
1537 } else if (length > bytes_left) {
1538 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu but only %td "
1539 "bytes left", length, bytes_left);
1541 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1542 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu has %zu bytes "
1543 "left over in final action", length,
1544 (length - sizeof *fs) % sizeof fs->actions[0]);
1547 iter->pos += length;
1552 check_action_exact_len(const union ofp_action *a, unsigned int len,
1553 unsigned int required_len)
1555 if (len != required_len) {
1556 VLOG_DBG_RL(&bad_ofmsg_rl,
1557 "action %u has invalid length %"PRIu16" (must be %u)\n",
1558 a->type, ntohs(a->header.len), required_len);
1559 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1564 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
1565 * that the switch will never have more than 'max_ports' ports. Returns 0 if
1566 * 'port' is valid, otherwise an ofp_mkerr() return code. */
1568 check_output_port(uint16_t port, int max_ports)
1576 case OFPP_CONTROLLER:
1581 if (port < max_ports) {
1584 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
1585 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1589 /* Checks that 'action' is a valid OFPAT_ENQUEUE action, given that the switch
1590 * will never have more than 'max_ports' ports. Returns 0 if 'port' is valid,
1591 * otherwise an ofp_mkerr() return code. */
1593 check_enqueue_action(const union ofp_action *a, unsigned int len,
1596 const struct ofp_action_enqueue *oae;
1600 error = check_action_exact_len(a, len, 16);
1605 oae = (const struct ofp_action_enqueue *) a;
1606 port = ntohs(oae->port);
1607 if (port < max_ports || port == OFPP_IN_PORT) {
1610 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown enqueue port %x", port);
1611 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1615 check_nicira_action(const union ofp_action *a, unsigned int len,
1616 const struct flow *flow)
1618 const struct nx_action_header *nah;
1622 VLOG_DBG_RL(&bad_ofmsg_rl,
1623 "Nicira vendor action only %u bytes", len);
1624 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1626 nah = (const struct nx_action_header *) a;
1628 switch (ntohs(nah->subtype)) {
1629 case NXAST_RESUBMIT:
1630 case NXAST_SET_TUNNEL:
1631 case NXAST_DROP_SPOOFED_ARP:
1632 case NXAST_SET_QUEUE:
1633 case NXAST_POP_QUEUE:
1634 return check_action_exact_len(a, len, 16);
1636 case NXAST_REG_MOVE:
1637 error = check_action_exact_len(a, len,
1638 sizeof(struct nx_action_reg_move));
1642 return nxm_check_reg_move((const struct nx_action_reg_move *) a, flow);
1644 case NXAST_REG_LOAD:
1645 error = check_action_exact_len(a, len,
1646 sizeof(struct nx_action_reg_load));
1650 return nxm_check_reg_load((const struct nx_action_reg_load *) a, flow);
1656 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1661 check_action(const union ofp_action *a, unsigned int len,
1662 const struct flow *flow, int max_ports)
1666 switch (ntohs(a->type)) {
1668 error = check_action_exact_len(a, len, 8);
1672 return check_output_port(ntohs(a->output.port), max_ports);
1674 case OFPAT_SET_VLAN_VID:
1675 error = check_action_exact_len(a, len, 8);
1679 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1680 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1684 case OFPAT_SET_VLAN_PCP:
1685 error = check_action_exact_len(a, len, 8);
1689 if (a->vlan_vid.vlan_vid & ~7) {
1690 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
1694 case OFPAT_STRIP_VLAN:
1695 case OFPAT_SET_NW_SRC:
1696 case OFPAT_SET_NW_DST:
1697 case OFPAT_SET_NW_TOS:
1698 case OFPAT_SET_TP_SRC:
1699 case OFPAT_SET_TP_DST:
1700 return check_action_exact_len(a, len, 8);
1702 case OFPAT_SET_DL_SRC:
1703 case OFPAT_SET_DL_DST:
1704 return check_action_exact_len(a, len, 16);
1707 return (a->vendor.vendor == htonl(NX_VENDOR_ID)
1708 ? check_nicira_action(a, len, flow)
1709 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
1712 return check_enqueue_action(a, len, max_ports);
1715 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %"PRIu16,
1717 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
1722 validate_actions(const union ofp_action *actions, size_t n_actions,
1723 const struct flow *flow, int max_ports)
1727 for (i = 0; i < n_actions; ) {
1728 const union ofp_action *a = &actions[i];
1729 unsigned int len = ntohs(a->header.len);
1730 unsigned int n_slots = len / OFP_ACTION_ALIGN;
1731 unsigned int slots_left = &actions[n_actions] - a;
1734 if (n_slots > slots_left) {
1735 VLOG_DBG_RL(&bad_ofmsg_rl,
1736 "action requires %u slots but only %u remain",
1737 n_slots, slots_left);
1738 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1740 VLOG_DBG_RL(&bad_ofmsg_rl, "action has invalid length 0");
1741 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1742 } else if (len % OFP_ACTION_ALIGN) {
1743 VLOG_DBG_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
1744 "of %d", len, OFP_ACTION_ALIGN);
1745 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1748 error = check_action(a, len, flow, max_ports);
1757 /* Returns true if 'action' outputs to 'port' (which must be in network byte
1758 * order), false otherwise. */
1760 action_outputs_to_port(const union ofp_action *action, uint16_t port)
1762 switch (ntohs(action->type)) {
1764 return action->output.port == port;
1766 return ((const struct ofp_action_enqueue *) action)->port == port;
1772 /* The set of actions must either come from a trusted source or have been
1773 * previously validated with validate_actions(). */
1774 const union ofp_action *
1775 actions_first(struct actions_iterator *iter,
1776 const union ofp_action *oa, size_t n_actions)
1779 iter->end = oa + n_actions;
1780 return actions_next(iter);
1783 const union ofp_action *
1784 actions_next(struct actions_iterator *iter)
1786 if (iter->pos != iter->end) {
1787 const union ofp_action *a = iter->pos;
1788 unsigned int len = ntohs(a->header.len);
1789 iter->pos += len / OFP_ACTION_ALIGN;
1797 normalize_match(struct ofp_match *m)
1799 enum { OFPFW_NW = (OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO
1801 enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
1804 wc = ntohl(m->wildcards) & OVSFW_ALL;
1805 if (wc & OFPFW_DL_TYPE) {
1808 /* Can't sensibly match on network or transport headers if the
1809 * data link type is unknown. */
1810 wc |= OFPFW_NW | OFPFW_TP;
1811 m->nw_src = m->nw_dst = m->nw_proto = m->nw_tos = 0;
1812 m->tp_src = m->tp_dst = 0;
1813 } else if (m->dl_type == htons(ETH_TYPE_IP)) {
1814 if (wc & OFPFW_NW_PROTO) {
1817 /* Can't sensibly match on transport headers if the network
1818 * protocol is unknown. */
1820 m->tp_src = m->tp_dst = 0;
1821 } else if (m->nw_proto == IPPROTO_TCP ||
1822 m->nw_proto == IPPROTO_UDP ||
1823 m->nw_proto == IPPROTO_ICMP) {
1824 if (wc & OFPFW_TP_SRC) {
1827 if (wc & OFPFW_TP_DST) {
1831 /* Transport layer fields will always be extracted as zeros, so we
1832 * can do an exact-match on those values. */
1834 m->tp_src = m->tp_dst = 0;
1836 if (wc & OFPFW_NW_SRC_MASK) {
1837 m->nw_src &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_SRC_SHIFT);
1839 if (wc & OFPFW_NW_DST_MASK) {
1840 m->nw_dst &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_DST_SHIFT);
1842 if (wc & OFPFW_NW_TOS) {
1845 m->nw_tos &= IP_DSCP_MASK;
1847 } else if (m->dl_type == htons(ETH_TYPE_ARP)) {
1848 if (wc & OFPFW_NW_PROTO) {
1851 if (wc & OFPFW_NW_SRC_MASK) {
1852 m->nw_src &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_SRC_SHIFT);
1854 if (wc & OFPFW_NW_DST_MASK) {
1855 m->nw_dst &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_DST_SHIFT);
1857 m->tp_src = m->tp_dst = m->nw_tos = 0;
1859 /* Network and transport layer fields will always be extracted as
1860 * zeros, so we can do an exact-match on those values. */
1861 wc &= ~(OFPFW_NW | OFPFW_TP);
1862 m->nw_proto = m->nw_src = m->nw_dst = m->nw_tos = 0;
1863 m->tp_src = m->tp_dst = 0;
1865 if (wc & OFPFW_DL_SRC) {
1866 memset(m->dl_src, 0, sizeof m->dl_src);
1868 if (wc & OFPFW_DL_DST) {
1869 memset(m->dl_dst, 0, sizeof m->dl_dst);
1871 m->wildcards = htonl(wc);
1874 /* Returns a string that describes 'match' in a very literal way, without
1875 * interpreting its contents except in a very basic fashion. The returned
1876 * string is intended to be fixed-length, so that it is easy to see differences
1877 * between two such strings if one is put above another. This is useful for
1878 * describing changes made by normalize_match().
1880 * The caller must free the returned string (with free()). */
1882 ofp_match_to_literal_string(const struct ofp_match *match)
1884 return xasprintf("wildcards=%#10"PRIx32" "
1885 " in_port=%5"PRId16" "
1886 " dl_src="ETH_ADDR_FMT" "
1887 " dl_dst="ETH_ADDR_FMT" "
1888 " dl_vlan=%5"PRId16" "
1889 " dl_vlan_pcp=%3"PRId8" "
1890 " dl_type=%#6"PRIx16" "
1891 " nw_tos=%#4"PRIx8" "
1892 " nw_proto=%#4"PRIx16" "
1893 " nw_src=%#10"PRIx32" "
1894 " nw_dst=%#10"PRIx32" "
1895 " tp_src=%5"PRId16" "
1897 ntohl(match->wildcards),
1898 ntohs(match->in_port),
1899 ETH_ADDR_ARGS(match->dl_src),
1900 ETH_ADDR_ARGS(match->dl_dst),
1901 ntohs(match->dl_vlan),
1903 ntohs(match->dl_type),
1906 ntohl(match->nw_src),
1907 ntohl(match->nw_dst),
1908 ntohs(match->tp_src),
1909 ntohs(match->tp_dst));
1913 vendor_code_to_id(uint8_t code)
1916 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
1918 #undef OFPUTIL_VENDOR
1924 /* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
1925 * information taken from 'error', whose encoding must be as described in the
1926 * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
1927 * oh->xid as its transaction ID, and it will include up to the first 64 bytes
1930 * Returns NULL if 'error' is not an OpenFlow error code. */
1932 make_ofp_error_msg(int error, const struct ofp_header *oh)
1934 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1944 if (!is_ofp_error(error)) {
1945 /* We format 'error' with strerror() here since it seems likely to be
1946 * a system errno value. */
1947 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
1948 error, strerror(error));
1955 len = ntohs(oh->length);
1965 vendor = get_ofp_err_vendor(error);
1966 type = get_ofp_err_type(error);
1967 code = get_ofp_err_code(error);
1968 if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
1969 struct ofp_error_msg *oem;
1971 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
1972 oem->type = htons(type);
1973 oem->code = htons(code);
1975 struct ofp_error_msg *oem;
1976 struct nx_vendor_error *nve;
1979 vendor_id = vendor_code_to_id(vendor);
1980 if (vendor_id == UINT32_MAX) {
1981 VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
1986 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
1987 OFPT_ERROR, xid, &buf);
1988 oem->type = htons(NXET_VENDOR);
1989 oem->code = htons(NXVC_VENDOR_ERROR);
1991 nve = ofpbuf_put_uninit(buf, sizeof *nve);
1992 nve->vendor = htonl(vendor_id);
1993 nve->type = htons(type);
1994 nve->code = htons(code);
1998 ofpbuf_put(buf, data, len);
2004 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
2005 * successful, otherwise an OpenFlow error.
2007 * If successful, the first action is stored in '*actionsp' and the number of
2008 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
2009 * are stored, respectively.
2011 * This function does not check that the actions are valid (the caller should
2012 * do so, with validate_actions()). The caller is also responsible for making
2013 * sure that 'b->data' is initially aligned appropriately for "union
2016 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2017 union ofp_action **actionsp, size_t *n_actionsp)
2019 if (actions_len % OFP_ACTION_ALIGN != 0) {
2020 VLOG_DBG_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2021 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
2025 *actionsp = ofpbuf_try_pull(b, actions_len);
2026 if (*actionsp == NULL) {
2027 VLOG_DBG_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2028 "exceeds remaining message length (%zu)",
2029 actions_len, b->size);
2033 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
2039 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);