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