2 * Copyright (c) 2012, 2013, 2014 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"
26 #include "openvswitch/vlog.h"
28 VLOG_DEFINE_THIS_MODULE(ofp_errors);
35 #include "ofp-errors.inc"
37 /* Returns an ofperr_domain that corresponds to the OpenFlow version number
38 * 'version' (one of the possible values of struct ofp_header's 'version'
39 * member). Returns NULL if the version isn't defined or isn't understood by
41 static const struct ofperr_domain *
42 ofperr_domain_from_version(enum ofp_version version)
62 /* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow version 'version'. */
64 ofperr_domain_get_name(enum ofp_version version)
66 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
67 return domain ? domain->name : NULL;
70 /* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
72 ofperr_is_valid(enum ofperr error)
74 return error >= OFPERR_OFS && error < OFPERR_OFS + OFPERR_N_ERRORS;
77 /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
78 * 'version', or 0 if either no such OFPERR_* value exists or 'version' is
81 ofperr_decode(enum ofp_version version,
82 uint32_t vendor, uint16_t type, uint16_t code)
84 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
85 return domain ? domain->decode(vendor, type, code) : 0;
88 /* Returns the name of 'error', e.g. "OFPBRC_BAD_TYPE" if 'error' is
89 * OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not a valid OFPERR_* value.
91 * Consider ofperr_to_string() instead, if the error code might be an errno
94 ofperr_get_name(enum ofperr error)
96 return (ofperr_is_valid(error)
97 ? error_names[error - OFPERR_OFS]
101 /* Returns the OFPERR_* value that corresponds for 'name', 0 if none exists.
102 * For example, returns OFPERR_OFPHFC_INCOMPATIBLE if 'name' is
103 * "OFPHFC_INCOMPATIBLE".
105 * This is probably useful only for debugging and testing. */
107 ofperr_from_name(const char *name)
111 for (i = 0; i < OFPERR_N_ERRORS; i++) {
112 if (!strcmp(name, error_names[i])) {
113 return i + OFPERR_OFS;
119 /* Returns an extended description name of 'error', e.g. "ofp_header.type not
120 * supported." if 'error' is OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not
121 * a valid OFPERR_* value. */
123 ofperr_get_description(enum ofperr error)
125 return (ofperr_is_valid(error)
126 ? error_comments[error - OFPERR_OFS]
130 static const struct triplet *
131 ofperr_get_triplet__(enum ofperr error, const struct ofperr_domain *domain)
133 size_t ofs = error - OFPERR_OFS;
135 ovs_assert(ofperr_is_valid(error));
136 return &domain->errors[ofs];
139 static struct ofpbuf *
140 ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version,
141 ovs_be32 xid, const void *data, size_t data_len)
143 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
144 const struct ofperr_domain *domain;
145 const struct triplet *triplet;
146 struct ofp_error_msg *oem;
149 /* Get the error domain for 'ofp_version', or fall back to OF1.0. */
150 domain = ofperr_domain_from_version(ofp_version);
152 VLOG_ERR_RL(&rl, "cannot encode error for unknown OpenFlow "
153 "version 0x%02x", ofp_version);
154 domain = &ofperr_of10;
157 /* Make sure 'error' is valid in 'domain', or use a fallback error. */
158 if (!ofperr_is_valid(error)) {
159 /* 'error' seems likely to be a system errno value. */
160 VLOG_ERR_RL(&rl, "invalid OpenFlow error code %d (%s)",
161 error, ovs_strerror(error));
162 error = OFPERR_NXBRC_UNENCODABLE_ERROR;
163 } else if (domain->errors[error - OFPERR_OFS].code < 0) {
164 VLOG_ERR_RL(&rl, "cannot encode %s for %s",
165 ofperr_get_name(error), domain->name);
166 error = OFPERR_NXBRC_UNENCODABLE_ERROR;
169 triplet = ofperr_get_triplet__(error, domain);
170 if (!triplet->vendor) {
171 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
172 sizeof *oem + data_len);
174 oem = ofpbuf_put_uninit(buf, sizeof *oem);
175 oem->type = htons(triplet->type);
176 oem->code = htons(triplet->code);
177 } else if (ofp_version <= OFP11_VERSION) {
178 struct nx_vendor_error *nve;
180 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
181 sizeof *oem + sizeof *nve + data_len);
183 oem = ofpbuf_put_uninit(buf, sizeof *oem);
184 oem->type = htons(NXET_VENDOR);
185 oem->code = htons(NXVC_VENDOR_ERROR);
187 nve = ofpbuf_put_uninit(buf, sizeof *nve);
188 nve->vendor = htonl(triplet->vendor);
189 nve->type = htons(triplet->type);
190 nve->code = htons(triplet->code);
192 ovs_be32 vendor = htonl(triplet->vendor);
194 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
195 sizeof *oem + sizeof(uint32_t) + data_len);
197 oem = ofpbuf_put_uninit(buf, sizeof *oem);
198 oem->type = htons(OFPET12_EXPERIMENTER);
199 oem->code = htons(triplet->type);
200 ofpbuf_put(buf, &vendor, sizeof vendor);
203 ofpbuf_put(buf, data, data_len);
204 ofpmsg_update_length(buf);
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 * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
218 * messages. Use ofperr_encode_hello() instead. */
220 ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh)
222 uint16_t len = ntohs(oh->length);
224 return ofperr_encode_msg__(error, oh->version, oh->xid, oh, MIN(len, 64));
227 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
228 * given 'error', in the error domain 'domain'. The error message will include
229 * the additional null-terminated text string 's'.
231 * If 'version' is an unknown version then OFP10_VERSION is used.
232 * OFPET_HELLO_FAILED error messages are supposed to be backward-compatible,
233 * so in theory this should work. */
235 ofperr_encode_hello(enum ofperr error, enum ofp_version ofp_version,
238 return ofperr_encode_msg__(error, ofp_version, htonl(0), s, strlen(s));
242 ofperr_get_vendor(enum ofperr error, enum ofp_version version)
244 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
245 return domain ? ofperr_get_triplet__(error, domain)->vendor : -1;
248 /* Returns the value that would go into an OFPT_ERROR message's 'type' for
249 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
250 * 'version' or 'version' is unknown.
252 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
254 ofperr_get_type(enum ofperr error, enum ofp_version version)
256 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
257 return domain ? ofperr_get_triplet__(error, domain)->type : -1;
260 /* Returns the value that would go into an OFPT_ERROR message's 'code' for
261 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
262 * 'version', 'version' is unknown or if 'error' represents a category
263 * rather than a specific error.
266 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
268 ofperr_get_code(enum ofperr error, enum ofp_version version)
270 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
271 return domain ? ofperr_get_triplet__(error, domain)->code : -1;
274 /* Tries to decode 'oh', which should be an OpenFlow OFPT_ERROR message.
275 * Returns an OFPERR_* constant on success, 0 on failure.
277 * If 'payload' is nonnull, on success '*payload' is initialized with a copy of
278 * the error's payload (copying is required because the payload is not properly
279 * aligned). The caller must free the payload (with ofpbuf_uninit()) when it
280 * is no longer needed. On failure, '*payload' is cleared. */
282 ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
284 const struct ofp_error_msg *oem;
292 memset(payload, 0, sizeof *payload);
295 /* Pull off the error message. */
296 ofpbuf_use_const(&b, oh, ntohs(oh->length));
297 error = ofpraw_pull(&raw, &b);
301 oem = ofpbuf_pull(&b, sizeof *oem);
303 /* 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 vendor = ntohl(nve->vendor);
314 type = ntohs(nve->type);
315 code = ntohs(nve->code);
316 } else if (type == OFPET12_EXPERIMENTER) {
317 const ovs_be32 *vendorp = ofpbuf_try_pull(&b, sizeof *vendorp);
322 vendor = ntohl(*vendorp);
327 /* Translate the error type and code into an ofperr. */
328 error = ofperr_decode(oh->version, vendor, type, code);
329 if (error && payload) {
330 ofpbuf_init(payload, b.size);
331 ofpbuf_push(payload, b.data, b.size);
336 /* If 'error' is a valid OFPERR_* value, returns its name
337 * (e.g. "OFPBRC_BAD_TYPE" for OFPBRC_BAD_TYPE). Otherwise, assumes that
338 * 'error' is a positive errno value and returns what ovs_strerror() produces
341 ofperr_to_string(enum ofperr error)
343 return (ofperr_is_valid(error)
344 ? ofperr_get_name(error)
345 : ovs_strerror(error));