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