]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ofp-msgs.c
ofp-util: New function ofputil_port_to_string().
[mirror_ovs.git] / lib / ofp-msgs.c
CommitLineData
982697a4 1/*
cb22974d 2 * Copyright (c) 2012, 2013 Nicira, Inc.
982697a4
BP
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"
982697a4
BP
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 "vlog.h"
27
28VLOG_DEFINE_THIS_MODULE(ofp_msgs);
29
30#define OFPT_VENDOR 4
31#define OFPT10_STATS_REQUEST 16
32#define OFPT10_STATS_REPLY 17
33#define OFPT11_STATS_REQUEST 18
34#define OFPT11_STATS_REPLY 19
35#define OFPST_VENDOR 0xffff
36
37/* A thin abstraction of OpenFlow headers:
38 *
39 * - 'version' and 'type' come straight from struct ofp_header, so these are
40 * always present and meaningful.
41 *
42 * - 'stat' comes from the 'type' member in statistics messages only. It is
43 * meaningful, therefore, only if 'version' and 'type' taken together
44 * specify a statistics request or reply. Otherwise it is 0.
45 *
46 * - 'vendor' is meaningful only for vendor messages, that is, if 'version'
47 * and 'type' specify a vendor message or if 'version' and 'type' specify
48 * a statistics message and 'stat' specifies a vendor statistic type.
49 * Otherwise it is 0.
50 *
51 * - 'subtype' is meaningful only for vendor messages and otherwise 0. It
52 * specifies a vendor-defined subtype. There is no standard format for
53 * these but 32 bits seems like it should be enough. */
54struct ofphdrs {
55 uint8_t version; /* From ofp_header. */
56 uint8_t type; /* From ofp_header. */
57 uint16_t stat; /* From ofp10_stats_msg or ofp11_stats_msg. */
58 uint32_t vendor; /* From ofp_vendor_header,
59 * ofp10_vendor_stats_msg, or
60 * ofp11_vendor_stats_msg. */
61 uint32_t subtype; /* From nicira_header, nicira10_stats_msg, or
62 * nicira11_stats_msg. */
63};
64BUILD_ASSERT_DECL(sizeof(struct ofphdrs) == 12);
65
66/* A mapping from OpenFlow headers to OFPRAW_*. */
67struct raw_instance {
68 struct hmap_node hmap_node; /* In 'raw_instance_map'. */
69 struct ofphdrs hdrs; /* Key. */
70 enum ofpraw raw; /* Value. */
71 unsigned int hdrs_len; /* ofphdrs_len(hdrs). */
72};
73
74/* Information about a particular 'enum ofpraw'. */
75struct raw_info {
76 /* All possible instantiations of this OFPRAW_* into OpenFlow headers. */
77 struct raw_instance *instances; /* min_version - max_version + 1 elems. */
78 uint8_t min_version;
79 uint8_t max_version;
80
81 unsigned int min_body;
82 unsigned int extra_multiple;
83 enum ofptype type;
84 const char *name;
85};
86
87/* All understood OpenFlow message types, indexed by their 'struct ofphdrs'. */
88static struct hmap raw_instance_map;
89#include "ofp-msgs.inc"
90
91static ovs_be32 alloc_xid(void);
92
93/* ofphdrs functions. */
94static uint32_t ofphdrs_hash(const struct ofphdrs *);
95static bool ofphdrs_equal(const struct ofphdrs *a, const struct ofphdrs *b);
96static enum ofperr ofphdrs_decode(struct ofphdrs *,
97 const struct ofp_header *oh, size_t length);
98static void ofphdrs_decode_assert(struct ofphdrs *,
99 const struct ofp_header *oh, size_t length);
100size_t ofphdrs_len(const struct ofphdrs *);
101
102static const struct raw_info *raw_info_get(enum ofpraw);
103static struct raw_instance *raw_instance_get(const struct raw_info *,
104 uint8_t version);
105
106static enum ofperr ofpraw_from_ofphdrs(enum ofpraw *, const struct ofphdrs *);
107\f
108/* Returns a transaction ID to use for an outgoing OpenFlow message. */
109static ovs_be32
110alloc_xid(void)
111{
112 static uint32_t next_xid = 1;
113 return htonl(next_xid++);
114}
115\f
116static uint32_t
117ofphdrs_hash(const struct ofphdrs *hdrs)
118{
119 BUILD_ASSERT_DECL(sizeof *hdrs == 12);
120 return hash_words((const uint32_t *) hdrs, 3, 0);
121}
122
123static bool
124ofphdrs_equal(const struct ofphdrs *a, const struct ofphdrs *b)
125{
126 return !memcmp(a, b, sizeof *a);
127}
128
129static void
130log_bad_vendor(uint32_t vendor)
131{
132 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
133
134 VLOG_WARN_RL(&rl, "OpenFlow message has unknown vendor %#"PRIx32, vendor);
135}
136
137static enum ofperr
138ofphdrs_decode(struct ofphdrs *hdrs,
139 const struct ofp_header *oh, size_t length)
140{
141 memset(hdrs, 0, sizeof *hdrs);
142 if (length < sizeof *oh) {
143 return OFPERR_OFPBRC_BAD_LEN;
144 }
145
146 /* Get base message version and type (OFPT_*). */
147 hdrs->version = oh->version;
148 hdrs->type = oh->type;
149
150 if (hdrs->type == OFPT_VENDOR) {
151 /* Get vendor. */
152 const struct ofp_vendor_header *ovh;
153
154 if (length < sizeof *ovh) {
155 return OFPERR_OFPBRC_BAD_LEN;
156 }
157
158 ovh = (const struct ofp_vendor_header *) oh;
159 hdrs->vendor = ntohl(ovh->vendor);
160 if (hdrs->vendor == NX_VENDOR_ID) {
161 /* Get Nicira message subtype (NXT_*). */
162 const struct nicira_header *nh;
163
164 if (length < sizeof *nh) {
165 return OFPERR_OFPBRC_BAD_LEN;
166 }
167 nh = (const struct nicira_header *) oh;
168 hdrs->subtype = ntohl(nh->subtype);
169 } else {
170 log_bad_vendor(hdrs->vendor);
171 return OFPERR_OFPBRC_BAD_VENDOR;
172 }
173 } else if (hdrs->version == OFP10_VERSION
174 && (hdrs->type == OFPT10_STATS_REQUEST ||
175 hdrs->type == OFPT10_STATS_REPLY)) {
e2b9ac44 176 const struct ofp10_stats_msg *osm;
982697a4
BP
177
178 /* Get statistic type (OFPST_*). */
179 if (length < sizeof *osm) {
180 return OFPERR_OFPBRC_BAD_LEN;
181 }
e2b9ac44 182 osm = (const struct ofp10_stats_msg *) oh;
982697a4
BP
183 hdrs->stat = ntohs(osm->type);
184
185 if (hdrs->stat == OFPST_VENDOR) {
186 /* Get vendor. */
187 const struct ofp10_vendor_stats_msg *ovsm;
188
189 if (length < sizeof *ovsm) {
190 return OFPERR_OFPBRC_BAD_LEN;
191 }
192
193 ovsm = (const struct ofp10_vendor_stats_msg *) oh;
194 hdrs->vendor = ntohl(ovsm->vendor);
195 if (hdrs->vendor == NX_VENDOR_ID) {
196 /* Get Nicira statistic type (NXST_*). */
197 const struct nicira10_stats_msg *nsm;
198
199 if (length < sizeof *nsm) {
200 return OFPERR_OFPBRC_BAD_LEN;
201 }
202 nsm = (const struct nicira10_stats_msg *) oh;
203 hdrs->subtype = ntohl(nsm->subtype);
204 } else {
205 log_bad_vendor(hdrs->vendor);
206 return OFPERR_OFPBRC_BAD_VENDOR;
207 }
208 }
209 } else if (hdrs->version != OFP10_VERSION
210 && (hdrs->type == OFPT11_STATS_REQUEST ||
211 hdrs->type == OFPT11_STATS_REPLY)) {
212 const struct ofp11_stats_msg *osm;
213
214 /* Get statistic type (OFPST_*). */
215 if (length < sizeof *osm) {
216 return OFPERR_OFPBRC_BAD_LEN;
217 }
218 osm = (const struct ofp11_stats_msg *) oh;
219 hdrs->stat = ntohs(osm->type);
220
221 if (hdrs->stat == OFPST_VENDOR) {
222 /* Get vendor. */
223 const struct ofp11_vendor_stats_msg *ovsm;
224
225 if (length < sizeof *ovsm) {
226 return OFPERR_OFPBRC_BAD_LEN;
227 }
228
229 ovsm = (const struct ofp11_vendor_stats_msg *) oh;
230 hdrs->vendor = ntohl(ovsm->vendor);
231 if (hdrs->vendor == NX_VENDOR_ID) {
232 /* Get Nicira statistic type (NXST_*). */
233 const struct nicira11_stats_msg *nsm;
234
235 if (length < sizeof *nsm) {
236 return OFPERR_OFPBRC_BAD_LEN;
237 }
238 nsm = (const struct nicira11_stats_msg *) oh;
239 hdrs->subtype = ntohl(nsm->subtype);
240 } else {
241 log_bad_vendor(hdrs->vendor);
242 return OFPERR_OFPBRC_BAD_VENDOR;
243 }
244 }
245 }
246
247 return 0;
248}
249
250static void
251ofphdrs_decode_assert(struct ofphdrs *hdrs,
252 const struct ofp_header *oh, size_t length)
253{
254 enum ofperr error = ofphdrs_decode(hdrs, oh, length);
cb22974d 255 ovs_assert(!error);
982697a4
BP
256}
257
258static bool
259ofphdrs_is_stat(const struct ofphdrs *hdrs)
260{
2e3fa633
SH
261 switch ((enum ofp_version) hdrs->version) {
262 case OFP10_VERSION:
263 return (hdrs->type == OFPT10_STATS_REQUEST ||
264 hdrs->type == OFPT10_STATS_REPLY);
265 case OFP11_VERSION:
266 case OFP12_VERSION:
2e1ae200 267 case OFP13_VERSION:
2e3fa633
SH
268 return (hdrs->type == OFPT11_STATS_REQUEST ||
269 hdrs->type == OFPT11_STATS_REPLY);
270 }
271
272 return false;
982697a4
BP
273}
274
275size_t
276ofphdrs_len(const struct ofphdrs *hdrs)
277{
278 if (hdrs->type == OFPT_VENDOR) {
279 return sizeof(struct nicira_header);
280 }
281
2e3fa633
SH
282 switch ((enum ofp_version) hdrs->version) {
283 case OFP10_VERSION:
982697a4
BP
284 if (hdrs->type == OFPT10_STATS_REQUEST ||
285 hdrs->type == OFPT10_STATS_REPLY) {
286 return (hdrs->stat == OFPST_VENDOR
287 ? sizeof(struct nicira10_stats_msg)
e2b9ac44 288 : sizeof(struct ofp10_stats_msg));
982697a4 289 }
2e3fa633
SH
290 break;
291
292 case OFP11_VERSION:
293 case OFP12_VERSION:
2e1ae200 294 case OFP13_VERSION:
982697a4
BP
295 if (hdrs->type == OFPT11_STATS_REQUEST ||
296 hdrs->type == OFPT11_STATS_REPLY) {
297 return (hdrs->stat == OFPST_VENDOR
298 ? sizeof(struct nicira11_stats_msg)
299 : sizeof(struct ofp11_stats_msg));
300 }
2e3fa633 301 break;
982697a4
BP
302 }
303
304 return sizeof(struct ofp_header);
305}
306\f
307/* Determines the OFPRAW_* type of the OpenFlow message at 'oh', which has
308 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
309 * data are readable at 'oh'.) On success, returns 0 and stores the type into
310 * '*raw'. On failure, returns an OFPERR_* error code and zeros '*raw'.
311 *
312 * This function checks that 'oh' is a valid length for its particular type of
313 * message, and returns an error if not. */
314enum ofperr
315ofpraw_decode(enum ofpraw *raw, const struct ofp_header *oh)
316{
317 struct ofpbuf msg;
318
319 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
320 return ofpraw_pull(raw, &msg);
321}
322
964a5f60
BP
323/* Does the same job as ofpraw_decode(), except that it assert-fails if
324 * ofpraw_decode() would have reported an error. Thus, it's able to use the
325 * return value for the OFPRAW_* message type instead of an error code.
326 *
327 * (It only makes sense to use this function if you previously called
328 * ofpraw_decode() on the message and thus know that it's OK.) */
329enum ofpraw
330ofpraw_decode_assert(const struct ofp_header *oh)
331{
332 enum ofperr error;
333 enum ofpraw raw;
334
335 error = ofpraw_decode(&raw, oh);
336 ovs_assert(!error);
337 return raw;
338}
339
982697a4
BP
340/* Determines the OFPRAW_* type of the OpenFlow message in 'msg', which starts
341 * at 'msg->data' and has length 'msg->size' bytes. On success, returns 0 and
342 * stores the type into '*rawp'. On failure, returns an OFPERR_* error code
343 * and zeros '*rawp'.
344 *
345 * This function checks that the message has a valid length for its particular
346 * type of message, and returns an error if not.
347 *
348 * In addition to setting '*rawp', this function pulls off the OpenFlow header
349 * (including the stats headers, vendor header, and any subtype header) with
350 * ofpbuf_pull(). It also sets 'msg->l2' to the start of the OpenFlow header
351 * and 'msg->l3' just beyond the headers (that is, to the final value of
352 * msg->data). */
353enum ofperr
354ofpraw_pull(enum ofpraw *rawp, struct ofpbuf *msg)
355{
356 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
357
358 const struct raw_instance *instance;
359 const struct raw_info *info;
360 struct ofphdrs hdrs;
361
362 unsigned int min_len;
363 unsigned int len;
364
365 enum ofperr error;
366 enum ofpraw raw;
367
368 /* Set default outputs. */
369 msg->l2 = msg->l3 = msg->data;
370 *rawp = 0;
371
372 len = msg->size;
373 error = ofphdrs_decode(&hdrs, msg->data, len);
374 if (error) {
375 return error;
376 }
377
378 error = ofpraw_from_ofphdrs(&raw, &hdrs);
379 if (error) {
380 return error;
381 }
382
383 info = raw_info_get(raw);
384 instance = raw_instance_get(info, hdrs.version);
385 msg->l2 = ofpbuf_pull(msg, instance->hdrs_len);
386 msg->l3 = msg->data;
387
388 min_len = instance->hdrs_len + info->min_body;
389 switch (info->extra_multiple) {
390 case 0:
391 if (len != min_len) {
392 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (expected "
393 "length %u)", info->name, len, min_len);
394 return OFPERR_OFPBRC_BAD_LEN;
395 }
396 break;
397
398 case 1:
399 if (len < min_len) {
400 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (expected "
401 "length at least %u bytes)",
402 info->name, len, min_len);
403 return OFPERR_OFPBRC_BAD_LEN;
404 }
405 break;
406
407 default:
408 if (len < min_len || (len - min_len) % info->extra_multiple) {
409 VLOG_WARN_RL(&rl, "received %s with incorrect length %u (must be "
410 "exactly %u bytes or longer by an integer multiple "
411 "of %u bytes)",
412 info->name, len, min_len, info->extra_multiple);
413 return OFPERR_OFPBRC_BAD_LEN;
414 }
415 break;
416 }
417
418 *rawp = raw;
419 return 0;
420}
421
422/* Does the same job as ofpraw_pull(), except that it assert-fails if
e032c086 423 * ofpraw_pull() would have reported an error. Thus, it's able to use the
982697a4
BP
424 * return value for the OFPRAW_* message type instead of an error code.
425 *
426 * (It only makes sense to use this function if you previously called
e032c086 427 * ofpraw_decode() on the message and thus know that it's OK.) */
982697a4
BP
428enum ofpraw
429ofpraw_pull_assert(struct ofpbuf *msg)
430{
431 enum ofperr error;
432 enum ofpraw raw;
433
434 error = ofpraw_pull(&raw, msg);
cb22974d 435 ovs_assert(!error);
982697a4
BP
436 return raw;
437}
438
439/* Determines the OFPRAW_* type of the OpenFlow message that starts at 'oh' and
440 * has length 'length' bytes. On success, returns 0 and stores the type into
441 * '*rawp'. On failure, returns an OFPERR_* error code and zeros '*rawp'.
442 *
443 * Unlike other functions for decoding message types, this one is not picky
444 * about message length. For example, it will successfully decode a message
445 * whose body is shorter than the minimum length for a message of its type.
446 * Thus, this is the correct function to use for decoding the type of a message
447 * that might have been truncated, such as the payload of an OpenFlow error
448 * message (which is allowed to be truncated to 64 bytes). */
449enum ofperr
450ofpraw_decode_partial(enum ofpraw *raw,
451 const struct ofp_header *oh, size_t length)
452{
453 struct ofphdrs hdrs;
454 enum ofperr error;
455
456 error = ofphdrs_decode(&hdrs, oh, length);
457 if (!error) {
458 error = ofpraw_from_ofphdrs(raw, &hdrs);
459 }
460
461 if (error) {
462 *raw = 0;
463 }
464 return error;
465}
466\f
467/* Encoding messages using OFPRAW_* values. */
468
469static void ofpraw_put__(enum ofpraw, uint8_t version, ovs_be32 xid,
470 size_t extra_tailroom, struct ofpbuf *);
471
472/* Allocates and returns a new ofpbuf that contains an OpenFlow header for
473 * 'raw' with OpenFlow version 'version' and a fresh OpenFlow transaction ID.
474 * The ofpbuf has enough tailroom for the minimum body length of 'raw', plus
475 * 'extra_tailroom' additional bytes.
476 *
477 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
478 * must specify a valid (raw, version) pair.
479 *
480 * In the returned ofpbuf, 'l2' points to the beginning of the OpenFlow header
481 * and 'l3' points just after it, to where the message's body will start. The
482 * caller must actually allocate the body into the space reserved for it,
483 * e.g. with ofpbuf_put_uninit().
484 *
485 * The caller owns the returned ofpbuf and must free it when it is no longer
486 * needed, e.g. with ofpbuf_delete(). */
487struct ofpbuf *
488ofpraw_alloc(enum ofpraw raw, uint8_t version, size_t extra_tailroom)
489{
490 return ofpraw_alloc_xid(raw, version, alloc_xid(), extra_tailroom);
491}
492
493/* Same as ofpraw_alloc() but the caller provides the transaction ID. */
494struct ofpbuf *
495ofpraw_alloc_xid(enum ofpraw raw, uint8_t version, ovs_be32 xid,
496 size_t extra_tailroom)
497{
498 struct ofpbuf *buf = ofpbuf_new(0);
499 ofpraw_put__(raw, version, xid, extra_tailroom, buf);
500 return buf;
501}
502
503/* Same as ofpraw_alloc(), but obtains the OpenFlow version and transaction ID
504 * from 'request->version' and 'request->xid', respectively.
505 *
506 * Even though the version comes from 'request->version', the caller must still
507 * know what it is doing, by specifying a valid pairing of 'raw' and
508 * 'request->version', just like ofpraw_alloc(). */
509struct ofpbuf *
510ofpraw_alloc_reply(enum ofpraw raw, const struct ofp_header *request,
511 size_t extra_tailroom)
512{
513 return ofpraw_alloc_xid(raw, request->version, request->xid,
514 extra_tailroom);
515}
516
517/* Allocates and returns a new ofpbuf that contains an OpenFlow header that is
518 * a stats reply to the stats request in 'request', using the same OpenFlow
519 * version and transaction ID as 'request'. The ofpbuf has enough tailroom for
520 * the stats reply's minimum body length, plus 'extra_tailroom' additional
521 * bytes.
522 *
523 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
524 * value. Every stats request has a corresponding reply, so the (raw, version)
525 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
526 *
527 * In the returned ofpbuf, 'l2' points to the beginning of the OpenFlow header
528 * and 'l3' points just after it, to where the message's body will start. The
529 * caller must actually allocate the body into the space reserved for it,
530 * e.g. with ofpbuf_put_uninit().
531 *
532 * The caller owns the returned ofpbuf and must free it when it is no longer
533 * needed, e.g. with ofpbuf_delete(). */
534struct ofpbuf *
535ofpraw_alloc_stats_reply(const struct ofp_header *request,
536 size_t extra_tailroom)
537{
538 enum ofpraw request_raw;
539 enum ofpraw reply_raw;
540 enum ofperr error;
541
542 error = ofpraw_decode_partial(&request_raw, request,
543 ntohs(request->length));
cb22974d 544 ovs_assert(!error);
982697a4
BP
545
546 reply_raw = ofpraw_stats_request_to_reply(request_raw, request->version);
cb22974d 547 ovs_assert(reply_raw);
982697a4
BP
548
549 return ofpraw_alloc_reply(reply_raw, request, extra_tailroom);
550}
551
552/* Appends to 'buf' an OpenFlow header for 'raw' with OpenFlow version
553 * 'version' and a fresh OpenFlow transaction ID. Preallocates enough tailroom
554 * in 'buf' for the minimum body length of 'raw', plus 'extra_tailroom'
555 * additional bytes.
556 *
557 * Each 'raw' value is valid only for certain OpenFlow versions. The caller
558 * must specify a valid (raw, version) pair.
559 *
560 * Upon return, 'buf->l2' points to the beginning of the OpenFlow header and
561 * 'buf->l3' points just after it, to where the message's body will start. The
562 * caller must actually allocating the body into the space reserved for it,
563 * e.g. with ofpbuf_put_uninit(). */
564void
565ofpraw_put(enum ofpraw raw, uint8_t version, struct ofpbuf *buf)
566{
567 ofpraw_put__(raw, version, alloc_xid(), 0, buf);
568}
569
570/* Same as ofpraw_put() but the caller provides the transaction ID. */
571void
572ofpraw_put_xid(enum ofpraw raw, uint8_t version, ovs_be32 xid,
573 struct ofpbuf *buf)
574{
575 ofpraw_put__(raw, version, xid, 0, buf);
576}
577
578/* Same as ofpraw_put(), but obtains the OpenFlow version and transaction ID
579 * from 'request->version' and 'request->xid', respectively.
580 *
581 * Even though the version comes from 'request->version', the caller must still
582 * know what it is doing, by specifying a valid pairing of 'raw' and
583 * 'request->version', just like ofpraw_put(). */
584void
585ofpraw_put_reply(enum ofpraw raw, const struct ofp_header *request,
586 struct ofpbuf *buf)
587{
588 ofpraw_put__(raw, request->version, request->xid, 0, buf);
589}
590
591/* Appends to 'buf' an OpenFlow header that is a stats reply to the stats
592 * request in 'request', using the same OpenFlow version and transaction ID as
593 * 'request'. Preallocate enough tailroom in 'buf for the stats reply's
594 * minimum body length, plus 'extra_tailroom' additional bytes.
595 *
596 * 'request' must be a stats request, that is, an OFPRAW_OFPST* or OFPRAW_NXST*
597 * value. Every stats request has a corresponding reply, so the (raw, version)
598 * pairing pitfalls of the other ofpraw_alloc_*() functions don't apply here.
599 *
600 * In the returned ofpbuf, 'l2' points to the beginning of the OpenFlow header
601 * and 'l3' points just after it, to where the message's body will start. The
602 * caller must actually allocate the body into the space reserved for it,
603 * e.g. with ofpbuf_put_uninit().
604 *
605 * The caller owns the returned ofpbuf and must free it when it is no longer
606 * needed, e.g. with ofpbuf_delete(). */
607void
608ofpraw_put_stats_reply(const struct ofp_header *request, struct ofpbuf *buf)
609{
610 enum ofperr error;
611 enum ofpraw raw;
612
613 error = ofpraw_decode_partial(&raw, request, ntohs(request->length));
cb22974d 614 ovs_assert(!error);
982697a4
BP
615
616 raw = ofpraw_stats_request_to_reply(raw, request->version);
cb22974d 617 ovs_assert(raw);
982697a4
BP
618
619 ofpraw_put__(raw, request->version, request->xid, 0, buf);
620}
621
622static void
623ofpraw_put__(enum ofpraw raw, uint8_t version, ovs_be32 xid,
624 size_t extra_tailroom, struct ofpbuf *buf)
625{
626 const struct raw_info *info = raw_info_get(raw);
627 const struct raw_instance *instance = raw_instance_get(info, version);
628 const struct ofphdrs *hdrs = &instance->hdrs;
629 struct ofp_header *oh;
630
631 ofpbuf_prealloc_tailroom(buf, (instance->hdrs_len + info->min_body
632 + extra_tailroom));
633 buf->l2 = ofpbuf_put_uninit(buf, instance->hdrs_len);
634 buf->l3 = ofpbuf_tail(buf);
635
636 oh = buf->l2;
637 oh->version = version;
638 oh->type = hdrs->type;
639 oh->length = htons(buf->size);
640 oh->xid = xid;
641
642 if (hdrs->type == OFPT_VENDOR) {
643 struct nicira_header *nh = buf->l2;
644
cb22974d 645 ovs_assert(hdrs->vendor == NX_VENDOR_ID);
982697a4
BP
646 nh->vendor = htonl(hdrs->vendor);
647 nh->subtype = htonl(hdrs->subtype);
648 } else if (version == OFP10_VERSION
649 && (hdrs->type == OFPT10_STATS_REQUEST ||
650 hdrs->type == OFPT10_STATS_REPLY)) {
e2b9ac44 651 struct ofp10_stats_msg *osm = buf->l2;
982697a4
BP
652
653 osm->type = htons(hdrs->stat);
654 osm->flags = htons(0);
655
656 if (hdrs->stat == OFPST_VENDOR) {
657 struct ofp10_vendor_stats_msg *ovsm = buf->l2;
658
659 ovsm->vendor = htonl(hdrs->vendor);
660 if (hdrs->vendor == NX_VENDOR_ID) {
661 struct nicira10_stats_msg *nsm = buf->l2;
662
663 nsm->subtype = htonl(hdrs->subtype);
664 memset(nsm->pad, 0, sizeof nsm->pad);
665 } else {
666 NOT_REACHED();
667 }
668 }
669 } else if (version != OFP10_VERSION
670 && (hdrs->type == OFPT11_STATS_REQUEST ||
671 hdrs->type == OFPT11_STATS_REPLY)) {
672 struct ofp11_stats_msg *osm = buf->l2;
673
674 osm->type = htons(hdrs->stat);
675 osm->flags = htons(0);
676 memset(osm->pad, 0, sizeof osm->pad);
677
678 if (hdrs->stat == OFPST_VENDOR) {
679 struct ofp11_vendor_stats_msg *ovsm = buf->l2;
680
681 ovsm->vendor = htonl(hdrs->vendor);
682 if (hdrs->vendor == NX_VENDOR_ID) {
683 struct nicira11_stats_msg *nsm = buf->l2;
684
685 nsm->subtype = htonl(hdrs->subtype);
686 } else {
687 NOT_REACHED();
688 }
689 }
690 }
691}
692\f
693/* Returns 'raw''s name.
694 *
695 * The name is the name used for 'raw' in the OpenFlow specification. For
696 * example, ofpraw_get_name(OFPRAW_OFPT10_FEATURES_REPLY) is
697 * "OFPT_FEATURES_REPLY".
698 *
699 * The caller must not modify or free the returned string. */
700const char *
701ofpraw_get_name(enum ofpraw raw)
702{
703 return raw_info_get(raw)->name;
704}
705
706/* Returns the stats reply that corresponds to 'raw' in the given OpenFlow
707 * 'version'. */
708enum ofpraw
709ofpraw_stats_request_to_reply(enum ofpraw raw, uint8_t version)
710{
711 const struct raw_info *info = raw_info_get(raw);
712 const struct raw_instance *instance = raw_instance_get(info, version);
713 enum ofpraw reply_raw;
714 struct ofphdrs hdrs;
715 enum ofperr error;
716
717 hdrs = instance->hdrs;
2e3fa633
SH
718 switch ((enum ofp_version)hdrs.version) {
719 case OFP10_VERSION:
cb22974d 720 ovs_assert(hdrs.type == OFPT10_STATS_REQUEST);
982697a4 721 hdrs.type = OFPT10_STATS_REPLY;
2e3fa633
SH
722 break;
723 case OFP11_VERSION:
724 case OFP12_VERSION:
2e1ae200 725 case OFP13_VERSION:
cb22974d 726 ovs_assert(hdrs.type == OFPT11_STATS_REQUEST);
982697a4 727 hdrs.type = OFPT11_STATS_REPLY;
2e3fa633
SH
728 break;
729 default:
730 NOT_REACHED();
982697a4
BP
731 }
732
733 error = ofpraw_from_ofphdrs(&reply_raw, &hdrs);
cb22974d 734 ovs_assert(!error);
982697a4
BP
735
736 return reply_raw;
737}
738\f
739/* Determines the OFPTYPE_* type of the OpenFlow message at 'oh', which has
740 * length 'oh->length'. (The caller must ensure that 'oh->length' bytes of
741 * data are readable at 'oh'.) On success, returns 0 and stores the type into
742 * '*typep'. On failure, returns an OFPERR_* error code and zeros '*typep'.
743 *
744 * This function checks that 'oh' is a valid length for its particular type of
745 * message, and returns an error if not. */
746enum ofperr
747ofptype_decode(enum ofptype *typep, const struct ofp_header *oh)
748{
749 enum ofperr error;
750 enum ofpraw raw;
751
752 error = ofpraw_decode(&raw, oh);
753 *typep = error ? 0 : ofptype_from_ofpraw(raw);
754 return error;
755}
756
757/* Determines the OFPTYPE_* type of the OpenFlow message in 'msg', which starts
758 * at 'msg->data' and has length 'msg->size' bytes. On success, returns 0 and
759 * stores the type into '*typep'. On failure, returns an OFPERR_* error code
760 * and zeros '*typep'.
761 *
762 * This function checks that the message has a valid length for its particular
763 * type of message, and returns an error if not.
764 *
765 * In addition to setting '*typep', this function pulls off the OpenFlow header
766 * (including the stats headers, vendor header, and any subtype header) with
767 * ofpbuf_pull(). It also sets 'msg->l2' to the start of the OpenFlow header
768 * and 'msg->l3' just beyond the headers (that is, to the final value of
769 * msg->data). */
770enum ofperr
771ofptype_pull(enum ofptype *typep, struct ofpbuf *buf)
772{
773 enum ofperr error;
774 enum ofpraw raw;
775
776 error = ofpraw_pull(&raw, buf);
777 *typep = error ? 0 : ofptype_from_ofpraw(raw);
778 return error;
779}
780
781/* Returns the OFPTYPE_* type that corresponds to 'raw'.
782 *
783 * (This is a one-way trip, because the mapping from ofpraw to ofptype is
784 * many-to-one.) */
785enum ofptype
786ofptype_from_ofpraw(enum ofpraw raw)
787{
788 return raw_info_get(raw)->type;
789}
790\f
791/* Updates the 'length' field of the OpenFlow message in 'buf' to
792 * 'buf->size'. */
793void
794ofpmsg_update_length(struct ofpbuf *buf)
795{
796 struct ofp_header *oh = ofpbuf_at_assert(buf, 0, sizeof *oh);
797 oh->length = htons(buf->size);
798}
799
800/* Returns just past the Openflow header (including the stats headers, vendor
801 * header, and any subtype header) in 'oh'. */
802const void *
803ofpmsg_body(const struct ofp_header *oh)
804{
805 struct ofphdrs hdrs;
806
807 ofphdrs_decode_assert(&hdrs, oh, ntohs(oh->length));
808 return (const uint8_t *) oh + ofphdrs_len(&hdrs);
809}
810\f
79b8c36c
BP
811static ovs_be16 *ofpmp_flags__(const struct ofp_header *);
812
982697a4
BP
813/* Initializes 'replies' as a new list of stats messages that reply to
814 * 'request', which must be a stats request message. Initially the list will
815 * consist of only a single reply part without any body. The caller should
816 * use calls to the other ofpmp_*() functions to add to the body and split the
817 * message into multiple parts, if necessary. */
818void
819ofpmp_init(struct list *replies, const struct ofp_header *request)
820{
821 struct ofpbuf *msg;
822
823 list_init(replies);
824
825 msg = ofpraw_alloc_stats_reply(request, 1000);
826 list_push_back(replies, &msg->list_node);
827}
828
829/* Prepares to append up to 'len' bytes to the series of statistics replies in
830 * 'replies', which should have been initialized with ofpmp_init(), if
831 * necessary adding a new reply to the list.
832 *
833 * Returns an ofpbuf with at least 'len' bytes of tailroom. The 'len' bytes
834 * have not actually been allocated, so the caller must do so with
835 * e.g. ofpbuf_put_uninit(). */
836struct ofpbuf *
837ofpmp_reserve(struct list *replies, size_t len)
838{
839 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
840
841 if (msg->size + len <= UINT16_MAX) {
842 ofpbuf_prealloc_tailroom(msg, len);
843 return msg;
844 } else {
845 unsigned int hdrs_len;
846 struct ofpbuf *next;
847 struct ofphdrs hdrs;
848
849 ofphdrs_decode_assert(&hdrs, msg->data, msg->size);
850 hdrs_len = ofphdrs_len(&hdrs);
851
852 next = ofpbuf_new(MAX(1024, hdrs_len + len));
853 ofpbuf_put(next, msg->data, hdrs_len);
c0bf00b9
BP
854 next->l2 = next->data;
855 next->l3 = ofpbuf_tail(next);
982697a4
BP
856 list_push_back(replies, &next->list_node);
857
79b8c36c
BP
858 *ofpmp_flags__(msg->data) |= htons(OFPSF_REPLY_MORE);
859
982697a4
BP
860 return next;
861 }
862}
863
864/* Appends 'len' bytes to the series of statistics replies in 'replies', and
865 * returns the first byte. */
866void *
867ofpmp_append(struct list *replies, size_t len)
868{
869 return ofpbuf_put_uninit(ofpmp_reserve(replies, len), len);
870}
871
872/* Sometimes, when composing stats replies, it's difficult to predict how long
873 * an individual reply chunk will be before actually encoding it into the reply
874 * buffer. This function allows easy handling of this case: just encode the
875 * reply, then use this function to break the message into two pieces if it
876 * exceeds the OpenFlow message limit.
877 *
878 * In detail, if the final stats message in 'replies' is too long for OpenFlow,
879 * this function breaks it into two separate stats replies, the first one with
880 * the first 'start_ofs' bytes, the second one containing the bytes from that
881 * offset onward. */
882void
883ofpmp_postappend(struct list *replies, size_t start_ofs)
884{
885 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
886
cb22974d 887 ovs_assert(start_ofs <= UINT16_MAX);
982697a4
BP
888 if (msg->size > UINT16_MAX) {
889 size_t len = msg->size - start_ofs;
890 memcpy(ofpmp_append(replies, len),
891 (const uint8_t *) msg->data + start_ofs, len);
892 msg->size = start_ofs;
893 }
894}
895
896static ovs_be16 *
897ofpmp_flags__(const struct ofp_header *oh)
898{
2e3fa633
SH
899 switch ((enum ofp_version)oh->version) {
900 case OFP10_VERSION:
901 return &((struct ofp10_stats_msg *) oh)->flags;
902 case OFP11_VERSION:
903 case OFP12_VERSION:
2e1ae200 904 case OFP13_VERSION:
2e3fa633
SH
905 return &((struct ofp11_stats_msg *) oh)->flags;
906 default:
907 NOT_REACHED();
908 }
982697a4
BP
909}
910
911/* Returns the OFPSF_* flags found in the OpenFlow stats header of 'oh', which
912 * must be an OpenFlow stats request or reply.
913 *
914 * (OFPSF_REPLY_MORE is the only defined flag.) */
915uint16_t
916ofpmp_flags(const struct ofp_header *oh)
917{
918 return ntohs(*ofpmp_flags__(oh));
919}
920
921/* Returns true if the OFPSF_REPLY_MORE flag is set in the OpenFlow stats
922 * header of 'oh', which must be an OpenFlow stats request or reply, false if
923 * it is not set. */
924bool
925ofpmp_more(const struct ofp_header *oh)
926{
927 return (ofpmp_flags(oh) & OFPSF_REPLY_MORE) != 0;
928}
929\f
930static void ofpmsgs_init(void);
931
932static const struct raw_info *
933raw_info_get(enum ofpraw raw)
934{
935 ofpmsgs_init();
936
cb22974d 937 ovs_assert(raw < ARRAY_SIZE(raw_infos));
982697a4
BP
938 return &raw_infos[raw];
939}
940
941static struct raw_instance *
942raw_instance_get(const struct raw_info *info, uint8_t version)
943{
cb22974d 944 ovs_assert(version >= info->min_version && version <= info->max_version);
982697a4
BP
945 return &info->instances[version - info->min_version];
946}
947
948static enum ofperr
949ofpraw_from_ofphdrs(enum ofpraw *raw, const struct ofphdrs *hdrs)
950{
951 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
952
953 struct raw_instance *raw_hdrs;
954 uint32_t hash;
955
956 ofpmsgs_init();
957
958 hash = ofphdrs_hash(hdrs);
959 HMAP_FOR_EACH_WITH_HASH (raw_hdrs, hmap_node, hash, &raw_instance_map) {
960 if (ofphdrs_equal(hdrs, &raw_hdrs->hdrs)) {
961 *raw = raw_hdrs->raw;
962 return 0;
963 }
964 }
965
966 if (!VLOG_DROP_WARN(&rl)) {
967 struct ds s;
968
969 ds_init(&s);
970 ds_put_format(&s, "version %"PRIu8", type %"PRIu8,
971 hdrs->version, hdrs->type);
972 if (ofphdrs_is_stat(hdrs)) {
973 ds_put_format(&s, ", stat %"PRIu16, hdrs->stat);
974 }
975 if (hdrs->vendor) {
976 ds_put_format(&s, ", vendor 0x%"PRIx32", subtype %"PRIu32,
977 hdrs->vendor, hdrs->subtype);
978 }
979 VLOG_WARN("unknown OpenFlow message (%s)", ds_cstr(&s));
980 ds_destroy(&s);
981 }
982
983 return (hdrs->vendor ? OFPERR_OFPBRC_BAD_SUBTYPE
984 : ofphdrs_is_stat(hdrs) ? OFPERR_OFPBRC_BAD_STAT
985 : OFPERR_OFPBRC_BAD_TYPE);
986}
987
988static void
989ofpmsgs_init(void)
990{
991 const struct raw_info *info;
992
993 if (raw_instance_map.buckets) {
994 return;
995 }
996
997 hmap_init(&raw_instance_map);
998 for (info = raw_infos; info < &raw_infos[ARRAY_SIZE(raw_infos)]; info++)
999 {
1000 int n_instances = info->max_version - info->min_version + 1;
1001 struct raw_instance *inst;
1002
1003 for (inst = info->instances;
1004 inst < &info->instances[n_instances];
1005 inst++) {
1006 inst->hdrs_len = ofphdrs_len(&inst->hdrs);
1007 hmap_insert(&raw_instance_map, &inst->hmap_node,
1008 ofphdrs_hash(&inst->hdrs));
1009 }
1010 }
1011}