Repository navigation
esp32 network: Add a fixed IPv4 address to Ethernet - #2484
gabrielmancini wants to merge 2 commits into
Conversation
An {ipv4, {Address, Netmask, Gateway}} option in the eth config sets a
fixed address instead of DHCP. It is set on the netif with the DHCP
client stopped before the link comes up, so esp_netif's connected action
posts ETH_GOT_IP itself and the client never starts: starting it, even
to stop it right after, clears the DNS servers, which lwIP shares with
the other netifs, and the station lost name resolution.
A gateway of {0, 0, 0, 0} is none, for a cable to a field network (a PLC
or a pump with a fixed address) that is not the way out; route_priority
keeps the default route on Wi-Fi or PPP.
Signed-off-by: Gabriel Mancini <gabriel.mancini@gmail.com>
|
AMP code:
Review: fixed IPv4 for ESP32 EthernetCommit: The oracle performed an independent read-only review. Both findings below were then checked against the repository and local ESP-IDF v5.5.5 source. The local v6.1-development checkout also contains the DNS-clearing setter. These are source-confirmed findings, not hardware reproductions. No firmware build, emulator run, or device test was performed. Suggested patches below are not applied. 1. [P1] Setting the static address still clears shared DNSLocation: The new static-address path stops DHCP and then calls This directly defeats the change's stated DNS-preservation goal. A concrete failing ordering is:
The schedule is conditional; the destructive setter call itself is unconditional on link state and is confirmed in the SDK source. A later STA DHCP response can mask the problem by repopulating DNS. Supporting SDK source (relative to
Suggested small fixSupply the static address in the netif creation configuration instead of calling the DNS-clearing setter. Retain the DHCP stop before starting Ethernet: it transitions the new client's state from INIT to STOPPED without starting DHCP or discarding the configured address. diff --git a/src/platforms/esp32/components/avm_builtins/network_driver.c b/src/platforms/esp32/components/avm_builtins/network_driver.c
--- a/src/platforms/esp32/components/avm_builtins/network_driver.c
+++ b/src/platforms/esp32/components/avm_builtins/network_driver.c
@@
esp_netif_inherent_config_t netif_base = ESP_NETIF_INHERENT_DEFAULT_ETH();
netif_base.route_prio = route_priority;
+ if (data->eth_static) {
+ netif_base.ip_info = &data->eth_ip;
+ }
esp_netif_config_t netif_config = { .base = &netif_base, .driver = NULL, .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH };
@@
err = esp_netif_dhcpc_stop(data->eth_netif);
- if ((err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED)
- || (err = esp_netif_set_ip_info(data->eth_netif, &data->eth_ip)) != ESP_OK) {
+ if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
goto error;
}Do not simply remove Required regression: In an isolated ESP32/OpenETH integration fixture, initialize the network stack, seed distinct main and backup DNS servers, start only static Ethernet through the public network API, and assert that both entries remain unchanged. For example, use main 2. [P2] Reject zero address/netmask before reporting successful startupLocation:
For example, on a working link these configurations make [{ipv4, {{0, 0, 0, 0}, {255, 255, 255, 0}, {0, 0, 0, 0}}}]
[{ipv4, {{10, 0, 2, 42}, {0, 0, 0, 0}, {0, 0, 0, 0}}}]Evidence: Suggested small fixMatch the SDK's minimum static-address validity requirement before initializing hardware. Keep zero gateway valid. This intentionally rejects diff --git a/src/platforms/esp32/components/avm_builtins/network_driver.c b/src/platforms/esp32/components/avm_builtins/network_driver.c
--- a/src/platforms/esp32/components/avm_builtins/network_driver.c
+++ b/src/platforms/esp32/components/avm_builtins/network_driver.c
@@
if (!term_is_tuple(ipv4) || term_get_tuple_arity(ipv4) != 3
|| !term_to_ip4(term_get_tuple_element(ipv4, 0), &data->eth_ip.ip)
|| !term_to_ip4(term_get_tuple_element(ipv4, 1), &data->eth_ip.netmask)
- || !term_to_ip4(term_get_tuple_element(ipv4, 2), &data->eth_ip.gw)) {
+ || !term_to_ip4(term_get_tuple_element(ipv4, 2), &data->eth_ip.gw)
+ || data->eth_ip.ip.addr == 0
+ || data->eth_ip.netmask.addr == 0) {
return false;
}Missing tests: suggested public-interface coverageThe commit adds no tests. Extend diff --git a/src/platforms/esp32/test/main/test_erl_sources/test_eth.erl b/src/platforms/esp32/test/main/test_erl_sources/test_eth.erl
--- a/src/platforms/esp32/test/main/test_erl_sources/test_eth.erl
+++ b/src/platforms/esp32/test/main/test_erl_sources/test_eth.erl
@@
start() ->
ok = test_badarg(),
ok = test_wait_for_eth_refused(),
+ ok = test_static_ip(),
ok = test_callbacks_in_order(),
@@
- [{route_priority, 256}]
+ [{route_priority, 256}],
+ [{ipv4, invalid}],
+ [{ipv4, {{10, 0, 2, 42}, {255, 255, 255, 0}}}],
+ [{ipv4, {{10, 0, 42}, {255, 255, 255, 0}, {0, 0, 0, 0}}}],
+ [{ipv4, {{10, 0, 2, -1}, {255, 255, 255, 0}, {0, 0, 0, 0}}}],
+ [{ipv4, {{10, 0, 2, 42}, {255, 255, 255, 256}, {0, 0, 0, 0}}}],
+ [{ipv4, {{10, 0, 2, 42}, {255, 255, 255, 0}, {0, 0, 0, 1 bsl 40}}}],
+ [{ipv4, {{0, 0, 0, 0}, {255, 255, 255, 0}, {0, 0, 0, 0}}}],
+ [{ipv4, {{10, 0, 2, 42}, {0, 0, 0, 0}, {0, 0, 0, 0}}}]
]).
@@
-%% Pid callbacks are sent from the network gen_server itself, so they arrive in
+test_static_ip() ->
+ IpInfo = {{10, 0, 2, 42}, {255, 255, 255, 0}, {0, 0, 0, 0}},
+ lists:foreach(
+ fun(_) ->
+ {ok, IpInfo} = network:wait_for_eth([{ipv4, IpInfo}], 5000),
+ connected = network:eth_status(),
+ ok = network:stop()
+ end,
+ [first, second]
+ ).
+
+%% Pid callbacks are sent from the network gen_server itself, so they arrive in
%% the order of the driver events.Also exercise static Small documentation suggestions (non-blocking)The Ethernet diff --git a/doc/src/network-programming-guide.md b/doc/src/network-programming-guide.md
--- a/doc/src/network-programming-guide.md
+++ b/doc/src/network-programming-guide.md
@@
-Invalid pins, a `phy_addr` out of range or a malformed `ipv4` make `network:start/1` return
-`{error, badarg}`.
+Invalid pins, a `phy_addr` out of range or an invalid `ipv4` make the network server terminate
+with `{start_port_failed, badarg}` after `network:start/1` returns `{ok, Pid}`.
+`network:wait_for_eth/2` monitors startup and returns `{error, badarg}` instead.In the Ethernet table, replace “DHCP lease obtained” with “IPv4 address ready (DHCP or static)”. In Review boundaries
|
e5354bc to
20e7168
Compare
|
Thanks for the review. Both findings are fixed in a new commit on top ( 1. [P1] DNS cleared by the setter. Confirmed in IDF 5.5.5
Regression: a new native case, I ran the ESP32 suite in QEMU (IDF 5.5.5, OpenETH):
2. [P2] Zero address or netmask. These are now refused with Tests. Your
Docs.
Not covered here: unplug/replug without a DHCP server, and mixed STA plus static Ethernet routing. Our fleet runs that combination on hardware (Wi-Fi uplink with route priority 100, fixed Ethernet to a Modbus device with priority 2). I will move those boards to this commit and report back. |
|
Hardware follow-up: the fix is on an ESP32 without PSRAM in our fleet. It runs Wi-Fi STA as the uplink (route priority 100) and a fixed Ethernet address
Same-subnet traffic goes to the Ethernet peer, and everything else goes over the STA. |
esp_netif_set_ip_info() clears lwIP's main and backup DNS servers for a
netif with the DHCP client flag, even with the client stopped, and lwIP
shares them with every interface: a fixed Ethernet address started next
to Wi-Fi could wipe the DNS servers the STA got. The address is now given
at netif creation (esp_netif_new copies base->ip_info without the
setter). The DHCP client is still stopped before the link comes up, and
the flag kept: without it esp_netif_action_connected() returns before it
posts ETH_GOT_IP.
A zero address or netmask is refused with badarg: the SDK's connected
handler rejects them as an invalid static IP and never posts got_ip, so
wait_for_eth/2 timed out instead.
Tests: test_eth gains malformed and zero ipv4 cases and two start/stop
cycles of a fixed address; the new native test_eth_static_dns seeds the
DNS servers, starts a fixed address and checks both are kept (it fails
with the setter). The guide and wait_for_eth/2 docs say got_ip is DHCP
or fixed, and that a bad configuration terminates the network server
with {start_port_failed, badarg} while wait_for_eth/2 returns
{error, badarg}.
Signed-off-by: Gabriel Mancini <gabriel.mancini@gmail.com>
20e7168 to
d0df010
Compare
|
Formatting-only amend: the |
Adds an
{ipv4, {Address, Netmask, Gateway}}option to theethconfiguration ofnetwork:start/1on ESP32. With it, the Ethernet netif uses a fixed address instead of DHCP.Use case
The cable goes to a field network with fixed addresses and no DHCP server, such as a PLC or a dosing pump speaking Modbus TCP, while STA or PPP stay the way out. A gateway of
{0, 0, 0, 0}and aroute_prioritybelow theirs keep the default route on them:{eth, [..., {route_priority, 2}, {ipv4, {{10, 10, 0, 50}, {255, 255, 255, 0}, {0, 0, 0, 0}}}]}How
The address is set on the netif, with the DHCP client stopped, before the link comes up. esp_netif's connected action then sees a stopped client and a valid static address, and posts
ETH_GOT_IPitself, sogot_ipstill fires as with DHCP.The DHCP client never starts. This matters because
esp_netif_dhcpc_startcallsdns_clear_servers, and lwIP's DNS servers are global. A first version started the client on link up and stopped it right after. That wiped the DNS servers the STA had obtained, and the station lost name resolution.Tests
CONFIG_AVM_ENABLE_ETHERNET=y.network_driver.c, and erlfmt passes onnetwork.erl.10.10.0.50/24cabled straight to a PC at10.10.0.11, and STA up at the same time.The docs (Ethernet options table) and the CHANGELOG are updated.
These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).
SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later