TCP层close系统调用的实现分析
在调用close系统调用关闭套接字时,如果套接字引用计数已经归零,则需继续向上层调用其close实现,tcp为tcp_close;本文仅介绍tcp部分,前置部分请参考本博关于close系统调用的文章;
void tcp_close(struct sock *sk, long timeout)
{
struct sk_buff *skb;
int data_was_unread = ;
int state; lock_sock(sk);
sk->sk_shutdown = SHUTDOWN_MASK; /* LISTEN状态处理 */
if (sk->sk_state == TCP_LISTEN) {
/* 设置close状态 */
tcp_set_state(sk, TCP_CLOSE); /* Special case. */
/* 清理完成连接队列 */
inet_csk_listen_stop(sk); goto adjudge_to_death;
} /* We need to flush the recv. buffs. We do this only on the
* descriptor close, not protocol-sourced closes, because the
* reader process may not have drained the data yet!
*/
/* 删除接收队列中用户进程未读取的skb */
while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
u32 len = TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq; /* 减去fin的一个序号长度 */
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
len--;
data_was_unread += len;
__kfree_skb(skb);
} sk_mem_reclaim(sk); /* If socket has been already reset (e.g. in tcp_reset()) - kill it. */
/* CLOSE状态 */
if (sk->sk_state == TCP_CLOSE)
goto adjudge_to_death; /* As outlined in RFC 2525, section 2.17, we send a RST here because
* data was lost. To witness the awful effects of the old behavior of
* always doing a FIN, run an older 2.1.x kernel or 2.0.x, start a bulk
* GET in an FTP client, suspend the process, wait for the client to
* advertise a zero window, then kill -9 the FTP client, wheee...
* Note: timeout is always zero in such a case.
*/
/* 修复状态,断开连接 */
if (unlikely(tcp_sk(sk)->repair)) {
sk->sk_prot->disconnect(sk, );
}
/* 用户进程有数据未读 */
else if (data_was_unread) {
/* Unread data was tossed, zap the connection. */
NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONCLOSE); /* 设置为close */
tcp_set_state(sk, TCP_CLOSE); /* 发送rst */
tcp_send_active_reset(sk, sk->sk_allocation);
}
/* lingertime==0,断开连接 */
else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
/* Check zero linger _after_ checking for unread data. */
sk->sk_prot->disconnect(sk, );
NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
}
/* 关闭状态转移 */
else if (tcp_close_state(sk)) {
/* We FIN if the application ate all the data before
* zapping the connection.
*/ /* RED-PEN. Formally speaking, we have broken TCP state
* machine. State transitions:
*
* TCP_ESTABLISHED -> TCP_FIN_WAIT1
* TCP_SYN_RECV -> TCP_FIN_WAIT1 (forget it, it's impossible)
* TCP_CLOSE_WAIT -> TCP_LAST_ACK
*
* are legal only when FIN has been sent (i.e. in window),
* rather than queued out of window. Purists blame.
*
* F.e. "RFC state" is ESTABLISHED,
* if Linux state is FIN-WAIT-1, but FIN is still not sent.
*
* The visible declinations are that sometimes
* we enter time-wait state, when it is not required really
* (harmless), do not send active resets, when they are
* required by specs (TCP_ESTABLISHED, TCP_CLOSE_WAIT, when
* they look as CLOSING or LAST_ACK for Linux)
* Probably, I missed some more holelets.
* --ANK
* XXX (TFO) - To start off we don't support SYN+ACK+FIN
* in a single packet! (May consider it later but will
* probably need API support or TCP_CORK SYN-ACK until
* data is written and socket is closed.)
*/
/* 发送fin */
tcp_send_fin(sk);
} /* 等待关闭,状态为FIN_WAIT_1 CLOSING LAST_ACK或sk_lingertime超时 */
sk_stream_wait_close(sk, timeout); adjudge_to_death:
state = sk->sk_state;
sock_hold(sk); /* 设置为DEAD状态 */
sock_orphan(sk); /* It is the last release_sock in its life. It will remove backlog. */
/* 删除控制块的backlog、cb */
release_sock(sk); /* Now socket is owned by kernel and we acquire BH lock
to finish close. No need to check for user refs.
*/
local_bh_disable();
bh_lock_sock(sk);
WARN_ON(sock_owned_by_user(sk)); /* 增加孤儿计数 */
percpu_counter_inc(sk->sk_prot->orphan_count); /* Have we already been destroyed by a softirq or backlog? */
/* 被软中断或者backlog销毁了????? */
if (state != TCP_CLOSE && sk->sk_state == TCP_CLOSE)
goto out; /* This is a (useful) BSD violating of the RFC. There is a
* problem with TCP as specified in that the other end could
* keep a socket open forever with no application left this end.
* We use a 1 minute timeout (about the same as BSD) then kill
* our end. If they send after that then tough - BUT: long enough
* that we won't make the old 4*rto = almost no time - whoops
* reset mistake.
*
* Nope, it was not mistake. It is really desired behaviour
* f.e. on http servers, when such sockets are useless, but
* consume significant resources. Let's do it with special
* linger2 option. --ANK
*/ if (sk->sk_state == TCP_FIN_WAIT2) {
struct tcp_sock *tp = tcp_sk(sk);
/* linger2小于0,无需等待 */
if (tp->linger2 < ) { /* 转到CLOSE */
tcp_set_state(sk, TCP_CLOSE);
/* 发送rst */
tcp_send_active_reset(sk, GFP_ATOMIC);
__NET_INC_STATS(sock_net(sk),
LINUX_MIB_TCPABORTONLINGER);
} else { /* 获取FIN_WAIT_2超时时间 */
const int tmo = tcp_fin_time(sk); /* FIN_WAIT_2超时时间> timewait时间,加FIN_WAIT_2定时器 */
if (tmo > TCP_TIMEWAIT_LEN) {
inet_csk_reset_keepalive_timer(sk,
tmo - TCP_TIMEWAIT_LEN);
}
/* 小于TIME_WAIT时间,则进入TIME_WAIT */
else {
tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
goto out;
}
}
} /* 未处于CLOSE */
if (sk->sk_state != TCP_CLOSE) {
sk_mem_reclaim(sk); /* 孤儿数量过多,或者socket内存过多 */
if (tcp_check_oom(sk, )) {
/* 进入CLOSE */
tcp_set_state(sk, TCP_CLOSE);
/* 发送rst */
tcp_send_active_reset(sk, GFP_ATOMIC);
__NET_INC_STATS(sock_net(sk),
LINUX_MIB_TCPABORTONMEMORY);
}
} /* 处于CLOSE */
if (sk->sk_state == TCP_CLOSE) {
struct request_sock *req = tcp_sk(sk)->fastopen_rsk;
/* We could get here with a non-NULL req if the socket is
* aborted (e.g., closed with unread data) before 3WHS
* finishes.
*/
if (req)
reqsk_fastopen_remove(sk, req, false); /* 销毁控制块 */
inet_csk_destroy_sock(sk);
}
/* Otherwise, socket is reprieved until protocol close. */ out:
bh_unlock_sock(sk);
local_bh_enable();
sock_put(sk);
}
TCP层close系统调用的实现分析的更多相关文章
- TCP层sendmsg系统调用的实现分析
		
概述 sendmsg系统调用在tcp层的实现是tcp_sendmsg函数,该函数完成以下任务:从用户空间读取数据,拷贝到内核skb,将skb加入到发送队列的任务,调用发送函数:函数在执行过程中会锁定控 ...
 - TCP层recvmsg系统调用的实现分析
		
概述 recvmsg系统调用在tcp层的实现是tcp_recvmsg函数,该函数完成从接收队列中读取数据复制到用户空间的任务:函数在执行过程中会锁定控制块,避免软中断在tcp层的影响:函数会涉及从接收 ...
 - TCP层shutdown系统调用的实现分析
		
概述 shutdown系统调用在tcp层会调用两个函数,对于ESTABLISHED状态需要调用tcp_shutdown关闭连接,对于LISTEN和SYN_SENT状态则需要以非阻塞模式调用tcp_di ...
 - TCP层accept系统调用的实现分析
		
inet_csk_accept函数实现了tcp协议accept操作,其主要完成的功能是,从已经完成三次握手的队列中取控制块,如果没有已经完成的连接,则需要根据阻塞标记来来区分对待,若非阻塞则直接返回, ...
 - TCP层bind系统调用的实现分析
		
说明:该文章中部分代码未能完全理解透彻,可能对您造成误解,请慎读: 并建议您先阅读本博另外一篇文章:<Linux TCP套接字选项 之 SO_REUSEADDR && SO_RE ...
 - Python Tornado框架(TCP层)
		
Tornado在TCP层里的工作机制 上一节是关于应用层的协议 HTTP,它依赖于传输层协议 TCP,例如服务器是如何绑定端口的?HTTP 服务器的 handle_stream 是在什么时候被调用的呢 ...
 - TCP拥塞控制算法 优缺点 适用环境 性能分析
		
[摘要]对多种TCP拥塞控制算法进行简要说明,指出它们的优缺点.以及它们的适用环境. [关键字]TCP拥塞控制算法 优点 缺点 适用环境公平性 公平性 公平性是在发生拥塞时各源端(或同一源端 ...
 - Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析
		
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6642463 在前面几篇文章中,我们详细介绍了A ...
 - TCP层的分段和IP层的分片之间的关系 & MTU和MSS之间的关系 (转载)
		
首先说明:数据报的分段和分片确实发生,分段发生在传输层,分片发生在网络层.但是对于分段来说,这是经常发生在UDP传输层协议上的情况,对于传输层使用TCP协议的通道来说,这种事情很少发生. 1,MTU( ...
 
随机推荐
- CSS3--transform相关属性
			
---transform属性使用--- 1.过度时间 :transition: transform 2s; 2.transform: 应用 2D 或 3D 转换.可以对元素进行旋转.缩放.移动或倾斜. ...
 - 小程序setData方法使用总结
			
做了一下小程序setData使用方法总结,如有错误,请不吝指出,Thanks♪(・ω・)ノ //示例data: data:{ user:'young', obj:{ name:'蓝色蒲公英', ag ...
 - JS可以做很多事情
			
JS可以做很多事情,例如: 使用JavaScript可以做很多事情,使网页更具互动性,并为网站用户提供更好.更令人兴奋的体验.JavaScript允许您创建一个活动的用户界面,当用户在页面之间导航时, ...
 - vbox的四种网络模式
			
一.NAT模式 特点: 1.如果主机可以上网,虚拟机可以上网 2.虚拟机之间不能ping通 3.虚拟机可以ping通主机(此时ping虚拟机的网关,即是ping主机) 4.主机不能ping通虚拟机 ...
 - 第十四章·Kibana深入-Timelion画图实现系统监控
			
什么是Timelion? Timelion使你可以轻松获得以下问题的答案: 1)随着时间的推移,每个唯一的用户会查看多少个页面?2)这个星期五和上周五之间的交通量有什么不同?3)今天有多少日本人口来到 ...
 - 复制SD启动卡 生成新启动卡
			
在已经有1张SD卡启动卡的情况下,如何复制出一张新卡: 1. 使用软件DiskGenius4.8.0->硬盘->备份分区表 备份 源sd卡分区信息 2. 使用软件DiskGenius4.8 ...
 - String类型为什么不可变
			
在学习Java的过程中,我们会被告知 String 被设计成不可变的类型.为什么 String 会被 Java 开发者有如此特殊的对待?他们的设计意图和设计理念到底是什么?因此,我带着以下三个问题,对 ...
 - python基础:python循环、三元运算、字典、文件操作
			
目录: python循环 三元运算 字符串 字典 文件操作基础 一.python编程 在面向过程式编程语言的执行流程中包含: 顺序执行 选择执行 循环执行 if是条件判断语句:if的执行流程属于选择执 ...
 - Linq表达树(固定参数)
			
这篇博客只能用来批判因为我刚刚学习linq对它了解只有简单的linq to sql 的语句所以来写这个博客只能说是班门弄斧了,看的下去的话就坚持看吧. 在网上看了别人的文章目前水平有限借鉴别人的思想吧 ...
 - shutdown immediate 持久无法关闭数据库之解决方案
			
问题引出:测试环境,进行oralce的shutdown immediate,等待时间很长,长的无法等待 ORACLE shutdown 过程: 1.shutdown normal(正常关闭方式):阻止 ...