]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Random policy for multipath. | |
3 | * | |
4 | * | |
5 | * Version: $Id: multipath_random.c,v 1.1.2.3 2004/09/21 08:42:11 elueck Exp $ | |
6 | * | |
7 | * Authors: Einar Lueck <[email protected]><[email protected]> | |
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 | ||
1da177e4 LT |
15 | #include <asm/system.h> |
16 | #include <asm/uaccess.h> | |
17 | #include <linux/types.h> | |
1da177e4 LT |
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> | |
6efd8455 | 32 | #include <linux/module.h> |
1da177e4 LT |
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_mp_alg.h> | |
48 | ||
49 | #define MULTIPATH_MAX_CANDIDATES 40 | |
50 | ||
51 | /* interface to random number generation */ | |
52 | static unsigned int RANDOM_SEED = 93186752; | |
53 | ||
54 | static inline unsigned int random(unsigned int ubound) | |
55 | { | |
56 | static unsigned int a = 1588635695, | |
57 | q = 2, | |
58 | r = 1117695901; | |
59 | ||
60 | RANDOM_SEED = a*(RANDOM_SEED % q) - r*(RANDOM_SEED / q); | |
61 | ||
62 | return RANDOM_SEED % ubound; | |
63 | } | |
64 | ||
65 | ||
66 | static void random_select_route(const struct flowi *flp, | |
67 | struct rtable *first, | |
68 | struct rtable **rp) | |
69 | { | |
70 | struct rtable *rt; | |
71 | struct rtable *decision; | |
72 | unsigned char candidate_count = 0; | |
73 | ||
74 | /* count all candidate */ | |
75 | for (rt = rcu_dereference(first); rt; | |
5ef213f6 | 76 | rt = rcu_dereference(rt->u.dst.rt_next)) { |
1da177e4 LT |
77 | if ((rt->u.dst.flags & DST_BALANCED) != 0 && |
78 | multipath_comparekeys(&rt->fl, flp)) | |
79 | ++candidate_count; | |
80 | } | |
81 | ||
82 | /* choose a random candidate */ | |
83 | decision = first; | |
84 | if (candidate_count > 1) { | |
85 | unsigned char i = 0; | |
86 | unsigned char candidate_no = (unsigned char) | |
87 | random(candidate_count); | |
88 | ||
89 | /* find chosen candidate and adjust GC data for all candidates | |
90 | * to ensure they stay in cache | |
91 | */ | |
5ef213f6 | 92 | for (rt = first; rt; rt = rt->u.dst.rt_next) { |
1da177e4 LT |
93 | if ((rt->u.dst.flags & DST_BALANCED) != 0 && |
94 | multipath_comparekeys(&rt->fl, flp)) { | |
95 | rt->u.dst.lastuse = jiffies; | |
96 | ||
97 | if (i == candidate_no) | |
98 | decision = rt; | |
99 | ||
100 | if (i >= candidate_count) | |
101 | break; | |
102 | ||
103 | i++; | |
104 | } | |
105 | } | |
106 | } | |
107 | ||
108 | decision->u.dst.__use++; | |
109 | *rp = decision; | |
110 | } | |
111 | ||
112 | static struct ip_mp_alg_ops random_ops = { | |
113 | .mp_alg_select_route = random_select_route, | |
114 | }; | |
115 | ||
116 | static int __init random_init(void) | |
117 | { | |
118 | return multipath_alg_register(&random_ops, IP_MP_ALG_RANDOM); | |
119 | } | |
120 | ||
121 | static void __exit random_exit(void) | |
122 | { | |
123 | multipath_alg_unregister(&random_ops, IP_MP_ALG_RANDOM); | |
124 | } | |
125 | ||
126 | module_init(random_init); | |
127 | module_exit(random_exit); | |
6efd8455 | 128 | MODULE_LICENSE("GPL"); |