multipath_drr.c 5.3 KB

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