26.12.2013 Views

lwIP - A Minimal TCP/IP implementation - Wikia

lwIP - A Minimal TCP/IP implementation - Wikia

lwIP - A Minimal TCP/IP implementation - Wikia

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

10 <strong>TCP</strong> PROCESSING 10.2 Data structures<br />

Application<br />

Application layer<br />

tcp_write()<br />

tcp_receive()<br />

tcp_enqueue()<br />

tcp_process()<br />

Transport layer<br />

tcp_output()<br />

tcp_input()<br />

ip_route()<br />

ip_output_if()<br />

ip_input()<br />

Internetwork layer<br />

netif->output()<br />

Network interface layer<br />

Figure 9. <strong>TCP</strong> processing<br />

When an application wants to send <strong>TCP</strong> data, tcp write() is called. The function tcp write()<br />

passes control to tcp enqueue() which will break the data into appropriate sized <strong>TCP</strong> segments<br />

if necessary and put the segments on the transmission queue for the connection. The function<br />

tcp output() will then check if it is possible to send the data, i.e., if there is enough space in the<br />

receiver’s window and if the congestion window is large enough and if so, sends the data using<br />

ip route() and ip output if().<br />

Input processing begins when ip input() after verifying the <strong>IP</strong> header hands over a <strong>TCP</strong><br />

segment to tcp input(). In this function the initial sanity checks (i.e., checksumming and <strong>TCP</strong><br />

options parsing) are done as well as deciding to which <strong>TCP</strong> connection the segment belongs. The<br />

segment is then processed by tcp process(), which implements the <strong>TCP</strong> state machine, and any<br />

necessary state transitions are made. The function tcp receive() will be called if the connection<br />

is in a state to accept data from the network. If so, tcp receive() will pass the segment up to<br />

an application program. If the segment constitutes an ACK for unacknowledged (thus previously<br />

buffered) data, the data is removed from the buffers and its memory is reclaimed. Also, if an ACK<br />

for data was received the receiver might be willing to accept more data and therefore tcp output()<br />

is called.<br />

10.2 Data structures<br />

The data structures used in the <strong>implementation</strong> of <strong>TCP</strong> are kept small due to the memory constraints<br />

in the minimal system for which <strong>lw<strong>IP</strong></strong> is intended. There is a tradeoff between the<br />

complexity of the data structures and the complexity of the code that uses the data structures,<br />

and in this case the code complexity has been sacrificed in order to keep the size of the data<br />

structures small.<br />

The <strong>TCP</strong> PCB is fairly large and is shown in Figure 10. Since <strong>TCP</strong> connections in the LISTEN<br />

and TIME-WAIT needs to keep less state information than connections in other states, a smaller<br />

PCB data structure is used for those connections. This data structure is overlaid with the full PCB<br />

structure, and the ordering of the items in the PCB structure in Figure 10 is therefore somewhat<br />

awkward.<br />

The <strong>TCP</strong> PCBs are kept on a linked list, and the next pointer links the PCB list together.<br />

The state variable contains the current <strong>TCP</strong> state of the connection. Next, the <strong>IP</strong> addresses and<br />

port numbers which identify the connection are stored. The mss variable contains the maximum<br />

segment size allowed for the connection.<br />

The rcv nxt and rcv wnd fields are used when receiving data. The rcv nxt field contains<br />

the next sequence number expected from the remote end and is thus used when sending ACKs<br />

10

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!