From: Daniel Borkmann <dborkman@redhat.com>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
"Theodore Ts'o" <tytso@mit.edu>, Joe Perches <joe@perches.com>
Subject: [PATCH net-next v2 1/2] random: add prandom_u32_range and prandom_u32_max helpers
Date: Wed, 4 Sep 2013 14:37:26 +0200 [thread overview]
Message-ID: <1378298247-29364-2-git-send-email-dborkman@redhat.com> (raw)
In-Reply-To: <1378298247-29364-1-git-send-email-dborkman@redhat.com>
We have implemented the same function over and over, so introduce
generic helpers that unify these implementations in order to migrate
such code to use them. Make the API similarly to randomize_range()
for consistency. prandom_u32_range() generates numbers in [start, end]
interval and prandom_u32_max() generates numbers in [0, end] interval.
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Joe Perches <joe@perches.com>
Cc: linux-kernel@vger.kernel.org
---
include/linux/random.h | 31 ++++++++++++++++++++++++++++++-
1 file changed, 30 insertions(+), 1 deletion(-)
diff --git a/include/linux/random.h b/include/linux/random.h
index 3b9377d..17c91c2 100644
--- a/include/linux/random.h
+++ b/include/linux/random.h
@@ -8,7 +8,6 @@
#include <uapi/linux/random.h>
-
extern void add_device_randomness(const void *, unsigned int);
extern void add_input_randomness(unsigned int type, unsigned int code,
unsigned int value);
@@ -32,6 +31,36 @@ void prandom_seed(u32 seed);
u32 prandom_u32_state(struct rnd_state *);
void prandom_bytes_state(struct rnd_state *state, void *buf, int nbytes);
+/**
+ * prandom_u32_range - return a random number in interval [start, end]
+ * @start: lower interval endpoint
+ * @end: higher interval endpoint
+ *
+ * Returns a number that is in the given interval:
+ *
+ * [...... <range> .....]
+ * start end
+ *
+ * Callers need to make sure that start <= end. Note that the result
+ * depends on PRNG being well distributed in [0, ~0U] space. Here we
+ * use maximally equidistributed combined Tausworthe generator.
+ */
+static inline u32 prandom_u32_range(u32 start, u32 end)
+{
+ return (u32)(((u64) prandom_u32() * (end + 1 - start)) >> 32) + start;
+}
+
+/**
+ * prandom_u32_max - return a random number in interval [0, max]
+ * @max: higher interval endpoint
+ *
+ * Returns a number that is in interval [0, end].
+ */
+static inline u32 prandom_u32_max(u32 end)
+{
+ return prandom_u32_range(0, end);
+}
+
/*
* Handle minimum values for seeds
*/
--
1.7.11.7
next prev parent reply other threads:[~2013-09-04 12:37 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-09-04 12:37 [PATCH net-next v2 0/2] prandom_u32_range, " Daniel Borkmann
2013-09-04 12:37 ` Daniel Borkmann [this message]
2013-09-04 15:54 ` [PATCH net-next v2 1/2] random: add prandom_u32_range and " Joe Perches
2013-09-04 12:37 ` [PATCH net-next v2 2/2] net: migrate direct users to prandom_u32_max Daniel Borkmann
2013-09-04 12:52 ` Eric Dumazet
2013-09-04 12:58 ` Daniel Borkmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1378298247-29364-2-git-send-email-dborkman@redhat.com \
--to=dborkman@redhat.com \
--cc=davem@davemloft.net \
--cc=joe@perches.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=tytso@mit.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome