TCP / UDP
reliable connection https://soheeparklee.github.io/posts/n-reliableconnection/#-reliable-transmission
โ TCP
Transmission Control Protocol
- reliable
- conection-oriented
๐๐ป assure transmission of data
- exchange data when server is connected
network congestion avoidance algorithm
- full duplex: ์ ์ด์ค, ์ ์ก ์๋ฐฉํฅ์ผ๋ก ๋์์
- point-to-point: ๊ฐ ์ฐ๊ฒฐ์ด ์ ํํ 2๊ฐ์ ์ข ๋จ์ ์ ๊ฐ์ง๊ณ ์์
1๏ธโฃ Reliable
- checksum, retransmission, ACK, timer, sequence number
๐ก reliable connection https://soheeparklee.github.io/posts/n-reliableconnection/#-reliable-transmission
2๏ธโฃ Connection Oriented
- before transmission, TCP sets parameters for connection
- Thus, pre-connection process before connection
- called
3-way-handshake
- TCP operates onlt on end systems
- does not function within intermediate network elements
routers between the ends cannot detect TCP connection
- full-duplex
point-to-point: one sender and one reciever, no multicasting
- to terminate connection,
4-way-handshake
๐ Reliable TCP
- provide reliable data transmission service
- in an unreliable(best-effort) IP environment
What is reliable transmission?
a reliable transmission is transmission whereโฆ
- data is not corrupted,
- in the right order
- TCP uses single timer
- (timer per segement would have huge overhead)
๐ซฑ๐ป TCP Sender
1. Recieve data from application and send
- TCP recieves data from application
- encapsulate data with segment
- send segment to IP
- If timer is not running, start timer when sending segment to IP
2. Timer
- If timeout, retransmit segment
- and restart timer
3. Recieve ACK from reciever
- compare
SendBaseandACK value ySendBase: oldest order number that has not been acknowledgedy: value ofACK
- TCP uses sequence numbers
ychecksrecievement of bytes before y- If
y > SendBase- recievement of packets from
SendBase to y-1 that has not recieved ACKhas been confirmed - Thus,
SendBasechanges toy+1 - Sliding window
- recievement of packets from
- If there is unchecked segment, restart timer
๐ Retransmission Scenarios in TCP
- three scenarios might be possible for retransmission
โ๏ธ lost ACK scenario
- Reciever recieved
segment 92successfully - Reciever send
ACK ACKwas lost- timeout occurs,
segment 92is retransmitted - Reciever has already recieved
92, thus throw away
โ๏ธ premature timeout
- Sender sends
segment 92,segment 100to reciever - Reciever recieved both segments successfully
Reciever sends
ACK100,ACK120accordingly- However, sender recieves
ACK100after timeout - Sender retransmits
segment 92 - Sender recieve
ACK120before timeout - Sender does not retransmit
segment 100
โ๏ธ cumulative ACK
- Sender sends
segment 92,segment 100to reciever - Reciever recieved, sends
ACK100,ACK120accordingly - However,
ACK100was lost - Sender recieves only
ACK120 - Sender can assume until
segment 100was successfully recieved - Thus, retransmission does not occur
โณ When timeout, timeout โ๏ธ 2
- When timeout occurs, TCP retransmits the segment with smallest order from segements that have not recieved
ACK - Thus, when timeout occurs, TCP makes
timeout period * 2 - To reduce network congestion
- This is called congestion control
๐ TCP fast retransmit
๐๐ป Problem of retransmitting according to timeout:
- retransmission according to timeout has problems too
- when timeout period is too long
- Sender can realize packet corruption after
duplicate ACKs - When Reciever recieves segment in wrong order, sends the
last correct ACKto sender - If sender recieves
triple dublicate ACKs - sender can realize packet has been lost for that
ACK - sender can retransmit
- Sender does not wait for timeout,
- thus this is fast retransmit
TCP ๐ GBN, SR
Is TCP GBN, or SR?
- TCP is simmilar to GBN
- Like GBN, TCP sender has to keep track of
SendBase(smallest order num withoutACK)NextSeqnum(next number segment to be transmitted)
- TCP is different from GBN
- If window size is
5, sent data1, 2, 3, 4, 5andACKfor packet2is lost - GBN would retransmit all
1, 2, 3, 4, 5 - TCP would retransmit only
2 - or, if sender recieves
ACK 3/4/5(later than 2)before timeout, does not retransmit anything - bc TCP uses
cumulative ACK
- TCP is similar to SR
- that it only retransmits the lost segment(selective)
- TCP is different from SR
- However, SR uses timer for each segment
- TCP uses only one timer for the oldest segment that did not recieve
ACK - SR does not use
cumulative ACK TCP uses
cumulative ACK- โญ๏ธ conclusion: TCP is similar to GBN and SR, but is distinctive
- TCP is more like a hybrid GBN, SR
โ๏ธ 3 way handshake
PAR
Positive Acknowledgement with Re-transmission
TCP provides reliable connection with PAR
device using PAR resend the data until it recieves acknowledgement
โ๏ธ SYN:
- synchronize
- client sends
SYN(Synchronize Sequence Number)to server - SYN bit = 1
send
Sequence Numberwith random num- Result
SYN: - client: port
CLOSEDโก๏ธSYN_SENT - server:
LISTEN(port open, ready for communication),Wait for client
โ๏ธ SYN+ACK:
- synchronize acknowledge
- server
ACKclient request, request client to open port as well - server responds to client with
SYN+ACKsignals bit set - SYN bit = 1
Acknowledgement Number:Sequence Number + 1client checks that server state is
alivewith serverACKmessage- Result
SYN+ACK: - client:
SYN_SENT - server:
LISTENโก๏ธSYN_RCVD(SYN recieved)
โ๏ธ ACK:
- acknowledge
- client acknowledges the response of server
- client sends server
ACKmessage so that server can check client and connect - establish reliable connection
- can start exchanging data
server checks that client state is
alivewith clientACKmessage- Result
ACK: - client:
ESTABLISHED(port connected) - server:
ESTABLISHED
โ๏ธ 4 way handshake
terminate connection
โ๏ธ FIN:
- client sends
FINto server Fin bit = 1
- Result
- client:
FIN_WAIT_1 - server:
CLOSE_WAIT
โ๏ธ ACK:
- when server revieves
FIN, sendsACKmessage to client to send all data, server is in CLOSE_WAIT status
- when client recieves
ACK, change port status toFIN_WAIT_2 client waits for server
FIN- Result
- client:
FIN_WAIT_2 - server:
CLOSE_WAIT
โ๏ธ FIN:
- server tells client โI finished sending dataโ
- if all data is sent, server send
FINto client FIN bit = 1
- Result
- client:
TIME_WAIT - server:
LAST_ACK
โ๏ธ ACK:
- client recieves
FIN, sendsACKmessage to server - server closes after getting final
ACK client closes after
TIME_WAIT- Result
- client:
CLOSED - server:
CLOSED
โ Two way handshake?
Why not use two-way-handshake? What are the problems of two-way-handshake
- Two scenarios of two-way-handshake
- 1๏ธโฃ Client request server for connection
2๏ธโฃ Server replies
ACK, connection created- However, following situations should not be connected
- ๐๐ป Client request that came to server is a retransmission request due to timeout
๐๐ป When client and server TCP communication is over
- Thus, server has to check
client statebefore connecting to client request - need to check if
client stateisalive - client has to tell server that its state is
aliveand possible for connection
โ Problems of TCP
- packet loss
- packet order miss
- congestion
- reciever overloaded
โ๏ธ Flow Control in TCP
- โ ๏ธ sender speed > reciever speed
- reciever recieve too much segments even before sending read data to TCP socket
reciever buffer overflow โก๏ธ cannot recieve more packet
- ๐ configure data sending speed according to reciever
- configure senderโs
window size - ensure reciever
Free Buffer Space(rwnd)is not overflowed - need to ensure reciever doesnt recieve too much packets
prevent reciever overflow
- reciever sends feedback of state to sender
- reciever send
ACKwithTCP header rwnd - with information about its
Free Buffer Space(rwnd)
๐ Solution
๐ Stop and Wait
- only send next packet when recieved message
- only can packet size according to reciever
- packet on air = sliding window
- sliding window= last sent byte - last checked byte
โ๏ธ Congestion Control in TCP
- prevent that number of packet in network do not overexceed
- configure
window sizeto reduce network congestion - configure host, router to prevent congestion
What happens when network is congested?
- more latency
- packet loss
๐ Solution
๐ AIMD
Additive Increase/Multiplicative Decrease
- Additive increase: send
npacket, if arrives successfully, sendn+1 - AIMD will, for every
RTT, increasecwndfor1 - Multiplicative decrease: if fail, packet send speed
/2
๐ Slow Start
- For each
RTT, sendnpacket, if arrives successfully, sendn*2 - double
cwnd - If
cwnd>ssthresh(OS threshold) - then for each
RTT, increasecwndfor1 - this moment is called the
slow start phrase - if packet loss, make
ssthresh = current cwnd/2 - packet loss can be detected with
3 duplicate ACKortimeout
โ๏ธ Fast Retransmit
- if reciever recieves the wrong packet, send ACK
- but in this ACK, send the number of packet that reciever missed to recieve
- sender will acknowledge he didnt sent this packet, and send.
- if repeated w same number packet more than three times, reduce
window size
๐ TCP Tahoe
- If packet loss is detected, make
cwndto1
๐ TCP RENO
- If packet loss is detected, make
cwndtocwnd/2(half) - widely used method
Flow control ๐ Congestion control
โ๏ธ Flow control:
- control traffic from sender to reciever
- protect reciever overflow
- usually data link layer
โ๏ธ Congestion control:
- control congestion on network
- prevent network from congestion
- usually network, transport layer
โ UDP
User Datagram Protocol
manage data in datagram unit
- conectionless
- NOT reliable (transmission not granted)
๐๐ป speed
- data might arrive in different order from sent order
- does not check if data was recieved
- no flow control
- no logical connection between client and server(no handshake)
packets are independent
- 1:1
- 1:N
- N:N
๐๐ป Why use UDP?
Faster than TCP UDP header is simple too used for for real time services
Why does Domain Name Service use UDP?
- DNS: application layer protocol
- DNS request is small โก๏ธ small enough to be stored inside UDP segment
- do not need 3 way handshake(TCP overhead > UDP overhead)
- loss for request will be configured by application layer
When does DNS have to use TCP?
zone transfer(request between DNS servers) if data is bigger than 512 bytes
1๏ธโฃ Not reliable
- data can be lost
- data can be revieced in different order
- can check data integrity: with
checksum header - if reliability is needed, compliment reliability in application level
โญ๏ธ UDP checksum header
- purpose: verify data integrity
- divide UDP data segment into word(16bit)
- add each word, add 1, create checksum
- send segment with UDP
checksum header
- recipient recieve segment, create checksum on recieved segemnt
- check if recieved checksum matches
- if matches, the segment has not been altered
2๏ธโฃ Connectionless
- no pre-connection process, no handshake
- faster
3๏ธโฃ faster speed, less overhead
- no reliability, no connection โก๏ธ fast
less overhead
- TCP: 20byte overhead
- UDP: 8byte overhead
- used in DNS, SNMP, streaming service
- DNS:
- purpose is to get IP address to domain name
- use UDP for speed
- and UDP less overhead
- SNMP:
- simple network management protocol
- network device can be numerous
- UDP is more adequate, less overhead and faster speed
- internet calls, streaming service:
- minimum transmission guaranteed
- allow little data loss
- thus, UDP is adequate
TCP ๐ UDP
- ๊ณตํต์ :
- way to establish connection
- unit of transport layer: segment
โ๏ธ TCP: Transmission Control Protocol
- ์์๋ฐ๋ฅธ ํ๋กํ ์ฝ
- โ๋ฐ์ดํฐ ์ ์กํด๋ ๋ ๊น์?โ ๋ฌผ์ด๋ณด๊ณ ๋ฌธ์ ์๋ค๊ณ ํ๋ฉด ๊ทธ์ ์ผ ๋ณด๋.
๋ณด๋ธ ํ โ๊ฐ์ฌํฉ๋๋คโ ์ธ์ฌ๊น์ง ํ๊ณ ๋ฌผ๋ฌ๊ฐ๋ค.
- ์ฐ๊ฒฐ ์งํฅํ ํ๋กํ ์ฝ
- ๋ฐ์ดํฐ ์์ ์ฑ์ ์ํด ์ ๋ขฐ์ฑ/ํ๋ฆ์ ์ ์ดํ๋ ์ ์ก ๊ณ์ธต ํ๋กํ ์ฝ
- UDP์ ๋นํด์๋ ๋๋ฆผ
- ์ ํ์ฑ ํ์ํ๋ฉด TCP
๐ก ์ฐ๊ฒฐ๊ณผ์ 3-way-handshake
- SYN: ๋ ๋ฐ์ดํฐ ๋ณด๋ด๋ ๋ผ?
- SYN/ACK: ์ ๋ณด๋ด
- ACK: ๊ทธ์ ์ผ ๋ฐ์ดํฐ ๋ณด๋ธ๋ค.
๐ก ๋ฐ์ดํฐ ์ก์์ง ์ ๋์
- ๋ฐ์ดํฐ(ํจํท) ์์ ๋ณด์ฅ
- ๋ฐ์ดํฐ ํ๋ฆ ์ ์ด
- ๋ฐ์ดํฐ ์ ๋ขฐ์ฑ ๋ณด์ฅ(์ฌ์ ์ก)
๐ก ์ฐ๊ฒฐ ํด์ ๊ณผ์ 4-way-handshake
- FIN
- ACK
- FIN
- ACK ํ๊ณ ๋์์ผ ์ฐ๊ฒฐ ํด์
โ๏ธ UDP: User Datagram Protocol
๋ง๋ฌด๊ฐ๋ด ํ๋กํ ์ฝ
๊ทธ๋ฅ ์ผ๋จ ๋ฐ์ดํฐ ๋ณด๋.
๋ฐ์ผ๋ฉด ๋ฐ๋๊ฑฐ๊ณ ์ ๋ฐ์ผ๋ฉด ์ ๋ฐ๋๊ฒจ~
๋ฐ์ดํฐ ์ ๋ขฐ์ฑ๋ณด๋ค๋ ์๋์ ๊ฐ๋จํ ํต์ ์ ์ค์ํ๋ ์ ์ก๊ณ์ธต ํ๋กํ ์ฝ
์๋ ์ค์ํ๋ฉด UDP
- ์ฐ๊ฒฐ๊ณผ์ 3-way-handใ
shake ์๋ต
- ๋ฐ์ดํฐ(ํจํท) ์์ ๋ณด์ฅ โ
- ๋ฐ์ดํฐ ํ๋ฆ ์ ์ด โ
- ๋ฐ์ดํฐ ์ ๋ขฐ์ฑ ๋ฎ์
- ์ฐ๊ฒฐ ํด์ ๊ณผ์ 4-way-handshake ์๋ต