multipath_random.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Random policy for multipath.
  3. *
  4. *
  5. * Version: $Id: multipath_random.c,v 1.1.2.3 2004/09/21 08:42:11 elueck Exp $
  6. *
  7. * Authors: Einar Lueck <elueck@de.ibm.com><lkml@einar-lueck.de>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/config.h>
  15. #include <asm/system.h>
  16. #include <asm/uaccess.h>
  17. #include <linux/types.h>
  18. #include <linux/sched.h>
  19. #include <linux/errno.h>
  20. #include <linux/timer.h>
  21. #include <linux/mm.h>
  22. #include <linux/kernel.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/stat.h>
  25. #include <linux/socket.h>
  26. #include <linux/in.h>
  27. #include <linux/inet.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/inetdevice.h>
  30. #include <linux/igmp.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/module.h>
  34. #include <linux/mroute.h>
  35. #include <linux/init.h>
  36. #include <net/ip.h>
  37. #include <net/protocol.h>
  38. #include <linux/skbuff.h>
  39. #include <net/sock.h>
  40. #include <net/icmp.h>
  41. #include <net/udp.h>
  42. #include <net/raw.h>
  43. #include <linux/notifier.h>
  44. #include <linux/if_arp.h>
  45. #include <linux/netfilter_ipv4.h>
  46. #include <net/ipip.h>
  47. #include <net/checksum.h>
  48. #include <net/ip_mp_alg.h>
  49. #define MULTIPATH_MAX_CANDIDATES 40
  50. /* interface to random number generation */
  51. static unsigned int RANDOM_SEED = 93186752;
  52. static inline unsigned int random(unsigned int ubound)
  53. {
  54. static unsigned int a = 1588635695,
  55. q = 2,
  56. r = 1117695901;
  57. RANDOM_SEED = a*(RANDOM_SEED % q) - r*(RANDOM_SEED / q);
  58. return RANDOM_SEED % ubound;
  59. }
  60. static void random_select_route(const struct flowi *flp,
  61. struct rtable *first,
  62. struct rtable **rp)
  63. {
  64. struct rtable *rt;
  65. struct rtable *decision;
  66. unsigned char candidate_count = 0;
  67. /* count all candidate */
  68. for (rt = rcu_dereference(first); rt;
  69. rt = rcu_dereference(rt->u.rt_next)) {
  70. if ((rt->u.dst.flags & DST_BALANCED) != 0 &&
  71. multipath_comparekeys(&rt->fl, flp))
  72. ++candidate_count;
  73. }
  74. /* choose a random candidate */
  75. decision = first;
  76. if (candidate_count > 1) {
  77. unsigned char i = 0;
  78. unsigned char candidate_no = (unsigned char)
  79. random(candidate_count);
  80. /* find chosen candidate and adjust GC data for all candidates
  81. * to ensure they stay in cache
  82. */
  83. for (rt = first; rt; rt = rt->u.rt_next) {
  84. if ((rt->u.dst.flags & DST_BALANCED) != 0 &&
  85. multipath_comparekeys(&rt->fl, flp)) {
  86. rt->u.dst.lastuse = jiffies;
  87. if (i == candidate_no)
  88. decision = rt;
  89. if (i >= candidate_count)
  90. break;
  91. i++;
  92. }
  93. }
  94. }
  95. decision->u.dst.__use++;
  96. *rp = decision;
  97. }
  98. static struct ip_mp_alg_ops random_ops = {
  99. .mp_alg_select_route = random_select_route,
  100. };
  101. static int __init random_init(void)
  102. {
  103. return multipath_alg_register(&random_ops, IP_MP_ALG_RANDOM);
  104. }
  105. static void __exit random_exit(void)
  106. {
  107. multipath_alg_unregister(&random_ops, IP_MP_ALG_RANDOM);
  108. }
  109. module_init(random_init);
  110. module_exit(random_exit);
  111. MODULE_LICENSE("GPL");