]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-errors.c
ovsdb-idl: Remove prototype for function that is not defined or used.
[mirror_ovs.git] / lib / ofp-errors.c
1 /*
2 * Copyright (c) 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
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
17 #include <config.h>
18 #include <errno.h>
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"
28 #include "util.h"
29
30 VLOG_DEFINE_THIS_MODULE(ofp_errors);
31
32 struct triplet {
33 uint32_t vendor;
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. */
43 static const struct ofperr_domain *
44 ofperr_domain_from_version(enum ofp_version version)
45 {
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;
53 case OFP13_VERSION:
54 return &ofperr_of13;
55 case OFP14_VERSION:
56 return &ofperr_of14;
57 case OFP15_VERSION:
58 return &ofperr_of15;
59 default:
60 return NULL;
61 }
62 }
63
64 /* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow version 'version'. */
65 const char *
66 ofperr_domain_get_name(enum ofp_version version)
67 {
68 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
69 return domain ? domain->name : NULL;
70 }
71
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
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
81 * unknown. */
82 static enum ofperr
83 ofperr_decode(enum ofp_version version,
84 uint32_t vendor, uint16_t type, uint16_t code)
85 {
86 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
87 return domain ? domain->decode(vendor, type, code) : 0;
88 }
89
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
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
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
132 static const struct triplet *
133 ofperr_get_triplet__(enum ofperr error, const struct ofperr_domain *domain)
134 {
135 size_t ofs = error - OFPERR_OFS;
136
137 ovs_assert(ofperr_is_valid(error));
138 return &domain->errors[ofs];
139 }
140
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)
144 {
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;
149 struct ofpbuf *buf;
150
151 /* Get the error domain for 'ofp_version', or fall back to OF1.0. */
152 domain = ofperr_domain_from_version(ofp_version);
153 if (!domain) {
154 VLOG_ERR_RL(&rl, "cannot encode error for unknown OpenFlow "
155 "version 0x%02x", ofp_version);
156 domain = &ofperr_of10;
157 }
158
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;
169 }
170
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);
175
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;
181
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);
186 oem->type = htons(NXET_VENDOR);
187 oem->code = htons(NXVC_VENDOR_ERROR);
188
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);
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);
203 }
204
205 ofpbuf_put(buf, data, MIN(data_len, UINT16_MAX - buf->size));
206 ofpmsg_update_length(buf);
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
217 * 'oh->length' (or however much fits).
218 *
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 {
224 return ofperr_encode_msg__(error, oh->version, oh->xid,
225 oh, ntohs(oh->length));
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 *
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. */
235 struct ofpbuf *
236 ofperr_encode_hello(enum ofperr error, enum ofp_version ofp_version,
237 const char *s)
238 {
239 return ofperr_encode_msg__(error, ofp_version, htonl(0), s, strlen(s));
240 }
241
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
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
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.
270 *
271 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
272 int
273 ofperr_get_type(enum ofperr error, enum ofp_version version)
274 {
275 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
276 return domain ? ofperr_get_triplet__(error, domain)->type : -1;
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
281 * 'version', 'version' is unknown or if 'error' represents a category
282 * rather than a specific error.
283 *
284 *
285 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
286 int
287 ofperr_get_code(enum ofperr error, enum ofp_version version)
288 {
289 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
290 return domain ? ofperr_get_triplet__(error, domain)->code : -1;
291 }
292
293 /* Tries to decode 'oh', which should be an OpenFlow OFPT_ERROR message.
294 * Returns an OFPERR_* constant on success, 0 on failure.
295 *
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. */
300 enum ofperr
301 ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
302 {
303 const struct ofp_error_msg *oem;
304 enum ofpraw raw;
305 uint16_t type, code;
306 uint32_t vendor;
307
308 if (payload) {
309 memset(payload, 0, sizeof *payload);
310 }
311
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);
315 if (error) {
316 return 0;
317 }
318 oem = ofpbuf_pull(&b, sizeof *oem);
319
320 /* Get the error type and code. */
321 vendor = 0;
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
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);
335 if (!vendorp) {
336 return 0;
337 }
338
339 vendor = ntohl(*vendorp);
340 type = code;
341 code = 0;
342 }
343
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);
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
355 * 'error' is a positive errno value and returns what ovs_strerror() produces
356 * for 'error'. */
357 const char *
358 ofperr_to_string(enum ofperr error)
359 {
360 return (ofperr_is_valid(error)
361 ? ofperr_get_name(error)
362 : ovs_strerror(error));
363 }