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.
21 #include "byte-order.h"
23 #include "ofp-errors.h"
24 #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'. Returns
57 * 0 if successful, otherwise an OpenFlow error code.
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.
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. */
69 ofpprop_pull__(struct ofpbuf *msg, struct ofpbuf *property,
70 unsigned int alignment, unsigned int min_exp,
73 struct ofp_prop_header *oph;
74 unsigned int padded_len;
77 if (msg->size < sizeof *oph) {
78 return OFPERR_OFPBPC_BAD_LEN;
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;
88 uint16_t type = ntohs(oph->type);
92 struct ofp_prop_experimenter *ope = msg->data;
93 if (len < sizeof *ope) {
94 return OFPERR_OFPBPC_BAD_LEN;
97 if (!ope->experimenter) {
98 /* Reject experimenter 0 because it yields ambiguity with standard
100 return OFPERR_OFPBPC_BAD_EXPERIMENTER;
103 *typep = OFPPROP_EXP(ntohl(ope->experimenter), ntohl(ope->exp_type));
107 ofpbuf_use_const(property, msg->data, len);
108 property->header = property->data;
109 property->msg = ((uint8_t *) property->data
111 ? sizeof(struct ofp_prop_header)
112 : sizeof(struct ofp_prop_experimenter)));
114 ofpbuf_pull(msg, padded_len);
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.
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. */
126 ofpprop_pull(struct ofpbuf *msg, struct ofpbuf *property, uint64_t *typep)
128 return ofpprop_pull__(msg, property, 8, 0xffff, typep);
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. */
135 ofpprop_parse_be16(const struct ofpbuf *property, ovs_be16 *value)
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;
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. */
152 ofpprop_parse_be32(const struct ofpbuf *property, ovs_be32 *value)
154 ovs_be32 *p = property->msg;
155 if (ofpbuf_msgsize(property) != sizeof *p) {
156 return OFPERR_OFPBPC_BAD_LEN;
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. */
166 ofpprop_parse_u16(const struct ofpbuf *property, uint16_t *value)
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;
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. */
183 ofpprop_parse_u32(const struct ofpbuf *property, uint32_t *value)
185 ovs_be32 *p = property->msg;
186 if (ofpbuf_msgsize(property) != sizeof *p) {
187 return OFPERR_OFPBPC_BAD_LEN;
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. */
196 ofpprop_put(struct ofpbuf *msg, uint64_t type, const void *value, size_t len)
198 size_t start_ofs = ofpprop_start(msg, type);
199 ofpbuf_put(msg, value, len);
200 ofpprop_end(msg, start_ofs);
203 /* Adds a property with the given 'type' and 16-bit 'value' to 'msg'. */
205 ofpprop_put_be16(struct ofpbuf *msg, uint64_t type, ovs_be16 value)
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);
214 /* There's no precedent but let's assume that this is generally done
216 ofpprop_put(msg, type, &value, sizeof value);
220 /* Adds a property with the given 'type' and 32-bit 'value' to 'msg'. */
222 ofpprop_put_be32(struct ofpbuf *msg, uint64_t type, ovs_be32 value)
224 ofpprop_put(msg, type, &value, sizeof value);
227 /* Adds a property with the given 'type' and 16-bit 'value' to 'msg'. */
229 ofpprop_put_u16(struct ofpbuf *msg, uint64_t type, uint16_t value)
231 ofpprop_put_be16(msg, type, htons(value));
234 /* Adds a property with the given 'type' and 32-bit 'value' to 'msg'. */
236 ofpprop_put_u32(struct ofpbuf *msg, uint64_t type, uint32_t value)
238 ofpprop_put_be32(msg, type, htonl(value));
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'. */
245 ofpprop_put_bitmap(struct ofpbuf *msg, uint64_t type, uint64_t bitmap)
247 size_t start_ofs = ofpprop_start(msg, type);
249 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
250 ofpprop_start(msg, rightmost_1bit_idx(bitmap));
252 ofpprop_end(msg, start_ofs);
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). */
260 ofpprop_start(struct ofpbuf *msg, uint64_t type)
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);
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));
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(). */
283 ofpprop_end(struct ofpbuf *msg, size_t start_ofs)
285 struct ofp_prop_header *oph;
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));
293 ofpprop_unknown(struct vlog_module *module, bool loose, const char *msg,
296 bool is_experimenter = ofpprop_is_experimenter(type);
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,
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));
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. */
316 : is_experimenter ? OFPERR_OFPBPC_BAD_EXP_TYPE
317 : OFPERR_OFPBPC_BAD_TYPE);