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"
28 ofpprop_type_to_exp_id(uint64_t type)
34 ofpprop_type_to_exp_type(uint64_t type)
36 return type & UINT32_MAX;
39 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
40 * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
41 * nonnull, the entire property, including the header, in '*property'. Returns
42 * 0 if successful, otherwise an OpenFlow error code.
44 * This function treats property types 'min_exp' and larger as introducing
45 * experimenter properties. For most kinds of properties, 0xffff is the
46 * appropriate value for 'min_exp', because 0xffff is the only property type
47 * used for experimenters, but async config properties also use 0xfffe. Use
48 * 0x10000 (or higher) if experimenter properties are not supported.
50 * This function pulls the property's stated size padded out to a multiple of
51 * 'alignment' bytes. The common case in OpenFlow is an 'alignment' of 8, so
52 * you can use ofpprop_pull() for that case. */
54 ofpprop_pull__(struct ofpbuf *msg, struct ofpbuf *property,
55 unsigned int alignment, unsigned int min_exp,
58 struct ofp_prop_header *oph;
59 unsigned int padded_len;
62 if (msg->size < sizeof *oph) {
63 return OFPERR_OFPBPC_BAD_LEN;
67 len = ntohs(oph->len);
68 padded_len = ROUND_UP(len, alignment);
69 if (len < sizeof *oph || padded_len > msg->size) {
70 return OFPERR_OFPBPC_BAD_LEN;
73 uint16_t type = ntohs(oph->type);
77 struct ofp_prop_experimenter *ope = msg->data;
78 if (len < sizeof *ope) {
79 return OFPERR_OFPBPC_BAD_LEN;
82 if (!ope->experimenter) {
83 /* Reject experimenter 0 because it yields ambiguity with standard
85 return OFPERR_OFPBPC_BAD_EXPERIMENTER;
88 *typep = OFPPROP_EXP(ntohl(ope->experimenter), ntohl(ope->exp_type));
92 ofpbuf_use_const(property, msg->data, len);
93 property->header = property->data;
94 property->msg = ((uint8_t *) property->data
96 ? sizeof(struct ofp_prop_header)
97 : sizeof(struct ofp_prop_experimenter)));
99 ofpbuf_pull(msg, padded_len);
103 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
104 * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
105 * nonnull, the entire property, including the header, in '*property'. Returns
106 * 0 if successful, otherwise an error code.
108 * This function pulls the property's stated size padded out to a multiple of
109 * 8 bytes, which is the common case for OpenFlow properties. */
111 ofpprop_pull(struct ofpbuf *msg, struct ofpbuf *property, uint64_t *typep)
113 return ofpprop_pull__(msg, property, 8, 0xffff, typep);
116 /* Adds a property with the given 'type' and 'len'-byte contents 'value' to
117 * 'msg', padding the property out to a multiple of 8 bytes. */
119 ofpprop_put(struct ofpbuf *msg, uint64_t type, const void *value, size_t len)
121 size_t start_ofs = ofpprop_start(msg, type);
122 ofpbuf_put(msg, value, len);
123 ofpprop_end(msg, start_ofs);
126 /* Appends a property to 'msg' whose type is 'type' and whose contents is a
127 * series of property headers, one for each 1-bit in 'bitmap'. */
129 ofpprop_put_bitmap(struct ofpbuf *msg, uint64_t type, uint64_t bitmap)
131 size_t start_ofs = ofpprop_start(msg, type);
133 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
134 ofpprop_start(msg, rightmost_1bit_idx(bitmap));
136 ofpprop_end(msg, start_ofs);
139 /* Appends a header for a property of type 'type' to 'msg'. The caller should
140 * add the contents of the property to 'msg', then finish it by calling
141 * ofpprop_end(). Returns the offset of the beginning of the property (to pass
142 * to ofpprop_end() later). */
144 ofpprop_start(struct ofpbuf *msg, uint64_t type)
146 size_t start_ofs = msg->size;
147 if (!ofpprop_is_experimenter(type)) {
148 struct ofp_prop_header *oph = ofpbuf_put_uninit(msg, sizeof *oph);
149 oph->type = htons(type);
152 struct ofp_prop_experimenter *ope
153 = ofpbuf_put_uninit(msg, sizeof *ope);
154 ope->type = htons(0xffff);
155 ope->len = htons(12);
156 ope->experimenter = htonl(ofpprop_type_to_exp_id(type));
157 ope->exp_type = htonl(ofpprop_type_to_exp_type(type));
162 /* Finishes serializing a property that was begun with ofpprop_start(), by
163 * padding 'msg' to a multiple of 8 bytes and updating the property's length.
164 * 'start_ofs' should be the offset of the beginning of the property, as
165 * returned by ofpprop_start(). */
167 ofpprop_end(struct ofpbuf *msg, size_t start_ofs)
169 struct ofp_prop_header *oph;
171 oph = ofpbuf_at_assert(msg, start_ofs, sizeof *oph);
172 oph->len = htons(msg->size - start_ofs);
173 ofpbuf_padto(msg, ROUND_UP(msg->size, 8));
177 ofpprop_unknown(struct vlog_module *module, bool loose, const char *msg,
180 bool is_experimenter = ofpprop_is_experimenter(type);
182 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
183 enum vlog_level level = loose ? VLL_DBG : VLL_WARN;
184 if (!is_experimenter) {
185 vlog_rate_limit(module, level, &rl, "unknown %s property type %"PRId64,
188 vlog_rate_limit(module, level, &rl,
189 "unknown %s property type for exp_id 0x%"PRIx32", "
190 "exp_type %"PRId32, msg,
191 ofpprop_type_to_exp_id(type),
192 ofpprop_type_to_exp_type(type));
195 /* There's an error OFPBPC_BAD_EXPERIMENTER that we could use for
196 * experimenter IDs that we don't know at all, but that seems like a
197 * difficult distinction and OFPERR_OFPBPC_BAD_EXP_TYPE communicates the
198 * problem quite well. */
200 : is_experimenter ? OFPERR_OFPBPC_BAD_EXP_TYPE
201 : OFPERR_OFPBPC_BAD_TYPE);