]>
Commit | Line | Data |
---|---|---|
dcabe857 | 1 | /* |
b79d45a1 | 2 | * Copyright (c) 2012, 2013, 2014, 2015, 2016 Nicira, Inc. |
dcabe857 BP |
3 | * |
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: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
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. | |
15 | */ | |
16 | ||
90bf1e07 | 17 | #include <config.h> |
90bf1e07 BP |
18 | #include <errno.h> |
19 | #include "byte-order.h" | |
d271907f | 20 | #include "openflow/openflow.h" |
3e8a2ad1 | 21 | #include "openvswitch/dynamic-string.h" |
d271907f BW |
22 | #include "openvswitch/ofp-errors.h" |
23 | #include "openvswitch/ofp-msgs.h" | |
f4248336 | 24 | #include "openvswitch/ofp-util.h" |
64c96779 | 25 | #include "openvswitch/ofpbuf.h" |
e6211adc | 26 | #include "openvswitch/vlog.h" |
d271907f | 27 | #include "util.h" |
90bf1e07 BP |
28 | |
29 | VLOG_DEFINE_THIS_MODULE(ofp_errors); | |
30 | ||
514887ee BP |
31 | struct triplet { |
32 | uint32_t vendor; | |
90bf1e07 BP |
33 | int type, code; |
34 | }; | |
35 | ||
36 | #include "ofp-errors.inc" | |
37 | ||
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 | |
41 | * OVS. */ | |
688e86e1 SH |
42 | static const struct ofperr_domain * |
43 | ofperr_domain_from_version(enum ofp_version version) | |
90bf1e07 | 44 | { |
688e86e1 SH |
45 | switch (version) { |
46 | case OFP10_VERSION: | |
47 | return &ofperr_of10; | |
48 | case OFP11_VERSION: | |
49 | return &ofperr_of11; | |
50 | case OFP12_VERSION: | |
51 | return &ofperr_of12; | |
2e1ae200 JR |
52 | case OFP13_VERSION: |
53 | return &ofperr_of13; | |
c37c0382 AC |
54 | case OFP14_VERSION: |
55 | return &ofperr_of14; | |
42dccab5 BP |
56 | case OFP15_VERSION: |
57 | return &ofperr_of15; | |
b79d45a1 BP |
58 | case OFP16_VERSION: |
59 | return &ofperr_of16; | |
688e86e1 SH |
60 | default: |
61 | return NULL; | |
62 | } | |
90bf1e07 BP |
63 | } |
64 | ||
688e86e1 | 65 | /* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow version 'version'. */ |
2e0525bc | 66 | const char * |
688e86e1 | 67 | ofperr_domain_get_name(enum ofp_version version) |
2e0525bc | 68 | { |
688e86e1 SH |
69 | const struct ofperr_domain *domain = ofperr_domain_from_version(version); |
70 | return domain ? domain->name : NULL; | |
2e0525bc SH |
71 | } |
72 | ||
90bf1e07 BP |
73 | /* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */ |
74 | bool | |
75 | ofperr_is_valid(enum ofperr error) | |
76 | { | |
77 | return error >= OFPERR_OFS && error < OFPERR_OFS + OFPERR_N_ERRORS; | |
78 | } | |
79 | ||
90bf1e07 | 80 | /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within |
688e86e1 SH |
81 | * 'version', or 0 if either no such OFPERR_* value exists or 'version' is |
82 | * unknown. */ | |
dfbec087 | 83 | static enum ofperr |
514887ee BP |
84 | ofperr_decode(enum ofp_version version, |
85 | uint32_t vendor, uint16_t type, uint16_t code) | |
90bf1e07 | 86 | { |
688e86e1 | 87 | const struct ofperr_domain *domain = ofperr_domain_from_version(version); |
514887ee | 88 | return domain ? domain->decode(vendor, type, code) : 0; |
90bf1e07 BP |
89 | } |
90 | ||
90bf1e07 BP |
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. | |
93 | * | |
94 | * Consider ofperr_to_string() instead, if the error code might be an errno | |
95 | * value. */ | |
96 | const char * | |
97 | ofperr_get_name(enum ofperr error) | |
98 | { | |
99 | return (ofperr_is_valid(error) | |
100 | ? error_names[error - OFPERR_OFS] | |
101 | : "<invalid>"); | |
102 | } | |
103 | ||
2e0525bc SH |
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". | |
107 | * | |
108 | * This is probably useful only for debugging and testing. */ | |
109 | enum ofperr | |
110 | ofperr_from_name(const char *name) | |
111 | { | |
112 | int i; | |
113 | ||
114 | for (i = 0; i < OFPERR_N_ERRORS; i++) { | |
115 | if (!strcmp(name, error_names[i])) { | |
116 | return i + OFPERR_OFS; | |
117 | } | |
118 | } | |
119 | return 0; | |
120 | } | |
121 | ||
90bf1e07 BP |
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. */ | |
125 | const char * | |
126 | ofperr_get_description(enum ofperr error) | |
127 | { | |
128 | return (ofperr_is_valid(error) | |
129 | ? error_comments[error - OFPERR_OFS] | |
130 | : "<invalid>"); | |
131 | } | |
132 | ||
514887ee BP |
133 | static const struct triplet * |
134 | ofperr_get_triplet__(enum ofperr error, const struct ofperr_domain *domain) | |
2e0525bc SH |
135 | { |
136 | size_t ofs = error - OFPERR_OFS; | |
137 | ||
cb22974d | 138 | ovs_assert(ofperr_is_valid(error)); |
2e0525bc SH |
139 | return &domain->errors[ofs]; |
140 | } | |
141 | ||
90bf1e07 | 142 | static struct ofpbuf * |
9b7e2112 | 143 | ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version, |
90bf1e07 BP |
144 | ovs_be32 xid, const void *data, size_t data_len) |
145 | { | |
07c8ec21 BP |
146 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
147 | const struct ofperr_domain *domain; | |
514887ee | 148 | const struct triplet *triplet; |
90bf1e07 | 149 | struct ofp_error_msg *oem; |
90bf1e07 | 150 | struct ofpbuf *buf; |
90bf1e07 | 151 | |
07c8ec21 | 152 | /* Get the error domain for 'ofp_version', or fall back to OF1.0. */ |
9b7e2112 | 153 | domain = ofperr_domain_from_version(ofp_version); |
90bf1e07 | 154 | if (!domain) { |
07c8ec21 BP |
155 | VLOG_ERR_RL(&rl, "cannot encode error for unknown OpenFlow " |
156 | "version 0x%02x", ofp_version); | |
157 | domain = &ofperr_of10; | |
90bf1e07 BP |
158 | } |
159 | ||
07c8ec21 BP |
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)", | |
10a89ef0 | 164 | error, ovs_strerror(error)); |
07c8ec21 BP |
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; | |
90bf1e07 BP |
170 | } |
171 | ||
514887ee BP |
172 | triplet = ofperr_get_triplet__(error, domain); |
173 | if (!triplet->vendor) { | |
982697a4 BP |
174 | buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid, |
175 | sizeof *oem + data_len); | |
176 | ||
177 | oem = ofpbuf_put_uninit(buf, sizeof *oem); | |
514887ee BP |
178 | oem->type = htons(triplet->type); |
179 | oem->code = htons(triplet->code); | |
180 | } else if (ofp_version <= OFP11_VERSION) { | |
90bf1e07 BP |
181 | struct nx_vendor_error *nve; |
182 | ||
982697a4 BP |
183 | buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid, |
184 | sizeof *oem + sizeof *nve + data_len); | |
185 | ||
186 | oem = ofpbuf_put_uninit(buf, sizeof *oem); | |
90bf1e07 BP |
187 | oem->type = htons(NXET_VENDOR); |
188 | oem->code = htons(NXVC_VENDOR_ERROR); | |
189 | ||
982697a4 | 190 | nve = ofpbuf_put_uninit(buf, sizeof *nve); |
514887ee BP |
191 | nve->vendor = htonl(triplet->vendor); |
192 | nve->type = htons(triplet->type); | |
193 | nve->code = htons(triplet->code); | |
194 | } else { | |
195 | ovs_be32 vendor = htonl(triplet->vendor); | |
196 | ||
197 | buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid, | |
198 | sizeof *oem + sizeof(uint32_t) + data_len); | |
199 | ||
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); | |
90bf1e07 | 204 | } |
90bf1e07 | 205 | |
90bf1e07 | 206 | ofpbuf_put(buf, data, data_len); |
edd70aa7 | 207 | ofpmsg_update_length(buf); |
90bf1e07 BP |
208 | |
209 | return buf; | |
210 | } | |
211 | ||
212 | /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the | |
213 | * given 'error'. | |
214 | * | |
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. | |
219 | * | |
90bf1e07 BP |
220 | * This function isn't appropriate for encoding OFPET_HELLO_FAILED error |
221 | * messages. Use ofperr_encode_hello() instead. */ | |
222 | struct ofpbuf * | |
223 | ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh) | |
224 | { | |
90bf1e07 BP |
225 | uint16_t len = ntohs(oh->length); |
226 | ||
9b7e2112 | 227 | return ofperr_encode_msg__(error, oh->version, oh->xid, oh, MIN(len, 64)); |
90bf1e07 BP |
228 | } |
229 | ||
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'. | |
233 | * | |
9b7e2112 SH |
234 | * If 'version' is an unknown version then OFP10_VERSION is used. |
235 | * OFPET_HELLO_FAILED error messages are supposed to be backward-compatible, | |
07c8ec21 | 236 | * so in theory this should work. */ |
90bf1e07 | 237 | struct ofpbuf * |
9b7e2112 | 238 | ofperr_encode_hello(enum ofperr error, enum ofp_version ofp_version, |
90bf1e07 BP |
239 | const char *s) |
240 | { | |
9b7e2112 | 241 | return ofperr_encode_msg__(error, ofp_version, htonl(0), s, strlen(s)); |
90bf1e07 BP |
242 | } |
243 | ||
514887ee BP |
244 | int |
245 | ofperr_get_vendor(enum ofperr error, enum ofp_version version) | |
246 | { | |
247 | const struct ofperr_domain *domain = ofperr_domain_from_version(version); | |
248 | return domain ? ofperr_get_triplet__(error, domain)->vendor : -1; | |
249 | } | |
250 | ||
2e0525bc SH |
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 | |
688e86e1 | 253 | * 'version' or 'version' is unknown. |
2e0525bc SH |
254 | * |
255 | * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */ | |
256 | int | |
688e86e1 | 257 | ofperr_get_type(enum ofperr error, enum ofp_version version) |
2e0525bc | 258 | { |
688e86e1 | 259 | const struct ofperr_domain *domain = ofperr_domain_from_version(version); |
514887ee | 260 | return domain ? ofperr_get_triplet__(error, domain)->type : -1; |
2e0525bc SH |
261 | } |
262 | ||
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 | |
688e86e1 SH |
265 | * 'version', 'version' is unknown or if 'error' represents a category |
266 | * rather than a specific error. | |
267 | * | |
2e0525bc SH |
268 | * |
269 | * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */ | |
270 | int | |
688e86e1 | 271 | ofperr_get_code(enum ofperr error, enum ofp_version version) |
2e0525bc | 272 | { |
688e86e1 | 273 | const struct ofperr_domain *domain = ofperr_domain_from_version(version); |
514887ee | 274 | return domain ? ofperr_get_triplet__(error, domain)->code : -1; |
2e0525bc SH |
275 | } |
276 | ||
7a7b6428 | 277 | /* Tries to decode 'oh', which should be an OpenFlow OFPT_ERROR message. |
90bf1e07 BP |
278 | * Returns an OFPERR_* constant on success, 0 on failure. |
279 | * | |
dea241f1 BP |
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. */ | |
90bf1e07 | 284 | enum ofperr |
982697a4 | 285 | ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload) |
90bf1e07 | 286 | { |
90bf1e07 | 287 | const struct ofp_error_msg *oem; |
982697a4 | 288 | enum ofpraw raw; |
90bf1e07 | 289 | uint16_t type, code; |
514887ee | 290 | uint32_t vendor; |
90bf1e07 | 291 | |
982697a4 BP |
292 | if (payload) { |
293 | memset(payload, 0, sizeof *payload); | |
90bf1e07 BP |
294 | } |
295 | ||
296 | /* Pull off the error message. */ | |
0a2869d5 BP |
297 | struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length)); |
298 | enum ofperr error = ofpraw_pull(&raw, &b); | |
982697a4 | 299 | if (error) { |
90bf1e07 BP |
300 | return 0; |
301 | } | |
982697a4 | 302 | oem = ofpbuf_pull(&b, sizeof *oem); |
90bf1e07 | 303 | |
90bf1e07 | 304 | /* Get the error type and code. */ |
514887ee | 305 | vendor = 0; |
90bf1e07 BP |
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); | |
310 | if (!nve) { | |
311 | return 0; | |
312 | } | |
313 | ||
514887ee | 314 | vendor = ntohl(nve->vendor); |
90bf1e07 BP |
315 | type = ntohs(nve->type); |
316 | code = ntohs(nve->code); | |
514887ee BP |
317 | } else if (type == OFPET12_EXPERIMENTER) { |
318 | const ovs_be32 *vendorp = ofpbuf_try_pull(&b, sizeof *vendorp); | |
319 | if (!vendorp) { | |
320 | return 0; | |
321 | } | |
322 | ||
323 | vendor = ntohl(*vendorp); | |
324 | type = code; | |
325 | code = 0; | |
90bf1e07 BP |
326 | } |
327 | ||
df30f9b1 | 328 | /* Translate the error type and code into an ofperr. */ |
514887ee | 329 | error = ofperr_decode(oh->version, vendor, type, code); |
982697a4 | 330 | if (error && payload) { |
6fd6ed71 PS |
331 | ofpbuf_init(payload, b.size); |
332 | ofpbuf_push(payload, b.data, b.size); | |
90bf1e07 BP |
333 | } |
334 | return error; | |
335 | } | |
336 | ||
337 | /* If 'error' is a valid OFPERR_* value, returns its name | |
338 | * (e.g. "OFPBRC_BAD_TYPE" for OFPBRC_BAD_TYPE). Otherwise, assumes that | |
10a89ef0 BP |
339 | * 'error' is a positive errno value and returns what ovs_strerror() produces |
340 | * for 'error'. */ | |
90bf1e07 BP |
341 | const char * |
342 | ofperr_to_string(enum ofperr error) | |
343 | { | |
10a89ef0 BP |
344 | return (ofperr_is_valid(error) |
345 | ? ofperr_get_name(error) | |
346 | : ovs_strerror(error)); | |
90bf1e07 | 347 | } |