CS 498 Lecture 17 TCP Implementation in Linux
Jennifer Hou Department of Computer Science University of Illinois at Urbana-Champaign Reading: Chapter 24, The Linux Networking Architecture: Design and Implementation of Network Protocols in the Linux Kernel
Outline Paths of Incoming and outgoing segments Connection management (this lecture) Flow control and congestion control
TCP Implementation in Linux send
tcp_data _queue
Section 24.3
tcp_data Slow Path
TCP
Fast Path
sk->data_ready tcp_ack_ snd_check tcp_data_ snd_check
tcp_send_ (delayed)_ack
tcp_v4_rcv ip_input.c ip_local_deliver
Retrans. Timer tcp_send_skb tcp_write_ timer
Pure tcp_rcv_ tcp_rcv_ ACK tcp_ack state_process established TCP_ESTABLISHED tcp_v4_do_rcv __tcp_v4_lookup()
tcp_sendmsg
tcp_re transmit_skb tcp_write_ xmit
tcp_transmit_skb
ip_output.c ip_queue_xmit
Initial State CLOSED :a n. io at N ic pl SY Ap nd: se
Application.: passive opening send: --timeout CK Ap send: RST ,A p N s Y en lic LISTEN S : d d: ati o sen T ; S Passive Opening SYN n.: N Y R S se ive e es: nd c v i Re ce da e R ta Receive: SYN send: SYN, ACK SYN_RECV SYN_SENT Application: Simultaneous opening close or timeout e iv ct
in
g
Re se cei nd ve :A :S CK YN ,A CK
en
e
os Ap se plic nd ati : F on IN : c l
Application: close sen: FIN
op
K AC ive ce --Re nd: se
FIN_WAIT_1 Receive: send: ---
Passive Close Re ce s en ive ESTABLISHED d: AC FIN Data Transmission K CLOSE_WAI T Simultaneous close Receive FIN Application: c;pse send ACK send: FIN CLOSING Re se ce Receive: ACK nd ive Receive ACK send: --:A :F LAST_ACK send: --I CK N ,A CK
ACK
FIN_WAIT_2
Receive: FIN send: ACK
TIME_WAIT 2 MSL timeout Aktive close
MSL: max. segment life
tcp_rcv_state_process() Handles mainly state transitions and connection management. For example, if the packet received contains an ACK flag, If state=SYN_RECV, then state ESTABLISHED, and the acknowledgement is processed. If state=FIN_WAIT_1, then state FIN-WAIT2 and the TIMEWAIT timer is set. If state=CLOSING, then state TIMEWAIT If state=LAST_ACK, stateCLOSED and the socket is reset.
Transition CLOSED SYN_SENT connect() tcp_v4_connect() tcp_connect() tcp_connect() changes the state to SYN_SENT by invoking tcp_set_state(sk, TCP_SYN_SENT).
Transition LISTENSYN_RECV The LISTEN state is set when the server application invokes listen(). When a SYN is received, tcp_rcv_state_process()tcp_v4_hnd_req() tcp_check_req() tcp_v4_syn_recv_sock() tcp_create_openreq_child(). In tcp_create_openreq_child(), the state is set to TCP_SYN_RECV. tcpaf_specificconn_request() (pointed to tcp_v4_conn_request()) is invoked to specify the initial SN. tcp_v4_send_synack() sends a reply with the ACK and SYN flags set.
Transition SYN_SENT ESTABLISHED tcp_rcv_state_process() tcp_rcv_synsent_state_process() if (thack) { …… if (!thsyn) goto discard; ……. tcp_set_state(sk, TCP_ESTABLISHED); ……. tcp_schedule_ack(tp); ……. }
Transition SYN_SENT SYN_RECEIVED This takes place in the case of simultaneous opening. tcp_rcv_state_process() tcp_rcv_synsent_state_process() if (thsyn) { tcp_set_state(sk, TCP_SYN_RECV); …… tcp_set_synack(sk); …… }
Transition SYN_RECV ESTABLISHED tcp_rcv_state_process() processes this case. If (thack) { switch(skstate) { case TCP_SYN_RECV: …. tcp_set_state(sk, TCP_ESTALISHED); } } Now the connection is established and the two peers can exchange data
Initial State CLOSED :a n. io at N ic pl SY Ap nd: se
Application.: passive opening send: --timeout CK Ap send: RST ,A p N s Y en lic LISTEN S : d d: ati o sen T ; S Passive Opening SYN n.: N Y R S se ive e es: nd c v i Re ce da e R ta Receive: SYN send: SYN, ACK SYN_RECV SYN_SENT Application: Simultaneous opening close or timeout e iv ct
in
g
Re se cei nd ve :A :S CK YN ,A CK
en
e
os Ap se plic nd ati : F on IN : c l
Application: close sen: FIN
op
K AC ive ce --Re nd: se
FIN_WAIT_1 Receive: send: ---
Passive Close Re ce s en ive ESTABLISHED d: AC FIN Data Transmission K CLOSE_WAI T Simultaneous close Receive FIN Application: c;pse send ACK send: FIN CLOSING Re se ce Receive: ACK nd ive Receive ACK send: --:A :F LAST_ACK send: --I CK N ,A CK
ACK
FIN_WAIT_2
Receive: FIN send: ACK
TIME_WAIT 2 MSL timeout Aktive close
MSL: max. segment life
Transition ESTABLISHED FIN_WAIT_1 close() tcp_close() tcp_close_state(). In tcp_close_state(), the state is changed from ESTABLISHED to FIN_WAIT_1.
Transition FIN_WAIT_1 FIN_WAIT_2 In tcp_rcv_state_process() if (thack) { switch(skstate) { …… case TCP_FIN_WAIT1: ………………… tcp_set_state(sk,TCP_FIN_WAIT2); …… } }
Transition FIN_WAIT2TIME_WAIT In tcp_fin(), switch(skstate) { ….. case TCP_FIN_WAIT2: tcp_send_ack(sk); tcp_time_wait(sk, TCP_TIME_WAIT, 0); break; ….. }
tcp_v4_do_rcv() tcp_rcv_state_process() tcp_data_queue() tcp_fin()
Transition ESTABLISHED CLOSE_WAIT tcp_v4_do_rcv() tcp_rcv_established() tcp_data() tcp_data_queue() tcp_fin()
tcp_ack_snd_check()
Transition ESTABLISHED CLOSE_WAIT In tcp_fin(), switch(skstate) { case TCP_SYN_RECV: case TCP_ESTABLISHED: tcp_set_state(sk,TCP_CLOSE_WAIT); if (thrst) skshutdown= SHUTDOWN_MASK; break;
In tcp_ack_snd_check(), a packet is sent with the ACK flag set.
Transition CLOSE_WAIT LAST_ACK TCP on the other hand has closed the connection. Now we are waiting for our TCP instance to close the connection. tcp_close() tcp_close_state() In tcp_close_state(), tcp_set_state(sk, LAST_ACK) is invoked.