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.
6 * Generic TIME_WAIT sockets functions
8 * From code orinally in TCP
11 #include <linux/kernel.h>
12 #include <net/inet_hashtables.h>
13 #include <net/inet_timewait_sock.h>
16 /* Must be called with locally disabled BHs. */
17 static void __inet_twsk_kill(struct inet_timewait_sock
*tw
,
18 struct inet_hashinfo
*hashinfo
)
20 struct inet_bind_hashbucket
*bhead
;
21 struct inet_bind_bucket
*tb
;
22 /* Unlink from established hashes. */
23 rwlock_t
*lock
= inet_ehash_lockp(hashinfo
, tw
->tw_hash
);
26 if (hlist_unhashed(&tw
->tw_node
)) {
30 __hlist_del(&tw
->tw_node
);
31 sk_node_init(&tw
->tw_node
);
34 /* Disassociate with bind bucket. */
35 bhead
= &hashinfo
->bhash
[inet_bhashfn(tw
->tw_num
, hashinfo
->bhash_size
)];
36 spin_lock(&bhead
->lock
);
38 __hlist_del(&tw
->tw_bind_node
);
40 inet_bind_bucket_destroy(hashinfo
->bind_bucket_cachep
, tb
);
41 spin_unlock(&bhead
->lock
);
42 #ifdef SOCK_REFCNT_DEBUG
43 if (atomic_read(&tw
->tw_refcnt
) != 1) {
44 printk(KERN_DEBUG
"%s timewait_sock %p refcnt=%d\n",
45 tw
->tw_prot
->name
, tw
, atomic_read(&tw
->tw_refcnt
));
52 * Enter the time wait state. This is called with locally disabled BH.
53 * Essentially we whip up a timewait bucket, copy the relevant info into it
54 * from the SK, and mess with hash chains and list linkage.
56 void __inet_twsk_hashdance(struct inet_timewait_sock
*tw
, struct sock
*sk
,
57 struct inet_hashinfo
*hashinfo
)
59 const struct inet_sock
*inet
= inet_sk(sk
);
60 const struct inet_connection_sock
*icsk
= inet_csk(sk
);
61 struct inet_ehash_bucket
*ehead
= inet_ehash_bucket(hashinfo
, sk
->sk_hash
);
62 rwlock_t
*lock
= inet_ehash_lockp(hashinfo
, sk
->sk_hash
);
63 struct inet_bind_hashbucket
*bhead
;
64 /* Step 1: Put TW into bind hash. Original socket stays there too.
65 Note, that any socket with inet->num != 0 MUST be bound in
66 binding cache, even if it is closed.
68 bhead
= &hashinfo
->bhash
[inet_bhashfn(inet
->num
, hashinfo
->bhash_size
)];
69 spin_lock(&bhead
->lock
);
70 tw
->tw_tb
= icsk
->icsk_bind_hash
;
71 BUG_TRAP(icsk
->icsk_bind_hash
);
72 inet_twsk_add_bind_node(tw
, &tw
->tw_tb
->owners
);
73 spin_unlock(&bhead
->lock
);
77 /* Step 2: Remove SK from established hash. */
78 if (__sk_del_node_init(sk
))
79 sock_prot_dec_use(sk
->sk_prot
);
81 /* Step 3: Hash TW into TIMEWAIT chain. */
82 inet_twsk_add_node(tw
, &ehead
->twchain
);
83 atomic_inc(&tw
->tw_refcnt
);
88 EXPORT_SYMBOL_GPL(__inet_twsk_hashdance
);
90 struct inet_timewait_sock
*inet_twsk_alloc(const struct sock
*sk
, const int state
)
92 struct inet_timewait_sock
*tw
=
93 kmem_cache_alloc(sk
->sk_prot_creator
->twsk_prot
->twsk_slab
,
96 const struct inet_sock
*inet
= inet_sk(sk
);
98 /* Give us an identity. */
99 tw
->tw_daddr
= inet
->daddr
;
100 tw
->tw_rcv_saddr
= inet
->rcv_saddr
;
101 tw
->tw_bound_dev_if
= sk
->sk_bound_dev_if
;
102 tw
->tw_num
= inet
->num
;
103 tw
->tw_state
= TCP_TIME_WAIT
;
104 tw
->tw_substate
= state
;
105 tw
->tw_sport
= inet
->sport
;
106 tw
->tw_dport
= inet
->dport
;
107 tw
->tw_family
= sk
->sk_family
;
108 tw
->tw_reuse
= sk
->sk_reuse
;
109 tw
->tw_hash
= sk
->sk_hash
;
111 tw
->tw_prot
= sk
->sk_prot_creator
;
112 atomic_set(&tw
->tw_refcnt
, 1);
113 inet_twsk_dead_node_init(tw
);
114 __module_get(tw
->tw_prot
->owner
);
120 EXPORT_SYMBOL_GPL(inet_twsk_alloc
);
122 /* Returns non-zero if quota exceeded. */
123 static int inet_twdr_do_twkill_work(struct inet_timewait_death_row
*twdr
,
126 struct inet_timewait_sock
*tw
;
127 struct hlist_node
*node
;
131 /* NOTE: compare this to previous version where lock
132 * was released after detaching chain. It was racy,
133 * because tw buckets are scheduled in not serialized context
134 * in 2.3 (with netfilter), and with softnet it is common, because
135 * soft irqs are not sequenced.
140 inet_twsk_for_each_inmate(tw
, node
, &twdr
->cells
[slot
]) {
141 __inet_twsk_del_dead_node(tw
);
142 spin_unlock(&twdr
->death_lock
);
143 __inet_twsk_kill(tw
, twdr
->hashinfo
);
146 spin_lock(&twdr
->death_lock
);
147 if (killed
> INET_TWDR_TWKILL_QUOTA
) {
152 /* While we dropped twdr->death_lock, another cpu may have
153 * killed off the next TW bucket in the list, therefore
154 * do a fresh re-read of the hlist head node with the
155 * lock reacquired. We still use the hlist traversal
156 * macro in order to get the prefetches.
161 twdr
->tw_count
-= killed
;
162 NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITED
, killed
);
167 void inet_twdr_hangman(unsigned long data
)
169 struct inet_timewait_death_row
*twdr
;
170 int unsigned need_timer
;
172 twdr
= (struct inet_timewait_death_row
*)data
;
173 spin_lock(&twdr
->death_lock
);
175 if (twdr
->tw_count
== 0)
179 if (inet_twdr_do_twkill_work(twdr
, twdr
->slot
)) {
180 twdr
->thread_slots
|= (1 << twdr
->slot
);
181 schedule_work(&twdr
->twkill_work
);
184 /* We purged the entire slot, anything left? */
188 twdr
->slot
= ((twdr
->slot
+ 1) & (INET_TWDR_TWKILL_SLOTS
- 1));
190 mod_timer(&twdr
->tw_timer
, jiffies
+ twdr
->period
);
192 spin_unlock(&twdr
->death_lock
);
195 EXPORT_SYMBOL_GPL(inet_twdr_hangman
);
197 void inet_twdr_twkill_work(struct work_struct
*work
)
199 struct inet_timewait_death_row
*twdr
=
200 container_of(work
, struct inet_timewait_death_row
, twkill_work
);
203 BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS
- 1) >
204 (sizeof(twdr
->thread_slots
) * 8));
206 while (twdr
->thread_slots
) {
207 spin_lock_bh(&twdr
->death_lock
);
208 for (i
= 0; i
< INET_TWDR_TWKILL_SLOTS
; i
++) {
209 if (!(twdr
->thread_slots
& (1 << i
)))
212 while (inet_twdr_do_twkill_work(twdr
, i
) != 0) {
213 if (need_resched()) {
214 spin_unlock_bh(&twdr
->death_lock
);
216 spin_lock_bh(&twdr
->death_lock
);
220 twdr
->thread_slots
&= ~(1 << i
);
222 spin_unlock_bh(&twdr
->death_lock
);
226 EXPORT_SYMBOL_GPL(inet_twdr_twkill_work
);
228 /* These are always called from BH context. See callers in
229 * tcp_input.c to verify this.
232 /* This is for handling early-kills of TIME_WAIT sockets. */
233 void inet_twsk_deschedule(struct inet_timewait_sock
*tw
,
234 struct inet_timewait_death_row
*twdr
)
236 spin_lock(&twdr
->death_lock
);
237 if (inet_twsk_del_dead_node(tw
)) {
239 if (--twdr
->tw_count
== 0)
240 del_timer(&twdr
->tw_timer
);
242 spin_unlock(&twdr
->death_lock
);
243 __inet_twsk_kill(tw
, twdr
->hashinfo
);
246 EXPORT_SYMBOL(inet_twsk_deschedule
);
248 void inet_twsk_schedule(struct inet_timewait_sock
*tw
,
249 struct inet_timewait_death_row
*twdr
,
250 const int timeo
, const int timewait_len
)
252 struct hlist_head
*list
;
255 /* timeout := RTO * 3.5
257 * 3.5 = 1+2+0.5 to wait for two retransmits.
259 * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
260 * our ACK acking that FIN can be lost. If N subsequent retransmitted
261 * FINs (or previous seqments) are lost (probability of such event
262 * is p^(N+1), where p is probability to lose single packet and
263 * time to detect the loss is about RTO*(2^N - 1) with exponential
264 * backoff). Normal timewait length is calculated so, that we
265 * waited at least for one retransmitted FIN (maximal RTO is 120sec).
266 * [ BTW Linux. following BSD, violates this requirement waiting
267 * only for 60sec, we should wait at least for 240 secs.
268 * Well, 240 consumes too much of resources 8)
270 * This interval is not reduced to catch old duplicate and
271 * responces to our wandering segments living for two MSLs.
272 * However, if we use PAWS to detect
273 * old duplicates, we can reduce the interval to bounds required
274 * by RTO, rather than MSL. So, if peer understands PAWS, we
275 * kill tw bucket after 3.5*RTO (it is important that this number
276 * is greater than TS tick!) and detect old duplicates with help
279 slot
= (timeo
+ (1 << INET_TWDR_RECYCLE_TICK
) - 1) >> INET_TWDR_RECYCLE_TICK
;
281 spin_lock(&twdr
->death_lock
);
283 /* Unlink it, if it was scheduled */
284 if (inet_twsk_del_dead_node(tw
))
287 atomic_inc(&tw
->tw_refcnt
);
289 if (slot
>= INET_TWDR_RECYCLE_SLOTS
) {
290 /* Schedule to slow timer */
291 if (timeo
>= timewait_len
) {
292 slot
= INET_TWDR_TWKILL_SLOTS
- 1;
294 slot
= DIV_ROUND_UP(timeo
, twdr
->period
);
295 if (slot
>= INET_TWDR_TWKILL_SLOTS
)
296 slot
= INET_TWDR_TWKILL_SLOTS
- 1;
298 tw
->tw_ttd
= jiffies
+ timeo
;
299 slot
= (twdr
->slot
+ slot
) & (INET_TWDR_TWKILL_SLOTS
- 1);
300 list
= &twdr
->cells
[slot
];
302 tw
->tw_ttd
= jiffies
+ (slot
<< INET_TWDR_RECYCLE_TICK
);
304 if (twdr
->twcal_hand
< 0) {
305 twdr
->twcal_hand
= 0;
306 twdr
->twcal_jiffie
= jiffies
;
307 twdr
->twcal_timer
.expires
= twdr
->twcal_jiffie
+
308 (slot
<< INET_TWDR_RECYCLE_TICK
);
309 add_timer(&twdr
->twcal_timer
);
311 if (time_after(twdr
->twcal_timer
.expires
,
312 jiffies
+ (slot
<< INET_TWDR_RECYCLE_TICK
)))
313 mod_timer(&twdr
->twcal_timer
,
314 jiffies
+ (slot
<< INET_TWDR_RECYCLE_TICK
));
315 slot
= (twdr
->twcal_hand
+ slot
) & (INET_TWDR_RECYCLE_SLOTS
- 1);
317 list
= &twdr
->twcal_row
[slot
];
320 hlist_add_head(&tw
->tw_death_node
, list
);
322 if (twdr
->tw_count
++ == 0)
323 mod_timer(&twdr
->tw_timer
, jiffies
+ twdr
->period
);
324 spin_unlock(&twdr
->death_lock
);
327 EXPORT_SYMBOL_GPL(inet_twsk_schedule
);
329 void inet_twdr_twcal_tick(unsigned long data
)
331 struct inet_timewait_death_row
*twdr
;
334 unsigned long now
= jiffies
;
338 twdr
= (struct inet_timewait_death_row
*)data
;
340 spin_lock(&twdr
->death_lock
);
341 if (twdr
->twcal_hand
< 0)
344 slot
= twdr
->twcal_hand
;
345 j
= twdr
->twcal_jiffie
;
347 for (n
= 0; n
< INET_TWDR_RECYCLE_SLOTS
; n
++) {
348 if (time_before_eq(j
, now
)) {
349 struct hlist_node
*node
, *safe
;
350 struct inet_timewait_sock
*tw
;
352 inet_twsk_for_each_inmate_safe(tw
, node
, safe
,
353 &twdr
->twcal_row
[slot
]) {
354 __inet_twsk_del_dead_node(tw
);
355 __inet_twsk_kill(tw
, twdr
->hashinfo
);
362 twdr
->twcal_jiffie
= j
;
363 twdr
->twcal_hand
= slot
;
366 if (!hlist_empty(&twdr
->twcal_row
[slot
])) {
367 mod_timer(&twdr
->twcal_timer
, j
);
371 j
+= 1 << INET_TWDR_RECYCLE_TICK
;
372 slot
= (slot
+ 1) & (INET_TWDR_RECYCLE_SLOTS
- 1);
374 twdr
->twcal_hand
= -1;
377 if ((twdr
->tw_count
-= killed
) == 0)
378 del_timer(&twdr
->tw_timer
);
379 NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITKILLED
, killed
);
380 spin_unlock(&twdr
->death_lock
);
383 EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick
);
This page took 0.03805 seconds and 6 git commands to generate.