multipath_drr.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/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 struct rtable *last_selection = NULL;
  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. struct notifier_block drr_dev_notifier = {
  97. .notifier_call = drr_dev_event,
  98. };
  99. static void drr_remove(struct rtable *rt)
  100. {
  101. if (last_selection == rt)
  102. last_selection = NULL;
  103. }
  104. static void drr_safe_inc(atomic_t *usecount)
  105. {
  106. int n;
  107. atomic_inc(usecount);
  108. n = atomic_read(usecount);
  109. if (n <= 0) {
  110. int i;
  111. spin_lock_bh(&state_lock);
  112. for (i = 0; i < MULTIPATH_MAX_DEVICECANDIDATES; i++)
  113. atomic_set(&state[i].usecount, 0);
  114. spin_unlock_bh(&state_lock);
  115. }
  116. }
  117. static void drr_select_route(const struct flowi *flp,
  118. struct rtable *first, struct rtable **rp)
  119. {
  120. struct rtable *nh, *result, *cur_min;
  121. int min_usecount = -1;
  122. int devidx = -1;
  123. int cur_min_devidx = -1;
  124. /* if necessary and possible utilize the old alternative */
  125. if ((flp->flags & FLOWI_FLAG_MULTIPATHOLDROUTE) != 0 &&
  126. last_selection != NULL) {
  127. result = last_selection;
  128. *rp = result;
  129. return;
  130. }
  131. /* 1. make sure all alt. nexthops have the same GC related data */
  132. /* 2. determine the new candidate to be returned */
  133. result = NULL;
  134. cur_min = NULL;
  135. for (nh = rcu_dereference(first); nh;
  136. nh = rcu_dereference(nh->u.rt_next)) {
  137. if ((nh->u.dst.flags & DST_BALANCED) != 0 &&
  138. multipath_comparekeys(&nh->fl, flp)) {
  139. int nh_ifidx = nh->u.dst.dev->ifindex;
  140. nh->u.dst.lastuse = jiffies;
  141. nh->u.dst.__use++;
  142. if (result != NULL)
  143. continue;
  144. /* search for the output interface */
  145. /* this is not SMP safe, only add/remove are
  146. * SMP safe as wrong usecount updates have no big
  147. * impact
  148. */
  149. devidx = __multipath_finddev(nh_ifidx);
  150. if (devidx == -1) {
  151. /* add the interface to the array
  152. * SMP safe
  153. */
  154. spin_lock_bh(&state_lock);
  155. /* due to SMP: search again */
  156. devidx = __multipath_finddev(nh_ifidx);
  157. if (devidx == -1) {
  158. /* add entry for device */
  159. devidx = __multipath_findslot();
  160. if (devidx == -1) {
  161. /* unlikely but possible */
  162. continue;
  163. }
  164. state[devidx].allocated = 1;
  165. state[devidx].ifi = nh_ifidx;
  166. atomic_set(&state[devidx].usecount, 0);
  167. min_usecount = 0;
  168. }
  169. spin_unlock_bh(&state_lock);
  170. }
  171. if (min_usecount == 0) {
  172. /* if the device has not been used it is
  173. * the primary target
  174. */
  175. drr_safe_inc(&state[devidx].usecount);
  176. result = nh;
  177. } else {
  178. int count =
  179. atomic_read(&state[devidx].usecount);
  180. if (min_usecount == -1 ||
  181. count < min_usecount) {
  182. cur_min = nh;
  183. cur_min_devidx = devidx;
  184. min_usecount = count;
  185. }
  186. }
  187. }
  188. }
  189. if (!result) {
  190. if (cur_min) {
  191. drr_safe_inc(&state[cur_min_devidx].usecount);
  192. result = cur_min;
  193. } else {
  194. result = first;
  195. }
  196. }
  197. *rp = result;
  198. last_selection = result;
  199. }
  200. static struct ip_mp_alg_ops drr_ops = {
  201. .mp_alg_select_route = drr_select_route,
  202. .mp_alg_remove = drr_remove,
  203. };
  204. static int __init drr_init(void)
  205. {
  206. int err = register_netdevice_notifier(&drr_dev_notifier);
  207. if (err)
  208. return err;
  209. err = multipath_alg_register(&drr_ops, IP_MP_ALG_RR);
  210. if (err)
  211. goto fail;
  212. return 0;
  213. fail:
  214. unregister_netdevice_notifier(&drr_dev_notifier);
  215. return err;
  216. }
  217. static void __exit drr_exit(void)
  218. {
  219. unregister_netdevice_notifier(&drr_dev_notifier);
  220. multipath_alg_unregister(&drr_ops, IP_MP_ALG_DRR);
  221. }
  222. module_init(drr_init);
  223. module_exit(drr_exit);