From: Ben Pfaff Date: Wed, 11 Mar 2009 00:01:16 +0000 (-0700) Subject: cfg: Add ability to parse datapath IDs. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9e2c0af8c8a30cea18a58922816aa4a60ab77308;p=openvswitch cfg: Add ability to parse datapath IDs. --- diff --git a/lib/cfg.c b/lib/cfg.c index 00d355ef..5994ad3e 100644 --- a/lib/cfg.c +++ b/lib/cfg.c @@ -70,6 +70,7 @@ static char **find_key_le(const char *key); static char **find_key_ge(const char *key); static char *find_key(const char *); static bool parse_mac(const char *, uint8_t mac[6]); +static bool parse_dpid(const char *, uint64_t *); static bool is_key(const char *); static bool is_int(const char *); static bool is_bool(const char *); @@ -368,6 +369,26 @@ cfg_get_mac(int idx, const char *key_, ...) return eth_addr_to_uint64(mac); } +/* Returns the value numbered 'idx' of 'key', parsed as an datapath ID. + * Returns 0 if 'idx' is greater than or equal to cfg_count(key) or if the + * value 'idx' of 'key' is not a valid datapath ID consisting of exactly 12 + * hexadecimal digits. */ +uint64_t +cfg_get_dpid(int idx, const char *key_, ...) +{ + uint64_t dpid; + const char *value; + char *key; + + FORMAT_KEY(key_, key); + value = get_nth_value(idx, key); + if (!value || !parse_dpid(value, &dpid)) { + dpid = 0; + } + free(key); + return dpid; +} + /* Returns the value numbered 'idx' of 'key', converted to an integer. Returns * -1 if 'idx' is greater than or equal to cfg_count(key) or if the value 'idx' * of 'key' is not a valid integer between 0 and 4095. */ @@ -739,6 +760,17 @@ parse_mac(const char *s, uint8_t mac[6]) &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) == 6; } +static bool +parse_dpid(const char *s, uint64_t *dpid) +{ + if (strlen(s) == 12 && strspn(s, "0123456789abcdefABCDEF") == 12) { + *dpid = strtoll(s, NULL, 16); + return true; + } else { + return false; + } +} + static bool is_key(const char *s) { @@ -778,11 +810,13 @@ is_type(const char *s, enum cfg_flags flags) { uint8_t mac[ETH_ADDR_LEN]; struct in_addr addr; + uint64_t dpid; return (flags & CFG_STRING || (flags & CFG_KEY && is_key(s)) || (flags & CFG_INT && is_int(s)) || (flags & CFG_BOOL && is_bool(s)) || (flags & CFG_IP && inet_aton(s, &addr)) - || (flags & CFG_MAC && parse_mac(s, mac))); + || (flags & CFG_MAC && parse_mac(s, mac)) + || (flags & CFG_DPID && parse_dpid(s, &dpid))); } diff --git a/lib/cfg.h b/lib/cfg.h index 250a7152..a4139adf 100644 --- a/lib/cfg.h +++ b/lib/cfg.h @@ -48,10 +48,11 @@ enum cfg_flags { CFG_IP = 1 << 4, /* IPv4 address. */ CFG_MAC = 1 << 5, /* MAC address. */ CFG_VLAN = 1 << 6, /* Integer in range 0...4095. */ + CFG_DPID = 1 << 7, /* 12 hexadecimal digits. */ /* Number allowed. */ - CFG_REQUIRED = 1 << 7, /* At least one value allowed. */ - CFG_MULTIPLE = 1 << 8 /* More than one value allowed. */ + CFG_REQUIRED = 1 << 8, /* At least one value allowed. */ + CFG_MULTIPLE = 1 << 9 /* More than one value allowed. */ }; void cfg_register(const char *key_spec, enum cfg_flags); @@ -67,6 +68,7 @@ bool cfg_get_bool(int idx, const char *key, ...) PRINTF_FORMAT(2, 3); uint32_t cfg_get_ip(int idx, const char *key, ...) PRINTF_FORMAT(2, 3); uint64_t cfg_get_mac(int idx, const char *key, ...) PRINTF_FORMAT(2, 3); int cfg_get_vlan(int idx, const char *key, ...) PRINTF_FORMAT(2, 3); +uint64_t cfg_get_dpid(int idx, const char *key, ...) PRINTF_FORMAT(2, 3); void cfg_get_all_strings(struct svec *, const char *key, ...) PRINTF_FORMAT(2, 3);