408e810ccecbbb99d39a8bab76ce0f9947314587
[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 static uint32_t
28 ofpprop_type_to_exp_id(uint64_t type)
29 {
30     return type >> 32;
31 }
32
33 static uint32_t
34 ofpprop_type_to_exp_type(uint64_t type)
35 {
36     return type & UINT32_MAX;
37 }
38
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.
43  *
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.
49  *
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. */
53 enum ofperr
54 ofpprop_pull__(struct ofpbuf *msg, struct ofpbuf *property,
55                unsigned int alignment, unsigned int min_exp,
56                uint64_t *typep)
57 {
58     struct ofp_prop_header *oph;
59     unsigned int padded_len;
60     unsigned int len;
61
62     if (msg->size < sizeof *oph) {
63         return OFPERR_OFPBPC_BAD_LEN;
64     }
65
66     oph = msg->data;
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;
71     }
72
73     uint16_t type = ntohs(oph->type);
74     if (type < min_exp) {
75         *typep = type;
76     } else {
77         struct ofp_prop_experimenter *ope = msg->data;
78         if (len < sizeof *ope) {
79             return OFPERR_OFPBPC_BAD_LEN;
80         }
81
82         if (!ope->experimenter) {
83             /* Reject experimenter 0 because it yields ambiguity with standard
84              * property types. */
85             return OFPERR_OFPBPC_BAD_EXPERIMENTER;
86         }
87
88         *typep = OFPPROP_EXP(ntohl(ope->experimenter), ntohl(ope->exp_type));
89     }
90
91     if (property) {
92         ofpbuf_use_const(property, msg->data, len);
93         property->header = property->data;
94         property->msg = ((uint8_t *) property->data
95                          + (type < min_exp
96                             ? sizeof(struct ofp_prop_header)
97                             : sizeof(struct ofp_prop_experimenter)));
98     }
99     ofpbuf_pull(msg, padded_len);
100     return 0;
101 }
102
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.
107  *
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. */
110 enum ofperr
111 ofpprop_pull(struct ofpbuf *msg, struct ofpbuf *property, uint64_t *typep)
112 {
113     return ofpprop_pull__(msg, property, 8, 0xffff, typep);
114 }
115
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. */
118 void
119 ofpprop_put(struct ofpbuf *msg, uint64_t type, const void *value, size_t len)
120 {
121     size_t start_ofs = ofpprop_start(msg, type);
122     ofpbuf_put(msg, value, len);
123     ofpprop_end(msg, start_ofs);
124 }
125
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'. */
128 void
129 ofpprop_put_bitmap(struct ofpbuf *msg, uint64_t type, uint64_t bitmap)
130 {
131     size_t start_ofs = ofpprop_start(msg, type);
132
133     for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
134         ofpprop_start(msg, rightmost_1bit_idx(bitmap));
135     }
136     ofpprop_end(msg, start_ofs);
137 }
138
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). */
143 size_t
144 ofpprop_start(struct ofpbuf *msg, uint64_t type)
145 {
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);
150         oph->len = htons(4);
151     } else {
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));
158     }
159     return start_ofs;
160 }
161
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(). */
166 void
167 ofpprop_end(struct ofpbuf *msg, size_t start_ofs)
168 {
169     struct ofp_prop_header *oph;
170
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));
174 }
175
176 enum ofperr
177 ofpprop_unknown(struct vlog_module *module, bool loose, const char *msg,
178                 uint64_t type)
179 {
180     bool is_experimenter = ofpprop_is_experimenter(type);
181
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,
186                         msg, type);
187     } else {
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));
193     }
194
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. */
199     return (loose ? 0
200             : is_experimenter ? OFPERR_OFPBPC_BAD_EXP_TYPE
201             : OFPERR_OFPBPC_BAD_TYPE);
202 }
203