• linux网络协议栈源码分析 - 传输层(TCP连接的终止)


    1、连接终止过程(四处挥手)

    1.1、四次挥手

            《TCP/IP详解卷 1:协议》“图18-4 连接终止期间报文段的正常交换”如下:

     1.2、tcp状态变迁图

    2、close系统调用(第一次挥手)

            客户端端调用close,发送FIN报文。

    2.1、close系统调用

            客户端调用close关闭socket,最终调用fput创建一个工作work来执行真正的关闭操作,fput函数代码如下:

    1. void fput(struct file *file)
    2. {
    3. if (atomic_long_dec_and_test(&file->f_count)) {
    4. struct task_struct *task = current;
    5. if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
    6. init_task_work(&file->f_u.fu_rcuhead, ____fput); // 初始化一个work,work的函数为____fput
    7. if (!task_work_add(task, &file->f_u.fu_rcuhead, true)) // 添加work task
    8. return; // 添加成功,返回
    9. /*
    10. * After this task has run exit_task_work(),
    11. * task_work_add() will fail. Fall through to delayed
    12. * fput to avoid leaking *file.
    13. */
    14. }
    15. if (llist_add(&file->f_u.fu_llist, &delayed_fput_list))
    16. schedule_delayed_work(&delayed_fput_work, 1);
    17. }
    18. }

            fput函数调用栈:

     2.2、tcp_close关闭socket并发送FIN(第一次挥手)

            fput系统调用添加一个work task之后,系统调用返回时处理挂起的work,调用____fput,最终调用tcp_close关闭socket。

            tcp_close检查各种状态,删除接收队列数据,有数据未接收,直接关闭socket发送RST;正常关闭以及其他需要发送FIN报文的状态,发送FIN,调用sk_stream_wait_close等待关闭socket(某些状态不能立即关闭socket,所以要等待);正常关闭,收到FIN的ACK,进入TCP_FIN_WAIT2状态,c需要启动定时器,避免服务器长时间不调用close,TCP_FIN_WAIT2状态没有数据要发送,不能通过超时重传判断网络异常,另外TCP_FIN_WAIT2状态超时时间太短的话,会直接关闭socket,由timewait控制块替换socket控制块,所以,书籍上的tcp状态转移图与linux内核实现不完全相同,TCP_FIN_WAIT2超时时间够长的情况,就能基本完全匹配。

            tcp_close函数代码如下:

    1. void tcp_close(struct sock *sk, long timeout)
    2. {
    3. struct sk_buff *skb;
    4. int data_was_unread = 0;
    5. int state;
    6. lock_sock(sk);
    7. sk->sk_shutdown = SHUTDOWN_MASK; // 关闭读写(SHUTDOWN_MASK = RCV_SHUTDOWN | SEND_SHUTDOWN);读写关闭状态,如果使用epoll等待该socket的话,调用tcp_poll会返回POLLHUP!!!
    8. if (sk->sk_state == TCP_LISTEN) { // 关闭LISTEN的socket
    9. tcp_set_state(sk, TCP_CLOSE); // 设置为CLOSE
    10. /* Special case. */
    11. inet_csk_listen_stop(sk); // 停止监听(具体参考机械工业出版社《Linux内核源码剖析:TCP/IP实现(下册)》"32.3 close传输接口层的实现: tcp_close()")
    12. goto adjudge_to_death; // 跳转到adjudge_to_death
    13. }
    14. /* We need to flush the recv. buffs. We do this only on the
    15. * descriptor close, not protocol-sourced closes, because the
    16. * reader process may not have drained the data yet!
    17. */
    18. while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { // tcp接收队列有数据,则删除接收队列的数据
    19. u32 len = TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq; // skb报文长度
    20. if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) // FIN报文(FIN报文占一个序号,但是FIN报文没有实际数据)
    21. len--; // 删除的数据减1(FIN序号不带数据)
    22. data_was_unread += len; // 删除的没有读的数据总长度
    23. __kfree_skb(skb); // 释放skb报文
    24. }
    25. sk_mem_reclaim(sk); // 回收缓存
    26. /* If socket has been already reset (e.g. in tcp_reset()) - kill it. */
    27. if (sk->sk_state == TCP_CLOSE)
    28. goto adjudge_to_death;
    29. /* As outlined in RFC 2525, section 2.17, we send a RST here because
    30. * data was lost. To witness the awful effects of the old behavior of
    31. * always doing a FIN, run an older 2.1.x kernel or 2.0.x, start a bulk
    32. * GET in an FTP client, suspend the process, wait for the client to
    33. * advertise a zero window, then kill -9 the FTP client, wheee...
    34. * Note: timeout is always zero in such a case.
    35. */
    36. if (unlikely(tcp_sk(sk)->repair)) {
    37. sk->sk_prot->disconnect(sk, 0);
    38. } else if (data_was_unread) { // 关闭socket的时候,有未接收的数据
    39. /* Unread data was tossed, zap the connection. */
    40. NET_INC_STATS_USER(sock_net(sk), LINUX_MIB_TCPABORTONCLOSE);
    41. tcp_set_state(sk, TCP_CLOSE); // 直接设置为CLOSE状态
    42. tcp_send_active_reset(sk, sk->sk_allocation); // 发送RST重置连接
    43. } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
    44. /* Check zero linger _after_ checking for unread data. */
    45. sk->sk_prot->disconnect(sk, 0);
    46. NET_INC_STATS_USER(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
    47. } else if (tcp_close_state(sk)) { // 调用tcp_close_state,从当前状态转换到下一个状态(正常关闭情况下,进入TCP_FIN_WAIT1状态,TCP_FIN_WAIT1需要发送FIN)
    48. /* We FIN if the application ate all the data before
    49. * zapping the connection.
    50. */
    51. /* RED-PEN. Formally speaking, we have broken TCP state
    52. * machine. State transitions:
    53. *
    54. * TCP_ESTABLISHED -> TCP_FIN_WAIT1
    55. * TCP_SYN_RECV -> TCP_FIN_WAIT1 (forget it, it's impossible)
    56. * TCP_CLOSE_WAIT -> TCP_LAST_ACK
    57. *
    58. * are legal only when FIN has been sent (i.e. in window),
    59. * rather than queued out of window. Purists blame.
    60. *
    61. * F.e. "RFC state" is ESTABLISHED,
    62. * if Linux state is FIN-WAIT-1, but FIN is still not sent.
    63. *
    64. * The visible declinations are that sometimes
    65. * we enter time-wait state, when it is not required really
    66. * (harmless), do not send active resets, when they are
    67. * required by specs (TCP_ESTABLISHED, TCP_CLOSE_WAIT, when
    68. * they look as CLOSING or LAST_ACK for Linux)
    69. * Probably, I missed some more holelets.
    70. * --ANK
    71. * XXX (TFO) - To start off we don't support SYN+ACK+FIN
    72. * in a single packet! (May consider it later but will
    73. * probably need API support or TCP_CORK SYN-ACK until
    74. * data is written and socket is closed.)
    75. */
    76. tcp_send_fin(sk); // 调用tcp_send_fin发送FIN报文(调用tcp_queue_skb将FIN报文缓存到发送队列(如果发送队列还有未确认的数据,那么需要等待其他数据发送完再发送FIN报文,FIN报文虽然不一定有数据(可以附加到最后一个带数据的报文),但是FIN可能丢失,FIN报文需要超时重传),调用__tcp_push_pending_frames发送挂起的报文)
    77. }
    78. sk_stream_wait_close(sk, timeout); // 等待socket关闭(查看sk_stream_closing函数,等待TCPF_FIN_WAIT1/TCPF_CLOSING/TCPF_LAST_ACK状态或者超时)
    79. adjudge_to_death:
    80. state = sk->sk_state;
    81. sock_hold(sk);
    82. sock_orphan(sk); // 设置套接口为孤儿套接口并且设置SOCK_DEAD标志
    83. /* It is the last release_sock in its life. It will remove backlog. */
    84. release_sock(sk); // 删除backlog队列并唤醒其他等待socket的线程
    85. /* Now socket is owned by kernel and we acquire BH lock
    86. to finish close. No need to check for user refs.
    87. */
    88. local_bh_disable();
    89. bh_lock_sock(sk);
    90. WARN_ON(sock_owned_by_user(sk));
    91. percpu_counter_inc(sk->sk_prot->orphan_count);
    92. /* Have we already been destroyed by a softirq or backlog? */
    93. if (state != TCP_CLOSE && sk->sk_state == TCP_CLOSE) // socket已经被关闭,跳转到out,不处理
    94. goto out;
    95. /* This is a (useful) BSD violating of the RFC. There is a
    96. * problem with TCP as specified in that the other end could
    97. * keep a socket open forever with no application left this end.
    98. * We use a 1 minute timeout (about the same as BSD) then kill
    99. * our end. If they send after that then tough - BUT: long enough
    100. * that we won't make the old 4*rto = almost no time - whoops
    101. * reset mistake.
    102. *
    103. * Nope, it was not mistake. It is really desired behaviour
    104. * f.e. on http servers, when such sockets are useless, but
    105. * consume significant resources. Let's do it with special
    106. * linger2 option. --ANK
    107. */
    108. if (sk->sk_state == TCP_FIN_WAIT2) { // TCP_FIN_WAIT2状态(已收到FIN的ACK,等待服务器调用close发送FIN)
    109. struct tcp_sock *tp = tcp_sk(sk);
    110. if (tp->linger2 < 0) {
    111. tcp_set_state(sk, TCP_CLOSE);
    112. tcp_send_active_reset(sk, GFP_ATOMIC);
    113. NET_INC_STATS_BH(sock_net(sk),
    114. LINUX_MIB_TCPABORTONLINGER);
    115. } else {
    116. const int tmo = tcp_fin_time(sk); // TCP_FIN_WAIT2状态超时时间(本地已经关闭了读写,已经没有任何数据要发送了,如果不启动超时定时器并且服务器不调用close的话,那么TCP_FIN_WAIT2将不会结束)
    117. if (tmo > TCP_TIMEWAIT_LEN) { // 大于60秒,使用TCP_FIN_WAIT2定时器处理TCP_FIN_WAIT2状态,超时之后,tcp_keepalive_timer直接发送RST然后强制关闭socket
    118. inet_csk_reset_keepalive_timer(sk,
    119. tmo - TCP_TIMEWAIT_LEN);
    120. } else { // 小于等于60秒
    121. tcp_time_wait(sk, TCP_FIN_WAIT2, tmo); // 调用tcp_time_wait处理,由timewait控制块替代socket控制块(将timewait添加到哈希表,从哈希表删除sk,输入报文将由timewait控制块接收),调用tcp_done将sk状态设置为TCP_CLOSE状态,timewait控制块tw_state处于TCP_TIME_WAIT状态,启动超时定时器,超时之后调用tw_timer_handler释放timewait控制块
    122. goto out;
    123. }
    124. }
    125. }
    126. if (sk->sk_state != TCP_CLOSE) { // 未处于TCP_CLOSE状态(TCP_FIN_WAIT2等状态,或者等待超时了...)
    127. sk_mem_reclaim(sk);
    128. if (tcp_check_oom(sk, 0)) { // 孤儿套接口、发送缓存等超限(具体参考机械工业出版社《Linux内核源码剖析:TCP/IP实现(下册)》P999),不等待服务器,强制关闭socket、发送RST给服务器
    129. tcp_set_state(sk, TCP_CLOSE); // 关闭socket
    130. tcp_send_active_reset(sk, GFP_ATOMIC); // 发送RST
    131. NET_INC_STATS_BH(sock_net(sk),
    132. LINUX_MIB_TCPABORTONMEMORY);
    133. }
    134. }
    135. if (sk->sk_state == TCP_CLOSE) {
    136. struct request_sock *req = tcp_sk(sk)->fastopen_rsk;
    137. /* We could get here with a non-NULL req if the socket is
    138. * aborted (e.g., closed with unread data) before 3WHS
    139. * finishes.
    140. */
    141. if (req)
    142. reqsk_fastopen_remove(sk, req, false);
    143. inet_csk_destroy_sock(sk);
    144. }
    145. /* Otherwise, socket is reprieved until protocol close. */
    146. out:
    147. bh_unlock_sock(sk);
    148. local_bh_enable();
    149. sock_put(sk);
    150. }

            tcp_send_fin函数调用栈:

     (调试过程,可以修改/proc/sys/net/ipv4/tcp_fin_timeout,增加tcp_fin_timeout,避免TCP_FIN_WAIT2状态直接关闭socket!!!)

    3、服务器收到FIN发送ACK(第二次挥手)

    3.1、服务器端收到处理FIN报文

            服务器在TCP_ESTABLISHED状态下收到报文调用tcp_rcv_established处理收到的报文,如果收到预期的报文(非乱序的报文),那么调用tcp_data_queue将数据接收到接收队列,并检查该报文是否有FIN标志,如果有,就调用tcp_fin关闭接收(客户端已经关闭了发送,所以客户端也不应该再接收数据了)、状态迁移到TCP_CLOSE_WAIT、删除乱序的数据(FIN之后的乱序数据不接收,这些乱序数据基本上应该就是错误的数据)并唤醒其他等待socket的线程。

            tcp_fin函数代码实现:

    1. static void tcp_fin(struct sock *sk)
    2. {
    3. struct tcp_sock *tp = tcp_sk(sk);
    4. inet_csk_schedule_ack(sk);
    5. sk->sk_shutdown |= RCV_SHUTDOWN; // 关闭接收
    6. sock_set_flag(sk, SOCK_DONE);
    7. switch (sk->sk_state) {
    8. case TCP_SYN_RECV:
    9. case TCP_ESTABLISHED:
    10. /* Move to CLOSE_WAIT */
    11. tcp_set_state(sk, TCP_CLOSE_WAIT); // 迁移到TCP_CLOSE_WAIT状态,等待调用close
    12. inet_csk(sk)->icsk_ack.pingpong = 1;
    13. break;
    14. case TCP_CLOSE_WAIT:
    15. case TCP_CLOSING:
    16. /* Received a retransmission of the FIN, do
    17. * nothing.
    18. */
    19. break;
    20. case TCP_LAST_ACK:
    21. /* RFC793: Remain in the LAST-ACK state. */
    22. break;
    23. case TCP_FIN_WAIT1:
    24. /* This case occurs when a simultaneous close
    25. * happens, we must ack the received FIN and
    26. * enter the CLOSING state.
    27. */
    28. tcp_send_ack(sk);
    29. tcp_set_state(sk, TCP_CLOSING);
    30. break;
    31. case TCP_FIN_WAIT2:
    32. /* Received a FIN -- send ACK and enter TIME_WAIT. */
    33. tcp_send_ack(sk);
    34. tcp_time_wait(sk, TCP_TIME_WAIT, 0);
    35. break;
    36. default:
    37. /* Only TCP_LISTEN and TCP_CLOSE are left, in these
    38. * cases we should never reach this piece of code.
    39. */
    40. pr_err("%s: Impossible, sk->sk_state=%d\n",
    41. __func__, sk->sk_state);
    42. break;
    43. }
    44. /* It _is_ possible, that we have something out-of-order _after_ FIN.
    45. * Probably, we should reset in this case. For now drop them.
    46. */
    47. __skb_queue_purge(&tp->out_of_order_queue); // 删除乱序的队列
    48. if (tcp_is_sack(tp))
    49. tcp_sack_reset(&tp->rx_opt);
    50. sk_mem_reclaim(sk);
    51. if (!sock_flag(sk, SOCK_DEAD)) {
    52. sk->sk_state_change(sk); // 调用sk_state_change(正常情况下,sk_state_change函数指针实际指向sock_def_wakeup函数,sk_state_change不是改变socket状态,而是socket状态改变之后的处理函数)
    53. /* Do not send POLL_HUP for half duplex close. */
    54. if (sk->sk_shutdown == SHUTDOWN_MASK ||
    55. sk->sk_state == TCP_CLOSE) // 读写都关闭了或者socket已经关闭了,这种情况下不能读写
    56. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); // 触发POLL_HUP事件
    57. else
    58. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); // 半关闭状态(这里应该是只关闭了读),那么触发POLL_IN事件,socket关闭了读,触发读事件,读socket会返回对应的错误,根据对应的错误就知道socket已经关闭了
    59. }
    60. }

             tcp_fin函数调用栈:

            服务器端收到FIN的时候,只关闭了接收,还没关闭发送,服务器端调用tcp_data_snd_check检查是否有数据被确认接收,调用tcp_ack_snd_check检查并发送ACK。

            tcp_send_ack函数调用栈:

     3.2、客户端收到FIN的ACK

            阻塞模式,客户端关闭等待socket的时候,收到报文之后,由"用户"处理报文,不在软中断处理;TCP_FIN_WAIT1处理报文的调用栈如下:

             (注意上面的调用栈,接着调用close的函数继续执行,也就是软中断不处理报文)

            客户端TCP_FIN_WAIT1状态收到FIN的ACK,迁移到TCP_FIN_WAIT2状态,并启动超时定时器。

            tcp_rcv_state_process函数代码如下:

    1. int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb)
    2. {
    3. struct tcp_sock *tp = tcp_sk(sk);
    4. struct inet_connection_sock *icsk = inet_csk(sk);
    5. const struct tcphdr *th = tcp_hdr(skb);
    6. struct request_sock *req;
    7. int queued = 0;
    8. bool acceptable;
    9. tp->rx_opt.saw_tstamp = 0;
    10. switch (sk->sk_state) {
    11. case TCP_CLOSE:
    12. goto discard;
    13. case TCP_LISTEN:
    14. if (th->ack)
    15. return 1;
    16. if (th->rst)
    17. goto discard;
    18. if (th->syn) {
    19. if (th->fin)
    20. goto discard;
    21. if (icsk->icsk_af_ops->conn_request(sk, skb) < 0)
    22. return 1;
    23. /* Now we have several options: In theory there is
    24. * nothing else in the frame. KA9Q has an option to
    25. * send data with the syn, BSD accepts data with the
    26. * syn up to the [to be] advertised window and
    27. * Solaris 2.1 gives you a protocol error. For now
    28. * we just ignore it, that fits the spec precisely
    29. * and avoids incompatibilities. It would be nice in
    30. * future to drop through and process the data.
    31. *
    32. * Now that TTCP is starting to be used we ought to
    33. * queue this data.
    34. * But, this leaves one open to an easy denial of
    35. * service attack, and SYN cookies can't defend
    36. * against this problem. So, we drop the data
    37. * in the interest of security over speed unless
    38. * it's still in use.
    39. */
    40. kfree_skb(skb);
    41. return 0;
    42. }
    43. goto discard;
    44. case TCP_SYN_SENT:
    45. queued = tcp_rcv_synsent_state_process(sk, skb, th);
    46. if (queued >= 0)
    47. return queued;
    48. /* Do step6 onward by hand. */
    49. tcp_urg(sk, skb, th);
    50. __kfree_skb(skb);
    51. tcp_data_snd_check(sk);
    52. return 0;
    53. }
    54. req = tp->fastopen_rsk;
    55. if (req) {
    56. WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
    57. sk->sk_state != TCP_FIN_WAIT1);
    58. if (!tcp_check_req(sk, skb, req, true))
    59. goto discard;
    60. }
    61. if (!th->ack && !th->rst && !th->syn)
    62. goto discard;
    63. if (!tcp_validate_incoming(sk, skb, th, 0))
    64. return 0;
    65. /* step 5: check the ACK field */
    66. acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH |
    67. FLAG_UPDATE_TS_RECENT) > 0;
    68. switch (sk->sk_state) {
    69. case TCP_SYN_RECV:
    70. if (!acceptable)
    71. return 1;
    72. if (!tp->srtt_us)
    73. tcp_synack_rtt_meas(sk, req);
    74. /* Once we leave TCP_SYN_RECV, we no longer need req
    75. * so release it.
    76. */
    77. if (req) {
    78. tp->total_retrans = req->num_retrans;
    79. reqsk_fastopen_remove(sk, req, false);
    80. } else {
    81. /* Make sure socket is routed, for correct metrics. */
    82. icsk->icsk_af_ops->rebuild_header(sk);
    83. tcp_init_congestion_control(sk);
    84. tcp_mtup_init(sk);
    85. tp->copied_seq = tp->rcv_nxt;
    86. tcp_init_buffer_space(sk);
    87. }
    88. smp_mb();
    89. tcp_set_state(sk, TCP_ESTABLISHED);
    90. sk->sk_state_change(sk);
    91. /* Note, that this wakeup is only for marginal crossed SYN case.
    92. * Passively open sockets are not waked up, because
    93. * sk->sk_sleep == NULL and sk->sk_socket == NULL.
    94. */
    95. if (sk->sk_socket)
    96. sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
    97. tp->snd_una = TCP_SKB_CB(skb)->ack_seq;
    98. tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale;
    99. tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
    100. if (tp->rx_opt.tstamp_ok)
    101. tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
    102. if (req) {
    103. /* Re-arm the timer because data may have been sent out.
    104. * This is similar to the regular data transmission case
    105. * when new data has just been ack'ed.
    106. *
    107. * (TFO) - we could try to be more aggressive and
    108. * retransmitting any data sooner based on when they
    109. * are sent out.
    110. */
    111. tcp_rearm_rto(sk);
    112. } else
    113. tcp_init_metrics(sk);
    114. tcp_update_pacing_rate(sk);
    115. /* Prevent spurious tcp_cwnd_restart() on first data packet */
    116. tp->lsndtime = tcp_time_stamp;
    117. tcp_initialize_rcv_mss(sk);
    118. tcp_fast_path_on(tp);
    119. break;
    120. case TCP_FIN_WAIT1: { // TCP_FIN_WAIT1状态收到报文
    121. struct dst_entry *dst;
    122. int tmo;
    123. /* If we enter the TCP_FIN_WAIT1 state and we are a
    124. * Fast Open socket and this is the first acceptable
    125. * ACK we have received, this would have acknowledged
    126. * our SYNACK so stop the SYNACK timer.
    127. */
    128. if (req) {
    129. /* Return RST if ack_seq is invalid.
    130. * Note that RFC793 only says to generate a
    131. * DUPACK for it but for TCP Fast Open it seems
    132. * better to treat this case like TCP_SYN_RECV
    133. * above.
    134. */
    135. if (!acceptable)
    136. return 1;
    137. /* We no longer need the request sock. */
    138. reqsk_fastopen_remove(sk, req, false);
    139. tcp_rearm_rto(sk);
    140. }
    141. if (tp->snd_una != tp->write_seq) // TCP_FIN_WAIT1收到ACK不并一定是对FIN的确认,发送FIN的时候并没有检查发送队列,发送队列有数据的时候,FIN报文可能还在排队,FIN的序号应该是tp->write_seq - 1,如果FIN被确认,那么tp->snd_una就应该等于tp->write_seq(下一个发送的序号)
    142. break; // FIN还没发送
    143. tcp_set_state(sk, TCP_FIN_WAIT2); // tp->snd_una == tp->write_seq表示FIN已经被确认了,迁移到TCP_FIN_WAIT2状态
    144. sk->sk_shutdown |= SEND_SHUTDOWN; // 关闭发送
    145. dst = __sk_dst_get(sk);
    146. if (dst)
    147. dst_confirm(dst);
    148. if (!sock_flag(sk, SOCK_DEAD)) {
    149. /* Wake up lingering close() */
    150. sk->sk_state_change(sk);
    151. break;
    152. }
    153. if (tp->linger2 < 0 ||
    154. (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
    155. after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt))) {
    156. tcp_done(sk);
    157. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
    158. return 1;
    159. }
    160. tmo = tcp_fin_time(sk); // 计算TCP_FIN_WAIT2超时时间(与调用tcp_close一样)
    161. if (tmo > TCP_TIMEWAIT_LEN) {
    162. inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN);
    163. } else if (th->fin || sock_owned_by_user(sk)) {
    164. /* Bad case. We could lose such FIN otherwise.
    165. * It is not a big problem, but it looks confusing
    166. * and not so rare event. We still can lose it now,
    167. * if it spins in bh_lock_sock(), but it is really
    168. * marginal case.
    169. */
    170. inet_csk_reset_keepalive_timer(sk, tmo); // 与调用tcp_close一样
    171. } else {
    172. tcp_time_wait(sk, TCP_FIN_WAIT2, tmo); // 与调用tcp_close一样
    173. goto discard;
    174. }
    175. break;
    176. }
    177. case TCP_CLOSING:
    178. if (tp->snd_una == tp->write_seq) {
    179. tcp_time_wait(sk, TCP_TIME_WAIT, 0);
    180. goto discard;
    181. }
    182. break;
    183. case TCP_LAST_ACK:
    184. if (tp->snd_una == tp->write_seq) {
    185. tcp_update_metrics(sk);
    186. tcp_done(sk);
    187. goto discard;
    188. }
    189. break;
    190. }
    191. /* step 6: check the URG bit */
    192. tcp_urg(sk, skb, th);
    193. /* step 7: process the segment text */
    194. switch (sk->sk_state) {
    195. case TCP_CLOSE_WAIT:
    196. case TCP_CLOSING:
    197. case TCP_LAST_ACK:
    198. if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
    199. break;
    200. case TCP_FIN_WAIT1:
    201. case TCP_FIN_WAIT2:
    202. /* RFC 793 says to queue data in these states,
    203. * RFC 1122 says we MUST send a reset.
    204. * BSD 4.4 also does reset.
    205. */
    206. if (sk->sk_shutdown & RCV_SHUTDOWN) {
    207. if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
    208. after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
    209. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
    210. tcp_reset(sk);
    211. return 1;
    212. }
    213. }
    214. /* Fall through */
    215. case TCP_ESTABLISHED:
    216. tcp_data_queue(sk, skb);
    217. queued = 1;
    218. break;
    219. }
    220. /* tcp_data could move socket to TIME-WAIT */
    221. if (sk->sk_state != TCP_CLOSE) {
    222. tcp_data_snd_check(sk);
    223. tcp_ack_snd_check(sk);
    224. }
    225. if (!queued) {
    226. discard:
    227. __kfree_skb(skb);
    228. }
    229. return 0;
    230. }

    4、服务器端调用close(第三次挥手)

            服务器调用close发送FIN与客户端一样,只不过服务器从TCP_CLOSE_WAIT迁移到TCP_LAST_ACK状态,TCP_LAST_ACK状态不用等待关闭,有不用启动定时器,TCP_LAST_ACK如果FIN没有应答的话,会超时重传,超时重传失败就会关闭socket。

     5、客户端收到服务器的FIN并发送ACK(第四次挥手)

             客户端收到服务器的FIN报文,与服务器收到FIN报文一样,调用tcp_fin处理FIN,客户端处于TCP_FIN_WAIT2状态,收到FIN就发送ACK并迁移到TCP_TIME_WAIT状态。

            客户端处理FIN并发送ACK代码如下:

            调用栈:

            服务器收到FIN的ACK,调用tcp_done关闭socket。

            代码如下:

            至此,客户端、服务器都已经关闭了socket,更具体细节可以参考机械工业出版社《Linux内核源码剖析:TCP IP实现(下册)》,关闭流程代码基本一致。

  • 相关阅读:
    16.2 ARP 主机探测技术
    星途星纪元 ES,用艺术思维表达工程技术
    人工智能|机器学习——k-近邻算法(KNN分类算法)
    2022-11-30 mysql-Tuning InnoDB Primary Keys
    Spring如何处理线程的并发问题?
    Unity中Shader使用最简屏幕坐标并且实现屏幕扭曲
    flink cdc MySQL2Doris 案例分享
    ssm基于微信小程序的警局服务管理系统
    Mac安装rabbitmq延迟队列插件
    【C++】初阶模板
  • 原文地址:https://blog.csdn.net/arm7star/article/details/126493089