2 * Copyright (c) 2012 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.
20 #include "byte-order.h"
21 #include "dynamic-string.h"
25 #include "openflow/nicira-ext.h"
26 #include "openflow/openflow.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 uint32_t next_xid
= 1;
114 return htonl(next_xid
++);
118 ofphdrs_hash(const struct ofphdrs
*hdrs
)
120 BUILD_ASSERT_DECL(sizeof *hdrs
== 12);
121 return hash_words((const uint32_t *) hdrs
, 3, 0);
125 ofphdrs_equal(const struct ofphdrs
*a
, const struct ofphdrs
*b
)
127 return !memcmp(a
, b
, sizeof *a
);
131 log_bad_vendor(uint32_t vendor
)
133 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
135 VLOG_WARN_RL(&rl
, "OpenFlow message has unknown vendor %#"PRIx32
, vendor
);
139 ofphdrs_decode(struct ofphdrs
*hdrs
,
140 const struct ofp_header
*oh
, size_t length
)
142 memset(hdrs
, 0, sizeof *hdrs
);
143 if (length
< sizeof *oh
) {
144 return OFPERR_OFPBRC_BAD_LEN
;
147 /* Get base message version and type (OFPT_*). */
148 hdrs
->version
= oh
->version
;
149 hdrs
->type
= oh
->type
;
151 if (hdrs
->type
== OFPT_VENDOR
) {
153 const struct ofp_vendor_header
*ovh
;
155 if (length
< sizeof *ovh
) {
156 return OFPERR_OFPBRC_BAD_LEN
;
159 ovh
= (const struct ofp_vendor_header
*) oh
;
160 hdrs
->vendor
= ntohl(ovh
->vendor
);
161 if (hdrs
->vendor
== NX_VENDOR_ID
) {
162 /* Get Nicira message subtype (NXT_*). */
163 const struct nicira_header
*nh
;
165 if (length
< sizeof *nh
) {
166 return OFPERR_OFPBRC_BAD_LEN
;
168 nh
= (const struct nicira_header
*) oh
;
169 hdrs
->subtype
= ntohl(nh
->subtype
);
171 log_bad_vendor(hdrs
->vendor
);
172 return OFPERR_OFPBRC_BAD_VENDOR
;
174 } else if (hdrs
->version
== OFP10_VERSION
175 && (hdrs
->type
== OFPT10_STATS_REQUEST
||
176 hdrs
->type
== OFPT10_STATS_REPLY
)) {
177 const struct ofp_stats_msg
*osm
;
179 /* Get statistic type (OFPST_*). */
180 if (length
< sizeof *osm
) {
181 return OFPERR_OFPBRC_BAD_LEN
;
183 osm
= (const struct ofp_stats_msg
*) oh
;
184 hdrs
->stat
= ntohs(osm
->type
);
186 if (hdrs
->stat
== OFPST_VENDOR
) {
188 const struct ofp10_vendor_stats_msg
*ovsm
;
190 if (length
< sizeof *ovsm
) {
191 return OFPERR_OFPBRC_BAD_LEN
;
194 ovsm
= (const struct ofp10_vendor_stats_msg
*) oh
;
195 hdrs
->vendor
= ntohl(ovsm
->vendor
);
196 if (hdrs
->vendor
== NX_VENDOR_ID
) {
197 /* Get Nicira statistic type (NXST_*). */
198 const struct nicira10_stats_msg
*nsm
;
200 if (length
< sizeof *nsm
) {
201 return OFPERR_OFPBRC_BAD_LEN
;
203 nsm
= (const struct nicira10_stats_msg
*) oh
;
204 hdrs
->subtype
= ntohl(nsm
->subtype
);
206 log_bad_vendor(hdrs
->vendor
);
207 return OFPERR_OFPBRC_BAD_VENDOR
;
210 } else if (hdrs
->version
!= OFP10_VERSION
211 && (hdrs
->type
== OFPT11_STATS_REQUEST
||
212 hdrs
->type
== OFPT11_STATS_REPLY
)) {
213 const struct ofp11_stats_msg
*osm
;
215 /* Get statistic type (OFPST_*). */
216 if (length
< sizeof *osm
) {
217 return OFPERR_OFPBRC_BAD_LEN
;
219 osm
= (const struct ofp11_stats_msg
*) oh
;
220 hdrs
->stat
= ntohs(osm
->type
);
222 if (hdrs
->stat
== OFPST_VENDOR
) {
224 const struct ofp11_vendor_stats_msg
*ovsm
;
226 if (length
< sizeof *ovsm
) {
227 return OFPERR_OFPBRC_BAD_LEN
;
230 ovsm
= (const struct ofp11_vendor_stats_msg
*) oh
;
231 hdrs
->vendor
= ntohl(ovsm
->vendor
);
232 if (hdrs
->vendor
== NX_VENDOR_ID
) {
233 /* Get Nicira statistic type (NXST_*). */
234 const struct nicira11_stats_msg
*nsm
;
236 if (length
< sizeof *nsm
) {
237 return OFPERR_OFPBRC_BAD_LEN
;
239 nsm
= (const struct nicira11_stats_msg
*) oh
;
240 hdrs
->subtype
= ntohl(nsm
->subtype
);
242 log_bad_vendor(hdrs
->vendor
);
243 return OFPERR_OFPBRC_BAD_VENDOR
;
252 ofphdrs_decode_assert(struct ofphdrs
*hdrs
,
253 const struct ofp_header
*oh
, size_t length
)
255 enum ofperr error
= ofphdrs_decode(hdrs
, oh
, length
);
260 ofphdrs_is_stat(const struct ofphdrs
*hdrs
)
262 return (hdrs
->version
== OFP10_VERSION
263 ? (hdrs
->type
== OFPT10_STATS_REQUEST
||
264 hdrs
->type
== OFPT10_STATS_REPLY
)
265 : (hdrs
->type
== OFPT11_STATS_REQUEST
||
266 hdrs
->type
== OFPT11_STATS_REPLY
));
270 ofphdrs_len(const struct ofphdrs
*hdrs
)
272 if (hdrs
->type
== OFPT_VENDOR
) {
273 return sizeof(struct nicira_header
);
276 if (hdrs
->version
== OFP10_VERSION
) {
277 if (hdrs
->type
== OFPT10_STATS_REQUEST
||
278 hdrs
->type
== OFPT10_STATS_REPLY
) {
279 return (hdrs
->stat
== OFPST_VENDOR
280 ? sizeof(struct nicira10_stats_msg
)
281 : sizeof(struct ofp_stats_msg
));
284 if (hdrs
->type
== OFPT11_STATS_REQUEST
||
285 hdrs
->type
== OFPT11_STATS_REPLY
) {
286 return (hdrs
->stat
== OFPST_VENDOR
287 ? sizeof(struct nicira11_stats_msg
)
288 : sizeof(struct ofp11_stats_msg
));
292 return sizeof(struct ofp_header
);
295 /* Determines the OFPRAW_* type of the OpenFlow message at 'oh', which has
296 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
297 * data are readable at 'oh'.) On success, returns 0 and stores the type into
298 * '*raw'. On failure, returns an OFPERR_* error code and zeros '*raw'.
300 * This function checks that 'oh' is a valid length for its particular type of
301 * message, and returns an error if not. */
303 ofpraw_decode(enum ofpraw
*raw
, const struct ofp_header
*oh
)
307 ofpbuf_use_const(&msg
, oh
, ntohs(oh
->length
));
308 return ofpraw_pull(raw
, &msg
);
311 /* Determines the OFPRAW_* type of the OpenFlow message in 'msg', which starts
312 * at 'msg->data' and has length 'msg->size' bytes. On success, returns 0 and
313 * stores the type into '*rawp'. On failure, returns an OFPERR_* error code
316 * This function checks that the message has a valid length for its particular
317 * type of message, and returns an error if not.
319 * In addition to setting '*rawp', this function pulls off the OpenFlow header
320 * (including the stats headers, vendor header, and any subtype header) with
321 * ofpbuf_pull(). It also sets 'msg->l2' to the start of the OpenFlow header
322 * and 'msg->l3' just beyond the headers (that is, to the final value of
325 ofpraw_pull(enum ofpraw
*rawp
, struct ofpbuf
*msg
)
327 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
329 const struct raw_instance
*instance
;
330 const struct raw_info
*info
;
333 unsigned int min_len
;
339 /* Set default outputs. */
340 msg
->l2
= msg
->l3
= msg
->data
;
344 error
= ofphdrs_decode(&hdrs
, msg
->data
, len
);
349 error
= ofpraw_from_ofphdrs(&raw
, &hdrs
);
354 info
= raw_info_get(raw
);
355 instance
= raw_instance_get(info
, hdrs
.version
);
356 msg
->l2
= ofpbuf_pull(msg
, instance
->hdrs_len
);
359 min_len
= instance
->hdrs_len
+ info
->min_body
;
360 switch (info
->extra_multiple
) {
362 if (len
!= min_len
) {
363 VLOG_WARN_RL(&rl
, "received %s with incorrect length %u (expected "
364 "length %u)", info
->name
, len
, min_len
);
365 return OFPERR_OFPBRC_BAD_LEN
;
371 VLOG_WARN_RL(&rl
, "received %s with incorrect length %u (expected "
372 "length at least %u bytes)",
373 info
->name
, len
, min_len
);
374 return OFPERR_OFPBRC_BAD_LEN
;
379 if (len
< min_len
|| (len
- min_len
) % info
->extra_multiple
) {
380 VLOG_WARN_RL(&rl
, "received %s with incorrect length %u (must be "
381 "exactly %u bytes or longer by an integer multiple "
383 info
->name
, len
, min_len
, info
->extra_multiple
);
384 return OFPERR_OFPBRC_BAD_LEN
;
393 /* Does the same job as ofpraw_pull(), except that it assert-fails if
394 * ofpbuf_pull() would have reported an error. Thus, it's able to use the
395 * return value for the OFPRAW_* message type instead of an error code.
397 * (It only makes sense to use this function if you previously called
398 * ofpbuf_decode() on the message and thus know that it's OK.) */
400 ofpraw_pull_assert(struct ofpbuf
*msg
)
405 error
= ofpraw_pull(&raw
, msg
);
410 /* Determines the OFPRAW_* type of the OpenFlow message that starts at 'oh' and
411 * has length 'length' bytes. On success, returns 0 and stores the type into
412 * '*rawp'. On failure, returns an OFPERR_* error code and zeros '*rawp'.
414 * Unlike other functions for decoding message types, this one is not picky
415 * about message length. For example, it will successfully decode a message
416 * whose body is shorter than the minimum length for a message of its type.
417 * Thus, this is the correct function to use for decoding the type of a message
418 * that might have been truncated, such as the payload of an OpenFlow error
419 * message (which is allowed to be truncated to 64 bytes). */
421 ofpraw_decode_partial(enum ofpraw
*raw
,
422 const struct ofp_header
*oh
, size_t length
)
427 error
= ofphdrs_decode(&hdrs
, oh
, length
);
429 error
= ofpraw_from_ofphdrs(raw
, &hdrs
);
438 /* Encoding messages using OFPRAW_* values. */
440 static void ofpraw_put__(enum ofpraw
, uint8_t version
, ovs_be32 xid
,
441 size_t extra_tailroom
, struct ofpbuf
*);
443 /* Allocates and returns a new ofpbuf that contains an OpenFlow header for
444 * 'raw' with OpenFlow version 'version' and a fresh OpenFlow transaction ID.
445 * The ofpbuf has enough tailroom for the minimum body length of 'raw', plus
446 * 'extra_tailroom' additional bytes.
448 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
449 * must specify a valid (raw, version) pair.
451 * In the returned ofpbuf, 'l2' points to the beginning of the OpenFlow header
452 * and 'l3' points just after it, to where the message's body will start. The
453 * caller must actually allocate the body into the space reserved for it,
454 * e.g. with ofpbuf_put_uninit().
456 * The caller owns the returned ofpbuf and must free it when it is no longer
457 * needed, e.g. with ofpbuf_delete(). */
459 ofpraw_alloc(enum ofpraw raw
, uint8_t version
, size_t extra_tailroom
)
461 return ofpraw_alloc_xid(raw
, version
, alloc_xid(), extra_tailroom
);
464 /* Same as ofpraw_alloc() but the caller provides the transaction ID. */
466 ofpraw_alloc_xid(enum ofpraw raw
, uint8_t version
, ovs_be32 xid
,
467 size_t extra_tailroom
)
469 struct ofpbuf
*buf
= ofpbuf_new(0);
470 ofpraw_put__(raw
, version
, xid
, extra_tailroom
, buf
);
474 /* Same as ofpraw_alloc(), but obtains the OpenFlow version and transaction ID
475 * from 'request->version' and 'request->xid', respectively.
477 * Even though the version comes from 'request->version', the caller must still
478 * know what it is doing, by specifying a valid pairing of 'raw' and
479 * 'request->version', just like ofpraw_alloc(). */
481 ofpraw_alloc_reply(enum ofpraw raw
, const struct ofp_header
*request
,
482 size_t extra_tailroom
)
484 return ofpraw_alloc_xid(raw
, request
->version
, request
->xid
,
488 /* Allocates and returns a new ofpbuf that contains an OpenFlow header that is
489 * a stats reply to the stats request in 'request', using the same OpenFlow
490 * version and transaction ID as 'request'. The ofpbuf has enough tailroom for
491 * the stats reply's minimum body length, plus 'extra_tailroom' additional
494 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
495 * value. Every stats request has a corresponding reply, so the (raw, version)
496 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
498 * In the returned ofpbuf, 'l2' points to the beginning of the OpenFlow header
499 * and 'l3' points just after it, to where the message's body will start. The
500 * caller must actually allocate the body into the space reserved for it,
501 * e.g. with ofpbuf_put_uninit().
503 * The caller owns the returned ofpbuf and must free it when it is no longer
504 * needed, e.g. with ofpbuf_delete(). */
506 ofpraw_alloc_stats_reply(const struct ofp_header
*request
,
507 size_t extra_tailroom
)
509 enum ofpraw request_raw
;
510 enum ofpraw reply_raw
;
513 error
= ofpraw_decode_partial(&request_raw
, request
,
514 ntohs(request
->length
));
517 reply_raw
= ofpraw_stats_request_to_reply(request_raw
, request
->version
);
520 return ofpraw_alloc_reply(reply_raw
, request
, extra_tailroom
);
523 /* Appends to 'buf' an OpenFlow header for 'raw' with OpenFlow version
524 * 'version' and a fresh OpenFlow transaction ID. Preallocates enough tailroom
525 * in 'buf' for the minimum body length of 'raw', plus 'extra_tailroom'
528 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
529 * must specify a valid (raw, version) pair.
531 * Upon return, 'buf->l2' points to the beginning of the OpenFlow header and
532 * 'buf->l3' points just after it, to where the message's body will start. The
533 * caller must actually allocating the body into the space reserved for it,
534 * e.g. with ofpbuf_put_uninit(). */
536 ofpraw_put(enum ofpraw raw
, uint8_t version
, struct ofpbuf
*buf
)
538 ofpraw_put__(raw
, version
, alloc_xid(), 0, buf
);
541 /* Same as ofpraw_put() but the caller provides the transaction ID. */
543 ofpraw_put_xid(enum ofpraw raw
, uint8_t version
, ovs_be32 xid
,
546 ofpraw_put__(raw
, version
, xid
, 0, buf
);
549 /* Same as ofpraw_put(), but obtains the OpenFlow version and transaction ID
550 * from 'request->version' and 'request->xid', respectively.
552 * Even though the version comes from 'request->version', the caller must still
553 * know what it is doing, by specifying a valid pairing of 'raw' and
554 * 'request->version', just like ofpraw_put(). */
556 ofpraw_put_reply(enum ofpraw raw
, const struct ofp_header
*request
,
559 ofpraw_put__(raw
, request
->version
, request
->xid
, 0, buf
);
562 /* Appends to 'buf' an OpenFlow header that is a stats reply to the stats
563 * request in 'request', using the same OpenFlow version and transaction ID as
564 * 'request'. Preallocate enough tailroom in 'buf for the stats reply's
565 * minimum body length, plus 'extra_tailroom' additional bytes.
567 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
568 * value. Every stats request has a corresponding reply, so the (raw, version)
569 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
571 * In the returned ofpbuf, 'l2' points to the beginning of the OpenFlow header
572 * and 'l3' points just after it, to where the message's body will start. The
573 * caller must actually allocate the body into the space reserved for it,
574 * 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_put_stats_reply(const struct ofp_header
*request
, struct ofpbuf
*buf
)
584 error
= ofpraw_decode_partial(&raw
, request
, ntohs(request
->length
));
587 raw
= ofpraw_stats_request_to_reply(raw
, request
->version
);
590 ofpraw_put__(raw
, request
->version
, request
->xid
, 0, buf
);
594 ofpraw_put__(enum ofpraw raw
, uint8_t version
, ovs_be32 xid
,
595 size_t extra_tailroom
, struct ofpbuf
*buf
)
597 const struct raw_info
*info
= raw_info_get(raw
);
598 const struct raw_instance
*instance
= raw_instance_get(info
, version
);
599 const struct ofphdrs
*hdrs
= &instance
->hdrs
;
600 struct ofp_header
*oh
;
602 ofpbuf_prealloc_tailroom(buf
, (instance
->hdrs_len
+ info
->min_body
604 buf
->l2
= ofpbuf_put_uninit(buf
, instance
->hdrs_len
);
605 buf
->l3
= ofpbuf_tail(buf
);
608 oh
->version
= version
;
609 oh
->type
= hdrs
->type
;
610 oh
->length
= htons(buf
->size
);
613 if (hdrs
->type
== OFPT_VENDOR
) {
614 struct nicira_header
*nh
= buf
->l2
;
616 assert(hdrs
->vendor
== NX_VENDOR_ID
);
617 nh
->vendor
= htonl(hdrs
->vendor
);
618 nh
->subtype
= htonl(hdrs
->subtype
);
619 } else if (version
== OFP10_VERSION
620 && (hdrs
->type
== OFPT10_STATS_REQUEST
||
621 hdrs
->type
== OFPT10_STATS_REPLY
)) {
622 struct ofp_stats_msg
*osm
= buf
->l2
;
624 osm
->type
= htons(hdrs
->stat
);
625 osm
->flags
= htons(0);
627 if (hdrs
->stat
== OFPST_VENDOR
) {
628 struct ofp10_vendor_stats_msg
*ovsm
= buf
->l2
;
630 ovsm
->vendor
= htonl(hdrs
->vendor
);
631 if (hdrs
->vendor
== NX_VENDOR_ID
) {
632 struct nicira10_stats_msg
*nsm
= buf
->l2
;
634 nsm
->subtype
= htonl(hdrs
->subtype
);
635 memset(nsm
->pad
, 0, sizeof nsm
->pad
);
640 } else if (version
!= OFP10_VERSION
641 && (hdrs
->type
== OFPT11_STATS_REQUEST
||
642 hdrs
->type
== OFPT11_STATS_REPLY
)) {
643 struct ofp11_stats_msg
*osm
= buf
->l2
;
645 osm
->type
= htons(hdrs
->stat
);
646 osm
->flags
= htons(0);
647 memset(osm
->pad
, 0, sizeof osm
->pad
);
649 if (hdrs
->stat
== OFPST_VENDOR
) {
650 struct ofp11_vendor_stats_msg
*ovsm
= buf
->l2
;
652 ovsm
->vendor
= htonl(hdrs
->vendor
);
653 if (hdrs
->vendor
== NX_VENDOR_ID
) {
654 struct nicira11_stats_msg
*nsm
= buf
->l2
;
656 nsm
->subtype
= htonl(hdrs
->subtype
);
664 /* Returns 'raw''s name.
666 * The name is the name used for 'raw' in the OpenFlow specification. For
667 * example, ofpraw_get_name(OFPRAW_OFPT10_FEATURES_REPLY) is
668 * "OFPT_FEATURES_REPLY".
670 * The caller must not modify or free the returned string. */
672 ofpraw_get_name(enum ofpraw raw
)
674 return raw_info_get(raw
)->name
;
677 /* Returns the stats reply that corresponds to 'raw' in the given OpenFlow
680 ofpraw_stats_request_to_reply(enum ofpraw raw
, uint8_t version
)
682 const struct raw_info
*info
= raw_info_get(raw
);
683 const struct raw_instance
*instance
= raw_instance_get(info
, version
);
684 enum ofpraw reply_raw
;
688 hdrs
= instance
->hdrs
;
689 if (hdrs
.version
== OFP10_VERSION
) {
690 assert(hdrs
.type
== OFPT10_STATS_REQUEST
);
691 hdrs
.type
= OFPT10_STATS_REPLY
;
693 assert(hdrs
.type
== OFPT11_STATS_REQUEST
);
694 hdrs
.type
= OFPT11_STATS_REPLY
;
697 error
= ofpraw_from_ofphdrs(&reply_raw
, &hdrs
);
703 /* Determines the OFPTYPE_* type of the OpenFlow message at 'oh', which has
704 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
705 * data are readable at 'oh'.) On success, returns 0 and stores the type into
706 * '*typep'. On failure, returns an OFPERR_* error code and zeros '*typep'.
708 * This function checks that 'oh' is a valid length for its particular type of
709 * message, and returns an error if not. */
711 ofptype_decode(enum ofptype
*typep
, const struct ofp_header
*oh
)
716 error
= ofpraw_decode(&raw
, oh
);
717 *typep
= error
? 0 : ofptype_from_ofpraw(raw
);
721 /* Determines the OFPTYPE_* type of the OpenFlow message in 'msg', which starts
722 * at 'msg->data' and has length 'msg->size' bytes. On success, returns 0 and
723 * stores the type into '*typep'. On failure, returns an OFPERR_* error code
724 * and zeros '*typep'.
726 * This function checks that the message has a valid length for its particular
727 * type of message, and returns an error if not.
729 * In addition to setting '*typep', this function pulls off the OpenFlow header
730 * (including the stats headers, vendor header, and any subtype header) with
731 * ofpbuf_pull(). It also sets 'msg->l2' to the start of the OpenFlow header
732 * and 'msg->l3' just beyond the headers (that is, to the final value of
735 ofptype_pull(enum ofptype
*typep
, struct ofpbuf
*buf
)
740 error
= ofpraw_pull(&raw
, buf
);
741 *typep
= error
? 0 : ofptype_from_ofpraw(raw
);
745 /* Returns the OFPTYPE_* type that corresponds to 'raw'.
747 * (This is a one-way trip, because the mapping from ofpraw to ofptype is
750 ofptype_from_ofpraw(enum ofpraw raw
)
752 return raw_info_get(raw
)->type
;
755 /* Updates the 'length' field of the OpenFlow message in 'buf' to
758 ofpmsg_update_length(struct ofpbuf
*buf
)
760 struct ofp_header
*oh
= ofpbuf_at_assert(buf
, 0, sizeof *oh
);
761 oh
->length
= htons(buf
->size
);
764 /* Returns just past the Openflow header (including the stats headers, vendor
765 * header, and any subtype header) in 'oh'. */
767 ofpmsg_body(const struct ofp_header
*oh
)
771 ofphdrs_decode_assert(&hdrs
, oh
, ntohs(oh
->length
));
772 return (const uint8_t *) oh
+ ofphdrs_len(&hdrs
);
775 /* Initializes 'replies' as a new list of stats messages that reply to
776 * 'request', which must be a stats request message. Initially the list will
777 * consist of only a single reply part without any body. The caller should
778 * use calls to the other ofpmp_*() functions to add to the body and split the
779 * message into multiple parts, if necessary. */
781 ofpmp_init(struct list
*replies
, const struct ofp_header
*request
)
787 msg
= ofpraw_alloc_stats_reply(request
, 1000);
788 list_push_back(replies
, &msg
->list_node
);
791 /* Prepares to append up to 'len' bytes to the series of statistics replies in
792 * 'replies', which should have been initialized with ofpmp_init(), if
793 * necessary adding a new reply to the list.
795 * Returns an ofpbuf with at least 'len' bytes of tailroom. The 'len' bytes
796 * have not actually been allocated, so the caller must do so with
797 * e.g. ofpbuf_put_uninit(). */
799 ofpmp_reserve(struct list
*replies
, size_t len
)
801 struct ofpbuf
*msg
= ofpbuf_from_list(list_back(replies
));
803 if (msg
->size
+ len
<= UINT16_MAX
) {
804 ofpbuf_prealloc_tailroom(msg
, len
);
807 unsigned int hdrs_len
;
811 ofphdrs_decode_assert(&hdrs
, msg
->data
, msg
->size
);
812 hdrs_len
= ofphdrs_len(&hdrs
);
814 next
= ofpbuf_new(MAX(1024, hdrs_len
+ len
));
815 ofpbuf_put(next
, msg
->data
, hdrs_len
);
816 list_push_back(replies
, &next
->list_node
);
822 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
823 * returns the first byte. */
825 ofpmp_append(struct list
*replies
, size_t len
)
827 return ofpbuf_put_uninit(ofpmp_reserve(replies
, len
), len
);
830 /* Sometimes, when composing stats replies, it's difficult to predict how long
831 * an individual reply chunk will be before actually encoding it into the reply
832 * buffer. This function allows easy handling of this case: just encode the
833 * reply, then use this function to break the message into two pieces if it
834 * exceeds the OpenFlow message limit.
836 * In detail, if the final stats message in 'replies' is too long for OpenFlow,
837 * this function breaks it into two separate stats replies, the first one with
838 * the first 'start_ofs' bytes, the second one containing the bytes from that
841 ofpmp_postappend(struct list
*replies
, size_t start_ofs
)
843 struct ofpbuf
*msg
= ofpbuf_from_list(list_back(replies
));
845 assert(start_ofs
<= UINT16_MAX
);
846 if (msg
->size
> UINT16_MAX
) {
847 size_t len
= msg
->size
- start_ofs
;
848 memcpy(ofpmp_append(replies
, len
),
849 (const uint8_t *) msg
->data
+ start_ofs
, len
);
850 msg
->size
= start_ofs
;
855 ofpmp_flags__(const struct ofp_header
*oh
)
857 return (oh
->version
== OFP10_VERSION
858 ? &((struct ofp_stats_msg
*) oh
)->flags
859 : &((struct ofp11_stats_msg
*) oh
)->flags
);
862 /* Returns the OFPSF_* flags found in the OpenFlow stats header of 'oh', which
863 * must be an OpenFlow stats request or reply.
865 * (OFPSF_REPLY_MORE is the only defined flag.) */
867 ofpmp_flags(const struct ofp_header
*oh
)
869 return ntohs(*ofpmp_flags__(oh
));
872 /* Returns true if the OFPSF_REPLY_MORE flag is set in the OpenFlow stats
873 * header of 'oh', which must be an OpenFlow stats request or reply, false if
876 ofpmp_more(const struct ofp_header
*oh
)
878 return (ofpmp_flags(oh
) & OFPSF_REPLY_MORE
) != 0;
881 static void ofpmsgs_init(void);
883 static const struct raw_info
*
884 raw_info_get(enum ofpraw raw
)
888 assert(raw
< ARRAY_SIZE(raw_infos
));
889 return &raw_infos
[raw
];
892 static struct raw_instance
*
893 raw_instance_get(const struct raw_info
*info
, uint8_t version
)
895 assert(version
>= info
->min_version
&& version
<= info
->max_version
);
896 return &info
->instances
[version
- info
->min_version
];
900 ofpraw_from_ofphdrs(enum ofpraw
*raw
, const struct ofphdrs
*hdrs
)
902 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
904 struct raw_instance
*raw_hdrs
;
909 hash
= ofphdrs_hash(hdrs
);
910 HMAP_FOR_EACH_WITH_HASH (raw_hdrs
, hmap_node
, hash
, &raw_instance_map
) {
911 if (ofphdrs_equal(hdrs
, &raw_hdrs
->hdrs
)) {
912 *raw
= raw_hdrs
->raw
;
917 if (!VLOG_DROP_WARN(&rl
)) {
921 ds_put_format(&s
, "version %"PRIu8
", type %"PRIu8
,
922 hdrs
->version
, hdrs
->type
);
923 if (ofphdrs_is_stat(hdrs
)) {
924 ds_put_format(&s
, ", stat %"PRIu16
, hdrs
->stat
);
927 ds_put_format(&s
, ", vendor 0x%"PRIx32
", subtype %"PRIu32
,
928 hdrs
->vendor
, hdrs
->subtype
);
930 VLOG_WARN("unknown OpenFlow message (%s)", ds_cstr(&s
));
934 return (hdrs
->vendor
? OFPERR_OFPBRC_BAD_SUBTYPE
935 : ofphdrs_is_stat(hdrs
) ? OFPERR_OFPBRC_BAD_STAT
936 : OFPERR_OFPBRC_BAD_TYPE
);
942 const struct raw_info
*info
;
944 if (raw_instance_map
.buckets
) {
948 hmap_init(&raw_instance_map
);
949 for (info
= raw_infos
; info
< &raw_infos
[ARRAY_SIZE(raw_infos
)]; info
++)
951 int n_instances
= info
->max_version
- info
->min_version
+ 1;
952 struct raw_instance
*inst
;
954 for (inst
= info
->instances
;
955 inst
< &info
->instances
[n_instances
];
957 inst
->hdrs_len
= ofphdrs_len(&inst
->hdrs
);
958 hmap_insert(&raw_instance_map
, &inst
->hmap_node
,
959 ofphdrs_hash(&inst
->hdrs
));