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