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.
18 #include "byte-order.h"
21 #include "openflow/nicira-ext.h"
22 #include "openflow/openflow.h"
23 #include "openvswitch/dynamic-string.h"
24 #include "openvswitch/ofp-msgs.h"
25 #include "openvswitch/ofpbuf.h"
26 #include "openvswitch/vlog.h"
27 #include "ovs-thread.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;
331 return type == OFPT11_STATS_REQUEST;
338 ofp_is_stat_reply(enum ofp_version version, uint8_t type)
342 return type == OFPT10_STATS_REPLY;
349 return type == OFPT11_STATS_REPLY;
356 ofp_is_stat(enum ofp_version version, uint8_t type)
358 return (ofp_is_stat_request(version, type) ||
359 ofp_is_stat_reply(version, type));
363 ofphdrs_is_stat(const struct ofphdrs *hdrs)
365 return ofp_is_stat(hdrs->version, hdrs->type);
369 ofphdrs_len(const struct ofphdrs *hdrs)
371 if (hdrs->type == OFPT_VENDOR) {
372 return sizeof(struct ofp_vendor_header);
375 switch ((enum ofp_version) hdrs->version) {
377 if (hdrs->type == OFPT10_STATS_REQUEST ||
378 hdrs->type == OFPT10_STATS_REPLY) {
379 return (hdrs->stat == OFPST_VENDOR
380 ? sizeof(struct nicira10_stats_msg)
381 : sizeof(struct ofp10_stats_msg));
391 if (hdrs->type == OFPT11_STATS_REQUEST ||
392 hdrs->type == OFPT11_STATS_REPLY) {
393 return (hdrs->stat == OFPST_VENDOR
394 ? sizeof(struct ofp11_vendor_stats_msg)
395 : sizeof(struct ofp11_stats_msg));
400 return sizeof(struct ofp_header);
403 /* Determines the OFPRAW_* type of the OpenFlow message at 'oh', which has
404 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
405 * data are readable at 'oh'.) On success, returns 0 and stores the type into
406 * '*raw'. On failure, returns an OFPERR_* error code and zeros '*raw'.
408 * This function checks that 'oh' is a valid length for its particular type of
409 * message, and returns an error if not. */
411 ofpraw_decode(enum ofpraw *raw, const struct ofp_header *oh)
413 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
414 return ofpraw_pull(raw, &msg);
417 /* Does the same job as ofpraw_decode(), except that it assert-fails if
418 * ofpraw_decode() would have reported an error. Thus, it's able to use the
419 * return value for the OFPRAW_* message type instead of an error code.
421 * (It only makes sense to use this function if you previously called
422 * ofpraw_decode() on the message and thus know that it's OK.) */
424 ofpraw_decode_assert(const struct ofp_header *oh)
429 error = ofpraw_decode(&raw, oh);
434 /* Determines the OFPRAW_* type of the OpenFlow message in 'msg', which starts
435 * at 'msg->data' and has length 'msg->size' bytes. On success,
436 * returns 0 and stores the type into '*rawp'. On failure, returns an OFPERR_*
437 * error code and zeros '*rawp'.
439 * This function checks that the message has a valid length for its particular
440 * type of message, and returns an error if not.
442 * In addition to setting '*rawp', this function pulls off the OpenFlow header
443 * (including the stats headers, vendor header, and any subtype header) with
444 * ofpbuf_pull(). It also sets 'msg->header' to the start of the OpenFlow
445 * header and 'msg->msg' just beyond the headers (that is, to the final value
448 ofpraw_pull(enum ofpraw *rawp, struct ofpbuf *msg)
450 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
452 const struct raw_instance *instance;
453 const struct raw_info *info;
456 unsigned int min_len;
462 /* Set default outputs. */
463 msg->header = msg->data;
464 msg->msg = msg->header;
468 error = ofphdrs_decode(&hdrs, msg->data, len);
473 error = ofpraw_from_ofphdrs(&raw, &hdrs);
478 info = raw_info_get(raw);
479 instance = raw_instance_get(info, hdrs.version);
480 msg->header = ofpbuf_pull(msg, instance->hdrs_len);
481 msg->msg = msg->data;
483 min_len = instance->hdrs_len + info->min_body;
484 switch (info->extra_multiple) {
486 if (len != min_len) {
487 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (expected "
488 "length %u)", info->name, len, min_len);
489 return OFPERR_OFPBRC_BAD_LEN;
495 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (expected "
496 "length at least %u bytes)",
497 info->name, len, min_len);
498 return OFPERR_OFPBRC_BAD_LEN;
503 if (len < min_len || (len - min_len) % info->extra_multiple) {
504 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (must be "
505 "exactly %u bytes or longer by an integer multiple "
507 info->name, len, min_len, info->extra_multiple);
508 return OFPERR_OFPBRC_BAD_LEN;
517 /* Does the same job as ofpraw_pull(), except that it assert-fails if
518 * ofpraw_pull() would have reported an error. Thus, it's able to use the
519 * return value for the OFPRAW_* message type instead of an error code.
521 * (It only makes sense to use this function if you previously called
522 * ofpraw_decode() on the message and thus know that it's OK.) */
524 ofpraw_pull_assert(struct ofpbuf *msg)
529 error = ofpraw_pull(&raw, msg);
534 /* Determines the OFPRAW_* type of the OpenFlow message that starts at 'oh' and
535 * has length 'length' bytes. On success, returns 0 and stores the type into
536 * '*rawp'. On failure, returns an OFPERR_* error code and zeros '*rawp'.
538 * Unlike other functions for decoding message types, this one is not picky
539 * about message length. For example, it will successfully decode a message
540 * whose body is shorter than the minimum length for a message of its type.
541 * Thus, this is the correct function to use for decoding the type of a message
542 * that might have been truncated, such as the payload of an OpenFlow error
543 * message (which is allowed to be truncated to 64 bytes). */
545 ofpraw_decode_partial(enum ofpraw *raw,
546 const struct ofp_header *oh, size_t length)
551 error = ofphdrs_decode(&hdrs, oh, length);
553 error = ofpraw_from_ofphdrs(raw, &hdrs);
562 /* Encoding messages using OFPRAW_* values. */
564 static void ofpraw_put__(enum ofpraw, uint8_t version, ovs_be32 xid,
565 size_t extra_tailroom, struct ofpbuf *);
567 /* Allocates and returns a new ofpbuf that contains an OpenFlow header for
568 * 'raw' with OpenFlow version 'version' and a fresh OpenFlow transaction ID.
569 * The ofpbuf has enough tailroom for the minimum body length of 'raw', plus
570 * 'extra_tailroom' additional bytes.
572 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
573 * must specify a valid (raw, version) pair.
575 * In the returned ofpbuf, 'header' points to the beginning of the
576 * OpenFlow header and 'msg' points just after it, to where the
577 * message's body will start. The caller must actually allocate the
578 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
580 * The caller owns the returned ofpbuf and must free it when it is no longer
581 * needed, e.g. with ofpbuf_delete(). */
583 ofpraw_alloc(enum ofpraw raw, uint8_t version, size_t extra_tailroom)
585 return ofpraw_alloc_xid(raw, version, alloc_xid(), extra_tailroom);
588 /* Same as ofpraw_alloc() but the caller provides the transaction ID. */
590 ofpraw_alloc_xid(enum ofpraw raw, uint8_t version, ovs_be32 xid,
591 size_t extra_tailroom)
593 struct ofpbuf *buf = ofpbuf_new(0);
594 ofpraw_put__(raw, version, xid, extra_tailroom, buf);
598 /* Same as ofpraw_alloc(), but obtains the OpenFlow version and transaction ID
599 * from 'request->version' and 'request->xid', respectively.
601 * Even though the version comes from 'request->version', the caller must still
602 * know what it is doing, by specifying a valid pairing of 'raw' and
603 * 'request->version', just like ofpraw_alloc(). */
605 ofpraw_alloc_reply(enum ofpraw raw, const struct ofp_header *request,
606 size_t extra_tailroom)
608 return ofpraw_alloc_xid(raw, request->version, request->xid,
612 /* Allocates and returns a new ofpbuf that contains an OpenFlow header that is
613 * a stats reply to the stats request in 'request', using the same OpenFlow
614 * version and transaction ID as 'request'. The ofpbuf has enough tailroom for
615 * the stats reply's minimum body length, plus 'extra_tailroom' additional
618 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
619 * value. Every stats request has a corresponding reply, so the (raw, version)
620 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
622 * In the returned ofpbuf, 'header' points to the beginning of the
623 * OpenFlow header and 'msg' points just after it, to where the
624 * message's body will start. The caller must actually allocate the
625 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
627 * The caller owns the returned ofpbuf and must free it when it is no longer
628 * needed, e.g. with ofpbuf_delete(). */
630 ofpraw_alloc_stats_reply(const struct ofp_header *request,
631 size_t extra_tailroom)
633 enum ofpraw request_raw;
634 enum ofpraw reply_raw;
637 error = ofpraw_decode_partial(&request_raw, request,
638 ntohs(request->length));
641 reply_raw = ofpraw_stats_request_to_reply(request_raw, request->version);
642 ovs_assert(reply_raw);
644 return ofpraw_alloc_reply(reply_raw, request, extra_tailroom);
647 /* Appends to 'buf' an OpenFlow header for 'raw' with OpenFlow version
648 * 'version' and a fresh OpenFlow transaction ID. Preallocates enough tailroom
649 * in 'buf' for the minimum body length of 'raw', plus 'extra_tailroom'
652 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
653 * must specify a valid (raw, version) pair.
655 * Upon return, 'buf->header' points to the beginning of the OpenFlow header
656 * and 'buf->msg' points just after it, to where the message's body will start.
657 * The caller must actually allocating the body into the space reserved for it,
658 * e.g. with ofpbuf_put_uninit(). */
660 ofpraw_put(enum ofpraw raw, uint8_t version, struct ofpbuf *buf)
662 ofpraw_put__(raw, version, alloc_xid(), 0, buf);
665 /* Same as ofpraw_put() but the caller provides the transaction ID. */
667 ofpraw_put_xid(enum ofpraw raw, uint8_t version, ovs_be32 xid,
670 ofpraw_put__(raw, version, xid, 0, buf);
673 /* Same as ofpraw_put(), but obtains the OpenFlow version and transaction ID
674 * from 'request->version' and 'request->xid', respectively.
676 * Even though the version comes from 'request->version', the caller must still
677 * know what it is doing, by specifying a valid pairing of 'raw' and
678 * 'request->version', just like ofpraw_put(). */
680 ofpraw_put_reply(enum ofpraw raw, const struct ofp_header *request,
683 ofpraw_put__(raw, request->version, request->xid, 0, buf);
686 /* Appends to 'buf' an OpenFlow header that is a stats reply to the stats
687 * request in 'request', using the same OpenFlow version and transaction ID as
688 * 'request'. Preallocate enough tailroom in 'buf for the stats reply's
689 * minimum body length, plus 'extra_tailroom' additional bytes.
691 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
692 * value. Every stats request has a corresponding reply, so the (raw, version)
693 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
695 * In the returned ofpbuf, 'header' points to the beginning of the
696 * OpenFlow header and 'msg' points just after it, to where the
697 * message's body will start. The caller must actually allocate the
698 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
700 * The caller owns the returned ofpbuf and must free it when it is no longer
701 * needed, e.g. with ofpbuf_delete(). */
703 ofpraw_put_stats_reply(const struct ofp_header *request, struct ofpbuf *buf)
708 error = ofpraw_decode_partial(&raw, request, ntohs(request->length));
711 raw = ofpraw_stats_request_to_reply(raw, request->version);
714 ofpraw_put__(raw, request->version, request->xid, 0, buf);
718 ofpraw_put__(enum ofpraw raw, uint8_t version, ovs_be32 xid,
719 size_t extra_tailroom, struct ofpbuf *buf)
721 const struct raw_info *info = raw_info_get(raw);
722 const struct raw_instance *instance = raw_instance_get(info, version);
723 const struct ofphdrs *hdrs = &instance->hdrs;
724 struct ofp_header *oh;
726 ofpbuf_prealloc_tailroom(buf, (instance->hdrs_len + info->min_body
728 buf->header = ofpbuf_put_uninit(buf, instance->hdrs_len);
729 buf->msg = ofpbuf_tail(buf);
732 oh->version = version;
733 oh->type = hdrs->type;
734 oh->length = htons(buf->size);
737 if (hdrs->type == OFPT_VENDOR) {
738 struct ofp_vendor_header *ovh = buf->header;
740 ovh->vendor = htonl(hdrs->vendor);
741 ovh->subtype = htonl(hdrs->subtype);
742 } else if (version == OFP10_VERSION
743 && (hdrs->type == OFPT10_STATS_REQUEST ||
744 hdrs->type == OFPT10_STATS_REPLY)) {
745 struct ofp10_stats_msg *osm = buf->header;
747 osm->type = htons(hdrs->stat);
748 osm->flags = htons(0);
750 if (hdrs->stat == OFPST_VENDOR) {
751 struct ofp10_vendor_stats_msg *ovsm = buf->header;
753 ovsm->vendor = htonl(hdrs->vendor);
754 if (hdrs->vendor == NX_VENDOR_ID) {
755 struct nicira10_stats_msg *nsm = buf->header;
757 nsm->subtype = htonl(hdrs->subtype);
758 memset(nsm->pad, 0, sizeof nsm->pad);
763 } else if (version != OFP10_VERSION
764 && (hdrs->type == OFPT11_STATS_REQUEST ||
765 hdrs->type == OFPT11_STATS_REPLY)) {
766 struct ofp11_stats_msg *osm = buf->header;
768 osm->type = htons(hdrs->stat);
769 osm->flags = htons(0);
770 memset(osm->pad, 0, sizeof osm->pad);
772 if (hdrs->stat == OFPST_VENDOR) {
773 struct ofp11_vendor_stats_msg *ovsm = buf->header;
775 ovsm->vendor = htonl(hdrs->vendor);
776 ovsm->subtype = htonl(hdrs->subtype);
781 /* Returns 'raw''s name.
783 * The name is the name used for 'raw' in the OpenFlow specification. For
784 * example, ofpraw_get_name(OFPRAW_OFPT10_FEATURES_REPLY) is
785 * "OFPT_FEATURES_REPLY".
787 * The caller must not modify or free the returned string. */
789 ofpraw_get_name(enum ofpraw raw)
791 return raw_info_get(raw)->name;
794 /* Returns the stats reply that corresponds to 'raw' in the given OpenFlow
797 ofpraw_stats_request_to_reply(enum ofpraw raw, uint8_t version)
799 const struct raw_info *info = raw_info_get(raw);
800 const struct raw_instance *instance = raw_instance_get(info, version);
801 enum ofpraw reply_raw;
805 hdrs = instance->hdrs;
806 switch ((enum ofp_version)hdrs.version) {
808 ovs_assert(hdrs.type == OFPT10_STATS_REQUEST);
809 hdrs.type = OFPT10_STATS_REPLY;
817 ovs_assert(hdrs.type == OFPT11_STATS_REQUEST);
818 hdrs.type = OFPT11_STATS_REPLY;
824 error = ofpraw_from_ofphdrs(&reply_raw, &hdrs);
830 /* Determines the OFPTYPE_* type of the OpenFlow message at 'oh', which has
831 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
832 * data are readable at 'oh'.) On success, returns 0 and stores the type into
833 * '*typep'. On failure, returns an OFPERR_* error code and zeros '*typep'.
835 * This function checks that 'oh' is a valid length for its particular type of
836 * message, and returns an error if not. */
838 ofptype_decode(enum ofptype *typep, const struct ofp_header *oh)
843 error = ofpraw_decode(&raw, oh);
844 *typep = error ? 0 : ofptype_from_ofpraw(raw);
848 /* Determines the OFPTYPE_* type of the OpenFlow message in 'msg', which starts
849 * at 'msg->data' and has length 'msg->size' bytes. On success,
850 * returns 0 and stores the type into '*typep'. On failure, returns an
851 * OFPERR_* error code and zeros '*typep'.
853 * This function checks that the message has a valid length for its particular
854 * type of message, and returns an error if not.
856 * In addition to setting '*typep', this function pulls off the OpenFlow header
857 * (including the stats headers, vendor header, and any subtype header) with
858 * ofpbuf_pull(). It also sets 'msg->header' to the start of the OpenFlow
859 * header and 'msg->msg' just beyond the headers (that is, to the final value
862 ofptype_pull(enum ofptype *typep, struct ofpbuf *buf)
867 error = ofpraw_pull(&raw, buf);
868 *typep = error ? 0 : ofptype_from_ofpraw(raw);
872 /* Returns the OFPTYPE_* type that corresponds to 'raw'.
874 * (This is a one-way trip, because the mapping from ofpraw to ofptype is
877 ofptype_from_ofpraw(enum ofpraw raw)
879 return raw_info_get(raw)->type;
883 ofptype_get_name(enum ofptype type)
885 ovs_assert(type < ARRAY_SIZE(type_names));
886 return type_names[type];
889 /* Updates the 'length' field of the OpenFlow message in 'buf' to
892 ofpmsg_update_length(struct ofpbuf *buf)
894 struct ofp_header *oh = ofpbuf_at_assert(buf, 0, sizeof *oh);
895 oh->length = htons(buf->size);
898 /* Returns just past the OpenFlow header (including the stats headers, vendor
899 * header, and any subtype header) in 'oh'. */
901 ofpmsg_body(const struct ofp_header *oh)
905 ofphdrs_decode_assert(&hdrs, oh, ntohs(oh->length));
906 return (const uint8_t *) oh + ofphdrs_len(&hdrs);
909 /* Return if it's a stat/multipart (OFPST) request message. */
911 ofpmsg_is_stat_request(const struct ofp_header *oh)
913 return ofp_is_stat_request(oh->version, oh->type);
916 static ovs_be16 *ofpmp_flags__(const struct ofp_header *);
918 /* Initializes 'replies' as a new list of stats messages that reply to
919 * 'request', which must be a stats request message. Initially the list will
920 * consist of only a single reply part without any body. The caller should
921 * use calls to the other ofpmp_*() functions to add to the body and split the
922 * message into multiple parts, if necessary. */
924 ofpmp_init(struct ovs_list *replies, const struct ofp_header *request)
928 ovs_list_init(replies);
930 msg = ofpraw_alloc_stats_reply(request, 1000);
931 ovs_list_push_back(replies, &msg->list_node);
934 /* Prepares to append up to 'len' bytes to the series of statistics replies in
935 * 'replies', which should have been initialized with ofpmp_init(), if
936 * necessary adding a new reply to the list.
938 * Returns an ofpbuf with at least 'len' bytes of tailroom. The 'len' bytes
939 * have not actually been allocated, so the caller must do so with
940 * e.g. ofpbuf_put_uninit(). */
942 ofpmp_reserve(struct ovs_list *replies, size_t len)
944 struct ofpbuf *msg = ofpbuf_from_list(ovs_list_back(replies));
946 if (msg->size + len <= UINT16_MAX) {
947 ofpbuf_prealloc_tailroom(msg, len);
950 unsigned int hdrs_len;
954 ofphdrs_decode_assert(&hdrs, msg->data, msg->size);
955 hdrs_len = ofphdrs_len(&hdrs);
957 next = ofpbuf_new(MAX(1024, hdrs_len + len));
958 ofpbuf_put(next, msg->data, hdrs_len);
959 next->header = next->data;
960 next->msg = ofpbuf_tail(next);
961 ovs_list_push_back(replies, &next->list_node);
963 *ofpmp_flags__(msg->data) |= htons(OFPSF_REPLY_MORE);
969 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
970 * returns the first byte. */
972 ofpmp_append(struct ovs_list *replies, size_t len)
974 return ofpbuf_put_uninit(ofpmp_reserve(replies, len), len);
977 /* Sometimes, when composing stats replies, it's difficult to predict how long
978 * an individual reply chunk will be before actually encoding it into the reply
979 * buffer. This function allows easy handling of this case: just encode the
980 * reply, then use this function to break the message into two pieces if it
981 * exceeds the OpenFlow message limit.
983 * In detail, if the final stats message in 'replies' is too long for OpenFlow,
984 * this function breaks it into two separate stats replies, the first one with
985 * the first 'start_ofs' bytes, the second one containing the bytes from that
988 ofpmp_postappend(struct ovs_list *replies, size_t start_ofs)
990 struct ofpbuf *msg = ofpbuf_from_list(ovs_list_back(replies));
992 ovs_assert(start_ofs <= UINT16_MAX);
993 if (msg->size > UINT16_MAX) {
994 size_t len = msg->size - start_ofs;
995 memcpy(ofpmp_append(replies, len),
996 (const uint8_t *) msg->data + start_ofs, len);
997 msg->size = start_ofs;
1001 /* Returns the OpenFlow version of the replies being constructed in 'replies',
1002 * which should have been initialized by ofpmp_init(). */
1004 ofpmp_version(struct ovs_list *replies)
1006 struct ofpbuf *msg = ofpbuf_from_list(ovs_list_back(replies));
1007 const struct ofp_header *oh = msg->data;
1012 /* Determines the OFPRAW_* type of the OpenFlow messages in 'replies', which
1013 * should have been initialized by ofpmp_init(). */
1015 ofpmp_decode_raw(struct ovs_list *replies)
1017 struct ofpbuf *msg = ofpbuf_from_list(ovs_list_back(replies));
1021 error = ofpraw_decode_partial(&raw, msg->data, msg->size);
1027 ofpmp_flags__(const struct ofp_header *oh)
1029 switch ((enum ofp_version)oh->version) {
1031 return &((struct ofp10_stats_msg *) oh)->flags;
1038 return &((struct ofp11_stats_msg *) oh)->flags;
1044 /* Returns the OFPSF_* flags found in the OpenFlow stats header of 'oh', which
1045 * must be an OpenFlow stats request or reply.
1047 * (OFPSF_REPLY_MORE is the only defined flag.) */
1049 ofpmp_flags(const struct ofp_header *oh)
1051 return ntohs(*ofpmp_flags__(oh));
1054 /* Returns true if the OFPSF_REPLY_MORE flag is set in the OpenFlow stats
1055 * header of 'oh', which must be an OpenFlow stats request or reply, false if
1058 ofpmp_more(const struct ofp_header *oh)
1060 return (ofpmp_flags(oh) & OFPSF_REPLY_MORE) != 0;
1063 static void ofpmsgs_init(void);
1065 static const struct raw_info *
1066 raw_info_get(enum ofpraw raw)
1070 ovs_assert(raw < ARRAY_SIZE(raw_infos));
1071 return &raw_infos[raw];
1074 static struct raw_instance *
1075 raw_instance_get(const struct raw_info *info, uint8_t version)
1077 ovs_assert(version >= info->min_version && version <= info->max_version);
1078 return &info->instances[version - info->min_version];
1082 ofpraw_from_ofphdrs(enum ofpraw *raw, const struct ofphdrs *hdrs)
1084 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1086 struct raw_instance *raw_hdrs;
1091 hash = ofphdrs_hash(hdrs);
1092 HMAP_FOR_EACH_WITH_HASH (raw_hdrs, hmap_node, hash, &raw_instance_map) {
1093 if (ofphdrs_equal(hdrs, &raw_hdrs->hdrs)) {
1094 *raw = raw_hdrs->raw;
1099 if (!VLOG_DROP_WARN(&rl)) {
1103 ds_put_format(&s, "version %"PRIu8", type %"PRIu8,
1104 hdrs->version, hdrs->type);
1105 if (ofphdrs_is_stat(hdrs)) {
1106 ds_put_format(&s, ", stat %"PRIu16, hdrs->stat);
1109 ds_put_format(&s, ", vendor 0x%"PRIx32", subtype %"PRIu32,
1110 hdrs->vendor, hdrs->subtype);
1112 VLOG_WARN("unknown OpenFlow message (%s)", ds_cstr(&s));
1116 return (hdrs->vendor ? OFPERR_OFPBRC_BAD_SUBTYPE
1117 : ofphdrs_is_stat(hdrs) ? OFPERR_OFPBRC_BAD_STAT
1118 : OFPERR_OFPBRC_BAD_TYPE);
1124 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1125 const struct raw_info *info;
1127 if (!ovsthread_once_start(&once)) {
1131 hmap_init(&raw_instance_map);
1132 for (info = raw_infos; info < &raw_infos[ARRAY_SIZE(raw_infos)]; info++)
1134 int n_instances = info->max_version - info->min_version + 1;
1135 struct raw_instance *inst;
1137 for (inst = info->instances;
1138 inst < &info->instances[n_instances];
1140 inst->hdrs_len = ofphdrs_len(&inst->hdrs);
1141 hmap_insert(&raw_instance_map, &inst->hmap_node,
1142 ofphdrs_hash(&inst->hdrs));
1146 ovsthread_once_done(&once);