2 * Copyright (c) 2012, 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-errors.h"
20 #include "byte-order.h"
21 #include "dynamic-string.h"
25 #include "openflow/openflow.h"
28 VLOG_DEFINE_THIS_MODULE(ofp_errors);
34 #include "ofp-errors.inc"
36 /* Returns an ofperr_domain that corresponds to the OpenFlow version number
37 * 'version' (one of the possible values of struct ofp_header's 'version'
38 * member). Returns NULL if the version isn't defined or isn't understood by
40 static const struct ofperr_domain *
41 ofperr_domain_from_version(enum ofp_version version)
57 /* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow version 'version'. */
59 ofperr_domain_get_name(enum ofp_version version)
61 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
62 return domain ? domain->name : NULL;
65 /* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
67 ofperr_is_valid(enum ofperr error)
69 return error >= OFPERR_OFS && error < OFPERR_OFS + OFPERR_N_ERRORS;
72 /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
73 * 'version', or 0 if either no such OFPERR_* value exists or 'version' is
76 ofperr_decode(enum ofp_version version, uint16_t type, uint16_t code)
78 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
79 return domain ? domain->decode(type, code) : 0;
82 /* Returns the name of 'error', e.g. "OFPBRC_BAD_TYPE" if 'error' is
83 * OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not a valid OFPERR_* value.
85 * Consider ofperr_to_string() instead, if the error code might be an errno
88 ofperr_get_name(enum ofperr error)
90 return (ofperr_is_valid(error)
91 ? error_names[error - OFPERR_OFS]
95 /* Returns the OFPERR_* value that corresponds for 'name', 0 if none exists.
96 * For example, returns OFPERR_OFPHFC_INCOMPATIBLE if 'name' is
97 * "OFPHFC_INCOMPATIBLE".
99 * This is probably useful only for debugging and testing. */
101 ofperr_from_name(const char *name)
105 for (i = 0; i < OFPERR_N_ERRORS; i++) {
106 if (!strcmp(name, error_names[i])) {
107 return i + OFPERR_OFS;
113 /* Returns an extended description name of 'error', e.g. "ofp_header.type not
114 * supported." if 'error' is OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not
115 * a valid OFPERR_* value. */
117 ofperr_get_description(enum ofperr error)
119 return (ofperr_is_valid(error)
120 ? error_comments[error - OFPERR_OFS]
124 static const struct pair *
125 ofperr_get_pair__(enum ofperr error, const struct ofperr_domain *domain)
127 size_t ofs = error - OFPERR_OFS;
129 ovs_assert(ofperr_is_valid(error));
130 return &domain->errors[ofs];
133 static struct ofpbuf *
134 ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version,
135 ovs_be32 xid, const void *data, size_t data_len)
137 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
138 const struct ofperr_domain *domain;
139 struct ofp_error_msg *oem;
140 const struct pair *pair;
143 /* Get the error domain for 'ofp_version', or fall back to OF1.0. */
144 domain = ofperr_domain_from_version(ofp_version);
146 VLOG_ERR_RL(&rl, "cannot encode error for unknown OpenFlow "
147 "version 0x%02x", ofp_version);
148 domain = &ofperr_of10;
151 /* Make sure 'error' is valid in 'domain', or use a fallback error. */
152 if (!ofperr_is_valid(error)) {
153 /* 'error' seems likely to be a system errno value. */
154 VLOG_ERR_RL(&rl, "invalid OpenFlow error code %d (%s)",
155 error, strerror(error));
156 error = OFPERR_NXBRC_UNENCODABLE_ERROR;
157 } else if (domain->errors[error - OFPERR_OFS].code < 0) {
158 VLOG_ERR_RL(&rl, "cannot encode %s for %s",
159 ofperr_get_name(error), domain->name);
160 error = OFPERR_NXBRC_UNENCODABLE_ERROR;
163 pair = ofperr_get_pair__(error, domain);
164 if (pair->code < 0x100) {
165 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
166 sizeof *oem + data_len);
168 oem = ofpbuf_put_uninit(buf, sizeof *oem);
169 oem->type = htons(pair->type);
170 oem->code = htons(pair->code);
172 struct nx_vendor_error *nve;
174 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
175 sizeof *oem + sizeof *nve + data_len);
177 oem = ofpbuf_put_uninit(buf, sizeof *oem);
178 oem->type = htons(NXET_VENDOR);
179 oem->code = htons(NXVC_VENDOR_ERROR);
181 nve = ofpbuf_put_uninit(buf, sizeof *nve);
182 nve->vendor = htonl(NX_VENDOR_ID);
183 nve->type = htons(pair->type);
184 nve->code = htons(pair->code);
187 ofpbuf_put(buf, data, data_len);
188 ofpmsg_update_length(buf);
193 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
196 * 'oh->version' determines the OpenFlow version of the error reply.
197 * 'oh->xid' determines the xid of the error reply.
198 * The error reply will contain an initial subsequence of 'oh', up to
199 * 'oh->length' or 64 bytes, whichever is shorter.
201 * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
202 * messages. Use ofperr_encode_hello() instead. */
204 ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh)
206 uint16_t len = ntohs(oh->length);
208 return ofperr_encode_msg__(error, oh->version, oh->xid, oh, MIN(len, 64));
211 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
212 * given 'error', in the error domain 'domain'. The error message will include
213 * the additional null-terminated text string 's'.
215 * If 'version' is an unknown version then OFP10_VERSION is used.
216 * OFPET_HELLO_FAILED error messages are supposed to be backward-compatible,
217 * so in theory this should work. */
219 ofperr_encode_hello(enum ofperr error, enum ofp_version ofp_version,
222 return ofperr_encode_msg__(error, ofp_version, htonl(0), s, strlen(s));
225 /* Returns the value that would go into an OFPT_ERROR message's 'type' for
226 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
227 * 'version' or 'version' is unknown.
229 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
231 ofperr_get_type(enum ofperr error, enum ofp_version version)
233 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
234 return domain ? ofperr_get_pair__(error, domain)->type : -1;
237 /* Returns the value that would go into an OFPT_ERROR message's 'code' for
238 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
239 * 'version', 'version' is unknown or if 'error' represents a category
240 * rather than a specific error.
243 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
245 ofperr_get_code(enum ofperr error, enum ofp_version version)
247 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
248 return domain ? ofperr_get_pair__(error, domain)->code : -1;
251 /* Tries to decode 'oh', which should be an OpenFlow OFPT_ERROR message.
252 * Returns an OFPERR_* constant on success, 0 on failure.
254 * If 'payload' is nonnull, on success '*payload' is initialized to the
255 * error's payload, and on failure it is cleared. */
257 ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
259 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
261 const struct ofp_error_msg *oem;
268 memset(payload, 0, sizeof *payload);
271 /* Pull off the error message. */
272 ofpbuf_use_const(&b, oh, ntohs(oh->length));
273 error = ofpraw_pull(&raw, &b);
277 oem = ofpbuf_pull(&b, sizeof *oem);
279 /* Get the error type and code. */
280 type = ntohs(oem->type);
281 code = ntohs(oem->code);
282 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
283 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
288 if (nve->vendor != htonl(NX_VENDOR_ID)) {
289 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
293 type = ntohs(nve->type);
294 code = ntohs(nve->code);
297 /* Translate the error type and code into an ofperr. */
298 error = ofperr_decode(oh->version, type, code);
299 if (error && payload) {
300 ofpbuf_use_const(payload, b.data, b.size);
305 /* If 'error' is a valid OFPERR_* value, returns its name
306 * (e.g. "OFPBRC_BAD_TYPE" for OFPBRC_BAD_TYPE). Otherwise, assumes that
307 * 'error' is a positive errno value and returns what strerror() produces for
310 ofperr_to_string(enum ofperr error)
312 return ofperr_is_valid(error) ? ofperr_get_name(error) : strerror(error);