Design and Implementation of the lwIP
TCP/IP Stack
Swedish Institute of Computer Science
February 20, 2001
Adam Dunkels
Abstract
lwIP is an implementation of the TCP/IP protocol stack. The focus of the lwIP stack is to
reduce memory usage and code size, making lwIP suitable for use in small clients with very
limited resources such as embedded systems. In order to reduce processing and memory demands,
lwIP uses a tailor made API that does not require any data copying.
This report describes the design and implementation of lwIP. The algorithms and data struc-
tures used both in the protocol implementations and in the sub systems such as the memory and
buffer management systems are described. Also included in this report is a reference manual for
the lwIP API and some code examples of using lwIP.
Contents
1 Introduction 1
2 Protocol layering 1
3 Overview 2
4 Process model 2
5 The operating system emulation layer 3
6 Buffer and memory management 3
6.1 Packet buffers — pbufs .................................. 3
6.2 Memory management .................................. 5
7 Network interfaces 5
8 IP processing 7
8.1 Receiving packets ..................................... 7
8.2 Sending packets ...................................... 7
8.3 Forwarding packets .................................... 8
8.4 ICMP processing ..................................... 8
9 UDP processing 8
10 TCP processing 9
10.1 Overview ......................................... 9
10.2 Data structures ...................................... 10
10.3 Sequence number calculations .............................. 12
10.4 Queuing and transmitting data ............................. 12
10.4.1 Silly window avoidance ............................. 13
10.5 Receiving segments .................................... 13
10.5.1 Demultiplexing .................................. 13
10.5.2 Receiving data .................................. 14
10.6 Accepting new connections ............................... 14
10.7 Fast retransmit ...................................... 14
10.8 Timers ........................................... 14
10.9 Round-trip time estimation ............................... 15
10.10Congestion control .................................... 15
11 Interfacing the stack 15
12 Application Program Interface 16
12.1 Basic concepts ...................................... 16
12.2 Implementation of the API ............................... 17
13 Statistical code analysis 17
13.1 Lines of code ....................................... 18
13.2 Object code size ..................................... 19
14 Performance analysis 20
15 API reference 21
15.1 Data types ........................................ 21
15.1.1 Netbufs ...................................... 21
15.2 Buffer functions ...................................... 21
15.2.1 netbuf
new() ................................... 21
15.2.2 netbuf
delete() .................................. 21
15.2.3 netbuf
alloc() ................................... 22
15.2.4 netbuf
free() ................................... 22
15.2.5 netbuf
ref() .................................... 22
15.2.6 netbuf
len() .................................... 23
15.2.7 netbuf
data() ................................... 23
15.2.8 netbuf
next() ................................... 23
15.2.9 netbuf
first() ................................... 24
15.2.10 netbuf
copy() ................................... 24
15.2.11 netbuf
chain() .................................. 24
15.2.12 netbuf
fromaddr() ................................ 24
15.2.13 netbuf
fromport() ................................ 25
16 Network connection functions 25
16.0.14 netconn
new() .................................. 25
16.0.15 netconn
delete() ................................. 25
16.0.16 netconn
type() .................................. 25
16.0.17 netconn
peer() .................................. 25
16.0.18 netconn
addr() .................................. 26
16.0.19 netconn
bind() .................................. 26
16.0.20 netconn
connect() ................................ 26
16.0.21 netconn
listen() .................................. 26
16.0.22 netconn
accept() ................................. 26
16.0.23 netconn
recv() .................................. 27
16.0.24 netconn
write() .................................. 28
16.0.25 netconn
send() .................................. 29
16.0.26 netconn
close() .................................. 30
17 BSD socket library 30
17.1 The representation of a socket .............................. 30
17.2 Allocating a socket .................................... 30
17.2.1 The socket() call ................................ 30
17.3 Connection setup ..................................... 31
17.3.1 The bind() call ................................. 31
17.3.2 The connect() call ............................... 31
17.3.3 The listen() call ................................ 32
17.3.4 The accept() call ................................ 32
17.4 Sending and receiving data ............................... 33
17.4.1 The send() call ................................. 33
17.4.2 The sendto() and sendmsg() calls ....................... 34
17.4.3 The write() call ................................. 34
17.4.4 The recv() and read() calls .......................... 35
17.4.5 The recvfrom() and recvmsg() calls ..................... 36
18 Code examples 36
18.1 Using the API ...................................... 36
18.2 Directly interfacing the stack .............................. 39
Bibliography 41