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);
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)
60 /* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow version 'version'. */
62 ofperr_domain_get_name(enum ofp_version version)
64 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
65 return domain ? domain->name : NULL;
68 /* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
70 ofperr_is_valid(enum ofperr error)
72 return error >= OFPERR_OFS && error < OFPERR_OFS + OFPERR_N_ERRORS;
75 /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
76 * 'version', or 0 if either no such OFPERR_* value exists or 'version' is
79 ofperr_decode(enum ofp_version version,
80 uint32_t vendor, uint16_t type, uint16_t code)
82 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
83 return domain ? domain->decode(vendor, type, code) : 0;
86 /* Returns the name of 'error', e.g. "OFPBRC_BAD_TYPE" if 'error' is
87 * OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not a valid OFPERR_* value.
89 * Consider ofperr_to_string() instead, if the error code might be an errno
92 ofperr_get_name(enum ofperr error)
94 return (ofperr_is_valid(error)
95 ? error_names[error - OFPERR_OFS]
99 /* Returns the OFPERR_* value that corresponds for 'name', 0 if none exists.
100 * For example, returns OFPERR_OFPHFC_INCOMPATIBLE if 'name' is
101 * "OFPHFC_INCOMPATIBLE".
103 * This is probably useful only for debugging and testing. */
105 ofperr_from_name(const char *name)
109 for (i = 0; i < OFPERR_N_ERRORS; i++) {
110 if (!strcmp(name, error_names[i])) {
111 return i + OFPERR_OFS;
117 /* Returns an extended description name of 'error', e.g. "ofp_header.type not
118 * supported." if 'error' is OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not
119 * a valid OFPERR_* value. */
121 ofperr_get_description(enum ofperr error)
123 return (ofperr_is_valid(error)
124 ? error_comments[error - OFPERR_OFS]
128 static const struct triplet *
129 ofperr_get_triplet__(enum ofperr error, const struct ofperr_domain *domain)
131 size_t ofs = error - OFPERR_OFS;
133 ovs_assert(ofperr_is_valid(error));
134 return &domain->errors[ofs];
137 static struct ofpbuf *
138 ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version,
139 ovs_be32 xid, const void *data, size_t data_len)
141 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
142 const struct ofperr_domain *domain;
143 const struct triplet *triplet;
144 struct ofp_error_msg *oem;
147 /* Get the error domain for 'ofp_version', or fall back to OF1.0. */
148 domain = ofperr_domain_from_version(ofp_version);
150 VLOG_ERR_RL(&rl, "cannot encode error for unknown OpenFlow "
151 "version 0x%02x", ofp_version);
152 domain = &ofperr_of10;
155 /* Make sure 'error' is valid in 'domain', or use a fallback error. */
156 if (!ofperr_is_valid(error)) {
157 /* 'error' seems likely to be a system errno value. */
158 VLOG_ERR_RL(&rl, "invalid OpenFlow error code %d (%s)",
159 error, ovs_strerror(error));
160 error = OFPERR_NXBRC_UNENCODABLE_ERROR;
161 } else if (domain->errors[error - OFPERR_OFS].code < 0) {
162 VLOG_ERR_RL(&rl, "cannot encode %s for %s",
163 ofperr_get_name(error), domain->name);
164 error = OFPERR_NXBRC_UNENCODABLE_ERROR;
167 triplet = ofperr_get_triplet__(error, domain);
168 if (!triplet->vendor) {
169 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
170 sizeof *oem + data_len);
172 oem = ofpbuf_put_uninit(buf, sizeof *oem);
173 oem->type = htons(triplet->type);
174 oem->code = htons(triplet->code);
175 } else if (ofp_version <= OFP11_VERSION) {
176 struct nx_vendor_error *nve;
178 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
179 sizeof *oem + sizeof *nve + data_len);
181 oem = ofpbuf_put_uninit(buf, sizeof *oem);
182 oem->type = htons(NXET_VENDOR);
183 oem->code = htons(NXVC_VENDOR_ERROR);
185 nve = ofpbuf_put_uninit(buf, sizeof *nve);
186 nve->vendor = htonl(triplet->vendor);
187 nve->type = htons(triplet->type);
188 nve->code = htons(triplet->code);
190 ovs_be32 vendor = htonl(triplet->vendor);
192 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
193 sizeof *oem + sizeof(uint32_t) + data_len);
195 oem = ofpbuf_put_uninit(buf, sizeof *oem);
196 oem->type = htons(OFPET12_EXPERIMENTER);
197 oem->code = htons(triplet->type);
198 ofpbuf_put(buf, &vendor, sizeof vendor);
201 ofpbuf_put(buf, data, data_len);
202 ofpmsg_update_length(buf);
207 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
210 * 'oh->version' determines the OpenFlow version of the error reply.
211 * 'oh->xid' determines the xid of the error reply.
212 * The error reply will contain an initial subsequence of 'oh', up to
213 * 'oh->length' or 64 bytes, whichever is shorter.
215 * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
216 * messages. Use ofperr_encode_hello() instead. */
218 ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh)
220 uint16_t len = ntohs(oh->length);
222 return ofperr_encode_msg__(error, oh->version, oh->xid, oh, MIN(len, 64));
225 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
226 * given 'error', in the error domain 'domain'. The error message will include
227 * the additional null-terminated text string 's'.
229 * If 'version' is an unknown version then OFP10_VERSION is used.
230 * OFPET_HELLO_FAILED error messages are supposed to be backward-compatible,
231 * so in theory this should work. */
233 ofperr_encode_hello(enum ofperr error, enum ofp_version ofp_version,
236 return ofperr_encode_msg__(error, ofp_version, htonl(0), s, strlen(s));
240 ofperr_get_vendor(enum ofperr error, enum ofp_version version)
242 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
243 return domain ? ofperr_get_triplet__(error, domain)->vendor : -1;
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
248 * 'version' or 'version' is unknown.
250 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
252 ofperr_get_type(enum ofperr error, enum ofp_version version)
254 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
255 return domain ? ofperr_get_triplet__(error, domain)->type : -1;
258 /* Returns the value that would go into an OFPT_ERROR message's 'code' for
259 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
260 * 'version', 'version' is unknown or if 'error' represents a category
261 * rather than a specific error.
264 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
266 ofperr_get_code(enum ofperr error, enum ofp_version version)
268 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
269 return domain ? ofperr_get_triplet__(error, domain)->code : -1;
272 /* Tries to decode 'oh', which should be an OpenFlow OFPT_ERROR message.
273 * Returns an OFPERR_* constant on success, 0 on failure.
275 * If 'payload' is nonnull, on success '*payload' is initialized to the
276 * error's payload, and on failure it is cleared. */
278 ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
280 const struct ofp_error_msg *oem;
288 memset(payload, 0, sizeof *payload);
291 /* Pull off the error message. */
292 ofpbuf_use_const(&b, oh, ntohs(oh->length));
293 error = ofpraw_pull(&raw, &b);
297 oem = ofpbuf_pull(&b, sizeof *oem);
299 /* Get the error type and code. */
301 type = ntohs(oem->type);
302 code = ntohs(oem->code);
303 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
304 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
309 vendor = ntohl(nve->vendor);
310 type = ntohs(nve->type);
311 code = ntohs(nve->code);
312 } else if (type == OFPET12_EXPERIMENTER) {
313 const ovs_be32 *vendorp = ofpbuf_try_pull(&b, sizeof *vendorp);
318 vendor = ntohl(*vendorp);
323 /* Translate the error type and code into an ofperr. */
324 error = ofperr_decode(oh->version, vendor, type, code);
325 if (error && payload) {
326 ofpbuf_use_const(payload, b.data, b.size);
331 /* If 'error' is a valid OFPERR_* value, returns its name
332 * (e.g. "OFPBRC_BAD_TYPE" for OFPBRC_BAD_TYPE). Otherwise, assumes that
333 * 'error' is a positive errno value and returns what ovs_strerror() produces
336 ofperr_to_string(enum ofperr error)
338 return (ofperr_is_valid(error)
339 ? ofperr_get_name(error)
340 : ovs_strerror(error));