2 #include "ofp-errors.h"
4 #include "byte-order.h"
5 #include "dynamic-string.h"
8 #include "openflow/openflow.h"
11 VLOG_DEFINE_THIS_MODULE(ofp_errors);
17 #include "ofp-errors.inc"
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
23 const struct ofperr_domain *
24 ofperr_domain_from_version(uint8_t version)
26 return (version == ofperr_of10.version ? &ofperr_of10
27 : version == ofperr_of11.version ? &ofperr_of11
28 : version == ofperr_of12.version ? &ofperr_of12
32 /* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow error domain 'domain'. */
34 ofperr_domain_get_name(const struct ofperr_domain *domain)
39 /* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
41 ofperr_is_valid(enum ofperr error)
43 return error >= OFPERR_OFS && error < OFPERR_OFS + OFPERR_N_ERRORS;
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. */
50 ofperr_is_category(enum ofperr error)
52 return (ofperr_is_valid(error)
53 && ofperr_of10.errors[error - OFPERR_OFS].code == -1
54 && ofperr_of11.errors[error - OFPERR_OFS].code == -1);
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. */
60 ofperr_is_nx_extension(enum ofperr error)
62 return (ofperr_is_valid(error)
63 && (ofperr_of10.errors[error - OFPERR_OFS].code >= 0x100 ||
64 ofperr_of11.errors[error - OFPERR_OFS].code >= 0x100));
67 /* Returns true if 'error' can be encoded as an OpenFlow error message in
68 * 'domain', false otherwise.
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. */
73 ofperr_is_encodable(enum ofperr error, const struct ofperr_domain *domain)
75 return (ofperr_is_valid(error)
76 && domain->errors[error - OFPERR_OFS].code >= 0);
79 /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
80 * 'domain', or 0 if no such OFPERR_* value exists. */
82 ofperr_decode(const struct ofperr_domain *domain, uint16_t type, uint16_t code)
84 return domain->decode(type, code);
87 /* Returns the OFPERR_* value that corresponds to the category 'type' within
88 * 'domain', or 0 if no such OFPERR_* value exists. */
90 ofperr_decode_type(const struct ofperr_domain *domain, uint16_t type)
92 return domain->decode_type(type);
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.
98 * Consider ofperr_to_string() instead, if the error code might be an errno
101 ofperr_get_name(enum ofperr error)
103 return (ofperr_is_valid(error)
104 ? error_names[error - OFPERR_OFS]
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".
112 * This is probably useful only for debugging and testing. */
114 ofperr_from_name(const char *name)
118 for (i = 0; i < OFPERR_N_ERRORS; i++) {
119 if (!strcmp(name, error_names[i])) {
120 return i + OFPERR_OFS;
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. */
130 ofperr_get_description(enum ofperr error)
132 return (ofperr_is_valid(error)
133 ? error_comments[error - OFPERR_OFS]
137 static const struct pair *
138 ofperr_get_pair__(enum ofperr error, const struct ofperr_domain *domain)
140 size_t ofs = error - OFPERR_OFS;
142 assert(ofperr_is_valid(error));
143 return &domain->errors[ofs];
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)
150 struct ofp_error_msg *oem;
151 const struct pair *pair;
158 if (!ofperr_is_encodable(error, domain)) {
159 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
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));
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);
170 VLOG_WARN_RL(&rl, "cannot encode %s for %s", s, domain->name);
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);
183 struct nx_vendor_error *nve;
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);
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);
195 oem->header.version = domain->version;
197 buf->size -= data_len;
198 ofpbuf_put(buf, data, data_len);
203 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
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.
211 * Returns NULL if 'error' is not an OpenFlow error code or if 'error' cannot
212 * be encoded as OpenFlow version 'oh->version'.
214 * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
215 * messages. Use ofperr_encode_hello() instead. */
217 ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh)
219 const struct ofperr_domain *domain;
220 uint16_t len = ntohs(oh->length);
222 domain = ofperr_domain_from_version(oh->version);
223 return ofperr_encode_msg__(error, domain, oh->xid, oh, MIN(len, 64));
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'.
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
234 * Returns NULL if 'error' is not an OpenFlow error code or if 'error' cannot
235 * be encoded in 'domain'. */
237 ofperr_encode_hello(enum ofperr error, const struct ofperr_domain *domain,
241 domain = &ofperr_of10;
243 return ofperr_encode_msg__(error, domain, htonl(0), s, strlen(s));
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
250 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
252 ofperr_get_type(enum ofperr error, const struct ofperr_domain *domain)
254 return ofperr_get_pair__(error, domain)->type;
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.
261 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
263 ofperr_get_code(enum ofperr error, const struct ofperr_domain *domain)
265 return ofperr_get_pair__(error, domain)->code;
268 /* Tries to decodes 'oh', which should be an OpenFlow OFPT_ERROR message.
269 * Returns an OFPERR_* constant on success, 0 on failure.
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. */
274 ofperr_decode_msg(const struct ofp_header *oh, size_t *payload_ofs)
276 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
278 const struct ofperr_domain *domain;
279 const struct ofp_error_msg *oem;
288 /* Pull off the error message. */
289 ofpbuf_use_const(&b, oh, ntohs(oh->length));
290 oem = ofpbuf_try_pull(&b, sizeof *oem);
295 /* Check message type and version. */
296 if (oh->type != OFPT_ERROR) {
299 domain = ofperr_domain_from_version(oh->version);
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);
313 if (nve->vendor != htonl(NX_VENDOR_ID)) {
314 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
318 type = ntohs(nve->type);
319 code = ntohs(nve->code);
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);
326 error = ofperr_decode_type(domain, type);
328 if (error && payload_ofs) {
329 *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
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
339 ofperr_to_string(enum ofperr error)
341 return ofperr_is_valid(error) ? ofperr_get_name(error) : strerror(error);