plugins: Wire up nbd plugin support for NBD_INFO_INIT_STATE
[nbdkit/ericb.git] / plugins / random / random.c
blobcdebdf9613db8eacf010aa9db48f89444d87389a
1 /* nbdkit
2 * Copyright (C) 2017-2019 Red Hat Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <time.h>
43 #define NBDKIT_API_VERSION 2
44 #include <nbdkit-plugin.h>
46 #include "random.h"
48 /* The size of disk in bytes (initialized by size=<SIZE> parameter). */
49 static int64_t size = 0;
51 /* Seed. */
52 static uint32_t seed;
54 static void
55 random_load (void)
57 /* Set the seed to a random-ish value. This is not meant to be
58 * cryptographically useful. It can be overridden using the seed
59 * parameter.
61 seed = time (NULL);
64 static int
65 random_config (const char *key, const char *value)
67 int64_t r;
69 if (strcmp (key, "seed") == 0) {
70 if (nbdkit_parse_uint32_t ("seed", value, &seed) == -1)
71 return -1;
73 else if (strcmp (key, "size") == 0) {
74 r = nbdkit_parse_size (value);
75 if (r == -1)
76 return -1;
77 size = r;
79 else {
80 nbdkit_error ("unknown parameter '%s'", key);
81 return -1;
84 return 0;
87 #define random_config_help \
88 "size=<SIZE> (required) Size of the backing disk\n" \
89 "seed=<SEED> Random number generator seed"
91 #define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL
93 /* Create the per-connection handle. */
94 static void *
95 random_open (int readonly)
97 return NBDKIT_HANDLE_NOT_NEEDED;
100 /* Get the disk size. */
101 static int64_t
102 random_get_size (void *handle)
104 return size;
107 /* Serves the same data over multiple connections. */
108 static int
109 random_can_multi_conn (void *handle)
111 return 1;
114 /* Cache. */
115 static int
116 random_can_cache (void *handle)
118 /* Everything is already in memory, returning this without
119 * implementing .cache lets nbdkit do the correct no-op.
121 return NBDKIT_CACHE_NATIVE;
124 /* Read data. */
125 static int
126 random_pread (void *handle, void *buf, uint32_t count, uint64_t offset,
127 uint32_t flags)
129 uint32_t i;
130 unsigned char *b = buf;
131 uint64_t s;
133 for (i = 0; i < count; ++i) {
134 /* We use nbdkit common/include/random.h to make random numbers.
136 * However we're not quite using it in the ordinary way. In order
137 * to be able to read any byte of data without needing to run the
138 * PRNG from the start, the random data is computed from the index
139 * and seed through three rounds of PRNG:
141 * index i PRNG(seed+i) -> PRNG -> PRNG -> mod 256 -> b[i]
142 * index i+1 PRNG(seed+i+1) -> PRNG -> PRNG -> mod 256 -> b[i+1]
143 * etc
145 struct random_state state;
147 xsrandom (seed + offset + i, &state);
148 xrandom (&state);
149 xrandom (&state);
150 s = xrandom (&state);
151 s &= 255;
152 b[i] = s;
154 return 0;
157 static struct nbdkit_plugin plugin = {
158 .name = "random",
159 .version = PACKAGE_VERSION,
160 .load = random_load,
161 .config = random_config,
162 .config_help = random_config_help,
163 .magic_config_key = "size",
164 .open = random_open,
165 .get_size = random_get_size,
166 .can_multi_conn = random_can_multi_conn,
167 .can_cache = random_can_cache,
168 .pread = random_pread,
169 /* In this plugin, errno is preserved properly along error return
170 * paths from failed system calls.
172 .errno_is_preserved = 1,
175 NBDKIT_REGISTER_PLUGIN(plugin)