lwip tcp/ip stack

25
LWIP TCP/IP Stack 김김김

Upload: junius

Post on 11-Jan-2016

132 views

Category:

Documents


6 download

DESCRIPTION

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 . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: LWIP TCP/IP Stack

LWIP TCP/IP Stack

김백규

Page 2: 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.

Page 3: LWIP TCP/IP Stack

Features 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 can’t aware of the

buffer mechanisms. (e.g. reuse buffers with

frequently used data.)

<Layered model>

Page 4: LWIP TCP/IP Stack

Features 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

Page 5: LWIP TCP/IP Stack

Process 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.

Page 6: LWIP TCP/IP Stack

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.

Page 7: LWIP TCP/IP Stack

Buffer and memory management

Packet buffers – pbufs A pbuf 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 A pbuf chain may consist of multiple types of pbufs.

Page 8: LWIP TCP/IP Stack

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.

Page 9: LWIP TCP/IP Stack

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 ROMHeader that are prepended to the data in a PBUF_ROM pbuf are stored in a PBUF_RAM pbuf.

Page 10: LWIP TCP/IP Stack

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

Page 11: LWIP TCP/IP Stack

Network interfacesThe network interfaces are kept on a global linked list.

Reflect the kind of H/WEx) Bluetooth => bt

WLAN => wlThe 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.

Page 12: LWIP TCP/IP Stack

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.

Page 13: LWIP TCP/IP Stack

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.

Page 14: LWIP TCP/IP Stack

IP processing(3/3)

ICMP processing This is for ICMP ECHO message. Just swapping the IP destination

and source address of the incoming packet.

Page 15: LWIP TCP/IP Stack

UDP processing(1/2)

The udp_pcb structureThe 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.

Page 16: LWIP TCP/IP Stack

UDP processing(2/2)

UDP processing

Page 17: LWIP TCP/IP Stack

TCP processing(1/2)

Next sequence number

Receiver’s window

Timer for TIME-WAIT

state

Used when passing received data to the

application layer.

Function to call when a listener has been

connected.

Page 18: LWIP TCP/IP Stack

TCP processing(2/2)

Page 19: LWIP TCP/IP Stack

Application 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.

Page 20: LWIP TCP/IP Stack

Data typesnetconn

- 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.

Page 21: LWIP TCP/IP Stack

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, unsig

ned short port)

netconn connect() int netconn connect(struct netconn *conn, struct ip addr *remote

addr, unsigned short remote port)

Page 22: LWIP TCP/IP Stack

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, unsign

ed int flags)

Page 23: LWIP TCP/IP Stack

Example #1Int main(){

struct netconn *conn, *newconn;/* create a connection structure */conn = netconn_new(NETCONN_TCP);/* bind the connection to port 2000 on any localIP address */netconn_bind(conn, NULL, 2000);/* tell the connection to listen for incomingconnection 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);

}

<This example shows how to open a TCP server on port 2000>

Page 24: LWIP TCP/IP Stack

Example #2

Void example_function(struct netconn *conn){

struct netbuf *buf;/* receive data until the other host closesthe connection */while((buf = netconn_recv(conn)) != NULL) {do_something(buf);

}/* the connection has now been closed by theother end, so we close our end */netconn_close(conn);

}

<This is a small example that shows a suggested use of the netconn_recv() function.>

Page 25: LWIP TCP/IP Stack

Example #3Int 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);2816 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);

}

<This example shows the basic usage of netconn_write().>