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