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 /* 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 #if RT_CACHE_DEBUG >= 2 33 static atomic_t dst_total = ATOMIC_INIT(0); 34 #endif 35 static DEFINE_SPINLOCK(dst_lock); 36 37 static unsigned long dst_gc_timer_expires; 38 static unsigned long dst_gc_timer_inc = DST_GC_MAX; 39 static void dst_run_gc(unsigned long); 40 static void ___dst_free(struct dst_entry * dst); 41 42 static struct timer_list dst_gc_timer = 43 TIMER_INITIALIZER(dst_run_gc, DST_GC_MIN, 0); 44 45 static void dst_run_gc(unsigned long dummy) 46 { 47 int delayed = 0; 48 int work_performed; 49 struct dst_entry * dst, **dstp; 50 51 if (!spin_trylock(&dst_lock)) { 52 mod_timer(&dst_gc_timer, jiffies + HZ/10); 53 return; 54 } 55 56 del_timer(&dst_gc_timer); 57 dstp = &dst_garbage_list; 58 work_performed = 0; 59 while ((dst = *dstp) != NULL) { 60 if (atomic_read(&dst->__refcnt)) { 61 dstp = &dst->next; 62 delayed++; 63 continue; 64 } 65 *dstp = dst->next; 66 work_performed = 1; 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 (!work_performed) { 92 if ((dst_gc_timer_expires += dst_gc_timer_inc) > DST_GC_MAX) 93 dst_gc_timer_expires = DST_GC_MAX; 94 dst_gc_timer_inc += DST_GC_INC; 95 } else { 96 dst_gc_timer_inc = DST_GC_INC; 97 dst_gc_timer_expires = DST_GC_MIN; 98 } 99 dst_gc_timer.expires = jiffies + dst_gc_timer_expires; 100 #if RT_CACHE_DEBUG >= 2 101 printk("dst_total: %d/%d %ld\n", 102 atomic_read(&dst_total), delayed, dst_gc_timer_expires); 103 #endif 104 add_timer(&dst_gc_timer); 105 106 out: 107 spin_unlock(&dst_lock); 108 } 109 110 static int dst_discard_in(struct sk_buff *skb) 111 { 112 kfree_skb(skb); 113 return 0; 114 } 115 116 static int dst_discard_out(struct sk_buff *skb) 117 { 118 kfree_skb(skb); 119 return 0; 120 } 121 122 void * dst_alloc(struct dst_ops * ops) 123 { 124 struct dst_entry * dst; 125 126 if (ops->gc && atomic_read(&ops->entries) > ops->gc_thresh) { 127 if (ops->gc()) 128 return NULL; 129 } 130 dst = kmem_cache_alloc(ops->kmem_cachep, SLAB_ATOMIC); 131 if (!dst) 132 return NULL; 133 memset(dst, 0, ops->entry_size); 134 atomic_set(&dst->__refcnt, 0); 135 dst->ops = ops; 136 dst->lastuse = jiffies; 137 dst->path = dst; 138 dst->input = dst_discard_in; 139 dst->output = dst_discard_out; 140 #if RT_CACHE_DEBUG >= 2 141 atomic_inc(&dst_total); 142 #endif 143 atomic_inc(&ops->entries); 144 return dst; 145 } 146 147 static void ___dst_free(struct dst_entry * dst) 148 { 149 /* The first case (dev==NULL) is required, when 150 protocol module is unloaded. 151 */ 152 if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) { 153 dst->input = dst_discard_in; 154 dst->output = dst_discard_out; 155 } 156 dst->obsolete = 2; 157 } 158 159 void __dst_free(struct dst_entry * dst) 160 { 161 spin_lock_bh(&dst_lock); 162 ___dst_free(dst); 163 dst->next = dst_garbage_list; 164 dst_garbage_list = dst; 165 if (dst_gc_timer_inc > DST_GC_INC) { 166 dst_gc_timer_inc = DST_GC_INC; 167 dst_gc_timer_expires = DST_GC_MIN; 168 mod_timer(&dst_gc_timer, jiffies + dst_gc_timer_expires); 169 } 170 spin_unlock_bh(&dst_lock); 171 } 172 173 struct dst_entry *dst_destroy(struct dst_entry * dst) 174 { 175 struct dst_entry *child; 176 struct neighbour *neigh; 177 struct hh_cache *hh; 178 179 smp_rmb(); 180 181 again: 182 neigh = dst->neighbour; 183 hh = dst->hh; 184 child = dst->child; 185 186 dst->hh = NULL; 187 if (hh && atomic_dec_and_test(&hh->hh_refcnt)) 188 kfree(hh); 189 190 if (neigh) { 191 dst->neighbour = NULL; 192 neigh_release(neigh); 193 } 194 195 atomic_dec(&dst->ops->entries); 196 197 if (dst->ops->destroy) 198 dst->ops->destroy(dst); 199 if (dst->dev) 200 dev_put(dst->dev); 201 #if RT_CACHE_DEBUG >= 2 202 atomic_dec(&dst_total); 203 #endif 204 kmem_cache_free(dst->ops->kmem_cachep, dst); 205 206 dst = child; 207 if (dst) { 208 int nohash = dst->flags & DST_NOHASH; 209 210 if (atomic_dec_and_test(&dst->__refcnt)) { 211 /* We were real parent of this dst, so kill child. */ 212 if (nohash) 213 goto again; 214 } else { 215 /* Child is still referenced, return it for freeing. */ 216 if (nohash) 217 return dst; 218 /* Child is still in his hash table */ 219 } 220 } 221 return NULL; 222 } 223 224 /* Dirty hack. We did it in 2.2 (in __dst_free), 225 * we have _very_ good reasons not to repeat 226 * this mistake in 2.3, but we have no choice 227 * now. _It_ _is_ _explicit_ _deliberate_ 228 * _race_ _condition_. 229 * 230 * Commented and originally written by Alexey. 231 */ 232 static inline void dst_ifdown(struct dst_entry *dst, struct net_device *dev, 233 int unregister) 234 { 235 if (dst->ops->ifdown) 236 dst->ops->ifdown(dst, dev, unregister); 237 238 if (dev != dst->dev) 239 return; 240 241 if (!unregister) { 242 dst->input = dst_discard_in; 243 dst->output = dst_discard_out; 244 } else { 245 dst->dev = &loopback_dev; 246 dev_hold(&loopback_dev); 247 dev_put(dev); 248 if (dst->neighbour && dst->neighbour->dev == dev) { 249 dst->neighbour->dev = &loopback_dev; 250 dev_put(dev); 251 dev_hold(&loopback_dev); 252 } 253 } 254 } 255 256 static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr) 257 { 258 struct net_device *dev = ptr; 259 struct dst_entry *dst; 260 261 switch (event) { 262 case NETDEV_UNREGISTER: 263 case NETDEV_DOWN: 264 spin_lock_bh(&dst_lock); 265 for (dst = dst_garbage_list; dst; dst = dst->next) { 266 dst_ifdown(dst, dev, event != NETDEV_DOWN); 267 } 268 spin_unlock_bh(&dst_lock); 269 break; 270 } 271 return NOTIFY_DONE; 272 } 273 274 static struct notifier_block dst_dev_notifier = { 275 .notifier_call = dst_dev_event, 276 }; 277 278 void __init dst_init(void) 279 { 280 register_netdevice_notifier(&dst_dev_notifier); 281 } 282 283 EXPORT_SYMBOL(__dst_free); 284 EXPORT_SYMBOL(dst_alloc); 285 EXPORT_SYMBOL(dst_destroy); 286