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 "openflow/nicira-ext.h"
22 #include "openvswitch/dynamic-string.h"
23 #include "openvswitch/ofp-errors.h"
24 #include "openvswitch/ofp-msgs.h"
25 #include "openvswitch/ofp-print.h"
26 #include "openvswitch/ofpbuf.h"
27 #include "openvswitch/vlog.h"
30 VLOG_DEFINE_THIS_MODULE(ofp_errors
);
37 #include "ofp-errors.inc"
39 /* Returns an ofperr_domain that corresponds to the OpenFlow version number
40 * 'version' (one of the possible values of struct ofp_header's 'version'
41 * member). Returns NULL if the version isn't defined or isn't understood by
43 static const struct ofperr_domain
*
44 ofperr_domain_from_version(enum ofp_version version
)
64 /* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow version 'version'. */
66 ofperr_domain_get_name(enum ofp_version version
)
68 const struct ofperr_domain
*domain
= ofperr_domain_from_version(version
);
69 return domain
? domain
->name
: NULL
;
72 /* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
74 ofperr_is_valid(enum ofperr error
)
76 return error
>= OFPERR_OFS
&& error
< OFPERR_OFS
+ OFPERR_N_ERRORS
;
79 /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
80 * 'version', or 0 if either no such OFPERR_* value exists or 'version' is
83 ofperr_decode(enum ofp_version version
,
84 uint32_t vendor
, uint16_t type
, uint16_t code
)
86 const struct ofperr_domain
*domain
= ofperr_domain_from_version(version
);
87 return domain
? domain
->decode(vendor
, type
, code
) : 0;
90 /* Returns the name of 'error', e.g. "OFPBRC_BAD_TYPE" if 'error' is
91 * OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not a valid OFPERR_* value.
93 * Consider ofperr_to_string() instead, if the error code might be an errno
96 ofperr_get_name(enum ofperr error
)
98 return (ofperr_is_valid(error
)
99 ? error_names
[error
- OFPERR_OFS
]
103 /* Returns the OFPERR_* value that corresponds for 'name', 0 if none exists.
104 * For example, returns OFPERR_OFPHFC_INCOMPATIBLE if 'name' is
105 * "OFPHFC_INCOMPATIBLE".
107 * This is probably useful only for debugging and testing. */
109 ofperr_from_name(const char *name
)
113 for (i
= 0; i
< OFPERR_N_ERRORS
; i
++) {
114 if (!strcmp(name
, error_names
[i
])) {
115 return i
+ OFPERR_OFS
;
121 /* Returns an extended description name of 'error', e.g. "ofp_header.type not
122 * supported." if 'error' is OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not
123 * a valid OFPERR_* value. */
125 ofperr_get_description(enum ofperr error
)
127 return (ofperr_is_valid(error
)
128 ? error_comments
[error
- OFPERR_OFS
]
132 static const struct triplet
*
133 ofperr_get_triplet__(enum ofperr error
, const struct ofperr_domain
*domain
)
135 size_t ofs
= error
- OFPERR_OFS
;
137 ovs_assert(ofperr_is_valid(error
));
138 return &domain
->errors
[ofs
];
141 static struct ofpbuf
*
142 ofperr_encode_msg__(enum ofperr error
, enum ofp_version ofp_version
,
143 ovs_be32 xid
, const void *data
, size_t data_len
)
145 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
146 const struct ofperr_domain
*domain
;
147 const struct triplet
*triplet
;
148 struct ofp_error_msg
*oem
;
151 /* Get the error domain for 'ofp_version', or fall back to OF1.0. */
152 domain
= ofperr_domain_from_version(ofp_version
);
154 VLOG_ERR_RL(&rl
, "cannot encode error for unknown OpenFlow "
155 "version 0x%02x", ofp_version
);
156 domain
= &ofperr_of10
;
159 /* Make sure 'error' is valid in 'domain', or use a fallback error. */
160 if (!ofperr_is_valid(error
)) {
161 /* 'error' seems likely to be a system errno value. */
162 VLOG_ERR_RL(&rl
, "invalid OpenFlow error code %d (%s)",
163 error
, ovs_strerror(error
));
164 error
= OFPERR_NXBRC_UNENCODABLE_ERROR
;
165 } else if (domain
->errors
[error
- OFPERR_OFS
].code
< 0) {
166 VLOG_ERR_RL(&rl
, "cannot encode %s for %s",
167 ofperr_get_name(error
), domain
->name
);
168 error
= OFPERR_NXBRC_UNENCODABLE_ERROR
;
171 triplet
= ofperr_get_triplet__(error
, domain
);
172 if (!triplet
->vendor
) {
173 buf
= ofpraw_alloc_xid(OFPRAW_OFPT_ERROR
, domain
->version
, xid
,
174 sizeof *oem
+ data_len
);
176 oem
= ofpbuf_put_uninit(buf
, sizeof *oem
);
177 oem
->type
= htons(triplet
->type
);
178 oem
->code
= htons(triplet
->code
);
179 } else if (ofp_version
<= OFP11_VERSION
) {
180 struct nx_vendor_error
*nve
;
182 buf
= ofpraw_alloc_xid(OFPRAW_OFPT_ERROR
, domain
->version
, xid
,
183 sizeof *oem
+ sizeof *nve
+ data_len
);
185 oem
= ofpbuf_put_uninit(buf
, sizeof *oem
);
186 oem
->type
= htons(NXET_VENDOR
);
187 oem
->code
= htons(NXVC_VENDOR_ERROR
);
189 nve
= ofpbuf_put_uninit(buf
, sizeof *nve
);
190 nve
->vendor
= htonl(triplet
->vendor
);
191 nve
->type
= htons(triplet
->type
);
192 nve
->code
= htons(triplet
->code
);
194 ovs_be32 vendor
= htonl(triplet
->vendor
);
196 buf
= ofpraw_alloc_xid(OFPRAW_OFPT_ERROR
, domain
->version
, xid
,
197 sizeof *oem
+ sizeof(uint32_t) + data_len
);
199 oem
= ofpbuf_put_uninit(buf
, sizeof *oem
);
200 oem
->type
= htons(OFPET12_EXPERIMENTER
);
201 oem
->code
= htons(triplet
->type
);
202 ofpbuf_put(buf
, &vendor
, sizeof vendor
);
205 ofpbuf_put(buf
, data
, MIN(data_len
, UINT16_MAX
- buf
->size
));
206 ofpmsg_update_length(buf
);
211 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
214 * 'oh->version' determines the OpenFlow version of the error reply.
215 * 'oh->xid' determines the xid of the error reply.
216 * The error reply will contain an initial subsequence of 'oh', up to
217 * 'oh->length' (or however much fits).
219 * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
220 * messages. Use ofperr_encode_hello() instead. */
222 ofperr_encode_reply(enum ofperr error
, const struct ofp_header
*oh
)
224 return ofperr_encode_msg__(error
, oh
->version
, oh
->xid
,
225 oh
, ntohs(oh
->length
));
228 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
229 * given 'error', in the error domain 'domain'. The error message will include
230 * the additional null-terminated text string 's'.
232 * If 'version' is an unknown version then OFP10_VERSION is used.
233 * OFPET_HELLO_FAILED error messages are supposed to be backward-compatible,
234 * so in theory this should work. */
236 ofperr_encode_hello(enum ofperr error
, enum ofp_version ofp_version
,
239 return ofperr_encode_msg__(error
, ofp_version
, htonl(0), s
, strlen(s
));
243 ofperr_msg_format(struct ds
*string
, enum ofperr error
,
244 const struct ofpbuf
*payload
,
245 const struct ofputil_port_map
*port_map
,
246 const struct ofputil_table_map
*table_map
)
248 ds_put_format(string
, " %s\n", ofperr_get_name(error
));
250 if (error
== OFPERR_OFPHFC_INCOMPATIBLE
|| error
== OFPERR_OFPHFC_EPERM
) {
251 ds_put_printable(string
, payload
->data
, payload
->size
);
253 char *s
= ofp_to_string(payload
->data
, payload
->size
,
254 port_map
, table_map
, 1);
255 ds_put_cstr(string
, s
);
261 ofperr_get_vendor(enum ofperr error
, enum ofp_version version
)
263 const struct ofperr_domain
*domain
= ofperr_domain_from_version(version
);
264 return domain
? ofperr_get_triplet__(error
, domain
)->vendor
: -1;
267 /* Returns the value that would go into an OFPT_ERROR message's 'type' for
268 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
269 * 'version' or 'version' is unknown.
271 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
273 ofperr_get_type(enum ofperr error
, enum ofp_version version
)
275 const struct ofperr_domain
*domain
= ofperr_domain_from_version(version
);
276 return domain
? ofperr_get_triplet__(error
, domain
)->type
: -1;
279 /* Returns the value that would go into an OFPT_ERROR message's 'code' for
280 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
281 * 'version', 'version' is unknown or if 'error' represents a category
282 * rather than a specific error.
285 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
287 ofperr_get_code(enum ofperr error
, enum ofp_version version
)
289 const struct ofperr_domain
*domain
= ofperr_domain_from_version(version
);
290 return domain
? ofperr_get_triplet__(error
, domain
)->code
: -1;
293 /* Tries to decode 'oh', which should be an OpenFlow OFPT_ERROR message.
294 * Returns an OFPERR_* constant on success, 0 on failure.
296 * If 'payload' is nonnull, on success '*payload' is initialized with a copy of
297 * the error's payload (copying is required because the payload is not properly
298 * aligned). The caller must free the payload (with ofpbuf_uninit()) when it
299 * is no longer needed. On failure, '*payload' is cleared. */
301 ofperr_decode_msg(const struct ofp_header
*oh
, struct ofpbuf
*payload
)
303 const struct ofp_error_msg
*oem
;
309 memset(payload
, 0, sizeof *payload
);
312 /* Pull off the error message. */
313 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
314 enum ofperr error
= ofpraw_pull(&raw
, &b
);
318 oem
= ofpbuf_pull(&b
, sizeof *oem
);
320 /* Get the error type and code. */
322 type
= ntohs(oem
->type
);
323 code
= ntohs(oem
->code
);
324 if (type
== NXET_VENDOR
&& code
== NXVC_VENDOR_ERROR
) {
325 const struct nx_vendor_error
*nve
= ofpbuf_try_pull(&b
, sizeof *nve
);
330 vendor
= ntohl(nve
->vendor
);
331 type
= ntohs(nve
->type
);
332 code
= ntohs(nve
->code
);
333 } else if (type
== OFPET12_EXPERIMENTER
) {
334 const ovs_be32
*vendorp
= ofpbuf_try_pull(&b
, sizeof *vendorp
);
339 vendor
= ntohl(*vendorp
);
344 /* Translate the error type and code into an ofperr. */
345 error
= ofperr_decode(oh
->version
, vendor
, type
, code
);
346 if (error
&& payload
) {
347 ofpbuf_init(payload
, b
.size
);
348 ofpbuf_push(payload
, b
.data
, b
.size
);
353 /* If 'error' is a valid OFPERR_* value, returns its name
354 * (e.g. "OFPBRC_BAD_TYPE" for OFPBRC_BAD_TYPE). Otherwise, assumes that
355 * 'error' is a positive errno value and returns what ovs_strerror() produces
358 ofperr_to_string(enum ofperr error
)
360 return (ofperr_is_valid(error
)
361 ? ofperr_get_name(error
)
362 : ovs_strerror(error
));