aboutsummaryrefslogtreecommitdiffstats
path: root/net/ipv4/multipath_random.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /net/ipv4/multipath_random.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'net/ipv4/multipath_random.c')
-rw-r--r--net/ipv4/multipath_random.c128
1 files changed, 128 insertions, 0 deletions
diff --git a/net/ipv4/multipath_random.c b/net/ipv4/multipath_random.c
new file mode 100644
index 000000000000..805a16e47de5
--- /dev/null
+++ b/net/ipv4/multipath_random.c
@@ -0,0 +1,128 @@
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 <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
15#include <linux/config.h>
16#include <asm/system.h>
17#include <asm/uaccess.h>
18#include <linux/types.h>
19#include <linux/sched.h>
20#include <linux/errno.h>
21#include <linux/timer.h>
22#include <linux/mm.h>
23#include <linux/kernel.h>
24#include <linux/fcntl.h>
25#include <linux/stat.h>
26#include <linux/socket.h>
27#include <linux/in.h>
28#include <linux/inet.h>
29#include <linux/netdevice.h>
30#include <linux/inetdevice.h>
31#include <linux/igmp.h>
32#include <linux/proc_fs.h>
33#include <linux/seq_file.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_mp_alg.h>
49
50#define MULTIPATH_MAX_CANDIDATES 40
51
52/* interface to random number generation */
53static unsigned int RANDOM_SEED = 93186752;
54
55static inline unsigned int random(unsigned int ubound)
56{
57 static unsigned int a = 1588635695,
58 q = 2,
59 r = 1117695901;
60
61 RANDOM_SEED = a*(RANDOM_SEED % q) - r*(RANDOM_SEED / q);
62
63 return RANDOM_SEED % ubound;
64}
65
66
67static void random_select_route(const struct flowi *flp,
68 struct rtable *first,
69 struct rtable **rp)
70{
71 struct rtable *rt;
72 struct rtable *decision;
73 unsigned char candidate_count = 0;
74
75 /* count all candidate */
76 for (rt = rcu_dereference(first); rt;
77 rt = rcu_dereference(rt->u.rt_next)) {
78 if ((rt->u.dst.flags & DST_BALANCED) != 0 &&
79 multipath_comparekeys(&rt->fl, flp))
80 ++candidate_count;
81 }
82
83 /* choose a random candidate */
84 decision = first;
85 if (candidate_count > 1) {
86 unsigned char i = 0;
87 unsigned char candidate_no = (unsigned char)
88 random(candidate_count);
89
90 /* find chosen candidate and adjust GC data for all candidates
91 * to ensure they stay in cache
92 */
93 for (rt = first; rt; rt = rt->u.rt_next) {
94 if ((rt->u.dst.flags & DST_BALANCED) != 0 &&
95 multipath_comparekeys(&rt->fl, flp)) {
96 rt->u.dst.lastuse = jiffies;
97
98 if (i == candidate_no)
99 decision = rt;
100
101 if (i >= candidate_count)
102 break;
103
104 i++;
105 }
106 }
107 }
108
109 decision->u.dst.__use++;
110 *rp = decision;
111}
112
113static struct ip_mp_alg_ops random_ops = {
114 .mp_alg_select_route = random_select_route,
115};
116
117static int __init random_init(void)
118{
119 return multipath_alg_register(&random_ops, IP_MP_ALG_RANDOM);
120}
121
122static void __exit random_exit(void)
123{
124 multipath_alg_unregister(&random_ops, IP_MP_ALG_RANDOM);
125}
126
127module_init(random_init);
128module_exit(random_exit);