2 * net/dst.c Protocol independent destination cache.
4 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8 #include <linux/bitops.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
13 #include <linux/string.h>
14 #include <linux/errno.h>
15 #include <linux/netdevice.h>
16 #include <linux/skbuff.h>
17 #include <linux/init.h>
22 * 1) Garbage collection state of dead destination cache
23 * entries is protected by dst_lock.
24 * 2) GC is run only from BH context, and is the only remover
26 * 3) Entries are added to the garbage list from both BH
27 * and non-BH context, so local BH disabling is needed.
28 * 4) All operations modify state, so a spinlock is used.
30 static struct dst_entry *dst_garbage_list;
31 #if RT_CACHE_DEBUG >= 2
32 static atomic_t dst_total = ATOMIC_INIT(0);
34 static spinlock_t dst_lock = SPIN_LOCK_UNLOCKED;
36 static unsigned long dst_gc_timer_expires;
37 static unsigned long dst_gc_timer_inc = DST_GC_MAX;
38 static void dst_run_gc(unsigned long);
40 static struct timer_list dst_gc_timer =
41 { data: DST_GC_MIN, function: dst_run_gc };
44 static void dst_run_gc(unsigned long dummy)
47 struct dst_entry * dst, **dstp;
49 if (!spin_trylock(&dst_lock)) {
50 mod_timer(&dst_gc_timer, jiffies + HZ/10);
55 del_timer(&dst_gc_timer);
56 dstp = &dst_garbage_list;
57 while ((dst = *dstp) != NULL) {
58 if (atomic_read(&dst->__refcnt)) {
66 if (!dst_garbage_list) {
67 dst_gc_timer_inc = DST_GC_MAX;
70 if ((dst_gc_timer_expires += dst_gc_timer_inc) > DST_GC_MAX)
71 dst_gc_timer_expires = DST_GC_MAX;
72 dst_gc_timer_inc += DST_GC_INC;
73 dst_gc_timer.expires = jiffies + dst_gc_timer_expires;
74 #if RT_CACHE_DEBUG >= 2
75 printk("dst_total: %d/%d %ld\n",
76 atomic_read(&dst_total), delayed, dst_gc_timer_expires);
78 add_timer(&dst_gc_timer);
81 spin_unlock(&dst_lock);
84 static int dst_discard(struct sk_buff *skb)
90 static int dst_blackhole(struct sk_buff *skb)
96 void * dst_alloc(struct dst_ops * ops)
98 struct dst_entry * dst;
100 if (ops->gc && atomic_read(&ops->entries) > ops->gc_thresh) {
104 dst = kmem_cache_alloc(ops->kmem_cachep, SLAB_ATOMIC);
107 memset(dst, 0, ops->entry_size);
109 dst->lastuse = jiffies;
110 dst->input = dst_discard;
111 dst->output = dst_blackhole;
112 #if RT_CACHE_DEBUG >= 2
113 atomic_inc(&dst_total);
115 atomic_inc(&ops->entries);
119 void __dst_free(struct dst_entry * dst)
121 spin_lock_bh(&dst_lock);
123 /* The first case (dev==NULL) is required, when
124 protocol module is unloaded.
126 if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) {
127 dst->input = dst_discard;
128 dst->output = dst_blackhole;
131 dst->next = dst_garbage_list;
132 dst_garbage_list = dst;
133 if (dst_gc_timer_inc > DST_GC_INC) {
134 del_timer(&dst_gc_timer);
135 dst_gc_timer_inc = DST_GC_INC;
136 dst_gc_timer_expires = DST_GC_MIN;
137 dst_gc_timer.expires = jiffies + dst_gc_timer_expires;
138 add_timer(&dst_gc_timer);
141 spin_unlock_bh(&dst_lock);
144 void dst_destroy(struct dst_entry * dst)
146 struct neighbour *neigh = dst->neighbour;
147 struct hh_cache *hh = dst->hh;
150 if (hh && atomic_dec_and_test(&hh->hh_refcnt))
154 dst->neighbour = NULL;
155 neigh_release(neigh);
158 atomic_dec(&dst->ops->entries);
160 if (dst->ops->destroy)
161 dst->ops->destroy(dst);
164 #if RT_CACHE_DEBUG >= 2
165 atomic_dec(&dst_total);
167 kmem_cache_free(dst->ops->kmem_cachep, dst);
170 static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
172 struct net_device *dev = ptr;
173 struct dst_entry *dst;
176 case NETDEV_UNREGISTER:
178 spin_lock_bh(&dst_lock);
179 for (dst = dst_garbage_list; dst; dst = dst->next) {
180 if (dst->dev == dev) {
181 /* Dirty hack. We did it in 2.2 (in __dst_free),
182 we have _very_ good reasons not to repeat
183 this mistake in 2.3, but we have no choice
184 now. _It_ _is_ _explicit_ _deliberate_
187 if (event!=NETDEV_DOWN &&
188 !(dev->features & NETIF_F_DYNALLOC) &&
189 dst->output == dst_blackhole) {
190 dst->dev = &loopback_dev;
192 dev_hold(&loopback_dev);
193 dst->output = dst_discard;
194 if (dst->neighbour && dst->neighbour->dev == dev) {
195 dst->neighbour->dev = &loopback_dev;
197 dev_hold(&loopback_dev);
200 dst->input = dst_discard;
201 dst->output = dst_blackhole;
205 spin_unlock_bh(&dst_lock);
211 struct notifier_block dst_dev_notifier = {
217 void __init dst_init(void)
219 register_netdevice_notifier(&dst_dev_notifier);