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