multipath_random.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <asm/system.h>
  15. #include <asm/uaccess.h>
  16. #include <linux/types.h>
  17. #include <linux/errno.h>
  18. #include <linux/timer.h>
  19. #include <linux/mm.h>
  20. #include <linux/kernel.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/stat.h>
  23. #include <linux/socket.h>
  24. #include <linux/in.h>
  25. #include <linux/inet.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/inetdevice.h>
  28. #include <linux/igmp.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/module.h>
  32. #include <linux/mroute.h>
  33. #include <linux/init.h>
  34. #include <linux/random.h>
  35. #include <net/ip.h>
  36. #include <net/protocol.h>
  37. #include <linux/skbuff.h>
  38. #include <net/sock.h>
  39. #include <net/icmp.h>
  40. #include <net/udp.h>
  41. #include <net/raw.h>
  42. #include <linux/notifier.h>
  43. #include <linux/if_arp.h>
  44. #include <linux/netfilter_ipv4.h>
  45. #include <net/ipip.h>
  46. #include <net/checksum.h>
  47. #include <net/ip_mp_alg.h>
  48. #define MULTIPATH_MAX_CANDIDATES 40
  49. static void random_select_route(const struct flowi *flp,
  50. struct rtable *first,
  51. struct rtable **rp)
  52. {
  53. struct rtable *rt;
  54. struct rtable *decision;
  55. unsigned char candidate_count = 0;
  56. /* count all candidate */
  57. for (rt = rcu_dereference(first); rt;
  58. rt = rcu_dereference(rt->u.dst.rt_next)) {
  59. if ((rt->u.dst.flags & DST_BALANCED) != 0 &&
  60. multipath_comparekeys(&rt->fl, flp))
  61. ++candidate_count;
  62. }
  63. /* choose a random candidate */
  64. decision = first;
  65. if (candidate_count > 1) {
  66. unsigned char i = 0;
  67. unsigned char candidate_no = (unsigned char)
  68. (random32() % candidate_count);
  69. /* find chosen candidate and adjust GC data for all candidates
  70. * to ensure they stay in cache
  71. */
  72. for (rt = first; rt; rt = rt->u.dst.rt_next) {
  73. if ((rt->u.dst.flags & DST_BALANCED) != 0 &&
  74. multipath_comparekeys(&rt->fl, flp)) {
  75. rt->u.dst.lastuse = jiffies;
  76. if (i == candidate_no)
  77. decision = rt;
  78. if (i >= candidate_count)
  79. break;
  80. i++;
  81. }
  82. }
  83. }
  84. decision->u.dst.__use++;
  85. *rp = decision;
  86. }
  87. static struct ip_mp_alg_ops random_ops = {
  88. .mp_alg_select_route = random_select_route,
  89. };
  90. static int __init random_init(void)
  91. {
  92. return multipath_alg_register(&random_ops, IP_MP_ALG_RANDOM);
  93. }
  94. static void __exit random_exit(void)
  95. {
  96. multipath_alg_unregister(&random_ops, IP_MP_ALG_RANDOM);
  97. }
  98. module_init(random_init);
  99. module_exit(random_exit);
  100. MODULE_LICENSE("GPL");