2 * Copyright (c) 2014, 2015, 2016 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "byte-order.h"
20 #include "openvswitch/ofpbuf.h"
21 #include "openvswitch/ofp-errors.h"
22 #include "openvswitch/ofp-prop.h"
23 #include "openvswitch/vlog.h"
27 struct ofp_prop_be16
{
33 BUILD_ASSERT_DECL(sizeof(struct ofp_prop_be16
) == 8);
35 struct ofp_prop_be32
{
40 BUILD_ASSERT_DECL(sizeof(struct ofp_prop_be32
) == 8);
43 ofpprop_type_to_exp_id(uint64_t type
)
49 ofpprop_type_to_exp_type(uint64_t type
)
51 return type
& UINT32_MAX
;
54 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
55 * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
56 * nonnull, the entire property, including the header, in '*property'. Points
57 * 'property->header' to the property header (which could be ofp_prop_header or
58 * ofp_prop_experimenter) and 'property->msg' to just past it. Returns 0 if
59 * successful, otherwise an OpenFlow error code.
61 * This function treats property types 'min_exp' and larger as introducing
62 * experimenter properties. For most kinds of properties, 0xffff is the
63 * appropriate value for 'min_exp', because 0xffff is the only property type
64 * used for experimenters, but async config properties also use 0xfffe. Use
65 * 0x10000 (or higher) if experimenter properties are not supported.
67 * This function pulls the property's stated size padded out to a multiple of
68 * 'alignment' bytes. The common case in OpenFlow is an 'alignment' of 8, so
69 * you can use ofpprop_pull() for that case. */
71 ofpprop_pull__(struct ofpbuf
*msg
, struct ofpbuf
*property
,
72 unsigned int alignment
, unsigned int min_exp
,
75 struct ofp_prop_header
*oph
;
76 unsigned int padded_len
;
79 if (msg
->size
< sizeof *oph
) {
80 return OFPERR_OFPBPC_BAD_LEN
;
84 len
= ntohs(oph
->len
);
85 padded_len
= ROUND_UP(len
, alignment
);
86 if (len
< sizeof *oph
|| padded_len
> msg
->size
) {
87 return OFPERR_OFPBPC_BAD_LEN
;
90 uint16_t type
= ntohs(oph
->type
);
94 struct ofp_prop_experimenter
*ope
= msg
->data
;
95 if (len
< sizeof *ope
) {
96 return OFPERR_OFPBPC_BAD_LEN
;
99 if (!ope
->experimenter
) {
100 /* Reject experimenter 0 because it yields ambiguity with standard
102 return OFPERR_OFPBPC_BAD_EXPERIMENTER
;
105 *typep
= OFPPROP_EXP(ntohl(ope
->experimenter
), ntohl(ope
->exp_type
));
109 ofpbuf_use_const(property
, msg
->data
, len
);
110 property
->header
= property
->data
;
111 property
->msg
= ((uint8_t *) property
->data
113 ? sizeof(struct ofp_prop_header
)
114 : sizeof(struct ofp_prop_experimenter
)));
116 ofpbuf_pull(msg
, padded_len
);
120 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
121 * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
122 * nonnull, the entire property, including the header, in '*property'. Points
123 * 'property->header' to the property header (which could be ofp_prop_header or
124 * ofp_prop_experimenter) and 'property->msg' to just past it. Returns 0 if
125 * successful, otherwise an error code.
127 * This function treats property type 0xffff as introducing an experimenter
128 * property. Use ofpprop_pull__() instead if some other behavior is needed.
130 * This function pulls the property's stated size padded out to a multiple of 8
131 * bytes, which is the common case for OpenFlow properties. Use
132 * ofpprop_pull__() instead if some other behavior is needed.*/
134 ofpprop_pull(struct ofpbuf
*msg
, struct ofpbuf
*property
, uint64_t *typep
)
136 return ofpprop_pull__(msg
, property
, 8, 0xffff, typep
);
139 /* Attempts to parse 'property' as a property containing a 16-bit value. If
140 * successful, stores the value into '*value' and returns 0; otherwise returns
141 * an OpenFlow error. */
143 ofpprop_parse_be16(const struct ofpbuf
*property
, ovs_be16
*value
)
145 /* OpenFlow uses 8-byte properties for 16-bit values, which doesn't really
146 * make sense. Be forgiving by allowing any size payload as long as it's
147 * at least big enough. */
148 ovs_be16
*p
= property
->msg
;
149 if (ofpbuf_msgsize(property
) < sizeof *p
) {
150 return OFPERR_OFPBPC_BAD_LEN
;
156 /* Attempts to parse 'property' as a property containing a 32-bit value. If
157 * successful, stores the value into '*value' and returns 0; otherwise returns
158 * an OpenFlow error. */
160 ofpprop_parse_be32(const struct ofpbuf
*property
, ovs_be32
*value
)
162 ovs_be32
*p
= property
->msg
;
163 if (ofpbuf_msgsize(property
) != sizeof *p
) {
164 return OFPERR_OFPBPC_BAD_LEN
;
170 /* Attempts to parse 'property' as a property containing a 64-bit value. If
171 * successful, stores the value into '*value' and returns 0; otherwise returns
172 * an OpenFlow error. */
174 ofpprop_parse_be64(const struct ofpbuf
*property
, ovs_be64
*value
)
177 size_t be64_offset
= ROUND_UP(ofpbuf_headersize(property
), 8);
178 if (property
->size
!= be64_offset
+ sizeof *p
) {
179 return OFPERR_OFPBPC_BAD_LEN
;
182 p
= ALIGNED_CAST(ovs_be64
*, (char *) property
->data
+ be64_offset
);
187 /* Attempts to parse 'property' as a property containing a 8-bit value. If
188 * successful, stores the value into '*value' and returns 0; otherwise returns
189 * an OpenFlow error. */
191 ofpprop_parse_u8(const struct ofpbuf
*property
, uint8_t *value
)
193 /* OpenFlow 1.5 and earlier don't have any 8-bit properties, but it uses
194 * 8-byte properties for 16-bit values, which doesn't really make sense.
195 * Be forgiving by allowing any size payload as long as it's at least big
197 uint8_t *p
= property
->msg
;
198 if (ofpbuf_msgsize(property
) < sizeof *p
) {
199 return OFPERR_OFPBPC_BAD_LEN
;
205 /* Attempts to parse 'property' as a property containing a 16-bit value. If
206 * successful, stores the value into '*value' and returns 0; otherwise returns
207 * an OpenFlow error. */
209 ofpprop_parse_u16(const struct ofpbuf
*property
, uint16_t *value
)
211 /* OpenFlow uses 8-byte properties for 16-bit values, which doesn't really
212 * make sense. Be forgiving by allowing any size payload as long as it's
213 * at least big enough. */
214 ovs_be16
*p
= property
->msg
;
215 if (ofpbuf_msgsize(property
) < sizeof *p
) {
216 return OFPERR_OFPBPC_BAD_LEN
;
222 /* Attempts to parse 'property' as a property containing a 32-bit value. If
223 * successful, stores the value into '*value' and returns 0; otherwise returns
224 * an OpenFlow error. */
226 ofpprop_parse_u32(const struct ofpbuf
*property
, uint32_t *value
)
228 ovs_be32
*p
= property
->msg
;
229 if (ofpbuf_msgsize(property
) != sizeof *p
) {
230 return OFPERR_OFPBPC_BAD_LEN
;
236 /* Attempts to parse 'property' as a property containing a 64-bit value. If
237 * successful, stores the value into '*value' and returns 0; otherwise returns
238 * an OpenFlow error. */
240 ofpprop_parse_u64(const struct ofpbuf
*property
, uint64_t *value
)
243 size_t be64_offset
= ROUND_UP(ofpbuf_headersize(property
), 8);
244 if (property
->size
!= be64_offset
+ sizeof *p
) {
245 return OFPERR_OFPBPC_BAD_LEN
;
248 p
= ALIGNED_CAST(ovs_be64
*, (char *) property
->data
+ be64_offset
);
253 /* Attempts to parse 'property' as a property containing a UUID. If
254 * successful, stores the value into '*uuid' and returns 0; otherwise returns
255 * an OpenFlow error. */
257 ofpprop_parse_uuid(const struct ofpbuf
*property
, struct uuid
*uuid
)
259 struct uuid
*p
= property
->msg
;
260 if (ofpbuf_msgsize(property
) != sizeof *p
) {
261 return OFPERR_OFPBPC_BAD_LEN
;
267 /* Attempts to parse 'property' as a property that contains nested properties.
268 * If successful, stores the nested data into '*nested' and returns 0;
269 * otherwise returns an OpenFlow error.
271 * The only thing special about nested properties is that the property header
272 * is followed by 4 bytes of padding, so that the nested properties begin at an
273 * 8-byte aligned offset. This function can be used in other situations where
274 * this is the case. */
276 ofpprop_parse_nested(const struct ofpbuf
*property
, struct ofpbuf
*nested
)
278 size_t nested_offset
= ROUND_UP(ofpbuf_headersize(property
), 8);
279 if (property
->size
< nested_offset
) {
280 return OFPERR_OFPBPC_BAD_LEN
;
283 ofpbuf_use_const(nested
, property
->data
, property
->size
);
284 ofpbuf_pull(nested
, nested_offset
);
288 /* Adds a property with the given 'type' and 'len'-byte contents 'value' to
289 * 'msg', padding the property out to a multiple of 8 bytes. */
291 ofpprop_put(struct ofpbuf
*msg
, uint64_t type
, const void *value
, size_t len
)
293 size_t start_ofs
= ofpprop_start(msg
, type
);
294 ofpbuf_put(msg
, value
, len
);
295 ofpprop_end(msg
, start_ofs
);
298 /* Adds a property with the given 'type' to 'msg', consisting of a struct
299 * ofp_prop_header or ofp_prop_experimenter followed by enough zero bytes to
300 * total 'len' bytes, followed by padding to bring the property up to a
301 * multiple of 8 bytes. Returns the property header. */
303 ofpprop_put_zeros(struct ofpbuf
*msg
, uint64_t type
, size_t len
)
305 void *header
= ofpbuf_put_zeros(msg
, ROUND_UP(len
, 8));
306 if (!ofpprop_is_experimenter(type
)) {
307 struct ofp_prop_header
*oph
= header
;
308 oph
->type
= htons(type
);
309 oph
->len
= htons(len
);
311 struct ofp_prop_experimenter
*ope
= header
;
312 ope
->type
= htons(0xffff);
313 ope
->len
= htons(len
);
314 ope
->experimenter
= htonl(ofpprop_type_to_exp_id(type
));
315 ope
->exp_type
= htonl(ofpprop_type_to_exp_type(type
));
320 /* Adds a property with the given 'type' and 16-bit 'value' to 'msg'. */
322 ofpprop_put_be16(struct ofpbuf
*msg
, uint64_t type
, ovs_be16 value
)
324 if (!ofpprop_is_experimenter(type
)) {
325 /* The OpenFlow specs consistently (at least they're consistent!) give
326 * properties with a 16-bit integer value a length of 8, not 6, so add
327 * two bytes of padding. */
328 ovs_be16 padded_value
[2] = { value
, 0 };
329 ofpprop_put(msg
, type
, padded_value
, sizeof padded_value
);
331 /* There's no precedent but let's assume that this is generally done
333 ofpprop_put(msg
, type
, &value
, sizeof value
);
337 /* Adds a property with the given 'type' and 32-bit 'value' to 'msg'. */
339 ofpprop_put_be32(struct ofpbuf
*msg
, uint64_t type
, ovs_be32 value
)
341 ofpprop_put(msg
, type
, &value
, sizeof value
);
344 /* Adds a property with the given 'type' and 64-bit 'value' to 'msg'. */
346 ofpprop_put_be64(struct ofpbuf
*msg
, uint64_t type
, ovs_be64 value
)
348 size_t start
= ofpprop_start(msg
, type
);
349 ofpbuf_put_zeros(msg
, 4);
350 ofpbuf_put(msg
, &value
, sizeof value
);
351 ofpprop_end(msg
, start
);
354 /* Adds a property with the given 'type' and 8-bit 'value' to 'msg'. */
356 ofpprop_put_u8(struct ofpbuf
*msg
, uint64_t type
, uint8_t value
)
358 /* There's no precedent for 8-bit properties in OpenFlow 1.5 and earlier
359 * but let's assume they're done sanely. */
360 ofpprop_put(msg
, type
, &value
, 1);
363 /* Adds a property with the given 'type' and 16-bit 'value' to 'msg'. */
365 ofpprop_put_u16(struct ofpbuf
*msg
, uint64_t type
, uint16_t value
)
367 ofpprop_put_be16(msg
, type
, htons(value
));
370 /* Adds a property with the given 'type' and 32-bit 'value' to 'msg'. */
372 ofpprop_put_u32(struct ofpbuf
*msg
, uint64_t type
, uint32_t value
)
374 ofpprop_put_be32(msg
, type
, htonl(value
));
377 /* Adds a property with the given 'type' and 64-bit 'value' to 'msg'. */
379 ofpprop_put_u64(struct ofpbuf
*msg
, uint64_t type
, uint64_t value
)
381 ofpprop_put_be64(msg
, type
, htonll(value
));
384 /* Appends a property to 'msg' whose type is 'type' and whose contents is a
385 * series of property headers, one for each 1-bit in 'bitmap'. */
387 ofpprop_put_bitmap(struct ofpbuf
*msg
, uint64_t type
, uint64_t bitmap
)
389 size_t start_ofs
= ofpprop_start(msg
, type
);
391 for (; bitmap
; bitmap
= zero_rightmost_1bit(bitmap
)) {
392 ofpprop_start(msg
, rightmost_1bit_idx(bitmap
));
394 ofpprop_end(msg
, start_ofs
);
397 /* Appends a content-free property with the given 'type' to 'msg'.
399 * (The idea is that the presence of the property acts as a flag.) */
401 ofpprop_put_flag(struct ofpbuf
*msg
, uint64_t type
)
403 size_t start
= ofpprop_start(msg
, type
);
404 ofpprop_end(msg
, start
);
407 /* Appends a property to 'msg' with the given 'type' and 'uuid' as its
410 ofpprop_put_uuid(struct ofpbuf
*msg
, uint64_t type
, const struct uuid
*uuid
)
412 ofpprop_put(msg
, type
, uuid
, sizeof *uuid
);
415 /* Appends a property of type 'type' to 'msg' whose contents are padding to
416 * 8-byte alignment followed by 'nested'. This is a suitable way to add nested
417 * properties to 'msg'. */
419 ofpprop_put_nested(struct ofpbuf
*msg
, uint64_t type
,
420 const struct ofpbuf
*nested
)
422 size_t start
= ofpprop_start_nested(msg
, type
);
423 ofpbuf_put(msg
, nested
->data
, nested
->size
);
424 ofpprop_end(msg
, start
);
427 /* Appends a header for a property of type 'type' to 'msg'. The caller should
428 * add the contents of the property to 'msg', then finish it by calling
429 * ofpprop_end(). Returns the offset of the beginning of the property (to pass
430 * to ofpprop_end() later). */
432 ofpprop_start(struct ofpbuf
*msg
, uint64_t type
)
434 size_t start_ofs
= msg
->size
;
435 if (!ofpprop_is_experimenter(type
)) {
436 struct ofp_prop_header
*oph
= ofpbuf_put_uninit(msg
, sizeof *oph
);
437 oph
->type
= htons(type
);
440 struct ofp_prop_experimenter
*ope
441 = ofpbuf_put_uninit(msg
, sizeof *ope
);
442 ope
->type
= htons(0xffff);
443 ope
->len
= htons(12);
444 ope
->experimenter
= htonl(ofpprop_type_to_exp_id(type
));
445 ope
->exp_type
= htonl(ofpprop_type_to_exp_type(type
));
450 /* Finishes serializing a property that was begun with ofpprop_start(), by
451 * padding 'msg' to a multiple of 8 bytes and updating the property's length.
452 * 'start_ofs' should be the offset of the beginning of the property, as
453 * returned by ofpprop_start(). */
455 ofpprop_end(struct ofpbuf
*msg
, size_t start_ofs
)
457 struct ofp_prop_header
*oph
;
459 oph
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *oph
);
460 oph
->len
= htons(msg
->size
- start_ofs
);
461 ofpbuf_padto(msg
, ROUND_UP(msg
->size
, 8));
464 /* Appends a header for a property of type 'type' to 'msg', followed by padding
465 * suitable for putting nested properties into the property; that is, padding
466 * to an 8-byte alignment.
468 * This otherwise works like ofpprop_start().
470 * There's no need for ofpprop_end_nested(), because ofpprop_end() works fine
473 ofpprop_start_nested(struct ofpbuf
*msg
, uint64_t type
)
475 size_t start_ofs
= ofpprop_start(msg
, type
);
476 ofpbuf_padto(msg
, ROUND_UP(msg
->size
, 8));
481 ofpprop_unknown(struct vlog_module
*module
, bool loose
, const char *msg
,
484 bool is_experimenter
= ofpprop_is_experimenter(type
);
486 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
487 enum vlog_level level
= loose
? VLL_DBG
: VLL_WARN
;
488 if (!is_experimenter
) {
489 vlog_rate_limit(module
, level
, &rl
, "unknown %s property type %"PRId64
,
492 vlog_rate_limit(module
, level
, &rl
,
493 "unknown %s property type for exp_id 0x%"PRIx32
", "
494 "exp_type %"PRId32
, msg
,
495 ofpprop_type_to_exp_id(type
),
496 ofpprop_type_to_exp_type(type
));
499 /* There's an error OFPBPC_BAD_EXPERIMENTER that we could use for
500 * experimenter IDs that we don't know at all, but that seems like a
501 * difficult distinction and OFPERR_OFPBPC_BAD_EXP_TYPE communicates the
502 * problem quite well. */
504 : is_experimenter
? OFPERR_OFPBPC_BAD_EXP_TYPE
505 : OFPERR_OFPBPC_BAD_TYPE
);