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/skbuff.h>
16 #include <linux/string.h>
17 #include <linux/types.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 DEFINE_SPINLOCK(dst_lock
);
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);
39 static void ___dst_free(struct dst_entry
* dst
);
41 static DEFINE_TIMER(dst_gc_timer
, dst_run_gc
, DST_GC_MIN
, 0);
43 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);
54 del_timer(&dst_gc_timer
);
55 dstp
= &dst_garbage_list
;
57 while ((dst
= *dstp
) != NULL
) {
58 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 (!work_performed
) {
90 if ((dst_gc_timer_expires
+= dst_gc_timer_inc
) > DST_GC_MAX
)
91 dst_gc_timer_expires
= DST_GC_MAX
;
92 dst_gc_timer_inc
+= DST_GC_INC
;
94 dst_gc_timer_inc
= DST_GC_INC
;
95 dst_gc_timer_expires
= DST_GC_MIN
;
97 #if RT_CACHE_DEBUG >= 2
98 printk("dst_total: %d/%d %ld\n",
99 atomic_read(&dst_total
), delayed
, dst_gc_timer_expires
);
101 /* if the next desired timer is more than 4 seconds in the future
102 * then round the timer to whole seconds
104 if (dst_gc_timer_expires
> 4*HZ
)
105 mod_timer(&dst_gc_timer
,
106 round_jiffies(jiffies
+ dst_gc_timer_expires
));
108 mod_timer(&dst_gc_timer
, jiffies
+ dst_gc_timer_expires
);
111 spin_unlock(&dst_lock
);
114 static int dst_discard(struct sk_buff
*skb
)
120 void * dst_alloc(struct dst_ops
* ops
)
122 struct dst_entry
* dst
;
124 if (ops
->gc
&& atomic_read(&ops
->entries
) > ops
->gc_thresh
) {
128 dst
= kmem_cache_zalloc(ops
->kmem_cachep
, GFP_ATOMIC
);
131 atomic_set(&dst
->__refcnt
, 0);
133 dst
->lastuse
= jiffies
;
135 dst
->input
= dst
->output
= dst_discard
;
136 #if RT_CACHE_DEBUG >= 2
137 atomic_inc(&dst_total
);
139 atomic_inc(&ops
->entries
);
143 static void ___dst_free(struct dst_entry
* dst
)
145 /* The first case (dev==NULL) is required, when
146 protocol module is unloaded.
148 if (dst
->dev
== NULL
|| !(dst
->dev
->flags
&IFF_UP
)) {
149 dst
->input
= dst
->output
= dst_discard
;
154 void __dst_free(struct dst_entry
* dst
)
156 spin_lock_bh(&dst_lock
);
158 dst
->next
= dst_garbage_list
;
159 dst_garbage_list
= dst
;
160 if (dst_gc_timer_inc
> DST_GC_INC
) {
161 dst_gc_timer_inc
= DST_GC_INC
;
162 dst_gc_timer_expires
= DST_GC_MIN
;
163 mod_timer(&dst_gc_timer
, jiffies
+ dst_gc_timer_expires
);
165 spin_unlock_bh(&dst_lock
);
168 struct dst_entry
*dst_destroy(struct dst_entry
* dst
)
170 struct dst_entry
*child
;
171 struct neighbour
*neigh
;
177 neigh
= dst
->neighbour
;
182 if (hh
&& atomic_dec_and_test(&hh
->hh_refcnt
))
186 dst
->neighbour
= NULL
;
187 neigh_release(neigh
);
190 atomic_dec(&dst
->ops
->entries
);
192 if (dst
->ops
->destroy
)
193 dst
->ops
->destroy(dst
);
196 #if RT_CACHE_DEBUG >= 2
197 atomic_dec(&dst_total
);
199 kmem_cache_free(dst
->ops
->kmem_cachep
, dst
);
203 int nohash
= dst
->flags
& DST_NOHASH
;
205 if (atomic_dec_and_test(&dst
->__refcnt
)) {
206 /* We were real parent of this dst, so kill child. */
210 /* Child is still referenced, return it for freeing. */
213 /* Child is still in his hash table */
219 /* Dirty hack. We did it in 2.2 (in __dst_free),
220 * we have _very_ good reasons not to repeat
221 * this mistake in 2.3, but we have no choice
222 * now. _It_ _is_ _explicit_ _deliberate_
223 * _race_ _condition_.
225 * Commented and originally written by Alexey.
227 static inline void dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
,
230 if (dst
->ops
->ifdown
)
231 dst
->ops
->ifdown(dst
, dev
, unregister
);
237 dst
->input
= dst
->output
= dst_discard
;
239 dst
->dev
= &loopback_dev
;
240 dev_hold(&loopback_dev
);
242 if (dst
->neighbour
&& dst
->neighbour
->dev
== dev
) {
243 dst
->neighbour
->dev
= &loopback_dev
;
245 dev_hold(&loopback_dev
);
250 static int dst_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
252 struct net_device
*dev
= ptr
;
253 struct dst_entry
*dst
;
256 case NETDEV_UNREGISTER
:
258 spin_lock_bh(&dst_lock
);
259 for (dst
= dst_garbage_list
; dst
; dst
= dst
->next
) {
260 dst_ifdown(dst
, dev
, event
!= NETDEV_DOWN
);
262 spin_unlock_bh(&dst_lock
);
268 static struct notifier_block dst_dev_notifier
= {
269 .notifier_call
= dst_dev_event
,
272 void __init
dst_init(void)
274 register_netdevice_notifier(&dst_dev_notifier
);
277 EXPORT_SYMBOL(__dst_free
);
278 EXPORT_SYMBOL(dst_alloc
);
279 EXPORT_SYMBOL(dst_destroy
);