[PATCH] uml: delete unused file
[linux-2.6/openmoko-kernel/knife-kernel.git] / net / core / dst.c
blob1a53fb39b7e008efa67709bcd4ac984426e064df
1 /*
2 * net/core/dst.c Protocol independent destination cache.
4 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 */
8 #include <linux/bitops.h>
9 #include <linux/errno.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/sched.h>
16 #include <linux/skbuff.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
20 #include <net/dst.h>
22 /* Locking strategy:
23 * 1) Garbage collection state of dead destination cache
24 * entries is protected by dst_lock.
25 * 2) GC is run only from BH context, and is the only remover
26 * of entries.
27 * 3) Entries are added to the garbage list from both BH
28 * and non-BH context, so local BH disabling is needed.
29 * 4) All operations modify state, so a spinlock is used.
31 static struct dst_entry *dst_garbage_list;
32 #if RT_CACHE_DEBUG >= 2
33 static atomic_t dst_total = ATOMIC_INIT(0);
34 #endif
35 static DEFINE_SPINLOCK(dst_lock);
37 static unsigned long dst_gc_timer_expires;
38 static unsigned long dst_gc_timer_inc = DST_GC_MAX;
39 static void dst_run_gc(unsigned long);
40 static void ___dst_free(struct dst_entry * dst);
42 static DEFINE_TIMER(dst_gc_timer, dst_run_gc, DST_GC_MIN, 0);
44 static void dst_run_gc(unsigned long dummy)
46 int delayed = 0;
47 int work_performed;
48 struct dst_entry * dst, **dstp;
50 if (!spin_trylock(&dst_lock)) {
51 mod_timer(&dst_gc_timer, jiffies + HZ/10);
52 return;
55 del_timer(&dst_gc_timer);
56 dstp = &dst_garbage_list;
57 work_performed = 0;
58 while ((dst = *dstp) != NULL) {
59 if (atomic_read(&dst->__refcnt)) {
60 dstp = &dst->next;
61 delayed++;
62 continue;
64 *dstp = dst->next;
65 work_performed = 1;
67 dst = dst_destroy(dst);
68 if (dst) {
69 /* NOHASH and still referenced. Unless it is already
70 * on gc list, invalidate it and add to gc list.
72 * Note: this is temporary. Actually, NOHASH dst's
73 * must be obsoleted when parent is obsoleted.
74 * But we do not have state "obsoleted, but
75 * referenced by parent", so it is right.
77 if (dst->obsolete > 1)
78 continue;
80 ___dst_free(dst);
81 dst->next = *dstp;
82 *dstp = dst;
83 dstp = &dst->next;
86 if (!dst_garbage_list) {
87 dst_gc_timer_inc = DST_GC_MAX;
88 goto out;
90 if (!work_performed) {
91 if ((dst_gc_timer_expires += dst_gc_timer_inc) > DST_GC_MAX)
92 dst_gc_timer_expires = DST_GC_MAX;
93 dst_gc_timer_inc += DST_GC_INC;
94 } else {
95 dst_gc_timer_inc = DST_GC_INC;
96 dst_gc_timer_expires = DST_GC_MIN;
98 #if RT_CACHE_DEBUG >= 2
99 printk("dst_total: %d/%d %ld\n",
100 atomic_read(&dst_total), delayed, dst_gc_timer_expires);
101 #endif
102 /* if the next desired timer is more than 4 seconds in the future
103 * then round the timer to whole seconds
105 if (dst_gc_timer_expires > 4*HZ)
106 mod_timer(&dst_gc_timer,
107 round_jiffies(jiffies + dst_gc_timer_expires));
108 else
109 mod_timer(&dst_gc_timer, jiffies + dst_gc_timer_expires);
111 out:
112 spin_unlock(&dst_lock);
115 static int dst_discard_in(struct sk_buff *skb)
117 kfree_skb(skb);
118 return 0;
121 static int dst_discard_out(struct sk_buff *skb)
123 kfree_skb(skb);
124 return 0;
127 void * dst_alloc(struct dst_ops * ops)
129 struct dst_entry * dst;
131 if (ops->gc && atomic_read(&ops->entries) > ops->gc_thresh) {
132 if (ops->gc())
133 return NULL;
135 dst = kmem_cache_alloc(ops->kmem_cachep, GFP_ATOMIC);
136 if (!dst)
137 return NULL;
138 memset(dst, 0, ops->entry_size);
139 atomic_set(&dst->__refcnt, 0);
140 dst->ops = ops;
141 dst->lastuse = jiffies;
142 dst->path = dst;
143 dst->input = dst_discard_in;
144 dst->output = dst_discard_out;
145 #if RT_CACHE_DEBUG >= 2
146 atomic_inc(&dst_total);
147 #endif
148 atomic_inc(&ops->entries);
149 return dst;
152 static void ___dst_free(struct dst_entry * dst)
154 /* The first case (dev==NULL) is required, when
155 protocol module is unloaded.
157 if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) {
158 dst->input = dst_discard_in;
159 dst->output = dst_discard_out;
161 dst->obsolete = 2;
164 void __dst_free(struct dst_entry * dst)
166 spin_lock_bh(&dst_lock);
167 ___dst_free(dst);
168 dst->next = dst_garbage_list;
169 dst_garbage_list = dst;
170 if (dst_gc_timer_inc > DST_GC_INC) {
171 dst_gc_timer_inc = DST_GC_INC;
172 dst_gc_timer_expires = DST_GC_MIN;
173 mod_timer(&dst_gc_timer, jiffies + dst_gc_timer_expires);
175 spin_unlock_bh(&dst_lock);
178 struct dst_entry *dst_destroy(struct dst_entry * dst)
180 struct dst_entry *child;
181 struct neighbour *neigh;
182 struct hh_cache *hh;
184 smp_rmb();
186 again:
187 neigh = dst->neighbour;
188 hh = dst->hh;
189 child = dst->child;
191 dst->hh = NULL;
192 if (hh && atomic_dec_and_test(&hh->hh_refcnt))
193 kfree(hh);
195 if (neigh) {
196 dst->neighbour = NULL;
197 neigh_release(neigh);
200 atomic_dec(&dst->ops->entries);
202 if (dst->ops->destroy)
203 dst->ops->destroy(dst);
204 if (dst->dev)
205 dev_put(dst->dev);
206 #if RT_CACHE_DEBUG >= 2
207 atomic_dec(&dst_total);
208 #endif
209 kmem_cache_free(dst->ops->kmem_cachep, dst);
211 dst = child;
212 if (dst) {
213 int nohash = dst->flags & DST_NOHASH;
215 if (atomic_dec_and_test(&dst->__refcnt)) {
216 /* We were real parent of this dst, so kill child. */
217 if (nohash)
218 goto again;
219 } else {
220 /* Child is still referenced, return it for freeing. */
221 if (nohash)
222 return dst;
223 /* Child is still in his hash table */
226 return NULL;
229 /* Dirty hack. We did it in 2.2 (in __dst_free),
230 * we have _very_ good reasons not to repeat
231 * this mistake in 2.3, but we have no choice
232 * now. _It_ _is_ _explicit_ _deliberate_
233 * _race_ _condition_.
235 * Commented and originally written by Alexey.
237 static inline void dst_ifdown(struct dst_entry *dst, struct net_device *dev,
238 int unregister)
240 if (dst->ops->ifdown)
241 dst->ops->ifdown(dst, dev, unregister);
243 if (dev != dst->dev)
244 return;
246 if (!unregister) {
247 dst->input = dst_discard_in;
248 dst->output = dst_discard_out;
249 } else {
250 dst->dev = &loopback_dev;
251 dev_hold(&loopback_dev);
252 dev_put(dev);
253 if (dst->neighbour && dst->neighbour->dev == dev) {
254 dst->neighbour->dev = &loopback_dev;
255 dev_put(dev);
256 dev_hold(&loopback_dev);
261 static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
263 struct net_device *dev = ptr;
264 struct dst_entry *dst;
266 switch (event) {
267 case NETDEV_UNREGISTER:
268 case NETDEV_DOWN:
269 spin_lock_bh(&dst_lock);
270 for (dst = dst_garbage_list; dst; dst = dst->next) {
271 dst_ifdown(dst, dev, event != NETDEV_DOWN);
273 spin_unlock_bh(&dst_lock);
274 break;
276 return NOTIFY_DONE;
279 static struct notifier_block dst_dev_notifier = {
280 .notifier_call = dst_dev_event,
283 void __init dst_init(void)
285 register_netdevice_notifier(&dst_dev_notifier);
288 EXPORT_SYMBOL(__dst_free);
289 EXPORT_SYMBOL(dst_alloc);
290 EXPORT_SYMBOL(dst_destroy);