]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ofp-errors.c
INSTALL.DPDK.md: Correct mergeable buffers parameter.
[mirror_ovs.git] / lib / ofp-errors.c
CommitLineData
dcabe857 1/*
dea241f1 2 * Copyright (c) 2012, 2013, 2014 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
BP
17#include <config.h>
18#include "ofp-errors.h"
19#include <errno.h>
20#include "byte-order.h"
21#include "dynamic-string.h"
982697a4 22#include "ofp-msgs.h"
90bf1e07
BP
23#include "ofp-util.h"
24#include "ofpbuf.h"
25#include "openflow/openflow.h"
e6211adc 26#include "openvswitch/vlog.h"
90bf1e07
BP
27
28VLOG_DEFINE_THIS_MODULE(ofp_errors);
29
514887ee
BP
30struct triplet {
31 uint32_t vendor;
90bf1e07
BP
32 int type, code;
33};
34
35#include "ofp-errors.inc"
36
37/* Returns an ofperr_domain that corresponds to the OpenFlow version number
38 * 'version' (one of the possible values of struct ofp_header's 'version'
39 * member). Returns NULL if the version isn't defined or isn't understood by
40 * OVS. */
688e86e1
SH
41static const struct ofperr_domain *
42ofperr_domain_from_version(enum ofp_version version)
90bf1e07 43{
688e86e1
SH
44 switch (version) {
45 case OFP10_VERSION:
46 return &ofperr_of10;
47 case OFP11_VERSION:
48 return &ofperr_of11;
49 case OFP12_VERSION:
50 return &ofperr_of12;
2e1ae200
JR
51 case OFP13_VERSION:
52 return &ofperr_of13;
c37c0382
AC
53 case OFP14_VERSION:
54 return &ofperr_of14;
42dccab5
BP
55 case OFP15_VERSION:
56 return &ofperr_of15;
688e86e1
SH
57 default:
58 return NULL;
59 }
90bf1e07
BP
60}
61
688e86e1 62/* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow version 'version'. */
2e0525bc 63const char *
688e86e1 64ofperr_domain_get_name(enum ofp_version version)
2e0525bc 65{
688e86e1
SH
66 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
67 return domain ? domain->name : NULL;
2e0525bc
SH
68}
69
90bf1e07
BP
70/* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
71bool
72ofperr_is_valid(enum ofperr error)
73{
74 return error >= OFPERR_OFS && error < OFPERR_OFS + OFPERR_N_ERRORS;
75}
76
90bf1e07 77/* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
688e86e1
SH
78 * 'version', or 0 if either no such OFPERR_* value exists or 'version' is
79 * unknown. */
dfbec087 80static enum ofperr
514887ee
BP
81ofperr_decode(enum ofp_version version,
82 uint32_t vendor, uint16_t type, uint16_t code)
90bf1e07 83{
688e86e1 84 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
514887ee 85 return domain ? domain->decode(vendor, type, code) : 0;
90bf1e07
BP
86}
87
90bf1e07
BP
88/* Returns the name of 'error', e.g. "OFPBRC_BAD_TYPE" if 'error' is
89 * OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not a valid OFPERR_* value.
90 *
91 * Consider ofperr_to_string() instead, if the error code might be an errno
92 * value. */
93const char *
94ofperr_get_name(enum ofperr error)
95{
96 return (ofperr_is_valid(error)
97 ? error_names[error - OFPERR_OFS]
98 : "<invalid>");
99}
100
2e0525bc
SH
101/* Returns the OFPERR_* value that corresponds for 'name', 0 if none exists.
102 * For example, returns OFPERR_OFPHFC_INCOMPATIBLE if 'name' is
103 * "OFPHFC_INCOMPATIBLE".
104 *
105 * This is probably useful only for debugging and testing. */
106enum ofperr
107ofperr_from_name(const char *name)
108{
109 int i;
110
111 for (i = 0; i < OFPERR_N_ERRORS; i++) {
112 if (!strcmp(name, error_names[i])) {
113 return i + OFPERR_OFS;
114 }
115 }
116 return 0;
117}
118
90bf1e07
BP
119/* Returns an extended description name of 'error', e.g. "ofp_header.type not
120 * supported." if 'error' is OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not
121 * a valid OFPERR_* value. */
122const char *
123ofperr_get_description(enum ofperr error)
124{
125 return (ofperr_is_valid(error)
126 ? error_comments[error - OFPERR_OFS]
127 : "<invalid>");
128}
129
514887ee
BP
130static const struct triplet *
131ofperr_get_triplet__(enum ofperr error, const struct ofperr_domain *domain)
2e0525bc
SH
132{
133 size_t ofs = error - OFPERR_OFS;
134
cb22974d 135 ovs_assert(ofperr_is_valid(error));
2e0525bc
SH
136 return &domain->errors[ofs];
137}
138
90bf1e07 139static struct ofpbuf *
9b7e2112 140ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version,
90bf1e07
BP
141 ovs_be32 xid, const void *data, size_t data_len)
142{
07c8ec21
BP
143 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
144 const struct ofperr_domain *domain;
514887ee 145 const struct triplet *triplet;
90bf1e07 146 struct ofp_error_msg *oem;
90bf1e07 147 struct ofpbuf *buf;
90bf1e07 148
07c8ec21 149 /* Get the error domain for 'ofp_version', or fall back to OF1.0. */
9b7e2112 150 domain = ofperr_domain_from_version(ofp_version);
90bf1e07 151 if (!domain) {
07c8ec21
BP
152 VLOG_ERR_RL(&rl, "cannot encode error for unknown OpenFlow "
153 "version 0x%02x", ofp_version);
154 domain = &ofperr_of10;
90bf1e07
BP
155 }
156
07c8ec21
BP
157 /* Make sure 'error' is valid in 'domain', or use a fallback error. */
158 if (!ofperr_is_valid(error)) {
159 /* 'error' seems likely to be a system errno value. */
160 VLOG_ERR_RL(&rl, "invalid OpenFlow error code %d (%s)",
10a89ef0 161 error, ovs_strerror(error));
07c8ec21
BP
162 error = OFPERR_NXBRC_UNENCODABLE_ERROR;
163 } else if (domain->errors[error - OFPERR_OFS].code < 0) {
164 VLOG_ERR_RL(&rl, "cannot encode %s for %s",
165 ofperr_get_name(error), domain->name);
166 error = OFPERR_NXBRC_UNENCODABLE_ERROR;
90bf1e07
BP
167 }
168
514887ee
BP
169 triplet = ofperr_get_triplet__(error, domain);
170 if (!triplet->vendor) {
982697a4
BP
171 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
172 sizeof *oem + data_len);
173
174 oem = ofpbuf_put_uninit(buf, sizeof *oem);
514887ee
BP
175 oem->type = htons(triplet->type);
176 oem->code = htons(triplet->code);
177 } else if (ofp_version <= OFP11_VERSION) {
90bf1e07
BP
178 struct nx_vendor_error *nve;
179
982697a4
BP
180 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
181 sizeof *oem + sizeof *nve + data_len);
182
183 oem = ofpbuf_put_uninit(buf, sizeof *oem);
90bf1e07
BP
184 oem->type = htons(NXET_VENDOR);
185 oem->code = htons(NXVC_VENDOR_ERROR);
186
982697a4 187 nve = ofpbuf_put_uninit(buf, sizeof *nve);
514887ee
BP
188 nve->vendor = htonl(triplet->vendor);
189 nve->type = htons(triplet->type);
190 nve->code = htons(triplet->code);
191 } else {
192 ovs_be32 vendor = htonl(triplet->vendor);
193
194 buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
195 sizeof *oem + sizeof(uint32_t) + data_len);
196
197 oem = ofpbuf_put_uninit(buf, sizeof *oem);
198 oem->type = htons(OFPET12_EXPERIMENTER);
199 oem->code = htons(triplet->type);
200 ofpbuf_put(buf, &vendor, sizeof vendor);
90bf1e07 201 }
90bf1e07 202
90bf1e07 203 ofpbuf_put(buf, data, data_len);
edd70aa7 204 ofpmsg_update_length(buf);
90bf1e07
BP
205
206 return buf;
207}
208
209/* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
210 * given 'error'.
211 *
212 * 'oh->version' determines the OpenFlow version of the error reply.
213 * 'oh->xid' determines the xid of the error reply.
214 * The error reply will contain an initial subsequence of 'oh', up to
215 * 'oh->length' or 64 bytes, whichever is shorter.
216 *
90bf1e07
BP
217 * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
218 * messages. Use ofperr_encode_hello() instead. */
219struct ofpbuf *
220ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh)
221{
90bf1e07
BP
222 uint16_t len = ntohs(oh->length);
223
9b7e2112 224 return ofperr_encode_msg__(error, oh->version, oh->xid, oh, MIN(len, 64));
90bf1e07
BP
225}
226
227/* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
228 * given 'error', in the error domain 'domain'. The error message will include
229 * the additional null-terminated text string 's'.
230 *
9b7e2112
SH
231 * If 'version' is an unknown version then OFP10_VERSION is used.
232 * OFPET_HELLO_FAILED error messages are supposed to be backward-compatible,
07c8ec21 233 * so in theory this should work. */
90bf1e07 234struct ofpbuf *
9b7e2112 235ofperr_encode_hello(enum ofperr error, enum ofp_version ofp_version,
90bf1e07
BP
236 const char *s)
237{
9b7e2112 238 return ofperr_encode_msg__(error, ofp_version, htonl(0), s, strlen(s));
90bf1e07
BP
239}
240
514887ee
BP
241int
242ofperr_get_vendor(enum ofperr error, enum ofp_version version)
243{
244 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
245 return domain ? ofperr_get_triplet__(error, domain)->vendor : -1;
246}
247
2e0525bc
SH
248/* Returns the value that would go into an OFPT_ERROR message's 'type' for
249 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
688e86e1 250 * 'version' or 'version' is unknown.
2e0525bc
SH
251 *
252 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
253int
688e86e1 254ofperr_get_type(enum ofperr error, enum ofp_version version)
2e0525bc 255{
688e86e1 256 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
514887ee 257 return domain ? ofperr_get_triplet__(error, domain)->type : -1;
2e0525bc
SH
258}
259
260/* Returns the value that would go into an OFPT_ERROR message's 'code' for
261 * encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
688e86e1
SH
262 * 'version', 'version' is unknown or if 'error' represents a category
263 * rather than a specific error.
264 *
2e0525bc
SH
265 *
266 * 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
267int
688e86e1 268ofperr_get_code(enum ofperr error, enum ofp_version version)
2e0525bc 269{
688e86e1 270 const struct ofperr_domain *domain = ofperr_domain_from_version(version);
514887ee 271 return domain ? ofperr_get_triplet__(error, domain)->code : -1;
2e0525bc
SH
272}
273
7a7b6428 274/* Tries to decode 'oh', which should be an OpenFlow OFPT_ERROR message.
90bf1e07
BP
275 * Returns an OFPERR_* constant on success, 0 on failure.
276 *
dea241f1
BP
277 * If 'payload' is nonnull, on success '*payload' is initialized with a copy of
278 * the error's payload (copying is required because the payload is not properly
279 * aligned). The caller must free the payload (with ofpbuf_uninit()) when it
280 * is no longer needed. On failure, '*payload' is cleared. */
90bf1e07 281enum ofperr
982697a4 282ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
90bf1e07 283{
90bf1e07 284 const struct ofp_error_msg *oem;
982697a4 285 enum ofpraw raw;
90bf1e07
BP
286 uint16_t type, code;
287 enum ofperr error;
514887ee 288 uint32_t vendor;
90bf1e07
BP
289 struct ofpbuf b;
290
982697a4
BP
291 if (payload) {
292 memset(payload, 0, sizeof *payload);
90bf1e07
BP
293 }
294
295 /* Pull off the error message. */
296 ofpbuf_use_const(&b, oh, ntohs(oh->length));
982697a4
BP
297 error = ofpraw_pull(&raw, &b);
298 if (error) {
90bf1e07
BP
299 return 0;
300 }
982697a4 301 oem = ofpbuf_pull(&b, sizeof *oem);
90bf1e07 302
90bf1e07 303 /* Get the error type and code. */
514887ee 304 vendor = 0;
90bf1e07
BP
305 type = ntohs(oem->type);
306 code = ntohs(oem->code);
307 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
308 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
309 if (!nve) {
310 return 0;
311 }
312
514887ee 313 vendor = ntohl(nve->vendor);
90bf1e07
BP
314 type = ntohs(nve->type);
315 code = ntohs(nve->code);
514887ee
BP
316 } else if (type == OFPET12_EXPERIMENTER) {
317 const ovs_be32 *vendorp = ofpbuf_try_pull(&b, sizeof *vendorp);
318 if (!vendorp) {
319 return 0;
320 }
321
322 vendor = ntohl(*vendorp);
323 type = code;
324 code = 0;
90bf1e07
BP
325 }
326
df30f9b1 327 /* Translate the error type and code into an ofperr. */
514887ee 328 error = ofperr_decode(oh->version, vendor, type, code);
982697a4 329 if (error && payload) {
6fd6ed71
PS
330 ofpbuf_init(payload, b.size);
331 ofpbuf_push(payload, b.data, b.size);
90bf1e07
BP
332 }
333 return error;
334}
335
336/* If 'error' is a valid OFPERR_* value, returns its name
337 * (e.g. "OFPBRC_BAD_TYPE" for OFPBRC_BAD_TYPE). Otherwise, assumes that
10a89ef0
BP
338 * 'error' is a positive errno value and returns what ovs_strerror() produces
339 * for 'error'. */
90bf1e07
BP
340const char *
341ofperr_to_string(enum ofperr error)
342{
10a89ef0
BP
343 return (ofperr_is_valid(error)
344 ? ofperr_get_name(error)
345 : ovs_strerror(error));
90bf1e07 346}