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, enum ofp_version ofp_version,
149 ovs_be32 xid, const void *data, size_t data_len)
151 struct ofp_error_msg *oem;
152 const struct pair *pair;
154 const struct ofperr_domain *domain;
156 domain = ofperr_domain_from_version(ofp_version);
161 if (!ofperr_is_encodable(error, domain)) {
162 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
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));
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);
173 VLOG_WARN_RL(&rl, "cannot encode %s for %s", s, domain->name);
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);
185 oem = ofpbuf_put_uninit(buf, sizeof *oem);
186 oem->type = htons(pair->type);
187 oem->code = htons(pair->code);
189 struct nx_vendor_error *nve;
191 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
192 sizeof *oem + sizeof *nve + data_len);
194 oem = ofpbuf_put_uninit(buf, sizeof *oem);
195 oem->type = htons(NXET_VENDOR);
196 oem->code = htons(NXVC_VENDOR_ERROR);
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);
204 ofpbuf_put(buf, data, data_len);
209 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
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.
217 * Returns NULL if 'error' is not an OpenFlow error code or if 'error' cannot
218 * be encoded as OpenFlow version 'oh->version'.
220 * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
221 * messages. Use ofperr_encode_hello() instead. */
223 ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh)
225 uint16_t len = ntohs(oh->length);
227 return ofperr_encode_msg__(error, oh->version, 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 '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.
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, enum ofp_version ofp_version,
244 switch (ofp_version) {
251 ofp_version = OFP10_VERSION;
254 return ofperr_encode_msg__(error, ofp_version, htonl(0), s, strlen(s));
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
261 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
263 ofperr_get_type(enum ofperr error, const struct ofperr_domain *domain)
265 return ofperr_get_pair__(error, domain)->type;
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.
272 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
274 ofperr_get_code(enum ofperr error, const struct ofperr_domain *domain)
276 return ofperr_get_pair__(error, domain)->code;
279 /* Tries to decodes 'oh', which should be an OpenFlow OFPT_ERROR message.
280 * Returns an OFPERR_* constant on success, 0 on failure.
282 * If 'payload' is nonnull, on success '*payload' is initialized to the
283 * error's payload, and on failure it is cleared. */
285 ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
287 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
289 const struct ofperr_domain *domain;
290 const struct ofp_error_msg *oem;
297 memset(payload, 0, sizeof *payload);
300 /* Pull off the error message. */
301 ofpbuf_use_const(&b, oh, ntohs(oh->length));
302 error = ofpraw_pull(&raw, &b);
306 oem = ofpbuf_pull(&b, sizeof *oem);
309 domain = ofperr_domain_from_version(oh->version);
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);
323 if (nve->vendor != htonl(NX_VENDOR_ID)) {
324 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
328 type = ntohs(nve->type);
329 code = ntohs(nve->code);
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);
336 error = ofperr_decode_type(domain, type);
338 if (error && payload) {
339 ofpbuf_use_const(payload, b.data, b.size);
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
349 ofperr_to_string(enum ofperr error)
351 return ofperr_is_valid(error) ? ofperr_get_name(error) : strerror(error);