multipath_drr.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Device round robin policy for multipath.
  3. *
  4. *
  5. * Version: $Id: multipath_drr.c,v 1.1.2.1 2004/09/16 07:42:34 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/sched.h>
  18. #include <linux/errno.h>
  19. #include <linux/timer.h>
  20. #include <linux/mm.h>
  21. #include <linux/kernel.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/stat.h>
  24. #include <linux/socket.h>
  25. #include <linux/in.h>
  26. #include <linux/inet.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/inetdevice.h>
  29. #include <linux/igmp.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/module.h>
  33. #include <linux/mroute.h>
  34. #include <linux/init.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. struct multipath_device {
  49. int ifi; /* interface index of device */
  50. atomic_t usecount;
  51. int allocated;
  52. };
  53. #define MULTIPATH_MAX_DEVICECANDIDATES 10
  54. static struct multipath_device state[MULTIPATH_MAX_DEVICECANDIDATES];
  55. static DEFINE_SPINLOCK(state_lock);
  56. static int inline __multipath_findslot(void)
  57. {
  58. int i;
  59. for (i = 0; i < MULTIPATH_MAX_DEVICECANDIDATES; i++) {
  60. if (state[i].allocated == 0)
  61. return i;
  62. }
  63. return -1;
  64. }
  65. static int inline __multipath_finddev(int ifindex)
  66. {
  67. int i;
  68. for (i = 0; i < MULTIPATH_MAX_DEVICECANDIDATES; i++) {
  69. if (state[i].allocated != 0 &&
  70. state[i].ifi == ifindex)
  71. return i;
  72. }
  73. return -1;
  74. }
  75. static int drr_dev_event(struct notifier_block *this,
  76. unsigned long event, void *ptr)
  77. {
  78. struct net_device *dev = ptr;
  79. int devidx;
  80. switch (event) {
  81. case NETDEV_UNREGISTER:
  82. case NETDEV_DOWN:
  83. spin_lock_bh(&state_lock);
  84. devidx = __multipath_finddev(dev->ifindex);
  85. if (devidx != -1) {
  86. state[devidx].allocated = 0;
  87. state[devidx].ifi = 0;
  88. atomic_set(&state[devidx].usecount, 0);
  89. }
  90. spin_unlock_bh(&state_lock);
  91. break;
  92. };
  93. return NOTIFY_DONE;
  94. }
  95. static struct notifier_block drr_dev_notifier = {
  96. .notifier_call = drr_dev_event,
  97. };
  98. static void drr_safe_inc(atomic_t *usecount)
  99. {
  100. int n;
  101. atomic_inc(usecount);
  102. n = atomic_read(usecount);
  103. if (n <= 0) {
  104. int i;
  105. spin_lock_bh(&state_lock);
  106. for (i = 0; i < MULTIPATH_MAX_DEVICECANDIDATES; i++)
  107. atomic_set(&state[i].usecount, 0);
  108. spin_unlock_bh(&state_lock);
  109. }
  110. }
  111. static void drr_select_route(const struct flowi *flp,
  112. struct rtable *first, struct rtable **rp)
  113. {
  114. struct rtable *nh, *result, *cur_min;
  115. int min_usecount = -1;
  116. int devidx = -1;
  117. int cur_min_devidx = -1;
  118. /* 1. make sure all alt. nexthops have the same GC related data */
  119. /* 2. determine the new candidate to be returned */
  120. result = NULL;
  121. cur_min = NULL;
  122. for (nh = rcu_dereference(first); nh;
  123. nh = rcu_dereference(nh->u.rt_next)) {
  124. if ((nh->u.dst.flags & DST_BALANCED) != 0 &&
  125. multipath_comparekeys(&nh->fl, flp)) {
  126. int nh_ifidx = nh->u.dst.dev->ifindex;
  127. nh->u.dst.lastuse = jiffies;
  128. nh->u.dst.__use++;
  129. if (result != NULL)
  130. continue;
  131. /* search for the output interface */
  132. /* this is not SMP safe, only add/remove are
  133. * SMP safe as wrong usecount updates have no big
  134. * impact
  135. */
  136. devidx = __multipath_finddev(nh_ifidx);
  137. if (devidx == -1) {
  138. /* add the interface to the array
  139. * SMP safe
  140. */
  141. spin_lock_bh(&state_lock);
  142. /* due to SMP: search again */
  143. devidx = __multipath_finddev(nh_ifidx);
  144. if (devidx == -1) {
  145. /* add entry for device */
  146. devidx = __multipath_findslot();
  147. if (devidx == -1) {
  148. /* unlikely but possible */
  149. continue;
  150. }
  151. state[devidx].allocated = 1;
  152. state[devidx].ifi = nh_ifidx;
  153. atomic_set(&state[devidx].usecount, 0);
  154. min_usecount = 0;
  155. }
  156. spin_unlock_bh(&state_lock);
  157. }
  158. if (min_usecount == 0) {
  159. /* if the device has not been used it is
  160. * the primary target
  161. */
  162. drr_safe_inc(&state[devidx].usecount);
  163. result = nh;
  164. } else {
  165. int count =
  166. atomic_read(&state[devidx].usecount);
  167. if (min_usecount == -1 ||
  168. count < min_usecount) {
  169. cur_min = nh;
  170. cur_min_devidx = devidx;
  171. min_usecount = count;
  172. }
  173. }
  174. }
  175. }
  176. if (!result) {
  177. if (cur_min) {
  178. drr_safe_inc(&state[cur_min_devidx].usecount);
  179. result = cur_min;
  180. } else {
  181. result = first;
  182. }
  183. }
  184. *rp = result;
  185. }
  186. static struct ip_mp_alg_ops drr_ops = {
  187. .mp_alg_select_route = drr_select_route,
  188. };
  189. static int __init drr_init(void)
  190. {
  191. int err = register_netdevice_notifier(&drr_dev_notifier);
  192. if (err)
  193. return err;
  194. err = multipath_alg_register(&drr_ops, IP_MP_ALG_DRR);
  195. if (err)
  196. goto fail;
  197. return 0;
  198. fail:
  199. unregister_netdevice_notifier(&drr_dev_notifier);
  200. return err;
  201. }
  202. static void __exit drr_exit(void)
  203. {
  204. unregister_netdevice_notifier(&drr_dev_notifier);
  205. multipath_alg_unregister(&drr_ops, IP_MP_ALG_DRR);
  206. }
  207. module_init(drr_init);
  208. module_exit(drr_exit);
  209. MODULE_LICENSE("GPL");