1 / 25

LWIP TCP/IP Stack

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 .

junius
Download Presentation

LWIP TCP/IP Stack

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LWIP TCP/IP Stack 김백규

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

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

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

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

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

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

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

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

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

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

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

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

  14. IP processing(3/3) • ICMP processing This is for ICMP ECHO message. Just swapping the IP destination and source address of the incoming packet.

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

  16. UDP processing(2/2) • UDP processing

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

  18. TCP processing(2/2)

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

  20. Data types • netconn - 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.

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

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

  23. 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); • }

  24. 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); • }

  25. 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); • }

More Related