Title: LWIP TCP/IP Stack
1LWIP TCP/IP Stack
2What is LWIP?
- An implementation of the TCP/IP protocol stack.
- The focus of the lwIP stack is to reduce memory
usage and code size - suitable for use in small clients with very
limited resources such as embedded systems. - uses a tailor made API that does not require any
data copying.
3Features of TCP/IP stack(Traditional version)
- Designing in a layered fashion leads to
- communication overhead between layers
- Network communication is similar to IPC or file
I/O - APP cant aware of the buffer mechanisms.
- (e.g. reuse buffers with frequently used data.)
ltLayered modelgt
4Features of TCP/IP stack(LWIP version)
- Do not maintain a strict layer.
- This allows using a more relaxed scheme for
communication between layers. - (By means of shared memory)
- APP layer can use the buffer handling mechanisms
used by the lower layers. - APP can more efficiently reuse buffers.
- Application process can use the same memory as
the networking code - App can read and write directly to the internal
buffers. - Saving the expense of performing a copy
5Process model of LWIP
- All protocols reside in a single process thus are
separated from the OS kernel. - Allow to be portable across different OS.
- APP may either reside in the LWIP process or be
in separate processes. - Communicate are done by function calls.
- Or a more abstract API.
6The operating system emulation layers
- OS specific function calls and data structures
are not used directly in the code. - The operating system emulation layer is used.
- The OS emulation layer provides
- Timers, process synchronization, message passing
mechanisms, and so on. - Porting to a different OS
- Only need the operating system emulation layer.
7Buffer and memory management
- Packet buffers pbufs
- A pbuf is LWIPs internal representation of a
packet, - And is designed for the special needs of the
minimal stack. - Types of pbufs
- PBUF_RAM, PBUF_ROM, PBUF_POOL
- A pbuf chain may consist of multiple types of
pbufs.
8PBUF_RAM pbuf
- has the packet data stored in memory managed by
the pbuf subsystem. - used when an application sends data that is
dynamically generated.
9PBUF_ROM pbuf
- Used when an application sends data that is
located in memory managed by the application. - The main use is when the data is located in ROM
- Header that are prepended to the data in a
PBUF_ROM pbuf are stored in a PBUF_RAM pbuf.
10PBUF_POOL
- Consist of fixed size pbufs allocated from a pool
of fixed size pbufs. - Mainly used by network device drivers since the
operation of allocating a single pbuf is fast and
is therefore suitable for use in an interrupt
handler
11Network interfaces
The network interfaces are kept on a global
linked list.
Reflect the kind of H/W Ex) Bluetooth gt bt WLAN
gt wl
The function the device driver should call when a
packet has been received.
The function in the device driver that transmits
a packet on the physical network and it is called
by the IP layer when a packet is to be sent.
Points to device driver specific state for the
network interface and is set by the device driver.
12IP processing(1/3)
- Receiving packets
- Network device driver calls ip_input() function.
- Checking IP version, header length
- Computing the header checksum
- Checking destination address.
- Sending packets
- Handled by the function ip_output()
- Find the appropriate network interface.
- All IP header fields are filled.
- IP header checksum is computed.
- The source and destination address are passed.
13IP processing(2/3)
- Forwarding packets
- The packet should be forwarded
- When none of the network interfaces has the same
IP address as an incoming packets destination
address. - This is done by the function ip_forward()
- ttl field is decreased.
- If ttl reaches zero, an ICMP error message is
sent.
14IP processing(3/3)
This is for ICMP ECHO message. Just swapping the
IP destination and source address of the incoming
packet.
15UDP processing(1/2)
The UDP PCBs are kept on a linked list which is
searched for a match when a UDP datagram arrives.
Called when a datagram is received.
16UDP processing(2/2)
17TCP processing(1/2)
Function to call when a listener has been
connected.
Next sequence number
Receivers window
Timer for TIME-WAIT state
Used when passing received data to the
application layer.
18TCP processing(2/2)
19Application Program Interface
- The BSD socket API
- Require data that is to be sent to be copied from
application program to internal buffers in the
TCP/IP stack. - Since the two layers reside in different
protection domains. - The LWIP socket API
- Utilizes knowledge of the internal structure of
LWIP to achieve effectiveness. - Does not require that data is copied.
- Since the application program can manipulate the
internal buffers directly.
20Data types
- Knowledge of the internal structure of the
struct should not be used in application
programs. - Instead, the API provides functions
for modifying and extracting necessary fields.
21Network connection function(1/2)
- netconn new()
- struct netconn netconn new(enum netconn type
type) - netconn delete()
- void netconn delete(struct netconn conn)
- netconn bind()
- int netconn bind(struct netconn conn, struct ip
addr addr, unsigned short port) - netconn connect()
- int netconn connect(struct netconn conn, struct
ip addr remote addr, unsigned short remote port)
22Network connection function(2/2)
- netconn listen()
- int netconn listen(struct netconn conn)
- netconn accept()
- struct netconn netconn accept(struct netconn
conn) - netconn recv()
- struct netbuf netconn recv(struct netconn
conn) - netconn write()
- int netconn write(struct netconn conn, void
data, int len, unsigned int flags)
23Example 1
ltThis example shows how to open a TCP server on
port 2000gt
Int main() struct netconn conn, newconn /
create a connection structure / conn
netconn_new(NETCONN_TCP) / bind the connection
to port 2000 on any local IP address
/ netconn_bind(conn, NULL, 2000) / tell the
connection to listen for incoming connection
requests / netconn_listen(conn) / block until
we get an incoming connection / newconn
netconn_accept(conn) / do something with the
connection / process_connection(newconn) /
deallocate both connections / netconn_delete(newc
onn) netconn_delete(conn)
24Example 2
ltThis is a small example that shows a suggested
use of the netconn_recv() function.gt
Void example_function(struct netconn
conn) struct netbuf buf / receive data
until the other host closes the connection
/ while((buf netconn_recv(conn)) ! NULL)
do_something(buf) / the connection has now
been closed by the other end, so we close our end
/ netconn_close(conn)
25Example 3
ltThis example shows the basic usage of
netconn_write().gt
Int main() struct netconn conn char
data10 char text "Static text" int i /
set up the connection conn / / ... / /
create some arbitrary data / for(i 0 i lt 10
i) datai i netconn_write(conn, data, 10,
NETCONN_COPY) netconn_write(conn, text,
sizeof(text), NETCONN_NOCOPY) 28 16 NETWORK
CONNECTION FUNCTIONS / the data can be modified
/ for(i 0 i lt 10 i) datai 10 - i /
take down the connection conn / netconn_close(con
n)