multipath_drr.c 5.3 KB

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