Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / lib / ofp-errors.c
1 /*
2  * Copyright (c) 2012, 2013, 2014 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 "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     default:
56         return NULL;
57     }
58 }
59
60 /* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow version 'version'. */
61 const char *
62 ofperr_domain_get_name(enum ofp_version version)
63 {
64     const struct ofperr_domain *domain = ofperr_domain_from_version(version);
65     return domain ? domain->name : NULL;
66 }
67
68 /* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
69 bool
70 ofperr_is_valid(enum ofperr error)
71 {
72     return error >= OFPERR_OFS && error < OFPERR_OFS + OFPERR_N_ERRORS;
73 }
74
75 /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
76  * 'version', or 0 if either no such OFPERR_* value exists or 'version' is
77  * unknown. */
78 static enum ofperr
79 ofperr_decode(enum ofp_version version,
80               uint32_t vendor, uint16_t type, uint16_t code)
81 {
82     const struct ofperr_domain *domain = ofperr_domain_from_version(version);
83     return domain ? domain->decode(vendor, type, code) : 0;
84 }
85
86 /* Returns the name of 'error', e.g. "OFPBRC_BAD_TYPE" if 'error' is
87  * OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not a valid OFPERR_* value.
88  *
89  * Consider ofperr_to_string() instead, if the error code might be an errno
90  * value. */
91 const char *
92 ofperr_get_name(enum ofperr error)
93 {
94     return (ofperr_is_valid(error)
95             ? error_names[error - OFPERR_OFS]
96             : "<invalid>");
97 }
98
99 /* Returns the OFPERR_* value that corresponds for 'name', 0 if none exists.
100  * For example, returns OFPERR_OFPHFC_INCOMPATIBLE if 'name' is
101  * "OFPHFC_INCOMPATIBLE".
102  *
103  * This is probably useful only for debugging and testing. */
104 enum ofperr
105 ofperr_from_name(const char *name)
106 {
107     int i;
108
109     for (i = 0; i < OFPERR_N_ERRORS; i++) {
110         if (!strcmp(name, error_names[i])) {
111             return i + OFPERR_OFS;
112         }
113     }
114     return 0;
115 }
116
117 /* Returns an extended description name of 'error', e.g. "ofp_header.type not
118  * supported." if 'error' is OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not
119  * a valid OFPERR_* value. */
120 const char *
121 ofperr_get_description(enum ofperr error)
122 {
123     return (ofperr_is_valid(error)
124             ? error_comments[error - OFPERR_OFS]
125             : "<invalid>");
126 }
127
128 static const struct triplet *
129 ofperr_get_triplet__(enum ofperr error, const struct ofperr_domain *domain)
130 {
131     size_t ofs = error - OFPERR_OFS;
132
133     ovs_assert(ofperr_is_valid(error));
134     return &domain->errors[ofs];
135 }
136
137 static struct ofpbuf *
138 ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version,
139                     ovs_be32 xid, const void *data, size_t data_len)
140 {
141     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
142     const struct ofperr_domain *domain;
143     const struct triplet *triplet;
144     struct ofp_error_msg *oem;
145     struct ofpbuf *buf;
146
147     /* Get the error domain for 'ofp_version', or fall back to OF1.0. */
148     domain = ofperr_domain_from_version(ofp_version);
149     if (!domain) {
150         VLOG_ERR_RL(&rl, "cannot encode error for unknown OpenFlow "
151                     "version 0x%02x", ofp_version);
152         domain = &ofperr_of10;
153     }
154
155     /* Make sure 'error' is valid in 'domain', or use a fallback error. */
156     if (!ofperr_is_valid(error)) {
157         /* 'error' seems likely to be a system errno value. */
158         VLOG_ERR_RL(&rl, "invalid OpenFlow error code %d (%s)",
159                     error, ovs_strerror(error));
160         error = OFPERR_NXBRC_UNENCODABLE_ERROR;
161     } else if (domain->errors[error - OFPERR_OFS].code < 0) {
162         VLOG_ERR_RL(&rl, "cannot encode %s for %s",
163                     ofperr_get_name(error), domain->name);
164         error = OFPERR_NXBRC_UNENCODABLE_ERROR;
165     }
166
167     triplet = ofperr_get_triplet__(error, domain);
168     if (!triplet->vendor) {
169         buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
170                                sizeof *oem + data_len);
171
172         oem = ofpbuf_put_uninit(buf, sizeof *oem);
173         oem->type = htons(triplet->type);
174         oem->code = htons(triplet->code);
175     } else if (ofp_version <= OFP11_VERSION) {
176         struct nx_vendor_error *nve;
177
178         buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
179                                sizeof *oem + sizeof *nve + data_len);
180
181         oem = ofpbuf_put_uninit(buf, sizeof *oem);
182         oem->type = htons(NXET_VENDOR);
183         oem->code = htons(NXVC_VENDOR_ERROR);
184
185         nve = ofpbuf_put_uninit(buf, sizeof *nve);
186         nve->vendor = htonl(triplet->vendor);
187         nve->type = htons(triplet->type);
188         nve->code = htons(triplet->code);
189     } else {
190         ovs_be32 vendor = htonl(triplet->vendor);
191
192         buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
193                                sizeof *oem + sizeof(uint32_t) + data_len);
194
195         oem = ofpbuf_put_uninit(buf, sizeof *oem);
196         oem->type = htons(OFPET12_EXPERIMENTER);
197         oem->code = htons(triplet->type);
198         ofpbuf_put(buf, &vendor, sizeof vendor);
199     }
200
201     ofpbuf_put(buf, data, data_len);
202     ofpmsg_update_length(buf);
203
204     return buf;
205 }
206
207 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
208  * given 'error'.
209  *
210  * 'oh->version' determines the OpenFlow version of the error reply.
211  * 'oh->xid' determines the xid of the error reply.
212  * The error reply will contain an initial subsequence of 'oh', up to
213  * 'oh->length' or 64 bytes, whichever is shorter.
214  *
215  * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
216  * messages.  Use ofperr_encode_hello() instead. */
217 struct ofpbuf *
218 ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh)
219 {
220     uint16_t len = ntohs(oh->length);
221
222     return ofperr_encode_msg__(error, oh->version, oh->xid, oh, MIN(len, 64));
223 }
224
225 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
226  * given 'error', in the error domain 'domain'.  The error message will include
227  * the additional null-terminated text string 's'.
228  *
229  * If 'version' is an unknown version then OFP10_VERSION is used.
230  * OFPET_HELLO_FAILED error messages are supposed to be backward-compatible,
231  * so in theory this should work. */
232 struct ofpbuf *
233 ofperr_encode_hello(enum ofperr error, enum ofp_version ofp_version,
234                     const char *s)
235 {
236     return ofperr_encode_msg__(error, ofp_version, htonl(0), s, strlen(s));
237 }
238
239 int
240 ofperr_get_vendor(enum ofperr error, enum ofp_version version)
241 {
242     const struct ofperr_domain *domain = ofperr_domain_from_version(version);
243     return domain ? ofperr_get_triplet__(error, domain)->vendor : -1;
244 }
245
246 /* Returns the value that would go into an OFPT_ERROR message's 'type' for
247  * encoding 'error' in 'domain'.  Returns -1 if 'error' is not encodable in
248  * 'version' or 'version' is unknown.
249  *
250  * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
251 int
252 ofperr_get_type(enum ofperr error, enum ofp_version version)
253 {
254     const struct ofperr_domain *domain = ofperr_domain_from_version(version);
255     return domain ? ofperr_get_triplet__(error, domain)->type : -1;
256 }
257
258 /* Returns the value that would go into an OFPT_ERROR message's 'code' for
259  * encoding 'error' in 'domain'.  Returns -1 if 'error' is not encodable in
260  * 'version', 'version' is unknown or if 'error' represents a category
261  * rather than a specific error.
262  *
263  *
264  * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
265 int
266 ofperr_get_code(enum ofperr error, enum ofp_version version)
267 {
268     const struct ofperr_domain *domain = ofperr_domain_from_version(version);
269     return domain ? ofperr_get_triplet__(error, domain)->code : -1;
270 }
271
272 /* Tries to decode 'oh', which should be an OpenFlow OFPT_ERROR message.
273  * Returns an OFPERR_* constant on success, 0 on failure.
274  *
275  * If 'payload' is nonnull, on success '*payload' is initialized with a copy of
276  * the error's payload (copying is required because the payload is not properly
277  * aligned).  The caller must free the payload (with ofpbuf_uninit()) when it
278  * is no longer needed.  On failure, '*payload' is cleared. */
279 enum ofperr
280 ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
281 {
282     const struct ofp_error_msg *oem;
283     enum ofpraw raw;
284     uint16_t type, code;
285     enum ofperr error;
286     uint32_t vendor;
287     struct ofpbuf b;
288
289     if (payload) {
290         memset(payload, 0, sizeof *payload);
291     }
292
293     /* Pull off the error message. */
294     ofpbuf_use_const(&b, oh, ntohs(oh->length));
295     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, ofpbuf_size(&b));
329         ofpbuf_push(payload, ofpbuf_data(&b), ofpbuf_size(&b));
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 }