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