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