ofp-prop: Add generic functions for working with 16- and 32-bit properties.
[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
27 struct ofp_prop_be16 {
28     ovs_be16 type;
29     ovs_be16 len;
30     ovs_be16 value;
31     uint8_t pad[2];
32 };
33 BUILD_ASSERT_DECL(sizeof(struct ofp_prop_be16) == 8);
34
35 struct ofp_prop_be32 {
36     ovs_be16 type;
37     ovs_be16 len;
38     ovs_be32 value;
39 };
40 BUILD_ASSERT_DECL(sizeof(struct ofp_prop_be32) == 8);
41
42 static uint32_t
43 ofpprop_type_to_exp_id(uint64_t type)
44 {
45     return type >> 32;
46 }
47
48 static uint32_t
49 ofpprop_type_to_exp_type(uint64_t type)
50 {
51     return type & UINT32_MAX;
52 }
53
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'.  Returns
57  * 0 if successful, otherwise an OpenFlow error code.
58  *
59  * This function treats property types 'min_exp' and larger as introducing
60  * experimenter properties.  For most kinds of properties, 0xffff is the
61  * appropriate value for 'min_exp', because 0xffff is the only property type
62  * used for experimenters, but async config properties also use 0xfffe.  Use
63  * 0x10000 (or higher) if experimenter properties are not supported.
64  *
65  * This function pulls the property's stated size padded out to a multiple of
66  * 'alignment' bytes.  The common case in OpenFlow is an 'alignment' of 8, so
67  * you can use ofpprop_pull() for that case. */
68 enum ofperr
69 ofpprop_pull__(struct ofpbuf *msg, struct ofpbuf *property,
70                unsigned int alignment, unsigned int min_exp,
71                uint64_t *typep)
72 {
73     struct ofp_prop_header *oph;
74     unsigned int padded_len;
75     unsigned int len;
76
77     if (msg->size < sizeof *oph) {
78         return OFPERR_OFPBPC_BAD_LEN;
79     }
80
81     oph = msg->data;
82     len = ntohs(oph->len);
83     padded_len = ROUND_UP(len, alignment);
84     if (len < sizeof *oph || padded_len > msg->size) {
85         return OFPERR_OFPBPC_BAD_LEN;
86     }
87
88     uint16_t type = ntohs(oph->type);
89     if (type < min_exp) {
90         *typep = type;
91     } else {
92         struct ofp_prop_experimenter *ope = msg->data;
93         if (len < sizeof *ope) {
94             return OFPERR_OFPBPC_BAD_LEN;
95         }
96
97         if (!ope->experimenter) {
98             /* Reject experimenter 0 because it yields ambiguity with standard
99              * property types. */
100             return OFPERR_OFPBPC_BAD_EXPERIMENTER;
101         }
102
103         *typep = OFPPROP_EXP(ntohl(ope->experimenter), ntohl(ope->exp_type));
104     }
105
106     if (property) {
107         ofpbuf_use_const(property, msg->data, len);
108         property->header = property->data;
109         property->msg = ((uint8_t *) property->data
110                          + (type < min_exp
111                             ? sizeof(struct ofp_prop_header)
112                             : sizeof(struct ofp_prop_experimenter)));
113     }
114     ofpbuf_pull(msg, padded_len);
115     return 0;
116 }
117
118 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
119  * of 'msg'.  Stores the type of the property in '*typep' and, if 'property' is
120  * nonnull, the entire property, including the header, in '*property'.  Returns
121  * 0 if successful, otherwise an error code.
122  *
123  * This function pulls the property's stated size padded out to a multiple of
124  * 8 bytes, which is the common case for OpenFlow properties. */
125 enum ofperr
126 ofpprop_pull(struct ofpbuf *msg, struct ofpbuf *property, uint64_t *typep)
127 {
128     return ofpprop_pull__(msg, property, 8, 0xffff, typep);
129 }
130
131 /* Attempts to parse 'property' as a property containing a 16-bit value.  If
132  * successful, stores the value into '*value' and returns 0; otherwise returns
133  * an OpenFlow error. */
134 enum ofperr
135 ofpprop_parse_be16(const struct ofpbuf *property, ovs_be16 *value)
136 {
137     /* OpenFlow uses 8-byte properties for 16-bit values, which doesn't really
138      * make sense.  Be forgiving by allowing any size payload as long as it's
139      * at least big enough.  */
140     ovs_be16 *p = property->msg;
141     if (ofpbuf_msgsize(property) < sizeof *p) {
142         return OFPERR_OFPBPC_BAD_LEN;
143     }
144     *value = *p;
145     return 0;
146 }
147
148 /* Attempts to parse 'property' as a property containing a 32-bit value.  If
149  * successful, stores the value into '*value' and returns 0; otherwise returns
150  * an OpenFlow error. */
151 enum ofperr
152 ofpprop_parse_be32(const struct ofpbuf *property, ovs_be32 *value)
153 {
154     ovs_be32 *p = property->msg;
155     if (ofpbuf_msgsize(property) != sizeof *p) {
156         return OFPERR_OFPBPC_BAD_LEN;
157     }
158     *value = *p;
159     return 0;
160 }
161
162 /* Attempts to parse 'property' as a property containing a 16-bit value.  If
163  * successful, stores the value into '*value' and returns 0; otherwise returns
164  * an OpenFlow error. */
165 enum ofperr
166 ofpprop_parse_u16(const struct ofpbuf *property, uint16_t *value)
167 {
168     /* OpenFlow uses 8-byte properties for 16-bit values, which doesn't really
169      * make sense.  Be forgiving by allowing any size payload as long as it's
170      * at least big enough.  */
171     ovs_be16 *p = property->msg;
172     if (ofpbuf_msgsize(property) < sizeof *p) {
173         return OFPERR_OFPBPC_BAD_LEN;
174     }
175     *value = ntohs(*p);
176     return 0;
177 }
178
179 /* Attempts to parse 'property' as a property containing a 32-bit value.  If
180  * successful, stores the value into '*value' and returns 0; otherwise returns
181  * an OpenFlow error. */
182 enum ofperr
183 ofpprop_parse_u32(const struct ofpbuf *property, uint32_t *value)
184 {
185     ovs_be32 *p = property->msg;
186     if (ofpbuf_msgsize(property) != sizeof *p) {
187         return OFPERR_OFPBPC_BAD_LEN;
188     }
189     *value = ntohl(*p);
190     return 0;
191 }
192
193 /* Adds a property with the given 'type' and 'len'-byte contents 'value' to
194  * 'msg', padding the property out to a multiple of 8 bytes. */
195 void
196 ofpprop_put(struct ofpbuf *msg, uint64_t type, const void *value, size_t len)
197 {
198     size_t start_ofs = ofpprop_start(msg, type);
199     ofpbuf_put(msg, value, len);
200     ofpprop_end(msg, start_ofs);
201 }
202
203 /* Adds a property with the given 'type' and 16-bit 'value' to 'msg'. */
204 void
205 ofpprop_put_be16(struct ofpbuf *msg, uint64_t type, ovs_be16 value)
206 {
207     if (!ofpprop_is_experimenter(type)) {
208         /* The OpenFlow specs consistently (at least they're consistent!)  give
209          * properties with a 16-bit integer value a length of 8, not 6, so add
210          * two bytes of padding.  */
211         ovs_be16 padded_value[2] = { value, 0 };
212         ofpprop_put(msg, type, padded_value, sizeof padded_value);
213     } else {
214         /* There's no precedent but let's assume that this is generally done
215          * sanely. */
216         ofpprop_put(msg, type, &value, sizeof value);
217     }
218 }
219
220 /* Adds a property with the given 'type' and 32-bit 'value' to 'msg'. */
221 void
222 ofpprop_put_be32(struct ofpbuf *msg, uint64_t type, ovs_be32 value)
223 {
224     ofpprop_put(msg, type, &value, sizeof value);
225 }
226
227 /* Adds a property with the given 'type' and 16-bit 'value' to 'msg'. */
228 void
229 ofpprop_put_u16(struct ofpbuf *msg, uint64_t type, uint16_t value)
230 {
231     ofpprop_put_be16(msg, type, htons(value));
232 }
233
234 /* Adds a property with the given 'type' and 32-bit 'value' to 'msg'. */
235 void
236 ofpprop_put_u32(struct ofpbuf *msg, uint64_t type, uint32_t value)
237 {
238     ofpprop_put_be32(msg, type, htonl(value));
239 }
240
241 /* Adds a property header to 'msg' for each 1-bit in 'bitmap'. */
242 /* Appends a property to 'msg' whose type is 'type' and whose contents is a
243  * series of property headers, one for each 1-bit in 'bitmap'. */
244 void
245 ofpprop_put_bitmap(struct ofpbuf *msg, uint64_t type, uint64_t bitmap)
246 {
247     size_t start_ofs = ofpprop_start(msg, type);
248
249     for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
250         ofpprop_start(msg, rightmost_1bit_idx(bitmap));
251     }
252     ofpprop_end(msg, start_ofs);
253 }
254
255 /* Appends a header for a property of type 'type' to 'msg'.  The caller should
256  * add the contents of the property to 'msg', then finish it by calling
257  * ofpprop_end().  Returns the offset of the beginning of the property (to pass
258  * to ofpprop_end() later). */
259 size_t
260 ofpprop_start(struct ofpbuf *msg, uint64_t type)
261 {
262     size_t start_ofs = msg->size;
263     if (!ofpprop_is_experimenter(type)) {
264         struct ofp_prop_header *oph = ofpbuf_put_uninit(msg, sizeof *oph);
265         oph->type = htons(type);
266         oph->len = htons(4);
267     } else {
268         struct ofp_prop_experimenter *ope
269             = ofpbuf_put_uninit(msg, sizeof *ope);
270         ope->type = htons(0xffff);
271         ope->len = htons(12);
272         ope->experimenter = htonl(ofpprop_type_to_exp_id(type));
273         ope->exp_type = htonl(ofpprop_type_to_exp_type(type));
274     }
275     return start_ofs;
276 }
277
278 /* Finishes serializing a property that was begun with ofpprop_start(), by
279  * padding 'msg' to a multiple of 8 bytes and updating the property's length.
280  * 'start_ofs' should be the offset of the beginning of the property, as
281  * returned by ofpprop_start(). */
282 void
283 ofpprop_end(struct ofpbuf *msg, size_t start_ofs)
284 {
285     struct ofp_prop_header *oph;
286
287     oph = ofpbuf_at_assert(msg, start_ofs, sizeof *oph);
288     oph->len = htons(msg->size - start_ofs);
289     ofpbuf_padto(msg, ROUND_UP(msg->size, 8));
290 }
291
292 enum ofperr
293 ofpprop_unknown(struct vlog_module *module, bool loose, const char *msg,
294                 uint64_t type)
295 {
296     bool is_experimenter = ofpprop_is_experimenter(type);
297
298     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
299     enum vlog_level level = loose ? VLL_DBG : VLL_WARN;
300     if (!is_experimenter) {
301         vlog_rate_limit(module, level, &rl, "unknown %s property type %"PRId64,
302                         msg, type);
303     } else {
304         vlog_rate_limit(module, level, &rl,
305                         "unknown %s property type for exp_id 0x%"PRIx32", "
306                         "exp_type %"PRId32, msg,
307                         ofpprop_type_to_exp_id(type),
308                         ofpprop_type_to_exp_type(type));
309     }
310
311     /* There's an error OFPBPC_BAD_EXPERIMENTER that we could use for
312      * experimenter IDs that we don't know at all, but that seems like a
313      * difficult distinction and OFPERR_OFPBPC_BAD_EXP_TYPE communicates the
314      * problem quite well. */
315     return (loose ? 0
316             : is_experimenter ? OFPERR_OFPBPC_BAD_EXP_TYPE
317             : OFPERR_OFPBPC_BAD_TYPE);
318 }
319