2 * Weighted random policy for multipath.
5 * Version: $Id: multipath_wrandom.c,v 1.1.2.3 2004/09/22 07:51:40 elueck Exp $
7 * Authors: Einar Lueck <elueck@de.ibm.com><lkml@einar-lueck.de>
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.
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>
22 #include <linux/kernel.h>
23 #include <linux/fcntl.h>
24 #include <linux/stat.h>
25 #include <linux/socket.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>
37 #include <net/protocol.h>
38 #include <linux/skbuff.h>
43 #include <linux/notifier.h>
44 #include <linux/if_arp.h>
45 #include <linux/netfilter_ipv4.h>
47 #include <net/checksum.h>
48 #include <net/ip_fib.h>
49 #include <net/ip_mp_alg.h>
51 #define MULTIPATH_STATE_SIZE 15
53 struct multipath_candidate {
54 struct multipath_candidate *next;
59 struct multipath_dest {
60 struct list_head list;
62 const struct fib_nh *nh_info;
65 unsigned char prefixlen;
70 struct multipath_bucket {
71 struct list_head head;
75 struct multipath_route {
76 struct list_head list;
80 struct list_head dests;
85 /* state: primarily weight per route information */
86 static struct multipath_bucket state[MULTIPATH_STATE_SIZE];
88 /* interface to random number generation */
89 static unsigned int RANDOM_SEED = 93186752;
91 static inline unsigned int random(unsigned int ubound)
93 static unsigned int a = 1588635695,
96 RANDOM_SEED = a*(RANDOM_SEED % q) - r*(RANDOM_SEED / q);
97 return RANDOM_SEED % ubound;
100 static unsigned char __multipath_lookup_weight(const struct flowi *fl,
101 const struct rtable *rt)
103 const int state_idx = rt->idev->dev->ifindex % MULTIPATH_STATE_SIZE;
104 struct multipath_route *r;
105 struct multipath_route *target_route = NULL;
106 struct multipath_dest *d;
109 /* lookup the weight information for a certain route */
112 /* find state entry for gateway or add one if necessary */
113 list_for_each_entry_rcu(r, &state[state_idx].head, list) {
114 if (r->gw == rt->rt_gateway &&
115 r->oif == rt->idev->dev->ifindex) {
122 /* this should not happen... but we are prepared */
123 printk( KERN_CRIT"%s: missing state for gateway: %u and " \
124 "device %d\n", __FUNCTION__, rt->rt_gateway,
125 rt->idev->dev->ifindex);
129 /* find state entry for destination */
130 list_for_each_entry_rcu(d, &target_route->dests, list) {
131 __be32 targetnetwork = fl->fl4_dst &
132 inet_make_mask(d->prefixlen);
134 if ((targetnetwork & d->netmask) == d->network) {
135 weight = d->nh_info->nh_weight;
145 static void wrandom_init_state(void)
149 for (i = 0; i < MULTIPATH_STATE_SIZE; ++i) {
150 INIT_LIST_HEAD(&state[i].head);
151 spin_lock_init(&state[i].lock);
155 static void wrandom_select_route(const struct flowi *flp,
156 struct rtable *first,
160 struct rtable *decision;
161 struct multipath_candidate *first_mpc = NULL;
162 struct multipath_candidate *mpc, *last_mpc = NULL;
166 const size_t size_mpc = sizeof(struct multipath_candidate);
168 /* collect all candidates and identify their weights */
169 for (rt = rcu_dereference(first); rt;
170 rt = rcu_dereference(rt->u.dst.rt_next)) {
171 if ((rt->u.dst.flags & DST_BALANCED) != 0 &&
172 multipath_comparekeys(&rt->fl, flp)) {
173 struct multipath_candidate* mpc =
174 (struct multipath_candidate*)
175 kmalloc(size_mpc, GFP_ATOMIC);
180 power += __multipath_lookup_weight(flp, rt) * 10000;
189 last_mpc->next = mpc;
195 /* choose a weighted random candidate */
197 selector = random(power);
200 /* select candidate, adjust GC data and cleanup local state */
203 for (mpc = first_mpc; mpc; mpc = mpc->next) {
204 mpc->rt->u.dst.lastuse = jiffies;
205 if (last_power <= selector && selector < mpc->power)
208 last_power = mpc->power;
213 /* concurrent __multipath_flush may lead to !last_mpc */
216 decision->u.dst.__use++;
220 static void wrandom_set_nhinfo(__be32 network,
222 unsigned char prefixlen,
223 const struct fib_nh *nh)
225 const int state_idx = nh->nh_oif % MULTIPATH_STATE_SIZE;
226 struct multipath_route *r, *target_route = NULL;
227 struct multipath_dest *d, *target_dest = NULL;
229 /* store the weight information for a certain route */
230 spin_lock_bh(&state[state_idx].lock);
232 /* find state entry for gateway or add one if necessary */
233 list_for_each_entry_rcu(r, &state[state_idx].head, list) {
234 if (r->gw == nh->nh_gw && r->oif == nh->nh_oif) {
241 const size_t size_rt = sizeof(struct multipath_route);
242 target_route = (struct multipath_route *)
243 kmalloc(size_rt, GFP_ATOMIC);
245 target_route->gw = nh->nh_gw;
246 target_route->oif = nh->nh_oif;
247 memset(&target_route->rcu, 0, sizeof(struct rcu_head));
248 INIT_LIST_HEAD(&target_route->dests);
250 list_add_rcu(&target_route->list, &state[state_idx].head);
253 /* find state entry for destination or add one if necessary */
254 list_for_each_entry_rcu(d, &target_route->dests, list) {
255 if (d->nh_info == nh) {
262 const size_t size_dst = sizeof(struct multipath_dest);
263 target_dest = (struct multipath_dest*)
264 kmalloc(size_dst, GFP_ATOMIC);
266 target_dest->nh_info = nh;
267 target_dest->network = network;
268 target_dest->netmask = netmask;
269 target_dest->prefixlen = prefixlen;
270 memset(&target_dest->rcu, 0, sizeof(struct rcu_head));
272 list_add_rcu(&target_dest->list, &target_route->dests);
274 /* else: we already stored this info for another destination =>
278 spin_unlock_bh(&state[state_idx].lock);
281 static void __multipath_free(struct rcu_head *head)
283 struct multipath_route *rt = container_of(head, struct multipath_route,
288 static void __multipath_free_dst(struct rcu_head *head)
290 struct multipath_dest *dst = container_of(head,
291 struct multipath_dest,
296 static void wrandom_flush(void)
300 /* defere delete to all entries */
301 for (i = 0; i < MULTIPATH_STATE_SIZE; ++i) {
302 struct multipath_route *r;
304 spin_lock_bh(&state[i].lock);
305 list_for_each_entry_rcu(r, &state[i].head, list) {
306 struct multipath_dest *d;
307 list_for_each_entry_rcu(d, &r->dests, list) {
308 list_del_rcu(&d->list);
310 __multipath_free_dst);
312 list_del_rcu(&r->list);
317 spin_unlock_bh(&state[i].lock);
321 static struct ip_mp_alg_ops wrandom_ops = {
322 .mp_alg_select_route = wrandom_select_route,
323 .mp_alg_flush = wrandom_flush,
324 .mp_alg_set_nhinfo = wrandom_set_nhinfo,
327 static int __init wrandom_init(void)
329 wrandom_init_state();
331 return multipath_alg_register(&wrandom_ops, IP_MP_ALG_WRANDOM);
334 static void __exit wrandom_exit(void)
336 multipath_alg_unregister(&wrandom_ops, IP_MP_ALG_WRANDOM);
339 module_init(wrandom_init);
340 module_exit(wrandom_exit);
341 MODULE_LICENSE("GPL");