X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fuuid.c;h=18748ea954c7e611c06863b336d0b8983ec86868;hb=b0fb94a346e52f36aeef238dd5f9bef9a10c14ef;hp=2aac4c7201f661ec1f168dac820b8ff5f45def85;hpb=c9cdd3d3a7139dc618ea603010bf6c73d2b4e21e;p=sliver-openvswitch.git diff --git a/lib/uuid.c b/lib/uuid.c index 2aac4c720..18748ea95 100644 --- a/lib/uuid.c +++ b/lib/uuid.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2008, 2009, 2010 Nicira Networks +/* Copyright (c) 2008, 2009, 2010, 2011, 2013 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,9 @@ #include "aes128.h" #include "entropy.h" +#include "ovs-thread.h" #include "sha1.h" +#include "timeval.h" #include "util.h" static struct aes128 key; @@ -48,11 +50,8 @@ static void do_init(void); void uuid_init(void) { - static bool inited; - if (!inited) { - do_init(); - inited = true; - } + static pthread_once_t once = PTHREAD_ONCE_INIT; + pthread_once(&once, do_init); } /* Generates a new random UUID in 'uuid'. @@ -82,15 +81,22 @@ uuid_init(void) void uuid_generate(struct uuid *uuid) { + static struct ovs_mutex mutex = OVS_ADAPTIVE_MUTEX_INITIALIZER; + uint64_t copy[2]; + uuid_init(); - /* Increment the counter. */ + /* Copy out the counter's current value, then increment it. */ + ovs_mutex_lock(&mutex); + copy[0] = counter[0]; + copy[1] = counter[1]; if (++counter[1] == 0) { counter[0]++; } + ovs_mutex_unlock(&mutex); /* AES output is exactly 16 bytes, so we encrypt directly into 'uuid'. */ - aes128_encrypt(&key, counter, uuid); + aes128_encrypt(&key, copy, uuid); /* Set bits to indicate a random UUID. See RFC 4122 section 4.4. */ uuid->parts[2] &= ~0xc0000000; @@ -156,24 +162,44 @@ uuid_from_string(struct uuid *uuid, const char *s) bool uuid_from_string_prefix(struct uuid *uuid, const char *s) { - static const char template[] = "00000000-1111-1111-2222-222233333333"; - const char *t; + /* 0 1 2 3 */ + /* 012345678901234567890123456789012345 */ + /* ------------------------------------ */ + /* 00000000-1111-1111-2222-222233333333 */ - uuid_zero(uuid); - for (t = template; ; t++, s++) { - if (*t >= '0' && *t <= '3') { - uint32_t *part = &uuid->parts[*t - '0']; - if (!isxdigit(*s)) { - goto error; - } - *part = (*part << 4) + hexit_value(*s); - } else if (*t == 0) { - return true; - } else if (*t != *s) { - goto error; - } + bool ok; + + uuid->parts[0] = hexits_value(s, 8, &ok); + if (!ok || s[8] != '-') { + goto error; + } + + uuid->parts[1] = hexits_value(s + 9, 4, &ok) << 16; + if (!ok || s[13] != '-') { + goto error; + } + + uuid->parts[1] += hexits_value(s + 14, 4, &ok); + if (!ok || s[18] != '-') { + goto error; } + uuid->parts[2] = hexits_value(s + 19, 4, &ok) << 16; + if (!ok || s[23] != '-') { + goto error; + } + + uuid->parts[2] += hexits_value(s + 24, 4, &ok); + if (!ok) { + goto error; + } + + uuid->parts[3] = hexits_value(s + 28, 8, &ok); + if (!ok) { + goto error; + } + return true; + error: uuid_zero(uuid); return false; @@ -192,9 +218,7 @@ do_init(void) /* Get seed data. */ get_entropy_or_die(random_seed, sizeof random_seed); - if (gettimeofday(&now, NULL)) { - ovs_fatal(errno, "gettimeofday failed"); - } + xgettimeofday(&now); pid = getpid(); ppid = getppid(); uid = getuid();