[SPARC64]: Fix 2 bugs in huge page support.
[linux-2.6/verdex.git] / net / ipv4 / multipath_drr.c
blobdb67373f9b348669de5045ba39c558105c7b1219
1 /*
2 * Device round robin policy for multipath.
5 * Version: $Id: multipath_drr.c,v 1.1.2.1 2004/09/16 07:42:34 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 <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/module.h>
35 #include <linux/mroute.h>
36 #include <linux/init.h>
37 #include <net/ip.h>
38 #include <net/protocol.h>
39 #include <linux/skbuff.h>
40 #include <net/sock.h>
41 #include <net/icmp.h>
42 #include <net/udp.h>
43 #include <net/raw.h>
44 #include <linux/notifier.h>
45 #include <linux/if_arp.h>
46 #include <linux/netfilter_ipv4.h>
47 #include <net/ipip.h>
48 #include <net/checksum.h>
49 #include <net/ip_mp_alg.h>
51 struct multipath_device {
52 int ifi; /* interface index of device */
53 atomic_t usecount;
54 int allocated;
57 #define MULTIPATH_MAX_DEVICECANDIDATES 10
59 static struct multipath_device state[MULTIPATH_MAX_DEVICECANDIDATES];
60 static DEFINE_SPINLOCK(state_lock);
62 static int inline __multipath_findslot(void)
64 int i;
66 for (i = 0; i < MULTIPATH_MAX_DEVICECANDIDATES; i++) {
67 if (state[i].allocated == 0)
68 return i;
70 return -1;
73 static int inline __multipath_finddev(int ifindex)
75 int i;
77 for (i = 0; i < MULTIPATH_MAX_DEVICECANDIDATES; i++) {
78 if (state[i].allocated != 0 &&
79 state[i].ifi == ifindex)
80 return i;
82 return -1;
85 static int drr_dev_event(struct notifier_block *this,
86 unsigned long event, void *ptr)
88 struct net_device *dev = ptr;
89 int devidx;
91 switch (event) {
92 case NETDEV_UNREGISTER:
93 case NETDEV_DOWN:
94 spin_lock_bh(&state_lock);
96 devidx = __multipath_finddev(dev->ifindex);
97 if (devidx != -1) {
98 state[devidx].allocated = 0;
99 state[devidx].ifi = 0;
100 atomic_set(&state[devidx].usecount, 0);
103 spin_unlock_bh(&state_lock);
104 break;
107 return NOTIFY_DONE;
110 static struct notifier_block drr_dev_notifier = {
111 .notifier_call = drr_dev_event,
115 static void drr_safe_inc(atomic_t *usecount)
117 int n;
119 atomic_inc(usecount);
121 n = atomic_read(usecount);
122 if (n <= 0) {
123 int i;
125 spin_lock_bh(&state_lock);
127 for (i = 0; i < MULTIPATH_MAX_DEVICECANDIDATES; i++)
128 atomic_set(&state[i].usecount, 0);
130 spin_unlock_bh(&state_lock);
134 static void drr_select_route(const struct flowi *flp,
135 struct rtable *first, struct rtable **rp)
137 struct rtable *nh, *result, *cur_min;
138 int min_usecount = -1;
139 int devidx = -1;
140 int cur_min_devidx = -1;
142 /* 1. make sure all alt. nexthops have the same GC related data */
143 /* 2. determine the new candidate to be returned */
144 result = NULL;
145 cur_min = NULL;
146 for (nh = rcu_dereference(first); nh;
147 nh = rcu_dereference(nh->u.rt_next)) {
148 if ((nh->u.dst.flags & DST_BALANCED) != 0 &&
149 multipath_comparekeys(&nh->fl, flp)) {
150 int nh_ifidx = nh->u.dst.dev->ifindex;
152 nh->u.dst.lastuse = jiffies;
153 nh->u.dst.__use++;
154 if (result != NULL)
155 continue;
157 /* search for the output interface */
159 /* this is not SMP safe, only add/remove are
160 * SMP safe as wrong usecount updates have no big
161 * impact
163 devidx = __multipath_finddev(nh_ifidx);
164 if (devidx == -1) {
165 /* add the interface to the array
166 * SMP safe
168 spin_lock_bh(&state_lock);
170 /* due to SMP: search again */
171 devidx = __multipath_finddev(nh_ifidx);
172 if (devidx == -1) {
173 /* add entry for device */
174 devidx = __multipath_findslot();
175 if (devidx == -1) {
176 /* unlikely but possible */
177 continue;
180 state[devidx].allocated = 1;
181 state[devidx].ifi = nh_ifidx;
182 atomic_set(&state[devidx].usecount, 0);
183 min_usecount = 0;
186 spin_unlock_bh(&state_lock);
189 if (min_usecount == 0) {
190 /* if the device has not been used it is
191 * the primary target
193 drr_safe_inc(&state[devidx].usecount);
194 result = nh;
195 } else {
196 int count =
197 atomic_read(&state[devidx].usecount);
199 if (min_usecount == -1 ||
200 count < min_usecount) {
201 cur_min = nh;
202 cur_min_devidx = devidx;
203 min_usecount = count;
209 if (!result) {
210 if (cur_min) {
211 drr_safe_inc(&state[cur_min_devidx].usecount);
212 result = cur_min;
213 } else {
214 result = first;
218 *rp = result;
221 static struct ip_mp_alg_ops drr_ops = {
222 .mp_alg_select_route = drr_select_route,
225 static int __init drr_init(void)
227 int err = register_netdevice_notifier(&drr_dev_notifier);
229 if (err)
230 return err;
232 err = multipath_alg_register(&drr_ops, IP_MP_ALG_DRR);
233 if (err)
234 goto fail;
236 return 0;
238 fail:
239 unregister_netdevice_notifier(&drr_dev_notifier);
240 return err;
243 static void __exit drr_exit(void)
245 unregister_netdevice_notifier(&drr_dev_notifier);
246 multipath_alg_unregister(&drr_ops, IP_MP_ALG_DRR);
249 module_init(drr_init);
250 module_exit(drr_exit);
251 MODULE_LICENSE("GPL");