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