2 * Copyright (c) 2012, 2013, 2014 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;
273 return type == OFPT11_STATS_REQUEST;
280 ofp_is_stat_reply(enum ofp_version version, uint8_t type)
284 return type == OFPT10_STATS_REPLY;
290 return type == OFPT11_STATS_REPLY;
297 ofp_is_stat(enum ofp_version version, uint8_t type)
299 return (ofp_is_stat_request(version, type) ||
300 ofp_is_stat_reply(version, type));
304 ofphdrs_is_stat(const struct ofphdrs *hdrs)
306 return ofp_is_stat(hdrs->version, hdrs->type);
310 ofphdrs_len(const struct ofphdrs *hdrs)
312 if (hdrs->type == OFPT_VENDOR) {
313 return sizeof(struct nicira_header);
316 switch ((enum ofp_version) hdrs->version) {
318 if (hdrs->type == OFPT10_STATS_REQUEST ||
319 hdrs->type == OFPT10_STATS_REPLY) {
320 return (hdrs->stat == OFPST_VENDOR
321 ? sizeof(struct nicira10_stats_msg)
322 : sizeof(struct ofp10_stats_msg));
331 if (hdrs->type == OFPT11_STATS_REQUEST ||
332 hdrs->type == OFPT11_STATS_REPLY) {
333 return (hdrs->stat == OFPST_VENDOR
334 ? sizeof(struct nicira11_stats_msg)
335 : sizeof(struct ofp11_stats_msg));
340 return sizeof(struct ofp_header);
343 /* Determines the OFPRAW_* type of the OpenFlow message at 'oh', which has
344 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
345 * data are readable at 'oh'.) On success, returns 0 and stores the type into
346 * '*raw'. On failure, returns an OFPERR_* error code and zeros '*raw'.
348 * This function checks that 'oh' is a valid length for its particular type of
349 * message, and returns an error if not. */
351 ofpraw_decode(enum ofpraw *raw, const struct ofp_header *oh)
355 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
356 return ofpraw_pull(raw, &msg);
359 /* Does the same job as ofpraw_decode(), except that it assert-fails if
360 * ofpraw_decode() would have reported an error. Thus, it's able to use the
361 * return value for the OFPRAW_* message type instead of an error code.
363 * (It only makes sense to use this function if you previously called
364 * ofpraw_decode() on the message and thus know that it's OK.) */
366 ofpraw_decode_assert(const struct ofp_header *oh)
371 error = ofpraw_decode(&raw, oh);
376 /* Determines the OFPRAW_* type of the OpenFlow message in 'msg', which starts
377 * at 'ofpbuf_data(msg)' and has length 'ofpbuf_size(msg)' bytes. On success,
378 * returns 0 and stores the type into '*rawp'. On failure, returns an OFPERR_*
379 * error code and zeros '*rawp'.
381 * This function checks that the message has a valid length for its particular
382 * type of message, and returns an error if not.
384 * In addition to setting '*rawp', this function pulls off the OpenFlow header
385 * (including the stats headers, vendor header, and any subtype header) with
386 * ofpbuf_pull(). It also sets 'msg->frame' to the start of the OpenFlow
387 * header and 'msg->l3' just beyond the headers (that is, to the final value of
388 * ofpbuf_data(msg)). */
390 ofpraw_pull(enum ofpraw *rawp, struct ofpbuf *msg)
392 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
394 const struct raw_instance *instance;
395 const struct raw_info *info;
398 unsigned int min_len;
404 /* Set default outputs. */
405 msg->frame = ofpbuf_data(msg);
406 ofpbuf_set_l3(msg, msg->frame);
409 len = ofpbuf_size(msg);
410 error = ofphdrs_decode(&hdrs, ofpbuf_data(msg), len);
415 error = ofpraw_from_ofphdrs(&raw, &hdrs);
420 info = raw_info_get(raw);
421 instance = raw_instance_get(info, hdrs.version);
422 msg->frame = ofpbuf_pull(msg, instance->hdrs_len);
423 ofpbuf_set_l3(msg, ofpbuf_data(msg));
425 min_len = instance->hdrs_len + info->min_body;
426 switch (info->extra_multiple) {
428 if (len != min_len) {
429 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (expected "
430 "length %u)", info->name, len, min_len);
431 return OFPERR_OFPBRC_BAD_LEN;
437 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (expected "
438 "length at least %u bytes)",
439 info->name, len, min_len);
440 return OFPERR_OFPBRC_BAD_LEN;
445 if (len < min_len || (len - min_len) % info->extra_multiple) {
446 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (must be "
447 "exactly %u bytes or longer by an integer multiple "
449 info->name, len, min_len, info->extra_multiple);
450 return OFPERR_OFPBRC_BAD_LEN;
459 /* Does the same job as ofpraw_pull(), except that it assert-fails if
460 * ofpraw_pull() would have reported an error. Thus, it's able to use the
461 * return value for the OFPRAW_* message type instead of an error code.
463 * (It only makes sense to use this function if you previously called
464 * ofpraw_decode() on the message and thus know that it's OK.) */
466 ofpraw_pull_assert(struct ofpbuf *msg)
471 error = ofpraw_pull(&raw, msg);
476 /* Determines the OFPRAW_* type of the OpenFlow message that starts at 'oh' and
477 * has length 'length' bytes. On success, returns 0 and stores the type into
478 * '*rawp'. On failure, returns an OFPERR_* error code and zeros '*rawp'.
480 * Unlike other functions for decoding message types, this one is not picky
481 * about message length. For example, it will successfully decode a message
482 * whose body is shorter than the minimum length for a message of its type.
483 * Thus, this is the correct function to use for decoding the type of a message
484 * that might have been truncated, such as the payload of an OpenFlow error
485 * message (which is allowed to be truncated to 64 bytes). */
487 ofpraw_decode_partial(enum ofpraw *raw,
488 const struct ofp_header *oh, size_t length)
493 error = ofphdrs_decode(&hdrs, oh, length);
495 error = ofpraw_from_ofphdrs(raw, &hdrs);
504 /* Encoding messages using OFPRAW_* values. */
506 static void ofpraw_put__(enum ofpraw, uint8_t version, ovs_be32 xid,
507 size_t extra_tailroom, struct ofpbuf *);
509 /* Allocates and returns a new ofpbuf that contains an OpenFlow header for
510 * 'raw' with OpenFlow version 'version' and a fresh OpenFlow transaction ID.
511 * The ofpbuf has enough tailroom for the minimum body length of 'raw', plus
512 * 'extra_tailroom' additional bytes.
514 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
515 * must specify a valid (raw, version) pair.
517 * In the returned ofpbuf, 'frame' points to the beginning of the
518 * OpenFlow header and 'l3' points just after it, to where the
519 * message's body will start. The caller must actually allocate the
520 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
522 * The caller owns the returned ofpbuf and must free it when it is no longer
523 * needed, e.g. with ofpbuf_delete(). */
525 ofpraw_alloc(enum ofpraw raw, uint8_t version, size_t extra_tailroom)
527 return ofpraw_alloc_xid(raw, version, alloc_xid(), extra_tailroom);
530 /* Same as ofpraw_alloc() but the caller provides the transaction ID. */
532 ofpraw_alloc_xid(enum ofpraw raw, uint8_t version, ovs_be32 xid,
533 size_t extra_tailroom)
535 struct ofpbuf *buf = ofpbuf_new(0);
536 ofpraw_put__(raw, version, xid, extra_tailroom, buf);
540 /* Same as ofpraw_alloc(), but obtains the OpenFlow version and transaction ID
541 * from 'request->version' and 'request->xid', respectively.
543 * Even though the version comes from 'request->version', the caller must still
544 * know what it is doing, by specifying a valid pairing of 'raw' and
545 * 'request->version', just like ofpraw_alloc(). */
547 ofpraw_alloc_reply(enum ofpraw raw, const struct ofp_header *request,
548 size_t extra_tailroom)
550 return ofpraw_alloc_xid(raw, request->version, request->xid,
554 /* Allocates and returns a new ofpbuf that contains an OpenFlow header that is
555 * a stats reply to the stats request in 'request', using the same OpenFlow
556 * version and transaction ID as 'request'. The ofpbuf has enough tailroom for
557 * the stats reply's minimum body length, plus 'extra_tailroom' additional
560 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
561 * value. Every stats request has a corresponding reply, so the (raw, version)
562 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
564 * In the returned ofpbuf, 'frame' points to the beginning of the
565 * OpenFlow header and 'l3' points just after it, to where the
566 * message's body will start. The caller must actually allocate the
567 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
569 * The caller owns the returned ofpbuf and must free it when it is no longer
570 * needed, e.g. with ofpbuf_delete(). */
572 ofpraw_alloc_stats_reply(const struct ofp_header *request,
573 size_t extra_tailroom)
575 enum ofpraw request_raw;
576 enum ofpraw reply_raw;
579 error = ofpraw_decode_partial(&request_raw, request,
580 ntohs(request->length));
583 reply_raw = ofpraw_stats_request_to_reply(request_raw, request->version);
584 ovs_assert(reply_raw);
586 return ofpraw_alloc_reply(reply_raw, request, extra_tailroom);
589 /* Appends to 'buf' an OpenFlow header for 'raw' with OpenFlow version
590 * 'version' and a fresh OpenFlow transaction ID. Preallocates enough tailroom
591 * in 'buf' for the minimum body length of 'raw', plus 'extra_tailroom'
594 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
595 * must specify a valid (raw, version) pair.
597 * Upon return, 'buf->frame' points to the beginning of the OpenFlow header and
598 * 'buf->l3' points just after it, to where the message's body will start. The
599 * caller must actually allocating the body into the space reserved for it,
600 * e.g. with ofpbuf_put_uninit(). */
602 ofpraw_put(enum ofpraw raw, uint8_t version, struct ofpbuf *buf)
604 ofpraw_put__(raw, version, alloc_xid(), 0, buf);
607 /* Same as ofpraw_put() but the caller provides the transaction ID. */
609 ofpraw_put_xid(enum ofpraw raw, uint8_t version, ovs_be32 xid,
612 ofpraw_put__(raw, version, xid, 0, buf);
615 /* Same as ofpraw_put(), but obtains the OpenFlow version and transaction ID
616 * from 'request->version' and 'request->xid', respectively.
618 * Even though the version comes from 'request->version', the caller must still
619 * know what it is doing, by specifying a valid pairing of 'raw' and
620 * 'request->version', just like ofpraw_put(). */
622 ofpraw_put_reply(enum ofpraw raw, const struct ofp_header *request,
625 ofpraw_put__(raw, request->version, request->xid, 0, buf);
628 /* Appends to 'buf' an OpenFlow header that is a stats reply to the stats
629 * request in 'request', using the same OpenFlow version and transaction ID as
630 * 'request'. Preallocate enough tailroom in 'buf for the stats reply's
631 * minimum body length, plus 'extra_tailroom' additional bytes.
633 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
634 * value. Every stats request has a corresponding reply, so the (raw, version)
635 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
637 * In the returned ofpbuf, 'frame' points to the beginning of the
638 * OpenFlow header and 'l3' points just after it, to where the
639 * message's body will start. The caller must actually allocate the
640 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
642 * The caller owns the returned ofpbuf and must free it when it is no longer
643 * needed, e.g. with ofpbuf_delete(). */
645 ofpraw_put_stats_reply(const struct ofp_header *request, struct ofpbuf *buf)
650 error = ofpraw_decode_partial(&raw, request, ntohs(request->length));
653 raw = ofpraw_stats_request_to_reply(raw, request->version);
656 ofpraw_put__(raw, request->version, request->xid, 0, buf);
660 ofpraw_put__(enum ofpraw raw, uint8_t version, ovs_be32 xid,
661 size_t extra_tailroom, struct ofpbuf *buf)
663 const struct raw_info *info = raw_info_get(raw);
664 const struct raw_instance *instance = raw_instance_get(info, version);
665 const struct ofphdrs *hdrs = &instance->hdrs;
666 struct ofp_header *oh;
668 ofpbuf_prealloc_tailroom(buf, (instance->hdrs_len + info->min_body
670 buf->frame = ofpbuf_put_uninit(buf, instance->hdrs_len);
671 ofpbuf_set_l3(buf, ofpbuf_tail(buf));
674 oh->version = version;
675 oh->type = hdrs->type;
676 oh->length = htons(ofpbuf_size(buf));
679 if (hdrs->type == OFPT_VENDOR) {
680 struct nicira_header *nh = buf->frame;
682 ovs_assert(hdrs->vendor == NX_VENDOR_ID);
683 nh->vendor = htonl(hdrs->vendor);
684 nh->subtype = htonl(hdrs->subtype);
685 } else if (version == OFP10_VERSION
686 && (hdrs->type == OFPT10_STATS_REQUEST ||
687 hdrs->type == OFPT10_STATS_REPLY)) {
688 struct ofp10_stats_msg *osm = buf->frame;
690 osm->type = htons(hdrs->stat);
691 osm->flags = htons(0);
693 if (hdrs->stat == OFPST_VENDOR) {
694 struct ofp10_vendor_stats_msg *ovsm = buf->frame;
696 ovsm->vendor = htonl(hdrs->vendor);
697 if (hdrs->vendor == NX_VENDOR_ID) {
698 struct nicira10_stats_msg *nsm = buf->frame;
700 nsm->subtype = htonl(hdrs->subtype);
701 memset(nsm->pad, 0, sizeof nsm->pad);
706 } else if (version != OFP10_VERSION
707 && (hdrs->type == OFPT11_STATS_REQUEST ||
708 hdrs->type == OFPT11_STATS_REPLY)) {
709 struct ofp11_stats_msg *osm = buf->frame;
711 osm->type = htons(hdrs->stat);
712 osm->flags = htons(0);
713 memset(osm->pad, 0, sizeof osm->pad);
715 if (hdrs->stat == OFPST_VENDOR) {
716 struct ofp11_vendor_stats_msg *ovsm = buf->frame;
718 ovsm->vendor = htonl(hdrs->vendor);
719 if (hdrs->vendor == NX_VENDOR_ID) {
720 struct nicira11_stats_msg *nsm = buf->frame;
722 nsm->subtype = htonl(hdrs->subtype);
730 /* Returns 'raw''s name.
732 * The name is the name used for 'raw' in the OpenFlow specification. For
733 * example, ofpraw_get_name(OFPRAW_OFPT10_FEATURES_REPLY) is
734 * "OFPT_FEATURES_REPLY".
736 * The caller must not modify or free the returned string. */
738 ofpraw_get_name(enum ofpraw raw)
740 return raw_info_get(raw)->name;
743 /* Returns the stats reply that corresponds to 'raw' in the given OpenFlow
746 ofpraw_stats_request_to_reply(enum ofpraw raw, uint8_t version)
748 const struct raw_info *info = raw_info_get(raw);
749 const struct raw_instance *instance = raw_instance_get(info, version);
750 enum ofpraw reply_raw;
754 hdrs = instance->hdrs;
755 switch ((enum ofp_version)hdrs.version) {
757 ovs_assert(hdrs.type == OFPT10_STATS_REQUEST);
758 hdrs.type = OFPT10_STATS_REPLY;
765 ovs_assert(hdrs.type == OFPT11_STATS_REQUEST);
766 hdrs.type = OFPT11_STATS_REPLY;
772 error = ofpraw_from_ofphdrs(&reply_raw, &hdrs);
778 /* Determines the OFPTYPE_* type of the OpenFlow message at 'oh', which has
779 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
780 * data are readable at 'oh'.) On success, returns 0 and stores the type into
781 * '*typep'. On failure, returns an OFPERR_* error code and zeros '*typep'.
783 * This function checks that 'oh' is a valid length for its particular type of
784 * message, and returns an error if not. */
786 ofptype_decode(enum ofptype *typep, const struct ofp_header *oh)
791 error = ofpraw_decode(&raw, oh);
792 *typep = error ? 0 : ofptype_from_ofpraw(raw);
796 /* Determines the OFPTYPE_* type of the OpenFlow message in 'msg', which starts
797 * at 'ofpbuf_data(msg)' and has length 'ofpbuf_size(msg)' bytes. On success,
798 * returns 0 and stores the type into '*typep'. On failure, returns an
799 * OFPERR_* error code and zeros '*typep'.
801 * This function checks that the message has a valid length for its particular
802 * type of message, and returns an error if not.
804 * In addition to setting '*typep', this function pulls off the OpenFlow header
805 * (including the stats headers, vendor header, and any subtype header) with
806 * ofpbuf_pull(). It also sets 'msg->frame' to the start of the OpenFlow
807 * header and 'msg->l3' just beyond the headers (that is, to the final value of
808 * ofpbuf_data(msg)). */
810 ofptype_pull(enum ofptype *typep, struct ofpbuf *buf)
815 error = ofpraw_pull(&raw, buf);
816 *typep = error ? 0 : ofptype_from_ofpraw(raw);
820 /* Returns the OFPTYPE_* type that corresponds to 'raw'.
822 * (This is a one-way trip, because the mapping from ofpraw to ofptype is
825 ofptype_from_ofpraw(enum ofpraw raw)
827 return raw_info_get(raw)->type;
830 /* Updates the 'length' field of the OpenFlow message in 'buf' to
831 * 'ofpbuf_size(buf)'. */
833 ofpmsg_update_length(struct ofpbuf *buf)
835 struct ofp_header *oh = ofpbuf_at_assert(buf, 0, sizeof *oh);
836 oh->length = htons(ofpbuf_size(buf));
839 /* Returns just past the Openflow header (including the stats headers, vendor
840 * header, and any subtype header) in 'oh'. */
842 ofpmsg_body(const struct ofp_header *oh)
846 ofphdrs_decode_assert(&hdrs, oh, ntohs(oh->length));
847 return (const uint8_t *) oh + ofphdrs_len(&hdrs);
850 /* Return if it's a stat/multipart (OFPST) request message. */
852 ofpmsg_is_stat_request(const struct ofp_header *oh)
854 return ofp_is_stat_request(oh->version, oh->type);
857 static ovs_be16 *ofpmp_flags__(const struct ofp_header *);
859 /* Initializes 'replies' as a new list of stats messages that reply to
860 * 'request', which must be a stats request message. Initially the list will
861 * consist of only a single reply part without any body. The caller should
862 * use calls to the other ofpmp_*() functions to add to the body and split the
863 * message into multiple parts, if necessary. */
865 ofpmp_init(struct list *replies, const struct ofp_header *request)
871 msg = ofpraw_alloc_stats_reply(request, 1000);
872 list_push_back(replies, &msg->list_node);
875 /* Prepares to append up to 'len' bytes to the series of statistics replies in
876 * 'replies', which should have been initialized with ofpmp_init(), if
877 * necessary adding a new reply to the list.
879 * Returns an ofpbuf with at least 'len' bytes of tailroom. The 'len' bytes
880 * have not actually been allocated, so the caller must do so with
881 * e.g. ofpbuf_put_uninit(). */
883 ofpmp_reserve(struct list *replies, size_t len)
885 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
887 if (ofpbuf_size(msg) + len <= UINT16_MAX) {
888 ofpbuf_prealloc_tailroom(msg, len);
891 unsigned int hdrs_len;
895 ofphdrs_decode_assert(&hdrs, ofpbuf_data(msg), ofpbuf_size(msg));
896 hdrs_len = ofphdrs_len(&hdrs);
898 next = ofpbuf_new(MAX(1024, hdrs_len + len));
899 ofpbuf_put(next, ofpbuf_data(msg), hdrs_len);
900 next->frame = ofpbuf_data(next);
901 ofpbuf_set_l3(next, ofpbuf_tail(next));
902 list_push_back(replies, &next->list_node);
904 *ofpmp_flags__(ofpbuf_data(msg)) |= htons(OFPSF_REPLY_MORE);
910 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
911 * returns the first byte. */
913 ofpmp_append(struct list *replies, size_t len)
915 return ofpbuf_put_uninit(ofpmp_reserve(replies, len), len);
918 /* Sometimes, when composing stats replies, it's difficult to predict how long
919 * an individual reply chunk will be before actually encoding it into the reply
920 * buffer. This function allows easy handling of this case: just encode the
921 * reply, then use this function to break the message into two pieces if it
922 * exceeds the OpenFlow message limit.
924 * In detail, if the final stats message in 'replies' is too long for OpenFlow,
925 * this function breaks it into two separate stats replies, the first one with
926 * the first 'start_ofs' bytes, the second one containing the bytes from that
929 ofpmp_postappend(struct list *replies, size_t start_ofs)
931 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
933 ovs_assert(start_ofs <= UINT16_MAX);
934 if (ofpbuf_size(msg) > UINT16_MAX) {
935 size_t len = ofpbuf_size(msg) - start_ofs;
936 memcpy(ofpmp_append(replies, len),
937 (const uint8_t *) ofpbuf_data(msg) + start_ofs, len);
938 ofpbuf_set_size(msg, start_ofs);
942 /* Returns the OpenFlow version of the replies being constructed in 'replies',
943 * which should have been initialized by ofpmp_init(). */
945 ofpmp_version(struct list *replies)
947 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
948 const struct ofp_header *oh = ofpbuf_data(msg);
953 /* Determines the OFPRAW_* type of the OpenFlow messages in 'replies', which
954 * should have been initialized by ofpmp_init(). */
956 ofpmp_decode_raw(struct list *replies)
958 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
962 error = ofpraw_decode_partial(&raw, ofpbuf_data(msg), ofpbuf_size(msg));
968 ofpmp_flags__(const struct ofp_header *oh)
970 switch ((enum ofp_version)oh->version) {
972 return &((struct ofp10_stats_msg *) oh)->flags;
978 return &((struct ofp11_stats_msg *) oh)->flags;
984 /* Returns the OFPSF_* flags found in the OpenFlow stats header of 'oh', which
985 * must be an OpenFlow stats request or reply.
987 * (OFPSF_REPLY_MORE is the only defined flag.) */
989 ofpmp_flags(const struct ofp_header *oh)
991 return ntohs(*ofpmp_flags__(oh));
994 /* Returns true if the OFPSF_REPLY_MORE flag is set in the OpenFlow stats
995 * header of 'oh', which must be an OpenFlow stats request or reply, false if
998 ofpmp_more(const struct ofp_header *oh)
1000 return (ofpmp_flags(oh) & OFPSF_REPLY_MORE) != 0;
1003 static void ofpmsgs_init(void);
1005 static const struct raw_info *
1006 raw_info_get(enum ofpraw raw)
1010 ovs_assert(raw < ARRAY_SIZE(raw_infos));
1011 return &raw_infos[raw];
1014 static struct raw_instance *
1015 raw_instance_get(const struct raw_info *info, uint8_t version)
1017 ovs_assert(version >= info->min_version && version <= info->max_version);
1018 return &info->instances[version - info->min_version];
1022 ofpraw_from_ofphdrs(enum ofpraw *raw, const struct ofphdrs *hdrs)
1024 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1026 struct raw_instance *raw_hdrs;
1031 hash = ofphdrs_hash(hdrs);
1032 HMAP_FOR_EACH_WITH_HASH (raw_hdrs, hmap_node, hash, &raw_instance_map) {
1033 if (ofphdrs_equal(hdrs, &raw_hdrs->hdrs)) {
1034 *raw = raw_hdrs->raw;
1039 if (!VLOG_DROP_WARN(&rl)) {
1043 ds_put_format(&s, "version %"PRIu8", type %"PRIu8,
1044 hdrs->version, hdrs->type);
1045 if (ofphdrs_is_stat(hdrs)) {
1046 ds_put_format(&s, ", stat %"PRIu16, hdrs->stat);
1049 ds_put_format(&s, ", vendor 0x%"PRIx32", subtype %"PRIu32,
1050 hdrs->vendor, hdrs->subtype);
1052 VLOG_WARN("unknown OpenFlow message (%s)", ds_cstr(&s));
1056 return (hdrs->vendor ? OFPERR_OFPBRC_BAD_SUBTYPE
1057 : ofphdrs_is_stat(hdrs) ? OFPERR_OFPBRC_BAD_STAT
1058 : OFPERR_OFPBRC_BAD_TYPE);
1064 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1065 const struct raw_info *info;
1067 if (!ovsthread_once_start(&once)) {
1071 hmap_init(&raw_instance_map);
1072 for (info = raw_infos; info < &raw_infos[ARRAY_SIZE(raw_infos)]; info++)
1074 int n_instances = info->max_version - info->min_version + 1;
1075 struct raw_instance *inst;
1077 for (inst = info->instances;
1078 inst < &info->instances[n_instances];
1080 inst->hdrs_len = ofphdrs_len(&inst->hdrs);
1081 hmap_insert(&raw_instance_map, &inst->hmap_node,
1082 ofphdrs_hash(&inst->hdrs));
1086 ovsthread_once_done(&once);