ofp-errors: Remove OFPERR_* values for error categories.
[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' can be encoded as an OpenFlow error message in
57  * 'domain', false otherwise.
58  *
59  * A given error may not be encodable in some domains because each OpenFlow
60  * version tends to introduce new errors and retire some old ones. */
61 bool
62 ofperr_is_encodable(enum ofperr error, enum ofp_version version)
63 {
64     const struct ofperr_domain *domain = ofperr_domain_from_version(version);
65     return (ofperr_is_valid(error)
66             && domain && domain->errors[error - OFPERR_OFS].code >= 0);
67 }
68
69 /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
70  * 'version', or 0 if either no such OFPERR_* value exists or 'version' is
71  * unknown. */
72 enum ofperr
73 ofperr_decode(enum ofp_version version, uint16_t type, uint16_t code)
74 {
75     const struct ofperr_domain *domain = ofperr_domain_from_version(version);
76     return domain ? domain->decode(type, code) : 0;
77 }
78
79 /* Returns the name of 'error', e.g. "OFPBRC_BAD_TYPE" if 'error' is
80  * OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not a valid OFPERR_* value.
81  *
82  * Consider ofperr_to_string() instead, if the error code might be an errno
83  * value. */
84 const char *
85 ofperr_get_name(enum ofperr error)
86 {
87     return (ofperr_is_valid(error)
88             ? error_names[error - OFPERR_OFS]
89             : "<invalid>");
90 }
91
92 /* Returns the OFPERR_* value that corresponds for 'name', 0 if none exists.
93  * For example, returns OFPERR_OFPHFC_INCOMPATIBLE if 'name' is
94  * "OFPHFC_INCOMPATIBLE".
95  *
96  * This is probably useful only for debugging and testing. */
97 enum ofperr
98 ofperr_from_name(const char *name)
99 {
100     int i;
101
102     for (i = 0; i < OFPERR_N_ERRORS; i++) {
103         if (!strcmp(name, error_names[i])) {
104             return i + OFPERR_OFS;
105         }
106     }
107     return 0;
108 }
109
110 /* Returns an extended description name of 'error', e.g. "ofp_header.type not
111  * supported." if 'error' is OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not
112  * a valid OFPERR_* value. */
113 const char *
114 ofperr_get_description(enum ofperr error)
115 {
116     return (ofperr_is_valid(error)
117             ? error_comments[error - OFPERR_OFS]
118             : "<invalid>");
119 }
120
121 static const struct pair *
122 ofperr_get_pair__(enum ofperr error, const struct ofperr_domain *domain)
123 {
124     size_t ofs = error - OFPERR_OFS;
125
126     assert(ofperr_is_valid(error));
127     return &domain->errors[ofs];
128 }
129
130 static struct ofpbuf *
131 ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version,
132                     ovs_be32 xid, const void *data, size_t data_len)
133 {
134     struct ofp_error_msg *oem;
135     const struct pair *pair;
136     struct ofpbuf *buf;
137     const struct ofperr_domain *domain;
138
139     domain = ofperr_domain_from_version(ofp_version);
140     if (!domain) {
141         return NULL;
142     }
143
144     if (!ofperr_is_encodable(error, ofp_version)) {
145         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
146
147         if (!ofperr_is_valid(error)) {
148             /* 'error' seems likely to be a system errno value. */
149             VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
150                          error, strerror(error));
151         } else {
152             const char *s = ofperr_get_name(error);
153             if (ofperr_is_category(error)) {
154                 VLOG_WARN_RL(&rl, "cannot encode error category (%s)", s);
155             } else {
156                 VLOG_WARN_RL(&rl, "cannot encode %s for %s", s, domain->name);
157             }
158         }
159
160         return NULL;
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  * Returns NULL if 'error' is not an OpenFlow error code or if 'error' cannot
202  * be encoded as OpenFlow version 'oh->version'.
203  *
204  * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
205  * messages.  Use ofperr_encode_hello() instead. */
206 struct ofpbuf *
207 ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh)
208 {
209     uint16_t len = ntohs(oh->length);
210
211     return ofperr_encode_msg__(error, oh->version, oh->xid, oh, MIN(len, 64));
212 }
213
214 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
215  * given 'error', in the error domain 'domain'.  The error message will include
216  * the additional null-terminated text string 's'.
217  *
218  * If 'version' is an unknown version then OFP10_VERSION is used.
219  * OFPET_HELLO_FAILED error messages are supposed to be backward-compatible,
220  * so in theory this should work.
221  *
222  * Returns NULL if 'error' is not an OpenFlow error code or if 'error' cannot
223  * be encoded in 'domain'. */
224 struct ofpbuf *
225 ofperr_encode_hello(enum ofperr error, enum ofp_version ofp_version,
226                     const char *s)
227 {
228     switch (ofp_version) {
229     case OFP10_VERSION:
230     case OFP11_VERSION:
231     case OFP12_VERSION:
232     case OFP13_VERSION:
233         break;
234
235     default:
236         ofp_version = OFP10_VERSION;
237     }
238
239     return ofperr_encode_msg__(error, ofp_version, htonl(0), s, strlen(s));
240 }
241
242 /* Returns the value that would go into an OFPT_ERROR message's 'type' for
243  * encoding 'error' in 'domain'.  Returns -1 if 'error' is not encodable in
244  * 'version' or 'version' is unknown.
245  *
246  * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
247 int
248 ofperr_get_type(enum ofperr error, enum ofp_version version)
249 {
250     const struct ofperr_domain *domain = ofperr_domain_from_version(version);
251     return domain ? ofperr_get_pair__(error, domain)->type : -1;
252 }
253
254 /* Returns the value that would go into an OFPT_ERROR message's 'code' for
255  * encoding 'error' in 'domain'.  Returns -1 if 'error' is not encodable in
256  * 'version', 'version' is unknown or if 'error' represents a category
257  * rather than a specific error.
258  *
259  *
260  * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
261 int
262 ofperr_get_code(enum ofperr error, enum ofp_version version)
263 {
264     const struct ofperr_domain *domain = ofperr_domain_from_version(version);
265     return domain ? ofperr_get_pair__(error, domain)->code : -1;
266 }
267
268 /* Tries to decode 'oh', which should be an OpenFlow OFPT_ERROR message.
269  * Returns an OFPERR_* constant on success, 0 on failure.
270  *
271  * If 'payload' is nonnull, on success '*payload' is initialized to the
272  * error's payload, and on failure it is cleared. */
273 enum ofperr
274 ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
275 {
276     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
277
278     const struct ofp_error_msg *oem;
279     enum ofpraw raw;
280     uint16_t type, code;
281     enum ofperr error;
282     struct ofpbuf b;
283
284     if (payload) {
285         memset(payload, 0, sizeof *payload);
286     }
287
288     /* Pull off the error message. */
289     ofpbuf_use_const(&b, oh, ntohs(oh->length));
290     error = ofpraw_pull(&raw, &b);
291     if (error) {
292         return 0;
293     }
294     oem = ofpbuf_pull(&b, sizeof *oem);
295
296     /* Get the error type and code. */
297     type = ntohs(oem->type);
298     code = ntohs(oem->code);
299     if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
300         const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
301         if (!nve) {
302             return 0;
303         }
304
305         if (nve->vendor != htonl(NX_VENDOR_ID)) {
306             VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
307                          ntohl(nve->vendor));
308             return 0;
309         }
310         type = ntohs(nve->type);
311         code = ntohs(nve->code);
312     }
313
314     /* Translate the error type and code into an ofperr. */
315     error = ofperr_decode(oh->version, type, code);
316     if (error && payload) {
317         ofpbuf_use_const(payload, b.data, b.size);
318     }
319     return error;
320 }
321
322 /* If 'error' is a valid OFPERR_* value, returns its name
323  * (e.g. "OFPBRC_BAD_TYPE" for OFPBRC_BAD_TYPE).  Otherwise, assumes that
324  * 'error' is a positive errno value and returns what strerror() produces for
325  * 'error'.  */
326 const char *
327 ofperr_to_string(enum ofperr error)
328 {
329     return ofperr_is_valid(error) ? ofperr_get_name(error) : strerror(error);
330 }