From c09ec58f0191ce63a66dba9ceef16d06f9998613 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Wed, 26 Mar 2025 14:59:55 +0200 Subject: [PATCH] samples: net: dsa: Enable DHCPv4 and start it when interface is up Enable DHCPv4 when network interface goes up. This makes it easier to test IP connectivity with DSA. Signed-off-by: Jukka Rissanen Signed-off-by: Yangbo Lu --- samples/net/dsa/prj.conf | 12 +++++++++++- samples/net/dsa/src/main.c | 31 ++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/samples/net/dsa/prj.conf b/samples/net/dsa/prj.conf index 39edbd32639..02d94378f82 100644 --- a/samples/net/dsa/prj.conf +++ b/samples/net/dsa/prj.conf @@ -2,7 +2,7 @@ CONFIG_NETWORKING=y CONFIG_NET_LOG=y CONFIG_NET_IPV6=y CONFIG_NET_IPV4=y -CONFIG_NET_DHCPV4=n +CONFIG_NET_DHCPV4=y CONFIG_NET_UDP=y CONFIG_NET_TCP=y CONFIG_NET_STATISTICS=y @@ -18,6 +18,9 @@ CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=5 CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=5 CONFIG_NET_IF_UNICAST_IPV4_ADDR_COUNT=1 CONFIG_NET_MAX_CONTEXTS=4 +CONFIG_ZVFS_OPEN_MAX=4 +CONFIG_ZVFS_POLL_MAX=4 +CONFIG_NET_MAX_CONN=4 CONFIG_INIT_STACKS=y CONFIG_PRINTK=y @@ -46,3 +49,10 @@ CONFIG_REQUIRES_FULL_LIBC=y CONFIG_NET_IF_MAX_IPV4_COUNT=4 CONFIG_NET_IF_MAX_IPV6_COUNT=4 + +CONFIG_NET_MGMT=y +CONFIG_NET_MGMT_EVENT=y +CONFIG_NET_MGMT_EVENT_STACK_SIZE=2048 + +CONFIG_NET_HOSTNAME_ENABLE=y +CONFIG_NET_HOSTNAME="dsa" diff --git a/samples/net/dsa/src/main.c b/samples/net/dsa/src/main.c index 5e13473d374..16d41af5999 100644 --- a/samples/net/dsa/src/main.c +++ b/samples/net/dsa/src/main.c @@ -39,7 +39,8 @@ static void dsa_iface_find_cb(struct net_if *iface, void *user_data) if (user == NULL) { continue; } - LOG_INF("User interface %d found.", i); + LOG_INF("[%d] User interface %d found.", i, + net_if_get_by_iface(user)); ifaces->lan[i] = user; } @@ -48,12 +49,40 @@ static void dsa_iface_find_cb(struct net_if *iface, void *user_data) } } +#if defined(CONFIG_NET_MGMT_EVENT) +#define EVENT_MASK (NET_EVENT_IF_UP) +static struct net_mgmt_event_callback mgmt_cb; + +static void event_handler(struct net_mgmt_event_callback *cb, + uint32_t mgmt_event, struct net_if *iface) +{ + ARG_UNUSED(iface); + ARG_UNUSED(cb); + + if (mgmt_event == NET_EVENT_IF_UP) { + LOG_INF("Port %d is up", net_if_get_by_iface(iface)); + +#if defined(CONFIG_NET_DHCPV4) + net_dhcpv4_start(iface); +#endif + + return; + } +} +#endif /* CONFIG_NET_MGMT_EVENT */ + int main(void) { /* Initialize interfaces - read them to user_data */ (void)memset(&user_data, 0, sizeof(user_data)); net_if_foreach(dsa_iface_find_cb, &user_data); +#if defined(CONFIG_NET_MGMT_EVENT) + net_mgmt_init_event_callback(&mgmt_cb, + event_handler, EVENT_MASK); + net_mgmt_add_event_callback(&mgmt_cb); +#endif /* CONFIG_NET_MGMT_EVENT */ + #if defined(CONFIG_NET_SAMPLE_DSA_LLDP) dsa_lldp(&user_data); #endif