Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gnrc_netif: use event loops by default to process ISR #16748

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ PSEUDOMODULES += gnrc_neterr
PSEUDOMODULES += gnrc_netapi_callbacks
PSEUDOMODULES += gnrc_netapi_mbox
PSEUDOMODULES += gnrc_netif_bus
PSEUDOMODULES += gnrc_netif_events
PSEUDOMODULES += gnrc_netif_timestamp
PSEUDOMODULES += gnrc_pktbuf_cmd
PSEUDOMODULES += gnrc_netif_6lo
Expand Down
2 changes: 0 additions & 2 deletions sys/include/net/gnrc/netif.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ typedef struct {
* @see net_gnrc_netif_flags
*/
uint32_t flags;
#if IS_USED(MODULE_GNRC_NETIF_EVENTS) || defined(DOXYGEN)
/**
* @brief Event queue for asynchronous events
*/
Expand All @@ -148,7 +147,6 @@ typedef struct {
* @brief ISR event for the network device
*/
event_t event_isr;
#endif /* MODULE_GNRC_NETIF_EVENTS */
#if (GNRC_NETIF_L2ADDR_MAXLEN > 0) || DOXYGEN
/**
* @brief The link-layer address currently used as the source address
Expand Down
5 changes: 0 additions & 5 deletions sys/include/net/gnrc/netif/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ extern "C" {
*/
#define GNRC_NETIF_PKTQ_DEQUEUE_MSG (0x1233)

/**
* @brief Message type for @ref netdev_event_t "netdev events"
*/
#define NETDEV_MSG_TYPE_EVENT (0x1234)

/**
* @brief Acquires exclusive access to the interface
*
Expand Down
7 changes: 2 additions & 5 deletions sys/net/gnrc/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ ifneq (,$(filter gnrc_netif_bus,$(USEMODULE)))
USEMODULE += core_msg_bus
endif

ifneq (,$(filter gnrc_netif_events,$(USEMODULE)))
USEMODULE += core_thread_flags
USEMODULE += event
endif

ifneq (,$(filter ieee802154 nrfmin esp_now cc110x gnrc_sixloenc,$(USEMODULE)))
ifneq (,$(filter gnrc_ipv6, $(USEMODULE)))
USEMODULE += gnrc_sixlowpan
Expand Down Expand Up @@ -466,6 +461,8 @@ endif

ifneq (,$(filter gnrc_netif_%,$(USEMODULE)))
USEMODULE += gnrc_netif
USEMODULE += core_thread_flags
USEMODULE += event
endif

ifneq (,$(filter gnrc_netif_pktq,$(USEMODULE)))
Expand Down
10 changes: 2 additions & 8 deletions sys/net/gnrc/link_layer/gomach/gomach.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <stdint.h>
#include <stdbool.h>

#include "event.h"
#include "random.h"
#include "timex.h"
#include "periph/rtt.h"
Expand Down Expand Up @@ -2031,14 +2032,7 @@ static void _gomach_event_cb(netdev_t *dev, netdev_event_t event)
gnrc_netif_t *netif = (gnrc_netif_t *) dev->context;

if (event == NETDEV_EVENT_ISR) {
msg_t msg;

msg.type = NETDEV_MSG_TYPE_EVENT;
msg.content.ptr = (void *) netif;

if (msg_send(&msg, netif->pid) <= 0) {
DEBUG("[GOMACH] gnrc_netdev: possibly lost interrupt.\n");
}
event_post(&netif->evq, &netif->event_isr);
}
else {
DEBUG("gnrc_netdev: event triggered -> %i\n", event);
Expand Down
10 changes: 2 additions & 8 deletions sys/net/gnrc/link_layer/lwmac/lwmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <stdint.h>
#include <stdbool.h>

#include "event.h"
#include "od.h"
#include "timex.h"
#include "random.h"
Expand Down Expand Up @@ -796,14 +797,7 @@ static void _lwmac_event_cb(netdev_t *dev, netdev_event_t event)
gnrc_netif_t *netif = (gnrc_netif_t *) dev->context;

if (event == NETDEV_EVENT_ISR) {
msg_t msg;

msg.type = NETDEV_MSG_TYPE_EVENT;
msg.content.ptr = (void *) netif;

if (msg_send(&msg, netif->pid) <= 0) {
LOG_WARNING("WARNING: [LWMAC] gnrc_netdev: possibly lost interrupt.\n");
}
event_post(&netif->evq, &netif->event_isr);
}
else {
DEBUG("gnrc_netdev: event triggered -> %i\n", event);
Expand Down
100 changes: 23 additions & 77 deletions sys/net/gnrc/netif/gnrc_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,6 @@ int gnrc_netif_default_init(gnrc_netif_t *netif)

static void _send(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt, bool push_back);

#if IS_USED(MODULE_GNRC_NETIF_EVENTS)
/**
* @brief Call the ISR handler from an event
*
Expand All @@ -1616,16 +1615,6 @@ static void _event_handler_isr(event_t *evp)
gnrc_netif_t *netif = container_of(evp, gnrc_netif_t, event_isr);
netif->dev->driver->isr(netif->dev);
}
#endif

static inline void _event_post(gnrc_netif_t *netif)
{
#if IS_USED(MODULE_GNRC_NETIF_EVENTS)
event_post(&netif->evq, &netif->event_isr);
#else
(void)netif;
#endif
}

static void _process_receive_stats(gnrc_netif_t *netdev, gnrc_pktsnip_t *pkt)
{
Expand All @@ -1648,24 +1637,6 @@ static void _process_receive_stats(gnrc_netif_t *netdev, gnrc_pktsnip_t *pkt)
netstats_nb_update_rx(&netdev->netif, src, src_len, hdr->rssi, hdr->lqi);
}

/**
* @brief Retrieve the netif event queue if enabled
*
* @param[in] netif gnrc_netif instance to operate on
*
* @return NULL if MODULE_GNRC_NETIF_EVENTS is not enabled
* @return gnrc_netif_t::evq if MODULE_GNRC_NETIF_EVENTS is enabled
*/
static inline event_queue_t *_get_evq(gnrc_netif_t *netif)
{
#ifdef MODULE_GNRC_NETIF_EVENTS
return &netif->evq;
#else
(void)netif;
return NULL;
#endif
}

/**
* @brief Process any pending events and wait for IPC messages
*
Expand All @@ -1679,37 +1650,30 @@ static inline event_queue_t *_get_evq(gnrc_netif_t *netif)
*/
static void _process_events_await_msg(gnrc_netif_t *netif, msg_t *msg)
{
if (IS_USED(MODULE_GNRC_NETIF_EVENTS)) {
while (1) {
/* Using messages for external IPC, and events for internal events */

/* First drain the queues before blocking the thread */
/* Events will be handled before messages */
DEBUG("gnrc_netif: handling events\n");
event_t *evp;
/* We can not use event_loop() or event_wait() because then we would not
* wake up when a message arrives */
event_queue_t *evq = _get_evq(netif);
while ((evp = event_get(evq))) {
DEBUG("gnrc_netif: event %p\n", (void *)evp);
if (evp->handler) {
evp->handler(evp);
}
}
/* non-blocking msg check */
int msg_waiting = msg_try_receive(msg);
if (msg_waiting > 0) {
return;
while (1) {
/* Using messages for external IPC, and events for internal events */

/* First drain the queues before blocking the thread */
/* Events will be handled before messages */
DEBUG("gnrc_netif: handling events\n");
event_t *evp;
/* We can not use event_loop() or event_wait() because then we would not
* wake up when a message arrives */
event_queue_t *evq = &netif->evq;
while ((evp = event_get(evq))) {
DEBUG("gnrc_netif: event %p\n", (void *)evp);
if (evp->handler) {
evp->handler(evp);
}
DEBUG("gnrc_netif: waiting for events\n");
/* Block the thread until something interesting happens */
thread_flags_wait_any(THREAD_FLAG_MSG_WAITING | THREAD_FLAG_EVENT);
}
}
else {
/* Only messages used for event handling */
DEBUG("gnrc_netif: waiting for incoming messages\n");
msg_receive(msg);
/* non-blocking msg check */
int msg_waiting = msg_try_receive(msg);
if (msg_waiting > 0) {
return;
}
DEBUG("gnrc_netif: waiting for events\n");
/* Block the thread until something interesting happens */
thread_flags_wait_any(THREAD_FLAG_MSG_WAITING | THREAD_FLAG_EVENT);
}
}

Expand Down Expand Up @@ -1831,7 +1795,6 @@ static void *_gnrc_netif_thread(void *args)
_netif_ctx_t *ctx = args;
gnrc_netapi_opt_t *opt;
gnrc_netif_t *netif;
netdev_t *dev;
int res;
msg_t reply = { .type = GNRC_NETAPI_MSG_TYPE_ACK };
msg_t msg_queue[GNRC_NETIF_MSG_QUEUE_SIZE];
Expand All @@ -1841,11 +1804,9 @@ static void *_gnrc_netif_thread(void *args)
gnrc_netif_acquire(netif);
netif->pid = thread_getpid();

#if IS_USED(MODULE_GNRC_NETIF_EVENTS)
netif->event_isr.handler = _event_handler_isr,
/* set up the event queue */
event_queue_init(&netif->evq);
#endif /* MODULE_GNRC_NETIF_EVENTS */

/* setup the link-layer's message queue */
msg_init_queue(msg_queue, GNRC_NETIF_MSG_QUEUE_SIZE);
Expand All @@ -1857,7 +1818,6 @@ static void *_gnrc_netif_thread(void *args)
LOG_ERROR("gnrc_netif: init failed: %d\n", ctx->result);
return NULL;
}
dev = netif->dev;
#if DEVELHELP
assert(options_tested);
#endif
Expand Down Expand Up @@ -1885,10 +1845,6 @@ static void *_gnrc_netif_thread(void *args)
_send_queued_pkt(netif);
break;
#endif /* IS_USED(MODULE_GNRC_NETIF_PKTQ) */
case NETDEV_MSG_TYPE_EVENT:
DEBUG("gnrc_netif: GNRC_NETDEV_MSG_TYPE_EVENT received\n");
dev->driver->isr(dev);
break;
case GNRC_NETAPI_MSG_TYPE_SND:
DEBUG("gnrc_netif: GNRC_NETDEV_MSG_TYPE_SND received\n");
_send(netif, msg.content.ptr, false);
Expand Down Expand Up @@ -1966,17 +1922,7 @@ static void _event_cb(netdev_t *dev, netdev_event_t event)
gnrc_netif_t *netif = (gnrc_netif_t *) dev->context;

if (event == NETDEV_EVENT_ISR) {
if (IS_USED(MODULE_GNRC_NETIF_EVENTS)) {
_event_post(netif);
}
else {
msg_t msg = { .type = NETDEV_MSG_TYPE_EVENT,
.content = { .ptr = netif } };

if (msg_send(&msg, netif->pid) <= 0) {
puts("gnrc_netif: possibly lost interrupt.");
}
}
event_post(&netif->evq, &netif->event_isr);
}
else {
DEBUG("gnrc_netif: event triggered -> %i\n", event);
Expand Down
7 changes: 1 addition & 6 deletions sys/net/gnrc/netif/lorawan/gnrc_netif_lorawan.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,7 @@ static void _driver_cb(netdev_t *dev, netdev_event_t event)
gnrc_lorawan_t *mac = &netif->lorawan.mac;

if (event == NETDEV_EVENT_ISR) {
msg_t msg = { .type = NETDEV_MSG_TYPE_EVENT,
.content = { .ptr = netif } };

if (msg_send(&msg, netif->pid) <= 0) {
DEBUG("gnrc_netif: possibly lost interrupt.\n");
}
event_post(&netif->evq, &netif->event_isr);
}
else {
DEBUG("gnrc_netif: event triggered -> %i\n", event);
Expand Down
2 changes: 1 addition & 1 deletion tests/gnrc_netif/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ static void test_netif_get_name(void)
TEST_ASSERT_NOT_NULL(netif);

res = netif_get_name(netif, name);
sprintf(exp_name, "%d", (int) ((gnrc_netif_t *)netif)->pid);
sprintf(exp_name, "%d", netif_get_id(netif));
TEST_ASSERT_EQUAL_INT(strlen(exp_name), res);
TEST_ASSERT_EQUAL_STRING(&exp_name[0], &name[0]);
}
Expand Down