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 
dst_run_gc(unsigned long dummy)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 
dst_discard(struct sk_buff * skb)84 static int dst_discard(struct sk_buff *skb)
85 {
86 	kfree_skb(skb);
87 	return 0;
88 }
89 
dst_blackhole(struct sk_buff * skb)90 static int dst_blackhole(struct sk_buff *skb)
91 {
92 	kfree_skb(skb);
93 	return 0;
94 }
95 
dst_alloc(struct dst_ops * ops)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 	atomic_set(&dst->__refcnt, 0);
109 	dst->ops = ops;
110 	dst->lastuse = jiffies;
111 	dst->input = dst_discard;
112 	dst->output = dst_blackhole;
113 #if RT_CACHE_DEBUG >= 2
114 	atomic_inc(&dst_total);
115 #endif
116 	atomic_inc(&ops->entries);
117 	return dst;
118 }
119 
__dst_free(struct dst_entry * dst)120 void __dst_free(struct dst_entry * dst)
121 {
122 	spin_lock_bh(&dst_lock);
123 
124 	/* The first case (dev==NULL) is required, when
125 	   protocol module is unloaded.
126 	 */
127 	if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) {
128 		dst->input = dst_discard;
129 		dst->output = dst_blackhole;
130 	}
131 	dst->obsolete = 2;
132 	dst->next = dst_garbage_list;
133 	dst_garbage_list = dst;
134 	if (dst_gc_timer_inc > DST_GC_INC) {
135 		dst_gc_timer_inc = DST_GC_INC;
136 		dst_gc_timer_expires = DST_GC_MIN;
137 		mod_timer(&dst_gc_timer, jiffies + dst_gc_timer_expires);
138 	}
139 
140 	spin_unlock_bh(&dst_lock);
141 }
142 
dst_destroy(struct dst_entry * dst)143 void dst_destroy(struct dst_entry * dst)
144 {
145 	struct neighbour *neigh;
146 	struct hh_cache *hh;
147 
148 	smp_rmb();
149 
150 	neigh = dst->neighbour;
151 	hh = dst->hh;
152 
153 	dst->hh = NULL;
154 	if (hh && atomic_dec_and_test(&hh->hh_refcnt))
155 		kfree(hh);
156 
157 	if (neigh) {
158 		dst->neighbour = NULL;
159 		neigh_release(neigh);
160 	}
161 
162 	atomic_dec(&dst->ops->entries);
163 
164 	if (dst->ops->destroy)
165 		dst->ops->destroy(dst);
166 	if (dst->dev)
167 		dev_put(dst->dev);
168 #if RT_CACHE_DEBUG >= 2
169 	atomic_dec(&dst_total);
170 #endif
171 	kmem_cache_free(dst->ops->kmem_cachep, dst);
172 }
173 
dst_dev_event(struct notifier_block * this,unsigned long event,void * ptr)174 static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
175 {
176 	struct net_device *dev = ptr;
177 	struct dst_entry *dst;
178 
179 	switch (event) {
180 	case NETDEV_UNREGISTER:
181 	case NETDEV_DOWN:
182 		spin_lock_bh(&dst_lock);
183 		for (dst = dst_garbage_list; dst; dst = dst->next) {
184 			if (dst->dev == dev) {
185 				/* Dirty hack. We did it in 2.2 (in __dst_free),
186 				   we have _very_ good reasons not to repeat
187 				   this mistake in 2.3, but we have no choice
188 				   now. _It_ _is_ _explicit_ _deliberate_
189 				   _race_ _condition_.
190 				 */
191 				if (event!=NETDEV_DOWN &&
192 				    !(dev->features & NETIF_F_DYNALLOC) &&
193 				    dst->output == dst_blackhole) {
194 					dst->dev = &loopback_dev;
195 					dev_put(dev);
196 					dev_hold(&loopback_dev);
197 					dst->output = dst_discard;
198 					if (dst->neighbour && dst->neighbour->dev == dev) {
199 						dst->neighbour->dev = &loopback_dev;
200 						dev_put(dev);
201 						dev_hold(&loopback_dev);
202 					}
203 				} else {
204 					dst->input = dst_discard;
205 					dst->output = dst_blackhole;
206 				}
207 			}
208 		}
209 		spin_unlock_bh(&dst_lock);
210 		break;
211 	}
212 	return NOTIFY_DONE;
213 }
214 
215 struct notifier_block dst_dev_notifier = {
216 	dst_dev_event,
217 	NULL,
218 	0
219 };
220 
dst_init(void)221 void __init dst_init(void)
222 {
223 	register_netdevice_notifier(&dst_dev_notifier);
224 }
225