From 75c5084b4b5267398589cdcc37dfa145f021495f Mon Sep 17 00:00:00 2001 From: Seppo Takalo Date: Fri, 15 May 2026 16:00:04 +0300 Subject: [PATCH] app: PPP fixes for recovery Decrease LCP timeouts to one second. We don't need to realistically wait for longer on the Serial Modem side. Max retransmits set to 5 times, so total timeout is 5 seconds. Increase PPPD holdoff to twice of that, so it is 10 seconds. This allows enough time for Zephyr side to tear down the connection. Retry the net_if_down() as the first attempt might fail when there is heavy traffic. The LCP packet is still queued, so the second retry just tears down the interface in dormant state, so it does not send the LCP anymore. Lower the PPP thread priority to K_LOWEST_APPLICATION_THREAD_PRIO so it does not block any work queues. Signed-off-by: Seppo Takalo --- app/overlay-ppp.conf | 7 ++++-- app/scripts/sm2_start_ppp.sh | 2 +- app/src/sm_ppp.c | 44 ++++++++++++++++++++---------------- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/app/overlay-ppp.conf b/app/overlay-ppp.conf index 7c5d47af..1782540e 100644 --- a/app/overlay-ppp.conf +++ b/app/overlay-ppp.conf @@ -22,8 +22,11 @@ CONFIG_NET_L2_PPP_MGMT=y CONFIG_NET_L2_PPP_OPTION_MRU=y CONFIG_NET_L2_PPP_OPTION_SERVE_IP=y CONFIG_NET_L2_PPP_OPTION_SERVE_DNS=y -CONFIG_NET_L2_PPP_TIMEOUT=5000 -CONFIG_NET_L2_PPP_MAX_CONFIGURE_REQ_RETRANSMITS=20 +CONFIG_NET_L2_PPP_TIMEOUT=1000 +CONFIG_NET_L2_PPP_MAX_CONFIGURE_REQ_RETRANSMITS=5 +CONFIG_NET_L2_PPP_MAX_TERMINATE_REQ_RETRANSMITS=5 +CONFIG_NET_IF_MAX_IPV4_COUNT=4 +CONFIG_NET_IF_MAX_IPV6_COUNT=4 # IP stack CONFIG_NET_IP_ADDR_CHECK=n diff --git a/app/scripts/sm2_start_ppp.sh b/app/scripts/sm2_start_ppp.sh index b8f6f676..fcc43915 100755 --- a/app/scripts/sm2_start_ppp.sh +++ b/app/scripts/sm2_start_ppp.sh @@ -114,7 +114,7 @@ linkname nrf91 local passive persist -holdoff 5 +holdoff 10 nodetach noauth noipdefault diff --git a/app/src/sm_ppp.c b/app/src/sm_ppp.c index 6de57848..a5718cd3 100644 --- a/app/src/sm_ppp.c +++ b/app/src/sm_ppp.c @@ -46,9 +46,6 @@ static struct modem_pipe *ppp_urc_pipe; static struct k_thread ppp_data_passing_thread_id; static k_timepoint_t ppp_pdn_timeout; static K_THREAD_STACK_DEFINE(ppp_data_passing_thread_stack, KB(2)); -static void ppp_data_passing_thread(void*, void*, void*); -static void sm_ppp_activate_pdp_dwork_fn(struct k_work *work); -static K_WORK_DELAYABLE_DEFINE(activate_pdp_dwork, sm_ppp_activate_pdp_dwork_fn); enum ppp_action { PPP_START, @@ -104,6 +101,13 @@ const char *const ppp_socket_names[PPP_FDS_COUNT] = { }; static int ppp_fds[PPP_FDS_COUNT] = { -1, -1, -1 }; +/* Forward declarations */ +static void ppp_data_passing_thread(void*, void*, void*); +static void sm_ppp_activate_pdp_dwork_fn(struct k_work *work); +static int ppp_stop(enum ppp_reason reason); +static void ppp_cmd_fail_return_to_at_mode(void); +static K_WORK_DELAYABLE_DEFINE(activate_pdp_dwork, sm_ppp_activate_pdp_dwork_fn); + static const char *ppp_action_str(enum ppp_action action) { switch (action) { @@ -374,13 +378,7 @@ static int ppp_start(void) return 0; error: - ppp_state = PPP_STATE_STOPPED; - - if (ppp_pipe) { - modem_ppp_release(&ppp_module); - sm_at_host_attach(ppp_pipe); - } - ppp_pipe = NULL; + ppp_stop(PPP_REASON_ERROR); return ret; } @@ -399,6 +397,7 @@ static int ppp_stop(enum ppp_reason reason) } ppp_state = PPP_STATE_STOPPING; + close_ppp_sockets(); if (sm_ppp_keep_pipe_attached) { switch (reason) { @@ -415,13 +414,19 @@ static int ppp_stop(enum ppp_reason reason) at_monitor_pause(&sm_ppp_on_cgev); } - /* Bring the interface down before releasing pipes and carrier. - * This is needed for LCP to notify the remote endpoint that the link is going down. - */ - int ret = net_if_down(ppp_iface); + if (net_if_is_admin_up(ppp_iface)) { + /* Bring the interface down before releasing pipes and carrier. + * This is needed for LCP to notify the remote endpoint that the link is going down. + */ + int ret = net_if_down(ppp_iface); - if (ret) { - LOG_WRN("Failed to bring PPP interface down (%d).", ret); + if (ret) { + LOG_WRN("Failed to bring PPP interface down (%d).", ret); + /* Retry later */ + net_if_dormant_on(ppp_iface); + delegate_ppp_event(PPP_STOP, reason); + return ret; + } } modem_ppp_release(&ppp_module); @@ -436,8 +441,6 @@ static int ppp_stop(enum ppp_reason reason) net_if_carrier_off(ppp_iface); net_if_dormant_on(ppp_iface); - close_ppp_sockets(); - ppp_state = PPP_STATE_STOPPED; send_status_notification(); @@ -692,7 +695,7 @@ static int sm_ppp_init(void) k_thread_create(&ppp_data_passing_thread_id, ppp_data_passing_thread_stack, K_THREAD_STACK_SIZEOF(ppp_data_passing_thread_stack), ppp_data_passing_thread, NULL, NULL, NULL, - K_PRIO_COOP(10), 0, K_NO_WAIT); + K_LOWEST_APPLICATION_THREAD_PRIO, 0, K_NO_WAIT); k_thread_name_set(&ppp_data_passing_thread_id, "ppp_data_passing"); ppp_iface = modem_ppp_get_iface(&ppp_module); @@ -933,6 +936,7 @@ static void ppp_data_passing_thread(void*, void*, void*) } else { LOG_DBG("Connection down. Stop."); } + ppp_state = PPP_STATE_STOPPING; delegate_ppp_event(PPP_STOP, PPP_REASON_NETWORK); continue; } @@ -975,7 +979,7 @@ static void ppp_data_passing_thread(void*, void*, void*) LOG_ERR("Only sent %zd out of %zd bytes to %s socket.", send_ret, len, ppp_socket_names[dst]); } else { - LOG_DBG("Forwarded %zd bytes to %s socket.", + LOG_DBG_RATELIMIT_RATE(5000, "Forwarded %zd bytes to %s socket.", send_ret, ppp_socket_names[dst]); } }