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