2 * Copyright (c) 2012, 2013, 2014, 2015, 2016 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.
19 #include "byte-order.h"
20 #include "openflow/openflow.h"
21 #include "openvswitch/dynamic-string.h"
22 #include "openvswitch/ofp-errors.h"
23 #include "openvswitch/ofp-msgs.h"
24 #include "openvswitch/ofp-util.h"
25 #include "openvswitch/ofpbuf.h"
26 #include "openvswitch/vlog.h"
29 VLOG_DEFINE_THIS_MODULE(ofp_errors
);
36 #include "ofp-errors.inc"
38 /* Returns an ofperr_domain that corresponds to the OpenFlow version number
39 * 'version' (one of the possible values of struct ofp_header's 'version'
40 * member). Returns NULL if the version isn't defined or isn't understood by
42 static const struct ofperr_domain
*
43 ofperr_domain_from_version(enum ofp_version version
)
65 /* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow version 'version'. */
67 ofperr_domain_get_name(enum ofp_version version
)
69 const struct ofperr_domain
*domain
= ofperr_domain_from_version(version
);
70 return domain
? domain
->name
: NULL
;
73 /* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
75 ofperr_is_valid(enum ofperr error
)
77 return error
>= OFPERR_OFS
&& error
< OFPERR_OFS
+ OFPERR_N_ERRORS
;
80 /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
81 * 'version', or 0 if either no such OFPERR_* value exists or 'version' is
84 ofperr_decode(enum ofp_version version
,
85 uint32_t vendor
, uint16_t type
, uint16_t code
)
87 const struct ofperr_domain
*domain
= ofperr_domain_from_version(version
);
88 return domain
? domain
->decode(vendor
, type
, code
) : 0;
91 /* Returns the name of 'error', e.g. "OFPBRC_BAD_TYPE" if 'error' is
92 * OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not a valid OFPERR_* value.
94 * Consider ofperr_to_string() instead, if the error code might be an errno
97 ofperr_get_name(enum ofperr error
)
99 return (ofperr_is_valid(error
)
100 ? error_names
[error
- OFPERR_OFS
]
104 /* Returns the OFPERR_* value that corresponds for 'name', 0 if none exists.
105 * For example, returns OFPERR_OFPHFC_INCOMPATIBLE if 'name' is
106 * "OFPHFC_INCOMPATIBLE".
108 * This is probably useful only for debugging and testing. */
110 ofperr_from_name(const char *name
)
114 for (i
= 0; i
< OFPERR_N_ERRORS
; i
++) {
115 if (!strcmp(name
, error_names
[i
])) {
116 return i
+ OFPERR_OFS
;
122 /* Returns an extended description name of 'error', e.g. "ofp_header.type not
123 * supported." if 'error' is OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not
124 * a valid OFPERR_* value. */
126 ofperr_get_description(enum ofperr error
)
128 return (ofperr_is_valid(error
)
129 ? error_comments
[error
- OFPERR_OFS
]
133 static const struct triplet
*
134 ofperr_get_triplet__(enum ofperr error
, const struct ofperr_domain
*domain
)
136 size_t ofs
= error
- OFPERR_OFS
;
138 ovs_assert(ofperr_is_valid(error
));
139 return &domain
->errors
[ofs
];
142 static struct ofpbuf
*
143 ofperr_encode_msg__(enum ofperr error
, enum ofp_version ofp_version
,
144 ovs_be32 xid
, const void *data
, size_t data_len
)
146 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
147 const struct ofperr_domain
*domain
;
148 const struct triplet
*triplet
;
149 struct ofp_error_msg
*oem
;
152 /* Get the error domain for 'ofp_version', or fall back to OF1.0. */
153 domain
= ofperr_domain_from_version(ofp_version
);
155 VLOG_ERR_RL(&rl
, "cannot encode error for unknown OpenFlow "
156 "version 0x%02x", ofp_version
);
157 domain
= &ofperr_of10
;
160 /* Make sure 'error' is valid in 'domain', or use a fallback error. */
161 if (!ofperr_is_valid(error
)) {
162 /* 'error' seems likely to be a system errno value. */
163 VLOG_ERR_RL(&rl
, "invalid OpenFlow error code %d (%s)",
164 error
, ovs_strerror(error
));
165 error
= OFPERR_NXBRC_UNENCODABLE_ERROR
;
166 } else if (domain
->errors
[error
- OFPERR_OFS
].code
< 0) {
167 VLOG_ERR_RL(&rl
, "cannot encode %s for %s",
168 ofperr_get_name(error
), domain
->name
);
169 error
= OFPERR_NXBRC_UNENCODABLE_ERROR
;
172 triplet
= ofperr_get_triplet__(error
, domain
);
173 if (!triplet
->vendor
) {
174 buf
= ofpraw_alloc_xid(OFPRAW_OFPT_ERROR
, domain
->version
, xid
,
175 sizeof *oem
+ data_len
);
177 oem
= ofpbuf_put_uninit(buf
, sizeof *oem
);
178 oem
->type
= htons(triplet
->type
);
179 oem
->code
= htons(triplet
->code
);
180 } else if (ofp_version
<= OFP11_VERSION
) {
181 struct nx_vendor_error
*nve
;
183 buf
= ofpraw_alloc_xid(OFPRAW_OFPT_ERROR
, domain
->version
, xid
,
184 sizeof *oem
+ sizeof *nve
+ data_len
);
186 oem
= ofpbuf_put_uninit(buf
, sizeof *oem
);
187 oem
->type
= htons(NXET_VENDOR
);
188 oem
->code
= htons(NXVC_VENDOR_ERROR
);
190 nve
= ofpbuf_put_uninit(buf
, sizeof *nve
);
191 nve
->vendor
= htonl(triplet
->vendor
);
192 nve
->type
= htons(triplet
->type
);
193 nve
->code
= htons(triplet
->code
);
195 ovs_be32 vendor
= htonl(triplet
->vendor
);
197 buf
= ofpraw_alloc_xid(OFPRAW_OFPT_ERROR
, domain
->version
, xid
,
198 sizeof *oem
+ sizeof(uint32_t) + data_len
);
200 oem
= ofpbuf_put_uninit(buf
, sizeof *oem
);
201 oem
->type
= htons(OFPET12_EXPERIMENTER
);
202 oem
->code
= htons(triplet
->type
);
203 ofpbuf_put(buf
, &vendor
, sizeof vendor
);
206 ofpbuf_put(buf
, data
, data_len
);
207 ofpmsg_update_length(buf
);
212 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
215 * 'oh->version' determines the OpenFlow version of the error reply.
216 * 'oh->xid' determines the xid of the error reply.
217 * The error reply will contain an initial subsequence of 'oh', up to
218 * 'oh->length' or 64 bytes, whichever is shorter.
220 * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
221 * messages. Use ofperr_encode_hello() instead. */
223 ofperr_encode_reply(enum ofperr error
, const struct ofp_header
*oh
)
225 uint16_t len
= ntohs(oh
->length
);
227 return ofperr_encode_msg__(error
, oh
->version
, oh
->xid
, oh
, MIN(len
, 64));
230 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
231 * given 'error', in the error domain 'domain'. The error message will include
232 * the additional null-terminated text string 's'.
234 * If 'version' is an unknown version then OFP10_VERSION is used.
235 * OFPET_HELLO_FAILED error messages are supposed to be backward-compatible,
236 * so in theory this should work. */
238 ofperr_encode_hello(enum ofperr error
, enum ofp_version ofp_version
,
241 return ofperr_encode_msg__(error
, ofp_version
, htonl(0), s
, strlen(s
));
245 ofperr_get_vendor(enum ofperr error
, enum ofp_version version
)
247 const struct ofperr_domain
*domain
= ofperr_domain_from_version(version
);
248 return domain
? ofperr_get_triplet__(error
, domain
)->vendor
: -1;
251 /* Returns the value that would go into an OFPT_ERROR message's 'type' for
252 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
253 * 'version' or 'version' is unknown.
255 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
257 ofperr_get_type(enum ofperr error
, enum ofp_version version
)
259 const struct ofperr_domain
*domain
= ofperr_domain_from_version(version
);
260 return domain
? ofperr_get_triplet__(error
, domain
)->type
: -1;
263 /* Returns the value that would go into an OFPT_ERROR message's 'code' for
264 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
265 * 'version', 'version' is unknown or if 'error' represents a category
266 * rather than a specific error.
269 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
271 ofperr_get_code(enum ofperr error
, enum ofp_version version
)
273 const struct ofperr_domain
*domain
= ofperr_domain_from_version(version
);
274 return domain
? ofperr_get_triplet__(error
, domain
)->code
: -1;
277 /* Tries to decode 'oh', which should be an OpenFlow OFPT_ERROR message.
278 * Returns an OFPERR_* constant on success, 0 on failure.
280 * If 'payload' is nonnull, on success '*payload' is initialized with a copy of
281 * the error's payload (copying is required because the payload is not properly
282 * aligned). The caller must free the payload (with ofpbuf_uninit()) when it
283 * is no longer needed. On failure, '*payload' is cleared. */
285 ofperr_decode_msg(const struct ofp_header
*oh
, struct ofpbuf
*payload
)
287 const struct ofp_error_msg
*oem
;
293 memset(payload
, 0, sizeof *payload
);
296 /* Pull off the error message. */
297 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
298 enum ofperr error
= ofpraw_pull(&raw
, &b
);
302 oem
= ofpbuf_pull(&b
, sizeof *oem
);
304 /* Get the error type and code. */
306 type
= ntohs(oem
->type
);
307 code
= ntohs(oem
->code
);
308 if (type
== NXET_VENDOR
&& code
== NXVC_VENDOR_ERROR
) {
309 const struct nx_vendor_error
*nve
= ofpbuf_try_pull(&b
, sizeof *nve
);
314 vendor
= ntohl(nve
->vendor
);
315 type
= ntohs(nve
->type
);
316 code
= ntohs(nve
->code
);
317 } else if (type
== OFPET12_EXPERIMENTER
) {
318 const ovs_be32
*vendorp
= ofpbuf_try_pull(&b
, sizeof *vendorp
);
323 vendor
= ntohl(*vendorp
);
328 /* Translate the error type and code into an ofperr. */
329 error
= ofperr_decode(oh
->version
, vendor
, type
, code
);
330 if (error
&& payload
) {
331 ofpbuf_init(payload
, b
.size
);
332 ofpbuf_push(payload
, b
.data
, b
.size
);
337 /* If 'error' is a valid OFPERR_* value, returns its name
338 * (e.g. "OFPBRC_BAD_TYPE" for OFPBRC_BAD_TYPE). Otherwise, assumes that
339 * 'error' is a positive errno value and returns what ovs_strerror() produces
342 ofperr_to_string(enum ofperr error
)
344 return (ofperr_is_valid(error
)
345 ? ofperr_get_name(error
)
346 : ovs_strerror(error
));