2 * Copyright (c) 2012, 2013 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 "dynamic-string.h"
24 #include "openflow/nicira-ext.h"
25 #include "openflow/openflow.h"
26 #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 /* A thin abstraction of OpenFlow headers:
40 * - 'version' and 'type' come straight from struct ofp_header, so these are
41 * always present and meaningful.
43 * - 'stat' comes from the 'type' member in statistics messages only. It is
44 * meaningful, therefore, only if 'version' and 'type' taken together
45 * specify a statistics request or reply. Otherwise it is 0.
47 * - 'vendor' is meaningful only for vendor messages, that is, if 'version'
48 * and 'type' specify a vendor message or if 'version' and 'type' specify
49 * a statistics message and 'stat' specifies a vendor statistic type.
52 * - 'subtype' is meaningful only for vendor messages and otherwise 0. It
53 * specifies a vendor-defined subtype. There is no standard format for
54 * these but 32 bits seems like it should be enough. */
56 uint8_t version; /* From ofp_header. */
57 uint8_t type; /* From ofp_header. */
58 uint16_t stat; /* From ofp10_stats_msg or ofp11_stats_msg. */
59 uint32_t vendor; /* From ofp_vendor_header,
60 * ofp10_vendor_stats_msg, or
61 * ofp11_vendor_stats_msg. */
62 uint32_t subtype; /* From nicira_header, nicira10_stats_msg, or
63 * nicira11_stats_msg. */
65 BUILD_ASSERT_DECL(sizeof(struct ofphdrs) == 12);
67 /* A mapping from OpenFlow headers to OFPRAW_*. */
69 struct hmap_node hmap_node; /* In 'raw_instance_map'. */
70 struct ofphdrs hdrs; /* Key. */
71 enum ofpraw raw; /* Value. */
72 unsigned int hdrs_len; /* ofphdrs_len(hdrs). */
75 /* Information about a particular 'enum ofpraw'. */
77 /* All possible instantiations of this OFPRAW_* into OpenFlow headers. */
78 struct raw_instance *instances; /* min_version - max_version + 1 elems. */
82 unsigned int min_body;
83 unsigned int extra_multiple;
88 /* All understood OpenFlow message types, indexed by their 'struct ofphdrs'. */
89 static struct hmap raw_instance_map;
90 #include "ofp-msgs.inc"
92 static ovs_be32 alloc_xid(void);
94 /* ofphdrs functions. */
95 static uint32_t ofphdrs_hash(const struct ofphdrs *);
96 static bool ofphdrs_equal(const struct ofphdrs *a, const struct ofphdrs *b);
97 static enum ofperr ofphdrs_decode(struct ofphdrs *,
98 const struct ofp_header *oh, size_t length);
99 static void ofphdrs_decode_assert(struct ofphdrs *,
100 const struct ofp_header *oh, size_t length);
101 size_t ofphdrs_len(const struct ofphdrs *);
103 static const struct raw_info *raw_info_get(enum ofpraw);
104 static struct raw_instance *raw_instance_get(const struct raw_info *,
107 static enum ofperr ofpraw_from_ofphdrs(enum ofpraw *, const struct ofphdrs *);
109 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
113 static atomic_uint32_t next_xid = ATOMIC_VAR_INIT(1);
116 atomic_add(&next_xid, 1, &xid);
121 ofphdrs_hash(const struct ofphdrs *hdrs)
123 BUILD_ASSERT_DECL(sizeof *hdrs == 12);
124 return hash_words((const uint32_t *) hdrs, 3, 0);
128 ofphdrs_equal(const struct ofphdrs *a, const struct ofphdrs *b)
130 return !memcmp(a, b, sizeof *a);
134 log_bad_vendor(uint32_t vendor)
136 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
138 VLOG_WARN_RL(&rl, "OpenFlow message has unknown vendor %#"PRIx32, vendor);
142 ofphdrs_decode(struct ofphdrs *hdrs,
143 const struct ofp_header *oh, size_t length)
145 memset(hdrs, 0, sizeof *hdrs);
146 if (length < sizeof *oh) {
147 return OFPERR_OFPBRC_BAD_LEN;
150 /* Get base message version and type (OFPT_*). */
151 hdrs->version = oh->version;
152 hdrs->type = oh->type;
154 if (hdrs->type == OFPT_VENDOR) {
156 const struct ofp_vendor_header *ovh;
158 if (length < sizeof *ovh) {
159 return OFPERR_OFPBRC_BAD_LEN;
162 ovh = (const struct ofp_vendor_header *) oh;
163 hdrs->vendor = ntohl(ovh->vendor);
164 if (hdrs->vendor == NX_VENDOR_ID) {
165 /* Get Nicira message subtype (NXT_*). */
166 const struct nicira_header *nh;
168 if (length < sizeof *nh) {
169 return OFPERR_OFPBRC_BAD_LEN;
171 nh = (const struct nicira_header *) oh;
172 hdrs->subtype = ntohl(nh->subtype);
174 log_bad_vendor(hdrs->vendor);
175 return OFPERR_OFPBRC_BAD_VENDOR;
177 } else if (hdrs->version == OFP10_VERSION
178 && (hdrs->type == OFPT10_STATS_REQUEST ||
179 hdrs->type == OFPT10_STATS_REPLY)) {
180 const struct ofp10_stats_msg *osm;
182 /* Get statistic type (OFPST_*). */
183 if (length < sizeof *osm) {
184 return OFPERR_OFPBRC_BAD_LEN;
186 osm = (const struct ofp10_stats_msg *) oh;
187 hdrs->stat = ntohs(osm->type);
189 if (hdrs->stat == OFPST_VENDOR) {
191 const struct ofp10_vendor_stats_msg *ovsm;
193 if (length < sizeof *ovsm) {
194 return OFPERR_OFPBRC_BAD_LEN;
197 ovsm = (const struct ofp10_vendor_stats_msg *) oh;
198 hdrs->vendor = ntohl(ovsm->vendor);
199 if (hdrs->vendor == NX_VENDOR_ID) {
200 /* Get Nicira statistic type (NXST_*). */
201 const struct nicira10_stats_msg *nsm;
203 if (length < sizeof *nsm) {
204 return OFPERR_OFPBRC_BAD_LEN;
206 nsm = (const struct nicira10_stats_msg *) oh;
207 hdrs->subtype = ntohl(nsm->subtype);
209 log_bad_vendor(hdrs->vendor);
210 return OFPERR_OFPBRC_BAD_VENDOR;
213 } else if (hdrs->version != OFP10_VERSION
214 && (hdrs->type == OFPT11_STATS_REQUEST ||
215 hdrs->type == OFPT11_STATS_REPLY)) {
216 const struct ofp11_stats_msg *osm;
218 /* Get statistic type (OFPST_*). */
219 if (length < sizeof *osm) {
220 return OFPERR_OFPBRC_BAD_LEN;
222 osm = (const struct ofp11_stats_msg *) oh;
223 hdrs->stat = ntohs(osm->type);
225 if (hdrs->stat == OFPST_VENDOR) {
227 const struct ofp11_vendor_stats_msg *ovsm;
229 if (length < sizeof *ovsm) {
230 return OFPERR_OFPBRC_BAD_LEN;
233 ovsm = (const struct ofp11_vendor_stats_msg *) oh;
234 hdrs->vendor = ntohl(ovsm->vendor);
235 if (hdrs->vendor == NX_VENDOR_ID) {
236 /* Get Nicira statistic type (NXST_*). */
237 const struct nicira11_stats_msg *nsm;
239 if (length < sizeof *nsm) {
240 return OFPERR_OFPBRC_BAD_LEN;
242 nsm = (const struct nicira11_stats_msg *) oh;
243 hdrs->subtype = ntohl(nsm->subtype);
245 log_bad_vendor(hdrs->vendor);
246 return OFPERR_OFPBRC_BAD_VENDOR;
255 ofphdrs_decode_assert(struct ofphdrs *hdrs,
256 const struct ofp_header *oh, size_t length)
258 enum ofperr error = ofphdrs_decode(hdrs, oh, length);
263 ofp_is_stat_request(enum ofp_version version, uint8_t type)
267 return type == OFPT10_STATS_REQUEST;
272 return type == OFPT11_STATS_REQUEST;
279 ofp_is_stat_reply(enum ofp_version version, uint8_t type)
283 return type == OFPT10_STATS_REPLY;
288 return type == OFPT11_STATS_REPLY;
295 ofp_is_stat(enum ofp_version version, uint8_t type)
297 return (ofp_is_stat_request(version, type) ||
298 ofp_is_stat_reply(version, type));
302 ofphdrs_is_stat(const struct ofphdrs *hdrs)
304 return ofp_is_stat(hdrs->version, hdrs->type);
308 ofphdrs_len(const struct ofphdrs *hdrs)
310 if (hdrs->type == OFPT_VENDOR) {
311 return sizeof(struct nicira_header);
314 switch ((enum ofp_version) hdrs->version) {
316 if (hdrs->type == OFPT10_STATS_REQUEST ||
317 hdrs->type == OFPT10_STATS_REPLY) {
318 return (hdrs->stat == OFPST_VENDOR
319 ? sizeof(struct nicira10_stats_msg)
320 : sizeof(struct ofp10_stats_msg));
328 if (hdrs->type == OFPT11_STATS_REQUEST ||
329 hdrs->type == OFPT11_STATS_REPLY) {
330 return (hdrs->stat == OFPST_VENDOR
331 ? sizeof(struct nicira11_stats_msg)
332 : sizeof(struct ofp11_stats_msg));
337 return sizeof(struct ofp_header);
340 /* Determines the OFPRAW_* type of the OpenFlow message at 'oh', which has
341 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
342 * data are readable at 'oh'.) On success, returns 0 and stores the type into
343 * '*raw'. On failure, returns an OFPERR_* error code and zeros '*raw'.
345 * This function checks that 'oh' is a valid length for its particular type of
346 * message, and returns an error if not. */
348 ofpraw_decode(enum ofpraw *raw, const struct ofp_header *oh)
352 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
353 return ofpraw_pull(raw, &msg);
356 /* Does the same job as ofpraw_decode(), except that it assert-fails if
357 * ofpraw_decode() would have reported an error. Thus, it's able to use the
358 * return value for the OFPRAW_* message type instead of an error code.
360 * (It only makes sense to use this function if you previously called
361 * ofpraw_decode() on the message and thus know that it's OK.) */
363 ofpraw_decode_assert(const struct ofp_header *oh)
368 error = ofpraw_decode(&raw, oh);
373 /* Determines the OFPRAW_* type of the OpenFlow message in 'msg', which starts
374 * at 'ofpbuf_data(msg)' and has length 'ofpbuf_size(msg)' bytes. On success,
375 * returns 0 and stores the type into '*rawp'. On failure, returns an OFPERR_*
376 * error code and zeros '*rawp'.
378 * This function checks that the message has a valid length for its particular
379 * type of message, and returns an error if not.
381 * In addition to setting '*rawp', this function pulls off the OpenFlow header
382 * (including the stats headers, vendor header, and any subtype header) with
383 * ofpbuf_pull(). It also sets 'msg->frame' to the start of the OpenFlow
384 * header and 'msg->l3' just beyond the headers (that is, to the final value of
385 * ofpbuf_data(msg)). */
387 ofpraw_pull(enum ofpraw *rawp, struct ofpbuf *msg)
389 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
391 const struct raw_instance *instance;
392 const struct raw_info *info;
395 unsigned int min_len;
401 /* Set default outputs. */
402 msg->frame = ofpbuf_data(msg);
403 ofpbuf_set_l3(msg, msg->frame);
406 len = ofpbuf_size(msg);
407 error = ofphdrs_decode(&hdrs, ofpbuf_data(msg), len);
412 error = ofpraw_from_ofphdrs(&raw, &hdrs);
417 info = raw_info_get(raw);
418 instance = raw_instance_get(info, hdrs.version);
419 msg->frame = ofpbuf_pull(msg, instance->hdrs_len);
420 ofpbuf_set_l3(msg, ofpbuf_data(msg));
422 min_len = instance->hdrs_len + info->min_body;
423 switch (info->extra_multiple) {
425 if (len != min_len) {
426 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (expected "
427 "length %u)", info->name, len, min_len);
428 return OFPERR_OFPBRC_BAD_LEN;
434 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (expected "
435 "length at least %u bytes)",
436 info->name, len, min_len);
437 return OFPERR_OFPBRC_BAD_LEN;
442 if (len < min_len || (len - min_len) % info->extra_multiple) {
443 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (must be "
444 "exactly %u bytes or longer by an integer multiple "
446 info->name, len, min_len, info->extra_multiple);
447 return OFPERR_OFPBRC_BAD_LEN;
456 /* Does the same job as ofpraw_pull(), except that it assert-fails if
457 * ofpraw_pull() would have reported an error. Thus, it's able to use the
458 * return value for the OFPRAW_* message type instead of an error code.
460 * (It only makes sense to use this function if you previously called
461 * ofpraw_decode() on the message and thus know that it's OK.) */
463 ofpraw_pull_assert(struct ofpbuf *msg)
468 error = ofpraw_pull(&raw, msg);
473 /* Determines the OFPRAW_* type of the OpenFlow message that starts at 'oh' and
474 * has length 'length' bytes. On success, returns 0 and stores the type into
475 * '*rawp'. On failure, returns an OFPERR_* error code and zeros '*rawp'.
477 * Unlike other functions for decoding message types, this one is not picky
478 * about message length. For example, it will successfully decode a message
479 * whose body is shorter than the minimum length for a message of its type.
480 * Thus, this is the correct function to use for decoding the type of a message
481 * that might have been truncated, such as the payload of an OpenFlow error
482 * message (which is allowed to be truncated to 64 bytes). */
484 ofpraw_decode_partial(enum ofpraw *raw,
485 const struct ofp_header *oh, size_t length)
490 error = ofphdrs_decode(&hdrs, oh, length);
492 error = ofpraw_from_ofphdrs(raw, &hdrs);
501 /* Encoding messages using OFPRAW_* values. */
503 static void ofpraw_put__(enum ofpraw, uint8_t version, ovs_be32 xid,
504 size_t extra_tailroom, struct ofpbuf *);
506 /* Allocates and returns a new ofpbuf that contains an OpenFlow header for
507 * 'raw' with OpenFlow version 'version' and a fresh OpenFlow transaction ID.
508 * The ofpbuf has enough tailroom for the minimum body length of 'raw', plus
509 * 'extra_tailroom' additional bytes.
511 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
512 * must specify a valid (raw, version) pair.
514 * In the returned ofpbuf, 'frame' points to the beginning of the
515 * OpenFlow header and 'l3' points just after it, to where the
516 * message's body will start. The caller must actually allocate the
517 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
519 * The caller owns the returned ofpbuf and must free it when it is no longer
520 * needed, e.g. with ofpbuf_delete(). */
522 ofpraw_alloc(enum ofpraw raw, uint8_t version, size_t extra_tailroom)
524 return ofpraw_alloc_xid(raw, version, alloc_xid(), extra_tailroom);
527 /* Same as ofpraw_alloc() but the caller provides the transaction ID. */
529 ofpraw_alloc_xid(enum ofpraw raw, uint8_t version, ovs_be32 xid,
530 size_t extra_tailroom)
532 struct ofpbuf *buf = ofpbuf_new(0);
533 ofpraw_put__(raw, version, xid, extra_tailroom, buf);
537 /* Same as ofpraw_alloc(), but obtains the OpenFlow version and transaction ID
538 * from 'request->version' and 'request->xid', respectively.
540 * Even though the version comes from 'request->version', the caller must still
541 * know what it is doing, by specifying a valid pairing of 'raw' and
542 * 'request->version', just like ofpraw_alloc(). */
544 ofpraw_alloc_reply(enum ofpraw raw, const struct ofp_header *request,
545 size_t extra_tailroom)
547 return ofpraw_alloc_xid(raw, request->version, request->xid,
551 /* Allocates and returns a new ofpbuf that contains an OpenFlow header that is
552 * a stats reply to the stats request in 'request', using the same OpenFlow
553 * version and transaction ID as 'request'. The ofpbuf has enough tailroom for
554 * the stats reply's minimum body length, plus 'extra_tailroom' additional
557 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
558 * value. Every stats request has a corresponding reply, so the (raw, version)
559 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
561 * In the returned ofpbuf, 'frame' points to the beginning of the
562 * OpenFlow header and 'l3' points just after it, to where the
563 * message's body will start. The caller must actually allocate the
564 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
566 * The caller owns the returned ofpbuf and must free it when it is no longer
567 * needed, e.g. with ofpbuf_delete(). */
569 ofpraw_alloc_stats_reply(const struct ofp_header *request,
570 size_t extra_tailroom)
572 enum ofpraw request_raw;
573 enum ofpraw reply_raw;
576 error = ofpraw_decode_partial(&request_raw, request,
577 ntohs(request->length));
580 reply_raw = ofpraw_stats_request_to_reply(request_raw, request->version);
581 ovs_assert(reply_raw);
583 return ofpraw_alloc_reply(reply_raw, request, extra_tailroom);
586 /* Appends to 'buf' an OpenFlow header for 'raw' with OpenFlow version
587 * 'version' and a fresh OpenFlow transaction ID. Preallocates enough tailroom
588 * in 'buf' for the minimum body length of 'raw', plus 'extra_tailroom'
591 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
592 * must specify a valid (raw, version) pair.
594 * Upon return, 'buf->frame' points to the beginning of the OpenFlow header and
595 * 'buf->l3' points just after it, to where the message's body will start. The
596 * caller must actually allocating the body into the space reserved for it,
597 * e.g. with ofpbuf_put_uninit(). */
599 ofpraw_put(enum ofpraw raw, uint8_t version, struct ofpbuf *buf)
601 ofpraw_put__(raw, version, alloc_xid(), 0, buf);
604 /* Same as ofpraw_put() but the caller provides the transaction ID. */
606 ofpraw_put_xid(enum ofpraw raw, uint8_t version, ovs_be32 xid,
609 ofpraw_put__(raw, version, xid, 0, buf);
612 /* Same as ofpraw_put(), but obtains the OpenFlow version and transaction ID
613 * from 'request->version' and 'request->xid', respectively.
615 * Even though the version comes from 'request->version', the caller must still
616 * know what it is doing, by specifying a valid pairing of 'raw' and
617 * 'request->version', just like ofpraw_put(). */
619 ofpraw_put_reply(enum ofpraw raw, const struct ofp_header *request,
622 ofpraw_put__(raw, request->version, request->xid, 0, buf);
625 /* Appends to 'buf' an OpenFlow header that is a stats reply to the stats
626 * request in 'request', using the same OpenFlow version and transaction ID as
627 * 'request'. Preallocate enough tailroom in 'buf for the stats reply's
628 * minimum body length, plus 'extra_tailroom' additional bytes.
630 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
631 * value. Every stats request has a corresponding reply, so the (raw, version)
632 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
634 * In the returned ofpbuf, 'frame' points to the beginning of the
635 * OpenFlow header and 'l3' points just after it, to where the
636 * message's body will start. The caller must actually allocate the
637 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
639 * The caller owns the returned ofpbuf and must free it when it is no longer
640 * needed, e.g. with ofpbuf_delete(). */
642 ofpraw_put_stats_reply(const struct ofp_header *request, struct ofpbuf *buf)
647 error = ofpraw_decode_partial(&raw, request, ntohs(request->length));
650 raw = ofpraw_stats_request_to_reply(raw, request->version);
653 ofpraw_put__(raw, request->version, request->xid, 0, buf);
657 ofpraw_put__(enum ofpraw raw, uint8_t version, ovs_be32 xid,
658 size_t extra_tailroom, struct ofpbuf *buf)
660 const struct raw_info *info = raw_info_get(raw);
661 const struct raw_instance *instance = raw_instance_get(info, version);
662 const struct ofphdrs *hdrs = &instance->hdrs;
663 struct ofp_header *oh;
665 ofpbuf_prealloc_tailroom(buf, (instance->hdrs_len + info->min_body
667 buf->frame = ofpbuf_put_uninit(buf, instance->hdrs_len);
668 ofpbuf_set_l3(buf, ofpbuf_tail(buf));
671 oh->version = version;
672 oh->type = hdrs->type;
673 oh->length = htons(ofpbuf_size(buf));
676 if (hdrs->type == OFPT_VENDOR) {
677 struct nicira_header *nh = buf->frame;
679 ovs_assert(hdrs->vendor == NX_VENDOR_ID);
680 nh->vendor = htonl(hdrs->vendor);
681 nh->subtype = htonl(hdrs->subtype);
682 } else if (version == OFP10_VERSION
683 && (hdrs->type == OFPT10_STATS_REQUEST ||
684 hdrs->type == OFPT10_STATS_REPLY)) {
685 struct ofp10_stats_msg *osm = buf->frame;
687 osm->type = htons(hdrs->stat);
688 osm->flags = htons(0);
690 if (hdrs->stat == OFPST_VENDOR) {
691 struct ofp10_vendor_stats_msg *ovsm = buf->frame;
693 ovsm->vendor = htonl(hdrs->vendor);
694 if (hdrs->vendor == NX_VENDOR_ID) {
695 struct nicira10_stats_msg *nsm = buf->frame;
697 nsm->subtype = htonl(hdrs->subtype);
698 memset(nsm->pad, 0, sizeof nsm->pad);
703 } else if (version != OFP10_VERSION
704 && (hdrs->type == OFPT11_STATS_REQUEST ||
705 hdrs->type == OFPT11_STATS_REPLY)) {
706 struct ofp11_stats_msg *osm = buf->frame;
708 osm->type = htons(hdrs->stat);
709 osm->flags = htons(0);
710 memset(osm->pad, 0, sizeof osm->pad);
712 if (hdrs->stat == OFPST_VENDOR) {
713 struct ofp11_vendor_stats_msg *ovsm = buf->frame;
715 ovsm->vendor = htonl(hdrs->vendor);
716 if (hdrs->vendor == NX_VENDOR_ID) {
717 struct nicira11_stats_msg *nsm = buf->frame;
719 nsm->subtype = htonl(hdrs->subtype);
727 /* Returns 'raw''s name.
729 * The name is the name used for 'raw' in the OpenFlow specification. For
730 * example, ofpraw_get_name(OFPRAW_OFPT10_FEATURES_REPLY) is
731 * "OFPT_FEATURES_REPLY".
733 * The caller must not modify or free the returned string. */
735 ofpraw_get_name(enum ofpraw raw)
737 return raw_info_get(raw)->name;
740 /* Returns the stats reply that corresponds to 'raw' in the given OpenFlow
743 ofpraw_stats_request_to_reply(enum ofpraw raw, uint8_t version)
745 const struct raw_info *info = raw_info_get(raw);
746 const struct raw_instance *instance = raw_instance_get(info, version);
747 enum ofpraw reply_raw;
751 hdrs = instance->hdrs;
752 switch ((enum ofp_version)hdrs.version) {
754 ovs_assert(hdrs.type == OFPT10_STATS_REQUEST);
755 hdrs.type = OFPT10_STATS_REPLY;
761 ovs_assert(hdrs.type == OFPT11_STATS_REQUEST);
762 hdrs.type = OFPT11_STATS_REPLY;
768 error = ofpraw_from_ofphdrs(&reply_raw, &hdrs);
774 /* Determines the OFPTYPE_* type of the OpenFlow message at 'oh', which has
775 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
776 * data are readable at 'oh'.) On success, returns 0 and stores the type into
777 * '*typep'. On failure, returns an OFPERR_* error code and zeros '*typep'.
779 * This function checks that 'oh' is a valid length for its particular type of
780 * message, and returns an error if not. */
782 ofptype_decode(enum ofptype *typep, const struct ofp_header *oh)
787 error = ofpraw_decode(&raw, oh);
788 *typep = error ? 0 : ofptype_from_ofpraw(raw);
792 /* Determines the OFPTYPE_* type of the OpenFlow message in 'msg', which starts
793 * at 'ofpbuf_data(msg)' and has length 'ofpbuf_size(msg)' bytes. On success,
794 * returns 0 and stores the type into '*typep'. On failure, returns an
795 * OFPERR_* error code and zeros '*typep'.
797 * This function checks that the message has a valid length for its particular
798 * type of message, and returns an error if not.
800 * In addition to setting '*typep', this function pulls off the OpenFlow header
801 * (including the stats headers, vendor header, and any subtype header) with
802 * ofpbuf_pull(). It also sets 'msg->frame' to the start of the OpenFlow
803 * header and 'msg->l3' just beyond the headers (that is, to the final value of
804 * ofpbuf_data(msg)). */
806 ofptype_pull(enum ofptype *typep, struct ofpbuf *buf)
811 error = ofpraw_pull(&raw, buf);
812 *typep = error ? 0 : ofptype_from_ofpraw(raw);
816 /* Returns the OFPTYPE_* type that corresponds to 'raw'.
818 * (This is a one-way trip, because the mapping from ofpraw to ofptype is
821 ofptype_from_ofpraw(enum ofpraw raw)
823 return raw_info_get(raw)->type;
826 /* Updates the 'length' field of the OpenFlow message in 'buf' to
827 * 'ofpbuf_size(buf)'. */
829 ofpmsg_update_length(struct ofpbuf *buf)
831 struct ofp_header *oh = ofpbuf_at_assert(buf, 0, sizeof *oh);
832 oh->length = htons(ofpbuf_size(buf));
835 /* Returns just past the Openflow header (including the stats headers, vendor
836 * header, and any subtype header) in 'oh'. */
838 ofpmsg_body(const struct ofp_header *oh)
842 ofphdrs_decode_assert(&hdrs, oh, ntohs(oh->length));
843 return (const uint8_t *) oh + ofphdrs_len(&hdrs);
846 /* Return if it's a stat/multipart (OFPST) request message. */
848 ofpmsg_is_stat_request(const struct ofp_header *oh)
850 return ofp_is_stat_request(oh->version, oh->type);
853 static ovs_be16 *ofpmp_flags__(const struct ofp_header *);
855 /* Initializes 'replies' as a new list of stats messages that reply to
856 * 'request', which must be a stats request message. Initially the list will
857 * consist of only a single reply part without any body. The caller should
858 * use calls to the other ofpmp_*() functions to add to the body and split the
859 * message into multiple parts, if necessary. */
861 ofpmp_init(struct list *replies, const struct ofp_header *request)
867 msg = ofpraw_alloc_stats_reply(request, 1000);
868 list_push_back(replies, &msg->list_node);
871 /* Prepares to append up to 'len' bytes to the series of statistics replies in
872 * 'replies', which should have been initialized with ofpmp_init(), if
873 * necessary adding a new reply to the list.
875 * Returns an ofpbuf with at least 'len' bytes of tailroom. The 'len' bytes
876 * have not actually been allocated, so the caller must do so with
877 * e.g. ofpbuf_put_uninit(). */
879 ofpmp_reserve(struct list *replies, size_t len)
881 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
883 if (ofpbuf_size(msg) + len <= UINT16_MAX) {
884 ofpbuf_prealloc_tailroom(msg, len);
887 unsigned int hdrs_len;
891 ofphdrs_decode_assert(&hdrs, ofpbuf_data(msg), ofpbuf_size(msg));
892 hdrs_len = ofphdrs_len(&hdrs);
894 next = ofpbuf_new(MAX(1024, hdrs_len + len));
895 ofpbuf_put(next, ofpbuf_data(msg), hdrs_len);
896 next->frame = ofpbuf_data(next);
897 ofpbuf_set_l3(next, ofpbuf_tail(next));
898 list_push_back(replies, &next->list_node);
900 *ofpmp_flags__(ofpbuf_data(msg)) |= htons(OFPSF_REPLY_MORE);
906 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
907 * returns the first byte. */
909 ofpmp_append(struct list *replies, size_t len)
911 return ofpbuf_put_uninit(ofpmp_reserve(replies, len), len);
914 /* Sometimes, when composing stats replies, it's difficult to predict how long
915 * an individual reply chunk will be before actually encoding it into the reply
916 * buffer. This function allows easy handling of this case: just encode the
917 * reply, then use this function to break the message into two pieces if it
918 * exceeds the OpenFlow message limit.
920 * In detail, if the final stats message in 'replies' is too long for OpenFlow,
921 * this function breaks it into two separate stats replies, the first one with
922 * the first 'start_ofs' bytes, the second one containing the bytes from that
925 ofpmp_postappend(struct list *replies, size_t start_ofs)
927 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
929 ovs_assert(start_ofs <= UINT16_MAX);
930 if (ofpbuf_size(msg) > UINT16_MAX) {
931 size_t len = ofpbuf_size(msg) - start_ofs;
932 memcpy(ofpmp_append(replies, len),
933 (const uint8_t *) ofpbuf_data(msg) + start_ofs, len);
934 ofpbuf_set_size(msg, start_ofs);
939 ofpmp_flags__(const struct ofp_header *oh)
941 switch ((enum ofp_version)oh->version) {
943 return &((struct ofp10_stats_msg *) oh)->flags;
948 return &((struct ofp11_stats_msg *) oh)->flags;
954 /* Returns the OFPSF_* flags found in the OpenFlow stats header of 'oh', which
955 * must be an OpenFlow stats request or reply.
957 * (OFPSF_REPLY_MORE is the only defined flag.) */
959 ofpmp_flags(const struct ofp_header *oh)
961 return ntohs(*ofpmp_flags__(oh));
964 /* Returns true if the OFPSF_REPLY_MORE flag is set in the OpenFlow stats
965 * header of 'oh', which must be an OpenFlow stats request or reply, false if
968 ofpmp_more(const struct ofp_header *oh)
970 return (ofpmp_flags(oh) & OFPSF_REPLY_MORE) != 0;
973 static void ofpmsgs_init(void);
975 static const struct raw_info *
976 raw_info_get(enum ofpraw raw)
980 ovs_assert(raw < ARRAY_SIZE(raw_infos));
981 return &raw_infos[raw];
984 static struct raw_instance *
985 raw_instance_get(const struct raw_info *info, uint8_t version)
987 ovs_assert(version >= info->min_version && version <= info->max_version);
988 return &info->instances[version - info->min_version];
992 ofpraw_from_ofphdrs(enum ofpraw *raw, const struct ofphdrs *hdrs)
994 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
996 struct raw_instance *raw_hdrs;
1001 hash = ofphdrs_hash(hdrs);
1002 HMAP_FOR_EACH_WITH_HASH (raw_hdrs, hmap_node, hash, &raw_instance_map) {
1003 if (ofphdrs_equal(hdrs, &raw_hdrs->hdrs)) {
1004 *raw = raw_hdrs->raw;
1009 if (!VLOG_DROP_WARN(&rl)) {
1013 ds_put_format(&s, "version %"PRIu8", type %"PRIu8,
1014 hdrs->version, hdrs->type);
1015 if (ofphdrs_is_stat(hdrs)) {
1016 ds_put_format(&s, ", stat %"PRIu16, hdrs->stat);
1019 ds_put_format(&s, ", vendor 0x%"PRIx32", subtype %"PRIu32,
1020 hdrs->vendor, hdrs->subtype);
1022 VLOG_WARN("unknown OpenFlow message (%s)", ds_cstr(&s));
1026 return (hdrs->vendor ? OFPERR_OFPBRC_BAD_SUBTYPE
1027 : ofphdrs_is_stat(hdrs) ? OFPERR_OFPBRC_BAD_STAT
1028 : OFPERR_OFPBRC_BAD_TYPE);
1034 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1035 const struct raw_info *info;
1037 if (!ovsthread_once_start(&once)) {
1041 hmap_init(&raw_instance_map);
1042 for (info = raw_infos; info < &raw_infos[ARRAY_SIZE(raw_infos)]; info++)
1044 int n_instances = info->max_version - info->min_version + 1;
1045 struct raw_instance *inst;
1047 for (inst = info->instances;
1048 inst < &info->instances[n_instances];
1050 inst->hdrs_len = ofphdrs_len(&inst->hdrs);
1051 hmap_insert(&raw_instance_map, &inst->hmap_node,
1052 ofphdrs_hash(&inst->hdrs));
1056 ovsthread_once_done(&once);