

LwIP protocol stack supports various network interfaces (network cards). Since network cards interact directly with the hardware platform, the handling differs based on the hardware. How does LwIP accommodate these different network cards?
LwIP provides a unified interface, while the underlying functions need to be implemented by the user, such as initializing the network card, receiving, and sending data. When LwIP receives network data at the lower level, it must be passed through several layers before being processed by the kernel; conversely, when LwIP sends data, it will also call the network card’s send function. For beginners who have not worked with LwIP, how do we write this low-level code? Don’t worry, LwIP provides an ethernetif.c file as a template for the low-level interface driver. Users can modify it according to their network device.
In LwIP, the netif data structure is used to describe the characteristics of a network interface. This structure is defined in the netif.h file.

These fields are used to describe the differences between various network cards, with each network card abstractly represented by a netif structure. Multiple network cards correspond to multiple netif, which are linked together in a singly linked list.
The first node of this linked list is pointed to by the netif_list pointer, which the LwIP kernel uses to traverse and query the netif linked list.

In LwIP, the netif.c file contains the core functions for managing network interfaces. Among them, the netif_default pointer points to the default network interface. When the network layer needs to send a packet, the system prioritizes the network card pointed to by this pointer for data transmission. If this network card does not respond, other network cards will be selected for sending.
1. netif_add function
This function is used to insert a newly created netif into the netif_list queue, indicating that a network interface has been added.

Each netif structure abstracts a network card, containing information such as the card’s send and receive functions and its status.


Note: The newly inserted netif structure is inserted at the head of the netif_list queue.
2. netif_set_default function
This function is used to set a specified netif structure as the default network card, allowing the LwIP kernel to prioritize operations on this network card.

By calling this function, you can set a specified network interface as the default network card. Internally, it updates the corresponding default network card pointer and ensures that subsequent network operations prioritize this network card for data transmission.

Disclaimer: We respect originality and emphasize sharing; the copyright of text and images belongs to the original author. The purpose of reprinting is to share more information and does not represent the position of this account. If your rights are infringed, please contact us promptly, and we will delete it as soon as possible. Thank you!
Original link:
https://blog.csdn.net/m0_73409202/article/details/142657917