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 static const struct ofperr_domain *
25 ofperr_domain_from_version(enum ofp_version version)
41 /* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow version 'version'. */
43 ofperr_domain_get_name(enum ofp_version version)
45 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
46 return domain ? domain->name : NULL;
49 /* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
51 ofperr_is_valid(enum ofperr error)
53 return error >= OFPERR_OFS && error < OFPERR_OFS + OFPERR_N_ERRORS;
56 /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
57 * 'version', or 0 if either no such OFPERR_* value exists or 'version' is
60 ofperr_decode(enum ofp_version version, uint16_t type, uint16_t code)
62 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
63 return domain ? domain->decode(type, code) : 0;
66 /* Returns the name of 'error', e.g. "OFPBRC_BAD_TYPE" if 'error' is
67 * OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not a valid OFPERR_* value.
69 * Consider ofperr_to_string() instead, if the error code might be an errno
72 ofperr_get_name(enum ofperr error)
74 return (ofperr_is_valid(error)
75 ? error_names[error - OFPERR_OFS]
79 /* Returns the OFPERR_* value that corresponds for 'name', 0 if none exists.
80 * For example, returns OFPERR_OFPHFC_INCOMPATIBLE if 'name' is
81 * "OFPHFC_INCOMPATIBLE".
83 * This is probably useful only for debugging and testing. */
85 ofperr_from_name(const char *name)
89 for (i = 0; i < OFPERR_N_ERRORS; i++) {
90 if (!strcmp(name, error_names[i])) {
91 return i + OFPERR_OFS;
97 /* Returns an extended description name of 'error', e.g. "ofp_header.type not
98 * supported." if 'error' is OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not
99 * a valid OFPERR_* value. */
101 ofperr_get_description(enum ofperr error)
103 return (ofperr_is_valid(error)
104 ? error_comments[error - OFPERR_OFS]
108 static const struct pair *
109 ofperr_get_pair__(enum ofperr error, const struct ofperr_domain *domain)
111 size_t ofs = error - OFPERR_OFS;
113 ovs_assert(ofperr_is_valid(error));
114 return &domain->errors[ofs];
117 static struct ofpbuf *
118 ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version,
119 ovs_be32 xid, const void *data, size_t data_len)
121 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
122 const struct ofperr_domain *domain;
123 struct ofp_error_msg *oem;
124 const struct pair *pair;
127 /* Get the error domain for 'ofp_version', or fall back to OF1.0. */
128 domain = ofperr_domain_from_version(ofp_version);
130 VLOG_ERR_RL(&rl, "cannot encode error for unknown OpenFlow "
131 "version 0x%02x", ofp_version);
132 domain = &ofperr_of10;
135 /* Make sure 'error' is valid in 'domain', or use a fallback error. */
136 if (!ofperr_is_valid(error)) {
137 /* 'error' seems likely to be a system errno value. */
138 VLOG_ERR_RL(&rl, "invalid OpenFlow error code %d (%s)",
139 error, strerror(error));
140 error = OFPERR_NXBRC_UNENCODABLE_ERROR;
141 } else if (domain->errors[error - OFPERR_OFS].code < 0) {
142 VLOG_ERR_RL(&rl, "cannot encode %s for %s",
143 ofperr_get_name(error), domain->name);
144 error = OFPERR_NXBRC_UNENCODABLE_ERROR;
147 pair = ofperr_get_pair__(error, domain);
148 if (pair->code < 0x100) {
149 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
150 sizeof *oem + data_len);
152 oem = ofpbuf_put_uninit(buf, sizeof *oem);
153 oem->type = htons(pair->type);
154 oem->code = htons(pair->code);
156 struct nx_vendor_error *nve;
158 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
159 sizeof *oem + sizeof *nve + data_len);
161 oem = ofpbuf_put_uninit(buf, sizeof *oem);
162 oem->type = htons(NXET_VENDOR);
163 oem->code = htons(NXVC_VENDOR_ERROR);
165 nve = ofpbuf_put_uninit(buf, sizeof *nve);
166 nve->vendor = htonl(NX_VENDOR_ID);
167 nve->type = htons(pair->type);
168 nve->code = htons(pair->code);
171 ofpbuf_put(buf, data, data_len);
172 ofpmsg_update_length(buf);
177 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
180 * 'oh->version' determines the OpenFlow version of the error reply.
181 * 'oh->xid' determines the xid of the error reply.
182 * The error reply will contain an initial subsequence of 'oh', up to
183 * 'oh->length' or 64 bytes, whichever is shorter.
185 * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
186 * messages. Use ofperr_encode_hello() instead. */
188 ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh)
190 uint16_t len = ntohs(oh->length);
192 return ofperr_encode_msg__(error, oh->version, oh->xid, oh, MIN(len, 64));
195 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
196 * given 'error', in the error domain 'domain'. The error message will include
197 * the additional null-terminated text string 's'.
199 * If 'version' is an unknown version then OFP10_VERSION is used.
200 * OFPET_HELLO_FAILED error messages are supposed to be backward-compatible,
201 * so in theory this should work. */
203 ofperr_encode_hello(enum ofperr error, enum ofp_version ofp_version,
206 return ofperr_encode_msg__(error, ofp_version, htonl(0), s, strlen(s));
209 /* Returns the value that would go into an OFPT_ERROR message's 'type' for
210 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
211 * 'version' or 'version' is unknown.
213 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
215 ofperr_get_type(enum ofperr error, enum ofp_version version)
217 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
218 return domain ? ofperr_get_pair__(error, domain)->type : -1;
221 /* Returns the value that would go into an OFPT_ERROR message's 'code' for
222 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
223 * 'version', 'version' is unknown or if 'error' represents a category
224 * rather than a specific error.
227 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
229 ofperr_get_code(enum ofperr error, enum ofp_version version)
231 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
232 return domain ? ofperr_get_pair__(error, domain)->code : -1;
235 /* Tries to decode 'oh', which should be an OpenFlow OFPT_ERROR message.
236 * Returns an OFPERR_* constant on success, 0 on failure.
238 * If 'payload' is nonnull, on success '*payload' is initialized to the
239 * error's payload, and on failure it is cleared. */
241 ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
243 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
245 const struct ofp_error_msg *oem;
252 memset(payload, 0, sizeof *payload);
255 /* Pull off the error message. */
256 ofpbuf_use_const(&b, oh, ntohs(oh->length));
257 error = ofpraw_pull(&raw, &b);
261 oem = ofpbuf_pull(&b, sizeof *oem);
263 /* Get the error type and code. */
264 type = ntohs(oem->type);
265 code = ntohs(oem->code);
266 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
267 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
272 if (nve->vendor != htonl(NX_VENDOR_ID)) {
273 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
277 type = ntohs(nve->type);
278 code = ntohs(nve->code);
281 /* Translate the error type and code into an ofperr. */
282 error = ofperr_decode(oh->version, type, code);
283 if (error && payload) {
284 ofpbuf_use_const(payload, b.data, b.size);
289 /* If 'error' is a valid OFPERR_* value, returns its name
290 * (e.g. "OFPBRC_BAD_TYPE" for OFPBRC_BAD_TYPE). Otherwise, assumes that
291 * 'error' is a positive errno value and returns what strerror() produces for
294 ofperr_to_string(enum ofperr error)
296 return ofperr_is_valid(error) ? ofperr_get_name(error) : strerror(error);