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"
20 #include "openvswitch/hmap.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"
30 VLOG_DEFINE_THIS_MODULE(ofp_msgs
);
33 #define OFPT10_STATS_REQUEST 16
34 #define OFPT10_STATS_REPLY 17
35 #define OFPT11_STATS_REQUEST 18
36 #define OFPT11_STATS_REPLY 19
37 #define OFPST_VENDOR 0xffff
39 /* Vendor extension message. */
40 struct ofp_vendor_header
{
41 struct ofp_header header
; /* OFPT_VENDOR. */
42 ovs_be32 vendor
; /* Vendor ID:
43 * - MSB 0: low-order bytes are IEEE OUI.
44 * - MSB != 0: defined by OpenFlow
47 /* In theory everything after 'vendor' is vendor specific. In practice,
48 * the vendors we support put a 32-bit subtype here. We'll change this
49 * structure if we start adding support for other vendor formats. */
50 ovs_be32 subtype
; /* Vendor-specific subtype. */
52 /* Followed by vendor-defined additional data. */
54 OFP_ASSERT(sizeof(struct ofp_vendor_header
) == 16);
56 /* Statistics request or reply message. */
57 struct ofp10_stats_msg
{
58 struct ofp_header header
;
59 ovs_be16 type
; /* One of the OFPST_* constants. */
60 ovs_be16 flags
; /* Requests: always 0.
61 * Replies: 0 or OFPSF_REPLY_MORE. */
63 OFP_ASSERT(sizeof(struct ofp10_stats_msg
) == 12);
65 /* Vendor extension stats message. */
66 struct ofp10_vendor_stats_msg
{
67 struct ofp10_stats_msg osm
; /* Type OFPST_VENDOR. */
68 ovs_be32 vendor
; /* Vendor ID:
69 * - MSB 0: low-order bytes are IEEE OUI.
70 * - MSB != 0: defined by OpenFlow
72 /* Followed by vendor-defined arbitrary additional data. */
74 OFP_ASSERT(sizeof(struct ofp10_vendor_stats_msg
) == 16);
76 struct ofp11_stats_msg
{
77 struct ofp_header header
;
78 ovs_be16 type
; /* One of the OFPST_* constants. */
79 ovs_be16 flags
; /* OFPSF_REQ_* flags (none yet defined). */
81 /* Followed by the body of the request. */
83 OFP_ASSERT(sizeof(struct ofp11_stats_msg
) == 16);
85 /* Vendor extension stats message. */
86 struct ofp11_vendor_stats_msg
{
87 struct ofp11_stats_msg osm
; /* Type OFPST_VENDOR. */
88 ovs_be32 vendor
; /* Vendor ID:
89 * - MSB 0: low-order bytes are IEEE OUI.
90 * - MSB != 0: defined by OpenFlow
93 /* In theory everything after 'vendor' is vendor specific. In practice,
94 * the vendors we support put a 32-bit subtype here. We'll change this
95 * structure if we start adding support for other vendor formats. */
96 ovs_be32 subtype
; /* Vendor-specific subtype. */
98 /* Followed by vendor-defined additional data. */
100 OFP_ASSERT(sizeof(struct ofp11_vendor_stats_msg
) == 24);
102 /* Header for Nicira vendor stats request and reply messages in OpenFlow
104 struct nicira10_stats_msg
{
105 struct ofp10_vendor_stats_msg vsm
; /* Vendor NX_VENDOR_ID. */
106 ovs_be32 subtype
; /* One of NXST_* below. */
107 uint8_t pad
[4]; /* Align to 64-bits. */
109 OFP_ASSERT(sizeof(struct nicira10_stats_msg
) == 24);
111 /* A thin abstraction of OpenFlow headers:
113 * - 'version' and 'type' come straight from struct ofp_header, so these are
114 * always present and meaningful.
116 * - 'stat' comes from the 'type' member in statistics messages only. It is
117 * meaningful, therefore, only if 'version' and 'type' taken together
118 * specify a statistics request or reply. Otherwise it is 0.
120 * - 'vendor' is meaningful only for vendor messages, that is, if 'version'
121 * and 'type' specify a vendor message or if 'version' and 'type' specify
122 * a statistics message and 'stat' specifies a vendor statistic type.
125 * - 'subtype' is meaningful only for vendor messages and otherwise 0. It
126 * specifies a vendor-defined subtype. There is no standard format for
127 * these but 32 bits seems like it should be enough. */
129 uint8_t version
; /* From ofp_header. */
130 uint8_t type
; /* From ofp_header. */
131 uint16_t stat
; /* From ofp10_stats_msg or ofp11_stats_msg. */
132 uint32_t vendor
; /* From ofp_vendor_header,
133 * ofp10_vendor_stats_msg, or
134 * ofp11_vendor_stats_msg. */
135 uint32_t subtype
; /* From nicira_header, nicira10_stats_msg, or
136 * nicira11_stats_msg. */
138 BUILD_ASSERT_DECL(sizeof(struct ofphdrs
) == 12);
140 /* A mapping from OpenFlow headers to OFPRAW_*. */
141 struct raw_instance
{
142 struct hmap_node hmap_node
; /* In 'raw_instance_map'. */
143 struct ofphdrs hdrs
; /* Key. */
144 enum ofpraw raw
; /* Value. */
145 unsigned int hdrs_len
; /* ofphdrs_len(hdrs). */
148 /* Information about a particular 'enum ofpraw'. */
150 /* All possible instantiations of this OFPRAW_* into OpenFlow headers. */
151 struct raw_instance
*instances
; /* min_version - max_version + 1 elems. */
155 unsigned int min_body
;
156 unsigned int extra_multiple
;
161 /* All understood OpenFlow message types, indexed by their 'struct ofphdrs'. */
162 static struct hmap raw_instance_map
;
163 #include "ofp-msgs.inc"
165 static ovs_be32
alloc_xid(void);
167 /* ofphdrs functions. */
168 static uint32_t ofphdrs_hash(const struct ofphdrs
*);
169 static bool ofphdrs_equal(const struct ofphdrs
*a
, const struct ofphdrs
*b
);
170 static enum ofperr
ofphdrs_decode(struct ofphdrs
*,
171 const struct ofp_header
*oh
, size_t length
);
172 static void ofphdrs_decode_assert(struct ofphdrs
*,
173 const struct ofp_header
*oh
, size_t length
);
174 size_t ofphdrs_len(const struct ofphdrs
*);
176 static const struct raw_info
*raw_info_get(enum ofpraw
);
177 static struct raw_instance
*raw_instance_get(const struct raw_info
*,
180 static enum ofperr
ofpraw_from_ofphdrs(enum ofpraw
*, const struct ofphdrs
*);
182 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
186 static atomic_count next_xid
= ATOMIC_COUNT_INIT(1);
188 return htonl(atomic_count_inc(&next_xid
));
192 ofphdrs_hash(const struct ofphdrs
*hdrs
)
194 BUILD_ASSERT_DECL(sizeof *hdrs
% 4 == 0);
195 return hash_bytes32((const uint32_t *) hdrs
, sizeof *hdrs
, 0);
199 ofphdrs_equal(const struct ofphdrs
*a
, const struct ofphdrs
*b
)
201 return !memcmp(a
, b
, sizeof *a
);
205 log_bad_vendor(uint32_t vendor
)
207 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
209 VLOG_WARN_RL(&rl
, "OpenFlow message has unknown vendor %#"PRIx32
, vendor
);
213 ofphdrs_decode(struct ofphdrs
*hdrs
,
214 const struct ofp_header
*oh
, size_t length
)
216 memset(hdrs
, 0, sizeof *hdrs
);
217 if (length
< sizeof *oh
) {
218 return OFPERR_OFPBRC_BAD_LEN
;
221 /* Get base message version and type (OFPT_*). */
222 hdrs
->version
= oh
->version
;
223 hdrs
->type
= oh
->type
;
225 if (hdrs
->type
== OFPT_VENDOR
) {
227 const struct ofp_vendor_header
*ovh
;
229 if (length
< sizeof *ovh
) {
230 return OFPERR_OFPBRC_BAD_LEN
;
233 ovh
= (const struct ofp_vendor_header
*) oh
;
234 hdrs
->vendor
= ntohl(ovh
->vendor
);
235 if (hdrs
->vendor
== NX_VENDOR_ID
|| hdrs
->vendor
== ONF_VENDOR_ID
) {
236 hdrs
->subtype
= ntohl(ovh
->subtype
);
238 log_bad_vendor(hdrs
->vendor
);
239 return OFPERR_OFPBRC_BAD_VENDOR
;
241 } else if (hdrs
->version
== OFP10_VERSION
242 && (hdrs
->type
== OFPT10_STATS_REQUEST
||
243 hdrs
->type
== OFPT10_STATS_REPLY
)) {
244 const struct ofp10_stats_msg
*osm
;
246 /* Get statistic type (OFPST_*). */
247 if (length
< sizeof *osm
) {
248 return OFPERR_OFPBRC_BAD_LEN
;
250 osm
= (const struct ofp10_stats_msg
*) oh
;
251 hdrs
->stat
= ntohs(osm
->type
);
253 if (hdrs
->stat
== OFPST_VENDOR
) {
255 const struct ofp10_vendor_stats_msg
*ovsm
;
257 if (length
< sizeof *ovsm
) {
258 return OFPERR_OFPBRC_BAD_LEN
;
261 ovsm
= (const struct ofp10_vendor_stats_msg
*) oh
;
262 hdrs
->vendor
= ntohl(ovsm
->vendor
);
263 if (hdrs
->vendor
== NX_VENDOR_ID
) {
264 /* Get Nicira statistic type (NXST_*). */
265 const struct nicira10_stats_msg
*nsm
;
267 if (length
< sizeof *nsm
) {
268 return OFPERR_OFPBRC_BAD_LEN
;
270 nsm
= (const struct nicira10_stats_msg
*) oh
;
271 hdrs
->subtype
= ntohl(nsm
->subtype
);
273 log_bad_vendor(hdrs
->vendor
);
274 return OFPERR_OFPBRC_BAD_VENDOR
;
277 } else if (hdrs
->version
!= OFP10_VERSION
278 && (hdrs
->type
== OFPT11_STATS_REQUEST
||
279 hdrs
->type
== OFPT11_STATS_REPLY
)) {
280 const struct ofp11_stats_msg
*osm
;
282 /* Get statistic type (OFPST_*). */
283 if (length
< sizeof *osm
) {
284 return OFPERR_OFPBRC_BAD_LEN
;
286 osm
= (const struct ofp11_stats_msg
*) oh
;
287 hdrs
->stat
= ntohs(osm
->type
);
289 if (hdrs
->stat
== OFPST_VENDOR
) {
291 const struct ofp11_vendor_stats_msg
*ovsm
;
293 if (length
< sizeof *ovsm
) {
294 return OFPERR_OFPBRC_BAD_LEN
;
297 ovsm
= (const struct ofp11_vendor_stats_msg
*) oh
;
298 hdrs
->vendor
= ntohl(ovsm
->vendor
);
299 if (hdrs
->vendor
== NX_VENDOR_ID
||
300 hdrs
->vendor
== ONF_VENDOR_ID
) {
301 hdrs
->subtype
= ntohl(ovsm
->subtype
);
303 log_bad_vendor(hdrs
->vendor
);
304 return OFPERR_OFPBRC_BAD_VENDOR
;
313 ofphdrs_decode_assert(struct ofphdrs
*hdrs
,
314 const struct ofp_header
*oh
, size_t length
)
316 ovs_assert(!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
)
424 ovs_assert(!ofpraw_decode(&raw
, oh
));
428 /* Checks that 'len' is a valid length for an OpenFlow message that corresponds
429 * to 'info' and 'instance'. Returns 0 if so, otherwise an OpenFlow error. */
431 ofpraw_check_length(const struct raw_info
*info
,
432 const struct raw_instance
*instance
,
435 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
437 unsigned int min_len
= instance
->hdrs_len
+ info
->min_body
;
438 switch (info
->extra_multiple
) {
440 if (len
!= min_len
) {
441 VLOG_WARN_RL(&rl
, "received %s with incorrect length %u (expected "
442 "length %u)", info
->name
, len
, min_len
);
443 return OFPERR_OFPBRC_BAD_LEN
;
449 VLOG_WARN_RL(&rl
, "received %s with incorrect length %u (expected "
450 "length at least %u bytes)",
451 info
->name
, len
, min_len
);
452 return OFPERR_OFPBRC_BAD_LEN
;
457 if (len
< min_len
|| (len
- min_len
) % info
->extra_multiple
) {
458 VLOG_WARN_RL(&rl
, "received %s with incorrect length %u (must be "
459 "exactly %u bytes or longer by an integer multiple "
461 info
->name
, len
, min_len
, info
->extra_multiple
);
462 return OFPERR_OFPBRC_BAD_LEN
;
470 /* Determines the OFPRAW_* type of the OpenFlow message in 'msg', which starts
471 * at 'msg->data' and has length 'msg->size' bytes. On success,
472 * returns 0 and stores the type into '*rawp'. On failure, returns an OFPERR_*
473 * error code and zeros '*rawp'.
475 * This function checks that the message has a valid length for its particular
476 * type of message, and returns an error if not.
478 * In addition to setting '*rawp', this function pulls off the OpenFlow header
479 * (including the stats headers, vendor header, and any subtype header) with
480 * ofpbuf_pull(). It also sets 'msg->header' to the start of the OpenFlow
481 * header and 'msg->msg' just beyond the headers (that is, to the final value
484 ofpraw_pull(enum ofpraw
*rawp
, struct ofpbuf
*msg
)
486 /* Set default outputs. */
487 msg
->header
= msg
->data
;
488 msg
->msg
= msg
->header
;
492 enum ofperr error
= ofphdrs_decode(&hdrs
, msg
->data
, msg
->size
);
498 error
= ofpraw_from_ofphdrs(&raw
, &hdrs
);
503 const struct raw_info
*info
= raw_info_get(raw
);
504 const struct raw_instance
*instance
= raw_instance_get(info
, hdrs
.version
);
505 error
= ofpraw_check_length(info
, instance
, msg
->size
);
510 msg
->header
= ofpbuf_pull(msg
, instance
->hdrs_len
);
511 msg
->msg
= msg
->data
;
516 /* Does the same job as ofpraw_pull(), except that it assert-fails if
517 * ofpraw_pull() would have reported an error. Thus, it's able to use the
518 * return value for the OFPRAW_* message type instead of an error code.
520 * (It only makes sense to use this function if you previously called
521 * ofpraw_decode() on the message and thus know that it's OK.) */
523 ofpraw_pull_assert(struct ofpbuf
*msg
)
526 ovs_assert(!ofpraw_pull(&raw
, msg
));
530 /* Determines the OFPRAW_* type of the OpenFlow message that starts at 'oh' and
531 * has length 'length' bytes. On success, returns 0 and stores the type into
532 * '*rawp'. On failure, returns an OFPERR_* error code and zeros '*rawp'.
534 * Unlike other functions for decoding message types, this one is not picky
535 * about message length. For example, it will successfully decode a message
536 * whose body is shorter than the minimum length for a message of its type.
537 * Thus, this is the correct function to use for decoding the type of a message
538 * that might have been truncated, such as the payload of an OpenFlow error
539 * message (which is allowed to be truncated to 64 bytes). */
541 ofpraw_decode_partial(enum ofpraw
*raw
,
542 const struct ofp_header
*oh
, size_t length
)
547 error
= ofphdrs_decode(&hdrs
, oh
, length
);
549 error
= ofpraw_from_ofphdrs(raw
, &hdrs
);
558 /* Encoding messages using OFPRAW_* values. */
560 static void ofpraw_put__(enum ofpraw
, uint8_t version
, ovs_be32 xid
,
561 size_t extra_tailroom
, struct ofpbuf
*);
563 /* Allocates and returns a new ofpbuf that contains an OpenFlow header for
564 * 'raw' with OpenFlow version 'version' and a fresh OpenFlow transaction ID.
565 * The ofpbuf has enough tailroom for the minimum body length of 'raw', plus
566 * 'extra_tailroom' additional bytes.
568 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
569 * must specify a valid (raw, version) pair.
571 * In the returned ofpbuf, 'header' points to the beginning of the
572 * OpenFlow header and 'msg' points just after it, to where the
573 * message's body will start. The caller must actually allocate the
574 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
576 * The caller owns the returned ofpbuf and must free it when it is no longer
577 * needed, e.g. with ofpbuf_delete(). */
579 ofpraw_alloc(enum ofpraw raw
, uint8_t version
, size_t extra_tailroom
)
581 return ofpraw_alloc_xid(raw
, version
, alloc_xid(), extra_tailroom
);
584 /* Same as ofpraw_alloc() but the caller provides the transaction ID. */
586 ofpraw_alloc_xid(enum ofpraw raw
, uint8_t version
, ovs_be32 xid
,
587 size_t extra_tailroom
)
589 struct ofpbuf
*buf
= ofpbuf_new(0);
590 ofpraw_put__(raw
, version
, xid
, extra_tailroom
, buf
);
594 /* Same as ofpraw_alloc(), but obtains the OpenFlow version and transaction ID
595 * from 'request->version' and 'request->xid', respectively.
597 * Even though the version comes from 'request->version', the caller must still
598 * know what it is doing, by specifying a valid pairing of 'raw' and
599 * 'request->version', just like ofpraw_alloc(). */
601 ofpraw_alloc_reply(enum ofpraw raw
, const struct ofp_header
*request
,
602 size_t extra_tailroom
)
604 return ofpraw_alloc_xid(raw
, request
->version
, request
->xid
,
608 /* Allocates and returns a new ofpbuf that contains an OpenFlow header that is
609 * a stats reply to the stats request in 'request', using the same OpenFlow
610 * version and transaction ID as 'request'. The ofpbuf has enough tailroom for
611 * the stats reply's minimum body length, plus 'extra_tailroom' additional
614 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
615 * value. Every stats request has a corresponding reply, so the (raw, version)
616 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
618 * In the returned ofpbuf, 'header' points to the beginning of the
619 * OpenFlow header and 'msg' points just after it, to where the
620 * message's body will start. The caller must actually allocate the
621 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
623 * The caller owns the returned ofpbuf and must free it when it is no longer
624 * needed, e.g. with ofpbuf_delete(). */
626 ofpraw_alloc_stats_reply(const struct ofp_header
*request
,
627 size_t extra_tailroom
)
629 enum ofpraw request_raw
;
630 enum ofpraw reply_raw
;
632 ovs_assert(!ofpraw_decode_partial(&request_raw
, request
,
633 ntohs(request
->length
)));
635 reply_raw
= ofpraw_stats_request_to_reply(request_raw
, request
->version
);
636 ovs_assert(reply_raw
);
638 return ofpraw_alloc_reply(reply_raw
, request
, extra_tailroom
);
641 /* Appends to 'buf' an OpenFlow header for 'raw' with OpenFlow version
642 * 'version' and a fresh OpenFlow transaction ID. Preallocates enough tailroom
643 * in 'buf' for the minimum body length of 'raw', plus 'extra_tailroom'
646 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
647 * must specify a valid (raw, version) pair.
649 * Upon return, 'buf->header' points to the beginning of the OpenFlow header
650 * and 'buf->msg' points just after it, to where the message's body will start.
651 * The caller must actually allocating the body into the space reserved for it,
652 * e.g. with ofpbuf_put_uninit(). */
654 ofpraw_put(enum ofpraw raw
, uint8_t version
, struct ofpbuf
*buf
)
656 ofpraw_put__(raw
, version
, alloc_xid(), 0, buf
);
659 /* Same as ofpraw_put() but the caller provides the transaction ID. */
661 ofpraw_put_xid(enum ofpraw raw
, uint8_t version
, ovs_be32 xid
,
664 ofpraw_put__(raw
, version
, xid
, 0, buf
);
667 /* Same as ofpraw_put(), but obtains the OpenFlow version and transaction ID
668 * from 'request->version' and 'request->xid', respectively.
670 * Even though the version comes from 'request->version', the caller must still
671 * know what it is doing, by specifying a valid pairing of 'raw' and
672 * 'request->version', just like ofpraw_put(). */
674 ofpraw_put_reply(enum ofpraw raw
, const struct ofp_header
*request
,
677 ofpraw_put__(raw
, request
->version
, request
->xid
, 0, buf
);
680 /* Appends to 'buf' an OpenFlow header that is a stats reply to the stats
681 * request in 'request', using the same OpenFlow version and transaction ID as
682 * 'request'. Preallocate enough tailroom in 'buf for the stats reply's
683 * minimum body length, plus 'extra_tailroom' additional bytes.
685 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
686 * value. Every stats request has a corresponding reply, so the (raw, version)
687 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
689 * In the returned ofpbuf, 'header' points to the beginning of the
690 * OpenFlow header and 'msg' points just after it, to where the
691 * message's body will start. The caller must actually allocate the
692 * body into the space reserved for it, e.g. with ofpbuf_put_uninit().
694 * The caller owns the returned ofpbuf and must free it when it is no longer
695 * needed, e.g. with ofpbuf_delete(). */
697 ofpraw_put_stats_reply(const struct ofp_header
*request
, struct ofpbuf
*buf
)
701 ovs_assert(!ofpraw_decode_partial(&raw
, request
, ntohs(request
->length
)));
703 raw
= ofpraw_stats_request_to_reply(raw
, request
->version
);
706 ofpraw_put__(raw
, request
->version
, request
->xid
, 0, buf
);
710 ofpraw_put__(enum ofpraw raw
, uint8_t version
, ovs_be32 xid
,
711 size_t extra_tailroom
, struct ofpbuf
*buf
)
713 const struct raw_info
*info
= raw_info_get(raw
);
714 const struct raw_instance
*instance
= raw_instance_get(info
, version
);
715 const struct ofphdrs
*hdrs
= &instance
->hdrs
;
716 struct ofp_header
*oh
;
718 ofpbuf_prealloc_tailroom(buf
, (instance
->hdrs_len
+ info
->min_body
720 buf
->header
= ofpbuf_put_uninit(buf
, instance
->hdrs_len
);
721 buf
->msg
= ofpbuf_tail(buf
);
724 oh
->version
= version
;
725 oh
->type
= hdrs
->type
;
726 oh
->length
= htons(buf
->size
);
729 if (hdrs
->type
== OFPT_VENDOR
) {
730 struct ofp_vendor_header
*ovh
= buf
->header
;
732 ovh
->vendor
= htonl(hdrs
->vendor
);
733 ovh
->subtype
= htonl(hdrs
->subtype
);
734 } else if (version
== OFP10_VERSION
735 && (hdrs
->type
== OFPT10_STATS_REQUEST
||
736 hdrs
->type
== OFPT10_STATS_REPLY
)) {
737 struct ofp10_stats_msg
*osm
= buf
->header
;
739 osm
->type
= htons(hdrs
->stat
);
740 osm
->flags
= htons(0);
742 if (hdrs
->stat
== OFPST_VENDOR
) {
743 struct ofp10_vendor_stats_msg
*ovsm
= buf
->header
;
745 ovsm
->vendor
= htonl(hdrs
->vendor
);
746 if (hdrs
->vendor
== NX_VENDOR_ID
) {
747 struct nicira10_stats_msg
*nsm
= buf
->header
;
749 nsm
->subtype
= htonl(hdrs
->subtype
);
750 memset(nsm
->pad
, 0, sizeof nsm
->pad
);
755 } else if (version
!= OFP10_VERSION
756 && (hdrs
->type
== OFPT11_STATS_REQUEST
||
757 hdrs
->type
== OFPT11_STATS_REPLY
)) {
758 struct ofp11_stats_msg
*osm
= buf
->header
;
760 osm
->type
= htons(hdrs
->stat
);
761 osm
->flags
= htons(0);
762 memset(osm
->pad
, 0, sizeof osm
->pad
);
764 if (hdrs
->stat
== OFPST_VENDOR
) {
765 struct ofp11_vendor_stats_msg
*ovsm
= buf
->header
;
767 ovsm
->vendor
= htonl(hdrs
->vendor
);
768 ovsm
->subtype
= htonl(hdrs
->subtype
);
773 /* Returns 'raw''s name.
775 * The name is the name used for 'raw' in the OpenFlow specification. For
776 * example, ofpraw_get_name(OFPRAW_OFPT10_FEATURES_REPLY) is
777 * "OFPT_FEATURES_REPLY".
779 * The caller must not modify or free the returned string. */
781 ofpraw_get_name(enum ofpraw raw
)
783 return raw_info_get(raw
)->name
;
786 /* Returns the stats reply that corresponds to 'raw' in the given OpenFlow
789 ofpraw_stats_request_to_reply(enum ofpraw raw
, uint8_t version
)
791 const struct raw_info
*info
= raw_info_get(raw
);
792 const struct raw_instance
*instance
= raw_instance_get(info
, version
);
793 enum ofpraw reply_raw
;
796 hdrs
= instance
->hdrs
;
797 switch ((enum ofp_version
)hdrs
.version
) {
799 ovs_assert(hdrs
.type
== OFPT10_STATS_REQUEST
);
800 hdrs
.type
= OFPT10_STATS_REPLY
;
807 ovs_assert(hdrs
.type
== OFPT11_STATS_REQUEST
);
808 hdrs
.type
= OFPT11_STATS_REPLY
;
814 ovs_assert(!ofpraw_from_ofphdrs(&reply_raw
, &hdrs
));
819 /* Determines the OFPTYPE_* type of the OpenFlow message at 'oh', which has
820 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
821 * data are readable at 'oh'.) On success, returns 0 and stores the type into
822 * '*typep'. On failure, returns an OFPERR_* error code and zeros '*typep'.
824 * This function checks that 'oh' is a valid length for its particular type of
825 * message, and returns an error if not. */
827 ofptype_decode(enum ofptype
*typep
, const struct ofp_header
*oh
)
832 error
= ofpraw_decode(&raw
, oh
);
833 *typep
= error
? 0 : ofptype_from_ofpraw(raw
);
837 /* Determines the OFPTYPE_* type of the OpenFlow message in 'msg', which starts
838 * at 'msg->data' and has length 'msg->size' bytes. On success,
839 * returns 0 and stores the type into '*typep'. On failure, returns an
840 * OFPERR_* error code and zeros '*typep'.
842 * This function checks that the message has a valid length for its particular
843 * type of message, and returns an error if not.
845 * In addition to setting '*typep', this function pulls off the OpenFlow header
846 * (including the stats headers, vendor header, and any subtype header) with
847 * ofpbuf_pull(). It also sets 'msg->header' to the start of the OpenFlow
848 * header and 'msg->msg' just beyond the headers (that is, to the final value
851 ofptype_pull(enum ofptype
*typep
, struct ofpbuf
*buf
)
856 error
= ofpraw_pull(&raw
, buf
);
857 *typep
= error
? 0 : ofptype_from_ofpraw(raw
);
861 /* Returns the OFPTYPE_* type that corresponds to 'raw'.
863 * (This is a one-way trip, because the mapping from ofpraw to ofptype is
866 ofptype_from_ofpraw(enum ofpraw raw
)
868 return raw_info_get(raw
)->type
;
872 ofptype_get_name(enum ofptype type
)
874 ovs_assert(type
< ARRAY_SIZE(type_names
));
875 return type_names
[type
];
878 /* Updates the 'length' field of the OpenFlow message in 'buf' to
881 ofpmsg_update_length(struct ofpbuf
*buf
)
883 struct ofp_header
*oh
= ofpbuf_at_assert(buf
, 0, sizeof *oh
);
884 oh
->length
= htons(buf
->size
);
887 /* Returns just past the OpenFlow header (including the stats headers, vendor
888 * header, and any subtype header) in 'oh'. */
890 ofpmsg_body(const struct ofp_header
*oh
)
894 ofphdrs_decode_assert(&hdrs
, oh
, ntohs(oh
->length
));
895 return (const uint8_t *) oh
+ ofphdrs_len(&hdrs
);
898 /* Return if 'oh' is a stat/multipart (OFPST) request message. */
900 ofpmsg_is_stat_request(const struct ofp_header
*oh
)
902 return ofp_is_stat_request(oh
->version
, oh
->type
);
905 /* Return if 'oh' is a stat/multipart (OFPST) reply message. */
907 ofpmsg_is_stat_reply(const struct ofp_header
*oh
)
909 return ofp_is_stat_reply(oh
->version
, oh
->type
);
912 /* Return if 'oh' is a stat/multipart (OFPST) request or reply message. */
914 ofpmsg_is_stat(const struct ofp_header
*oh
)
916 return ofp_is_stat(oh
->version
, oh
->type
);
919 static ovs_be16
*ofpmp_flags__(const struct ofp_header
*);
921 /* Initializes 'replies' as a new list of stats messages that reply to
922 * 'request', which must be a stats request message. Initially the list will
923 * consist of only a single reply part without any body. The caller should
924 * use calls to the other ofpmp_*() functions to add to the body and split the
925 * message into multiple parts, if necessary. */
927 ofpmp_init(struct ovs_list
*replies
, const struct ofp_header
*request
)
931 ovs_list_init(replies
);
933 msg
= ofpraw_alloc_stats_reply(request
, 1000);
934 ovs_list_push_back(replies
, &msg
->list_node
);
937 /* Prepares to append up to 'len' bytes to the series of statistics replies in
938 * 'replies', which should have been initialized with ofpmp_init(), if
939 * necessary adding a new reply to the list.
941 * Returns an ofpbuf with at least 'len' bytes of tailroom. The 'len' bytes
942 * have not actually been allocated, so the caller must do so with
943 * e.g. ofpbuf_put_uninit(). */
945 ofpmp_reserve(struct ovs_list
*replies
, size_t len
)
947 struct ofpbuf
*msg
= ofpbuf_from_list(ovs_list_back(replies
));
949 if (msg
->size
+ len
<= UINT16_MAX
) {
950 ofpbuf_prealloc_tailroom(msg
, len
);
953 unsigned int hdrs_len
;
957 ofphdrs_decode_assert(&hdrs
, msg
->data
, msg
->size
);
958 hdrs_len
= ofphdrs_len(&hdrs
);
960 next
= ofpbuf_new(MAX(1024, hdrs_len
+ len
));
961 ofpbuf_put(next
, msg
->data
, hdrs_len
);
962 next
->header
= next
->data
;
963 next
->msg
= ofpbuf_tail(next
);
964 ovs_list_push_back(replies
, &next
->list_node
);
966 *ofpmp_flags__(msg
->data
) |= htons(OFPSF_REPLY_MORE
);
972 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
973 * returns the first byte. */
975 ofpmp_append(struct ovs_list
*replies
, size_t len
)
977 return ofpbuf_put_uninit(ofpmp_reserve(replies
, len
), len
);
980 /* Sometimes, when composing stats replies, it's difficult to predict how long
981 * an individual reply chunk will be before actually encoding it into the reply
982 * buffer. This function allows easy handling of this case: just encode the
983 * reply, then use this function to break the message into two pieces if it
984 * exceeds the OpenFlow message limit.
986 * In detail, if the final stats message in 'replies' is too long for OpenFlow,
987 * this function breaks it into two separate stats replies, the first one with
988 * the first 'start_ofs' bytes, the second one containing the bytes from that
991 ofpmp_postappend(struct ovs_list
*replies
, size_t start_ofs
)
993 struct ofpbuf
*msg
= ofpbuf_from_list(ovs_list_back(replies
));
995 ovs_assert(start_ofs
<= UINT16_MAX
);
996 if (msg
->size
> UINT16_MAX
) {
997 size_t len
= msg
->size
- start_ofs
;
998 memcpy(ofpmp_append(replies
, len
),
999 (const uint8_t *) msg
->data
+ start_ofs
, len
);
1000 msg
->size
= start_ofs
;
1004 /* Returns the OpenFlow version of the replies being constructed in 'replies',
1005 * which should have been initialized by ofpmp_init(). */
1007 ofpmp_version(struct ovs_list
*replies
)
1009 struct ofpbuf
*msg
= ofpbuf_from_list(ovs_list_back(replies
));
1010 const struct ofp_header
*oh
= msg
->data
;
1015 /* Determines the OFPRAW_* type of the OpenFlow messages in 'replies', which
1016 * should have been initialized by ofpmp_init(). */
1018 ofpmp_decode_raw(struct ovs_list
*replies
)
1020 struct ofpbuf
*msg
= ofpbuf_from_list(ovs_list_back(replies
));
1022 ovs_assert(!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
;
1037 return &((struct ofp11_stats_msg
*) oh
)->flags
;
1043 /* Returns the OFPSF_* flags found in the OpenFlow stats header of 'oh', which
1044 * must be an OpenFlow stats request or reply.
1046 * (OFPSF_REPLY_MORE is the only defined flag.) */
1048 ofpmp_flags(const struct ofp_header
*oh
)
1050 return ntohs(*ofpmp_flags__(oh
));
1053 /* Returns true if the OFPSF_REPLY_MORE flag is set in the OpenFlow stats
1054 * header of 'oh', which must be an OpenFlow stats request or reply, false if
1057 ofpmp_more(const struct ofp_header
*oh
)
1059 return (ofpmp_flags(oh
) & OFPSF_REPLY_MORE
) != 0;
1062 /* Multipart request assembler. */
1064 struct ofpmp_partial
{
1065 struct hmap_node hmap_node
; /* In struct ofpmp_assembler's 'msgs'. */
1068 long long int timeout
;
1069 struct ovs_list msgs
;
1075 hash_xid(ovs_be32 xid
)
1077 return hash_int((OVS_FORCE
uint32_t) xid
, 0);
1080 static struct ofpmp_partial
*
1081 ofpmp_assembler_find(struct hmap
*assembler
, ovs_be32 xid
)
1083 if (hmap_is_empty(assembler
)) {
1088 struct ofpmp_partial
*p
;
1089 HMAP_FOR_EACH_IN_BUCKET (p
, hmap_node
, hash_xid(xid
), assembler
) {
1090 if (p
->xid
== xid
) {
1098 ofpmp_partial_destroy(struct hmap
*assembler
, struct ofpmp_partial
*p
)
1101 hmap_remove(assembler
, &p
->hmap_node
);
1102 ofpbuf_list_delete(&p
->msgs
);
1107 static struct ofpbuf
*
1108 ofpmp_partial_error(struct hmap
*assembler
, struct ofpmp_partial
*p
,
1111 const struct ofpbuf
*head
= ofpbuf_from_list(ovs_list_back(&p
->msgs
));
1112 const struct ofp_header
*oh
= head
->data
;
1113 struct ofpbuf
*reply
= ofperr_encode_reply(error
, oh
);
1115 ofpmp_partial_destroy(assembler
, p
);
1120 /* Clears out and frees any messages currently being reassembled. Afterward,
1121 * the caller may destroy the hmap, with hmap_destroy(), without risk of
1124 ofpmp_assembler_clear(struct hmap
*assembler
)
1126 struct ofpmp_partial
*p
, *next
;
1127 HMAP_FOR_EACH_SAFE (p
, next
, hmap_node
, assembler
) {
1128 ofpmp_partial_destroy(assembler
, p
);
1132 /* Does periodic maintenance on 'assembler'. If any partially assembled
1133 * requests have timed out, returns an appropriate error message for the caller
1134 * to send to the controller.
1136 * 'now' should be the current time as returned by time_msec(). */
1137 struct ofpbuf
* OVS_WARN_UNUSED_RESULT
1138 ofpmp_assembler_run(struct hmap
*assembler
, long long int now
)
1140 struct ofpmp_partial
*p
;
1141 HMAP_FOR_EACH (p
, hmap_node
, assembler
) {
1142 if (now
>= p
->timeout
) {
1143 return ofpmp_partial_error(
1144 assembler
, p
, OFPERR_OFPBRC_MULTIPART_REQUEST_TIMEOUT
);
1150 /* Returns the time at which the next partially assembled request times out.
1151 * The caller should pass this time to poll_timer_wait_until(). */
1153 ofpmp_assembler_wait(struct hmap
*assembler
)
1155 long long int timeout
= LLONG_MAX
;
1157 struct ofpmp_partial
*p
;
1158 HMAP_FOR_EACH (p
, hmap_node
, assembler
) {
1159 timeout
= MIN(timeout
, p
->timeout
);
1165 /* Submits 'msg' to 'assembler' for reassembly.
1167 * If 'msg' was accepted, returns 0 and initializes 'out' either to an empty
1168 * list (if 'msg' is being held for reassembly) or to a list of one or more
1169 * reassembled messages. The reassembler takes ownership of 'msg'; the caller
1170 * takes ownership of the messages in 'out'.
1172 * If 'msg' was rejected, returns an OpenFlow error that the caller should
1173 * reply to the caller and initializes 'out' as empty. The caller retains
1174 * ownership of 'msg'.
1176 * 'now' should be the current time as returned by time_msec(). */
1178 ofpmp_assembler_execute(struct hmap
*assembler
, struct ofpbuf
*msg
,
1179 struct ovs_list
*out
, long long int now
)
1183 /* If the message is not a multipart request, pass it along without further
1186 * We could also do this kind of early-out for multipart requests that have
1187 * only a single piece, or for pre-OF1.3 multipart requests (since only
1188 * OF1.3 introduced multipart requests with more than one piece), but we
1189 * don't because this allows us to assure code that runs after us that
1190 * invariants checked below on correct message lengths are always
1191 * satisfied, even if there's only a single piece. */
1192 struct ofp_header
*oh
= msg
->data
;
1193 if (!ofpmsg_is_stat_request(oh
)) {
1194 ovs_list_push_back(out
, &msg
->list_node
);
1198 /* Decode the multipart request. */
1199 struct ofphdrs hdrs
;
1200 enum ofperr error
= ofphdrs_decode(&hdrs
, msg
->data
, msg
->size
);
1206 error
= ofpraw_from_ofphdrs(&raw
, &hdrs
);
1211 /* If the message has a nonempty body, check that it is a valid length.
1213 * The OpenFlow spec says that pieces with empty bodies are allowed
1214 * anywhere in a multipart sequence, so for now we allow such messages even
1215 * if the overall multipart request requires a body. */
1216 const struct raw_info
*info
= raw_info_get(raw
);
1217 const struct raw_instance
*instance
= raw_instance_get(info
, hdrs
.version
);
1218 unsigned int min_len
= ofphdrs_len(&hdrs
);
1219 bool has_body
= msg
->size
> min_len
;
1221 error
= ofpraw_check_length(info
, instance
, msg
->size
);
1227 /* Find or create an ofpmp_partial record. */
1228 struct ofpmp_partial
*p
= ofpmp_assembler_find(assembler
, oh
->xid
);
1230 p
= xzalloc(sizeof *p
);
1231 hmap_insert(assembler
, &p
->hmap_node
, hash_xid(oh
->xid
));
1233 ovs_list_init(&p
->msgs
);
1236 p
->timeout
= now
+ 1000;
1238 /* Check that the type is the same as any previous messages in this
1240 if (p
->raw
!= raw
) {
1241 ofpmp_partial_destroy(assembler
, p
);
1242 return OFPERR_OFPBRC_BAD_STAT
;
1245 /* Limit the size of a multipart sequence.
1247 * (Table features requests can actually be over 1 MB.) */
1248 p
->size
+= msg
->size
;
1249 if (p
->size
> 4 * 1024 * 1024) {
1250 ofpmp_partial_destroy(assembler
, p
);
1251 return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW
;
1254 /* If a multipart request type requires a body, ensure that at least one of
1255 * the pieces in a multipart request has one. */
1256 bool more
= oh
->version
>= OFP13_VERSION
&& ofpmp_more(oh
);
1260 if (!more
&& !p
->has_body
&& info
->min_body
) {
1261 ofpmp_partial_destroy(assembler
, p
);
1262 return OFPERR_OFPBRC_BAD_LEN
;
1265 /* Append the part to the list.
1267 * If there are more pieces to come, we're done for now. */
1268 ovs_list_push_back(&p
->msgs
, &msg
->list_node
);
1273 /* This multipart request is complete. Move the messages from 'p' to 'out'
1274 * and discard 'p'. */
1275 ovs_list_move(out
, &p
->msgs
);
1276 ovs_list_init(&p
->msgs
);
1277 ofpmp_partial_destroy(assembler
, p
);
1279 /* Delete pieces with empty bodies from 'out' (but leave at least one
1282 * Most types of multipart requests have fixed-size bodies. For example,
1283 * OFPMP_PORT_DESCRIPTION has an 8-byte body. Thus, it doesn't really make
1284 * sense for a controller to use multiple pieces for these messages, and
1285 * it's simpler to implement OVS as if they weren't really multipart.
1287 * However, the OpenFlow spec says that messages with empty bodies are
1288 * allowed anywhere in a multipart sequence, so in theory a controller
1289 * could send an OFPMP_PORT_DESCRIPTION with an 8-byte body bracketed
1290 * on either side by parts with 0-byte bodies. We remove the 0-byte
1291 * ones here to simplify processing later.
1293 struct ofpbuf
*b
, *next
;
1294 LIST_FOR_EACH_SAFE (b
, next
, list_node
, out
) {
1295 if (b
->size
<= min_len
&& !ovs_list_is_short(out
)) {
1296 ovs_list_remove(&b
->list_node
);
1303 static void ofpmsgs_init(void);
1305 static const struct raw_info
*
1306 raw_info_get(enum ofpraw raw
)
1310 ovs_assert(raw
< ARRAY_SIZE(raw_infos
));
1311 return &raw_infos
[raw
];
1314 static struct raw_instance
*
1315 raw_instance_get(const struct raw_info
*info
, uint8_t version
)
1317 ovs_assert(version
>= info
->min_version
&& version
<= info
->max_version
);
1318 return &info
->instances
[version
- info
->min_version
];
1322 ofpraw_from_ofphdrs(enum ofpraw
*raw
, const struct ofphdrs
*hdrs
)
1324 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
1326 struct raw_instance
*raw_hdrs
;
1331 hash
= ofphdrs_hash(hdrs
);
1332 HMAP_FOR_EACH_WITH_HASH (raw_hdrs
, hmap_node
, hash
, &raw_instance_map
) {
1333 if (ofphdrs_equal(hdrs
, &raw_hdrs
->hdrs
)) {
1334 *raw
= raw_hdrs
->raw
;
1339 if (!VLOG_DROP_WARN(&rl
)) {
1343 ds_put_format(&s
, "version %"PRIu8
", type %"PRIu8
,
1344 hdrs
->version
, hdrs
->type
);
1345 if (ofphdrs_is_stat(hdrs
)) {
1346 ds_put_format(&s
, ", stat %"PRIu16
, hdrs
->stat
);
1349 ds_put_format(&s
, ", vendor 0x%"PRIx32
", subtype %"PRIu32
,
1350 hdrs
->vendor
, hdrs
->subtype
);
1352 VLOG_WARN("unknown OpenFlow message (%s)", ds_cstr(&s
));
1356 return (hdrs
->vendor
? OFPERR_OFPBRC_BAD_SUBTYPE
1357 : ofphdrs_is_stat(hdrs
) ? OFPERR_OFPBRC_BAD_STAT
1358 : OFPERR_OFPBRC_BAD_TYPE
);
1364 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
1365 const struct raw_info
*info
;
1367 if (!ovsthread_once_start(&once
)) {
1371 hmap_init(&raw_instance_map
);
1372 for (info
= raw_infos
; info
< &raw_infos
[ARRAY_SIZE(raw_infos
)]; info
++)
1374 int n_instances
= info
->max_version
- info
->min_version
+ 1;
1375 struct raw_instance
*inst
;
1377 for (inst
= info
->instances
;
1378 inst
< &info
->instances
[n_instances
];
1380 inst
->hdrs_len
= ofphdrs_len(&inst
->hdrs
);
1381 hmap_insert(&raw_instance_map
, &inst
->hmap_node
,
1382 ofphdrs_hash(&inst
->hdrs
));
1386 ovsthread_once_done(&once
);