multipath_wrandom.c 8.0 KB

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