]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ofp-prop.c
ofp-prop: Add helpers for u8, be64/u64, flag, and UUID properties.
[mirror_ovs.git] / lib / ofp-prop.c
CommitLineData
c5562271
BP
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"
34a543e3 24#include "openvswitch/vlog.h"
c5562271 25#include "util.h"
f01c94c5 26#include "uuid.h"
c5562271 27
b611d3ac
BP
28struct ofp_prop_be16 {
29 ovs_be16 type;
30 ovs_be16 len;
31 ovs_be16 value;
32 uint8_t pad[2];
33};
34BUILD_ASSERT_DECL(sizeof(struct ofp_prop_be16) == 8);
35
36struct ofp_prop_be32 {
37 ovs_be16 type;
38 ovs_be16 len;
39 ovs_be32 value;
40};
41BUILD_ASSERT_DECL(sizeof(struct ofp_prop_be32) == 8);
42
34a543e3
BP
43static uint32_t
44ofpprop_type_to_exp_id(uint64_t type)
45{
46 return type >> 32;
47}
48
49static uint32_t
50ofpprop_type_to_exp_type(uint64_t type)
51{
52 return type & UINT32_MAX;
53}
54
c5562271
BP
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
f01c94c5
BP
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.
c5562271 61 *
34a543e3
BP
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 *
c5562271
BP
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. */
71enum ofperr
72ofpprop_pull__(struct ofpbuf *msg, struct ofpbuf *property,
34a543e3
BP
73 unsigned int alignment, unsigned int min_exp,
74 uint64_t *typep)
c5562271
BP
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
34a543e3
BP
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
c5562271
BP
109 if (property) {
110 ofpbuf_use_const(property, msg->data, len);
34a543e3
BP
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)));
c5562271
BP
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
f01c94c5
BP
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.
c5562271 127 *
f01c94c5
BP
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.*/
c5562271 134enum ofperr
34a543e3 135ofpprop_pull(struct ofpbuf *msg, struct ofpbuf *property, uint64_t *typep)
c5562271 136{
34a543e3 137 return ofpprop_pull__(msg, property, 8, 0xffff, typep);
c5562271
BP
138}
139
b611d3ac
BP
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. */
143enum ofperr
144ofpprop_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. */
160enum ofperr
161ofpprop_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
f01c94c5
BP
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. */
174enum ofperr
175ofpprop_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. */
191enum ofperr
192ofpprop_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
b611d3ac
BP
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. */
209enum ofperr
210ofpprop_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. */
226enum ofperr
227ofpprop_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
f01c94c5
BP
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. */
240enum ofperr
241ofpprop_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. */
257enum ofperr
258ofpprop_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
c5562271
BP
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. */
270void
34a543e3 271ofpprop_put(struct ofpbuf *msg, uint64_t type, const void *value, size_t len)
c5562271
BP
272{
273 size_t start_ofs = ofpprop_start(msg, type);
274 ofpbuf_put(msg, value, len);
275 ofpprop_end(msg, start_ofs);
276}
277
b611d3ac
BP
278/* Adds a property with the given 'type' and 16-bit 'value' to 'msg'. */
279void
280ofpprop_put_be16(struct ofpbuf *msg, uint64_t type, ovs_be16 value)
281{
282 if (!ofpprop_is_experimenter(type)) {
283 /* The OpenFlow specs consistently (at least they're consistent!) give
284 * properties with a 16-bit integer value a length of 8, not 6, so add
285 * two bytes of padding. */
286 ovs_be16 padded_value[2] = { value, 0 };
287 ofpprop_put(msg, type, padded_value, sizeof padded_value);
288 } else {
289 /* There's no precedent but let's assume that this is generally done
290 * sanely. */
291 ofpprop_put(msg, type, &value, sizeof value);
292 }
293}
294
295/* Adds a property with the given 'type' and 32-bit 'value' to 'msg'. */
296void
297ofpprop_put_be32(struct ofpbuf *msg, uint64_t type, ovs_be32 value)
298{
299 ofpprop_put(msg, type, &value, sizeof value);
300}
301
f01c94c5
BP
302/* Adds a property with the given 'type' and 64-bit 'value' to 'msg'. */
303void
304ofpprop_put_be64(struct ofpbuf *msg, uint64_t type, ovs_be64 value)
305{
306 size_t start = ofpprop_start(msg, type);
307 ofpbuf_put_zeros(msg, 4);
308 ofpbuf_put(msg, &value, sizeof value);
309 ofpprop_end(msg, start);
310}
311
312/* Adds a property with the given 'type' and 8-bit 'value' to 'msg'. */
313void
314ofpprop_put_u8(struct ofpbuf *msg, uint64_t type, uint8_t value)
315{
316 /* There's no precedent for 8-bit properties in OpenFlow 1.5 and earlier
317 * but let's assume they're done sanely. */
318 ofpprop_put(msg, type, &value, 1);
319}
320
b611d3ac
BP
321/* Adds a property with the given 'type' and 16-bit 'value' to 'msg'. */
322void
323ofpprop_put_u16(struct ofpbuf *msg, uint64_t type, uint16_t value)
324{
325 ofpprop_put_be16(msg, type, htons(value));
326}
327
328/* Adds a property with the given 'type' and 32-bit 'value' to 'msg'. */
329void
330ofpprop_put_u32(struct ofpbuf *msg, uint64_t type, uint32_t value)
331{
332 ofpprop_put_be32(msg, type, htonl(value));
333}
334
f01c94c5
BP
335/* Adds a property with the given 'type' and 64-bit 'value' to 'msg'. */
336void
337ofpprop_put_u64(struct ofpbuf *msg, uint64_t type, uint64_t value)
338{
339 ofpprop_put_be64(msg, type, htonll(value));
340}
341
c5562271
BP
342/* Appends a property to 'msg' whose type is 'type' and whose contents is a
343 * series of property headers, one for each 1-bit in 'bitmap'. */
344void
34a543e3 345ofpprop_put_bitmap(struct ofpbuf *msg, uint64_t type, uint64_t bitmap)
c5562271
BP
346{
347 size_t start_ofs = ofpprop_start(msg, type);
348
349 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
350 ofpprop_start(msg, rightmost_1bit_idx(bitmap));
351 }
352 ofpprop_end(msg, start_ofs);
353}
354
f01c94c5
BP
355/* Appends a content-free property with the given 'type' to 'msg'.
356 *
357 * (The idea is that the presence of the property acts as a flag.) */
358void
359ofpprop_put_flag(struct ofpbuf *msg, uint64_t type)
360{
361 size_t start = ofpprop_start(msg, type);
362 ofpprop_end(msg, start);
363}
364
365/* Appends a property to 'msg' with the given 'type' and 'uuid' as its
366 * value. */
367void
368ofpprop_put_uuid(struct ofpbuf *msg, uint64_t type, const struct uuid *uuid)
369{
370 ofpprop_put(msg, type, uuid, sizeof *uuid);
371}
372
c5562271
BP
373/* Appends a header for a property of type 'type' to 'msg'. The caller should
374 * add the contents of the property to 'msg', then finish it by calling
375 * ofpprop_end(). Returns the offset of the beginning of the property (to pass
376 * to ofpprop_end() later). */
377size_t
34a543e3 378ofpprop_start(struct ofpbuf *msg, uint64_t type)
c5562271
BP
379{
380 size_t start_ofs = msg->size;
34a543e3
BP
381 if (!ofpprop_is_experimenter(type)) {
382 struct ofp_prop_header *oph = ofpbuf_put_uninit(msg, sizeof *oph);
383 oph->type = htons(type);
384 oph->len = htons(4);
385 } else {
386 struct ofp_prop_experimenter *ope
387 = ofpbuf_put_uninit(msg, sizeof *ope);
388 ope->type = htons(0xffff);
389 ope->len = htons(12);
390 ope->experimenter = htonl(ofpprop_type_to_exp_id(type));
391 ope->exp_type = htonl(ofpprop_type_to_exp_type(type));
392 }
c5562271
BP
393 return start_ofs;
394}
395
396/* Finishes serializing a property that was begun with ofpprop_start(), by
397 * padding 'msg' to a multiple of 8 bytes and updating the property's length.
398 * 'start_ofs' should be the offset of the beginning of the property, as
399 * returned by ofpprop_start(). */
400void
401ofpprop_end(struct ofpbuf *msg, size_t start_ofs)
402{
403 struct ofp_prop_header *oph;
404
405 oph = ofpbuf_at_assert(msg, start_ofs, sizeof *oph);
406 oph->len = htons(msg->size - start_ofs);
407 ofpbuf_padto(msg, ROUND_UP(msg->size, 8));
408}
409
34a543e3
BP
410enum ofperr
411ofpprop_unknown(struct vlog_module *module, bool loose, const char *msg,
412 uint64_t type)
413{
414 bool is_experimenter = ofpprop_is_experimenter(type);
415
416 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
417 enum vlog_level level = loose ? VLL_DBG : VLL_WARN;
418 if (!is_experimenter) {
419 vlog_rate_limit(module, level, &rl, "unknown %s property type %"PRId64,
420 msg, type);
421 } else {
422 vlog_rate_limit(module, level, &rl,
423 "unknown %s property type for exp_id 0x%"PRIx32", "
424 "exp_type %"PRId32, msg,
425 ofpprop_type_to_exp_id(type),
426 ofpprop_type_to_exp_type(type));
427 }
428
429 /* There's an error OFPBPC_BAD_EXPERIMENTER that we could use for
430 * experimenter IDs that we don't know at all, but that seems like a
431 * difficult distinction and OFPERR_OFPBPC_BAD_EXP_TYPE communicates the
432 * problem quite well. */
433 return (loose ? 0
434 : is_experimenter ? OFPERR_OFPBPC_BAD_EXP_TYPE
435 : OFPERR_OFPBPC_BAD_TYPE);
436}
437