]> git.proxmox.com Git - mirror_spl.git/blob - src/splat/splat-random.c
Initial commit. All spl source written up to this point wrapped
[mirror_spl.git] / src / splat / splat-random.c
1 #include <sys/zfs_context.h>
2 #include <sys/splat-ctl.h>
3
4 #define KZT_SUBSYSTEM_KRNG 0x0300
5 #define KZT_KRNG_NAME "krng"
6 #define KZT_KRNG_DESC "Kernel Random Number Generator Tests"
7
8 #define KZT_KRNG_TEST1_ID 0x0301
9 #define KZT_KRNG_TEST1_NAME "freq"
10 #define KZT_KRNG_TEST1_DESC "Frequency Test"
11
12 #define KRNG_NUM_BITS 1048576
13 #define KRNG_NUM_BYTES (KRNG_NUM_BITS >> 3)
14 #define KRNG_NUM_BITS_DIV2 (KRNG_NUM_BITS >> 1)
15 #define KRNG_ERROR_RANGE 2097
16
17 /* Random Number Generator Tests
18 There can be meny more tests on quality of the
19 random number generator. For now we are only
20 testing the frequency of particular bits.
21 We could also test consecutive sequences,
22 randomness within a particular block, etc.
23 but is probably not necessary for our purposes */
24
25 static int
26 kzt_krng_test1(struct file *file, void *arg)
27 {
28 uint8_t *buf;
29 int i, j, diff, num = 0, rc = 0;
30
31 buf = kmalloc(sizeof(*buf) * KRNG_NUM_BYTES, GFP_KERNEL);
32 if (buf == NULL) {
33 rc = -ENOMEM;
34 goto out;
35 }
36
37 memset(buf, 0, sizeof(*buf) * KRNG_NUM_BYTES);
38
39 /* Always succeeds */
40 random_get_pseudo_bytes(buf, sizeof(uint8_t) * KRNG_NUM_BYTES);
41
42 for (i = 0; i < KRNG_NUM_BYTES; i++) {
43 uint8_t tmp = buf[i];
44 for (j = 0; j < 8; j++) {
45 uint8_t tmp2 = ((tmp >> j) & 0x01);
46 if (tmp2 == 1) {
47 num++;
48 }
49 }
50 }
51
52 kfree(buf);
53
54 diff = KRNG_NUM_BITS_DIV2 - num;
55 if (diff < 0)
56 diff *= -1;
57
58 kzt_print(file, "Test 1 Number of ones: %d\n", num);
59 kzt_print(file, "Test 1 Difference from expected: %d Allowed: %d\n",
60 diff, KRNG_ERROR_RANGE);
61
62 if (diff > KRNG_ERROR_RANGE)
63 rc = -ERANGE;
64 out:
65 return rc;
66 }
67
68 kzt_subsystem_t *
69 kzt_krng_init(void)
70 {
71 kzt_subsystem_t *sub;
72
73 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
74 if (sub == NULL)
75 return NULL;
76
77 memset(sub, 0, sizeof(*sub));
78 strncpy(sub->desc.name, KZT_KRNG_NAME, KZT_NAME_SIZE);
79 strncpy(sub->desc.desc, KZT_KRNG_DESC, KZT_DESC_SIZE);
80 INIT_LIST_HEAD(&sub->subsystem_list);
81 INIT_LIST_HEAD(&sub->test_list);
82 spin_lock_init(&sub->test_lock);
83 sub->desc.id = KZT_SUBSYSTEM_KRNG;
84
85 KZT_TEST_INIT(sub, KZT_KRNG_TEST1_NAME, KZT_KRNG_TEST1_DESC,
86 KZT_KRNG_TEST1_ID, kzt_krng_test1);
87
88 return sub;
89 }
90
91 void
92 kzt_krng_fini(kzt_subsystem_t *sub)
93 {
94 ASSERT(sub);
95
96 KZT_TEST_FINI(sub, KZT_KRNG_TEST1_ID);
97
98 kfree(sub);
99 }
100
101 int
102 kzt_krng_id(void) {
103 return KZT_SUBSYSTEM_KRNG;
104 }