multipath_wrandom.c 8.0 KB

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