You are on page 1of 17

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
sk->data_ready send

TCP tcp_ack_
Fast Path

tcp_data _queue snd_check tcp_sendmsg


Retrans.
tcp_send_ Timer
tcp_data tcp_data_ (delayed)_ack tcp_send_skb
Section 24.3 snd_check
Slow
Path

tcp_write_
Pure
tcp_rcv_ tcp_rcv_ ACK timer
tcp_ack
state_process established
TCP_ESTABLISHED tcp_re -
transmit_skb

tcp_v4_do_rcv
tcp_write_
__tcp_v4_lookup()
xmit
tcp_v4_rcv tcp_transmit_skb

ip_input.c ip_output.c

ip_local_deliver ip_queue_xmit
Initial State

CLOSED

Application.: passive

Ap nd:
se
opening

pl SY
ic
send: ---

at N
timeout
CK

io
Ap
send: RST ,A

n.
YN s p
en lic

:a
: S LISTEN
d d: ati
sen

ct
o
S Passive Opening SYN n.:
T

iv
N ;
Y R

e
S ive se
es:

op
i v c e nd
ce Re

en
Re da

CK

in
Receive: SYN send: SYN, ACK ta
SYN_RECV SYN_SENT

g
,A
Simultaneous opening Application:

CK YN
close or
Application: close

Re nd:

:A :S
se
ce --- timeout

nd ve
ive

se cei
Re
AC
sen: FIN

K
Re Passive Close
e

ce
os

ESTABLISHED s en ive
l

d:
IN : c

Data Transmission AC FIN


: F on

K
nd ati

CLOSE_WAI
se plic

T
Ap

Simultaneous close
Receive FIN Application: c;pse
FIN_WAIT_1 send ACK CLOSING send: FIN
Re
se ce Receive: ACK
Receive: ACK nd ive Receive ACK
:A :F LAST_ACK send: ---
send: --- CK NI send: ---
,A
CK

FIN_WAIT_2 TIME_WAIT
Receive: FIN
send: ACK 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, stateCLOSED 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 LISTENSYN_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.
 tcpaf_specificconn_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 (thack) {
……
if (!thsyn) 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 (thsyn) {
tcp_set_state(sk, TCP_SYN_RECV);
……
tcp_set_synack(sk);
……
}
Transition SYN_RECV ESTABLISHED
tcp_rcv_state_process() processes this case.
If (thack) {
switch(skstate) {
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

Application.: passive

Ap nd:
se
opening

pl SY
ic
send: ---

at N
timeout
CK

io
Ap
send: RST ,A

n.
YN s p
en lic

:a
: S LISTEN
d d: ati
sen

ct
o
S Passive Opening SYN n.:
T

iv
N ;
Y R

e
S ive se
es:

op
i v c e nd
ce Re

en
Re da

CK

in
Receive: SYN send: SYN, ACK ta
SYN_RECV SYN_SENT

g
,A
Simultaneous opening Application:

CK YN
close or
Application: close

Re nd:

:A :S
se
ce --- timeout

nd ve
ive

se cei
Re
AC
sen: FIN

K
Re Passive Close
e

ce
os

ESTABLISHED s en ive
l

d:
IN : c

Data Transmission AC FIN


: F on

K
nd ati

CLOSE_WAI
se plic

T
Ap

Simultaneous close
Receive FIN Application: c;pse
FIN_WAIT_1 send ACK CLOSING send: FIN
Re
se ce Receive: ACK
Receive: ACK nd ive Receive ACK
:A :F LAST_ACK send: ---
send: --- CK NI send: ---
,A
CK

FIN_WAIT_2 TIME_WAIT
Receive: FIN
send: ACK 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 (thack) {
switch(skstate) {
……
case TCP_FIN_WAIT1:
…………………
tcp_set_state(sk,TCP_FIN_WAIT2);
……
}
}
Transition FIN_WAIT2TIME_WAIT

In tcp_fin(), tcp_v4_do_rcv()
switch(skstate) {
…..
case TCP_FIN_WAIT2:
tcp_rcv_state_process()
tcp_send_ack(sk);
tcp_time_wait(sk, TCP_TIME_WAIT,
0); tcp_data_queue()
break;
…..
tcp_fin()
}
Transition ESTABLISHED
CLOSE_WAIT
tcp_v4_do_rcv()

tcp_rcv_established()

tcp_data() tcp_ack_snd_check()

tcp_data_queue()

tcp_fin()
Transition ESTABLISHED
CLOSE_WAIT
In tcp_fin(),
switch(skstate) {
case TCP_SYN_RECV:
case TCP_ESTABLISHED:
tcp_set_state(sk,TCP_CLOSE_WAIT);
if (thrst)
skshutdown= 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.

You might also like