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