Lwip is a small open source TCP/IP protocol stack developed by Adam Dunkels of the Swedish Computer Science Institute (SICS). The focus of implementation is to reduce RAM usage while maintaining the main features of the TCP protocol.
The main modules of LWIP (Light Weight internet protocol) include: configuration module, initialization module, NeTIf module, mem (memp) module, netarp module, ip module, udp module, icmp module, igmp module, dhcp module, tcp module, snmp module. Wait. The following mainly explains and sorts out the protocol processing that we need to care about.
Configuration module:The configuration module configures the system and submodules in various macro definitions. For example, through the macro, the parameters of the mem management module are configured. The configuration module also configures the protocol clusters supported by the protocol stack through macros, and determines which protocols are supported by macro customization. The main file is opt.h.
Initialization module:The file that initializes the module entry is tcpip.c, and its initialization entry function is: void tcpip_init(void (* initfunc)(void *), void *arg)
The entry initializes all submodules by calling the lwip_init() function and starts the stack management process. At the same time, the function also has a callback hook and its parameters. It can be called where it is needed.
The protocol stack data distribution management process is responsible for the processing of input messages, timeout processing, API functions, and callback processing. The prototype is as follows:
staTIc void tcpip_thread(void *arg)
NeTIf module:The NeTIf module is an interface module of the protocol stack and the underlying driver, and describes a network port device of the bottom layer as an interface interface of the protocol stack. The main file for this module is netif.c. It describes all the network port devices in the system by means of a linked list.
Netif's data structure describes the parameters of the network port, including IP address, MAC address, link status, network port number, transceiver function and other parameters. The data transmission and reception of a network port device is mainly performed through the structure.
Mem (memp) module:The Mem module manages the content buffer used by the protocol stack and manages the pbuf structure and field processing of the message. The main files include mem.c, memp.c, and pbuf.c.
Netarp module:The netarp module is a module for processing the arp protocol. The main source file is etharp.c. Its main entry function is: err_t ethernet_input(struct pbuf *p, struct netif *netif)
The entry function determines whether the protocol is processed according to the ARP protocol or the packet is submitted to the IP protocol by determining the protocol type of the input message p. If the message is an arp message, the interface calls etharp_arp_input to process the arp request.
If it is an ip packet, the interface calls etharp_ip_input for arp update, and calls the ip_input interface to submit the packet to the ip layer.
In this module, the address mapping arp table of the device is created, and an address mapping relationship query interface is provided. At the same time, the sending interface of the arp packet is also provided. as follows
Err_t etharp_output(struct netif *netif, struct pbuf *q, struct ip_addr *ipaddr)
The interface needs to be registered to the output field of the netif. When the ip layer outputs the packet, the MAC address of the target device is obtained through the interface. After the final packet is combined, the interface sends the data to the driver interface of the underlying device.
In the etharp_output interface, the packet type is determined. If it is a broadcast packet or a multicast packet, the etharp_send_ip (assembly target mac and source mac) interface is called, and etharp_send_ip calls the link driver hook function registered by the device driver in the netif structure to send the final packet. If it is a unicast packet, the etharp_output interface calls etharp_query to map the IP address and MAC address to obtain the MAC address of the target machine. The etharp_send_ip is called in etharp_query to send the final combined message.
Ip module:The ip module implements the ip layer processing of the protocol, and the main file is ip.c. Its main entry function is: err_t ip_input(struct pbuf *p, struct netif *inp)
The interface inputs the protocol type of the input message into the corresponding upper layer protocol module. For example, send a udp message to udp_input.
The other interface of the module is the input function, the prototype is as follows:
Err_t ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest, u8_t ttl, u8_t
Tos, u8_t proto)
After the interface passes the routing table or transmits the ip, it calls the output field function hook of netif to send the packet.
Udp module:The udp module implements the protocol processing of the udp protocol layer, and the main file is udp.c. The module binds the application port to the application via the PCB control block. When receiving a new message, analyze the corresponding PCB, find the corresponding processing hook, and process the application. The main entry function is:
Void udp_input(struct pbuf *p, struct netif *inp) The interface responsible for this module is as follows:
Err_t udp_send(struct udp_pcb *pcb, struct pbuf *p)
The module is responsible for binding a PCB to a local port as follows:
Err_t udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port) This module is responsible for binding a PCB to a remote port as follows:
Err_t udp_connect(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
Igmp module:The igmp module is responsible for group management. Its main interface functions are as follows:
Void igmp_input(struct pbuf *p, struct netif *inp, struct ip_addr *dest) This interface is responsible for the processing of IGMP messages, such as whether the current message is a request or a response. Err_t igmp_joingroup(struct ip_addr *ifaddr, struct ip_addr *groupaddr) This interface adds a network port to a group.
Err_t igmp_leavegroup(struct ip_addr *ifaddr, struct ip_addr *groupaddr) This interface removes a network port from a group.
Dhcp module:The dhcp module is used to obtain information about the IP address of the device. There are mainly several processing entries: dpch startup, dpch receiving message processing, and timer module processing.
The main interface prototypes are as follows:
Err_t dhcp_start(struct netif *netif)
This interface is used to start the dhcp module on the device, mainly the function of the client. The module implements the device dhcp description structure generation, binds the port of dhcp to the udp protocol, and binds the dhcp module to the remote server port. Finally start the dhcp application.
Static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
The interface is a registration interface and is used for dhcp packet reception. At start dhcp, the interface is registered to the udp protocol layer via udp pcb of dhcp. After Udp processes the packet, the registration interface is called according to the port. The protocol of the dhcp packet is implemented on the interface.
Void dhcp_fine_tmr() Void dhcp_coarse_tmr()
These two function interfaces implement dhcp related timeout processing monitoring. The above one is used to request a response timeout. The following is an expiration process for address leases.
From the source code analysis, the above interface needs to be focused on when applying the lwip protocol stack. For small memory applications, the memory management of the protocol stack and the pbuf application part need to be rewritten.
2, lwip features(1) Support IP forwarding under multiple network interfaces;
(2) Support the ICMP protocol;
(3) UDP (User Datagram Protocol) including experimental extensions;
(4) TCP (Transmission Control Protocol) including congestion control, RTT estimation, fast recovery, and fast forwarding;
(5) Provide a dedicated internal callback interface (Raw API) to improve application performance;
(6) Optional Berkeley Interface API (used in multi-threaded situations).
(7) Support for ppp in the latest version
(8) Added support for IP fragments in the new version.
(9) Support DHCP protocol, dynamically allocate ip address.
3. LWIP protocol processThe following picture clearly describes the packet processing process of lwip, oh, borrow it. However, its description of netif-"output is not enough. From the code point of view, the output is actually the output of the arp layer, and finally the packet is sent by the arp layer to call the underlying output interface in netif.
Capillary Thermostat,Water Heater Thermostat,Electric Water Heater Capillary Thermostat,Hot Water Heater Thermostat
Foshan City Jiulong Machine Co., Ltd , https://www.jlthermostat.com