Sei sulla pagina 1di 25

LWIP TCP/IP Stack

김백규
What 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.
Features of TCP/IP stack(Traditional version)

Designing in a layered
fashion leads to…
<Layered model>
 communication overhead
between layers

Network communication is
similar to IPC or file I/O
 APP can’t aware of the
buffer mechanisms.
 (e.g. reuse buffers with
frequently used data.)
Features of TCP/IP stack(L
WIP 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


Process model of L
WIP
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 L


WIP process or be in
separate processes.
 Communicate are done by function calls.
 Or a more abstractAPI.
The 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
.
Buffer and memory management

Packet buffers – pbufs


 Apbuf is LWIP’s 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

 Apbuf chain may consist of multiple types of pbufs.


PBUF_RAM pbuf

has the packet data stored in memory managed by the pbuf subsystem.
used when an application sends data that is dynamically generated.
PBUF_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.
PBUF_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
Network interfaces
The network interfaces are kept
on a global linked list.

Reflect the kind of H/W


Ex) Bluetooth => bt
WLAN => 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.
IP 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.
IP processing(2/3)
Forwarding packets
 The packet should be forwarded…
 When none of the network interfaces has the same IP
address as an incoming packet’s destination address.
 This is done by the function ip_forward()
 ttl field is decreased.
 If ttl reaches zero, an ICMP error message is sent.
IP processing(3/3)
ICMP processing This is for ICMP ECHO message. Just
swapping the IP destination and source
address of the incoming packet.
UDP processing(1/2)
The udp_pcb structure
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.
UDP processing(2/2)
UDP processing
TCP processing(1/2)
Function to call when a
listener has been connected.

Next sequence
number

Receiver’s
window

Timer for TIME-


WAIT state

Used when passing received


data to the application layer.
TCP processing(2/2)
Application Program Interface
The BSD socketAPI
 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 socketAPI
 Utilizes knowledge of the internal structure of L
WIP to
achieve effectiveness.
 Does not require that data is copied.
 Since the application program can manipulate the internal
buffers directly.
Data types
netconn

- Knowledge of the internal structure of the struct should not be used


in application programs.
- Instead, theAPI provides functions for modifying and extracting
necessary fields.
Network 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)
Network 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)
Example #1
<This example shows how to open a TCP server on port 2000>
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(newconn);
netconn_delete(conn);
}
Example #2
<This is a small example that shows a suggested use of the netconn_recv() function.>

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);
}
Example #3
<This example shows the basic usage of netconn_write().>
Int main()
{
struct netconn *conn;
char data[10];
char text[] = "Static text";
int i;
/* set up the connection conn */
/* [...] */
/* create some arbitrary data */
for(i = 0; i < 10; i++)
data[i] = 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 < 10; i++)
data[i] = 10 - i;
/* take down the connection conn */
netconn_close(conn);
}

Potrebbero piacerti anche