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
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
43 #define NBDKIT_API_VERSION 2
44 #include <nbdkit-plugin.h>
48 /* The size of disk in bytes (initialized by size=<SIZE> parameter). */
49 static int64_t size
= 0;
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
65 random_config (const char *key
, const char *value
)
69 if (strcmp (key
, "seed") == 0) {
70 if (nbdkit_parse_uint32_t ("seed", value
, &seed
) == -1)
73 else if (strcmp (key
, "size") == 0) {
74 r
= nbdkit_parse_size (value
);
80 nbdkit_error ("unknown parameter '%s'", key
);
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. */
95 random_open (int readonly
)
97 return NBDKIT_HANDLE_NOT_NEEDED
;
100 /* Get the disk size. */
102 random_get_size (void *handle
)
107 /* Serves the same data over multiple connections. */
109 random_can_multi_conn (void *handle
)
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
;
126 random_pread (void *handle
, void *buf
, uint32_t count
, uint64_t offset
,
130 unsigned char *b
= buf
;
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]
145 struct random_state state
;
147 xsrandom (seed
+ offset
+ i
, &state
);
150 s
= xrandom (&state
);
157 static struct nbdkit_plugin plugin
= {
159 .version
= PACKAGE_VERSION
,
161 .config
= random_config
,
162 .config_help
= random_config_help
,
163 .magic_config_key
= "size",
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
)