Sungju's Slow Life

Personal journal


What is the meaning of the tcp_max_tw_buckets?

You can find the file named /proc/sys/net/ipv4/tcp_max_tw_buckets.
In the kernel-doc, it explained something like following messages.

tcp_max_tw_buckets – INTEGER
Maximal number of timewait sockets held by system simultaneously.
If this number is exceeded time-wait socket is immediately destroyed
and warning is printed. This limit exists only to prevent
simple DoS attacks, you _must_ not lower the limit artificially,
but rather increase it (probably, after increasing installed memory),
if network conditions require more than default value.

As it said on the doc, it is not a good idea to change this tunable parameter. But, if you want to reduce the TIME_WAIT state sockets, you can change this value to the small one.

Following is the code which check the bucket size and decide whether or not allocate TIME_WAIT state connection before close.

At the line number 282, it checks if maximum time_wait counts are reached. If it reached, do not allocate new memory block which will be used to keep some information for a while. From the line number 348, you can see that no works are done for unallocated case. It just displays the warning messages and finish the connection.

http://lxr.linux.no/linux+v2.6.24/net/ipv4/tcp_minisocks.c#L272

269/*
270 * Move a socket to time-wait or dead fin-wait-2 state.
271 */
272void tcp_time_wait(struct sock *sk, int state, int timeo)
273{
274 struct inet_timewait_sock *tw = NULL;
275 const struct inet_connection_sock *icsk = inet_csk(sk);
276 const struct tcp_sock *tp = tcp_sk(sk);
277 int recycle_ok = 0;
278
279 if (tcp_death_row.sysctl_tw_recycle && tp->rx_opt.ts_recent_stamp)
280 recycle_ok = icsk->icsk_af_ops->remember_stamp(sk);
281
282 if (tcp_death_row.tw_count icsk_rto <icsk_rto >> 1);
288
289 tw->tw_rcv_wscale = tp->rx_opt.rcv_wscale;
290 tcptw->tw_rcv_nxt = tp->rcv_nxt;
291 tcptw->tw_snd_nxt = tp->snd_nxt;
292 tcptw->tw_rcv_wnd = tcp_receive_window(tp);
293 tcptw->tw_ts_recent = tp->rx_opt.ts_recent;
294 tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp;
295
296#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
297 if (tw->tw_family == PF_INET6) {
298 struct ipv6_pinfo *np = inet6_sk(sk);
299 struct inet6_timewait_sock *tw6;
300
301 tw->tw_ipv6_offset = inet6_tw_offset(sk->sk_prot);
302 tw6 = inet6_twsk((struct sock *)tw);
303 ipv6_addr_copy(&tw6->tw_v6_daddr, &np->daddr);
304 ipv6_addr_copy(&tw6->tw_v6_rcv_saddr, &np->rcv_saddr);
305 tw->tw_ipv6only = np->ipv6only;
306 }
307#endif
308
309#ifdef CONFIG_TCP_MD5SIG
310 /*
311 * The timewait bucket does not have the key DB from the
312 * sock structure. We just make a quick copy of the
313 * md5 key being used (if indeed we are using one)
314 * so the timewait ack generating code has the key.
315 */
316 do {
317 struct tcp_md5sig_key *key;
318 memset(tcptw->tw_md5_key, 0, sizeof(tcptw->tw_md5_key));
319 tcptw->tw_md5_keylen = 0;
320 key = tp->af_specific->md5_lookup(sk, sk);
321 if (key != NULL) {
322 memcpy(&tcptw->tw_md5_key, key->key, key->keylen);
323 tcptw->tw_md5_keylen = key->keylen;
324 if (tcp_alloc_md5sig_pool() == NULL)
325 BUG();
326 }
327 } while (0);
328#endif
329
330 /* Linkage updates. */
331 __inet_twsk_hashdance(tw, sk, &tcp_hashinfo);
332
333 /* Get the TIME_WAIT timeout firing. */
334 if (timeo tw_timeout = rto;
339 } else {
340 tw->tw_timeout = TCP_TIMEWAIT_LEN;
341 if (state == TCP_TIME_WAIT)
342 timeo = TCP_TIMEWAIT_LEN;
343 }
344
345 inet_twsk_schedule(tw, &tcp_death_row, timeo,
346 TCP_TIMEWAIT_LEN);
347 inet_twsk_put(tw);
348 } else {
349 /* Sorry, if we're out of memory, just CLOSE this
350 * socket up. We've got bigger problems than
351 * non-graceful socket closings.
352 */
353 LIMIT_NETDEBUG(KERN_INFO "TCP: time wait bucket table overflown");
354 }
355
356 tcp_update_metrics(sk);
357 tcp_done(sk);
358}



Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About Me

A software engineer who loves any technologies that makes life easier. That’s why I love Linux and Mac at the same time.

Newsletter

%d bloggers like this: