From 03e1125c312b223a28eb75454cc418a044df3ade Mon Sep 17 00:00:00 2001 From: Simon Horman Date: Sun, 4 Nov 2012 21:40:54 -0800 Subject: [PATCH] ofp-util: Add functions to support version number bitmaps. The bitmap is simple a uint32_t, allowing for versions up until wire-protocol number 31. Bit offsets correspond to ofp_version numbers which in turn correspond to wire-protocol numbers for Open Flow versions.. E.g. (1 << OFP10_VERSION) is the mask for Open Flow 1.1. If the bit for a version is set then it is allowed, otherwise it is disallowed. This is in preparation for allowing the range of allowed OpenFlow versions to be configured. Signed-off-by: Simon Horman [blp@nicira.com simplified some functions to constants, and rewrote a few functions in terms of utility bitwise functions] Signed-off-by: Ben Pfaff --- lib/ofp-util.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/ofp-util.h | 26 +++++++++++++ 2 files changed, 130 insertions(+) diff --git a/lib/ofp-util.c b/lib/ofp-util.c index 20306e48..afb7dcdf 100644 --- a/lib/ofp-util.c +++ b/lib/ofp-util.c @@ -16,6 +16,7 @@ #include #include "ofp-print.h" +#include #include #include #include @@ -846,6 +847,71 @@ ofputil_protocols_from_string(const char *s) return protocols; } +static enum ofp_version +ofputil_version_from_string(const char *s) +{ + if (!strcasecmp(s, "OpenFlow10")) { + return OFP10_VERSION; + } + if (!strcasecmp(s, "OpenFlow11")) { + return OFP11_VERSION; + } + if (!strcasecmp(s, "OpenFlow12")) { + return OFP12_VERSION; + } + VLOG_FATAL("Unknown OpenFlow version: \"%s\"", s); +} + +static bool +is_delimiter(char c) +{ + return isspace(c) || c == ','; +} + +uint32_t +ofputil_versions_from_string(const char *s) +{ + size_t i = 0; + uint32_t bitmap = 0; + + while (s[i]) { + size_t j; + enum ofp_version version; + char *key; + + if (is_delimiter(s[i])) { + i++; + continue; + } + j = 0; + while (s[i + j] && !is_delimiter(s[i + j])) { + j++; + } + key = xmemdup0(s + i, j); + version = ofputil_version_from_string(key); + free(key); + bitmap |= 1u << version; + i += j; + } + + return bitmap; +} + +const char * +ofputil_version_to_string(enum ofp_version ofp_version) +{ + switch (ofp_version) { + case OFP10_VERSION: + return "OpenFlow10"; + case OFP11_VERSION: + return "OpenFlow11"; + case OFP12_VERSION: + return "OpenFlow12"; + default: + NOT_REACHED(); + } +} + bool ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format) { @@ -974,6 +1040,44 @@ ofputil_usable_protocols(const struct match *match) return OFPUTIL_P_ANY; } +void +ofputil_format_version(struct ds *msg, enum ofp_version version) +{ + ds_put_format(msg, "0x%02zx", version); +} + +void +ofputil_format_version_name(struct ds *msg, enum ofp_version version) +{ + ds_put_cstr(msg, ofputil_version_to_string(version)); +} + +static void +ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap, + void (*format_version)(struct ds *msg, + enum ofp_version)) +{ + while (bitmap) { + format_version(msg, raw_ctz(bitmap)); + bitmap = zero_rightmost_1bit(bitmap); + if (bitmap) { + ds_put_cstr(msg, ", "); + } + } +} + +void +ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap) +{ + ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version); +} + +void +ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap) +{ + ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name); +} + /* Returns an OpenFlow message that, sent on an OpenFlow connection whose * protocol is 'current', at least partly transitions the protocol to 'want'. * Stores in '*next' the protocol that will be in effect on the OpenFlow diff --git a/lib/ofp-util.h b/lib/ofp-util.h index b6268daf..ad9f2bbb 100644 --- a/lib/ofp-util.h +++ b/lib/ofp-util.h @@ -98,6 +98,32 @@ char *ofputil_protocols_to_string(enum ofputil_protocol); enum ofputil_protocol ofputil_protocols_from_string(const char *); enum ofputil_protocol ofputil_usable_protocols(const struct match *); +void ofputil_format_version(struct ds *, enum ofp_version); +void ofputil_format_version_name(struct ds *, enum ofp_version); + +/* A bitmap of version numbers + * + * Bit offsets correspond to ofp_version numbers which in turn correspond to + * wire-protocol numbers for Open Flow versions.. E.g. (1u << OFP11_VERSION) + * is the mask for Open Flow 1.1. If the bit for a version is set then it is + * allowed, otherwise it is disallowed. */ + +void ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap); +void ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap); + +/* Bitmap of OpenFlow versions that Open vSwitch supports. */ +#define OFPUTIL_SUPPORTED_VERSIONS \ + ((1u << OFP10_VERSION) | (1u << OFP12_VERSION)) + +/* Bitmap of OpenFlow versions to enable by default (a subset of + * OFPUTIL_SUPPORTED_VERSIONS). */ +#define OFPUTIL_DEFAULT_VERSIONS (1u << OFP10_VERSION) + +enum ofputil_protocol ofputil_protocols_from_string(const char *s); + +const char *ofputil_version_to_string(enum ofp_version ofp_version); +uint32_t ofputil_versions_from_string(const char *s); + struct ofpbuf *ofputil_encode_set_protocol(enum ofputil_protocol current, enum ofputil_protocol want, enum ofputil_protocol *next); -- 2.30.2