sset: Make sset iteration check the type of the nodes it's iterating.
[cascardo/ovs.git] / lib / ofp-prop.c
1 /*
2  * Copyright (c) 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
19 #include "ofp-prop.h"
20
21 #include "byte-order.h"
22 #include "ofpbuf.h"
23 #include "ofp-errors.h"
24 #include "openvswitch/vlog.h"
25 #include "util.h"
26 #include "uuid.h"
27
28 struct ofp_prop_be16 {
29     ovs_be16 type;
30     ovs_be16 len;
31     ovs_be16 value;
32     uint8_t pad[2];
33 };
34 BUILD_ASSERT_DECL(sizeof(struct ofp_prop_be16) == 8);
35
36 struct ofp_prop_be32 {
37     ovs_be16 type;
38     ovs_be16 len;
39     ovs_be32 value;
40 };
41 BUILD_ASSERT_DECL(sizeof(struct ofp_prop_be32) == 8);
42
43 static uint32_t
44 ofpprop_type_to_exp_id(uint64_t type)
45 {
46     return type >> 32;
47 }
48
49 static uint32_t
50 ofpprop_type_to_exp_type(uint64_t type)
51 {
52     return type & UINT32_MAX;
53 }
54
55 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
56  * of 'msg'.  Stores the type of the property in '*typep' and, if 'property' is
57  * nonnull, the entire property, including the header, in '*property'.  Points
58  * 'property->header' to the property header (which could be ofp_prop_header or
59  * ofp_prop_experimenter) and 'property->msg' to just past it.  Returns 0 if
60  * successful, otherwise an OpenFlow error code.
61  *
62  * This function treats property types 'min_exp' and larger as introducing
63  * experimenter properties.  For most kinds of properties, 0xffff is the
64  * appropriate value for 'min_exp', because 0xffff is the only property type
65  * used for experimenters, but async config properties also use 0xfffe.  Use
66  * 0x10000 (or higher) if experimenter properties are not supported.
67  *
68  * This function pulls the property's stated size padded out to a multiple of
69  * 'alignment' bytes.  The common case in OpenFlow is an 'alignment' of 8, so
70  * you can use ofpprop_pull() for that case. */
71 enum ofperr
72 ofpprop_pull__(struct ofpbuf *msg, struct ofpbuf *property,
73                unsigned int alignment, unsigned int min_exp,
74                uint64_t *typep)
75 {
76     struct ofp_prop_header *oph;
77     unsigned int padded_len;
78     unsigned int len;
79
80     if (msg->size < sizeof *oph) {
81         return OFPERR_OFPBPC_BAD_LEN;
82     }
83
84     oph = msg->data;
85     len = ntohs(oph->len);
86     padded_len = ROUND_UP(len, alignment);
87     if (len < sizeof *oph || padded_len > msg->size) {
88         return OFPERR_OFPBPC_BAD_LEN;
89     }
90
91     uint16_t type = ntohs(oph->type);
92     if (type < min_exp) {
93         *typep = type;
94     } else {
95         struct ofp_prop_experimenter *ope = msg->data;
96         if (len < sizeof *ope) {
97             return OFPERR_OFPBPC_BAD_LEN;
98         }
99
100         if (!ope->experimenter) {
101             /* Reject experimenter 0 because it yields ambiguity with standard
102              * property types. */
103             return OFPERR_OFPBPC_BAD_EXPERIMENTER;
104         }
105
106         *typep = OFPPROP_EXP(ntohl(ope->experimenter), ntohl(ope->exp_type));
107     }
108
109     if (property) {
110         ofpbuf_use_const(property, msg->data, len);
111         property->header = property->data;
112         property->msg = ((uint8_t *) property->data
113                          + (type < min_exp
114                             ? sizeof(struct ofp_prop_header)
115                             : sizeof(struct ofp_prop_experimenter)));
116     }
117     ofpbuf_pull(msg, padded_len);
118     return 0;
119 }
120
121 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
122  * of 'msg'.  Stores the type of the property in '*typep' and, if 'property' is
123  * nonnull, the entire property, including the header, in '*property'.  Points
124  * 'property->header' to the property header (which could be ofp_prop_header or
125  * ofp_prop_experimenter) and 'property->msg' to just past it.  Returns 0 if
126  * successful, otherwise an error code.
127  *
128  * This function treats property type 0xffff as introducing an experimenter
129  * property.  Use ofpprop_pull__() instead if some other behavior is needed.
130  *
131  * This function pulls the property's stated size padded out to a multiple of 8
132  * bytes, which is the common case for OpenFlow properties.  Use
133  * ofpprop_pull__() instead if some other behavior is needed.*/
134 enum ofperr
135 ofpprop_pull(struct ofpbuf *msg, struct ofpbuf *property, uint64_t *typep)
136 {
137     return ofpprop_pull__(msg, property, 8, 0xffff, typep);
138 }
139
140 /* Attempts to parse 'property' as a property containing a 16-bit value.  If
141  * successful, stores the value into '*value' and returns 0; otherwise returns
142  * an OpenFlow error. */
143 enum ofperr
144 ofpprop_parse_be16(const struct ofpbuf *property, ovs_be16 *value)
145 {
146     /* OpenFlow uses 8-byte properties for 16-bit values, which doesn't really
147      * make sense.  Be forgiving by allowing any size payload as long as it's
148      * at least big enough.  */
149     ovs_be16 *p = property->msg;
150     if (ofpbuf_msgsize(property) < sizeof *p) {
151         return OFPERR_OFPBPC_BAD_LEN;
152     }
153     *value = *p;
154     return 0;
155 }
156
157 /* Attempts to parse 'property' as a property containing a 32-bit value.  If
158  * successful, stores the value into '*value' and returns 0; otherwise returns
159  * an OpenFlow error. */
160 enum ofperr
161 ofpprop_parse_be32(const struct ofpbuf *property, ovs_be32 *value)
162 {
163     ovs_be32 *p = property->msg;
164     if (ofpbuf_msgsize(property) != sizeof *p) {
165         return OFPERR_OFPBPC_BAD_LEN;
166     }
167     *value = *p;
168     return 0;
169 }
170
171 /* Attempts to parse 'property' as a property containing a 64-bit value.  If
172  * successful, stores the value into '*value' and returns 0; otherwise returns
173  * an OpenFlow error. */
174 enum ofperr
175 ofpprop_parse_be64(const struct ofpbuf *property, ovs_be64 *value)
176 {
177     ovs_be64 *p;
178     size_t be64_offset = ROUND_UP(ofpbuf_headersize(property), 8);
179     if (property->size != be64_offset + sizeof *p) {
180         return OFPERR_OFPBPC_BAD_LEN;
181     }
182
183     p = ALIGNED_CAST(ovs_be64 *, (char *) property->data + be64_offset);
184     *value = *p;
185     return 0;
186 }
187
188 /* Attempts to parse 'property' as a property containing a 8-bit value.  If
189  * successful, stores the value into '*value' and returns 0; otherwise returns
190  * an OpenFlow error. */
191 enum ofperr
192 ofpprop_parse_u8(const struct ofpbuf *property, uint8_t *value)
193 {
194     /* OpenFlow 1.5 and earlier don't have any 8-bit properties, but it uses
195      * 8-byte properties for 16-bit values, which doesn't really make sense.
196      * Be forgiving by allowing any size payload as long as it's at least big
197      * enough. */
198     uint8_t *p = property->msg;
199     if (ofpbuf_msgsize(property) < sizeof *p) {
200         return OFPERR_OFPBPC_BAD_LEN;
201     }
202     *value = *p;
203     return 0;
204 }
205
206 /* Attempts to parse 'property' as a property containing a 16-bit value.  If
207  * successful, stores the value into '*value' and returns 0; otherwise returns
208  * an OpenFlow error. */
209 enum ofperr
210 ofpprop_parse_u16(const struct ofpbuf *property, uint16_t *value)
211 {
212     /* OpenFlow uses 8-byte properties for 16-bit values, which doesn't really
213      * make sense.  Be forgiving by allowing any size payload as long as it's
214      * at least big enough.  */
215     ovs_be16 *p = property->msg;
216     if (ofpbuf_msgsize(property) < sizeof *p) {
217         return OFPERR_OFPBPC_BAD_LEN;
218     }
219     *value = ntohs(*p);
220     return 0;
221 }
222
223 /* Attempts to parse 'property' as a property containing a 32-bit value.  If
224  * successful, stores the value into '*value' and returns 0; otherwise returns
225  * an OpenFlow error. */
226 enum ofperr
227 ofpprop_parse_u32(const struct ofpbuf *property, uint32_t *value)
228 {
229     ovs_be32 *p = property->msg;
230     if (ofpbuf_msgsize(property) != sizeof *p) {
231         return OFPERR_OFPBPC_BAD_LEN;
232     }
233     *value = ntohl(*p);
234     return 0;
235 }
236
237 /* Attempts to parse 'property' as a property containing a 64-bit value.  If
238  * successful, stores the value into '*value' and returns 0; otherwise returns
239  * an OpenFlow error. */
240 enum ofperr
241 ofpprop_parse_u64(const struct ofpbuf *property, uint64_t *value)
242 {
243     ovs_be64 *p;
244     size_t be64_offset = ROUND_UP(ofpbuf_headersize(property), 8);
245     if (property->size != be64_offset + sizeof *p) {
246         return OFPERR_OFPBPC_BAD_LEN;
247     }
248
249     p = ALIGNED_CAST(ovs_be64 *, (char *) property->data + be64_offset);
250     *value = ntohll(*p);
251     return 0;
252 }
253
254 /* Attempts to parse 'property' as a property containing a UUID.  If
255  * successful, stores the value into '*uuid' and returns 0; otherwise returns
256  * an OpenFlow error. */
257 enum ofperr
258 ofpprop_parse_uuid(const struct ofpbuf *property, struct uuid *uuid)
259 {
260     struct uuid *p = property->msg;
261     if (ofpbuf_msgsize(property) != sizeof *p) {
262         return OFPERR_OFPBPC_BAD_LEN;
263     }
264     *uuid = *p;
265     return 0;
266 }
267
268 /* Adds a property with the given 'type' and 'len'-byte contents 'value' to
269  * 'msg', padding the property out to a multiple of 8 bytes. */
270 void
271 ofpprop_put(struct ofpbuf *msg, uint64_t type, const void *value, size_t len)
272 {
273     size_t start_ofs = ofpprop_start(msg, type);
274     ofpbuf_put(msg, value, len);
275     ofpprop_end(msg, start_ofs);
276 }
277
278 /* Adds a property with the given 'type' to 'msg', consisting of a struct
279  * ofp_prop_header or ofp_prop_experimenter followed by enough zero bytes to
280  * total 'len' bytes, followed by padding to bring the property up to a
281  * multiple of 8 bytes.  Returns the property header. */
282 void *
283 ofpprop_put_zeros(struct ofpbuf *msg, uint64_t type, size_t len)
284 {
285     void *header = ofpbuf_put_zeros(msg, ROUND_UP(len, 8));
286     if (!ofpprop_is_experimenter(type)) {
287         struct ofp_prop_header *oph = header;
288         oph->type = htons(type);
289         oph->len = htons(len);
290     } else {
291         struct ofp_prop_experimenter *ope = header;
292         ope->type = htons(0xffff);
293         ope->len = htons(len);
294         ope->experimenter = htonl(ofpprop_type_to_exp_id(type));
295         ope->exp_type = htonl(ofpprop_type_to_exp_type(type));
296     }
297     return header;
298 }
299
300 /* Adds a property with the given 'type' and 16-bit 'value' to 'msg'. */
301 void
302 ofpprop_put_be16(struct ofpbuf *msg, uint64_t type, ovs_be16 value)
303 {
304     if (!ofpprop_is_experimenter(type)) {
305         /* The OpenFlow specs consistently (at least they're consistent!)  give
306          * properties with a 16-bit integer value a length of 8, not 6, so add
307          * two bytes of padding.  */
308         ovs_be16 padded_value[2] = { value, 0 };
309         ofpprop_put(msg, type, padded_value, sizeof padded_value);
310     } else {
311         /* There's no precedent but let's assume that this is generally done
312          * sanely. */
313         ofpprop_put(msg, type, &value, sizeof value);
314     }
315 }
316
317 /* Adds a property with the given 'type' and 32-bit 'value' to 'msg'. */
318 void
319 ofpprop_put_be32(struct ofpbuf *msg, uint64_t type, ovs_be32 value)
320 {
321     ofpprop_put(msg, type, &value, sizeof value);
322 }
323
324 /* Adds a property with the given 'type' and 64-bit 'value' to 'msg'. */
325 void
326 ofpprop_put_be64(struct ofpbuf *msg, uint64_t type, ovs_be64 value)
327 {
328     size_t start = ofpprop_start(msg, type);
329     ofpbuf_put_zeros(msg, 4);
330     ofpbuf_put(msg, &value, sizeof value);
331     ofpprop_end(msg, start);
332 }
333
334 /* Adds a property with the given 'type' and 8-bit 'value' to 'msg'. */
335 void
336 ofpprop_put_u8(struct ofpbuf *msg, uint64_t type, uint8_t value)
337 {
338     /* There's no precedent for 8-bit properties in OpenFlow 1.5 and earlier
339      * but let's assume they're done sanely. */
340     ofpprop_put(msg, type, &value, 1);
341 }
342
343 /* Adds a property with the given 'type' and 16-bit 'value' to 'msg'. */
344 void
345 ofpprop_put_u16(struct ofpbuf *msg, uint64_t type, uint16_t value)
346 {
347     ofpprop_put_be16(msg, type, htons(value));
348 }
349
350 /* Adds a property with the given 'type' and 32-bit 'value' to 'msg'. */
351 void
352 ofpprop_put_u32(struct ofpbuf *msg, uint64_t type, uint32_t value)
353 {
354     ofpprop_put_be32(msg, type, htonl(value));
355 }
356
357 /* Adds a property with the given 'type' and 64-bit 'value' to 'msg'. */
358 void
359 ofpprop_put_u64(struct ofpbuf *msg, uint64_t type, uint64_t value)
360 {
361     ofpprop_put_be64(msg, type, htonll(value));
362 }
363
364 /* Appends a property to 'msg' whose type is 'type' and whose contents is a
365  * series of property headers, one for each 1-bit in 'bitmap'. */
366 void
367 ofpprop_put_bitmap(struct ofpbuf *msg, uint64_t type, uint64_t bitmap)
368 {
369     size_t start_ofs = ofpprop_start(msg, type);
370
371     for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
372         ofpprop_start(msg, rightmost_1bit_idx(bitmap));
373     }
374     ofpprop_end(msg, start_ofs);
375 }
376
377 /* Appends a content-free property with the given 'type' to 'msg'.
378  *
379  * (The idea is that the presence of the property acts as a flag.) */
380 void
381 ofpprop_put_flag(struct ofpbuf *msg, uint64_t type)
382 {
383     size_t start = ofpprop_start(msg, type);
384     ofpprop_end(msg, start);
385 }
386
387 /* Appends a property to 'msg' with the given 'type' and 'uuid' as its
388  * value. */
389 void
390 ofpprop_put_uuid(struct ofpbuf *msg, uint64_t type, const struct uuid *uuid)
391 {
392     ofpprop_put(msg, type, uuid, sizeof *uuid);
393 }
394
395 /* Appends a header for a property of type 'type' to 'msg'.  The caller should
396  * add the contents of the property to 'msg', then finish it by calling
397  * ofpprop_end().  Returns the offset of the beginning of the property (to pass
398  * to ofpprop_end() later). */
399 size_t
400 ofpprop_start(struct ofpbuf *msg, uint64_t type)
401 {
402     size_t start_ofs = msg->size;
403     if (!ofpprop_is_experimenter(type)) {
404         struct ofp_prop_header *oph = ofpbuf_put_uninit(msg, sizeof *oph);
405         oph->type = htons(type);
406         oph->len = htons(4);
407     } else {
408         struct ofp_prop_experimenter *ope
409             = ofpbuf_put_uninit(msg, sizeof *ope);
410         ope->type = htons(0xffff);
411         ope->len = htons(12);
412         ope->experimenter = htonl(ofpprop_type_to_exp_id(type));
413         ope->exp_type = htonl(ofpprop_type_to_exp_type(type));
414     }
415     return start_ofs;
416 }
417
418 /* Finishes serializing a property that was begun with ofpprop_start(), by
419  * padding 'msg' to a multiple of 8 bytes and updating the property's length.
420  * 'start_ofs' should be the offset of the beginning of the property, as
421  * returned by ofpprop_start(). */
422 void
423 ofpprop_end(struct ofpbuf *msg, size_t start_ofs)
424 {
425     struct ofp_prop_header *oph;
426
427     oph = ofpbuf_at_assert(msg, start_ofs, sizeof *oph);
428     oph->len = htons(msg->size - start_ofs);
429     ofpbuf_padto(msg, ROUND_UP(msg->size, 8));
430 }
431
432 enum ofperr
433 ofpprop_unknown(struct vlog_module *module, bool loose, const char *msg,
434                 uint64_t type)
435 {
436     bool is_experimenter = ofpprop_is_experimenter(type);
437
438     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
439     enum vlog_level level = loose ? VLL_DBG : VLL_WARN;
440     if (!is_experimenter) {
441         vlog_rate_limit(module, level, &rl, "unknown %s property type %"PRId64,
442                         msg, type);
443     } else {
444         vlog_rate_limit(module, level, &rl,
445                         "unknown %s property type for exp_id 0x%"PRIx32", "
446                         "exp_type %"PRId32, msg,
447                         ofpprop_type_to_exp_id(type),
448                         ofpprop_type_to_exp_type(type));
449     }
450
451     /* There's an error OFPBPC_BAD_EXPERIMENTER that we could use for
452      * experimenter IDs that we don't know at all, but that seems like a
453      * difficult distinction and OFPERR_OFPBPC_BAD_EXP_TYPE communicates the
454      * problem quite well. */
455     return (loose ? 0
456             : is_experimenter ? OFPERR_OFPBPC_BAD_EXP_TYPE
457             : OFPERR_OFPBPC_BAD_TYPE);
458 }
459