2012-01-12 15:48:19 -08:00
|
|
|
#include <config.h>
|
|
|
|
#include "ofp-errors.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include "byte-order.h"
|
|
|
|
#include "dynamic-string.h"
|
2012-07-19 23:23:17 -07:00
|
|
|
#include "ofp-msgs.h"
|
2012-01-12 15:48:19 -08:00
|
|
|
#include "ofp-util.h"
|
|
|
|
#include "ofpbuf.h"
|
|
|
|
#include "openflow/openflow.h"
|
|
|
|
#include "vlog.h"
|
|
|
|
|
|
|
|
VLOG_DEFINE_THIS_MODULE(ofp_errors);
|
|
|
|
|
|
|
|
struct pair {
|
|
|
|
int type, code;
|
|
|
|
};
|
|
|
|
|
|
|
|
#include "ofp-errors.inc"
|
|
|
|
|
|
|
|
/* Returns an ofperr_domain that corresponds to the OpenFlow version number
|
|
|
|
* 'version' (one of the possible values of struct ofp_header's 'version'
|
|
|
|
* member). Returns NULL if the version isn't defined or isn't understood by
|
|
|
|
* OVS. */
|
|
|
|
const struct ofperr_domain *
|
|
|
|
ofperr_domain_from_version(uint8_t version)
|
|
|
|
{
|
|
|
|
return (version == ofperr_of10.version ? &ofperr_of10
|
|
|
|
: version == ofperr_of11.version ? &ofperr_of11
|
2012-03-26 13:46:35 -07:00
|
|
|
: version == ofperr_of12.version ? &ofperr_of12
|
2012-01-12 15:48:19 -08:00
|
|
|
: NULL);
|
|
|
|
}
|
|
|
|
|
2012-03-26 13:46:35 -07:00
|
|
|
/* Returns the name (e.g. "OpenFlow 1.0") of OpenFlow error domain 'domain'. */
|
|
|
|
const char *
|
|
|
|
ofperr_domain_get_name(const struct ofperr_domain *domain)
|
|
|
|
{
|
|
|
|
return domain->name;
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:48:19 -08:00
|
|
|
/* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
|
|
|
|
bool
|
|
|
|
ofperr_is_valid(enum ofperr error)
|
|
|
|
{
|
|
|
|
return error >= OFPERR_OFS && error < OFPERR_OFS + OFPERR_N_ERRORS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if 'error' is a valid OFPERR_* value that designates a whole
|
|
|
|
* category of errors instead of a particular error, e.g. if it is an
|
|
|
|
* OFPERR_OFPET_* value, and false otherwise. */
|
|
|
|
bool
|
|
|
|
ofperr_is_category(enum ofperr error)
|
|
|
|
{
|
|
|
|
return (ofperr_is_valid(error)
|
|
|
|
&& ofperr_of10.errors[error - OFPERR_OFS].code == -1
|
|
|
|
&& ofperr_of11.errors[error - OFPERR_OFS].code == -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if 'error' is a valid OFPERR_* value that is a Nicira
|
|
|
|
* extension, e.g. if it is an OFPERR_NX* value, and false otherwise. */
|
|
|
|
bool
|
|
|
|
ofperr_is_nx_extension(enum ofperr error)
|
|
|
|
{
|
|
|
|
return (ofperr_is_valid(error)
|
|
|
|
&& (ofperr_of10.errors[error - OFPERR_OFS].code >= 0x100 ||
|
|
|
|
ofperr_of11.errors[error - OFPERR_OFS].code >= 0x100));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if 'error' can be encoded as an OpenFlow error message in
|
|
|
|
* 'domain', false otherwise.
|
|
|
|
*
|
|
|
|
* A given error may not be encodable in some domains because each OpenFlow
|
|
|
|
* version tends to introduce new errors and retire some old ones. */
|
|
|
|
bool
|
|
|
|
ofperr_is_encodable(enum ofperr error, const struct ofperr_domain *domain)
|
|
|
|
{
|
|
|
|
return (ofperr_is_valid(error)
|
|
|
|
&& domain->errors[error - OFPERR_OFS].code >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the OFPERR_* value that corresponds to 'type' and 'code' within
|
|
|
|
* 'domain', or 0 if no such OFPERR_* value exists. */
|
|
|
|
enum ofperr
|
|
|
|
ofperr_decode(const struct ofperr_domain *domain, uint16_t type, uint16_t code)
|
|
|
|
{
|
|
|
|
return domain->decode(type, code);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the OFPERR_* value that corresponds to the category 'type' within
|
|
|
|
* 'domain', or 0 if no such OFPERR_* value exists. */
|
|
|
|
enum ofperr
|
|
|
|
ofperr_decode_type(const struct ofperr_domain *domain, uint16_t type)
|
|
|
|
{
|
|
|
|
return domain->decode_type(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the name of 'error', e.g. "OFPBRC_BAD_TYPE" if 'error' is
|
|
|
|
* OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not a valid OFPERR_* value.
|
|
|
|
*
|
|
|
|
* Consider ofperr_to_string() instead, if the error code might be an errno
|
|
|
|
* value. */
|
|
|
|
const char *
|
|
|
|
ofperr_get_name(enum ofperr error)
|
|
|
|
{
|
|
|
|
return (ofperr_is_valid(error)
|
|
|
|
? error_names[error - OFPERR_OFS]
|
|
|
|
: "<invalid>");
|
|
|
|
}
|
|
|
|
|
2012-03-26 13:46:35 -07:00
|
|
|
/* Returns the OFPERR_* value that corresponds for 'name', 0 if none exists.
|
|
|
|
* For example, returns OFPERR_OFPHFC_INCOMPATIBLE if 'name' is
|
|
|
|
* "OFPHFC_INCOMPATIBLE".
|
|
|
|
*
|
|
|
|
* This is probably useful only for debugging and testing. */
|
|
|
|
enum ofperr
|
|
|
|
ofperr_from_name(const char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < OFPERR_N_ERRORS; i++) {
|
|
|
|
if (!strcmp(name, error_names[i])) {
|
|
|
|
return i + OFPERR_OFS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:48:19 -08:00
|
|
|
/* Returns an extended description name of 'error', e.g. "ofp_header.type not
|
|
|
|
* supported." if 'error' is OFPBRC_BAD_TYPE, or "<invalid>" if 'error' is not
|
|
|
|
* a valid OFPERR_* value. */
|
|
|
|
const char *
|
|
|
|
ofperr_get_description(enum ofperr error)
|
|
|
|
{
|
|
|
|
return (ofperr_is_valid(error)
|
|
|
|
? error_comments[error - OFPERR_OFS]
|
|
|
|
: "<invalid>");
|
|
|
|
}
|
|
|
|
|
2012-03-26 13:46:35 -07:00
|
|
|
static const struct pair *
|
|
|
|
ofperr_get_pair__(enum ofperr error, const struct ofperr_domain *domain)
|
|
|
|
{
|
|
|
|
size_t ofs = error - OFPERR_OFS;
|
|
|
|
|
|
|
|
assert(ofperr_is_valid(error));
|
|
|
|
return &domain->errors[ofs];
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:48:19 -08:00
|
|
|
static struct ofpbuf *
|
|
|
|
ofperr_encode_msg__(enum ofperr error, const struct ofperr_domain *domain,
|
|
|
|
ovs_be32 xid, const void *data, size_t data_len)
|
|
|
|
{
|
|
|
|
struct ofp_error_msg *oem;
|
|
|
|
const struct pair *pair;
|
|
|
|
struct ofpbuf *buf;
|
|
|
|
|
|
|
|
if (!domain) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ofperr_is_encodable(error, domain)) {
|
|
|
|
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
|
|
|
|
|
|
|
|
if (!ofperr_is_valid(error)) {
|
|
|
|
/* 'error' seems likely to be a system errno value. */
|
|
|
|
VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
|
|
|
|
error, strerror(error));
|
|
|
|
} else {
|
|
|
|
const char *s = ofperr_get_name(error);
|
|
|
|
if (ofperr_is_category(error)) {
|
|
|
|
VLOG_WARN_RL(&rl, "cannot encode error category (%s)", s);
|
|
|
|
} else {
|
|
|
|
VLOG_WARN_RL(&rl, "cannot encode %s for %s", s, domain->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-26 13:46:35 -07:00
|
|
|
pair = ofperr_get_pair__(error, domain);
|
2012-01-12 15:48:19 -08:00
|
|
|
if (!ofperr_is_nx_extension(error)) {
|
2012-07-19 23:23:17 -07:00
|
|
|
buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
|
|
|
|
sizeof *oem + data_len);
|
|
|
|
|
|
|
|
oem = ofpbuf_put_uninit(buf, sizeof *oem);
|
2012-01-12 15:48:19 -08:00
|
|
|
oem->type = htons(pair->type);
|
|
|
|
oem->code = htons(pair->code);
|
|
|
|
} else {
|
|
|
|
struct nx_vendor_error *nve;
|
|
|
|
|
2012-07-19 23:23:17 -07:00
|
|
|
buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
|
|
|
|
sizeof *oem + sizeof *nve + data_len);
|
|
|
|
|
|
|
|
oem = ofpbuf_put_uninit(buf, sizeof *oem);
|
2012-01-12 15:48:19 -08:00
|
|
|
oem->type = htons(NXET_VENDOR);
|
|
|
|
oem->code = htons(NXVC_VENDOR_ERROR);
|
|
|
|
|
2012-07-19 23:23:17 -07:00
|
|
|
nve = ofpbuf_put_uninit(buf, sizeof *nve);
|
2012-01-12 15:48:19 -08:00
|
|
|
nve->vendor = htonl(NX_VENDOR_ID);
|
|
|
|
nve->type = htons(pair->type);
|
|
|
|
nve->code = htons(pair->code);
|
|
|
|
}
|
|
|
|
|
|
|
|
ofpbuf_put(buf, data, data_len);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
|
|
|
|
* given 'error'.
|
|
|
|
*
|
|
|
|
* 'oh->version' determines the OpenFlow version of the error reply.
|
|
|
|
* 'oh->xid' determines the xid of the error reply.
|
|
|
|
* The error reply will contain an initial subsequence of 'oh', up to
|
|
|
|
* 'oh->length' or 64 bytes, whichever is shorter.
|
|
|
|
*
|
|
|
|
* Returns NULL if 'error' is not an OpenFlow error code or if 'error' cannot
|
|
|
|
* be encoded as OpenFlow version 'oh->version'.
|
|
|
|
*
|
|
|
|
* This function isn't appropriate for encoding OFPET_HELLO_FAILED error
|
|
|
|
* messages. Use ofperr_encode_hello() instead. */
|
|
|
|
struct ofpbuf *
|
|
|
|
ofperr_encode_reply(enum ofperr error, const struct ofp_header *oh)
|
|
|
|
{
|
|
|
|
const struct ofperr_domain *domain;
|
|
|
|
uint16_t len = ntohs(oh->length);
|
|
|
|
|
|
|
|
domain = ofperr_domain_from_version(oh->version);
|
|
|
|
return ofperr_encode_msg__(error, domain, oh->xid, oh, MIN(len, 64));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Creates and returns an OpenFlow message of type OFPT_ERROR that conveys the
|
|
|
|
* given 'error', in the error domain 'domain'. The error message will include
|
|
|
|
* the additional null-terminated text string 's'.
|
|
|
|
*
|
|
|
|
* If 'domain' is NULL, uses the OpenFlow 1.0 error domain. OFPET_HELLO_FAILED
|
|
|
|
* error messages are supposed to be backward-compatible, so in theory this
|
|
|
|
* should work.
|
|
|
|
*
|
|
|
|
* Returns NULL if 'error' is not an OpenFlow error code or if 'error' cannot
|
|
|
|
* be encoded in 'domain'. */
|
|
|
|
struct ofpbuf *
|
|
|
|
ofperr_encode_hello(enum ofperr error, const struct ofperr_domain *domain,
|
|
|
|
const char *s)
|
|
|
|
{
|
|
|
|
if (!domain) {
|
|
|
|
domain = &ofperr_of10;
|
|
|
|
}
|
|
|
|
return ofperr_encode_msg__(error, domain, htonl(0), s, strlen(s));
|
|
|
|
}
|
|
|
|
|
2012-03-26 13:46:35 -07:00
|
|
|
/* Returns the value that would go into an OFPT_ERROR message's 'type' for
|
|
|
|
* encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
|
|
|
|
* 'domain'.
|
|
|
|
*
|
|
|
|
* 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
|
|
|
|
int
|
|
|
|
ofperr_get_type(enum ofperr error, const struct ofperr_domain *domain)
|
|
|
|
{
|
|
|
|
return ofperr_get_pair__(error, domain)->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the value that would go into an OFPT_ERROR message's 'code' for
|
|
|
|
* encoding 'error' in 'domain'. Returns -1 if 'error' is not encodable in
|
|
|
|
* 'domain' or if 'error' represents a category rather than a specific error.
|
|
|
|
*
|
|
|
|
* 'error' must be a valid OFPERR_* code, as checked by ofperr_is_valid(). */
|
|
|
|
int
|
|
|
|
ofperr_get_code(enum ofperr error, const struct ofperr_domain *domain)
|
|
|
|
{
|
|
|
|
return ofperr_get_pair__(error, domain)->code;
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:48:19 -08:00
|
|
|
/* Tries to decodes 'oh', which should be an OpenFlow OFPT_ERROR message.
|
|
|
|
* Returns an OFPERR_* constant on success, 0 on failure.
|
|
|
|
*
|
2012-07-19 23:23:17 -07:00
|
|
|
* If 'payload' is nonnull, on success '*payload' is initialized to the
|
|
|
|
* error's payload, and on failure it is cleared. */
|
2012-01-12 15:48:19 -08:00
|
|
|
enum ofperr
|
2012-07-19 23:23:17 -07:00
|
|
|
ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
|
2012-01-12 15:48:19 -08:00
|
|
|
{
|
|
|
|
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
|
|
|
|
|
|
|
|
const struct ofperr_domain *domain;
|
|
|
|
const struct ofp_error_msg *oem;
|
2012-07-19 23:23:17 -07:00
|
|
|
enum ofpraw raw;
|
2012-01-12 15:48:19 -08:00
|
|
|
uint16_t type, code;
|
|
|
|
enum ofperr error;
|
|
|
|
struct ofpbuf b;
|
|
|
|
|
2012-07-19 23:23:17 -07:00
|
|
|
if (payload) {
|
|
|
|
memset(payload, 0, sizeof *payload);
|
2012-01-12 15:48:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Pull off the error message. */
|
|
|
|
ofpbuf_use_const(&b, oh, ntohs(oh->length));
|
2012-07-19 23:23:17 -07:00
|
|
|
error = ofpraw_pull(&raw, &b);
|
|
|
|
if (error) {
|
2012-01-12 15:48:19 -08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-07-19 23:23:17 -07:00
|
|
|
oem = ofpbuf_pull(&b, sizeof *oem);
|
2012-01-12 15:48:19 -08:00
|
|
|
|
2012-07-19 23:23:17 -07:00
|
|
|
/* Check version. */
|
2012-01-12 15:48:19 -08:00
|
|
|
domain = ofperr_domain_from_version(oh->version);
|
|
|
|
if (!domain) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the error type and code. */
|
|
|
|
type = ntohs(oem->type);
|
|
|
|
code = ntohs(oem->code);
|
|
|
|
if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
|
|
|
|
const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
|
|
|
|
if (!nve) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nve->vendor != htonl(NX_VENDOR_ID)) {
|
|
|
|
VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
|
|
|
|
ntohl(nve->vendor));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
type = ntohs(nve->type);
|
|
|
|
code = ntohs(nve->code);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Translate the error type and code into an ofperr.
|
|
|
|
* If we don't know the error type and code, at least try for the type. */
|
|
|
|
error = ofperr_decode(domain, type, code);
|
|
|
|
if (!error) {
|
|
|
|
error = ofperr_decode_type(domain, type);
|
|
|
|
}
|
2012-07-19 23:23:17 -07:00
|
|
|
if (error && payload) {
|
|
|
|
ofpbuf_use_const(payload, b.data, b.size);
|
2012-01-12 15:48:19 -08:00
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If 'error' is a valid OFPERR_* value, returns its name
|
|
|
|
* (e.g. "OFPBRC_BAD_TYPE" for OFPBRC_BAD_TYPE). Otherwise, assumes that
|
|
|
|
* 'error' is a positive errno value and returns what strerror() produces for
|
|
|
|
* 'error'. */
|
|
|
|
const char *
|
|
|
|
ofperr_to_string(enum ofperr error)
|
|
|
|
{
|
|
|
|
return ofperr_is_valid(error) ? ofperr_get_name(error) : strerror(error);
|
|
|
|
}
|