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