multipath_wrandom.c 8.0 KB

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