#include <errno.h>
#include "byte-order.h"
#include "dynamic-string.h"
+#include "ofp-msgs.h"
#include "ofp-util.h"
#include "ofpbuf.h"
#include "openflow/openflow.h"
{
return (version == ofperr_of10.version ? &ofperr_of10
: version == ofperr_of11.version ? &ofperr_of11
+ : version == ofperr_of12.version ? &ofperr_of12
: NULL);
}
+/* 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;
+}
+
/* Returns true if 'error' is a valid OFPERR_* value, false otherwise. */
bool
ofperr_is_valid(enum ofperr error)
: "<invalid>");
}
+/* 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;
+}
+
/* 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. */
: "<invalid>");
}
+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];
+}
+
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;
- size_t ofs;
if (!domain) {
return NULL;
return NULL;
}
- ofs = error - OFPERR_OFS;
- pair = &domain->errors[ofs];
+ pair = ofperr_get_pair__(error, domain);
if (!ofperr_is_nx_extension(error)) {
- oem = make_openflow_xid(data_len + sizeof *oem, OFPT_ERROR, xid, &buf);
+ buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
+ sizeof *oem + data_len);
+
+ oem = ofpbuf_put_uninit(buf, sizeof *oem);
oem->type = htons(pair->type);
oem->code = htons(pair->code);
} else {
struct nx_vendor_error *nve;
- oem = make_openflow_xid(data_len + sizeof *oem + sizeof *nve,
- OFPT_ERROR, xid, &buf);
+ buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid,
+ sizeof *oem + sizeof *nve + data_len);
+
+ oem = ofpbuf_put_uninit(buf, sizeof *oem);
oem->type = htons(NXET_VENDOR);
oem->code = htons(NXVC_VENDOR_ERROR);
- nve = (struct nx_vendor_error *) oem->data;
+ nve = ofpbuf_put_uninit(buf, sizeof *nve);
nve->vendor = htonl(NX_VENDOR_ID);
nve->type = htons(pair->type);
nve->code = htons(pair->code);
}
- oem->header.version = domain->version;
- buf->size -= data_len;
ofpbuf_put(buf, data, data_len);
return buf;
return ofperr_encode_msg__(error, domain, htonl(0), s, strlen(s));
}
+/* 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;
+}
+
/* Tries to decodes 'oh', which should be an OpenFlow OFPT_ERROR message.
* Returns an OFPERR_* constant on success, 0 on failure.
*
- * If 'payload_ofs' is nonnull, on success '*payload_ofs' is set to the offset
- * to the payload starting from 'oh' and on failure it is set to 0. */
+ * If 'payload' is nonnull, on success '*payload' is initialized to the
+ * error's payload, and on failure it is cleared. */
enum ofperr
-ofperr_decode_msg(const struct ofp_header *oh, size_t *payload_ofs)
+ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload)
{
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
const struct ofperr_domain *domain;
const struct ofp_error_msg *oem;
+ enum ofpraw raw;
uint16_t type, code;
enum ofperr error;
struct ofpbuf b;
- if (payload_ofs) {
- *payload_ofs = 0;
+ if (payload) {
+ memset(payload, 0, sizeof *payload);
}
/* Pull off the error message. */
ofpbuf_use_const(&b, oh, ntohs(oh->length));
- oem = ofpbuf_try_pull(&b, sizeof *oem);
- if (!oem) {
+ error = ofpraw_pull(&raw, &b);
+ if (error) {
return 0;
}
+ oem = ofpbuf_pull(&b, sizeof *oem);
- /* Check message type and version. */
- if (oh->type != OFPT_ERROR) {
- return 0;
- }
+ /* Check version. */
domain = ofperr_domain_from_version(oh->version);
if (!domain) {
return 0;
if (!error) {
error = ofperr_decode_type(domain, type);
}
- if (error && payload_ofs) {
- *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
+ if (error && payload) {
+ ofpbuf_use_const(payload, b.data, b.size);
}
return error;
}