2 #include "ofp-errors.h"
4 #include "byte-order.h"
5 #include "dynamic-string.h"
9 #include "openflow/openflow.h"
12 VLOG_DEFINE_THIS_MODULE(ofp_errors);
18 #include "ofp-errors.inc"
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
24 const struct ofperr_domain *
25 ofperr_domain_from_version(uint8_t version)
27 return (version == ofperr_of10.version ? &ofperr_of10
28 : version == ofperr_of11.version ? &ofperr_of11
29 : version == ofperr_of12.version ? &ofperr_of12
33 /* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow error domain 'domain'. */
35 ofperr_domain_get_name(const struct ofperr_domain *domain)
40 /* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
42 ofperr_is_valid(enum ofperr error)
44 return error >= OFPERR_OFS && error < OFPERR_OFS + OFPERR_N_ERRORS;
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. */
51 ofperr_is_category(enum ofperr error)
53 return (ofperr_is_valid(error)
54 && ofperr_of10.errors[error - OFPERR_OFS].code == -1
55 && ofperr_of11.errors[error - OFPERR_OFS].code == -1);
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. */
61 ofperr_is_nx_extension(enum ofperr error)
63 return (ofperr_is_valid(error)
64 && (ofperr_of10.errors[error - OFPERR_OFS].code >= 0x100 ||
65 ofperr_of11.errors[error - OFPERR_OFS].code >= 0x100));
68 /* Returns true if 'error' can be encoded as an OpenFlow error message in
69 * 'domain', false otherwise.
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. */
74 ofperr_is_encodable(enum ofperr error, const struct ofperr_domain *domain)
76 return (ofperr_is_valid(error)
77 && domain->errors[error - OFPERR_OFS].code >= 0);
80 /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
81 * 'domain', or 0 if no such OFPERR_* value exists. */
83 ofperr_decode(const struct ofperr_domain *domain, uint16_t type, uint16_t code)
85 return domain->decode(type, code);
88 /* Returns the OFPERR_* value that corresponds to the category 'type' within
89 * 'domain', or 0 if no such OFPERR_* value exists. */
91 ofperr_decode_type(const struct ofperr_domain *domain, uint16_t type)
93 return domain->decode_type(type);
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.
99 * Consider ofperr_to_string() instead, if the error code might be an errno
102 ofperr_get_name(enum ofperr error)
104 return (ofperr_is_valid(error)
105 ? error_names[error - OFPERR_OFS]
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".
113 * This is probably useful only for debugging and testing. */
115 ofperr_from_name(const char *name)
119 for (i = 0; i < OFPERR_N_ERRORS; i++) {
120 if (!strcmp(name, error_names[i])) {
121 return i + OFPERR_OFS;
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. */
131 ofperr_get_description(enum ofperr error)
133 return (ofperr_is_valid(error)
134 ? error_comments[error - OFPERR_OFS]
138 static const struct pair *
139 ofperr_get_pair__(enum ofperr error, const struct ofperr_domain *domain)
141 size_t ofs = error - OFPERR_OFS;
143 assert(ofperr_is_valid(error));
144 return &domain->errors[ofs];
147 static struct ofpbuf *
148 ofperr_encode_msg__(enum ofperr error, const struct ofperr_domain *domain,
149 ovs_be32 xid, const void *data, size_t data_len)
151 struct ofp_error_msg *oem;
152 const struct pair *pair;
159 if (!ofperr_is_encodable(error, domain)) {
160 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
162 if (!ofperr_is_valid(error)) {
163 /* 'error' seems likely to be a system errno value. */
164 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
165 error, strerror(error));
167 const char *s = ofperr_get_name(error);
168 if (ofperr_is_category(error)) {
169 VLOG_WARN_RL(&rl, "cannot encode error category (%s)", s);
171 VLOG_WARN_RL(&rl, "cannot encode %s for %s", s, domain->name);
178 pair = ofperr_get_pair__(error, domain);
179 if (!ofperr_is_nx_extension(error)) {
180 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
181 sizeof *oem + data_len);
183 oem = ofpbuf_put_uninit(buf, sizeof *oem);
184 oem->type = htons(pair->type);
185 oem->code = htons(pair->code);
187 struct nx_vendor_error *nve;
189 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
190 sizeof *oem + sizeof *nve + data_len);
192 oem = ofpbuf_put_uninit(buf, sizeof *oem);
193 oem->type = htons(NXET_VENDOR);
194 oem->code = htons(NXVC_VENDOR_ERROR);
196 nve = ofpbuf_put_uninit(buf, sizeof *nve);
197 nve->vendor = htonl(NX_VENDOR_ID);
198 nve->type = htons(pair->type);
199 nve->code = htons(pair->code);
202 ofpbuf_put(buf, data, data_len);
207 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
210 * 'oh->version' determines the OpenFlow version of the error reply.
211 * 'oh->xid' determines the xid of the error reply.
212 * The error reply will contain an initial subsequence of 'oh', up to
213 * 'oh->length' or 64 bytes, whichever is shorter.
215 * Returns NULL if 'error' is not an OpenFlow error code or if 'error' cannot
216 * be encoded as OpenFlow version 'oh->version'.
218 * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
219 * messages. Use ofperr_encode_hello() instead. */
221 ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh)
223 const struct ofperr_domain *domain;
224 uint16_t len = ntohs(oh->length);
226 domain = ofperr_domain_from_version(oh->version);
227 return ofperr_encode_msg__(error, domain, oh->xid, oh, MIN(len, 64));
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'.
234 * If 'domain' is NULL, uses the OpenFlow 1.0 error domain. OFPET_HELLO_FAILED
235 * error messages are supposed to be backward-compatible, so in theory this
238 * Returns NULL if 'error' is not an OpenFlow error code or if 'error' cannot
239 * be encoded in 'domain'. */
241 ofperr_encode_hello(enum ofperr error, const struct ofperr_domain *domain,
245 domain = &ofperr_of10;
247 return ofperr_encode_msg__(error, domain, htonl(0), s, strlen(s));
250 /* Returns the value that would go into an OFPT_ERROR message's 'type' for
251 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
254 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
256 ofperr_get_type(enum ofperr error, const struct ofperr_domain *domain)
258 return ofperr_get_pair__(error, domain)->type;
261 /* Returns the value that would go into an OFPT_ERROR message's 'code' for
262 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
263 * 'domain' or if 'error' represents a category rather than a specific error.
265 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
267 ofperr_get_code(enum ofperr error, const struct ofperr_domain *domain)
269 return ofperr_get_pair__(error, domain)->code;
272 /* Tries to decodes 'oh', which should be an OpenFlow OFPT_ERROR message.
273 * Returns an OFPERR_* constant on success, 0 on failure.
275 * If 'payload' is nonnull, on success '*payload' is initialized to the
276 * error's payload, and on failure it is cleared. */
278 ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
280 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
282 const struct ofperr_domain *domain;
283 const struct ofp_error_msg *oem;
290 memset(payload, 0, sizeof *payload);
293 /* Pull off the error message. */
294 ofpbuf_use_const(&b, oh, ntohs(oh->length));
295 error = ofpraw_pull(&raw, &b);
299 oem = ofpbuf_pull(&b, sizeof *oem);
302 domain = ofperr_domain_from_version(oh->version);
307 /* Get the error type and code. */
308 type = ntohs(oem->type);
309 code = ntohs(oem->code);
310 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
311 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
316 if (nve->vendor != htonl(NX_VENDOR_ID)) {
317 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
321 type = ntohs(nve->type);
322 code = ntohs(nve->code);
325 /* Translate the error type and code into an ofperr.
326 * If we don't know the error type and code, at least try for the type. */
327 error = ofperr_decode(domain, type, code);
329 error = ofperr_decode_type(domain, type);
331 if (error && payload) {
332 ofpbuf_use_const(payload, b.data, b.size);
337 /* If 'error' is a valid OFPERR_* value, returns its name
338 * (e.g. "OFPBRC_BAD_TYPE" for OFPBRC_BAD_TYPE). Otherwise, assumes that
339 * 'error' is a positive errno value and returns what strerror() produces for
342 ofperr_to_string(enum ofperr error)
344 return ofperr_is_valid(error) ? ofperr_get_name(error) : strerror(error);