2 #include "ofp-errors.h"
4 #include "byte-order.h"
5 #include "dynamic-string.h"
8 #include "openflow/openflow.h"
11 VLOG_DEFINE_THIS_MODULE(ofp_errors);
17 #include "ofp-errors.inc"
19 /* Returns an ofperr_domain that corresponds to the OpenFlow version number
20 * 'version' (one of the possible values of struct ofp_header's 'version'
21 * member). Returns NULL if the version isn't defined or isn't understood by
23 const struct ofperr_domain *
24 ofperr_domain_from_version(uint8_t version)
26 return (version == ofperr_of10.version ? &ofperr_of10
27 : version == ofperr_of11.version ? &ofperr_of11
31 /* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
33 ofperr_is_valid(enum ofperr error)
35 return error >= OFPERR_OFS && error < OFPERR_OFS + OFPERR_N_ERRORS;
38 /* Returns true if 'error' is a valid OFPERR_* value that designates a whole
39 * category of errors instead of a particular error, e.g. if it is an
40 * OFPERR_OFPET_* value, and false otherwise. */
42 ofperr_is_category(enum ofperr error)
44 return (ofperr_is_valid(error)
45 && ofperr_of10.errors[error - OFPERR_OFS].code == -1
46 && ofperr_of11.errors[error - OFPERR_OFS].code == -1);
49 /* Returns true if 'error' is a valid OFPERR_* value that is a Nicira
50 * extension, e.g. if it is an OFPERR_NX* value, and false otherwise. */
52 ofperr_is_nx_extension(enum ofperr error)
54 return (ofperr_is_valid(error)
55 && (ofperr_of10.errors[error - OFPERR_OFS].code >= 0x100 ||
56 ofperr_of11.errors[error - OFPERR_OFS].code >= 0x100));
59 /* Returns true if 'error' can be encoded as an OpenFlow error message in
60 * 'domain', false otherwise.
62 * A given error may not be encodable in some domains because each OpenFlow
63 * version tends to introduce new errors and retire some old ones. */
65 ofperr_is_encodable(enum ofperr error, const struct ofperr_domain *domain)
67 return (ofperr_is_valid(error)
68 && domain->errors[error - OFPERR_OFS].code >= 0);
71 /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
72 * 'domain', or 0 if no such OFPERR_* value exists. */
74 ofperr_decode(const struct ofperr_domain *domain, uint16_t type, uint16_t code)
76 return domain->decode(type, code);
79 /* Returns the OFPERR_* value that corresponds to the category 'type' within
80 * 'domain', or 0 if no such OFPERR_* value exists. */
82 ofperr_decode_type(const struct ofperr_domain *domain, uint16_t type)
84 return domain->decode_type(type);
87 /* Returns the name of 'error', e.g. "OFPBRC_BAD_TYPE" if 'error' is
88 * OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not a valid OFPERR_* value.
90 * Consider ofperr_to_string() instead, if the error code might be an errno
93 ofperr_get_name(enum ofperr error)
95 return (ofperr_is_valid(error)
96 ? error_names[error - OFPERR_OFS]
100 /* Returns an extended description name of 'error', e.g. "ofp_header.type not
101 * supported." if 'error' is OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not
102 * a valid OFPERR_* value. */
104 ofperr_get_description(enum ofperr error)
106 return (ofperr_is_valid(error)
107 ? error_comments[error - OFPERR_OFS]
111 static struct ofpbuf *
112 ofperr_encode_msg__(enum ofperr error, const struct ofperr_domain *domain,
113 ovs_be32 xid, const void *data, size_t data_len)
115 struct ofp_error_msg *oem;
116 const struct pair *pair;
124 if (!ofperr_is_encodable(error, domain)) {
125 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
127 if (!ofperr_is_valid(error)) {
128 /* 'error' seems likely to be a system errno value. */
129 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
130 error, strerror(error));
132 const char *s = ofperr_get_name(error);
133 if (ofperr_is_category(error)) {
134 VLOG_WARN_RL(&rl, "cannot encode error category (%s)", s);
136 VLOG_WARN_RL(&rl, "cannot encode %s for %s", s, domain->name);
143 ofs = error - OFPERR_OFS;
144 pair = &domain->errors[ofs];
145 if (!ofperr_is_nx_extension(error)) {
146 oem = make_openflow_xid(data_len + sizeof *oem, OFPT_ERROR, xid, &buf);
147 oem->type = htons(pair->type);
148 oem->code = htons(pair->code);
150 struct nx_vendor_error *nve;
152 oem = make_openflow_xid(data_len + sizeof *oem + sizeof *nve,
153 OFPT_ERROR, xid, &buf);
154 oem->type = htons(NXET_VENDOR);
155 oem->code = htons(NXVC_VENDOR_ERROR);
157 nve = (struct nx_vendor_error *) oem->data;
158 nve->vendor = htonl(NX_VENDOR_ID);
159 nve->type = htons(pair->type);
160 nve->code = htons(pair->code);
162 oem->header.version = domain->version;
164 buf->size -= data_len;
165 ofpbuf_put(buf, data, data_len);
170 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
173 * 'oh->version' determines the OpenFlow version of the error reply.
174 * 'oh->xid' determines the xid of the error reply.
175 * The error reply will contain an initial subsequence of 'oh', up to
176 * 'oh->length' or 64 bytes, whichever is shorter.
178 * Returns NULL if 'error' is not an OpenFlow error code or if 'error' cannot
179 * be encoded as OpenFlow version 'oh->version'.
181 * This function isn't appropriate for encoding OFPET_HELLO_FAILED error
182 * messages. Use ofperr_encode_hello() instead. */
184 ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh)
186 const struct ofperr_domain *domain;
187 uint16_t len = ntohs(oh->length);
189 domain = ofperr_domain_from_version(oh->version);
190 return ofperr_encode_msg__(error, domain, oh->xid, oh, MIN(len, 64));
193 /* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
194 * given 'error', in the error domain 'domain'. The error message will include
195 * the additional null-terminated text string 's'.
197 * If 'domain' is NULL, uses the OpenFlow 1.0 error domain. OFPET_HELLO_FAILED
198 * error messages are supposed to be backward-compatible, so in theory this
201 * Returns NULL if 'error' is not an OpenFlow error code or if 'error' cannot
202 * be encoded in 'domain'. */
204 ofperr_encode_hello(enum ofperr error, const struct ofperr_domain *domain,
208 domain = &ofperr_of10;
210 return ofperr_encode_msg__(error, domain, htonl(0), s, strlen(s));
213 /* Tries to decodes 'oh', which should be an OpenFlow OFPT_ERROR message.
214 * Returns an OFPERR_* constant on success, 0 on failure.
216 * If 'payload_ofs' is nonnull, on success '*payload_ofs' is set to the offset
217 * to the payload starting from 'oh' and on failure it is set to 0. */
219 ofperr_decode_msg(const struct ofp_header *oh, size_t *payload_ofs)
221 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
223 const struct ofperr_domain *domain;
224 const struct ofp_error_msg *oem;
233 /* Pull off the error message. */
234 ofpbuf_use_const(&b, oh, ntohs(oh->length));
235 oem = ofpbuf_try_pull(&b, sizeof *oem);
240 /* Check message type and version. */
241 if (oh->type != OFPT_ERROR) {
244 domain = ofperr_domain_from_version(oh->version);
249 /* Get the error type and code. */
250 type = ntohs(oem->type);
251 code = ntohs(oem->code);
252 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
253 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
258 if (nve->vendor != htonl(NX_VENDOR_ID)) {
259 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
263 type = ntohs(nve->type);
264 code = ntohs(nve->code);
267 /* Translate the error type and code into an ofperr.
268 * If we don't know the error type and code, at least try for the type. */
269 error = ofperr_decode(domain, type, code);
271 error = ofperr_decode_type(domain, type);
273 if (error && payload_ofs) {
274 *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
279 /* If 'error' is a valid OFPERR_* value, returns its name
280 * (e.g. "OFPBRC_BAD_TYPE" for OFPBRC_BAD_TYPE). Otherwise, assumes that
281 * 'error' is a positive errno value and returns what strerror() produces for
284 ofperr_to_string(enum ofperr error)
286 return ofperr_is_valid(error) ? ofperr_get_name(error) : strerror(error);