X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fofp-errors.c;h=bd4e43a4ed93d99e60a0ecaf4326531cb5292394;hb=28c5588e8e1a8d091c5d2275232c35f2968a97fa;hp=74a3358e0eca3b01a81a6e618efe456d01a79b11;hpb=dcabe8570f2acc92b022382232d82a3607f401a9;p=sliver-openvswitch.git diff --git a/lib/ofp-errors.c b/lib/ofp-errors.c index 74a3358e0..bd4e43a4e 100644 --- a/lib/ofp-errors.c +++ b/lib/ofp-errors.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2013 Nicira, Inc. + * Copyright (c) 2012, 2013, 2014 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,7 +27,8 @@ VLOG_DEFINE_THIS_MODULE(ofp_errors); -struct pair { +struct triplet { + uint32_t vendor; int type, code; }; @@ -49,6 +50,8 @@ ofperr_domain_from_version(enum ofp_version version) return &ofperr_of12; case OFP13_VERSION: return &ofperr_of13; + case OFP14_VERSION: + return &ofperr_of14; default: return NULL; } @@ -72,11 +75,12 @@ ofperr_is_valid(enum ofperr error) /* Returns the OFPERR_* value that corresponds to 'type' and 'code' within * 'version', or 0 if either no such OFPERR_* value exists or 'version' is * unknown. */ -enum ofperr -ofperr_decode(enum ofp_version version, uint16_t type, uint16_t code) +static enum ofperr +ofperr_decode(enum ofp_version version, + uint32_t vendor, uint16_t type, uint16_t code) { const struct ofperr_domain *domain = ofperr_domain_from_version(version); - return domain ? domain->decode(type, code) : 0; + return domain ? domain->decode(vendor, type, code) : 0; } /* Returns the name of 'error', e.g. "OFPBRC_BAD_TYPE" if 'error' is @@ -121,8 +125,8 @@ ofperr_get_description(enum ofperr error) : ""); } -static const struct pair * -ofperr_get_pair__(enum ofperr error, const struct ofperr_domain *domain) +static const struct triplet * +ofperr_get_triplet__(enum ofperr error, const struct ofperr_domain *domain) { size_t ofs = error - OFPERR_OFS; @@ -136,8 +140,8 @@ ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version, { static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); const struct ofperr_domain *domain; + const struct triplet *triplet; struct ofp_error_msg *oem; - const struct pair *pair; struct ofpbuf *buf; /* Get the error domain for 'ofp_version', or fall back to OF1.0. */ @@ -152,7 +156,7 @@ ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version, if (!ofperr_is_valid(error)) { /* 'error' seems likely to be a system errno value. */ VLOG_ERR_RL(&rl, "invalid OpenFlow error code %d (%s)", - error, strerror(error)); + error, ovs_strerror(error)); error = OFPERR_NXBRC_UNENCODABLE_ERROR; } else if (domain->errors[error - OFPERR_OFS].code < 0) { VLOG_ERR_RL(&rl, "cannot encode %s for %s", @@ -160,15 +164,15 @@ ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version, error = OFPERR_NXBRC_UNENCODABLE_ERROR; } - pair = ofperr_get_pair__(error, domain); - if (pair->code < 0x100) { + triplet = ofperr_get_triplet__(error, domain); + if (!triplet->vendor) { 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 { + oem->type = htons(triplet->type); + oem->code = htons(triplet->code); + } else if (ofp_version <= OFP11_VERSION) { struct nx_vendor_error *nve; buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid, @@ -179,9 +183,19 @@ ofperr_encode_msg__(enum ofperr error, enum ofp_version ofp_version, oem->code = htons(NXVC_VENDOR_ERROR); nve = ofpbuf_put_uninit(buf, sizeof *nve); - nve->vendor = htonl(NX_VENDOR_ID); - nve->type = htons(pair->type); - nve->code = htons(pair->code); + nve->vendor = htonl(triplet->vendor); + nve->type = htons(triplet->type); + nve->code = htons(triplet->code); + } else { + ovs_be32 vendor = htonl(triplet->vendor); + + buf = ofpraw_alloc_xid(OFPRAW_OFPT_ERROR, domain->version, xid, + sizeof *oem + sizeof(uint32_t) + data_len); + + oem = ofpbuf_put_uninit(buf, sizeof *oem); + oem->type = htons(OFPET12_EXPERIMENTER); + oem->code = htons(triplet->type); + ofpbuf_put(buf, &vendor, sizeof vendor); } ofpbuf_put(buf, data, data_len); @@ -222,6 +236,13 @@ ofperr_encode_hello(enum ofperr error, enum ofp_version ofp_version, return ofperr_encode_msg__(error, ofp_version, htonl(0), s, strlen(s)); } +int +ofperr_get_vendor(enum ofperr error, enum ofp_version version) +{ + const struct ofperr_domain *domain = ofperr_domain_from_version(version); + return domain ? ofperr_get_triplet__(error, domain)->vendor : -1; +} + /* 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 * 'version' or 'version' is unknown. @@ -231,7 +252,7 @@ int ofperr_get_type(enum ofperr error, enum ofp_version version) { const struct ofperr_domain *domain = ofperr_domain_from_version(version); - return domain ? ofperr_get_pair__(error, domain)->type : -1; + return domain ? ofperr_get_triplet__(error, domain)->type : -1; } /* Returns the value that would go into an OFPT_ERROR message's 'code' for @@ -245,23 +266,24 @@ int ofperr_get_code(enum ofperr error, enum ofp_version version) { const struct ofperr_domain *domain = ofperr_domain_from_version(version); - return domain ? ofperr_get_pair__(error, domain)->code : -1; + return domain ? ofperr_get_triplet__(error, domain)->code : -1; } /* Tries to decode 'oh', which should be an OpenFlow OFPT_ERROR message. * Returns an OFPERR_* constant on success, 0 on failure. * - * If 'payload' is nonnull, on success '*payload' is initialized to the - * error's payload, and on failure it is cleared. */ + * If 'payload' is nonnull, on success '*payload' is initialized with a copy of + * the error's payload (copying is required because the payload is not properly + * aligned). The caller must free the payload (with ofpbuf_uninit()) when it + * is no longer needed. On failure, '*payload' is cleared. */ enum ofperr 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 ofp_error_msg *oem; enum ofpraw raw; uint16_t type, code; enum ofperr error; + uint32_t vendor; struct ofpbuf b; if (payload) { @@ -277,6 +299,7 @@ ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload) oem = ofpbuf_pull(&b, sizeof *oem); /* Get the error type and code. */ + vendor = 0; type = ntohs(oem->type); code = ntohs(oem->code); if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) { @@ -285,29 +308,37 @@ ofperr_decode_msg(const struct ofp_header *oh, struct ofpbuf *payload) return 0; } - if (nve->vendor != htonl(NX_VENDOR_ID)) { - VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32, - ntohl(nve->vendor)); - return 0; - } + vendor = ntohl(nve->vendor); type = ntohs(nve->type); code = ntohs(nve->code); + } else if (type == OFPET12_EXPERIMENTER) { + const ovs_be32 *vendorp = ofpbuf_try_pull(&b, sizeof *vendorp); + if (!vendorp) { + return 0; + } + + vendor = ntohl(*vendorp); + type = code; + code = 0; } /* Translate the error type and code into an ofperr. */ - error = ofperr_decode(oh->version, type, code); + error = ofperr_decode(oh->version, vendor, type, code); if (error && payload) { - ofpbuf_use_const(payload, b.data, b.size); + ofpbuf_init(payload, ofpbuf_size(&b)); + ofpbuf_push(payload, ofpbuf_data(&b), ofpbuf_size(&b)); } 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'. */ + * 'error' is a positive errno value and returns what ovs_strerror() produces + * for 'error'. */ const char * ofperr_to_string(enum ofperr error) { - return ofperr_is_valid(error) ? ofperr_get_name(error) : strerror(error); + return (ofperr_is_valid(error) + ? ofperr_get_name(error) + : ovs_strerror(error)); }