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