]> git.proxmox.com Git - mirror_spl.git/blob - module/splat/splat-random.c
ed8f694c30c7d6b20323d91c82b4961de1f4734b
[mirror_spl.git] / module / splat / splat-random.c
1 /*
2 * This file is part of the SPL: Solaris Porting Layer.
3 *
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
6 * Written by:
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
10 * UCRL-CODE-235197
11 *
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
27 #include "splat-internal.h"
28
29 #define SPLAT_KRNG_NAME "krng"
30 #define SPLAT_KRNG_DESC "Kernel Random Number Generator Tests"
31
32 #define SPLAT_KRNG_TEST1_ID 0x0301
33 #define SPLAT_KRNG_TEST1_NAME "freq"
34 #define SPLAT_KRNG_TEST1_DESC "Frequency Test"
35
36 #define KRNG_NUM_BITS 1048576
37 #define KRNG_NUM_BYTES (KRNG_NUM_BITS >> 3)
38 #define KRNG_NUM_BITS_DIV2 (KRNG_NUM_BITS >> 1)
39 #define KRNG_ERROR_RANGE 2097
40
41 /* Random Number Generator Tests
42 There can be meny more tests on quality of the
43 random number generator. For now we are only
44 testing the frequency of particular bits.
45 We could also test consecutive sequences,
46 randomness within a particular block, etc.
47 but is probably not necessary for our purposes */
48
49 static int
50 splat_krng_test1(struct file *file, void *arg)
51 {
52 uint8_t *buf;
53 int i, j, diff, num = 0, rc = 0;
54
55 buf = kmalloc(sizeof(*buf) * KRNG_NUM_BYTES, GFP_KERNEL);
56 if (buf == NULL) {
57 rc = -ENOMEM;
58 goto out;
59 }
60
61 memset(buf, 0, sizeof(*buf) * KRNG_NUM_BYTES);
62
63 /* Always succeeds */
64 random_get_pseudo_bytes(buf, sizeof(uint8_t) * KRNG_NUM_BYTES);
65
66 for (i = 0; i < KRNG_NUM_BYTES; i++) {
67 uint8_t tmp = buf[i];
68 for (j = 0; j < 8; j++) {
69 uint8_t tmp2 = ((tmp >> j) & 0x01);
70 if (tmp2 == 1) {
71 num++;
72 }
73 }
74 }
75
76 kfree(buf);
77
78 diff = KRNG_NUM_BITS_DIV2 - num;
79 if (diff < 0)
80 diff *= -1;
81
82 splat_print(file, "Test 1 Number of ones: %d\n", num);
83 splat_print(file, "Test 1 Difference from expected: %d Allowed: %d\n",
84 diff, KRNG_ERROR_RANGE);
85
86 if (diff > KRNG_ERROR_RANGE)
87 rc = -ERANGE;
88 out:
89 return rc;
90 }
91
92 splat_subsystem_t *
93 splat_krng_init(void)
94 {
95 splat_subsystem_t *sub;
96
97 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
98 if (sub == NULL)
99 return NULL;
100
101 memset(sub, 0, sizeof(*sub));
102 strncpy(sub->desc.name, SPLAT_KRNG_NAME, SPLAT_NAME_SIZE);
103 strncpy(sub->desc.desc, SPLAT_KRNG_DESC, SPLAT_DESC_SIZE);
104 INIT_LIST_HEAD(&sub->subsystem_list);
105 INIT_LIST_HEAD(&sub->test_list);
106 spin_lock_init(&sub->test_lock);
107 sub->desc.id = SPLAT_SUBSYSTEM_KRNG;
108
109 SPLAT_TEST_INIT(sub, SPLAT_KRNG_TEST1_NAME, SPLAT_KRNG_TEST1_DESC,
110 SPLAT_KRNG_TEST1_ID, splat_krng_test1);
111
112 return sub;
113 }
114
115 void
116 splat_krng_fini(splat_subsystem_t *sub)
117 {
118 ASSERT(sub);
119
120 SPLAT_TEST_FINI(sub, SPLAT_KRNG_TEST1_ID);
121
122 kfree(sub);
123 }
124
125 int
126 splat_krng_id(void) {
127 return SPLAT_SUBSYSTEM_KRNG;
128 }