]> git.proxmox.com Git - mirror_spl.git/blame - modules/splat/splat-random.c
Go through and add a header with the proper UCRL number.
[mirror_spl.git] / modules / splat / splat-random.c
CommitLineData
715f6251 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
7c50328b 27#include "splat-internal.h"
f1ca4da6 28
7c50328b 29#define SPLAT_SUBSYSTEM_KRNG 0x0300
30#define SPLAT_KRNG_NAME "krng"
31#define SPLAT_KRNG_DESC "Kernel Random Number Generator Tests"
f1ca4da6 32
7c50328b 33#define SPLAT_KRNG_TEST1_ID 0x0301
34#define SPLAT_KRNG_TEST1_NAME "freq"
35#define SPLAT_KRNG_TEST1_DESC "Frequency Test"
f1ca4da6 36
37#define KRNG_NUM_BITS 1048576
38#define KRNG_NUM_BYTES (KRNG_NUM_BITS >> 3)
39#define KRNG_NUM_BITS_DIV2 (KRNG_NUM_BITS >> 1)
40#define KRNG_ERROR_RANGE 2097
41
42/* Random Number Generator Tests
43 There can be meny more tests on quality of the
44 random number generator. For now we are only
45 testing the frequency of particular bits.
46 We could also test consecutive sequences,
47 randomness within a particular block, etc.
48 but is probably not necessary for our purposes */
49
50static int
7c50328b 51splat_krng_test1(struct file *file, void *arg)
f1ca4da6 52{
53 uint8_t *buf;
54 int i, j, diff, num = 0, rc = 0;
55
56 buf = kmalloc(sizeof(*buf) * KRNG_NUM_BYTES, GFP_KERNEL);
57 if (buf == NULL) {
58 rc = -ENOMEM;
59 goto out;
60 }
61
62 memset(buf, 0, sizeof(*buf) * KRNG_NUM_BYTES);
63
64 /* Always succeeds */
65 random_get_pseudo_bytes(buf, sizeof(uint8_t) * KRNG_NUM_BYTES);
66
67 for (i = 0; i < KRNG_NUM_BYTES; i++) {
68 uint8_t tmp = buf[i];
69 for (j = 0; j < 8; j++) {
70 uint8_t tmp2 = ((tmp >> j) & 0x01);
71 if (tmp2 == 1) {
72 num++;
73 }
74 }
75 }
76
77 kfree(buf);
78
79 diff = KRNG_NUM_BITS_DIV2 - num;
80 if (diff < 0)
81 diff *= -1;
82
7c50328b 83 splat_print(file, "Test 1 Number of ones: %d\n", num);
84 splat_print(file, "Test 1 Difference from expected: %d Allowed: %d\n",
f1ca4da6 85 diff, KRNG_ERROR_RANGE);
86
87 if (diff > KRNG_ERROR_RANGE)
88 rc = -ERANGE;
89out:
90 return rc;
91}
92
7c50328b 93splat_subsystem_t *
94splat_krng_init(void)
f1ca4da6 95{
7c50328b 96 splat_subsystem_t *sub;
f1ca4da6 97
98 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
99 if (sub == NULL)
100 return NULL;
101
102 memset(sub, 0, sizeof(*sub));
7c50328b 103 strncpy(sub->desc.name, SPLAT_KRNG_NAME, SPLAT_NAME_SIZE);
104 strncpy(sub->desc.desc, SPLAT_KRNG_DESC, SPLAT_DESC_SIZE);
f1ca4da6 105 INIT_LIST_HEAD(&sub->subsystem_list);
106 INIT_LIST_HEAD(&sub->test_list);
107 spin_lock_init(&sub->test_lock);
7c50328b 108 sub->desc.id = SPLAT_SUBSYSTEM_KRNG;
f1ca4da6 109
7c50328b 110 SPLAT_TEST_INIT(sub, SPLAT_KRNG_TEST1_NAME, SPLAT_KRNG_TEST1_DESC,
111 SPLAT_KRNG_TEST1_ID, splat_krng_test1);
f1ca4da6 112
113 return sub;
114}
115
116void
7c50328b 117splat_krng_fini(splat_subsystem_t *sub)
f1ca4da6 118{
119 ASSERT(sub);
120
7c50328b 121 SPLAT_TEST_FINI(sub, SPLAT_KRNG_TEST1_ID);
f1ca4da6 122
123 kfree(sub);
124}
125
126int
7c50328b 127splat_krng_id(void) {
128 return SPLAT_SUBSYSTEM_KRNG;
f1ca4da6 129}