Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 3 | * operating system. INET is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * Generic TIME_WAIT sockets functions |
| 7 | * |
| 8 | * From code orinally in TCP |
| 9 | */ |
| 10 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Vegard Nossum | 9e337b0 | 2008-10-18 17:37:51 +0200 | [diff] [blame] | 12 | #include <linux/kmemcheck.h> |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 13 | #include <net/inet_hashtables.h> |
| 14 | #include <net/inet_timewait_sock.h> |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 15 | #include <net/ip.h> |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 16 | |
| 17 | /* Must be called with locally disabled BHs. */ |
Adrian Bunk | acd159b | 2007-07-14 19:00:59 -0700 | [diff] [blame] | 18 | static void __inet_twsk_kill(struct inet_timewait_sock *tw, |
| 19 | struct inet_hashinfo *hashinfo) |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 20 | { |
| 21 | struct inet_bind_hashbucket *bhead; |
| 22 | struct inet_bind_bucket *tb; |
| 23 | /* Unlink from established hashes. */ |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 24 | spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 25 | |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 26 | spin_lock(lock); |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 27 | if (hlist_nulls_unhashed(&tw->tw_node)) { |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 28 | spin_unlock(lock); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 29 | return; |
| 30 | } |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 31 | hlist_nulls_del_rcu(&tw->tw_node); |
| 32 | sk_nulls_node_init(&tw->tw_node); |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 33 | spin_unlock(lock); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 34 | |
| 35 | /* Disassociate with bind bucket. */ |
Pavel Emelyanov | 7f635ab | 2008-06-16 17:12:49 -0700 | [diff] [blame] | 36 | bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num, |
| 37 | hashinfo->bhash_size)]; |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 38 | spin_lock(&bhead->lock); |
| 39 | tb = tw->tw_tb; |
| 40 | __hlist_del(&tw->tw_bind_node); |
| 41 | tw->tw_tb = NULL; |
| 42 | inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb); |
| 43 | spin_unlock(&bhead->lock); |
| 44 | #ifdef SOCK_REFCNT_DEBUG |
| 45 | if (atomic_read(&tw->tw_refcnt) != 1) { |
| 46 | printk(KERN_DEBUG "%s timewait_sock %p refcnt=%d\n", |
| 47 | tw->tw_prot->name, tw, atomic_read(&tw->tw_refcnt)); |
| 48 | } |
| 49 | #endif |
| 50 | inet_twsk_put(tw); |
| 51 | } |
| 52 | |
Arnaldo Carvalho de Melo | 4dbc8ef | 2009-05-06 16:50:52 -0700 | [diff] [blame] | 53 | static noinline void inet_twsk_free(struct inet_timewait_sock *tw) |
| 54 | { |
| 55 | struct module *owner = tw->tw_prot->owner; |
| 56 | twsk_destructor((struct sock *)tw); |
| 57 | #ifdef SOCK_REFCNT_DEBUG |
| 58 | pr_debug("%s timewait_sock %p released\n", tw->tw_prot->name, tw); |
| 59 | #endif |
| 60 | release_net(twsk_net(tw)); |
| 61 | kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw); |
| 62 | module_put(owner); |
| 63 | } |
| 64 | |
Pavel Emelyanov | 7054fb9 | 2007-12-20 15:32:54 -0800 | [diff] [blame] | 65 | void inet_twsk_put(struct inet_timewait_sock *tw) |
| 66 | { |
Arnaldo Carvalho de Melo | 4dbc8ef | 2009-05-06 16:50:52 -0700 | [diff] [blame] | 67 | if (atomic_dec_and_test(&tw->tw_refcnt)) |
| 68 | inet_twsk_free(tw); |
Pavel Emelyanov | 7054fb9 | 2007-12-20 15:32:54 -0800 | [diff] [blame] | 69 | } |
| 70 | EXPORT_SYMBOL_GPL(inet_twsk_put); |
| 71 | |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 72 | /* |
| 73 | * Enter the time wait state. This is called with locally disabled BH. |
| 74 | * Essentially we whip up a timewait bucket, copy the relevant info into it |
| 75 | * from the SK, and mess with hash chains and list linkage. |
| 76 | */ |
| 77 | void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk, |
| 78 | struct inet_hashinfo *hashinfo) |
| 79 | { |
| 80 | const struct inet_sock *inet = inet_sk(sk); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 81 | const struct inet_connection_sock *icsk = inet_csk(sk); |
Eric Dumazet | 81c3d54 | 2005-10-03 14:13:38 -0700 | [diff] [blame] | 82 | struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash); |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 83 | spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 84 | struct inet_bind_hashbucket *bhead; |
| 85 | /* Step 1: Put TW into bind hash. Original socket stays there too. |
| 86 | Note, that any socket with inet->num != 0 MUST be bound in |
| 87 | binding cache, even if it is closed. |
| 88 | */ |
Pavel Emelyanov | 7f635ab | 2008-06-16 17:12:49 -0700 | [diff] [blame] | 89 | bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->num, |
| 90 | hashinfo->bhash_size)]; |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 91 | spin_lock(&bhead->lock); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 92 | tw->tw_tb = icsk->icsk_bind_hash; |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 93 | WARN_ON(!icsk->icsk_bind_hash); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 94 | inet_twsk_add_bind_node(tw, &tw->tw_tb->owners); |
| 95 | spin_unlock(&bhead->lock); |
| 96 | |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 97 | spin_lock(lock); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 98 | |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 99 | /* |
| 100 | * Step 2: Hash TW into TIMEWAIT chain. |
| 101 | * Should be done before removing sk from established chain |
| 102 | * because readers are lockless and search established first. |
| 103 | */ |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 104 | atomic_inc(&tw->tw_refcnt); |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 105 | inet_twsk_add_node_rcu(tw, &ehead->twchain); |
| 106 | |
| 107 | /* Step 3: Remove SK from established hash. */ |
| 108 | if (__sk_nulls_del_node_init_rcu(sk)) |
| 109 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 110 | |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 111 | spin_unlock(lock); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 112 | } |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 113 | |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 114 | EXPORT_SYMBOL_GPL(__inet_twsk_hashdance); |
| 115 | |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 116 | struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state) |
| 117 | { |
Arnaldo Carvalho de Melo | 6d6ee43 | 2005-12-13 23:25:19 -0800 | [diff] [blame] | 118 | struct inet_timewait_sock *tw = |
| 119 | kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab, |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 120 | GFP_ATOMIC); |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 121 | if (tw != NULL) { |
| 122 | const struct inet_sock *inet = inet_sk(sk); |
| 123 | |
Vegard Nossum | 9e337b0 | 2008-10-18 17:37:51 +0200 | [diff] [blame] | 124 | kmemcheck_annotate_bitfield(tw, flags); |
| 125 | |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 126 | /* Give us an identity. */ |
| 127 | tw->tw_daddr = inet->daddr; |
| 128 | tw->tw_rcv_saddr = inet->rcv_saddr; |
| 129 | tw->tw_bound_dev_if = sk->sk_bound_dev_if; |
| 130 | tw->tw_num = inet->num; |
| 131 | tw->tw_state = TCP_TIME_WAIT; |
| 132 | tw->tw_substate = state; |
| 133 | tw->tw_sport = inet->sport; |
| 134 | tw->tw_dport = inet->dport; |
| 135 | tw->tw_family = sk->sk_family; |
| 136 | tw->tw_reuse = sk->sk_reuse; |
Eric Dumazet | 81c3d54 | 2005-10-03 14:13:38 -0700 | [diff] [blame] | 137 | tw->tw_hash = sk->sk_hash; |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 138 | tw->tw_ipv6only = 0; |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 139 | tw->tw_transparent = inet->transparent; |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 140 | tw->tw_prot = sk->sk_prot_creator; |
Denis V. Lunev | cd5342d | 2008-04-16 02:00:28 -0700 | [diff] [blame] | 141 | twsk_net_set(tw, hold_net(sock_net(sk))); |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 142 | atomic_set(&tw->tw_refcnt, 1); |
| 143 | inet_twsk_dead_node_init(tw); |
Arnaldo Carvalho de Melo | eeb2b85 | 2005-10-10 21:25:23 -0700 | [diff] [blame] | 144 | __module_get(tw->tw_prot->owner); |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return tw; |
| 148 | } |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 149 | |
| 150 | EXPORT_SYMBOL_GPL(inet_twsk_alloc); |
| 151 | |
| 152 | /* Returns non-zero if quota exceeded. */ |
| 153 | static int inet_twdr_do_twkill_work(struct inet_timewait_death_row *twdr, |
| 154 | const int slot) |
| 155 | { |
| 156 | struct inet_timewait_sock *tw; |
| 157 | struct hlist_node *node; |
| 158 | unsigned int killed; |
| 159 | int ret; |
| 160 | |
| 161 | /* NOTE: compare this to previous version where lock |
| 162 | * was released after detaching chain. It was racy, |
| 163 | * because tw buckets are scheduled in not serialized context |
| 164 | * in 2.3 (with netfilter), and with softnet it is common, because |
| 165 | * soft irqs are not sequenced. |
| 166 | */ |
| 167 | killed = 0; |
| 168 | ret = 0; |
| 169 | rescan: |
| 170 | inet_twsk_for_each_inmate(tw, node, &twdr->cells[slot]) { |
| 171 | __inet_twsk_del_dead_node(tw); |
| 172 | spin_unlock(&twdr->death_lock); |
| 173 | __inet_twsk_kill(tw, twdr->hashinfo); |
Pavel Emelyanov | f2bf415 | 2008-07-16 20:32:25 -0700 | [diff] [blame] | 174 | #ifdef CONFIG_NET_NS |
| 175 | NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITED); |
| 176 | #endif |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 177 | inet_twsk_put(tw); |
| 178 | killed++; |
| 179 | spin_lock(&twdr->death_lock); |
| 180 | if (killed > INET_TWDR_TWKILL_QUOTA) { |
| 181 | ret = 1; |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | /* While we dropped twdr->death_lock, another cpu may have |
| 186 | * killed off the next TW bucket in the list, therefore |
| 187 | * do a fresh re-read of the hlist head node with the |
| 188 | * lock reacquired. We still use the hlist traversal |
| 189 | * macro in order to get the prefetches. |
| 190 | */ |
| 191 | goto rescan; |
| 192 | } |
| 193 | |
| 194 | twdr->tw_count -= killed; |
Pavel Emelyanov | f2bf415 | 2008-07-16 20:32:25 -0700 | [diff] [blame] | 195 | #ifndef CONFIG_NET_NS |
| 196 | NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITED, killed); |
| 197 | #endif |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | void inet_twdr_hangman(unsigned long data) |
| 202 | { |
| 203 | struct inet_timewait_death_row *twdr; |
| 204 | int unsigned need_timer; |
| 205 | |
| 206 | twdr = (struct inet_timewait_death_row *)data; |
| 207 | spin_lock(&twdr->death_lock); |
| 208 | |
| 209 | if (twdr->tw_count == 0) |
| 210 | goto out; |
| 211 | |
| 212 | need_timer = 0; |
| 213 | if (inet_twdr_do_twkill_work(twdr, twdr->slot)) { |
| 214 | twdr->thread_slots |= (1 << twdr->slot); |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 215 | schedule_work(&twdr->twkill_work); |
| 216 | need_timer = 1; |
| 217 | } else { |
| 218 | /* We purged the entire slot, anything left? */ |
| 219 | if (twdr->tw_count) |
| 220 | need_timer = 1; |
Octavian Purdila | 80a1096 | 2009-08-29 00:00:35 -0700 | [diff] [blame] | 221 | twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1)); |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 222 | } |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 223 | if (need_timer) |
| 224 | mod_timer(&twdr->tw_timer, jiffies + twdr->period); |
| 225 | out: |
| 226 | spin_unlock(&twdr->death_lock); |
| 227 | } |
| 228 | |
| 229 | EXPORT_SYMBOL_GPL(inet_twdr_hangman); |
| 230 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 231 | void inet_twdr_twkill_work(struct work_struct *work) |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 232 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 233 | struct inet_timewait_death_row *twdr = |
| 234 | container_of(work, struct inet_timewait_death_row, twkill_work); |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 235 | int i; |
| 236 | |
Pavel Emelyanov | 95c9382 | 2007-12-11 02:12:36 -0800 | [diff] [blame] | 237 | BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) > |
| 238 | (sizeof(twdr->thread_slots) * 8)); |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 239 | |
| 240 | while (twdr->thread_slots) { |
| 241 | spin_lock_bh(&twdr->death_lock); |
| 242 | for (i = 0; i < INET_TWDR_TWKILL_SLOTS; i++) { |
| 243 | if (!(twdr->thread_slots & (1 << i))) |
| 244 | continue; |
| 245 | |
| 246 | while (inet_twdr_do_twkill_work(twdr, i) != 0) { |
| 247 | if (need_resched()) { |
| 248 | spin_unlock_bh(&twdr->death_lock); |
| 249 | schedule(); |
| 250 | spin_lock_bh(&twdr->death_lock); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | twdr->thread_slots &= ~(1 << i); |
| 255 | } |
| 256 | spin_unlock_bh(&twdr->death_lock); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | EXPORT_SYMBOL_GPL(inet_twdr_twkill_work); |
| 261 | |
| 262 | /* These are always called from BH context. See callers in |
| 263 | * tcp_input.c to verify this. |
| 264 | */ |
| 265 | |
| 266 | /* This is for handling early-kills of TIME_WAIT sockets. */ |
| 267 | void inet_twsk_deschedule(struct inet_timewait_sock *tw, |
| 268 | struct inet_timewait_death_row *twdr) |
| 269 | { |
| 270 | spin_lock(&twdr->death_lock); |
| 271 | if (inet_twsk_del_dead_node(tw)) { |
| 272 | inet_twsk_put(tw); |
| 273 | if (--twdr->tw_count == 0) |
| 274 | del_timer(&twdr->tw_timer); |
| 275 | } |
| 276 | spin_unlock(&twdr->death_lock); |
| 277 | __inet_twsk_kill(tw, twdr->hashinfo); |
| 278 | } |
| 279 | |
| 280 | EXPORT_SYMBOL(inet_twsk_deschedule); |
| 281 | |
| 282 | void inet_twsk_schedule(struct inet_timewait_sock *tw, |
| 283 | struct inet_timewait_death_row *twdr, |
| 284 | const int timeo, const int timewait_len) |
| 285 | { |
| 286 | struct hlist_head *list; |
| 287 | int slot; |
| 288 | |
| 289 | /* timeout := RTO * 3.5 |
| 290 | * |
| 291 | * 3.5 = 1+2+0.5 to wait for two retransmits. |
| 292 | * |
| 293 | * RATIONALE: if FIN arrived and we entered TIME-WAIT state, |
| 294 | * our ACK acking that FIN can be lost. If N subsequent retransmitted |
| 295 | * FINs (or previous seqments) are lost (probability of such event |
| 296 | * is p^(N+1), where p is probability to lose single packet and |
| 297 | * time to detect the loss is about RTO*(2^N - 1) with exponential |
| 298 | * backoff). Normal timewait length is calculated so, that we |
| 299 | * waited at least for one retransmitted FIN (maximal RTO is 120sec). |
| 300 | * [ BTW Linux. following BSD, violates this requirement waiting |
| 301 | * only for 60sec, we should wait at least for 240 secs. |
| 302 | * Well, 240 consumes too much of resources 8) |
| 303 | * ] |
| 304 | * This interval is not reduced to catch old duplicate and |
| 305 | * responces to our wandering segments living for two MSLs. |
| 306 | * However, if we use PAWS to detect |
| 307 | * old duplicates, we can reduce the interval to bounds required |
| 308 | * by RTO, rather than MSL. So, if peer understands PAWS, we |
| 309 | * kill tw bucket after 3.5*RTO (it is important that this number |
| 310 | * is greater than TS tick!) and detect old duplicates with help |
| 311 | * of PAWS. |
| 312 | */ |
| 313 | slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK; |
| 314 | |
| 315 | spin_lock(&twdr->death_lock); |
| 316 | |
| 317 | /* Unlink it, if it was scheduled */ |
| 318 | if (inet_twsk_del_dead_node(tw)) |
| 319 | twdr->tw_count--; |
| 320 | else |
| 321 | atomic_inc(&tw->tw_refcnt); |
| 322 | |
| 323 | if (slot >= INET_TWDR_RECYCLE_SLOTS) { |
| 324 | /* Schedule to slow timer */ |
| 325 | if (timeo >= timewait_len) { |
| 326 | slot = INET_TWDR_TWKILL_SLOTS - 1; |
| 327 | } else { |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 328 | slot = DIV_ROUND_UP(timeo, twdr->period); |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 329 | if (slot >= INET_TWDR_TWKILL_SLOTS) |
| 330 | slot = INET_TWDR_TWKILL_SLOTS - 1; |
| 331 | } |
| 332 | tw->tw_ttd = jiffies + timeo; |
| 333 | slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1); |
| 334 | list = &twdr->cells[slot]; |
| 335 | } else { |
| 336 | tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK); |
| 337 | |
| 338 | if (twdr->twcal_hand < 0) { |
| 339 | twdr->twcal_hand = 0; |
| 340 | twdr->twcal_jiffie = jiffies; |
| 341 | twdr->twcal_timer.expires = twdr->twcal_jiffie + |
| 342 | (slot << INET_TWDR_RECYCLE_TICK); |
| 343 | add_timer(&twdr->twcal_timer); |
| 344 | } else { |
| 345 | if (time_after(twdr->twcal_timer.expires, |
| 346 | jiffies + (slot << INET_TWDR_RECYCLE_TICK))) |
| 347 | mod_timer(&twdr->twcal_timer, |
| 348 | jiffies + (slot << INET_TWDR_RECYCLE_TICK)); |
| 349 | slot = (twdr->twcal_hand + slot) & (INET_TWDR_RECYCLE_SLOTS - 1); |
| 350 | } |
| 351 | list = &twdr->twcal_row[slot]; |
| 352 | } |
| 353 | |
| 354 | hlist_add_head(&tw->tw_death_node, list); |
| 355 | |
| 356 | if (twdr->tw_count++ == 0) |
| 357 | mod_timer(&twdr->tw_timer, jiffies + twdr->period); |
| 358 | spin_unlock(&twdr->death_lock); |
| 359 | } |
| 360 | |
| 361 | EXPORT_SYMBOL_GPL(inet_twsk_schedule); |
| 362 | |
| 363 | void inet_twdr_twcal_tick(unsigned long data) |
| 364 | { |
| 365 | struct inet_timewait_death_row *twdr; |
| 366 | int n, slot; |
| 367 | unsigned long j; |
| 368 | unsigned long now = jiffies; |
| 369 | int killed = 0; |
| 370 | int adv = 0; |
| 371 | |
| 372 | twdr = (struct inet_timewait_death_row *)data; |
| 373 | |
| 374 | spin_lock(&twdr->death_lock); |
| 375 | if (twdr->twcal_hand < 0) |
| 376 | goto out; |
| 377 | |
| 378 | slot = twdr->twcal_hand; |
| 379 | j = twdr->twcal_jiffie; |
| 380 | |
| 381 | for (n = 0; n < INET_TWDR_RECYCLE_SLOTS; n++) { |
| 382 | if (time_before_eq(j, now)) { |
| 383 | struct hlist_node *node, *safe; |
| 384 | struct inet_timewait_sock *tw; |
| 385 | |
| 386 | inet_twsk_for_each_inmate_safe(tw, node, safe, |
| 387 | &twdr->twcal_row[slot]) { |
| 388 | __inet_twsk_del_dead_node(tw); |
| 389 | __inet_twsk_kill(tw, twdr->hashinfo); |
Pavel Emelyanov | f2bf415 | 2008-07-16 20:32:25 -0700 | [diff] [blame] | 390 | #ifdef CONFIG_NET_NS |
| 391 | NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED); |
| 392 | #endif |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 393 | inet_twsk_put(tw); |
| 394 | killed++; |
| 395 | } |
| 396 | } else { |
| 397 | if (!adv) { |
| 398 | adv = 1; |
| 399 | twdr->twcal_jiffie = j; |
| 400 | twdr->twcal_hand = slot; |
| 401 | } |
| 402 | |
| 403 | if (!hlist_empty(&twdr->twcal_row[slot])) { |
| 404 | mod_timer(&twdr->twcal_timer, j); |
| 405 | goto out; |
| 406 | } |
| 407 | } |
| 408 | j += 1 << INET_TWDR_RECYCLE_TICK; |
| 409 | slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1); |
| 410 | } |
| 411 | twdr->twcal_hand = -1; |
| 412 | |
| 413 | out: |
| 414 | if ((twdr->tw_count -= killed) == 0) |
| 415 | del_timer(&twdr->tw_timer); |
Pavel Emelyanov | f2bf415 | 2008-07-16 20:32:25 -0700 | [diff] [blame] | 416 | #ifndef CONFIG_NET_NS |
| 417 | NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITKILLED, killed); |
| 418 | #endif |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 419 | spin_unlock(&twdr->death_lock); |
| 420 | } |
| 421 | |
| 422 | EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick); |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 423 | |
| 424 | void inet_twsk_purge(struct net *net, struct inet_hashinfo *hashinfo, |
| 425 | struct inet_timewait_death_row *twdr, int family) |
| 426 | { |
| 427 | struct inet_timewait_sock *tw; |
| 428 | struct sock *sk; |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 429 | struct hlist_nulls_node *node; |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 430 | int h; |
| 431 | |
| 432 | local_bh_disable(); |
| 433 | for (h = 0; h < (hashinfo->ehash_size); h++) { |
| 434 | struct inet_ehash_bucket *head = |
| 435 | inet_ehash_bucket(hashinfo, h); |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 436 | spinlock_t *lock = inet_ehash_lockp(hashinfo, h); |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 437 | restart: |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 438 | spin_lock(lock); |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 439 | sk_nulls_for_each(sk, node, &head->twchain) { |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 440 | |
| 441 | tw = inet_twsk(sk); |
| 442 | if (!net_eq(twsk_net(tw), net) || |
| 443 | tw->tw_family != family) |
| 444 | continue; |
| 445 | |
| 446 | atomic_inc(&tw->tw_refcnt); |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 447 | spin_unlock(lock); |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 448 | inet_twsk_deschedule(tw, twdr); |
| 449 | inet_twsk_put(tw); |
| 450 | |
| 451 | goto restart; |
| 452 | } |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 453 | spin_unlock(lock); |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 454 | } |
| 455 | local_bh_enable(); |
| 456 | } |
| 457 | EXPORT_SYMBOL_GPL(inet_twsk_purge); |