multipath_wrandom.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Weighted random policy for multipath.
  3. *
  4. *
  5. * Version: $Id: multipath_wrandom.c,v 1.1.2.3 2004/09/22 07:51:40 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_fib.h>
  48. #include <net/ip_mp_alg.h>
  49. #define MULTIPATH_STATE_SIZE 15
  50. struct multipath_candidate {
  51. struct multipath_candidate *next;
  52. int power;
  53. struct rtable *rt;
  54. };
  55. struct multipath_dest {
  56. struct list_head list;
  57. const struct fib_nh *nh_info;
  58. __be32 netmask;
  59. __be32 network;
  60. unsigned char prefixlen;
  61. struct rcu_head rcu;
  62. };
  63. struct multipath_bucket {
  64. struct list_head head;
  65. spinlock_t lock;
  66. };
  67. struct multipath_route {
  68. struct list_head list;
  69. int oif;
  70. __be32 gw;
  71. struct list_head dests;
  72. struct rcu_head rcu;
  73. };
  74. /* state: primarily weight per route information */
  75. static struct multipath_bucket state[MULTIPATH_STATE_SIZE];
  76. static unsigned char __multipath_lookup_weight(const struct flowi *fl,
  77. const struct rtable *rt)
  78. {
  79. const int state_idx = rt->idev->dev->ifindex % MULTIPATH_STATE_SIZE;
  80. struct multipath_route *r;
  81. struct multipath_route *target_route = NULL;
  82. struct multipath_dest *d;
  83. int weight = 1;
  84. /* lookup the weight information for a certain route */
  85. rcu_read_lock();
  86. /* find state entry for gateway or add one if necessary */
  87. list_for_each_entry_rcu(r, &state[state_idx].head, list) {
  88. if (r->gw == rt->rt_gateway &&
  89. r->oif == rt->idev->dev->ifindex) {
  90. target_route = r;
  91. break;
  92. }
  93. }
  94. if (!target_route) {
  95. /* this should not happen... but we are prepared */
  96. printk( KERN_CRIT"%s: missing state for gateway: %u and " \
  97. "device %d\n", __FUNCTION__, rt->rt_gateway,
  98. rt->idev->dev->ifindex);
  99. goto out;
  100. }
  101. /* find state entry for destination */
  102. list_for_each_entry_rcu(d, &target_route->dests, list) {
  103. __be32 targetnetwork = fl->fl4_dst &
  104. inet_make_mask(d->prefixlen);
  105. if ((targetnetwork & d->netmask) == d->network) {
  106. weight = d->nh_info->nh_weight;
  107. goto out;
  108. }
  109. }
  110. out:
  111. rcu_read_unlock();
  112. return weight;
  113. }
  114. static void wrandom_init_state(void)
  115. {
  116. int i;
  117. for (i = 0; i < MULTIPATH_STATE_SIZE; ++i) {
  118. INIT_LIST_HEAD(&state[i].head);
  119. spin_lock_init(&state[i].lock);
  120. }
  121. }
  122. static void wrandom_select_route(const struct flowi *flp,
  123. struct rtable *first,
  124. struct rtable **rp)
  125. {
  126. struct rtable *rt;
  127. struct rtable *decision;
  128. struct multipath_candidate *first_mpc = NULL;
  129. struct multipath_candidate *mpc, *last_mpc = NULL;
  130. int power = 0;
  131. int last_power;
  132. int selector;
  133. const size_t size_mpc = sizeof(struct multipath_candidate);
  134. /* collect all candidates and identify their weights */
  135. for (rt = rcu_dereference(first); rt;
  136. rt = rcu_dereference(rt->u.dst.rt_next)) {
  137. if ((rt->u.dst.flags & DST_BALANCED) != 0 &&
  138. multipath_comparekeys(&rt->fl, flp)) {
  139. struct multipath_candidate* mpc =
  140. (struct multipath_candidate*)
  141. kmalloc(size_mpc, GFP_ATOMIC);
  142. if (!mpc)
  143. return;
  144. power += __multipath_lookup_weight(flp, rt) * 10000;
  145. mpc->power = power;
  146. mpc->rt = rt;
  147. mpc->next = NULL;
  148. if (!first_mpc)
  149. first_mpc = mpc;
  150. else
  151. last_mpc->next = mpc;
  152. last_mpc = mpc;
  153. }
  154. }
  155. /* choose a weighted random candidate */
  156. decision = first;
  157. selector = random32() % power;
  158. last_power = 0;
  159. /* select candidate, adjust GC data and cleanup local state */
  160. decision = first;
  161. last_mpc = NULL;
  162. for (mpc = first_mpc; mpc; mpc = mpc->next) {
  163. mpc->rt->u.dst.lastuse = jiffies;
  164. if (last_power <= selector && selector < mpc->power)
  165. decision = mpc->rt;
  166. last_power = mpc->power;
  167. kfree(last_mpc);
  168. last_mpc = mpc;
  169. }
  170. /* concurrent __multipath_flush may lead to !last_mpc */
  171. kfree(last_mpc);
  172. decision->u.dst.__use++;
  173. *rp = decision;
  174. }
  175. static void wrandom_set_nhinfo(__be32 network,
  176. __be32 netmask,
  177. unsigned char prefixlen,
  178. const struct fib_nh *nh)
  179. {
  180. const int state_idx = nh->nh_oif % MULTIPATH_STATE_SIZE;
  181. struct multipath_route *r, *target_route = NULL;
  182. struct multipath_dest *d, *target_dest = NULL;
  183. /* store the weight information for a certain route */
  184. spin_lock_bh(&state[state_idx].lock);
  185. /* find state entry for gateway or add one if necessary */
  186. list_for_each_entry_rcu(r, &state[state_idx].head, list) {
  187. if (r->gw == nh->nh_gw && r->oif == nh->nh_oif) {
  188. target_route = r;
  189. break;
  190. }
  191. }
  192. if (!target_route) {
  193. const size_t size_rt = sizeof(struct multipath_route);
  194. target_route = (struct multipath_route *)
  195. kmalloc(size_rt, GFP_ATOMIC);
  196. target_route->gw = nh->nh_gw;
  197. target_route->oif = nh->nh_oif;
  198. memset(&target_route->rcu, 0, sizeof(struct rcu_head));
  199. INIT_LIST_HEAD(&target_route->dests);
  200. list_add_rcu(&target_route->list, &state[state_idx].head);
  201. }
  202. /* find state entry for destination or add one if necessary */
  203. list_for_each_entry_rcu(d, &target_route->dests, list) {
  204. if (d->nh_info == nh) {
  205. target_dest = d;
  206. break;
  207. }
  208. }
  209. if (!target_dest) {
  210. const size_t size_dst = sizeof(struct multipath_dest);
  211. target_dest = (struct multipath_dest*)
  212. kmalloc(size_dst, GFP_ATOMIC);
  213. target_dest->nh_info = nh;
  214. target_dest->network = network;
  215. target_dest->netmask = netmask;
  216. target_dest->prefixlen = prefixlen;
  217. memset(&target_dest->rcu, 0, sizeof(struct rcu_head));
  218. list_add_rcu(&target_dest->list, &target_route->dests);
  219. }
  220. /* else: we already stored this info for another destination =>
  221. * we are finished
  222. */
  223. spin_unlock_bh(&state[state_idx].lock);
  224. }
  225. static void __multipath_free(struct rcu_head *head)
  226. {
  227. struct multipath_route *rt = container_of(head, struct multipath_route,
  228. rcu);
  229. kfree(rt);
  230. }
  231. static void __multipath_free_dst(struct rcu_head *head)
  232. {
  233. struct multipath_dest *dst = container_of(head,
  234. struct multipath_dest,
  235. rcu);
  236. kfree(dst);
  237. }
  238. static void wrandom_flush(void)
  239. {
  240. int i;
  241. /* defere delete to all entries */
  242. for (i = 0; i < MULTIPATH_STATE_SIZE; ++i) {
  243. struct multipath_route *r;
  244. spin_lock_bh(&state[i].lock);
  245. list_for_each_entry_rcu(r, &state[i].head, list) {
  246. struct multipath_dest *d;
  247. list_for_each_entry_rcu(d, &r->dests, list) {
  248. list_del_rcu(&d->list);
  249. call_rcu(&d->rcu,
  250. __multipath_free_dst);
  251. }
  252. list_del_rcu(&r->list);
  253. call_rcu(&r->rcu,
  254. __multipath_free);
  255. }
  256. spin_unlock_bh(&state[i].lock);
  257. }
  258. }
  259. static struct ip_mp_alg_ops wrandom_ops = {
  260. .mp_alg_select_route = wrandom_select_route,
  261. .mp_alg_flush = wrandom_flush,
  262. .mp_alg_set_nhinfo = wrandom_set_nhinfo,
  263. };
  264. static int __init wrandom_init(void)
  265. {
  266. wrandom_init_state();
  267. return multipath_alg_register(&wrandom_ops, IP_MP_ALG_WRANDOM);
  268. }
  269. static void __exit wrandom_exit(void)
  270. {
  271. multipath_alg_unregister(&wrandom_ops, IP_MP_ALG_WRANDOM);
  272. }
  273. module_init(wrandom_init);
  274. module_exit(wrandom_exit);
  275. MODULE_LICENSE("GPL");