VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / net / core / dst.c
1 /*
2  * net/core/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/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>
19
20 #include <net/dst.h>
21
22 const char dst_underflow_bug_msg[] = KERN_DEBUG "BUG: dst underflow %d: %p at %p\n";
23
24 /* Locking strategy:
25  * 1) Garbage collection state of dead destination cache
26  *    entries is protected by dst_lock.
27  * 2) GC is run only from BH context, and is the only remover
28  *    of entries.
29  * 3) Entries are added to the garbage list from both BH
30  *    and non-BH context, so local BH disabling is needed.
31  * 4) All operations modify state, so a spinlock is used.
32  */
33 static struct dst_entry         *dst_garbage_list;
34 #if RT_CACHE_DEBUG >= 2 
35 static atomic_t                  dst_total = ATOMIC_INIT(0);
36 #endif
37 static spinlock_t                dst_lock = SPIN_LOCK_UNLOCKED;
38
39 static unsigned long dst_gc_timer_expires;
40 static unsigned long dst_gc_timer_inc = DST_GC_MAX;
41 static void dst_run_gc(unsigned long);
42 static void ___dst_free(struct dst_entry * dst);
43
44 static struct timer_list dst_gc_timer =
45         TIMER_INITIALIZER(dst_run_gc, DST_GC_MIN, 0);
46
47 static void dst_run_gc(unsigned long dummy)
48 {
49         int    delayed = 0;
50         struct dst_entry * dst, **dstp;
51
52         if (!spin_trylock(&dst_lock)) {
53                 mod_timer(&dst_gc_timer, jiffies + HZ/10);
54                 return;
55         }
56
57
58         del_timer(&dst_gc_timer);
59         dstp = &dst_garbage_list;
60         while ((dst = *dstp) != NULL) {
61                 if (atomic_read(&dst->__refcnt)) {
62                         dstp = &dst->next;
63                         delayed++;
64                         continue;
65                 }
66                 *dstp = dst->next;
67
68                 dst = dst_destroy(dst);
69                 if (dst) {
70                         /* NOHASH and still referenced. Unless it is already
71                          * on gc list, invalidate it and add to gc list.
72                          *
73                          * Note: this is temporary. Actually, NOHASH dst's
74                          * must be obsoleted when parent is obsoleted.
75                          * But we do not have state "obsoleted, but
76                          * referenced by parent", so it is right.
77                          */
78                         if (dst->obsolete > 1)
79                                 continue;
80
81                         ___dst_free(dst);
82                         dst->next = *dstp;
83                         *dstp = dst;
84                         dstp = &dst->next;
85                 }
86         }
87         if (!dst_garbage_list) {
88                 dst_gc_timer_inc = DST_GC_MAX;
89                 goto out;
90         }
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         dst_gc_timer.expires = jiffies + dst_gc_timer_expires;
95 #if RT_CACHE_DEBUG >= 2
96         printk("dst_total: %d/%d %ld\n",
97                atomic_read(&dst_total), delayed,  dst_gc_timer_expires);
98 #endif
99         add_timer(&dst_gc_timer);
100
101 out:
102         spin_unlock(&dst_lock);
103 }
104
105 static int dst_discard_in(struct sk_buff *skb)
106 {
107         kfree_skb(skb);
108         return 0;
109 }
110
111 static int dst_discard_out(struct sk_buff **pskb)
112 {
113         kfree_skb(*pskb);
114         return 0;
115 }
116
117 void * dst_alloc(struct dst_ops * ops)
118 {
119         struct dst_entry * dst;
120
121         if (ops->gc && atomic_read(&ops->entries) > ops->gc_thresh) {
122                 if (ops->gc())
123                         return NULL;
124         }
125         dst = kmem_cache_alloc(ops->kmem_cachep, SLAB_ATOMIC);
126         if (!dst)
127                 return NULL;
128         memset(dst, 0, ops->entry_size);
129         atomic_set(&dst->__refcnt, 0);
130         dst->ops = ops;
131         dst->lastuse = jiffies;
132         dst->path = dst;
133         dst->input = dst_discard_in;
134         dst->output = dst_discard_out;
135 #if RT_CACHE_DEBUG >= 2 
136         atomic_inc(&dst_total);
137 #endif
138         atomic_inc(&ops->entries);
139         return dst;
140 }
141
142 static void ___dst_free(struct dst_entry * dst)
143 {
144         /* The first case (dev==NULL) is required, when
145            protocol module is unloaded.
146          */
147         if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) {
148                 dst->input = dst_discard_in;
149                 dst->output = dst_discard_out;
150         }
151         dst->obsolete = 2;
152 }
153
154 void __dst_free(struct dst_entry * dst)
155 {
156         spin_lock_bh(&dst_lock);
157         ___dst_free(dst);
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);
164         }
165         spin_unlock_bh(&dst_lock);
166 }
167
168 struct dst_entry *dst_destroy(struct dst_entry * dst)
169 {
170         struct dst_entry *child;
171         struct neighbour *neigh;
172         struct hh_cache *hh;
173
174 again:
175         neigh = dst->neighbour;
176         hh = dst->hh;
177         child = dst->child;
178
179         dst->hh = NULL;
180         if (hh && atomic_dec_and_test(&hh->hh_refcnt))
181                 kfree(hh);
182
183         if (neigh) {
184                 dst->neighbour = NULL;
185                 neigh_release(neigh);
186         }
187
188         atomic_dec(&dst->ops->entries);
189
190         if (dst->ops->destroy)
191                 dst->ops->destroy(dst);
192         if (dst->dev)
193                 dev_put(dst->dev);
194 #if RT_CACHE_DEBUG >= 2 
195         atomic_dec(&dst_total);
196 #endif
197         kmem_cache_free(dst->ops->kmem_cachep, dst);
198
199         dst = child;
200         if (dst) {
201                 if (atomic_dec_and_test(&dst->__refcnt)) {
202                         /* We were real parent of this dst, so kill child. */
203                         if (dst->flags&DST_NOHASH)
204                                 goto again;
205                 } else {
206                         /* Child is still referenced, return it for freeing. */
207                         if (dst->flags&DST_NOHASH)
208                                 return dst;
209                         /* Child is still in his hash table */
210                 }
211         }
212         return NULL;
213 }
214
215 /* Dirty hack. We did it in 2.2 (in __dst_free),
216  * we have _very_ good reasons not to repeat
217  * this mistake in 2.3, but we have no choice
218  * now. _It_ _is_ _explicit_ _deliberate_
219  * _race_ _condition_.
220  *
221  * Commented and originally written by Alexey.
222  */
223 static void dst_ifdown(struct dst_entry *dst, int unregister)
224 {
225         struct net_device *dev = dst->dev;
226
227         if (!unregister) {
228                 dst->input = dst_discard_in;
229                 dst->output = dst_discard_out;
230         }
231
232         do {
233                 if (unregister) {
234                         dst->dev = &loopback_dev;
235                         dev_hold(&loopback_dev);
236                         dev_put(dev);
237                         if (dst->neighbour && dst->neighbour->dev == dev) {
238                                 dst->neighbour->dev = &loopback_dev;
239                                 dev_put(dev);
240                                 dev_hold(&loopback_dev);
241                         }
242                 }
243
244                 if (dst->ops->ifdown)
245                         dst->ops->ifdown(dst, unregister);
246         } while ((dst = dst->child) && dst->flags & DST_NOHASH &&
247                  dst->dev == dev);
248 }
249
250 static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
251 {
252         struct net_device *dev = ptr;
253         struct dst_entry *dst;
254
255         switch (event) {
256         case NETDEV_UNREGISTER:
257         case NETDEV_DOWN:
258                 spin_lock_bh(&dst_lock);
259                 for (dst = dst_garbage_list; dst; dst = dst->next) {
260                         if (dst->dev == dev)
261                                 dst_ifdown(dst, event != NETDEV_DOWN);
262                 }
263                 spin_unlock_bh(&dst_lock);
264                 break;
265         }
266         return NOTIFY_DONE;
267 }
268
269 struct notifier_block dst_dev_notifier = {
270         .notifier_call  = dst_dev_event,
271 };
272
273 void __init dst_init(void)
274 {
275         register_netdevice_notifier(&dst_dev_notifier);
276 }
277
278 EXPORT_SYMBOL(dst_underflow_bug_msg);
279 EXPORT_SYMBOL(__dst_free);
280 EXPORT_SYMBOL(dst_alloc);
281 EXPORT_SYMBOL(dst_destroy);