2010-05-18 02:18:00 +04:00
|
|
|
/*****************************************************************************\
|
|
|
|
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
|
|
|
|
* Copyright (C) 2007 The Regents of the University of California.
|
|
|
|
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
|
|
|
|
* Written by Brian Behlendorf <behlendorf1@llnl.gov>.
|
2008-05-26 08:38:26 +04:00
|
|
|
* UCRL-CODE-235197
|
|
|
|
*
|
2010-05-18 02:18:00 +04:00
|
|
|
* This file is part of the SPL, Solaris Porting Layer.
|
2013-03-05 05:26:55 +04:00
|
|
|
* For details, see <http://zfsonlinux.org/>.
|
2010-05-18 02:18:00 +04:00
|
|
|
*
|
|
|
|
* The SPL is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
2008-05-26 08:38:26 +04:00
|
|
|
*
|
2010-05-18 02:18:00 +04:00
|
|
|
* The SPL is distributed in the hope that it will be useful, but WITHOUT
|
2008-05-26 08:38:26 +04:00
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2010-05-18 02:18:00 +04:00
|
|
|
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*****************************************************************************
|
|
|
|
* Solaris Porting LAyer Tests (SPLAT) Random Number Generator Tests.
|
|
|
|
\*****************************************************************************/
|
2008-05-26 08:38:26 +04:00
|
|
|
|
2012-11-03 02:29:44 +04:00
|
|
|
#include <sys/random.h>
|
|
|
|
#include <sys/kmem.h>
|
2008-02-28 02:42:31 +03:00
|
|
|
#include "splat-internal.h"
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-02-28 02:42:31 +03:00
|
|
|
#define SPLAT_KRNG_NAME "krng"
|
|
|
|
#define SPLAT_KRNG_DESC "Kernel Random Number Generator Tests"
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-02-28 02:42:31 +03:00
|
|
|
#define SPLAT_KRNG_TEST1_ID 0x0301
|
|
|
|
#define SPLAT_KRNG_TEST1_NAME "freq"
|
|
|
|
#define SPLAT_KRNG_TEST1_DESC "Frequency Test"
|
2008-02-26 23:36:04 +03:00
|
|
|
|
|
|
|
#define KRNG_NUM_BITS 1048576
|
|
|
|
#define KRNG_NUM_BYTES (KRNG_NUM_BITS >> 3)
|
|
|
|
#define KRNG_NUM_BITS_DIV2 (KRNG_NUM_BITS >> 1)
|
|
|
|
#define KRNG_ERROR_RANGE 2097
|
|
|
|
|
|
|
|
/* Random Number Generator Tests
|
|
|
|
There can be meny more tests on quality of the
|
|
|
|
random number generator. For now we are only
|
|
|
|
testing the frequency of particular bits.
|
|
|
|
We could also test consecutive sequences,
|
|
|
|
randomness within a particular block, etc.
|
|
|
|
but is probably not necessary for our purposes */
|
|
|
|
|
|
|
|
static int
|
2008-02-28 02:42:31 +03:00
|
|
|
splat_krng_test1(struct file *file, void *arg)
|
2008-02-26 23:36:04 +03:00
|
|
|
{
|
|
|
|
uint8_t *buf;
|
|
|
|
int i, j, diff, num = 0, rc = 0;
|
|
|
|
|
|
|
|
buf = kmalloc(sizeof(*buf) * KRNG_NUM_BYTES, GFP_KERNEL);
|
|
|
|
if (buf == NULL) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(*buf) * KRNG_NUM_BYTES);
|
|
|
|
|
|
|
|
/* Always succeeds */
|
|
|
|
random_get_pseudo_bytes(buf, sizeof(uint8_t) * KRNG_NUM_BYTES);
|
|
|
|
|
|
|
|
for (i = 0; i < KRNG_NUM_BYTES; i++) {
|
|
|
|
uint8_t tmp = buf[i];
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
uint8_t tmp2 = ((tmp >> j) & 0x01);
|
|
|
|
if (tmp2 == 1) {
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(buf);
|
|
|
|
|
|
|
|
diff = KRNG_NUM_BITS_DIV2 - num;
|
|
|
|
if (diff < 0)
|
|
|
|
diff *= -1;
|
|
|
|
|
2008-02-28 02:42:31 +03:00
|
|
|
splat_print(file, "Test 1 Number of ones: %d\n", num);
|
|
|
|
splat_print(file, "Test 1 Difference from expected: %d Allowed: %d\n",
|
2008-02-26 23:36:04 +03:00
|
|
|
diff, KRNG_ERROR_RANGE);
|
|
|
|
|
|
|
|
if (diff > KRNG_ERROR_RANGE)
|
|
|
|
rc = -ERANGE;
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-02-28 02:42:31 +03:00
|
|
|
splat_subsystem_t *
|
|
|
|
splat_krng_init(void)
|
2008-02-26 23:36:04 +03:00
|
|
|
{
|
2008-02-28 02:42:31 +03:00
|
|
|
splat_subsystem_t *sub;
|
2008-02-26 23:36:04 +03:00
|
|
|
|
|
|
|
sub = kmalloc(sizeof(*sub), GFP_KERNEL);
|
|
|
|
if (sub == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(sub, 0, sizeof(*sub));
|
2008-02-28 02:42:31 +03:00
|
|
|
strncpy(sub->desc.name, SPLAT_KRNG_NAME, SPLAT_NAME_SIZE);
|
|
|
|
strncpy(sub->desc.desc, SPLAT_KRNG_DESC, SPLAT_DESC_SIZE);
|
2008-02-26 23:36:04 +03:00
|
|
|
INIT_LIST_HEAD(&sub->subsystem_list);
|
|
|
|
INIT_LIST_HEAD(&sub->test_list);
|
|
|
|
spin_lock_init(&sub->test_lock);
|
2008-02-28 02:42:31 +03:00
|
|
|
sub->desc.id = SPLAT_SUBSYSTEM_KRNG;
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-02-28 02:42:31 +03:00
|
|
|
SPLAT_TEST_INIT(sub, SPLAT_KRNG_TEST1_NAME, SPLAT_KRNG_TEST1_DESC,
|
|
|
|
SPLAT_KRNG_TEST1_ID, splat_krng_test1);
|
2008-02-26 23:36:04 +03:00
|
|
|
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-02-28 02:42:31 +03:00
|
|
|
splat_krng_fini(splat_subsystem_t *sub)
|
2008-02-26 23:36:04 +03:00
|
|
|
{
|
|
|
|
ASSERT(sub);
|
|
|
|
|
2008-02-28 02:42:31 +03:00
|
|
|
SPLAT_TEST_FINI(sub, SPLAT_KRNG_TEST1_ID);
|
2008-02-26 23:36:04 +03:00
|
|
|
|
|
|
|
kfree(sub);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-02-28 02:42:31 +03:00
|
|
|
splat_krng_id(void) {
|
|
|
|
return SPLAT_SUBSYSTEM_KRNG;
|
2008-02-26 23:36:04 +03:00
|
|
|
}
|