Skip to content

Commit cfcfd11

Browse files
author
Arun S
committed
Remove ethernet interface logic for SoftAP and STA
Issue: The problem is that there is a race condition introduced in that the LWIP thread is relying on the interface as it is taken down by a application thread while calling disconnect. In disconnect api called from application context, whd_emac_wifi_link_state_changed() will refer to netif interface structure in its callback api netif_link_irq(netif). This netif will be cleared by remove_etherent_interface(). whd_emac_wifi_link_state_changed will post message to tcpip_thread. tcpip_thread will process the message and call the callback api netif_link_irq(netif) Calling sequence is whd_emac_wifi_link_state_changed -> remove_etherent_interface(). Hence there is a timing issue that netif might be cleared first before tcpip thread process the message netif_link_irq(netif) Fix: remove_etherent_interface() will post message to tcpip thread and tcpip thread process the message delete_interface() which will actually remove the inferface from the netif_list. Calling sequence is whd_emac_wifi_link_state_changed() message post -> remove_etherent_interface() message post. message processing order netif_link_irq(netif) -> delete_interface(). Since both the processing is handled in single thread, processing of message is handled sequentially.
1 parent 7fce7f5 commit cfcfd11

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

features/lwipstack/LWIPInterface.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ LWIP::Interface::Interface() :
388388
attr.cb_mem = &has_any_addr_sem;
389389
attr.cb_size = sizeof has_any_addr_sem;
390390
has_any_addr = osSemaphoreNew(UINT16_MAX, 0, &attr);
391+
392+
attr.cb_mem = &remove_interface_sem;
393+
attr.cb_size = sizeof remove_interface_sem;
394+
remove_interface = osSemaphoreNew(UINT16_MAX, 0, &attr);
391395
#if PREF_ADDR_TIMEOUT
392396
attr.cb_mem = &has_pref_addr_sem;
393397
attr.cb_size = sizeof has_pref_addr_sem;
@@ -459,6 +463,50 @@ nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardN
459463
#endif //LWIP_ETHERNET
460464
}
461465

466+
void LWIP::Interface::delete_interface(OnboardNetworkStack::Interface **interface_out)
467+
{
468+
#if LWIP_ETHERNET
469+
if ((interface_out != NULL) && (*interface_out != NULL)) {
470+
LWIP::Interface *lwip = static_cast<Interface *>(*interface_out);
471+
LWIP::Interface *node = lwip->list;
472+
473+
if (lwip->list != NULL) {
474+
if (lwip->list == lwip) {
475+
476+
lwip->list = lwip->list->next;
477+
netif_remove(&node->netif);
478+
*interface_out = NULL;
479+
delete node;
480+
} else {
481+
while (node->next != NULL && node->next != lwip) {
482+
node = node->next;
483+
}
484+
if (node->next != NULL && node->next == lwip) {
485+
Interface *remove = node->next;
486+
node->next = node->next->next;
487+
netif_remove(&remove->netif);
488+
*interface_out = NULL;
489+
delete remove;
490+
}
491+
}
492+
}
493+
osSemaphoreRelease(lwip->remove_interface);
494+
}
495+
#endif
496+
}
497+
498+
nsapi_error_t LWIP::remove_ethernet_interface(OnboardNetworkStack::Interface **interface_out)
499+
{
500+
#if LWIP_ETHERNET
501+
LWIP::Interface *lwip = static_cast<Interface *>(*interface_out);
502+
tcpip_callback_with_block((tcpip_callback_fn)&LWIP::Interface::delete_interface, interface_out, 1);
503+
osSemaphoreAcquire(lwip->remove_interface, osWaitForever);
504+
return NSAPI_ERROR_OK;
505+
#else
506+
return NSAPI_ERROR_UNSUPPORTED;
507+
#endif //LWIP_ETHERNET
508+
}
509+
462510
nsapi_error_t LWIP::add_l3ip_interface(L3IP &l3ip, bool default_if, OnboardNetworkStack::Interface **interface_out)
463511
{
464512
#if LWIP_L3IP

features/lwipstack/LWIPStack.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
128128
static void netif_link_irq(struct netif *netif);
129129
static void netif_status_irq(struct netif *netif);
130130
static Interface *our_if_from_netif(struct netif *netif);
131+
static void delete_interface(OnboardNetworkStack::Interface **interface_out);
131132

132133
#if LWIP_ETHERNET
133134
static err_t emac_low_level_output(struct netif *netif, struct pbuf *p);
@@ -187,6 +188,8 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
187188
void *hw; /**< alternative implementation pointer - used for PPP */
188189
};
189190

191+
mbed_rtos_storage_semaphore_t remove_interface_sem;
192+
osSemaphoreId_t remove_interface;
190193
mbed_rtos_storage_semaphore_t linked_sem;
191194
osSemaphoreId_t linked;
192195
mbed_rtos_storage_semaphore_t unlinked_sem;
@@ -261,6 +264,14 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
261264
*/
262265
nsapi_error_t add_ppp_interface(PPP &ppp, bool default_if, OnboardNetworkStack::Interface **interface_out) override;
263266

267+
/** Remove a network interface from IP stack
268+
*
269+
* Removes layer 3 IP objects,network interface from stack list .
270+
* @param[out] interface_out pointer to stack interface object controlling the EMAC
271+
* @return NSAPI_ERROR_OK on success, or error code
272+
*/
273+
nsapi_error_t remove_ethernet_interface(OnboardNetworkStack::Interface **interface_out) override;
274+
264275
/** Remove a network interface from IP stack
265276
*
266277
* Removes PPP objects,network interface from stack list, and shutdown device driver.

features/netsocket/OnboardNetworkStack.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ class OnboardNetworkStack : public NetworkStack {
149149
return NSAPI_ERROR_UNSUPPORTED;
150150
};
151151

152+
virtual nsapi_error_t remove_ethernet_interface(Interface **interface_out)
153+
{
154+
return NSAPI_ERROR_OK;
155+
};
156+
152157
virtual nsapi_error_t remove_l3ip_interface(Interface **interface_out)
153158
{
154159
return NSAPI_ERROR_OK;

features/netsocket/emac-drivers/TARGET_Cypress/COMPONENT_WHD/interface/WhdSTAInterface.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,15 @@ nsapi_error_t WhdSTAInterface::disconnect()
381381
}
382382
whd_emac_wifi_link_state_changed(_whd_emac.ifp, WHD_FALSE);
383383

384+
// remove the interface added in connect
385+
if (_interface) {
386+
nsapi_error_t err = _stack.remove_ethernet_interface(&_interface);
387+
if (err != NSAPI_ERROR_OK) {
388+
return err;
389+
}
390+
_iface_shared.iface_sta = NULL;
391+
}
392+
384393
res = whd_wifi_deregister_event_handler(_whd_emac.ifp, sta_link_update_entry);
385394
if (res != WHD_SUCCESS) {
386395
return whd_toerror(res);

features/netsocket/emac-drivers/TARGET_Cypress/COMPONENT_WHD/interface/WhdSoftAPInterface.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,15 @@ int WhdSoftAPInterface::stop(void)
201201
if (res != WHD_SUCCESS) {
202202
return whd_toerror(res);
203203
}
204+
205+
// remove the interface added in start
206+
if (_interface) {
207+
nsapi_error_t err = _stack.remove_ethernet_interface(&_interface);
208+
if (err != NSAPI_ERROR_OK) {
209+
return err;
210+
}
211+
_iface_shared.iface_softap = NULL;
212+
}
204213
return NSAPI_ERROR_OK;
205214
}
206215

0 commit comments

Comments
 (0)