added a lot of printk output to ease writing of emulator
[linux-2.4.21-pre4.git] / net / core / dst.c
1 /*
2  * net/dst.c    Protocol independent destination cache.
3  *
4  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
5  *
6  */
7
8 #include <linux/bitops.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/mm.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>
18
19 #include <net/dst.h>
20
21 /* Locking strategy:
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
25  *    of entries.
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.
29  */
30 static struct dst_entry         *dst_garbage_list;
31 #if RT_CACHE_DEBUG >= 2 
32 static atomic_t                  dst_total = ATOMIC_INIT(0);
33 #endif
34 static spinlock_t                dst_lock = SPIN_LOCK_UNLOCKED;
35
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
40 static struct timer_list dst_gc_timer =
41         { data: DST_GC_MIN, function: dst_run_gc };
42
43
44 static void dst_run_gc(unsigned long dummy)
45 {
46         int    delayed = 0;
47         struct dst_entry * dst, **dstp;
48
49         if (!spin_trylock(&dst_lock)) {
50                 mod_timer(&dst_gc_timer, jiffies + HZ/10);
51                 return;
52         }
53
54
55         del_timer(&dst_gc_timer);
56         dstp = &dst_garbage_list;
57         while ((dst = *dstp) != NULL) {
58                 if (atomic_read(&dst->__refcnt)) {
59                         dstp = &dst->next;
60                         delayed++;
61                         continue;
62                 }
63                 *dstp = dst->next;
64                 dst_destroy(dst);
65         }
66         if (!dst_garbage_list) {
67                 dst_gc_timer_inc = DST_GC_MAX;
68                 goto out;
69         }
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);
77 #endif
78         add_timer(&dst_gc_timer);
79
80 out:
81         spin_unlock(&dst_lock);
82 }
83
84 static int dst_discard(struct sk_buff *skb)
85 {
86         kfree_skb(skb);
87         return 0;
88 }
89
90 static int dst_blackhole(struct sk_buff *skb)
91 {
92         kfree_skb(skb);
93         return 0;
94 }
95
96 void * dst_alloc(struct dst_ops * ops)
97 {
98         struct dst_entry * dst;
99
100         if (ops->gc && atomic_read(&ops->entries) > ops->gc_thresh) {
101                 if (ops->gc())
102                         return NULL;
103         }
104         dst = kmem_cache_alloc(ops->kmem_cachep, SLAB_ATOMIC);
105         if (!dst)
106                 return NULL;
107         memset(dst, 0, ops->entry_size);
108         dst->ops = ops;
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);
114 #endif
115         atomic_inc(&ops->entries);
116         return dst;
117 }
118
119 void __dst_free(struct dst_entry * dst)
120 {
121         spin_lock_bh(&dst_lock);
122
123         /* The first case (dev==NULL) is required, when
124            protocol module is unloaded.
125          */
126         if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) {
127                 dst->input = dst_discard;
128                 dst->output = dst_blackhole;
129         }
130         dst->obsolete = 2;
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);
139         }
140
141         spin_unlock_bh(&dst_lock);
142 }
143
144 void dst_destroy(struct dst_entry * dst)
145 {
146         struct neighbour *neigh = dst->neighbour;
147         struct hh_cache *hh = dst->hh;
148
149         dst->hh = NULL;
150         if (hh && atomic_dec_and_test(&hh->hh_refcnt))
151                 kfree(hh);
152
153         if (neigh) {
154                 dst->neighbour = NULL;
155                 neigh_release(neigh);
156         }
157
158         atomic_dec(&dst->ops->entries);
159
160         if (dst->ops->destroy)
161                 dst->ops->destroy(dst);
162         if (dst->dev)
163                 dev_put(dst->dev);
164 #if RT_CACHE_DEBUG >= 2 
165         atomic_dec(&dst_total);
166 #endif
167         kmem_cache_free(dst->ops->kmem_cachep, dst);
168 }
169
170 static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
171 {
172         struct net_device *dev = ptr;
173         struct dst_entry *dst;
174
175         switch (event) {
176         case NETDEV_UNREGISTER:
177         case NETDEV_DOWN:
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_
185                                    _race_ _condition_.
186                                  */
187                                 if (event!=NETDEV_DOWN &&
188                                     !(dev->features & NETIF_F_DYNALLOC) &&
189                                     dst->output == dst_blackhole) {
190                                         dst->dev = &loopback_dev;
191                                         dev_put(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;
196                                                 dev_put(dev);
197                                                 dev_hold(&loopback_dev);
198                                         }
199                                 } else {
200                                         dst->input = dst_discard;
201                                         dst->output = dst_blackhole;
202                                 }
203                         }
204                 }
205                 spin_unlock_bh(&dst_lock);
206                 break;
207         }
208         return NOTIFY_DONE;
209 }
210
211 struct notifier_block dst_dev_notifier = {
212         dst_dev_event,
213         NULL,
214         0
215 };
216
217 void __init dst_init(void)
218 {
219         register_netdevice_notifier(&dst_dev_notifier);
220 }