2 * Copyright (c) 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
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.
19 #include "byte-order.h"
20 #include "openvswitch/dynamic-string.h"
24 #include "openflow/nicira-ext.h"
25 #include "openflow/openflow.h"
26 #include "ovs-thread.h"
27 #include "openvswitch/vlog.h"
29 VLOG_DEFINE_THIS_MODULE(ofp_msgs);
32 #define OFPT10_STATS_REQUEST 16
33 #define OFPT10_STATS_REPLY 17
34 #define OFPT11_STATS_REQUEST 18
35 #define OFPT11_STATS_REPLY 19
36 #define OFPST_VENDOR 0xffff
38 /* Vendor extension message. */
39 struct ofp_vendor_header {
40 struct ofp_header header; /* OFPT_VENDOR. */
41 ovs_be32 vendor; /* Vendor ID:
42 * - MSB 0: low-order bytes are IEEE OUI.
43 * - MSB != 0: defined by OpenFlow
46 /* In theory everything after 'vendor' is vendor specific. In practice,
47 * the vendors we support put a 32-bit subtype here. We'll change this
48 * structure if we start adding support for other vendor formats. */
49 ovs_be32 subtype; /* Vendor-specific subtype. */
51 /* Followed by vendor-defined additional data. */
53 OFP_ASSERT(sizeof(struct ofp_vendor_header) == 16);
55 /* Statistics request or reply message. */
56 struct ofp10_stats_msg {
57 struct ofp_header header;
58 ovs_be16 type; /* One of the OFPST_* constants. */
59 ovs_be16 flags; /* Requests: always 0.
60 * Replies: 0 or OFPSF_REPLY_MORE. */
62 OFP_ASSERT(sizeof(struct ofp10_stats_msg) == 12);
64 /* Vendor extension stats message. */
65 struct ofp10_vendor_stats_msg {
66 struct ofp10_stats_msg osm; /* Type OFPST_VENDOR. */
67 ovs_be32 vendor; /* Vendor ID:
68 * - MSB 0: low-order bytes are IEEE OUI.
69 * - MSB != 0: defined by OpenFlow
71 /* Followed by vendor-defined arbitrary additional data. */
73 OFP_ASSERT(sizeof(struct ofp10_vendor_stats_msg) == 16);
75 struct ofp11_stats_msg {
76 struct ofp_header header;
77 ovs_be16 type; /* One of the OFPST_* constants. */
78 ovs_be16 flags; /* OFPSF_REQ_* flags (none yet defined). */
80 /* Followed by the body of the request. */
82 OFP_ASSERT(sizeof(struct ofp11_stats_msg) == 16);
84 /* Vendor extension stats message. */
85 struct ofp11_vendor_stats_msg {
86 struct ofp11_stats_msg osm; /* Type OFPST_VENDOR. */
87 ovs_be32 vendor; /* Vendor ID:
88 * - MSB 0: low-order bytes are IEEE OUI.
89 * - MSB != 0: defined by OpenFlow
92 /* In theory everything after 'vendor' is vendor specific. In practice,
93 * the vendors we support put a 32-bit subtype here. We'll change this
94 * structure if we start adding support for other vendor formats. */
95 ovs_be32 subtype; /* Vendor-specific subtype. */
97 /* Followed by vendor-defined additional data. */
99 OFP_ASSERT(sizeof(struct ofp11_vendor_stats_msg) == 24);
101 /* Header for Nicira vendor stats request and reply messages in OpenFlow
103 struct nicira10_stats_msg {
104 struct ofp10_vendor_stats_msg vsm; /* Vendor NX_VENDOR_ID. */
105 ovs_be32 subtype; /* One of NXST_* below. */
106 uint8_t pad[4]; /* Align to 64-bits. */
108 OFP_ASSERT(sizeof(struct nicira10_stats_msg) == 24);
110 /* A thin abstraction of OpenFlow headers:
112 * - 'version' and 'type' come straight from struct ofp_header, so these are
113 * always present and meaningful.
115 * - 'stat' comes from the 'type' member in statistics messages only. It is
116 * meaningful, therefore, only if 'version' and 'type' taken together
117 * specify a statistics request or reply. Otherwise it is 0.
119 * - 'vendor' is meaningful only for vendor messages, that is, if 'version'
120 * and 'type' specify a vendor message or if 'version' and 'type' specify
121 * a statistics message and 'stat' specifies a vendor statistic type.
124 * - 'subtype' is meaningful only for vendor messages and otherwise 0. It
125 * specifies a vendor-defined subtype. There is no standard format for
126 * these but 32 bits seems like it should be enough. */
128 uint8_t version; /* From ofp_header. */
129 uint8_t type; /* From ofp_header. */
130 uint16_t stat; /* From ofp10_stats_msg or ofp11_stats_msg. */
131 uint32_t vendor; /* From ofp_vendor_header,
132 * ofp10_vendor_stats_msg, or
133 * ofp11_vendor_stats_msg. */
134 uint32_t subtype; /* From nicira_header, nicira10_stats_msg, or
135 * nicira11_stats_msg. */
137 BUILD_ASSERT_DECL(sizeof(struct ofphdrs) == 12);
139 /* A mapping from OpenFlow headers to OFPRAW_*. */
140 struct raw_instance {
141 struct hmap_node hmap_node; /* In 'raw_instance_map'. */
142 struct ofphdrs hdrs; /* Key. */
143 enum ofpraw raw; /* Value. */
144 unsigned int hdrs_len; /* ofphdrs_len(hdrs). */
147 /* Information about a particular 'enum ofpraw'. */
149 /* All possible instantiations of this OFPRAW_* into OpenFlow headers. */
150 struct raw_instance *instances; /* min_version - max_version + 1 elems. */
154 unsigned int min_body;
155 unsigned int extra_multiple;
160 /* All understood OpenFlow message types, indexed by their 'struct ofphdrs'. */
161 static struct hmap raw_instance_map;
162 #include "ofp-msgs.inc"
164 static ovs_be32 alloc_xid(void);
166 /* ofphdrs functions. */
167 static uint32_t ofphdrs_hash(const struct ofphdrs *);
168 static bool ofphdrs_equal(const struct ofphdrs *a, const struct ofphdrs *b);
169 static enum ofperr ofphdrs_decode(struct ofphdrs *,
170 const struct ofp_header *oh, size_t length);
171 static void ofphdrs_decode_assert(struct ofphdrs *,
172 const struct ofp_header *oh, size_t length);
173 size_t ofphdrs_len(const struct ofphdrs *);
175 static const struct raw_info *raw_info_get(enum ofpraw);
176 static struct raw_instance *raw_instance_get(const struct raw_info *,
179 static enum ofperr ofpraw_from_ofphdrs(enum ofpraw *, const struct ofphdrs *);
181 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
185 static atomic_count next_xid = ATOMIC_COUNT_INIT(1);
187 return htonl(atomic_count_inc(&next_xid));
191 ofphdrs_hash(const struct ofphdrs *hdrs)
193 BUILD_ASSERT_DECL(sizeof *hdrs % 4 == 0);
194 return hash_bytes32((const uint32_t *) hdrs, sizeof *hdrs, 0);
198 ofphdrs_equal(const struct ofphdrs *a, const struct ofphdrs *b)
200 return !memcmp(a, b, sizeof *a);
204 log_bad_vendor(uint32_t vendor)
206 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
208 VLOG_WARN_RL(&rl, "OpenFlow message has unknown vendor %#"PRIx32, vendor);
212 ofphdrs_decode(struct ofphdrs *hdrs,
213 const struct ofp_header *oh, size_t length)
215 memset(hdrs, 0, sizeof *hdrs);
216 if (length < sizeof *oh) {
217 return OFPERR_OFPBRC_BAD_LEN;
220 /* Get base message version and type (OFPT_*). */
221 hdrs->version = oh->version;
222 hdrs->type = oh->type;
224 if (hdrs->type == OFPT_VENDOR) {
226 const struct ofp_vendor_header *ovh;
228 if (length < sizeof *ovh) {
229 return OFPERR_OFPBRC_BAD_LEN;
232 ovh = (const struct ofp_vendor_header *) oh;
233 hdrs->vendor = ntohl(ovh->vendor);
234 if (hdrs->vendor == NX_VENDOR_ID || hdrs->vendor == ONF_VENDOR_ID) {
235 hdrs->subtype = ntohl(ovh->subtype);
237 log_bad_vendor(hdrs->vendor);
238 return OFPERR_OFPBRC_BAD_VENDOR;
240 } else if (hdrs->version == OFP10_VERSION
241 && (hdrs->type == OFPT10_STATS_REQUEST ||
242 hdrs->type == OFPT10_STATS_REPLY)) {
243 const struct ofp10_stats_msg *osm;
245 /* Get statistic type (OFPST_*). */
246 if (length < sizeof *osm) {
247 return OFPERR_OFPBRC_BAD_LEN;
249 osm = (const struct ofp10_stats_msg *) oh;
250 hdrs->stat = ntohs(osm->type);
252 if (hdrs->stat == OFPST_VENDOR) {
254 const struct ofp10_vendor_stats_msg *ovsm;
256 if (length < sizeof *ovsm) {
257 return OFPERR_OFPBRC_BAD_LEN;
260 ovsm = (const struct ofp10_vendor_stats_msg *) oh;
261 hdrs->vendor = ntohl(ovsm->vendor);
262 if (hdrs->vendor == NX_VENDOR_ID) {
263 /* Get Nicira statistic type (NXST_*). */
264 const struct nicira10_stats_msg *nsm;
266 if (length < sizeof *nsm) {
267 return OFPERR_OFPBRC_BAD_LEN;
269 nsm = (const struct nicira10_stats_msg *) oh;
270 hdrs->subtype = ntohl(nsm->subtype);
272 log_bad_vendor(hdrs->vendor);
273 return OFPERR_OFPBRC_BAD_VENDOR;
276 } else if (hdrs->version != OFP10_VERSION
277 && (hdrs->type == OFPT11_STATS_REQUEST ||
278 hdrs->type == OFPT11_STATS_REPLY)) {
279 const struct ofp11_stats_msg *osm;
281 /* Get statistic type (OFPST_*). */
282 if (length < sizeof *osm) {
283 return OFPERR_OFPBRC_BAD_LEN;
285 osm = (const struct ofp11_stats_msg *) oh;
286 hdrs->stat = ntohs(osm->type);
288 if (hdrs->stat == OFPST_VENDOR) {
290 const struct ofp11_vendor_stats_msg *ovsm;
292 if (length < sizeof *ovsm) {
293 return OFPERR_OFPBRC_BAD_LEN;
296 ovsm = (const struct ofp11_vendor_stats_msg *) oh;
297 hdrs->vendor = ntohl(ovsm->vendor);
298 if (hdrs->vendor == NX_VENDOR_ID ||
299 hdrs->vendor == ONF_VENDOR_ID) {
300 hdrs->subtype = ntohl(ovsm->subtype);
302 log_bad_vendor(hdrs->vendor);
303 return OFPERR_OFPBRC_BAD_VENDOR;
312 ofphdrs_decode_assert(struct ofphdrs *hdrs,
313 const struct ofp_header *oh, size_t length)
315 enum ofperr error = ofphdrs_decode(hdrs, oh, length);
320 ofp_is_stat_request(enum ofp_version version, uint8_t type)
324 return type == OFPT10_STATS_REQUEST;
330 return type == OFPT11_STATS_REQUEST;
337 ofp_is_stat_reply(enum ofp_version version, uint8_t type)
341 return type == OFPT10_STATS_REPLY;
347 return type == OFPT11_STATS_REPLY;
354 ofp_is_stat(enum ofp_version version, uint8_t type)
356 return (ofp_is_stat_request(version, type) ||
357 ofp_is_stat_reply(version, type));
361 ofphdrs_is_stat(const struct ofphdrs *hdrs)
363 return ofp_is_stat(hdrs->version, hdrs->type);
367 ofphdrs_len(const struct ofphdrs *hdrs)
369 if (hdrs->type == OFPT_VENDOR) {
370 return sizeof(struct ofp_vendor_header);
373 switch ((enum ofp_version) hdrs->version) {
375 if (hdrs->type == OFPT10_STATS_REQUEST ||
376 hdrs->type == OFPT10_STATS_REPLY) {
377 return (hdrs->stat == OFPST_VENDOR
378 ? sizeof(struct nicira10_stats_msg)
379 : sizeof(struct ofp10_stats_msg));
388 if (hdrs->type == OFPT11_STATS_REQUEST ||
389 hdrs->type == OFPT11_STATS_REPLY) {
390 return (hdrs->stat == OFPST_VENDOR
391 ? sizeof(struct ofp11_vendor_stats_msg)
392 : sizeof(struct ofp11_stats_msg));
397 return sizeof(struct ofp_header);
400 /* Determines the OFPRAW_* type of the OpenFlow message at 'oh', which has
401 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
402 * data are readable at 'oh'.) On success, returns 0 and stores the type into
403 * '*raw'. On failure, returns an OFPERR_* error code and zeros '*raw'.
405 * This function checks that 'oh' is a valid length for its particular type of
406 * message, and returns an error if not. */
408 ofpraw_decode(enum ofpraw *raw, const struct ofp_header *oh)
410 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
411 return ofpraw_pull(raw, &msg);
414 /* Does the same job as ofpraw_decode(), except that it assert-fails if
415 * ofpraw_decode() would have reported an error. Thus, it's able to use the
416 * return value for the OFPRAW_* message type instead of an error code.
418 * (It only makes sense to use this function if you previously called
419 * ofpraw_decode() on the message and thus know that it's OK.) */
421 ofpraw_decode_assert(const struct ofp_header *oh)
426 error = ofpraw_decode(&raw, oh);
431 /* Determines the OFPRAW_* type of the OpenFlow message in 'msg', which starts
432 * at 'msg->data' and has length 'msg->size' bytes. On success,
433 * returns 0 and stores the type into '*rawp'. On failure, returns an OFPERR_*
434 * error code and zeros '*rawp'.
436 * This function checks that the message has a valid length for its particular
437 * type of message, and returns an error if not.
439 * In addition to setting '*rawp', this function pulls off the OpenFlow header
440 * (including the stats headers, vendor header, and any subtype header) with
441 * ofpbuf_pull(). It also sets 'msg->header' to the start of the OpenFlow
442 * header and 'msg->msg' just beyond the headers (that is, to the final value
445 ofpraw_pull(enum ofpraw *rawp, struct ofpbuf *msg)
447 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
449 const struct raw_instance *instance;
450 const struct raw_info *info;
453 unsigned int min_len;
459 /* Set default outputs. */
460 msg->header = msg->data;
461 msg->msg = msg->header;
465 error = ofphdrs_decode(&hdrs, msg->data, len);
470 error = ofpraw_from_ofphdrs(&raw, &hdrs);
475 info = raw_info_get(raw);
476 instance = raw_instance_get(info, hdrs.version);
477 msg->header = ofpbuf_pull(msg, instance->hdrs_len);
478 msg->msg = msg->data;
480 min_len = instance->hdrs_len + info->min_body;
481 switch (info->extra_multiple) {
483 if (len != min_len) {
484 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (expected "
485 "length %u)", info->name, len, min_len);
486 return OFPERR_OFPBRC_BAD_LEN;
492 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (expected "
493 "length at least %u bytes)",
494 info->name, len, min_len);
495 return OFPERR_OFPBRC_BAD_LEN;
500 if (len < min_len || (len - min_len) % info->extra_multiple) {
501 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (must be "
502 "exactly %u bytes or longer by an integer multiple "
504 info->name, len, min_len, info->extra_multiple);
505 return OFPERR_OFPBRC_BAD_LEN;
514 /* Does the same job as ofpraw_pull(), except that it assert-fails if
515 * ofpraw_pull() would have reported an error. Thus, it's able to use the
516 * return value for the OFPRAW_* message type instead of an error code.
518 * (It only makes sense to use this function if you previously called
519 * ofpraw_decode() on the message and thus know that it's OK.) */
521 ofpraw_pull_assert(struct ofpbuf *msg)
526 error = ofpraw_pull(&raw, msg);
531 /* Determines the OFPRAW_* type of the OpenFlow message that starts at 'oh' and
532 * has length 'length' bytes. On success, returns 0 and stores the type into
533 * '*rawp'. On failure, returns an OFPERR_* error code and zeros '*rawp'.
535 * Unlike other functions for decoding message types, this one is not picky
536 * about message length. For example, it will successfully decode a message
537 * whose body is shorter than the minimum length for a message of its type.
538 * Thus, this is the correct function to use for decoding the type of a message
539 * that might have been truncated, such as the payload of an OpenFlow error
540 * message (which is allowed to be truncated to 64 bytes). */
542 ofpraw_decode_partial(enum ofpraw *raw,
543 const struct ofp_header *oh, size_t length)
548 error = ofphdrs_decode(&hdrs, oh, length);
550 error = ofpraw_from_ofphdrs(raw, &hdrs);
559 /* Encoding messages using OFPRAW_* values. */
561 static void ofpraw_put__(enum ofpraw, uint8_t version, ovs_be32 xid,
562 size_t extra_tailroom, struct ofpbuf *);
564 /* Allocates and returns a new ofpbuf that contains an OpenFlow header for
565 * 'raw' with OpenFlow version 'version' and a fresh OpenFlow transaction ID.
566 * The ofpbuf has enough tailroom for the minimum body length of 'raw', plus
567 * 'extra_tailroom' additional bytes.
569 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
570 * must specify a valid (raw, version) pair.
572 * In the returned ofpbuf, 'header' points to the beginning of the
573 * OpenFlow header and 'msg' points just after it, to where the
574 * message's body will start. The caller must actually allocate the
575 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
577 * The caller owns the returned ofpbuf and must free it when it is no longer
578 * needed, e.g. with ofpbuf_delete(). */
580 ofpraw_alloc(enum ofpraw raw, uint8_t version, size_t extra_tailroom)
582 return ofpraw_alloc_xid(raw, version, alloc_xid(), extra_tailroom);
585 /* Same as ofpraw_alloc() but the caller provides the transaction ID. */
587 ofpraw_alloc_xid(enum ofpraw raw, uint8_t version, ovs_be32 xid,
588 size_t extra_tailroom)
590 struct ofpbuf *buf = ofpbuf_new(0);
591 ofpraw_put__(raw, version, xid, extra_tailroom, buf);
595 /* Same as ofpraw_alloc(), but obtains the OpenFlow version and transaction ID
596 * from 'request->version' and 'request->xid', respectively.
598 * Even though the version comes from 'request->version', the caller must still
599 * know what it is doing, by specifying a valid pairing of 'raw' and
600 * 'request->version', just like ofpraw_alloc(). */
602 ofpraw_alloc_reply(enum ofpraw raw, const struct ofp_header *request,
603 size_t extra_tailroom)
605 return ofpraw_alloc_xid(raw, request->version, request->xid,
609 /* Allocates and returns a new ofpbuf that contains an OpenFlow header that is
610 * a stats reply to the stats request in 'request', using the same OpenFlow
611 * version and transaction ID as 'request'. The ofpbuf has enough tailroom for
612 * the stats reply's minimum body length, plus 'extra_tailroom' additional
615 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
616 * value. Every stats request has a corresponding reply, so the (raw, version)
617 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
619 * In the returned ofpbuf, 'header' points to the beginning of the
620 * OpenFlow header and 'msg' points just after it, to where the
621 * message's body will start. The caller must actually allocate the
622 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
624 * The caller owns the returned ofpbuf and must free it when it is no longer
625 * needed, e.g. with ofpbuf_delete(). */
627 ofpraw_alloc_stats_reply(const struct ofp_header *request,
628 size_t extra_tailroom)
630 enum ofpraw request_raw;
631 enum ofpraw reply_raw;
634 error = ofpraw_decode_partial(&request_raw, request,
635 ntohs(request->length));
638 reply_raw = ofpraw_stats_request_to_reply(request_raw, request->version);
639 ovs_assert(reply_raw);
641 return ofpraw_alloc_reply(reply_raw, request, extra_tailroom);
644 /* Appends to 'buf' an OpenFlow header for 'raw' with OpenFlow version
645 * 'version' and a fresh OpenFlow transaction ID. Preallocates enough tailroom
646 * in 'buf' for the minimum body length of 'raw', plus 'extra_tailroom'
649 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
650 * must specify a valid (raw, version) pair.
652 * Upon return, 'buf->header' points to the beginning of the OpenFlow header
653 * and 'buf->msg' points just after it, to where the message's body will start.
654 * The caller must actually allocating the body into the space reserved for it,
655 * e.g. with ofpbuf_put_uninit(). */
657 ofpraw_put(enum ofpraw raw, uint8_t version, struct ofpbuf *buf)
659 ofpraw_put__(raw, version, alloc_xid(), 0, buf);
662 /* Same as ofpraw_put() but the caller provides the transaction ID. */
664 ofpraw_put_xid(enum ofpraw raw, uint8_t version, ovs_be32 xid,
667 ofpraw_put__(raw, version, xid, 0, buf);
670 /* Same as ofpraw_put(), but obtains the OpenFlow version and transaction ID
671 * from 'request->version' and 'request->xid', respectively.
673 * Even though the version comes from 'request->version', the caller must still
674 * know what it is doing, by specifying a valid pairing of 'raw' and
675 * 'request->version', just like ofpraw_put(). */
677 ofpraw_put_reply(enum ofpraw raw, const struct ofp_header *request,
680 ofpraw_put__(raw, request->version, request->xid, 0, buf);
683 /* Appends to 'buf' an OpenFlow header that is a stats reply to the stats
684 * request in 'request', using the same OpenFlow version and transaction ID as
685 * 'request'. Preallocate enough tailroom in 'buf for the stats reply's
686 * minimum body length, plus 'extra_tailroom' additional bytes.
688 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
689 * value. Every stats request has a corresponding reply, so the (raw, version)
690 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
692 * In the returned ofpbuf, 'header' points to the beginning of the
693 * OpenFlow header and 'msg' points just after it, to where the
694 * message's body will start. The caller must actually allocate the
695 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
697 * The caller owns the returned ofpbuf and must free it when it is no longer
698 * needed, e.g. with ofpbuf_delete(). */
700 ofpraw_put_stats_reply(const struct ofp_header *request, struct ofpbuf *buf)
705 error = ofpraw_decode_partial(&raw, request, ntohs(request->length));
708 raw = ofpraw_stats_request_to_reply(raw, request->version);
711 ofpraw_put__(raw, request->version, request->xid, 0, buf);
715 ofpraw_put__(enum ofpraw raw, uint8_t version, ovs_be32 xid,
716 size_t extra_tailroom, struct ofpbuf *buf)
718 const struct raw_info *info = raw_info_get(raw);
719 const struct raw_instance *instance = raw_instance_get(info, version);
720 const struct ofphdrs *hdrs = &instance->hdrs;
721 struct ofp_header *oh;
723 ofpbuf_prealloc_tailroom(buf, (instance->hdrs_len + info->min_body
725 buf->header = ofpbuf_put_uninit(buf, instance->hdrs_len);
726 buf->msg = ofpbuf_tail(buf);
729 oh->version = version;
730 oh->type = hdrs->type;
731 oh->length = htons(buf->size);
734 if (hdrs->type == OFPT_VENDOR) {
735 struct ofp_vendor_header *ovh = buf->header;
737 ovh->vendor = htonl(hdrs->vendor);
738 ovh->subtype = htonl(hdrs->subtype);
739 } else if (version == OFP10_VERSION
740 && (hdrs->type == OFPT10_STATS_REQUEST ||
741 hdrs->type == OFPT10_STATS_REPLY)) {
742 struct ofp10_stats_msg *osm = buf->header;
744 osm->type = htons(hdrs->stat);
745 osm->flags = htons(0);
747 if (hdrs->stat == OFPST_VENDOR) {
748 struct ofp10_vendor_stats_msg *ovsm = buf->header;
750 ovsm->vendor = htonl(hdrs->vendor);
751 if (hdrs->vendor == NX_VENDOR_ID) {
752 struct nicira10_stats_msg *nsm = buf->header;
754 nsm->subtype = htonl(hdrs->subtype);
755 memset(nsm->pad, 0, sizeof nsm->pad);
760 } else if (version != OFP10_VERSION
761 && (hdrs->type == OFPT11_STATS_REQUEST ||
762 hdrs->type == OFPT11_STATS_REPLY)) {
763 struct ofp11_stats_msg *osm = buf->header;
765 osm->type = htons(hdrs->stat);
766 osm->flags = htons(0);
767 memset(osm->pad, 0, sizeof osm->pad);
769 if (hdrs->stat == OFPST_VENDOR) {
770 struct ofp11_vendor_stats_msg *ovsm = buf->header;
772 ovsm->vendor = htonl(hdrs->vendor);
773 ovsm->subtype = htonl(hdrs->subtype);
778 /* Returns 'raw''s name.
780 * The name is the name used for 'raw' in the OpenFlow specification. For
781 * example, ofpraw_get_name(OFPRAW_OFPT10_FEATURES_REPLY) is
782 * "OFPT_FEATURES_REPLY".
784 * The caller must not modify or free the returned string. */
786 ofpraw_get_name(enum ofpraw raw)
788 return raw_info_get(raw)->name;
791 /* Returns the stats reply that corresponds to 'raw' in the given OpenFlow
794 ofpraw_stats_request_to_reply(enum ofpraw raw, uint8_t version)
796 const struct raw_info *info = raw_info_get(raw);
797 const struct raw_instance *instance = raw_instance_get(info, version);
798 enum ofpraw reply_raw;
802 hdrs = instance->hdrs;
803 switch ((enum ofp_version)hdrs.version) {
805 ovs_assert(hdrs.type == OFPT10_STATS_REQUEST);
806 hdrs.type = OFPT10_STATS_REPLY;
813 ovs_assert(hdrs.type == OFPT11_STATS_REQUEST);
814 hdrs.type = OFPT11_STATS_REPLY;
820 error = ofpraw_from_ofphdrs(&reply_raw, &hdrs);
826 /* Determines the OFPTYPE_* type of the OpenFlow message at 'oh', which has
827 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
828 * data are readable at 'oh'.) On success, returns 0 and stores the type into
829 * '*typep'. On failure, returns an OFPERR_* error code and zeros '*typep'.
831 * This function checks that 'oh' is a valid length for its particular type of
832 * message, and returns an error if not. */
834 ofptype_decode(enum ofptype *typep, const struct ofp_header *oh)
839 error = ofpraw_decode(&raw, oh);
840 *typep = error ? 0 : ofptype_from_ofpraw(raw);
844 /* Determines the OFPTYPE_* type of the OpenFlow message in 'msg', which starts
845 * at 'msg->data' and has length 'msg->size' bytes. On success,
846 * returns 0 and stores the type into '*typep'. On failure, returns an
847 * OFPERR_* error code and zeros '*typep'.
849 * This function checks that the message has a valid length for its particular
850 * type of message, and returns an error if not.
852 * In addition to setting '*typep', this function pulls off the OpenFlow header
853 * (including the stats headers, vendor header, and any subtype header) with
854 * ofpbuf_pull(). It also sets 'msg->header' to the start of the OpenFlow
855 * header and 'msg->msg' just beyond the headers (that is, to the final value
858 ofptype_pull(enum ofptype *typep, struct ofpbuf *buf)
863 error = ofpraw_pull(&raw, buf);
864 *typep = error ? 0 : ofptype_from_ofpraw(raw);
868 /* Returns the OFPTYPE_* type that corresponds to 'raw'.
870 * (This is a one-way trip, because the mapping from ofpraw to ofptype is
873 ofptype_from_ofpraw(enum ofpraw raw)
875 return raw_info_get(raw)->type;
879 ofptype_get_name(enum ofptype type)
881 ovs_assert(type < ARRAY_SIZE(type_names));
882 return type_names[type];
885 /* Updates the 'length' field of the OpenFlow message in 'buf' to
888 ofpmsg_update_length(struct ofpbuf *buf)
890 struct ofp_header *oh = ofpbuf_at_assert(buf, 0, sizeof *oh);
891 oh->length = htons(buf->size);
894 /* Returns just past the OpenFlow header (including the stats headers, vendor
895 * header, and any subtype header) in 'oh'. */
897 ofpmsg_body(const struct ofp_header *oh)
901 ofphdrs_decode_assert(&hdrs, oh, ntohs(oh->length));
902 return (const uint8_t *) oh + ofphdrs_len(&hdrs);
905 /* Return if it's a stat/multipart (OFPST) request message. */
907 ofpmsg_is_stat_request(const struct ofp_header *oh)
909 return ofp_is_stat_request(oh->version, oh->type);
912 static ovs_be16 *ofpmp_flags__(const struct ofp_header *);
914 /* Initializes 'replies' as a new list of stats messages that reply to
915 * 'request', which must be a stats request message. Initially the list will
916 * consist of only a single reply part without any body. The caller should
917 * use calls to the other ofpmp_*() functions to add to the body and split the
918 * message into multiple parts, if necessary. */
920 ofpmp_init(struct ovs_list *replies, const struct ofp_header *request)
924 ovs_list_init(replies);
926 msg = ofpraw_alloc_stats_reply(request, 1000);
927 ovs_list_push_back(replies, &msg->list_node);
930 /* Prepares to append up to 'len' bytes to the series of statistics replies in
931 * 'replies', which should have been initialized with ofpmp_init(), if
932 * necessary adding a new reply to the list.
934 * Returns an ofpbuf with at least 'len' bytes of tailroom. The 'len' bytes
935 * have not actually been allocated, so the caller must do so with
936 * e.g. ofpbuf_put_uninit(). */
938 ofpmp_reserve(struct ovs_list *replies, size_t len)
940 struct ofpbuf *msg = ofpbuf_from_list(ovs_list_back(replies));
942 if (msg->size + len <= UINT16_MAX) {
943 ofpbuf_prealloc_tailroom(msg, len);
946 unsigned int hdrs_len;
950 ofphdrs_decode_assert(&hdrs, msg->data, msg->size);
951 hdrs_len = ofphdrs_len(&hdrs);
953 next = ofpbuf_new(MAX(1024, hdrs_len + len));
954 ofpbuf_put(next, msg->data, hdrs_len);
955 next->header = next->data;
956 next->msg = ofpbuf_tail(next);
957 ovs_list_push_back(replies, &next->list_node);
959 *ofpmp_flags__(msg->data) |= htons(OFPSF_REPLY_MORE);
965 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
966 * returns the first byte. */
968 ofpmp_append(struct ovs_list *replies, size_t len)
970 return ofpbuf_put_uninit(ofpmp_reserve(replies, len), len);
973 /* Sometimes, when composing stats replies, it's difficult to predict how long
974 * an individual reply chunk will be before actually encoding it into the reply
975 * buffer. This function allows easy handling of this case: just encode the
976 * reply, then use this function to break the message into two pieces if it
977 * exceeds the OpenFlow message limit.
979 * In detail, if the final stats message in 'replies' is too long for OpenFlow,
980 * this function breaks it into two separate stats replies, the first one with
981 * the first 'start_ofs' bytes, the second one containing the bytes from that
984 ofpmp_postappend(struct ovs_list *replies, size_t start_ofs)
986 struct ofpbuf *msg = ofpbuf_from_list(ovs_list_back(replies));
988 ovs_assert(start_ofs <= UINT16_MAX);
989 if (msg->size > UINT16_MAX) {
990 size_t len = msg->size - start_ofs;
991 memcpy(ofpmp_append(replies, len),
992 (const uint8_t *) msg->data + start_ofs, len);
993 msg->size = start_ofs;
997 /* Returns the OpenFlow version of the replies being constructed in 'replies',
998 * which should have been initialized by ofpmp_init(). */
1000 ofpmp_version(struct ovs_list *replies)
1002 struct ofpbuf *msg = ofpbuf_from_list(ovs_list_back(replies));
1003 const struct ofp_header *oh = msg->data;
1008 /* Determines the OFPRAW_* type of the OpenFlow messages in 'replies', which
1009 * should have been initialized by ofpmp_init(). */
1011 ofpmp_decode_raw(struct ovs_list *replies)
1013 struct ofpbuf *msg = ofpbuf_from_list(ovs_list_back(replies));
1017 error = ofpraw_decode_partial(&raw, msg->data, msg->size);
1023 ofpmp_flags__(const struct ofp_header *oh)
1025 switch ((enum ofp_version)oh->version) {
1027 return &((struct ofp10_stats_msg *) oh)->flags;
1033 return &((struct ofp11_stats_msg *) oh)->flags;
1039 /* Returns the OFPSF_* flags found in the OpenFlow stats header of 'oh', which
1040 * must be an OpenFlow stats request or reply.
1042 * (OFPSF_REPLY_MORE is the only defined flag.) */
1044 ofpmp_flags(const struct ofp_header *oh)
1046 return ntohs(*ofpmp_flags__(oh));
1049 /* Returns true if the OFPSF_REPLY_MORE flag is set in the OpenFlow stats
1050 * header of 'oh', which must be an OpenFlow stats request or reply, false if
1053 ofpmp_more(const struct ofp_header *oh)
1055 return (ofpmp_flags(oh) & OFPSF_REPLY_MORE) != 0;
1058 static void ofpmsgs_init(void);
1060 static const struct raw_info *
1061 raw_info_get(enum ofpraw raw)
1065 ovs_assert(raw < ARRAY_SIZE(raw_infos));
1066 return &raw_infos[raw];
1069 static struct raw_instance *
1070 raw_instance_get(const struct raw_info *info, uint8_t version)
1072 ovs_assert(version >= info->min_version && version <= info->max_version);
1073 return &info->instances[version - info->min_version];
1077 ofpraw_from_ofphdrs(enum ofpraw *raw, const struct ofphdrs *hdrs)
1079 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1081 struct raw_instance *raw_hdrs;
1086 hash = ofphdrs_hash(hdrs);
1087 HMAP_FOR_EACH_WITH_HASH (raw_hdrs, hmap_node, hash, &raw_instance_map) {
1088 if (ofphdrs_equal(hdrs, &raw_hdrs->hdrs)) {
1089 *raw = raw_hdrs->raw;
1094 if (!VLOG_DROP_WARN(&rl)) {
1098 ds_put_format(&s, "version %"PRIu8", type %"PRIu8,
1099 hdrs->version, hdrs->type);
1100 if (ofphdrs_is_stat(hdrs)) {
1101 ds_put_format(&s, ", stat %"PRIu16, hdrs->stat);
1104 ds_put_format(&s, ", vendor 0x%"PRIx32", subtype %"PRIu32,
1105 hdrs->vendor, hdrs->subtype);
1107 VLOG_WARN("unknown OpenFlow message (%s)", ds_cstr(&s));
1111 return (hdrs->vendor ? OFPERR_OFPBRC_BAD_SUBTYPE
1112 : ofphdrs_is_stat(hdrs) ? OFPERR_OFPBRC_BAD_STAT
1113 : OFPERR_OFPBRC_BAD_TYPE);
1119 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1120 const struct raw_info *info;
1122 if (!ovsthread_once_start(&once)) {
1126 hmap_init(&raw_instance_map);
1127 for (info = raw_infos; info < &raw_infos[ARRAY_SIZE(raw_infos)]; info++)
1129 int n_instances = info->max_version - info->min_version + 1;
1130 struct raw_instance *inst;
1132 for (inst = info->instances;
1133 inst < &info->instances[n_instances];
1135 inst->hdrs_len = ofphdrs_len(&inst->hdrs);
1136 hmap_insert(&raw_instance_map, &inst->hmap_node,
1137 ofphdrs_hash(&inst->hdrs));
1141 ovsthread_once_done(&once);