Skip to content

Commit

Permalink
pkg/lwip: use sys/event for handling ISR and bhp
Browse files Browse the repository at this point in the history
This is directly useful for the two driver using the bhp API, but also
other drivers profit from not loosing IRQs.

The main selling point is that this paves the way for implementing
netdev_driver_t::confirm_send().
  • Loading branch information
maribu committed Apr 26, 2024
1 parent 925644e commit 96ed4f4
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 135 deletions.
3 changes: 2 additions & 1 deletion pkg/lwip/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
FEATURES_REQUIRED_ANY += arch_32bit|arch_64bit

DEFAULT_MODULE += auto_init_lwip
USEMODULE += event

ifneq (,$(filter sock_async,$(USEMODULE)))
USEMODULE += lwip_sock_async
Expand Down Expand Up @@ -90,7 +91,7 @@ ifneq (,$(filter lwip_contrib,$(USEMODULE)))
USEMODULE += sema
USEMODULE += ztimer_msec
ifneq (,$(filter bhp,$(USEMODULE)))
USEMODULE += bhp_msg
USEMODULE += bhp_event
endif
endif

Expand Down
103 changes: 46 additions & 57 deletions pkg/lwip/contrib/netdev/lwip_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@
*/

#include <assert.h>
#include <sys/uio.h>
#include <inttypes.h>
#include <sys/uio.h>

#include "architecture.h"
#include "event.h"
#include "lwip.h"
#include "lwip/err.h"
#include "lwip/ethip6.h"
#include "lwip/netif.h"
#include "lwip/netifapi.h"
#include "lwip/netif/compat.h"
#include "lwip/netif/netdev.h"
#include "lwip/netifapi.h"
#include "lwip/opt.h"
#include "lwip/pbuf.h"
#include "netif/etharp.h"
#include "netif/lowpan6.h"

#include "net/eui64.h"
#include "net/ieee802154.h"
#include "net/ipv6/addr.h"
#include "net/netdev.h"
#include "net/netopt.h"
#include "utlist.h"
#include "netif/etharp.h"
#include "netif/lowpan6.h"
#include "thread.h"

#define ENABLE_DEBUG 0
Expand All @@ -42,7 +43,6 @@
#define LWIP_NETDEV_NAME "lwip_netdev_mux"
#define LWIP_NETDEV_PRIO (THREAD_PRIORITY_MAIN - 4)
#define LWIP_NETDEV_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
#define LWIP_NETDEV_QUEUE_LEN (8)
#define LWIP_NETDEV_MSG_TYPE_EVENT 0x1235

#define ETHERNET_IFNAME1 'E'
Expand All @@ -51,9 +51,10 @@
#define WPAN_IFNAME1 'W'
#define WPAN_IFNAME2 'P'

event_queue_t lwip_event_queue;

static kernel_pid_t _pid = KERNEL_PID_UNDEF;
static char _stack[LWIP_NETDEV_STACKSIZE];
static msg_t _queue[LWIP_NETDEV_QUEUE_LEN];
static WORD_ALIGNED char _stack[LWIP_NETDEV_STACKSIZE];
static char _tmp_buf[LWIP_NETDEV_BUFLEN];

#ifdef MODULE_NETDEV_ETH
Expand All @@ -73,15 +74,19 @@ static err_t slip_output6(struct netif *netif, struct pbuf *q, const ip6_addr_t
#endif
static void _event_cb(netdev_t *dev, netdev_event_t event);
static void *_event_loop(void *arg);
static void _isr(event_t *ev);

err_t lwip_netdev_init(struct netif *netif)
{
LWIP_ASSERT("netif != NULL", (netif != NULL));
LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
lwip_netif_t *compat_netif = container_of(netif, lwip_netif_t, lwip_netif);
compat_netif->ev_isr.handler = _isr;
netdev_t *netdev;
netopt_enable_t enabled = 0;
uint16_t dev_type;
err_t res = ERR_OK;
event_queue_init_detached(&lwip_event_queue);

/* Init device lock */
lwip_netif_dev_lock_init(netif);
Expand All @@ -96,11 +101,6 @@ err_t lwip_netdev_init(struct netif *netif)
}
}

/* initialize Bottom Half Processor, netdev and netif */
if (IS_USED(MODULE_BHP_MSG)) {
bhp_msg_claim_thread(lwip_netif_get_bhp(netif), _pid);
}

netdev = netif->state;
lwip_netif_dev_acquire(netif);
netdev->event_callback = _event_cb;
Expand Down Expand Up @@ -364,22 +364,16 @@ static struct pbuf *_get_recv_pkt(netdev_t *dev)

static void _event_cb(netdev_t *dev, netdev_event_t event)
{
if (event == NETDEV_EVENT_ISR) {
assert(_pid != KERNEL_PID_UNDEF);
msg_t msg;

msg.type = LWIP_NETDEV_MSG_TYPE_EVENT;
msg.content.ptr = dev;
lwip_netif_t *compat_netif = dev->context;
assert(compat_netif != NULL);
struct netif *netif = &compat_netif->lwip_netif;

if (msg_send(&msg, _pid) <= 0) {
DEBUG("lwip_netdev: possibly lost interrupt.\n");
}
}
else {
lwip_netif_t *compat_netif = dev->context;
struct netif *netif = &compat_netif->lwip_netif;
switch (event) {
case NETDEV_EVENT_RX_COMPLETE: {
switch (event) {
case NETDEV_EVENT_ISR:
event_post(&lwip_event_queue, &compat_netif->ev_isr);
break;
case NETDEV_EVENT_RX_COMPLETE:
{
struct pbuf *p = _get_recv_pkt(dev);
if (p == NULL) {
DEBUG("lwip_netdev: error receiving packet\n");
Expand All @@ -389,40 +383,35 @@ static void _event_cb(netdev_t *dev, netdev_event_t event)
DEBUG("lwip_netdev: error inputing packet\n");
return;
}
break;
}
case NETDEV_EVENT_LINK_UP: {
/* Will wake up DHCP state machine */
netifapi_netif_set_link_up(netif);
break;
}
case NETDEV_EVENT_LINK_DOWN: {
netifapi_netif_set_link_down(netif);
break;
}
default:
break;
}
break;
case NETDEV_EVENT_LINK_UP:
/* Will wake up DHCP state machine */
netifapi_netif_set_link_up(netif);
break;
case NETDEV_EVENT_LINK_DOWN:
netifapi_netif_set_link_down(netif);
break;
default:
break;
}
}

static void _isr(event_t *ev)
{
lwip_netif_t *compat_netif = container_of(ev, lwip_netif_t, ev_isr);
netdev_t *dev = compat_netif->lwip_netif.state;
dev->driver->isr(dev);
}

static void *_event_loop(void *arg)
{
struct netif *netif = arg;
msg_init_queue(_queue, LWIP_NETDEV_QUEUE_LEN);
while (1) {
msg_t msg;
msg_receive(&msg);
if (msg.type == LWIP_NETDEV_MSG_TYPE_EVENT) {
netdev_t *dev = msg.content.ptr;
lwip_netif_dev_acquire(netif);
dev->driver->isr(dev);
lwip_netif_dev_release(netif);
}
else if (IS_USED(MODULE_BHP_MSG) && msg.type == BHP_MSG_BH_REQUEST) {
bhp_msg_handler(&msg);
}
}
(void)arg;
event_queue_claim(&lwip_event_queue);
event_loop(&lwip_event_queue);

/* this should never be reached */
assert(0);
return NULL;
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/lwip/include/lwip.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
#ifndef LWIP_H
#define LWIP_H

#include "event.h"

#ifdef __cplusplus
extern "C" {
#endif

extern event_queue_t lwip_event_queue;

Check failure on line 28 in pkg/lwip/include/lwip.h

View workflow job for this annotation

GitHub Actions / static-tests

Member lwip_event_queue (variable) of file lwip.h is not documented.

/**
* @brief Initializes lwIP stack.
*
Expand Down
34 changes: 8 additions & 26 deletions pkg/lwip/include/lwip/netif/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
#ifndef LWIP_NETIF_COMPAT_H
#define LWIP_NETIF_COMPAT_H

#include "bhp/event.h"
#include "event.h"
#include "lwip/netif.h"
#include "net/netif.h"
#include "bhp/msg.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -32,11 +33,12 @@ extern "C" {
* @brief Representation of a network interface
*/
typedef struct {
netif_t common_netif; /**< network interface descriptor */
struct netif lwip_netif; /**< lwIP interface data */
rmutex_t lock; /**< lock for the interface */
#if IS_USED(MODULE_BHP_MSG)
bhp_msg_t bhp; /**< IPC Bottom Half Processor */
netif_t common_netif; /**< network interface descriptor */
struct netif lwip_netif; /**< lwIP interface data */
rmutex_t lock; /**< lock for the interface */
event_t ev_isr; /**< ISR event */
#ifdef MODULE_BHP_EVENT
bhp_event_t bhp; /**< IPC Bottom Half Processor */
#endif
} lwip_netif_t;

Expand Down Expand Up @@ -73,26 +75,6 @@ static inline void lwip_netif_dev_release(struct netif *netif)
rmutex_unlock(&compat_netif->lock);
}

/**
* @brief Get the IPC based Bottom Half Processor for LWIP
*
* @param[in] netif pointer to the LWIP network interface
*
* @return pointer to the IPC based Bottom Half Processor descriptor, if
* @ref sys_bhp_msg is present.
* @return NULL otherwise
*/
static inline bhp_msg_t *lwip_netif_get_bhp(struct netif *netif)
{
#if IS_USED(MODULE_BHP_MSG)
lwip_netif_t *compat_netif = container_of(netif, lwip_netif_t, lwip_netif);
return &compat_netif->bhp;
#else
(void) netif;
return NULL;
#endif
}

#ifdef __cplusplus
}
#endif
Expand Down
22 changes: 12 additions & 10 deletions pkg/lwip/init_devs/auto_init_kw2xrf.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright (C) 2022 HAW Hamburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
*/
* Copyright (C) 2022 HAW Hamburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
*/

/**
* @ingroup sys_auto_init_lwip_netif
Expand All @@ -20,9 +20,10 @@
#include "kw2xrf.h"
#include "kw2xrf_params.h"

#include "lwip.h"
#include "lwip_init_devs.h"

#include "bhp/msg.h"
#include "bhp/event.h"
#include "net/netdev/ieee802154_submac.h"

#define ENABLE_DEBUG 0
Expand All @@ -37,9 +38,10 @@ static netdev_ieee802154_submac_t kw2xrf_netdev[NETIF_KW2XRF_NUMOF];
static void auto_init_kw2xrf(void)
{
for (unsigned i = 0; i < NETIF_KW2XRF_NUMOF; i++) {
bhp_msg_init(&netif[i].bhp, &kw2xrf_radio_hal_irq_handler, &kw2xrf_netdev[i].submac.dev);
bhp_event_init(&netif[i].bhp, &lwip_event_queue,
&kw2xrf_radio_hal_irq_handler, &kw2xrf_netdev[i].submac.dev);
kw2xrf_init(&kw2xrf_devs[i], &kw2xrf_params[i], &kw2xrf_netdev[i].submac.dev,
bhp_msg_isr_cb, &netif[i].bhp);
bhp_event_isr_cb, &netif[i].bhp);

netdev_register(&kw2xrf_netdev[i].dev.netdev, NETDEV_KW2XRF, i);
netdev_ieee802154_submac_init(&kw2xrf_netdev[i]);
Expand Down
22 changes: 12 additions & 10 deletions pkg/lwip/init_devs/auto_init_mrf24j40.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright (C) 2017 Neo Nenaco <neo@nenaco.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
*/
* Copyright (C) 2017 Neo Nenaco <neo@nenaco.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
*/

/**
* @ingroup sys_auto_init_lwip_netif
Expand All @@ -21,8 +21,9 @@
#include "mrf24j40.h"
#include "mrf24j40_params.h"

#include "bhp/event.h"
#include "lwip.h"
#include "lwip_init_devs.h"
#include "bhp/msg.h"
#include "net/netdev/ieee802154_submac.h"

#define ENABLE_DEBUG 0
Expand All @@ -37,9 +38,10 @@ static netdev_ieee802154_submac_t mrf24j40_netdev[NETIF_MRF24J40_NUMOF];
static void auto_init_mrf24j40(void)
{
for (unsigned i = 0; i < NETIF_MRF24J40_NUMOF; i++) {
bhp_msg_init(&netif[i].bhp, &mrf24j40_radio_irq_handler, &mrf24j40_netdev[i].submac.dev);
bhp_event_init(&netif[i].bhp, &lwip_event_queue, &mrf24j40_radio_irq_handler,
&mrf24j40_netdev[i].submac.dev);
mrf24j40_init(&mrf24j40_devs[i], &mrf24j40_params[i], &mrf24j40_netdev[i].submac.dev,
bhp_msg_isr_cb, &netif[i].bhp);
bhp_event_isr_cb, &netif[i].bhp);


Check warning on line 46 in pkg/lwip/init_devs/auto_init_mrf24j40.c

View workflow job for this annotation

GitHub Actions / static-tests

too many consecutive empty lines
netdev_register(&mrf24j40_netdev[i].dev.netdev, NETDEV_MRF24J40, i);
Expand Down
31 changes: 0 additions & 31 deletions tests/pkg/lwip/Makefile.ci
Original file line number Diff line number Diff line change
@@ -1,31 +0,0 @@
BOARD_INSUFFICIENT_MEMORY := \
airfy-beacon \
blackpill-stm32f103c8 \
bluepill-stm32f030c8 \
bluepill-stm32f103c8 \
i-nucleo-lrwan1 \
nrf6310 \
nucleo-c031c6 \
nucleo-f030r8 \
nucleo-f031k6 \
nucleo-f042k6 \
nucleo-f302r8 \
nucleo-f303k8 \
nucleo-f334r8 \
nucleo-l011k4 \
nucleo-l031k6 \
nucleo-l053r8 \
samd10-xmini \
saml10-xpro \
saml11-xpro \
slstk3400a \
stk3200 \
stm32f030f4-demo \
stm32f0discovery \
stm32f7508-dk \
stm32g0316-disco \
stm32l0538-disco \
stm32mp157c-dk2 \
weact-g030f6 \
yunjia-nrf51822 \
#

0 comments on commit 96ed4f4

Please sign in to comment.