STCP tutorial - NCLab, KAIST

Download Report

Transcript STCP tutorial - NCLab, KAIST

CS492B project #2
tutorial
2003. 9. 17
Jin Hyun Ju
## This material is from Stanford cs244A ##
Assignment overview
•
Client/server (client.c,
server.c)
–
•
File download application
“Mysocket” layer (mysock.c)
–
Replacement for socket API
(myopen, myread, etc.)
client
server
•
STCP (transport.c)
mysocket
mysocket
•
Network layer (network.c)
transport
transport
network
network
–
–
–
Transport layer: You fill this in!
Simulates random packet loss,
delay, duplication
network_send(),
network_recv()—datagram
service used by STCP
What is STCP?
• TCP lite
– Simplified connection setup
• Two-way handshake
– No piggybacking of data with acknowledgement packets
– No congestion control
– No slow-start, fast retransmissions, delayed ACKs, etc.
• Still provides reliable, connection-based, byteoriented transport between two endpoints
– Flow control (sliding sender/receiver window)
– Go-back N
– Retransmissions
STCP packets
• Simplified version of TCP segments
– SYN packet: start a connection
– ACK packet: acknowledge reception of data (next seq)
• No data payload permitted in STCP
• SYN-ACK: passive side completes connection
– FIN packet: end a connection
• FIN-ACK: passive side closed connection
– Data packet: Any packet without these flags set
– SYN/data packets: sequence numbers
– No advertised window
STCP model
• Each side of connection: two processes
– Parent (application/mysocket layer)
– Child (your STCP implementation/network layer)
• Why this model?
– Simulates real O/S implementation
• User-level socket library interfaces to kernel functionality
• Network data arrival is asynchronous
– STCP implementation can sleep until an “interesting” event
happens
• Timeout, packet arrival, etc.
STCP model
Parent
Child
client
server
mysocket
mysocket
transport
interface
transport
interface
transport
implementation
transport
implementation
network
network
Function calls
• one STCP connection
– socket() ~ close() in server and client
• mysocket(), mybind()
– wrapper function
• myconnect(), myaccept() -> transport_inite()
• mywrite(), myread()
-> write(), read() -> control_loop()
• myclose()
transport initiation
parent
process
bind()
syn_sd[]
0
1
connect()
child
process
comm_sd
0
1
data_sd[]
fork()
STCP transport layer initiation
myconnect(), myaccept()
transport_init()
• transport_init() creates
two socket pairs, forks;
parent blocks until
connected
–
In STCP, a SYN received from
peer by child
• Parent/child communicate
via local sockets (a la pipe)
–
–
syn_sd[]—notify parent of
connection completion
data_sd[]—pass data between
app/transport layer
Parent (app)
fork
syn_sd[0]
data_sd[0]
syn_sd[1]
data_sd[1]
Data
pipe
(syn pipe)
(data pipe)
IPC
Syn
pipe
Child (transport)
syn_sd[0]
data_sd[0]
syn_sd[1]
(syn pipe)
data_sd[1] (data pipe)
comm_sd (peer socket)
STCP IPC details
• Forking, IPC is provided already in transport.c
– You shouldn’t need to touch much of transport_init(), except to
• Add your SYN/SYN-ACK handling
• Change/add the transport layer state setup as needed
– Sockets created in transport_init() (using localsocketpair())
before forking
• Descriptors valid in both parent/child processes
• Syn_sd[] used only for connection setup
• Data_sd[] used for all data sent between application and transport
layer
• Only one of each socket is used in parent/child to communicate, a la
pipe/FIFO
– transport_init() closes unneeded sockets in parent and child
STCP/application interaction
• Connection establishment (myconnect, myaccept)
– Application blocks until SYN received
• STCP wakes up blocked parent process using “syn pipe”
• syn_sd is finished with at this point
• Data read/write (myread, mywrite)
– Application reads from/writes to its end of the “data pipe”
– STCP writes to/reads from its end of the “data pipe”
• Whenever data is available (from app or peer) and you have
space in your window
• Just assume data written to application has been read
STCP main control loop
local_data_sd
client
server
mysocket
mysocket
transport
interface
transport
interface
local_data_sd= data_sd[1]
= data_sd[1]
transport
implementation
transport
implementation
network
network
comm_sd
STCP main control loop
• Main loop repeatedly waits for event on application
socket (local_data_sd), peer socket (comm_sd),
timeout (in part B onwards)
– Select to see which event happened
STCP/application interaction
• Connection teardown (myclose)
– Application closes stream socket returned by transport_init()
– Pay attention to delivery semantics in assignment handout
• Multiple connections
– A unique child process is forked by transport_init(), so you
don’t have to worry about session demultiplexing
Select
• Online information
– man –s 3c select
• Looks in section 3c of manual (C library)
• Also useful: section 2 (system calls), 3socket (socket calls)
context_t ctx
• global variable
• you can add new variable
• see struct tcpcb
STCP in reliable mode
• Network layer is reliable
• All packets delivered in sequence, no losses
• You implement connection setup/teardown,
acknowledgements, send/receive windows
• Client/server pair should be able to
download files after you’ve finished this
STCP in unreliable mode
• Network layer becomes obnoxious
– Packets reordered, dropped, duplicated
– See network_send() in network.c
• You must handle retransmissions, timeouts, Goback N, buffering of data, …
– Minimum RTT estimate is 20 ms
– Five retransmissions of any segment
– Careful about packets crossing ends of receive window
• Client, server pair should now work with –U flags
• Your modified transport.c is the only file needed
(and accepted) for testing and the final submission
General comments
• See TCP FSM diagram (R. Stevens)
– Keep track of SYN_SENT, SYN_WAITING, etc.
– STCP state machine is simpler
• Network layer uses UDP datagrams
– Treat it like an IP implementation
– Packet-oriented rather than byte-oriented service
– Be sure to read the maximum length datagram!
• Unread bytes in a datagram are discarded by recv()
• Portability
– Don’t forget to use htonX()/ntohX() as appropriate in STCP
header fields