As the saying goes, “all good things must come to an end”… and so it is with TCP connections. The link between a pair of devices can remain open for a considerable period of time, assuming that a problem doesn't force the connection to be aborted. Eventually, however, one or both of the processes in the connection will run out of data to send and will shut down the TCP session, or will be instructed by the user to do so.
Requirements and Issues In Connection Termination
Terminating a TCP connection requires four segments. This is because TCP supports a half-closed state. A half closed state is when one side of the connection sends a close (a FIN) which means that it will no longer send anything, but it is still listening for anything coming from the other side. The connection is completely terminated when the other side sends its FIN segment. Both of the FIN segments have to be acknowledged hence four segments. Furthermore, when the last of these acknowledgments has been sent, the sender has to keep their connection open for 4 minutes. This is known as the TIME_WAIT state.
The standard specifies that the client should wait double a particular length of time called the maximum segment lifetime (MSL) before finishing the close of the connection. The TCP standard defines MSL as being a value of 120 seconds (2 minutes). In modern networks this is an eternity, so TCP allows implementations to choose a lower value if it is believed that will lead to better operation.
There are two reasons for this four minute wait. The first is so that the final ACK can be resent if lost. The server will resend its FIN if it does not receive an ACK within a specified time (known as the retransmission timeout, RTO) and the client stays open long enough so that it will receive the retransmission if sent. The second reason is so that any duplicate segments that have been sent can time out before the port is used again for a new connection (all segments have a limited lifetime on the Internet, they will be simply discarded when this lifetime has expired).
There are two side effects to having a four minute wait. The first limits the number of transactions a client can process a second and the second is concerned with the amount of memory used by keeping a number of connections open.
There are only a finite number of ports available at any time. Since a port address is 16 bits the number of ports is 216 which is 65535. The number of reserved ports is 1023 (reserved for well known servers), so the number of ports available to a client at any time is 65535 - 1023 = 64512. The four minute wait combined with the finite number of available ports means that the number of transactions able to be processed by a client is limited to 64512 / 240 = 268 per second.
For each open connection, the operating system kernel of the host must maintain information about the connection in memory. In a typical Unix TCP/IP implementation each open connection requires 264 bytes of memory. If a host had to keep 64512 connections open at any time this equates to more than 17000000 bytes of memory!
Normal Connection Termination:
Just as TCP follows an ordered sequence of operations to establish a connection, it includes a specific procedure for terminating a connection. As with connection establishment, each of the devices moves from one state to the next to terminate the connection. This process is more complicated than one might imagine it needs to be. In fact, an examination of the TCP finite state machine shows that there are more distinct states involved in shutting down a connection than in setting one up.
The reason that connection termination is complex is that during normal operation, both of the devices are sending and receiving data simultaneously. Usually, connection termination begins with the process on just one device indicating to TCP that it wants to close the connection. The matching process on the other device may not be aware that its peer wants to end the connection at all. Several steps are required to ensure that the connection is shut down gracefully by both devices, and that no data is lost in the process.
Ultimately, shut down of a TCP connection requires that the application processes on both ends of the connection recognize that “the end is nigh” for the connection and stop sending data. For this reason, connection termination is implemented so that each device terminates its end of the connection separately. The act of closing the connection by one device means that device will no longer send data, but can continue to receive it until the other device has decided to stop sending. This allows all data that is pending to be sent by both sides of the communication to be flushed before the connection is ended.
Client
|
Server
|
||||
Start
State
|
Action
|
Transitions
To State
|
Start
State
|
Action
|
Transitions
To State
|
ESTABLISHED
|
Client Close Step #1 Transmit:
The application using TCP signals that the connection is no longer needed.
The client TCP sends a segment with the FIN bit set to request
that the connection be closed.
|
FIN-WAIT-1
|
ESTABLISHED
|
At this stage the server is still
in normal operating mode.
|
—
|
FIN-WAIT-1
|
The client,
having sent a FIN, is waiting for it to both be acknowledged
and for the serve to send its own FIN. In this state the client
can still receive data from the server but will no longer accept data
from its local application to be sent to the server.
|
—
|
ESTABLISHED
|
Client Close
Step #1 Receive and Step #2 Transmit: The server receives the client's
FIN. It sends an ACK to acknowledge the FIN. The
server must wait for the application using it to be told the other end
is closing, so the application here can finish what it is doing.
|
CLOSE-WAIT
|
FIN-WAIT-1
|
Client Close Step #2 Receive:
The client receives the ACK for its FIN. It must now wait
for the server to close.
|
FIN-WAIT-2
|
CLOSE-WAIT
|
The server waits for the application
process on its end to signal that it is ready to close.
|
—
|
FIN-WAIT-2
|
The client
is waiting for the server's FIN.
|
—
|
CLOSE-WAIT
|
Server Close
Step #1 Transmit: The server's TCP receives notice from the local
application that it is done. The server sends its FIN to the
client.
|
LAST-ACK
|
FIN-WAIT-2
|
Server Close Step #1 Receive
and Step #2 Transmit: The client receives the server's FIN
and sends back an ACK.
|
TIME-WAIT
|
LAST-ACK
|
The server is waiting for an
ACK for the FIN it sent.
|
—
|
TIME-WAIT
|
The client
waits for a period of time equal to double the maximum segment life
(MSL) time, to ensure the ACK it sent was received.
|
—
|
LAST-ACK
|
Server Close
Step #2 Receive: The server receives the ACK to its FIN
and closes the connection.
|
CLOSED
|
TIME-WAIT
|
The timer expires after double
the MSL time.
|
CLOSED
|
CLOSED
|
The connection is closed on the
server's end.
|
|
CLOSED
|
The connection
is closed.
|
CLOSED
|
The connection
is closed.
|
||
Simultaneous Connection Termination:
Just as it is possible for the normal connection establishment process to be changed if two devices decide to actively OPEN a connection to each other, it is also possible for two devices to try to terminate a connection simultaneously. This term “simultaneously” doesn't mean that they both decide to shut down at exactly the same time—variances in network delays mean nothing can be simultaneous on an internetwork anyway. It simply means that, in the example above, the client decides to shut down and sends a FIN, but the server sends its own FIN before the client's FIN shows up at the server.
Client
|
Server
|
||||
Start
State
|
Action
|
Transitions
To State
|
Start
State
|
Action
|
Transitions
To State
|
ESTABLISHED
|
Client Close Step #1 Transmit:
The application using TCP signals that the connection is no longer needed.
The TCP on the client sends the next segment with the FIN bit
set, indicating a request to close the connection.
|
FIN-WAIT-1
|
ESTABLISHED
|
Server Close Step #1 Transmit:
Before the server can receive the FIN sent by the client, the
application on the server also signals a close. The server also sends
a FIN.
|
FIN-WAIT-1
|
FIN-WAIT-1
|
Server Close
Step #1 Receive and Step #2 Transmit: The client has sent a FIN
and is waiting for it to be acknowledged. Instead, it receives the FIN
sent by the server. It acknowledges the server's close request with
an ACK and continues to wait for its own ACK.
|
CLOSING
|
FIN-WAIT-1
|
Client Close
Step #1 Receive and Step #2 Transmit: The server has sent a FIN
and is waiting for it to be acknowledged. Instead, it receives the FIN
sent by the client. It acknowledges the client's close request with
an ACK and continues to wait for its own ACK.
|
CLOSING
|
CLOSING
|
Client Close Step #2 Receive:
The client receives the ACK for its FIN.
|
TIME-WAIT
|
CLOSING
|
Server Close Step #2 Receive:
The server receives the ACK for its FIN.
|
TIME-WAIT
|
TIME-WAIT
|
The client
waits for a period of time equal to double the maximum segment life
(MSL) time. This gives enough time to ensure the ACK it sent
to the server was received.
|
—
|
TIME-WAIT
|
The server
waits for a period of time equal to double the maximum segment life
(MSL) time. This gives enough time to ensure the ACK it sent
to the client was received.
|
—
|
TIME-WAIT
|
The timer expires after double
the MSL time.
|
CLOSED
|
TIME-WAIT
|
The timer expires after double
the MSL time.
|
CLOSED
|
CLOSED
|
The connection
is closed.
|
—
|
CLOSED
|
The connection
is closed.
|
—
|
No comments:
Post a Comment