2 * net/core/dst.c Protocol independent destination cache.
4 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8 #include <linux/bitops.h>
9 #include <linux/errno.h>
10 #include <linux/init.h>
11 #include <linux/kernel.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>
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
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);
35 static spinlock_t dst_lock = SPIN_LOCK_UNLOCKED;
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 struct timer_list dst_gc_timer =
43 TIMER_INITIALIZER(dst_run_gc, DST_GC_MIN, 0);
45 static void dst_run_gc(unsigned long dummy)
48 struct dst_entry * dst, **dstp;
50 if (!spin_trylock(&dst_lock)) {
51 mod_timer(&dst_gc_timer, jiffies + HZ/10);
56 del_timer(&dst_gc_timer);
57 dstp = &dst_garbage_list;
58 while ((dst = *dstp) != NULL) {
59 if (atomic_read(&dst->__refcnt)) {
66 dst = dst_destroy(dst);
68 /* NOHASH and still referenced. Unless it is already
69 * on gc list, invalidate it and add to gc list.
71 * Note: this is temporary. Actually, NOHASH dst's
72 * must be obsoleted when parent is obsoleted.
73 * But we do not have state "obsoleted, but
74 * referenced by parent", so it is right.
76 if (dst->obsolete > 1)
85 if (!dst_garbage_list) {
86 dst_gc_timer_inc = DST_GC_MAX;
89 if ((dst_gc_timer_expires += dst_gc_timer_inc) > DST_GC_MAX)
90 dst_gc_timer_expires = DST_GC_MAX;
91 dst_gc_timer_inc += DST_GC_INC;
92 dst_gc_timer.expires = jiffies + dst_gc_timer_expires;
93 #if RT_CACHE_DEBUG >= 2
94 printk("dst_total: %d/%d %ld\n",
95 atomic_read(&dst_total), delayed, dst_gc_timer_expires);
97 add_timer(&dst_gc_timer);
100 spin_unlock(&dst_lock);
103 static int dst_discard_in(struct sk_buff *skb)
109 static int dst_discard_out(struct sk_buff **pskb)
115 void * dst_alloc(struct dst_ops * ops)
117 struct dst_entry * dst;
119 if (ops->gc && atomic_read(&ops->entries) > ops->gc_thresh) {
123 dst = kmem_cache_alloc(ops->kmem_cachep, SLAB_ATOMIC);
126 memset(dst, 0, ops->entry_size);
127 atomic_set(&dst->__refcnt, 0);
129 dst->lastuse = jiffies;
131 dst->input = dst_discard_in;
132 dst->output = dst_discard_out;
133 #if RT_CACHE_DEBUG >= 2
134 atomic_inc(&dst_total);
136 atomic_inc(&ops->entries);
140 static void ___dst_free(struct dst_entry * dst)
142 /* The first case (dev==NULL) is required, when
143 protocol module is unloaded.
145 if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) {
146 dst->input = dst_discard_in;
147 dst->output = dst_discard_out;
152 void __dst_free(struct dst_entry * dst)
154 spin_lock_bh(&dst_lock);
156 dst->next = dst_garbage_list;
157 dst_garbage_list = dst;
158 if (dst_gc_timer_inc > DST_GC_INC) {
159 dst_gc_timer_inc = DST_GC_INC;
160 dst_gc_timer_expires = DST_GC_MIN;
161 mod_timer(&dst_gc_timer, jiffies + dst_gc_timer_expires);
163 spin_unlock_bh(&dst_lock);
166 struct dst_entry *dst_destroy(struct dst_entry * dst)
168 struct dst_entry *child;
169 struct neighbour *neigh;
173 neigh = dst->neighbour;
178 if (hh && atomic_dec_and_test(&hh->hh_refcnt))
182 dst->neighbour = NULL;
183 neigh_release(neigh);
186 atomic_dec(&dst->ops->entries);
188 if (dst->ops->destroy)
189 dst->ops->destroy(dst);
192 #if RT_CACHE_DEBUG >= 2
193 atomic_dec(&dst_total);
195 kmem_cache_free(dst->ops->kmem_cachep, dst);
199 if (atomic_dec_and_test(&dst->__refcnt)) {
200 /* We were real parent of this dst, so kill child. */
201 if (dst->flags&DST_NOHASH)
204 /* Child is still referenced, return it for freeing. */
205 if (dst->flags&DST_NOHASH)
207 /* Child is still in his hash table */
213 /* Dirty hack. We did it in 2.2 (in __dst_free),
214 * we have _very_ good reasons not to repeat
215 * this mistake in 2.3, but we have no choice
216 * now. _It_ _is_ _explicit_ _deliberate_
217 * _race_ _condition_.
219 * Commented and originally written by Alexey.
221 static void dst_ifdown(struct dst_entry *dst, int unregister)
223 struct net_device *dev = dst->dev;
226 dst->input = dst_discard_in;
227 dst->output = dst_discard_out;
232 dst->dev = &loopback_dev;
233 dev_hold(&loopback_dev);
235 if (dst->neighbour && dst->neighbour->dev == dev) {
236 dst->neighbour->dev = &loopback_dev;
238 dev_hold(&loopback_dev);
242 if (dst->ops->ifdown)
243 dst->ops->ifdown(dst, unregister);
244 } while ((dst = dst->child) && dst->flags & DST_NOHASH &&
248 static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
250 struct net_device *dev = ptr;
251 struct dst_entry *dst;
254 case NETDEV_UNREGISTER:
256 spin_lock_bh(&dst_lock);
257 for (dst = dst_garbage_list; dst; dst = dst->next) {
259 dst_ifdown(dst, event != NETDEV_DOWN);
261 spin_unlock_bh(&dst_lock);
267 struct notifier_block dst_dev_notifier = {
268 .notifier_call = dst_dev_event,
271 void __init dst_init(void)
273 register_netdevice_notifier(&dst_dev_notifier);
276 EXPORT_SYMBOL(__dst_free);
277 EXPORT_SYMBOL(dst_alloc);
278 EXPORT_SYMBOL(dst_destroy);