]> git.proxmox.com Git - mirror_ovs.git/blame - lib/random.c
ovsdb-idl: Fix memleak when reinserting tracked orphan rows.
[mirror_ovs.git] / lib / random.c
CommitLineData
064af421 1/*
61d71ceb 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
064af421 3 *
a14bc59f
BP
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
064af421 7 *
a14bc59f
BP
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
064af421
BP
15 */
16
17#include <config.h>
18#include "random.h"
19
20#include <errno.h>
21#include <stdlib.h>
22#include <sys/time.h>
23
10df718e 24#include "entropy.h"
61d71ceb
BP
25#include "hash.h"
26#include "ovs-thread.h"
279c9e03 27#include "timeval.h"
064af421
BP
28#include "util.h"
29
e519ea28
BP
30/* This is the 32-bit PRNG recommended in G. Marsaglia, "Xorshift RNGs",
31 * _Journal of Statistical Software_ 8:14 (July 2003). According to the paper,
32 * it has a period of 2**32 - 1 and passes almost all tests of randomness.
33 *
34 * We use this PRNG instead of libc's rand() because rand() varies in quality
35 * and because its maximum value also varies between 32767 and INT_MAX, whereas
10df718e
BP
36 * we often want random numbers in the full range of uint32_t.
37 *
38 * This random number generator is intended for purposes that do not require
39 * cryptographic-quality randomness. */
e519ea28
BP
40
41/* Current random state. */
2ba4f163 42DEFINE_STATIC_PER_THREAD_DATA(uint32_t, seed, 0);
e519ea28
BP
43
44static uint32_t random_next(void);
45
064af421
BP
46void
47random_init(void)
48{
61d71ceb
BP
49 uint32_t *seedp = seed_get();
50 while (!*seedp) {
064af421 51 struct timeval tv;
10df718e 52 uint32_t entropy;
61d71ceb 53 pthread_t self;
e519ea28 54
279c9e03 55 xgettimeofday(&tv);
10df718e 56 get_entropy_or_die(&entropy, 4);
61d71ceb 57 self = pthread_self();
e519ea28 58
61d71ceb
BP
59 *seedp = (tv.tv_sec ^ tv.tv_usec ^ entropy
60 ^ hash_bytes(&self, sizeof self, 0));
064af421
BP
61 }
62}
63
78f3f316
BP
64void
65random_set_seed(uint32_t seed_)
66{
cb22974d 67 ovs_assert(seed_);
61d71ceb 68 *seed_get() = seed_;
78f3f316
BP
69}
70
064af421
BP
71void
72random_bytes(void *p_, size_t n)
73{
74 uint8_t *p = p_;
e519ea28 75
064af421 76 random_init();
e519ea28
BP
77
78 for (; n > 4; p += 4, n -= 4) {
79 uint32_t x = random_next();
80 memcpy(p, &x, 4);
81 }
82
83 if (n) {
84 uint32_t x = random_next();
85 memcpy(p, &x, n);
064af421
BP
86 }
87}
88
064af421
BP
89
90uint32_t
91random_uint32(void)
92{
e519ea28
BP
93 random_init();
94 return random_next();
064af421
BP
95}
96
ddc4f8e2
BP
97uint64_t
98random_uint64(void)
99{
100 uint64_t x;
101
102 random_init();
103
104 x = random_next();
105 x |= (uint64_t) random_next() << 32;
106 return x;
107}
108
e519ea28
BP
109static uint32_t
110random_next(void)
111{
61d71ceb 112 uint32_t *seedp = seed_get_unsafe();
e519ea28 113
61d71ceb
BP
114 *seedp ^= *seedp << 13;
115 *seedp ^= *seedp >> 17;
116 *seedp ^= *seedp << 5;
117
118 return *seedp;
e519ea28 119}