]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-msgs.c
Rename NOT_REACHED to OVS_NOT_REACHED
[mirror_ovs.git] / lib / ofp-msgs.c
1 /*
2 * Copyright (c) 2012, 2013 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 return type == OFPT11_STATS_REQUEST;
272 }
273
274 return false;
275 }
276
277 static bool
278 ofp_is_stat_reply(enum ofp_version version, uint8_t type)
279 {
280 switch (version) {
281 case OFP10_VERSION:
282 return type == OFPT10_STATS_REPLY;
283 case OFP11_VERSION:
284 case OFP12_VERSION:
285 case OFP13_VERSION:
286 return type == OFPT11_STATS_REPLY;
287 }
288
289 return false;
290 }
291
292 static bool
293 ofp_is_stat(enum ofp_version version, uint8_t type)
294 {
295 return (ofp_is_stat_request(version, type) ||
296 ofp_is_stat_reply(version, type));
297 }
298
299 static bool
300 ofphdrs_is_stat(const struct ofphdrs *hdrs)
301 {
302 return ofp_is_stat(hdrs->version, hdrs->type);
303 }
304
305 size_t
306 ofphdrs_len(const struct ofphdrs *hdrs)
307 {
308 if (hdrs->type == OFPT_VENDOR) {
309 return sizeof(struct nicira_header);
310 }
311
312 switch ((enum ofp_version) hdrs->version) {
313 case OFP10_VERSION:
314 if (hdrs->type == OFPT10_STATS_REQUEST ||
315 hdrs->type == OFPT10_STATS_REPLY) {
316 return (hdrs->stat == OFPST_VENDOR
317 ? sizeof(struct nicira10_stats_msg)
318 : sizeof(struct ofp10_stats_msg));
319 }
320 break;
321
322 case OFP11_VERSION:
323 case OFP12_VERSION:
324 case OFP13_VERSION:
325 if (hdrs->type == OFPT11_STATS_REQUEST ||
326 hdrs->type == OFPT11_STATS_REPLY) {
327 return (hdrs->stat == OFPST_VENDOR
328 ? sizeof(struct nicira11_stats_msg)
329 : sizeof(struct ofp11_stats_msg));
330 }
331 break;
332 }
333
334 return sizeof(struct ofp_header);
335 }
336 \f
337 /* Determines the OFPRAW_* type of the OpenFlow message at 'oh', which has
338 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
339 * data are readable at 'oh'.) On success, returns 0 and stores the type into
340 * '*raw'. On failure, returns an OFPERR_* error code and zeros '*raw'.
341 *
342 * This function checks that 'oh' is a valid length for its particular type of
343 * message, and returns an error if not. */
344 enum ofperr
345 ofpraw_decode(enum ofpraw *raw, const struct ofp_header *oh)
346 {
347 struct ofpbuf msg;
348
349 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
350 return ofpraw_pull(raw, &msg);
351 }
352
353 /* Does the same job as ofpraw_decode(), except that it assert-fails if
354 * ofpraw_decode() would have reported an error. Thus, it's able to use the
355 * return value for the OFPRAW_* message type instead of an error code.
356 *
357 * (It only makes sense to use this function if you previously called
358 * ofpraw_decode() on the message and thus know that it's OK.) */
359 enum ofpraw
360 ofpraw_decode_assert(const struct ofp_header *oh)
361 {
362 enum ofperr error;
363 enum ofpraw raw;
364
365 error = ofpraw_decode(&raw, oh);
366 ovs_assert(!error);
367 return raw;
368 }
369
370 /* Determines the OFPRAW_* type of the OpenFlow message in 'msg', which starts
371 * at 'msg->data' and has length 'msg->size' bytes. On success, returns 0 and
372 * stores the type into '*rawp'. On failure, returns an OFPERR_* error code
373 * and zeros '*rawp'.
374 *
375 * This function checks that the message has a valid length for its particular
376 * type of message, and returns an error if not.
377 *
378 * In addition to setting '*rawp', this function pulls off the OpenFlow header
379 * (including the stats headers, vendor header, and any subtype header) with
380 * ofpbuf_pull(). It also sets 'msg->l2' to the start of the OpenFlow header
381 * and 'msg->l3' just beyond the headers (that is, to the final value of
382 * msg->data). */
383 enum ofperr
384 ofpraw_pull(enum ofpraw *rawp, struct ofpbuf *msg)
385 {
386 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
387
388 const struct raw_instance *instance;
389 const struct raw_info *info;
390 struct ofphdrs hdrs;
391
392 unsigned int min_len;
393 unsigned int len;
394
395 enum ofperr error;
396 enum ofpraw raw;
397
398 /* Set default outputs. */
399 msg->l2 = msg->l3 = msg->data;
400 *rawp = 0;
401
402 len = msg->size;
403 error = ofphdrs_decode(&hdrs, msg->data, len);
404 if (error) {
405 return error;
406 }
407
408 error = ofpraw_from_ofphdrs(&raw, &hdrs);
409 if (error) {
410 return error;
411 }
412
413 info = raw_info_get(raw);
414 instance = raw_instance_get(info, hdrs.version);
415 msg->l2 = ofpbuf_pull(msg, instance->hdrs_len);
416 msg->l3 = msg->data;
417
418 min_len = instance->hdrs_len + info->min_body;
419 switch (info->extra_multiple) {
420 case 0:
421 if (len != min_len) {
422 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (expected "
423 "length %u)", info->name, len, min_len);
424 return OFPERR_OFPBRC_BAD_LEN;
425 }
426 break;
427
428 case 1:
429 if (len < min_len) {
430 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (expected "
431 "length at least %u bytes)",
432 info->name, len, min_len);
433 return OFPERR_OFPBRC_BAD_LEN;
434 }
435 break;
436
437 default:
438 if (len < min_len || (len - min_len) % info->extra_multiple) {
439 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (must be "
440 "exactly %u bytes or longer by an integer multiple "
441 "of %u bytes)",
442 info->name, len, min_len, info->extra_multiple);
443 return OFPERR_OFPBRC_BAD_LEN;
444 }
445 break;
446 }
447
448 *rawp = raw;
449 return 0;
450 }
451
452 /* Does the same job as ofpraw_pull(), except that it assert-fails if
453 * ofpraw_pull() would have reported an error. Thus, it's able to use the
454 * return value for the OFPRAW_* message type instead of an error code.
455 *
456 * (It only makes sense to use this function if you previously called
457 * ofpraw_decode() on the message and thus know that it's OK.) */
458 enum ofpraw
459 ofpraw_pull_assert(struct ofpbuf *msg)
460 {
461 enum ofperr error;
462 enum ofpraw raw;
463
464 error = ofpraw_pull(&raw, msg);
465 ovs_assert(!error);
466 return raw;
467 }
468
469 /* Determines the OFPRAW_* type of the OpenFlow message that starts at 'oh' and
470 * has length 'length' bytes. On success, returns 0 and stores the type into
471 * '*rawp'. On failure, returns an OFPERR_* error code and zeros '*rawp'.
472 *
473 * Unlike other functions for decoding message types, this one is not picky
474 * about message length. For example, it will successfully decode a message
475 * whose body is shorter than the minimum length for a message of its type.
476 * Thus, this is the correct function to use for decoding the type of a message
477 * that might have been truncated, such as the payload of an OpenFlow error
478 * message (which is allowed to be truncated to 64 bytes). */
479 enum ofperr
480 ofpraw_decode_partial(enum ofpraw *raw,
481 const struct ofp_header *oh, size_t length)
482 {
483 struct ofphdrs hdrs;
484 enum ofperr error;
485
486 error = ofphdrs_decode(&hdrs, oh, length);
487 if (!error) {
488 error = ofpraw_from_ofphdrs(raw, &hdrs);
489 }
490
491 if (error) {
492 *raw = 0;
493 }
494 return error;
495 }
496 \f
497 /* Encoding messages using OFPRAW_* values. */
498
499 static void ofpraw_put__(enum ofpraw, uint8_t version, ovs_be32 xid,
500 size_t extra_tailroom, struct ofpbuf *);
501
502 /* Allocates and returns a new ofpbuf that contains an OpenFlow header for
503 * 'raw' with OpenFlow version 'version' and a fresh OpenFlow transaction ID.
504 * The ofpbuf has enough tailroom for the minimum body length of 'raw', plus
505 * 'extra_tailroom' additional bytes.
506 *
507 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
508 * must specify a valid (raw, version) pair.
509 *
510 * In the returned ofpbuf, 'l2' points to the beginning of the OpenFlow header
511 * and 'l3' points just after it, to where the message's body will start. The
512 * caller must actually allocate the body into the space reserved for it,
513 * e.g. with ofpbuf_put_uninit().
514 *
515 * The caller owns the returned ofpbuf and must free it when it is no longer
516 * needed, e.g. with ofpbuf_delete(). */
517 struct ofpbuf *
518 ofpraw_alloc(enum ofpraw raw, uint8_t version, size_t extra_tailroom)
519 {
520 return ofpraw_alloc_xid(raw, version, alloc_xid(), extra_tailroom);
521 }
522
523 /* Same as ofpraw_alloc() but the caller provides the transaction ID. */
524 struct ofpbuf *
525 ofpraw_alloc_xid(enum ofpraw raw, uint8_t version, ovs_be32 xid,
526 size_t extra_tailroom)
527 {
528 struct ofpbuf *buf = ofpbuf_new(0);
529 ofpraw_put__(raw, version, xid, extra_tailroom, buf);
530 return buf;
531 }
532
533 /* Same as ofpraw_alloc(), but obtains the OpenFlow version and transaction ID
534 * from 'request->version' and 'request->xid', respectively.
535 *
536 * Even though the version comes from 'request->version', the caller must still
537 * know what it is doing, by specifying a valid pairing of 'raw' and
538 * 'request->version', just like ofpraw_alloc(). */
539 struct ofpbuf *
540 ofpraw_alloc_reply(enum ofpraw raw, const struct ofp_header *request,
541 size_t extra_tailroom)
542 {
543 return ofpraw_alloc_xid(raw, request->version, request->xid,
544 extra_tailroom);
545 }
546
547 /* Allocates and returns a new ofpbuf that contains an OpenFlow header that is
548 * a stats reply to the stats request in 'request', using the same OpenFlow
549 * version and transaction ID as 'request'. The ofpbuf has enough tailroom for
550 * the stats reply's minimum body length, plus 'extra_tailroom' additional
551 * bytes.
552 *
553 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
554 * value. Every stats request has a corresponding reply, so the (raw, version)
555 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
556 *
557 * In the returned ofpbuf, 'l2' points to the beginning of the OpenFlow header
558 * and 'l3' points just after it, to where the message's body will start. The
559 * caller must actually allocate the body into the space reserved for it,
560 * e.g. with ofpbuf_put_uninit().
561 *
562 * The caller owns the returned ofpbuf and must free it when it is no longer
563 * needed, e.g. with ofpbuf_delete(). */
564 struct ofpbuf *
565 ofpraw_alloc_stats_reply(const struct ofp_header *request,
566 size_t extra_tailroom)
567 {
568 enum ofpraw request_raw;
569 enum ofpraw reply_raw;
570 enum ofperr error;
571
572 error = ofpraw_decode_partial(&request_raw, request,
573 ntohs(request->length));
574 ovs_assert(!error);
575
576 reply_raw = ofpraw_stats_request_to_reply(request_raw, request->version);
577 ovs_assert(reply_raw);
578
579 return ofpraw_alloc_reply(reply_raw, request, extra_tailroom);
580 }
581
582 /* Appends to 'buf' an OpenFlow header for 'raw' with OpenFlow version
583 * 'version' and a fresh OpenFlow transaction ID. Preallocates enough tailroom
584 * in 'buf' for the minimum body length of 'raw', plus 'extra_tailroom'
585 * additional bytes.
586 *
587 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
588 * must specify a valid (raw, version) pair.
589 *
590 * Upon return, 'buf->l2' points to the beginning of the OpenFlow header and
591 * 'buf->l3' points just after it, to where the message's body will start. The
592 * caller must actually allocating the body into the space reserved for it,
593 * e.g. with ofpbuf_put_uninit(). */
594 void
595 ofpraw_put(enum ofpraw raw, uint8_t version, struct ofpbuf *buf)
596 {
597 ofpraw_put__(raw, version, alloc_xid(), 0, buf);
598 }
599
600 /* Same as ofpraw_put() but the caller provides the transaction ID. */
601 void
602 ofpraw_put_xid(enum ofpraw raw, uint8_t version, ovs_be32 xid,
603 struct ofpbuf *buf)
604 {
605 ofpraw_put__(raw, version, xid, 0, buf);
606 }
607
608 /* Same as ofpraw_put(), but obtains the OpenFlow version and transaction ID
609 * from 'request->version' and 'request->xid', respectively.
610 *
611 * Even though the version comes from 'request->version', the caller must still
612 * know what it is doing, by specifying a valid pairing of 'raw' and
613 * 'request->version', just like ofpraw_put(). */
614 void
615 ofpraw_put_reply(enum ofpraw raw, const struct ofp_header *request,
616 struct ofpbuf *buf)
617 {
618 ofpraw_put__(raw, request->version, request->xid, 0, buf);
619 }
620
621 /* Appends to 'buf' an OpenFlow header that is a stats reply to the stats
622 * request in 'request', using the same OpenFlow version and transaction ID as
623 * 'request'. Preallocate enough tailroom in 'buf for the stats reply's
624 * minimum body length, plus 'extra_tailroom' additional bytes.
625 *
626 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
627 * value. Every stats request has a corresponding reply, so the (raw, version)
628 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
629 *
630 * In the returned ofpbuf, 'l2' points to the beginning of the OpenFlow header
631 * and 'l3' points just after it, to where the message's body will start. The
632 * caller must actually allocate the body into the space reserved for it,
633 * e.g. with ofpbuf_put_uninit().
634 *
635 * The caller owns the returned ofpbuf and must free it when it is no longer
636 * needed, e.g. with ofpbuf_delete(). */
637 void
638 ofpraw_put_stats_reply(const struct ofp_header *request, struct ofpbuf *buf)
639 {
640 enum ofperr error;
641 enum ofpraw raw;
642
643 error = ofpraw_decode_partial(&raw, request, ntohs(request->length));
644 ovs_assert(!error);
645
646 raw = ofpraw_stats_request_to_reply(raw, request->version);
647 ovs_assert(raw);
648
649 ofpraw_put__(raw, request->version, request->xid, 0, buf);
650 }
651
652 static void
653 ofpraw_put__(enum ofpraw raw, uint8_t version, ovs_be32 xid,
654 size_t extra_tailroom, struct ofpbuf *buf)
655 {
656 const struct raw_info *info = raw_info_get(raw);
657 const struct raw_instance *instance = raw_instance_get(info, version);
658 const struct ofphdrs *hdrs = &instance->hdrs;
659 struct ofp_header *oh;
660
661 ofpbuf_prealloc_tailroom(buf, (instance->hdrs_len + info->min_body
662 + extra_tailroom));
663 buf->l2 = ofpbuf_put_uninit(buf, instance->hdrs_len);
664 buf->l3 = ofpbuf_tail(buf);
665
666 oh = buf->l2;
667 oh->version = version;
668 oh->type = hdrs->type;
669 oh->length = htons(buf->size);
670 oh->xid = xid;
671
672 if (hdrs->type == OFPT_VENDOR) {
673 struct nicira_header *nh = buf->l2;
674
675 ovs_assert(hdrs->vendor == NX_VENDOR_ID);
676 nh->vendor = htonl(hdrs->vendor);
677 nh->subtype = htonl(hdrs->subtype);
678 } else if (version == OFP10_VERSION
679 && (hdrs->type == OFPT10_STATS_REQUEST ||
680 hdrs->type == OFPT10_STATS_REPLY)) {
681 struct ofp10_stats_msg *osm = buf->l2;
682
683 osm->type = htons(hdrs->stat);
684 osm->flags = htons(0);
685
686 if (hdrs->stat == OFPST_VENDOR) {
687 struct ofp10_vendor_stats_msg *ovsm = buf->l2;
688
689 ovsm->vendor = htonl(hdrs->vendor);
690 if (hdrs->vendor == NX_VENDOR_ID) {
691 struct nicira10_stats_msg *nsm = buf->l2;
692
693 nsm->subtype = htonl(hdrs->subtype);
694 memset(nsm->pad, 0, sizeof nsm->pad);
695 } else {
696 OVS_NOT_REACHED();
697 }
698 }
699 } else if (version != OFP10_VERSION
700 && (hdrs->type == OFPT11_STATS_REQUEST ||
701 hdrs->type == OFPT11_STATS_REPLY)) {
702 struct ofp11_stats_msg *osm = buf->l2;
703
704 osm->type = htons(hdrs->stat);
705 osm->flags = htons(0);
706 memset(osm->pad, 0, sizeof osm->pad);
707
708 if (hdrs->stat == OFPST_VENDOR) {
709 struct ofp11_vendor_stats_msg *ovsm = buf->l2;
710
711 ovsm->vendor = htonl(hdrs->vendor);
712 if (hdrs->vendor == NX_VENDOR_ID) {
713 struct nicira11_stats_msg *nsm = buf->l2;
714
715 nsm->subtype = htonl(hdrs->subtype);
716 } else {
717 OVS_NOT_REACHED();
718 }
719 }
720 }
721 }
722 \f
723 /* Returns 'raw''s name.
724 *
725 * The name is the name used for 'raw' in the OpenFlow specification. For
726 * example, ofpraw_get_name(OFPRAW_OFPT10_FEATURES_REPLY) is
727 * "OFPT_FEATURES_REPLY".
728 *
729 * The caller must not modify or free the returned string. */
730 const char *
731 ofpraw_get_name(enum ofpraw raw)
732 {
733 return raw_info_get(raw)->name;
734 }
735
736 /* Returns the stats reply that corresponds to 'raw' in the given OpenFlow
737 * 'version'. */
738 enum ofpraw
739 ofpraw_stats_request_to_reply(enum ofpraw raw, uint8_t version)
740 {
741 const struct raw_info *info = raw_info_get(raw);
742 const struct raw_instance *instance = raw_instance_get(info, version);
743 enum ofpraw reply_raw;
744 struct ofphdrs hdrs;
745 enum ofperr error;
746
747 hdrs = instance->hdrs;
748 switch ((enum ofp_version)hdrs.version) {
749 case OFP10_VERSION:
750 ovs_assert(hdrs.type == OFPT10_STATS_REQUEST);
751 hdrs.type = OFPT10_STATS_REPLY;
752 break;
753 case OFP11_VERSION:
754 case OFP12_VERSION:
755 case OFP13_VERSION:
756 ovs_assert(hdrs.type == OFPT11_STATS_REQUEST);
757 hdrs.type = OFPT11_STATS_REPLY;
758 break;
759 default:
760 OVS_NOT_REACHED();
761 }
762
763 error = ofpraw_from_ofphdrs(&reply_raw, &hdrs);
764 ovs_assert(!error);
765
766 return reply_raw;
767 }
768 \f
769 /* Determines the OFPTYPE_* type of the OpenFlow message at 'oh', which has
770 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
771 * data are readable at 'oh'.) On success, returns 0 and stores the type into
772 * '*typep'. On failure, returns an OFPERR_* error code and zeros '*typep'.
773 *
774 * This function checks that 'oh' is a valid length for its particular type of
775 * message, and returns an error if not. */
776 enum ofperr
777 ofptype_decode(enum ofptype *typep, const struct ofp_header *oh)
778 {
779 enum ofperr error;
780 enum ofpraw raw;
781
782 error = ofpraw_decode(&raw, oh);
783 *typep = error ? 0 : ofptype_from_ofpraw(raw);
784 return error;
785 }
786
787 /* Determines the OFPTYPE_* type of the OpenFlow message in 'msg', which starts
788 * at 'msg->data' and has length 'msg->size' bytes. On success, returns 0 and
789 * stores the type into '*typep'. On failure, returns an OFPERR_* error code
790 * and zeros '*typep'.
791 *
792 * This function checks that the message has a valid length for its particular
793 * type of message, and returns an error if not.
794 *
795 * In addition to setting '*typep', this function pulls off the OpenFlow header
796 * (including the stats headers, vendor header, and any subtype header) with
797 * ofpbuf_pull(). It also sets 'msg->l2' to the start of the OpenFlow header
798 * and 'msg->l3' just beyond the headers (that is, to the final value of
799 * msg->data). */
800 enum ofperr
801 ofptype_pull(enum ofptype *typep, struct ofpbuf *buf)
802 {
803 enum ofperr error;
804 enum ofpraw raw;
805
806 error = ofpraw_pull(&raw, buf);
807 *typep = error ? 0 : ofptype_from_ofpraw(raw);
808 return error;
809 }
810
811 /* Returns the OFPTYPE_* type that corresponds to 'raw'.
812 *
813 * (This is a one-way trip, because the mapping from ofpraw to ofptype is
814 * many-to-one.) */
815 enum ofptype
816 ofptype_from_ofpraw(enum ofpraw raw)
817 {
818 return raw_info_get(raw)->type;
819 }
820 \f
821 /* Updates the 'length' field of the OpenFlow message in 'buf' to
822 * 'buf->size'. */
823 void
824 ofpmsg_update_length(struct ofpbuf *buf)
825 {
826 struct ofp_header *oh = ofpbuf_at_assert(buf, 0, sizeof *oh);
827 oh->length = htons(buf->size);
828 }
829
830 /* Returns just past the Openflow header (including the stats headers, vendor
831 * header, and any subtype header) in 'oh'. */
832 const void *
833 ofpmsg_body(const struct ofp_header *oh)
834 {
835 struct ofphdrs hdrs;
836
837 ofphdrs_decode_assert(&hdrs, oh, ntohs(oh->length));
838 return (const uint8_t *) oh + ofphdrs_len(&hdrs);
839 }
840
841 /* Return if it's a stat/multipart (OFPST) request message. */
842 bool
843 ofpmsg_is_stat_request(const struct ofp_header *oh)
844 {
845 return ofp_is_stat_request(oh->version, oh->type);
846 }
847 \f
848 static ovs_be16 *ofpmp_flags__(const struct ofp_header *);
849
850 /* Initializes 'replies' as a new list of stats messages that reply to
851 * 'request', which must be a stats request message. Initially the list will
852 * consist of only a single reply part without any body. The caller should
853 * use calls to the other ofpmp_*() functions to add to the body and split the
854 * message into multiple parts, if necessary. */
855 void
856 ofpmp_init(struct list *replies, const struct ofp_header *request)
857 {
858 struct ofpbuf *msg;
859
860 list_init(replies);
861
862 msg = ofpraw_alloc_stats_reply(request, 1000);
863 list_push_back(replies, &msg->list_node);
864 }
865
866 /* Prepares to append up to 'len' bytes to the series of statistics replies in
867 * 'replies', which should have been initialized with ofpmp_init(), if
868 * necessary adding a new reply to the list.
869 *
870 * Returns an ofpbuf with at least 'len' bytes of tailroom. The 'len' bytes
871 * have not actually been allocated, so the caller must do so with
872 * e.g. ofpbuf_put_uninit(). */
873 struct ofpbuf *
874 ofpmp_reserve(struct list *replies, size_t len)
875 {
876 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
877
878 if (msg->size + len <= UINT16_MAX) {
879 ofpbuf_prealloc_tailroom(msg, len);
880 return msg;
881 } else {
882 unsigned int hdrs_len;
883 struct ofpbuf *next;
884 struct ofphdrs hdrs;
885
886 ofphdrs_decode_assert(&hdrs, msg->data, msg->size);
887 hdrs_len = ofphdrs_len(&hdrs);
888
889 next = ofpbuf_new(MAX(1024, hdrs_len + len));
890 ofpbuf_put(next, msg->data, hdrs_len);
891 next->l2 = next->data;
892 next->l3 = ofpbuf_tail(next);
893 list_push_back(replies, &next->list_node);
894
895 *ofpmp_flags__(msg->data) |= htons(OFPSF_REPLY_MORE);
896
897 return next;
898 }
899 }
900
901 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
902 * returns the first byte. */
903 void *
904 ofpmp_append(struct list *replies, size_t len)
905 {
906 return ofpbuf_put_uninit(ofpmp_reserve(replies, len), len);
907 }
908
909 /* Sometimes, when composing stats replies, it's difficult to predict how long
910 * an individual reply chunk will be before actually encoding it into the reply
911 * buffer. This function allows easy handling of this case: just encode the
912 * reply, then use this function to break the message into two pieces if it
913 * exceeds the OpenFlow message limit.
914 *
915 * In detail, if the final stats message in 'replies' is too long for OpenFlow,
916 * this function breaks it into two separate stats replies, the first one with
917 * the first 'start_ofs' bytes, the second one containing the bytes from that
918 * offset onward. */
919 void
920 ofpmp_postappend(struct list *replies, size_t start_ofs)
921 {
922 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
923
924 ovs_assert(start_ofs <= UINT16_MAX);
925 if (msg->size > UINT16_MAX) {
926 size_t len = msg->size - start_ofs;
927 memcpy(ofpmp_append(replies, len),
928 (const uint8_t *) msg->data + start_ofs, len);
929 msg->size = start_ofs;
930 }
931 }
932
933 static ovs_be16 *
934 ofpmp_flags__(const struct ofp_header *oh)
935 {
936 switch ((enum ofp_version)oh->version) {
937 case OFP10_VERSION:
938 return &((struct ofp10_stats_msg *) oh)->flags;
939 case OFP11_VERSION:
940 case OFP12_VERSION:
941 case OFP13_VERSION:
942 return &((struct ofp11_stats_msg *) oh)->flags;
943 default:
944 OVS_NOT_REACHED();
945 }
946 }
947
948 /* Returns the OFPSF_* flags found in the OpenFlow stats header of 'oh', which
949 * must be an OpenFlow stats request or reply.
950 *
951 * (OFPSF_REPLY_MORE is the only defined flag.) */
952 uint16_t
953 ofpmp_flags(const struct ofp_header *oh)
954 {
955 return ntohs(*ofpmp_flags__(oh));
956 }
957
958 /* Returns true if the OFPSF_REPLY_MORE flag is set in the OpenFlow stats
959 * header of 'oh', which must be an OpenFlow stats request or reply, false if
960 * it is not set. */
961 bool
962 ofpmp_more(const struct ofp_header *oh)
963 {
964 return (ofpmp_flags(oh) & OFPSF_REPLY_MORE) != 0;
965 }
966 \f
967 static void ofpmsgs_init(void);
968
969 static const struct raw_info *
970 raw_info_get(enum ofpraw raw)
971 {
972 ofpmsgs_init();
973
974 ovs_assert(raw < ARRAY_SIZE(raw_infos));
975 return &raw_infos[raw];
976 }
977
978 static struct raw_instance *
979 raw_instance_get(const struct raw_info *info, uint8_t version)
980 {
981 ovs_assert(version >= info->min_version && version <= info->max_version);
982 return &info->instances[version - info->min_version];
983 }
984
985 static enum ofperr
986 ofpraw_from_ofphdrs(enum ofpraw *raw, const struct ofphdrs *hdrs)
987 {
988 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
989
990 struct raw_instance *raw_hdrs;
991 uint32_t hash;
992
993 ofpmsgs_init();
994
995 hash = ofphdrs_hash(hdrs);
996 HMAP_FOR_EACH_WITH_HASH (raw_hdrs, hmap_node, hash, &raw_instance_map) {
997 if (ofphdrs_equal(hdrs, &raw_hdrs->hdrs)) {
998 *raw = raw_hdrs->raw;
999 return 0;
1000 }
1001 }
1002
1003 if (!VLOG_DROP_WARN(&rl)) {
1004 struct ds s;
1005
1006 ds_init(&s);
1007 ds_put_format(&s, "version %"PRIu8", type %"PRIu8,
1008 hdrs->version, hdrs->type);
1009 if (ofphdrs_is_stat(hdrs)) {
1010 ds_put_format(&s, ", stat %"PRIu16, hdrs->stat);
1011 }
1012 if (hdrs->vendor) {
1013 ds_put_format(&s, ", vendor 0x%"PRIx32", subtype %"PRIu32,
1014 hdrs->vendor, hdrs->subtype);
1015 }
1016 VLOG_WARN("unknown OpenFlow message (%s)", ds_cstr(&s));
1017 ds_destroy(&s);
1018 }
1019
1020 return (hdrs->vendor ? OFPERR_OFPBRC_BAD_SUBTYPE
1021 : ofphdrs_is_stat(hdrs) ? OFPERR_OFPBRC_BAD_STAT
1022 : OFPERR_OFPBRC_BAD_TYPE);
1023 }
1024
1025 static void
1026 ofpmsgs_init(void)
1027 {
1028 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1029 const struct raw_info *info;
1030
1031 if (!ovsthread_once_start(&once)) {
1032 return;
1033 }
1034
1035 hmap_init(&raw_instance_map);
1036 for (info = raw_infos; info < &raw_infos[ARRAY_SIZE(raw_infos)]; info++)
1037 {
1038 int n_instances = info->max_version - info->min_version + 1;
1039 struct raw_instance *inst;
1040
1041 for (inst = info->instances;
1042 inst < &info->instances[n_instances];
1043 inst++) {
1044 inst->hdrs_len = ofphdrs_len(&inst->hdrs);
1045 hmap_insert(&raw_instance_map, &inst->hmap_node,
1046 ofphdrs_hash(&inst->hdrs));
1047 }
1048 }
1049
1050 ovsthread_once_done(&once);
1051 }