netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / ofp-errors.c
1 /*
2  * Copyright (c) 2012, 2013, 2014, 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 #include "ofp-errors.h"
19 #include <errno.h>
20 #include "byte-order.h"
21 #include "dynamic-string.h"
22 #include "ofp-msgs.h"
23 #include "ofp-util.h"
24 #include "ofpbuf.h"
25 #include "openflow/openflow.h"
26 #include "openvswitch/vlog.h"
27
28 VLOG_DEFINE_THIS_MODULE(ofp_errors);
29
30 struct triplet {
31     uint32_t vendor;
32     int type, code;
33 };
34
35 #include "ofp-errors.inc"
36
37 /* Returns an ofperr_domain that corresponds to the OpenFlow version number
38  * 'version' (one of the possible values of struct ofp_header's 'version'
39  * member).  Returns NULL if the version isn't defined or isn't understood by
40  * OVS. */
41 static const struct ofperr_domain *
42 ofperr_domain_from_version(enum ofp_version version)
43 {
44     switch (version) {
45     case OFP10_VERSION:
46         return &ofperr_of10;
47     case OFP11_VERSION:
48         return &ofperr_of11;
49     case OFP12_VERSION:
50         return &ofperr_of12;
51     case OFP13_VERSION:
52         return &ofperr_of13;
53     case OFP14_VERSION:
54         return &ofperr_of14;
55     case OFP15_VERSION:
56         return &ofperr_of15;
57     default:
58         return NULL;
59     }
60 }
61
62 /* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow version 'version'. */
63 const char *
64 ofperr_domain_get_name(enum ofp_version version)
65 {
66     const struct ofperr_domain *domain = ofperr_domain_from_version(version);
67     return domain ? domain->name : NULL;
68 }
69
70 /* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
71 bool
72 ofperr_is_valid(enum ofperr error)
73 {
74     return error >= OFPERR_OFS && error < OFPERR_OFS + OFPERR_N_ERRORS;
75 }
76
77 /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
78  * 'version', or 0 if either no such OFPERR_* value exists or 'version' is
79  * unknown. */
80 static enum ofperr
81 ofperr_decode(enum ofp_version version,
82               uint32_t vendor, uint16_t type, uint16_t code)
83 {
84     const struct ofperr_domain *domain = ofperr_domain_from_version(version);
85     return domain ? domain->decode(vendor, type, code) : 0;
86 }
87
88 /* Returns the name of 'error', e.g. "OFPBRC_BAD_TYPE" if 'error' is
89  * OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not a valid OFPERR_* value.
90  *
91  * Consider ofperr_to_string() instead, if the error code might be an errno
92  * value. */
93 const char *
94 ofperr_get_name(enum ofperr error)
95 {
96     return (ofperr_is_valid(error)
97             ? error_names[error - OFPERR_OFS]
98             : "<invalid>");
99 }
100
101 /* Returns the OFPERR_* value that corresponds for 'name', 0 if none exists.
102  * For example, returns OFPERR_OFPHFC_INCOMPATIBLE if 'name' is
103  * "OFPHFC_INCOMPATIBLE".
104  *
105  * This is probably useful only for debugging and testing. */
106 enum ofperr
107 ofperr_from_name(const char *name)
108 {
109     int i;
110
111     for (i = 0; i < OFPERR_N_ERRORS; i++) {
112         if (!strcmp(name, error_names[i])) {
113             return i + OFPERR_OFS;
114         }
115     }
116     return 0;
117 }
118
119 /* Returns an extended description name of 'error', e.g. "ofp_header.type not
120  * supported." if 'error' is OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not
121  * a valid OFPERR_* value. */
122 const char *
123 ofperr_get_description(enum ofperr error)
124 {
125     return (ofperr_is_valid(error)
126             ? error_comments[error - OFPERR_OFS]
127             : "<invalid>");
128 }
129
130 static const struct triplet *
131 ofperr_get_triplet__(enum ofperr error, const struct ofperr_domain *domain)
132 {
133     size_t ofs = error - OFPERR_OFS;
134
135     ovs_assert(ofperr_is_valid(error));
136     return &domain->errors[ofs];
137 }
138
139 static struct ofpbuf *
140 ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version,
141                     ovs_be32 xid, const void *data, size_t data_len)
142 {
143     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
144     const struct ofperr_domain *domain;
145     const struct triplet *triplet;
146     struct ofp_error_msg *oem;
147     struct ofpbuf *buf;
148
149     /* Get the error domain for 'ofp_version', or fall back to OF1.0. */
150     domain = ofperr_domain_from_version(ofp_version);
151     if (!domain) {
152         VLOG_ERR_RL(&rl, "cannot encode error for unknown OpenFlow "
153                     "version 0x%02x", ofp_version);
154         domain = &ofperr_of10;
155     }
156
157     /* Make sure 'error' is valid in 'domain', or use a fallback error. */
158     if (!ofperr_is_valid(error)) {
159         /* 'error' seems likely to be a system errno value. */
160         VLOG_ERR_RL(&rl, "invalid OpenFlow error code %d (%s)",
161                     error, ovs_strerror(error));
162         error = OFPERR_NXBRC_UNENCODABLE_ERROR;
163     } else if (domain->errors[error - OFPERR_OFS].code < 0) {
164         VLOG_ERR_RL(&rl, "cannot encode %s for %s",
165                     ofperr_get_name(error), domain->name);
166         error = OFPERR_NXBRC_UNENCODABLE_ERROR;
167     }
168
169     triplet = ofperr_get_triplet__(error, domain);
170     if (!triplet->vendor) {
171         buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
172                                sizeof *oem + data_len);
173
174         oem = ofpbuf_put_uninit(buf, sizeof *oem);
175         oem->type = htons(triplet->type);
176         oem->code = htons(triplet->code);
177     } else if (ofp_version <= OFP11_VERSION) {
178         struct nx_vendor_error *nve;
179
180         buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
181                                sizeof *oem + sizeof *nve + data_len);
182
183         oem = ofpbuf_put_uninit(buf, sizeof *oem);
184         oem->type = htons(NXET_VENDOR);
185         oem->code = htons(NXVC_VENDOR_ERROR);
186
187         nve = ofpbuf_put_uninit(buf, sizeof *nve);
188         nve->vendor = htonl(triplet->vendor);
189         nve->type = htons(triplet->type);
190         nve->code = htons(triplet->code);
191     } else {
192         ovs_be32 vendor = htonl(triplet->vendor);
193
194         buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
195                                sizeof *oem + sizeof(uint32_t) + data_len);
196
197         oem = ofpbuf_put_uninit(buf, sizeof *oem);
198         oem->type = htons(OFPET12_EXPERIMENTER);
199         oem->code = htons(triplet->type);
200         ofpbuf_put(buf, &vendor, sizeof vendor);
201     }
202
203     ofpbuf_put(buf, data, data_len);
204     ofpmsg_update_length(buf);
205
206     return buf;
207 }
208
209 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
210  * given 'error'.
211  *
212  * 'oh->version' determines the OpenFlow version of the error reply.
213  * 'oh->xid' determines the xid of the error reply.
214  * The error reply will contain an initial subsequence of 'oh', up to
215  * 'oh->length' or 64 bytes, whichever is shorter.
216  *
217  * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
218  * messages.  Use ofperr_encode_hello() instead. */
219 struct ofpbuf *
220 ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh)
221 {
222     uint16_t len = ntohs(oh->length);
223
224     return ofperr_encode_msg__(error, oh->version, oh->xid, oh, MIN(len, 64));
225 }
226
227 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
228  * given 'error', in the error domain 'domain'.  The error message will include
229  * the additional null-terminated text string 's'.
230  *
231  * If 'version' is an unknown version then OFP10_VERSION is used.
232  * OFPET_HELLO_FAILED error messages are supposed to be backward-compatible,
233  * so in theory this should work. */
234 struct ofpbuf *
235 ofperr_encode_hello(enum ofperr error, enum ofp_version ofp_version,
236                     const char *s)
237 {
238     return ofperr_encode_msg__(error, ofp_version, htonl(0), s, strlen(s));
239 }
240
241 int
242 ofperr_get_vendor(enum ofperr error, enum ofp_version version)
243 {
244     const struct ofperr_domain *domain = ofperr_domain_from_version(version);
245     return domain ? ofperr_get_triplet__(error, domain)->vendor : -1;
246 }
247
248 /* Returns the value that would go into an OFPT_ERROR message's 'type' for
249  * encoding 'error' in 'domain'.  Returns -1 if 'error' is not encodable in
250  * 'version' or 'version' is unknown.
251  *
252  * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
253 int
254 ofperr_get_type(enum ofperr error, enum ofp_version version)
255 {
256     const struct ofperr_domain *domain = ofperr_domain_from_version(version);
257     return domain ? ofperr_get_triplet__(error, domain)->type : -1;
258 }
259
260 /* Returns the value that would go into an OFPT_ERROR message's 'code' for
261  * encoding 'error' in 'domain'.  Returns -1 if 'error' is not encodable in
262  * 'version', 'version' is unknown or if 'error' represents a category
263  * rather than a specific error.
264  *
265  *
266  * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
267 int
268 ofperr_get_code(enum ofperr error, enum ofp_version version)
269 {
270     const struct ofperr_domain *domain = ofperr_domain_from_version(version);
271     return domain ? ofperr_get_triplet__(error, domain)->code : -1;
272 }
273
274 /* Tries to decode 'oh', which should be an OpenFlow OFPT_ERROR message.
275  * Returns an OFPERR_* constant on success, 0 on failure.
276  *
277  * If 'payload' is nonnull, on success '*payload' is initialized with a copy of
278  * the error's payload (copying is required because the payload is not properly
279  * aligned).  The caller must free the payload (with ofpbuf_uninit()) when it
280  * is no longer needed.  On failure, '*payload' is cleared. */
281 enum ofperr
282 ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
283 {
284     const struct ofp_error_msg *oem;
285     enum ofpraw raw;
286     uint16_t type, code;
287     uint32_t vendor;
288
289     if (payload) {
290         memset(payload, 0, sizeof *payload);
291     }
292
293     /* Pull off the error message. */
294     struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
295     enum ofperr error = ofpraw_pull(&raw, &b);
296     if (error) {
297         return 0;
298     }
299     oem = ofpbuf_pull(&b, sizeof *oem);
300
301     /* Get the error type and code. */
302     vendor = 0;
303     type = ntohs(oem->type);
304     code = ntohs(oem->code);
305     if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
306         const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
307         if (!nve) {
308             return 0;
309         }
310
311         vendor = ntohl(nve->vendor);
312         type = ntohs(nve->type);
313         code = ntohs(nve->code);
314     } else if (type == OFPET12_EXPERIMENTER) {
315         const ovs_be32 *vendorp = ofpbuf_try_pull(&b, sizeof *vendorp);
316         if (!vendorp) {
317             return 0;
318         }
319
320         vendor = ntohl(*vendorp);
321         type = code;
322         code = 0;
323     }
324
325     /* Translate the error type and code into an ofperr. */
326     error = ofperr_decode(oh->version, vendor, type, code);
327     if (error && payload) {
328         ofpbuf_init(payload, b.size);
329         ofpbuf_push(payload, b.data, b.size);
330     }
331     return error;
332 }
333
334 /* If 'error' is a valid OFPERR_* value, returns its name
335  * (e.g. "OFPBRC_BAD_TYPE" for OFPBRC_BAD_TYPE).  Otherwise, assumes that
336  * 'error' is a positive errno value and returns what ovs_strerror() produces
337  * for 'error'.  */
338 const char *
339 ofperr_to_string(enum ofperr error)
340 {
341     return (ofperr_is_valid(error)
342             ? ofperr_get_name(error)
343             : ovs_strerror(error));
344 }