~james-page/ubuntu/saucy/openvswitch/1.12-snapshot

« back to all changes in this revision

Viewing changes to lib/uuid.c

  • Committer: James Page
  • Date: 2013-08-21 10:16:57 UTC
  • mfrom: (1.1.20)
  • Revision ID: james.page@canonical.com-20130821101657-3o0z0qeiv5zkwlzi
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2008, 2009, 2010, 2011 Nicira, Inc.
 
1
/* Copyright (c) 2008, 2009, 2010, 2011, 2013 Nicira, Inc.
2
2
 *
3
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
4
 * you may not use this file except in compliance with the License.
26
26
 
27
27
#include "aes128.h"
28
28
#include "entropy.h"
 
29
#include "ovs-thread.h"
29
30
#include "sha1.h"
30
31
#include "timeval.h"
31
32
#include "util.h"
49
50
void
50
51
uuid_init(void)
51
52
{
52
 
    static bool inited;
53
 
    if (!inited) {
54
 
        do_init();
55
 
        inited = true;
56
 
    }
 
53
    static pthread_once_t once = PTHREAD_ONCE_INIT;
 
54
    pthread_once(&once, do_init);
57
55
}
58
56
 
59
57
/* Generates a new random UUID in 'uuid'.
83
81
void
84
82
uuid_generate(struct uuid *uuid)
85
83
{
 
84
    static struct ovs_mutex mutex = OVS_ADAPTIVE_MUTEX_INITIALIZER;
 
85
    uint64_t copy[2];
 
86
 
86
87
    uuid_init();
87
88
 
88
 
    /* Increment the counter. */
 
89
    /* Copy out the counter's current value, then increment it. */
 
90
    ovs_mutex_lock(&mutex);
 
91
    copy[0] = counter[0];
 
92
    copy[1] = counter[1];
89
93
    if (++counter[1] == 0) {
90
94
        counter[0]++;
91
95
    }
 
96
    ovs_mutex_unlock(&mutex);
92
97
 
93
98
    /* AES output is exactly 16 bytes, so we encrypt directly into 'uuid'. */
94
 
    aes128_encrypt(&key, counter, uuid);
 
99
    aes128_encrypt(&key, copy, uuid);
95
100
 
96
101
    /* Set bits to indicate a random UUID.  See RFC 4122 section 4.4. */
97
102
    uuid->parts[2] &= ~0xc0000000;