| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Ingo Molnar | 8d06afa | 2005-09-09 13:10:40 -0700 | [diff] [blame] | 42 | static DEFINE_TIMER(dst_gc_timer, dst_run_gc, DST_GC_MIN, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 |  | 
|  | 44 | static void dst_run_gc(unsigned long dummy) | 
|  | 45 | { | 
|  | 46 | int    delayed = 0; | 
| Denis Lunev | f0098f7 | 2005-07-30 17:47:25 -0700 | [diff] [blame] | 47 | int    work_performed; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | struct dst_entry * dst, **dstp; | 
|  | 49 |  | 
|  | 50 | if (!spin_trylock(&dst_lock)) { | 
|  | 51 | mod_timer(&dst_gc_timer, jiffies + HZ/10); | 
|  | 52 | return; | 
|  | 53 | } | 
|  | 54 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | del_timer(&dst_gc_timer); | 
|  | 56 | dstp = &dst_garbage_list; | 
| Denis Lunev | f0098f7 | 2005-07-30 17:47:25 -0700 | [diff] [blame] | 57 | work_performed = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | while ((dst = *dstp) != NULL) { | 
|  | 59 | if (atomic_read(&dst->__refcnt)) { | 
|  | 60 | dstp = &dst->next; | 
|  | 61 | delayed++; | 
|  | 62 | continue; | 
|  | 63 | } | 
|  | 64 | *dstp = dst->next; | 
| Denis Lunev | f0098f7 | 2005-07-30 17:47:25 -0700 | [diff] [blame] | 65 | work_performed = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 |  | 
|  | 67 | dst = dst_destroy(dst); | 
|  | 68 | if (dst) { | 
|  | 69 | /* NOHASH and still referenced. Unless it is already | 
|  | 70 | * on gc list, invalidate it and add to gc list. | 
|  | 71 | * | 
|  | 72 | * Note: this is temporary. Actually, NOHASH dst's | 
|  | 73 | * must be obsoleted when parent is obsoleted. | 
|  | 74 | * But we do not have state "obsoleted, but | 
|  | 75 | * referenced by parent", so it is right. | 
|  | 76 | */ | 
|  | 77 | if (dst->obsolete > 1) | 
|  | 78 | continue; | 
|  | 79 |  | 
|  | 80 | ___dst_free(dst); | 
|  | 81 | dst->next = *dstp; | 
|  | 82 | *dstp = dst; | 
|  | 83 | dstp = &dst->next; | 
|  | 84 | } | 
|  | 85 | } | 
|  | 86 | if (!dst_garbage_list) { | 
|  | 87 | dst_gc_timer_inc = DST_GC_MAX; | 
|  | 88 | goto out; | 
|  | 89 | } | 
| Denis Lunev | f0098f7 | 2005-07-30 17:47:25 -0700 | [diff] [blame] | 90 | if (!work_performed) { | 
|  | 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 | } else { | 
|  | 95 | dst_gc_timer_inc = DST_GC_INC; | 
|  | 96 | dst_gc_timer_expires = DST_GC_MIN; | 
|  | 97 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | #if RT_CACHE_DEBUG >= 2 | 
|  | 99 | printk("dst_total: %d/%d %ld\n", | 
|  | 100 | atomic_read(&dst_total), delayed,  dst_gc_timer_expires); | 
|  | 101 | #endif | 
| Arjan van de Ven | f5a6e01 | 2007-02-05 17:59:51 -0800 | [diff] [blame] | 102 | /* if the next desired timer is more than 4 seconds in the future | 
|  | 103 | * then round the timer to whole seconds | 
|  | 104 | */ | 
|  | 105 | if (dst_gc_timer_expires > 4*HZ) | 
|  | 106 | mod_timer(&dst_gc_timer, | 
|  | 107 | round_jiffies(jiffies + dst_gc_timer_expires)); | 
|  | 108 | else | 
|  | 109 | mod_timer(&dst_gc_timer, jiffies + dst_gc_timer_expires); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 |  | 
|  | 111 | out: | 
|  | 112 | spin_unlock(&dst_lock); | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | static int dst_discard_in(struct sk_buff *skb) | 
|  | 116 | { | 
|  | 117 | kfree_skb(skb); | 
|  | 118 | return 0; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | static int dst_discard_out(struct sk_buff *skb) | 
|  | 122 | { | 
|  | 123 | kfree_skb(skb); | 
|  | 124 | return 0; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | void * dst_alloc(struct dst_ops * ops) | 
|  | 128 | { | 
|  | 129 | struct dst_entry * dst; | 
|  | 130 |  | 
|  | 131 | if (ops->gc && atomic_read(&ops->entries) > ops->gc_thresh) { | 
|  | 132 | if (ops->gc()) | 
|  | 133 | return NULL; | 
|  | 134 | } | 
| Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame^] | 135 | dst = kmem_cache_zalloc(ops->kmem_cachep, GFP_ATOMIC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | if (!dst) | 
|  | 137 | return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | atomic_set(&dst->__refcnt, 0); | 
|  | 139 | dst->ops = ops; | 
|  | 140 | dst->lastuse = jiffies; | 
|  | 141 | dst->path = dst; | 
|  | 142 | dst->input = dst_discard_in; | 
|  | 143 | dst->output = dst_discard_out; | 
|  | 144 | #if RT_CACHE_DEBUG >= 2 | 
|  | 145 | atomic_inc(&dst_total); | 
|  | 146 | #endif | 
|  | 147 | atomic_inc(&ops->entries); | 
|  | 148 | return dst; | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | static void ___dst_free(struct dst_entry * dst) | 
|  | 152 | { | 
|  | 153 | /* The first case (dev==NULL) is required, when | 
|  | 154 | protocol module is unloaded. | 
|  | 155 | */ | 
|  | 156 | if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) { | 
|  | 157 | dst->input = dst_discard_in; | 
|  | 158 | dst->output = dst_discard_out; | 
|  | 159 | } | 
|  | 160 | dst->obsolete = 2; | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | void __dst_free(struct dst_entry * dst) | 
|  | 164 | { | 
|  | 165 | spin_lock_bh(&dst_lock); | 
|  | 166 | ___dst_free(dst); | 
|  | 167 | dst->next = dst_garbage_list; | 
|  | 168 | dst_garbage_list = dst; | 
|  | 169 | if (dst_gc_timer_inc > DST_GC_INC) { | 
|  | 170 | dst_gc_timer_inc = DST_GC_INC; | 
|  | 171 | dst_gc_timer_expires = DST_GC_MIN; | 
|  | 172 | mod_timer(&dst_gc_timer, jiffies + dst_gc_timer_expires); | 
|  | 173 | } | 
|  | 174 | spin_unlock_bh(&dst_lock); | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | struct dst_entry *dst_destroy(struct dst_entry * dst) | 
|  | 178 | { | 
|  | 179 | struct dst_entry *child; | 
|  | 180 | struct neighbour *neigh; | 
|  | 181 | struct hh_cache *hh; | 
|  | 182 |  | 
|  | 183 | smp_rmb(); | 
|  | 184 |  | 
|  | 185 | again: | 
|  | 186 | neigh = dst->neighbour; | 
|  | 187 | hh = dst->hh; | 
|  | 188 | child = dst->child; | 
|  | 189 |  | 
|  | 190 | dst->hh = NULL; | 
|  | 191 | if (hh && atomic_dec_and_test(&hh->hh_refcnt)) | 
|  | 192 | kfree(hh); | 
|  | 193 |  | 
|  | 194 | if (neigh) { | 
|  | 195 | dst->neighbour = NULL; | 
|  | 196 | neigh_release(neigh); | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | atomic_dec(&dst->ops->entries); | 
|  | 200 |  | 
|  | 201 | if (dst->ops->destroy) | 
|  | 202 | dst->ops->destroy(dst); | 
|  | 203 | if (dst->dev) | 
|  | 204 | dev_put(dst->dev); | 
|  | 205 | #if RT_CACHE_DEBUG >= 2 | 
|  | 206 | atomic_dec(&dst_total); | 
|  | 207 | #endif | 
|  | 208 | kmem_cache_free(dst->ops->kmem_cachep, dst); | 
|  | 209 |  | 
|  | 210 | dst = child; | 
|  | 211 | if (dst) { | 
| Herbert Xu | 6775cab | 2005-04-16 15:24:10 -0700 | [diff] [blame] | 212 | int nohash = dst->flags & DST_NOHASH; | 
|  | 213 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (atomic_dec_and_test(&dst->__refcnt)) { | 
|  | 215 | /* We were real parent of this dst, so kill child. */ | 
| Herbert Xu | 6775cab | 2005-04-16 15:24:10 -0700 | [diff] [blame] | 216 | if (nohash) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | goto again; | 
|  | 218 | } else { | 
|  | 219 | /* Child is still referenced, return it for freeing. */ | 
| Herbert Xu | 6775cab | 2005-04-16 15:24:10 -0700 | [diff] [blame] | 220 | if (nohash) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | return dst; | 
|  | 222 | /* Child is still in his hash table */ | 
|  | 223 | } | 
|  | 224 | } | 
|  | 225 | return NULL; | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | /* Dirty hack. We did it in 2.2 (in __dst_free), | 
|  | 229 | * we have _very_ good reasons not to repeat | 
|  | 230 | * this mistake in 2.3, but we have no choice | 
|  | 231 | * now. _It_ _is_ _explicit_ _deliberate_ | 
|  | 232 | * _race_ _condition_. | 
|  | 233 | * | 
|  | 234 | * Commented and originally written by Alexey. | 
|  | 235 | */ | 
|  | 236 | static inline void dst_ifdown(struct dst_entry *dst, struct net_device *dev, | 
|  | 237 | int unregister) | 
|  | 238 | { | 
|  | 239 | if (dst->ops->ifdown) | 
|  | 240 | dst->ops->ifdown(dst, dev, unregister); | 
|  | 241 |  | 
|  | 242 | if (dev != dst->dev) | 
|  | 243 | return; | 
|  | 244 |  | 
|  | 245 | if (!unregister) { | 
|  | 246 | dst->input = dst_discard_in; | 
|  | 247 | dst->output = dst_discard_out; | 
|  | 248 | } else { | 
|  | 249 | dst->dev = &loopback_dev; | 
|  | 250 | dev_hold(&loopback_dev); | 
|  | 251 | dev_put(dev); | 
|  | 252 | if (dst->neighbour && dst->neighbour->dev == dev) { | 
|  | 253 | dst->neighbour->dev = &loopback_dev; | 
|  | 254 | dev_put(dev); | 
|  | 255 | dev_hold(&loopback_dev); | 
|  | 256 | } | 
|  | 257 | } | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr) | 
|  | 261 | { | 
|  | 262 | struct net_device *dev = ptr; | 
|  | 263 | struct dst_entry *dst; | 
|  | 264 |  | 
|  | 265 | switch (event) { | 
|  | 266 | case NETDEV_UNREGISTER: | 
|  | 267 | case NETDEV_DOWN: | 
|  | 268 | spin_lock_bh(&dst_lock); | 
|  | 269 | for (dst = dst_garbage_list; dst; dst = dst->next) { | 
|  | 270 | dst_ifdown(dst, dev, event != NETDEV_DOWN); | 
|  | 271 | } | 
|  | 272 | spin_unlock_bh(&dst_lock); | 
|  | 273 | break; | 
|  | 274 | } | 
|  | 275 | return NOTIFY_DONE; | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | static struct notifier_block dst_dev_notifier = { | 
|  | 279 | .notifier_call	= dst_dev_event, | 
|  | 280 | }; | 
|  | 281 |  | 
|  | 282 | void __init dst_init(void) | 
|  | 283 | { | 
|  | 284 | register_netdevice_notifier(&dst_dev_notifier); | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | EXPORT_SYMBOL(__dst_free); | 
|  | 288 | EXPORT_SYMBOL(dst_alloc); | 
|  | 289 | EXPORT_SYMBOL(dst_destroy); |