1 /*
2 * net/dst.c Protocol independent destination cache.
3 *
4 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
5 *
6 */
7
8 #include <asm/system.h>
9 #include <asm/bitops.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/string.h>
15 #include <linux/errno.h>
16 #include <linux/netdevice.h>
17 #include <linux/skbuff.h>
18 #include <linux/init.h>
19
20 #include <net/dst.h>
21
22 /* Locking strategy:
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
26 * of entries.
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.
30 */
31 static struct dst_entry *dst_garbage_list;
32 static atomic_t dst_total = ATOMIC_INIT(0);
33 static spinlock_t dst_lock = SPIN_LOCK_UNLOCKED;
34
35 static unsigned long dst_gc_timer_expires;
36 static unsigned long dst_gc_timer_inc = DST_GC_MAX;
37 static void dst_run_gc(unsigned long);
38
39 static struct timer_list dst_gc_timer =
40 { data: DST_GC_MIN, function: dst_run_gc };
41
42
43 static void dst_run_gc(unsigned long dummy)
44 {
45 int delayed = 0;
46 struct dst_entry * dst, **dstp;
47
48 if (!spin_trylock(&dst_lock)) {
49 mod_timer(&dst_gc_timer, jiffies + HZ/10);
50 return;
51 }
52
53
54 del_timer(&dst_gc_timer);
55 dstp = &dst_garbage_list;
56 while ((dst = *dstp) != NULL) {
57 if (atomic_read(&dst->__refcnt)) {
58 dstp = &dst->next;
59 delayed++;
60 continue;
61 }
62 *dstp = dst->next;
63 dst_destroy(dst);
64 }
65 if (!dst_garbage_list) {
66 dst_gc_timer_inc = DST_GC_MAX;
67 goto out;
68 }
69 if ((dst_gc_timer_expires += dst_gc_timer_inc) > DST_GC_MAX)
70 dst_gc_timer_expires = DST_GC_MAX;
71 dst_gc_timer_inc += DST_GC_INC;
72 dst_gc_timer.expires = jiffies + dst_gc_timer_expires;
73 #if RT_CACHE_DEBUG >= 2
74 printk("dst_total: %d/%d %ld\n",
75 atomic_read(&dst_total), delayed, dst_gc_timer_expires);
76 #endif
77 add_timer(&dst_gc_timer);
78
79 out:
80 spin_unlock(&dst_lock);
81 }
82
83 static int dst_discard(struct sk_buff *skb)
84 {
85 kfree_skb(skb);
86 return 0;
87 }
88
89 static int dst_blackhole(struct sk_buff *skb)
90 {
91 kfree_skb(skb);
92 return 0;
93 }
94
95 void * dst_alloc(struct dst_ops * ops)
96 {
97 struct dst_entry * dst;
98
99 if (ops->gc && atomic_read(&ops->entries) > ops->gc_thresh) {
100 if (ops->gc())
101 return NULL;
102 }
103 dst = kmem_cache_alloc(ops->kmem_cachep, SLAB_ATOMIC);
104 if (!dst)
105 return NULL;
106 memset(dst, 0, ops->entry_size);
107 dst->ops = ops;
108 dst->lastuse = jiffies;
109 dst->input = dst_discard;
110 dst->output = dst_blackhole;
111 atomic_inc(&dst_total);
112 atomic_inc(&ops->entries);
113 return dst;
114 }
115
116 void __dst_free(struct dst_entry * dst)
117 {
118 spin_lock_bh(&dst_lock);
119
120 /* The first case (dev==NULL) is required, when
121 protocol module is unloaded.
122 */
123 if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) {
124 dst->input = dst_discard;
125 dst->output = dst_blackhole;
126 }
127 dst->obsolete = 2;
128 dst->next = dst_garbage_list;
129 dst_garbage_list = dst;
130 if (dst_gc_timer_inc > DST_GC_INC) {
131 del_timer(&dst_gc_timer);
132 dst_gc_timer_inc = DST_GC_INC;
133 dst_gc_timer_expires = DST_GC_MIN;
134 dst_gc_timer.expires = jiffies + dst_gc_timer_expires;
135 add_timer(&dst_gc_timer);
136 }
137
138 spin_unlock_bh(&dst_lock);
139 }
140
141 void dst_destroy(struct dst_entry * dst)
142 {
143 struct neighbour *neigh = dst->neighbour;
144 struct hh_cache *hh = dst->hh;
145
146 dst->hh = NULL;
147 if (hh && atomic_dec_and_test(&hh->hh_refcnt))
148 kfree(hh);
149
150 if (neigh) {
151 dst->neighbour = NULL;
152 neigh_release(neigh);
153 }
154
155 atomic_dec(&dst->ops->entries);
156
157 if (dst->ops->destroy)
158 dst->ops->destroy(dst);
159 if (dst->dev)
160 dev_put(dst->dev);
161 atomic_dec(&dst_total);
162 kmem_cache_free(dst->ops->kmem_cachep, dst);
163 }
164
165 static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
166 {
167 struct net_device *dev = ptr;
168 struct dst_entry *dst;
169
170 switch (event) {
171 case NETDEV_UNREGISTER:
172 case NETDEV_DOWN:
173 spin_lock_bh(&dst_lock);
174 for (dst = dst_garbage_list; dst; dst = dst->next) {
175 if (dst->dev == dev) {
176 /* Dirty hack. We did it in 2.2 (in __dst_free),
177 we have _very_ good reasons not to repeat
178 this mistake in 2.3, but we have no choice
179 now. _It_ _is_ _explicit_ _deliberate_
180 _race_ _condition_.
181 */
182 if (event!=NETDEV_DOWN &&
183 !(dev->features & NETIF_F_DYNALLOC) &&
184 dst->output == dst_blackhole) {
185 dst->dev = &loopback_dev;
186 dev_put(dev);
187 dev_hold(&loopback_dev);
188 dst->output = dst_discard;
189 if (dst->neighbour && dst->neighbour->dev == dev) {
190 dst->neighbour->dev = &loopback_dev;
191 dev_put(dev);
192 dev_hold(&loopback_dev);
193 }
194 } else {
195 dst->input = dst_discard;
196 dst->output = dst_blackhole;
197 }
198 }
199 }
200 spin_unlock_bh(&dst_lock);
201 break;
202 }
203 return NOTIFY_DONE;
204 }
205
206 struct notifier_block dst_dev_notifier = {
207 dst_dev_event,
208 NULL,
209 0
210 };
211
212 void __init dst_init(void)
213 {
214 register_netdevice_notifier(&dst_dev_notifier);
215 }
216
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.