]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-msgs.c
Add skeleton for OF1.5 support.
[mirror_ovs.git] / lib / ofp-msgs.c
1 /*
2 * Copyright (c) 2012, 2013, 2014 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18 #include "ofp-msgs.h"
19 #include "byte-order.h"
20 #include "dynamic-string.h"
21 #include "hash.h"
22 #include "hmap.h"
23 #include "ofpbuf.h"
24 #include "openflow/nicira-ext.h"
25 #include "openflow/openflow.h"
26 #include "ovs-thread.h"
27 #include "vlog.h"
28
29 VLOG_DEFINE_THIS_MODULE(ofp_msgs);
30
31 #define OFPT_VENDOR 4
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
37
38 /* A thin abstraction of OpenFlow headers:
39 *
40 * - 'version' and 'type' come straight from struct ofp_header, so these are
41 * always present and meaningful.
42 *
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.
46 *
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.
50 * Otherwise it is 0.
51 *
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. */
55 struct ofphdrs {
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. */
64 };
65 BUILD_ASSERT_DECL(sizeof(struct ofphdrs) == 12);
66
67 /* A mapping from OpenFlow headers to OFPRAW_*. */
68 struct raw_instance {
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). */
73 };
74
75 /* Information about a particular 'enum ofpraw'. */
76 struct raw_info {
77 /* All possible instantiations of this OFPRAW_* into OpenFlow headers. */
78 struct raw_instance *instances; /* min_version - max_version + 1 elems. */
79 uint8_t min_version;
80 uint8_t max_version;
81
82 unsigned int min_body;
83 unsigned int extra_multiple;
84 enum ofptype type;
85 const char *name;
86 };
87
88 /* All understood OpenFlow message types, indexed by their 'struct ofphdrs'. */
89 static struct hmap raw_instance_map;
90 #include "ofp-msgs.inc"
91
92 static ovs_be32 alloc_xid(void);
93
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 *);
102
103 static const struct raw_info *raw_info_get(enum ofpraw);
104 static struct raw_instance *raw_instance_get(const struct raw_info *,
105 uint8_t version);
106
107 static enum ofperr ofpraw_from_ofphdrs(enum ofpraw *, const struct ofphdrs *);
108 \f
109 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
110 static ovs_be32
111 alloc_xid(void)
112 {
113 static atomic_uint32_t next_xid = ATOMIC_VAR_INIT(1);
114 uint32_t xid;
115
116 atomic_add(&next_xid, 1, &xid);
117 return htonl(xid);
118 }
119 \f
120 static uint32_t
121 ofphdrs_hash(const struct ofphdrs *hdrs)
122 {
123 BUILD_ASSERT_DECL(sizeof *hdrs == 12);
124 return hash_words((const uint32_t *) hdrs, 3, 0);
125 }
126
127 static bool
128 ofphdrs_equal(const struct ofphdrs *a, const struct ofphdrs *b)
129 {
130 return !memcmp(a, b, sizeof *a);
131 }
132
133 static void
134 log_bad_vendor(uint32_t vendor)
135 {
136 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
137
138 VLOG_WARN_RL(&rl, "OpenFlow message has unknown vendor %#"PRIx32, vendor);
139 }
140
141 static enum ofperr
142 ofphdrs_decode(struct ofphdrs *hdrs,
143 const struct ofp_header *oh, size_t length)
144 {
145 memset(hdrs, 0, sizeof *hdrs);
146 if (length < sizeof *oh) {
147 return OFPERR_OFPBRC_BAD_LEN;
148 }
149
150 /* Get base message version and type (OFPT_*). */
151 hdrs->version = oh->version;
152 hdrs->type = oh->type;
153
154 if (hdrs->type == OFPT_VENDOR) {
155 /* Get vendor. */
156 const struct ofp_vendor_header *ovh;
157
158 if (length < sizeof *ovh) {
159 return OFPERR_OFPBRC_BAD_LEN;
160 }
161
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;
167
168 if (length < sizeof *nh) {
169 return OFPERR_OFPBRC_BAD_LEN;
170 }
171 nh = (const struct nicira_header *) oh;
172 hdrs->subtype = ntohl(nh->subtype);
173 } else {
174 log_bad_vendor(hdrs->vendor);
175 return OFPERR_OFPBRC_BAD_VENDOR;
176 }
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;
181
182 /* Get statistic type (OFPST_*). */
183 if (length < sizeof *osm) {
184 return OFPERR_OFPBRC_BAD_LEN;
185 }
186 osm = (const struct ofp10_stats_msg *) oh;
187 hdrs->stat = ntohs(osm->type);
188
189 if (hdrs->stat == OFPST_VENDOR) {
190 /* Get vendor. */
191 const struct ofp10_vendor_stats_msg *ovsm;
192
193 if (length < sizeof *ovsm) {
194 return OFPERR_OFPBRC_BAD_LEN;
195 }
196
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;
202
203 if (length < sizeof *nsm) {
204 return OFPERR_OFPBRC_BAD_LEN;
205 }
206 nsm = (const struct nicira10_stats_msg *) oh;
207 hdrs->subtype = ntohl(nsm->subtype);
208 } else {
209 log_bad_vendor(hdrs->vendor);
210 return OFPERR_OFPBRC_BAD_VENDOR;
211 }
212 }
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;
217
218 /* Get statistic type (OFPST_*). */
219 if (length < sizeof *osm) {
220 return OFPERR_OFPBRC_BAD_LEN;
221 }
222 osm = (const struct ofp11_stats_msg *) oh;
223 hdrs->stat = ntohs(osm->type);
224
225 if (hdrs->stat == OFPST_VENDOR) {
226 /* Get vendor. */
227 const struct ofp11_vendor_stats_msg *ovsm;
228
229 if (length < sizeof *ovsm) {
230 return OFPERR_OFPBRC_BAD_LEN;
231 }
232
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;
238
239 if (length < sizeof *nsm) {
240 return OFPERR_OFPBRC_BAD_LEN;
241 }
242 nsm = (const struct nicira11_stats_msg *) oh;
243 hdrs->subtype = ntohl(nsm->subtype);
244 } else {
245 log_bad_vendor(hdrs->vendor);
246 return OFPERR_OFPBRC_BAD_VENDOR;
247 }
248 }
249 }
250
251 return 0;
252 }
253
254 static void
255 ofphdrs_decode_assert(struct ofphdrs *hdrs,
256 const struct ofp_header *oh, size_t length)
257 {
258 enum ofperr error = ofphdrs_decode(hdrs, oh, length);
259 ovs_assert(!error);
260 }
261
262 static bool
263 ofp_is_stat_request(enum ofp_version version, uint8_t type)
264 {
265 switch (version) {
266 case OFP10_VERSION:
267 return type == OFPT10_STATS_REQUEST;
268 case OFP11_VERSION:
269 case OFP12_VERSION:
270 case OFP13_VERSION:
271 case OFP14_VERSION:
272 case OFP15_VERSION:
273 return type == OFPT11_STATS_REQUEST;
274 }
275
276 return false;
277 }
278
279 static bool
280 ofp_is_stat_reply(enum ofp_version version, uint8_t type)
281 {
282 switch (version) {
283 case OFP10_VERSION:
284 return type == OFPT10_STATS_REPLY;
285 case OFP11_VERSION:
286 case OFP12_VERSION:
287 case OFP13_VERSION:
288 case OFP14_VERSION:
289 case OFP15_VERSION:
290 return type == OFPT11_STATS_REPLY;
291 }
292
293 return false;
294 }
295
296 static bool
297 ofp_is_stat(enum ofp_version version, uint8_t type)
298 {
299 return (ofp_is_stat_request(version, type) ||
300 ofp_is_stat_reply(version, type));
301 }
302
303 static bool
304 ofphdrs_is_stat(const struct ofphdrs *hdrs)
305 {
306 return ofp_is_stat(hdrs->version, hdrs->type);
307 }
308
309 size_t
310 ofphdrs_len(const struct ofphdrs *hdrs)
311 {
312 if (hdrs->type == OFPT_VENDOR) {
313 return sizeof(struct nicira_header);
314 }
315
316 switch ((enum ofp_version) hdrs->version) {
317 case OFP10_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));
323 }
324 break;
325
326 case OFP11_VERSION:
327 case OFP12_VERSION:
328 case OFP13_VERSION:
329 case OFP14_VERSION:
330 case OFP15_VERSION:
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));
336 }
337 break;
338 }
339
340 return sizeof(struct ofp_header);
341 }
342 \f
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'.
347 *
348 * This function checks that 'oh' is a valid length for its particular type of
349 * message, and returns an error if not. */
350 enum ofperr
351 ofpraw_decode(enum ofpraw *raw, const struct ofp_header *oh)
352 {
353 struct ofpbuf msg;
354
355 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
356 return ofpraw_pull(raw, &msg);
357 }
358
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.
362 *
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.) */
365 enum ofpraw
366 ofpraw_decode_assert(const struct ofp_header *oh)
367 {
368 enum ofperr error;
369 enum ofpraw raw;
370
371 error = ofpraw_decode(&raw, oh);
372 ovs_assert(!error);
373 return raw;
374 }
375
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'.
380 *
381 * This function checks that the message has a valid length for its particular
382 * type of message, and returns an error if not.
383 *
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)). */
389 enum ofperr
390 ofpraw_pull(enum ofpraw *rawp, struct ofpbuf *msg)
391 {
392 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
393
394 const struct raw_instance *instance;
395 const struct raw_info *info;
396 struct ofphdrs hdrs;
397
398 unsigned int min_len;
399 unsigned int len;
400
401 enum ofperr error;
402 enum ofpraw raw;
403
404 /* Set default outputs. */
405 msg->frame = ofpbuf_data(msg);
406 ofpbuf_set_l3(msg, msg->frame);
407 *rawp = 0;
408
409 len = ofpbuf_size(msg);
410 error = ofphdrs_decode(&hdrs, ofpbuf_data(msg), len);
411 if (error) {
412 return error;
413 }
414
415 error = ofpraw_from_ofphdrs(&raw, &hdrs);
416 if (error) {
417 return error;
418 }
419
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));
424
425 min_len = instance->hdrs_len + info->min_body;
426 switch (info->extra_multiple) {
427 case 0:
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;
432 }
433 break;
434
435 case 1:
436 if (len < min_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;
441 }
442 break;
443
444 default:
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 "
448 "of %u bytes)",
449 info->name, len, min_len, info->extra_multiple);
450 return OFPERR_OFPBRC_BAD_LEN;
451 }
452 break;
453 }
454
455 *rawp = raw;
456 return 0;
457 }
458
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.
462 *
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.) */
465 enum ofpraw
466 ofpraw_pull_assert(struct ofpbuf *msg)
467 {
468 enum ofperr error;
469 enum ofpraw raw;
470
471 error = ofpraw_pull(&raw, msg);
472 ovs_assert(!error);
473 return raw;
474 }
475
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'.
479 *
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). */
486 enum ofperr
487 ofpraw_decode_partial(enum ofpraw *raw,
488 const struct ofp_header *oh, size_t length)
489 {
490 struct ofphdrs hdrs;
491 enum ofperr error;
492
493 error = ofphdrs_decode(&hdrs, oh, length);
494 if (!error) {
495 error = ofpraw_from_ofphdrs(raw, &hdrs);
496 }
497
498 if (error) {
499 *raw = 0;
500 }
501 return error;
502 }
503 \f
504 /* Encoding messages using OFPRAW_* values. */
505
506 static void ofpraw_put__(enum ofpraw, uint8_t version, ovs_be32 xid,
507 size_t extra_tailroom, struct ofpbuf *);
508
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.
513 *
514 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
515 * must specify a valid (raw, version) pair.
516 *
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().
521 *
522 * The caller owns the returned ofpbuf and must free it when it is no longer
523 * needed, e.g. with ofpbuf_delete(). */
524 struct ofpbuf *
525 ofpraw_alloc(enum ofpraw raw, uint8_t version, size_t extra_tailroom)
526 {
527 return ofpraw_alloc_xid(raw, version, alloc_xid(), extra_tailroom);
528 }
529
530 /* Same as ofpraw_alloc() but the caller provides the transaction ID. */
531 struct ofpbuf *
532 ofpraw_alloc_xid(enum ofpraw raw, uint8_t version, ovs_be32 xid,
533 size_t extra_tailroom)
534 {
535 struct ofpbuf *buf = ofpbuf_new(0);
536 ofpraw_put__(raw, version, xid, extra_tailroom, buf);
537 return buf;
538 }
539
540 /* Same as ofpraw_alloc(), but obtains the OpenFlow version and transaction ID
541 * from 'request->version' and 'request->xid', respectively.
542 *
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(). */
546 struct ofpbuf *
547 ofpraw_alloc_reply(enum ofpraw raw, const struct ofp_header *request,
548 size_t extra_tailroom)
549 {
550 return ofpraw_alloc_xid(raw, request->version, request->xid,
551 extra_tailroom);
552 }
553
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
558 * bytes.
559 *
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.
563 *
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().
568 *
569 * The caller owns the returned ofpbuf and must free it when it is no longer
570 * needed, e.g. with ofpbuf_delete(). */
571 struct ofpbuf *
572 ofpraw_alloc_stats_reply(const struct ofp_header *request,
573 size_t extra_tailroom)
574 {
575 enum ofpraw request_raw;
576 enum ofpraw reply_raw;
577 enum ofperr error;
578
579 error = ofpraw_decode_partial(&request_raw, request,
580 ntohs(request->length));
581 ovs_assert(!error);
582
583 reply_raw = ofpraw_stats_request_to_reply(request_raw, request->version);
584 ovs_assert(reply_raw);
585
586 return ofpraw_alloc_reply(reply_raw, request, extra_tailroom);
587 }
588
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'
592 * additional bytes.
593 *
594 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
595 * must specify a valid (raw, version) pair.
596 *
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(). */
601 void
602 ofpraw_put(enum ofpraw raw, uint8_t version, struct ofpbuf *buf)
603 {
604 ofpraw_put__(raw, version, alloc_xid(), 0, buf);
605 }
606
607 /* Same as ofpraw_put() but the caller provides the transaction ID. */
608 void
609 ofpraw_put_xid(enum ofpraw raw, uint8_t version, ovs_be32 xid,
610 struct ofpbuf *buf)
611 {
612 ofpraw_put__(raw, version, xid, 0, buf);
613 }
614
615 /* Same as ofpraw_put(), but obtains the OpenFlow version and transaction ID
616 * from 'request->version' and 'request->xid', respectively.
617 *
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(). */
621 void
622 ofpraw_put_reply(enum ofpraw raw, const struct ofp_header *request,
623 struct ofpbuf *buf)
624 {
625 ofpraw_put__(raw, request->version, request->xid, 0, buf);
626 }
627
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.
632 *
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.
636 *
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().
641 *
642 * The caller owns the returned ofpbuf and must free it when it is no longer
643 * needed, e.g. with ofpbuf_delete(). */
644 void
645 ofpraw_put_stats_reply(const struct ofp_header *request, struct ofpbuf *buf)
646 {
647 enum ofperr error;
648 enum ofpraw raw;
649
650 error = ofpraw_decode_partial(&raw, request, ntohs(request->length));
651 ovs_assert(!error);
652
653 raw = ofpraw_stats_request_to_reply(raw, request->version);
654 ovs_assert(raw);
655
656 ofpraw_put__(raw, request->version, request->xid, 0, buf);
657 }
658
659 static void
660 ofpraw_put__(enum ofpraw raw, uint8_t version, ovs_be32 xid,
661 size_t extra_tailroom, struct ofpbuf *buf)
662 {
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;
667
668 ofpbuf_prealloc_tailroom(buf, (instance->hdrs_len + info->min_body
669 + extra_tailroom));
670 buf->frame = ofpbuf_put_uninit(buf, instance->hdrs_len);
671 ofpbuf_set_l3(buf, ofpbuf_tail(buf));
672
673 oh = buf->frame;
674 oh->version = version;
675 oh->type = hdrs->type;
676 oh->length = htons(ofpbuf_size(buf));
677 oh->xid = xid;
678
679 if (hdrs->type == OFPT_VENDOR) {
680 struct nicira_header *nh = buf->frame;
681
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;
689
690 osm->type = htons(hdrs->stat);
691 osm->flags = htons(0);
692
693 if (hdrs->stat == OFPST_VENDOR) {
694 struct ofp10_vendor_stats_msg *ovsm = buf->frame;
695
696 ovsm->vendor = htonl(hdrs->vendor);
697 if (hdrs->vendor == NX_VENDOR_ID) {
698 struct nicira10_stats_msg *nsm = buf->frame;
699
700 nsm->subtype = htonl(hdrs->subtype);
701 memset(nsm->pad, 0, sizeof nsm->pad);
702 } else {
703 OVS_NOT_REACHED();
704 }
705 }
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;
710
711 osm->type = htons(hdrs->stat);
712 osm->flags = htons(0);
713 memset(osm->pad, 0, sizeof osm->pad);
714
715 if (hdrs->stat == OFPST_VENDOR) {
716 struct ofp11_vendor_stats_msg *ovsm = buf->frame;
717
718 ovsm->vendor = htonl(hdrs->vendor);
719 if (hdrs->vendor == NX_VENDOR_ID) {
720 struct nicira11_stats_msg *nsm = buf->frame;
721
722 nsm->subtype = htonl(hdrs->subtype);
723 } else {
724 OVS_NOT_REACHED();
725 }
726 }
727 }
728 }
729 \f
730 /* Returns 'raw''s name.
731 *
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".
735 *
736 * The caller must not modify or free the returned string. */
737 const char *
738 ofpraw_get_name(enum ofpraw raw)
739 {
740 return raw_info_get(raw)->name;
741 }
742
743 /* Returns the stats reply that corresponds to 'raw' in the given OpenFlow
744 * 'version'. */
745 enum ofpraw
746 ofpraw_stats_request_to_reply(enum ofpraw raw, uint8_t version)
747 {
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;
751 struct ofphdrs hdrs;
752 enum ofperr error;
753
754 hdrs = instance->hdrs;
755 switch ((enum ofp_version)hdrs.version) {
756 case OFP10_VERSION:
757 ovs_assert(hdrs.type == OFPT10_STATS_REQUEST);
758 hdrs.type = OFPT10_STATS_REPLY;
759 break;
760 case OFP11_VERSION:
761 case OFP12_VERSION:
762 case OFP13_VERSION:
763 case OFP14_VERSION:
764 case OFP15_VERSION:
765 ovs_assert(hdrs.type == OFPT11_STATS_REQUEST);
766 hdrs.type = OFPT11_STATS_REPLY;
767 break;
768 default:
769 OVS_NOT_REACHED();
770 }
771
772 error = ofpraw_from_ofphdrs(&reply_raw, &hdrs);
773 ovs_assert(!error);
774
775 return reply_raw;
776 }
777 \f
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'.
782 *
783 * This function checks that 'oh' is a valid length for its particular type of
784 * message, and returns an error if not. */
785 enum ofperr
786 ofptype_decode(enum ofptype *typep, const struct ofp_header *oh)
787 {
788 enum ofperr error;
789 enum ofpraw raw;
790
791 error = ofpraw_decode(&raw, oh);
792 *typep = error ? 0 : ofptype_from_ofpraw(raw);
793 return error;
794 }
795
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'.
800 *
801 * This function checks that the message has a valid length for its particular
802 * type of message, and returns an error if not.
803 *
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)). */
809 enum ofperr
810 ofptype_pull(enum ofptype *typep, struct ofpbuf *buf)
811 {
812 enum ofperr error;
813 enum ofpraw raw;
814
815 error = ofpraw_pull(&raw, buf);
816 *typep = error ? 0 : ofptype_from_ofpraw(raw);
817 return error;
818 }
819
820 /* Returns the OFPTYPE_* type that corresponds to 'raw'.
821 *
822 * (This is a one-way trip, because the mapping from ofpraw to ofptype is
823 * many-to-one.) */
824 enum ofptype
825 ofptype_from_ofpraw(enum ofpraw raw)
826 {
827 return raw_info_get(raw)->type;
828 }
829 \f
830 /* Updates the 'length' field of the OpenFlow message in 'buf' to
831 * 'ofpbuf_size(buf)'. */
832 void
833 ofpmsg_update_length(struct ofpbuf *buf)
834 {
835 struct ofp_header *oh = ofpbuf_at_assert(buf, 0, sizeof *oh);
836 oh->length = htons(ofpbuf_size(buf));
837 }
838
839 /* Returns just past the Openflow header (including the stats headers, vendor
840 * header, and any subtype header) in 'oh'. */
841 const void *
842 ofpmsg_body(const struct ofp_header *oh)
843 {
844 struct ofphdrs hdrs;
845
846 ofphdrs_decode_assert(&hdrs, oh, ntohs(oh->length));
847 return (const uint8_t *) oh + ofphdrs_len(&hdrs);
848 }
849
850 /* Return if it's a stat/multipart (OFPST) request message. */
851 bool
852 ofpmsg_is_stat_request(const struct ofp_header *oh)
853 {
854 return ofp_is_stat_request(oh->version, oh->type);
855 }
856 \f
857 static ovs_be16 *ofpmp_flags__(const struct ofp_header *);
858
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. */
864 void
865 ofpmp_init(struct list *replies, const struct ofp_header *request)
866 {
867 struct ofpbuf *msg;
868
869 list_init(replies);
870
871 msg = ofpraw_alloc_stats_reply(request, 1000);
872 list_push_back(replies, &msg->list_node);
873 }
874
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.
878 *
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(). */
882 struct ofpbuf *
883 ofpmp_reserve(struct list *replies, size_t len)
884 {
885 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
886
887 if (ofpbuf_size(msg) + len <= UINT16_MAX) {
888 ofpbuf_prealloc_tailroom(msg, len);
889 return msg;
890 } else {
891 unsigned int hdrs_len;
892 struct ofpbuf *next;
893 struct ofphdrs hdrs;
894
895 ofphdrs_decode_assert(&hdrs, ofpbuf_data(msg), ofpbuf_size(msg));
896 hdrs_len = ofphdrs_len(&hdrs);
897
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);
903
904 *ofpmp_flags__(ofpbuf_data(msg)) |= htons(OFPSF_REPLY_MORE);
905
906 return next;
907 }
908 }
909
910 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
911 * returns the first byte. */
912 void *
913 ofpmp_append(struct list *replies, size_t len)
914 {
915 return ofpbuf_put_uninit(ofpmp_reserve(replies, len), len);
916 }
917
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.
923 *
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
927 * offset onward. */
928 void
929 ofpmp_postappend(struct list *replies, size_t start_ofs)
930 {
931 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
932
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);
939 }
940 }
941
942 /* Returns the OpenFlow version of the replies being constructed in 'replies',
943 * which should have been initialized by ofpmp_init(). */
944 enum ofp_version
945 ofpmp_version(struct list *replies)
946 {
947 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
948 const struct ofp_header *oh = ofpbuf_data(msg);
949
950 return oh->version;
951 }
952
953 /* Determines the OFPRAW_* type of the OpenFlow messages in 'replies', which
954 * should have been initialized by ofpmp_init(). */
955 enum ofpraw
956 ofpmp_decode_raw(struct list *replies)
957 {
958 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
959 enum ofperr error;
960 enum ofpraw raw;
961
962 error = ofpraw_decode_partial(&raw, ofpbuf_data(msg), ofpbuf_size(msg));
963 ovs_assert(!error);
964 return raw;
965 }
966
967 static ovs_be16 *
968 ofpmp_flags__(const struct ofp_header *oh)
969 {
970 switch ((enum ofp_version)oh->version) {
971 case OFP10_VERSION:
972 return &((struct ofp10_stats_msg *) oh)->flags;
973 case OFP11_VERSION:
974 case OFP12_VERSION:
975 case OFP13_VERSION:
976 case OFP14_VERSION:
977 case OFP15_VERSION:
978 return &((struct ofp11_stats_msg *) oh)->flags;
979 default:
980 OVS_NOT_REACHED();
981 }
982 }
983
984 /* Returns the OFPSF_* flags found in the OpenFlow stats header of 'oh', which
985 * must be an OpenFlow stats request or reply.
986 *
987 * (OFPSF_REPLY_MORE is the only defined flag.) */
988 uint16_t
989 ofpmp_flags(const struct ofp_header *oh)
990 {
991 return ntohs(*ofpmp_flags__(oh));
992 }
993
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
996 * it is not set. */
997 bool
998 ofpmp_more(const struct ofp_header *oh)
999 {
1000 return (ofpmp_flags(oh) & OFPSF_REPLY_MORE) != 0;
1001 }
1002 \f
1003 static void ofpmsgs_init(void);
1004
1005 static const struct raw_info *
1006 raw_info_get(enum ofpraw raw)
1007 {
1008 ofpmsgs_init();
1009
1010 ovs_assert(raw < ARRAY_SIZE(raw_infos));
1011 return &raw_infos[raw];
1012 }
1013
1014 static struct raw_instance *
1015 raw_instance_get(const struct raw_info *info, uint8_t version)
1016 {
1017 ovs_assert(version >= info->min_version && version <= info->max_version);
1018 return &info->instances[version - info->min_version];
1019 }
1020
1021 static enum ofperr
1022 ofpraw_from_ofphdrs(enum ofpraw *raw, const struct ofphdrs *hdrs)
1023 {
1024 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1025
1026 struct raw_instance *raw_hdrs;
1027 uint32_t hash;
1028
1029 ofpmsgs_init();
1030
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;
1035 return 0;
1036 }
1037 }
1038
1039 if (!VLOG_DROP_WARN(&rl)) {
1040 struct ds s;
1041
1042 ds_init(&s);
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);
1047 }
1048 if (hdrs->vendor) {
1049 ds_put_format(&s, ", vendor 0x%"PRIx32", subtype %"PRIu32,
1050 hdrs->vendor, hdrs->subtype);
1051 }
1052 VLOG_WARN("unknown OpenFlow message (%s)", ds_cstr(&s));
1053 ds_destroy(&s);
1054 }
1055
1056 return (hdrs->vendor ? OFPERR_OFPBRC_BAD_SUBTYPE
1057 : ofphdrs_is_stat(hdrs) ? OFPERR_OFPBRC_BAD_STAT
1058 : OFPERR_OFPBRC_BAD_TYPE);
1059 }
1060
1061 static void
1062 ofpmsgs_init(void)
1063 {
1064 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1065 const struct raw_info *info;
1066
1067 if (!ovsthread_once_start(&once)) {
1068 return;
1069 }
1070
1071 hmap_init(&raw_instance_map);
1072 for (info = raw_infos; info < &raw_infos[ARRAY_SIZE(raw_infos)]; info++)
1073 {
1074 int n_instances = info->max_version - info->min_version + 1;
1075 struct raw_instance *inst;
1076
1077 for (inst = info->instances;
1078 inst < &info->instances[n_instances];
1079 inst++) {
1080 inst->hdrs_len = ofphdrs_len(&inst->hdrs);
1081 hmap_insert(&raw_instance_map, &inst->hmap_node,
1082 ofphdrs_hash(&inst->hdrs));
1083 }
1084 }
1085
1086 ovsthread_once_done(&once);
1087 }