
From frames to an IP address
A link that is up has no IP address yet. On an ESP32 the driver hands frames to lwIP through esp_netif, and code should wait for the got-IP event, not the link-up event, before it talks.
Still the data link
Once the Ethernet driver is installed and the PHY answers, frames can come and go. That is all. The driver knows whether the link is up and what its MAC address is, and nothing about IP. Up to this point everything is layer 2: frames, MAC addresses, a link.
The glue and the stack
On an ESP32 the TCP/IP stack is lwIP, a small TCP/IP implementation for embedded systems, first written by Adam Dunkels at the Swedish Institute of Computer Science. The driver does not talk to it directly. A glue layer, esp_netif, sits between them: esp_eth_new_netif_glue() makes the glue for the Ethernet driver, and attaching it to a netif hands every received frame up to lwIP and every outgoing frame down to the driver.
lwIP reads the type field of each frame. Type 0800 goes to IPv4, type 0806 goes to ARP. ARP is how a station on the local network finds the MAC address that belongs to an IPv4 address, so that a frame can be addressed to it.
Wi-Fi is attached the same way, with its own esp_netif. To lwIP the two are both network interfaces carrying the same IP traffic.
An address, and two events
An IPv4 address usually comes from a DHCP server on the network, or it is set by hand in the code. Until one of those happens the interface has a link and no address.
ESP-IDF reports the two moments separately. ETHERNET_EVENT_CONNECTED means the link is up. IP_EVENT_ETH_GOT_IP means the interface has an IP address. Code that starts talking on the first event talks from a stack with no address yet, and fails whenever the DHCP server is slow to answer or not there at all. Wait for got IP.
A link light says the same thing as the first event and no more: the cable and the PHYs agree. It does not say that IP is configured or that anything on the network is listening.
Above lwIP sit the sockets, with TCP and UDP. They are the subject of the TCP and UDP course. The board side, with the pins and the code, is Ethernet instead of Wi-Fi in the ESP32 book.
Common mistakes
- Starting network code on the link-up event.
ETHERNET_EVENT_CONNECTEDarrives before there is an address. Start onIP_EVENT_ETH_GOT_IP. - Reading the link light as "the network works". It shows the physical link only. No DHCP server on the network means a lit jack and no address.
- Installing the driver and never attaching the glue. Without
esp_netifand its Ethernet glue, frames reach the driver and stop there, and no IP event ever comes.
Edit this page — content/fundamentals/ethernet/from-frames-to-an-ip-address.mdx
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.