utils.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Generic address resultion entity
  3. *
  4. * Authors:
  5. * net_random Alan Cox
  6. * net_ratelimit Andy Kleen
  7. *
  8. * Created by Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/string.h>
  20. #include <linux/types.h>
  21. #include <linux/random.h>
  22. #include <linux/percpu.h>
  23. #include <linux/init.h>
  24. #include <asm/system.h>
  25. #include <asm/uaccess.h>
  26. /*
  27. This is a maximally equidistributed combined Tausworthe generator
  28. based on code from GNU Scientific Library 1.5 (30 Jun 2004)
  29. x_n = (s1_n ^ s2_n ^ s3_n)
  30. s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
  31. s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
  32. s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
  33. The period of this generator is about 2^88.
  34. From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
  35. Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
  36. This is available on the net from L'Ecuyer's home page,
  37. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
  38. ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
  39. There is an erratum in the paper "Tables of Maximally
  40. Equidistributed Combined LFSR Generators", Mathematics of
  41. Computation, 68, 225 (1999), 261--269:
  42. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
  43. ... the k_j most significant bits of z_j must be non-
  44. zero, for each j. (Note: this restriction also applies to the
  45. computer code given in [4], but was mistakenly not mentioned in
  46. that paper.)
  47. This affects the seeding procedure by imposing the requirement
  48. s1 > 1, s2 > 7, s3 > 15.
  49. */
  50. struct nrnd_state {
  51. u32 s1, s2, s3;
  52. };
  53. static DEFINE_PER_CPU(struct nrnd_state, net_rand_state);
  54. static u32 __net_random(struct nrnd_state *state)
  55. {
  56. #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
  57. state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
  58. state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
  59. state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
  60. return (state->s1 ^ state->s2 ^ state->s3);
  61. }
  62. static void __net_srandom(struct nrnd_state *state, unsigned long s)
  63. {
  64. if (s == 0)
  65. s = 1; /* default seed is 1 */
  66. #define LCG(n) (69069 * n)
  67. state->s1 = LCG(s);
  68. state->s2 = LCG(state->s1);
  69. state->s3 = LCG(state->s2);
  70. /* "warm it up" */
  71. __net_random(state);
  72. __net_random(state);
  73. __net_random(state);
  74. __net_random(state);
  75. __net_random(state);
  76. __net_random(state);
  77. }
  78. unsigned long net_random(void)
  79. {
  80. unsigned long r;
  81. struct nrnd_state *state = &get_cpu_var(net_rand_state);
  82. r = __net_random(state);
  83. put_cpu_var(state);
  84. return r;
  85. }
  86. void net_srandom(unsigned long entropy)
  87. {
  88. struct nrnd_state *state = &get_cpu_var(net_rand_state);
  89. __net_srandom(state, state->s1^entropy);
  90. put_cpu_var(state);
  91. }
  92. void __init net_random_init(void)
  93. {
  94. int i;
  95. for (i = 0; i < NR_CPUS; i++) {
  96. struct nrnd_state *state = &per_cpu(net_rand_state,i);
  97. __net_srandom(state, i+jiffies);
  98. }
  99. }
  100. static int net_random_reseed(void)
  101. {
  102. int i;
  103. unsigned long seed[NR_CPUS];
  104. get_random_bytes(seed, sizeof(seed));
  105. for (i = 0; i < NR_CPUS; i++) {
  106. struct nrnd_state *state = &per_cpu(net_rand_state,i);
  107. __net_srandom(state, seed[i]);
  108. }
  109. return 0;
  110. }
  111. late_initcall(net_random_reseed);
  112. int net_msg_cost = 5*HZ;
  113. int net_msg_burst = 10;
  114. /*
  115. * All net warning printk()s should be guarded by this function.
  116. */
  117. int net_ratelimit(void)
  118. {
  119. return __printk_ratelimit(net_msg_cost, net_msg_burst);
  120. }
  121. EXPORT_SYMBOL(net_random);
  122. EXPORT_SYMBOL(net_ratelimit);
  123. EXPORT_SYMBOL(net_srandom);