Skip to content

Commit

Permalink
gnrc_netif: Add support for asynchronous events
Browse files Browse the repository at this point in the history
Enabled by the gnrc_netif_async_events pseudo module. Using asynchronous
events eliminates the risk of lost interrupts and lets ISR events always
be handled before any send/receive requests are processed.
The asynchronous events in the event loop is also a potential hook for
MAC layers and other link layer modules which may need to process events
outside of the normal message queue.
  • Loading branch information
Joakim Nohlgård committed Jun 11, 2018
1 parent 2006937 commit 1dfdad0
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 50 deletions.
4 changes: 4 additions & 0 deletions Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ ifneq (,$(filter gnrc_netif,$(USEMODULE)))
USEMODULE += netif
endif

ifneq (,$(filter gnrc_netif_async_events,$(USEMODULE)))
USEMODULE += event
endif

ifneq (,$(filter ieee802154 nrfmin,$(USEMODULE)))
ifneq (,$(filter gnrc_ipv6, $(USEMODULE)))
USEMODULE += gnrc_sixlowpan
Expand Down
1 change: 1 addition & 0 deletions makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ PSEUDOMODULES += gnrc_netdev_default
PSEUDOMODULES += gnrc_neterr
PSEUDOMODULES += gnrc_netapi_callbacks
PSEUDOMODULES += gnrc_netapi_mbox
PSEUDOMODULES += gnrc_netif_async_events
PSEUDOMODULES += gnrc_sixlowpan_border_router_default
PSEUDOMODULES += gnrc_sixlowpan_default
PSEUDOMODULES += gnrc_sixlowpan_iphc_nhc
Expand Down
28 changes: 28 additions & 0 deletions sys/include/net/gnrc/netif.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

#include "kernel_types.h"
#include "msg.h"
#ifdef MODULE_GNRC_NETIF_ASYNC_EVENTS
#include "event.h"
#endif /* MODULE_GNRC_NETIF_ASYNC_EVENTS */
#include "net/ipv6/addr.h"
#include "net/gnrc/netapi.h"
#include "net/gnrc/pkt.h"
Expand All @@ -56,6 +59,16 @@ extern "C" {
*/
typedef struct gnrc_netif_ops gnrc_netif_ops_t;

#ifdef MODULE_GNRC_NETIF_ASYNC_EVENTS
/**
* @brief Event type used for passing netdev pointers together with the event
*/
typedef struct {
event_t super;
netdev_t *dev;
} event_netdev_t;
#endif /* MODULE_GNRC_NETIF_ASYNC_EVENTS */

/**
* @brief Representation of a network interface
*/
Expand All @@ -75,6 +88,21 @@ typedef struct {
* @see net_gnrc_netif_flags
*/
uint32_t flags;
#if defined(MODULE_GNRC_NETIF_ASYNC_EVENTS) || DOXYGEN
/**
* @brief Event queue for asynchronous events
*/
event_queue_t evq;
/**
* @brief Pointer to event instance table
*
* This pointer is a way to pass an event instance allocated on the stack by
* the netif thread to the IRQ event handler called by the netdev driver
* while still keeping the allocation of specific events an implementation
* detail of gnrc_netif.c
*/
void *event_table;
#endif /* MODULE_GNRC_NETIF_ASYNC_EVENTS */
#if (GNRC_NETIF_L2ADDR_MAXLEN > 0)
/**
* @brief The link-layer address currently used as the source address
Expand Down
164 changes: 114 additions & 50 deletions sys/net/gnrc/netif/gnrc_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@

#define _NETIF_NETAPI_MSG_QUEUE_SIZE (8)

#ifdef MODULE_GNRC_NETIF_ASYNC_EVENTS
typedef struct {
event_netdev_t isr;
} event_table_t;
#endif /* MODULE_GNRC_NETIF_ASYNC_EVENTS */

static gnrc_netif_t _netifs[GNRC_NETIF_NUMOF];

static void _update_l2addr_from_dev(gnrc_netif_t *netif);
Expand Down Expand Up @@ -1221,6 +1227,19 @@ static void _init_from_device(gnrc_netif_t *netif)
_update_l2addr_from_dev(netif);
}

#ifdef MODULE_GNRC_NETIF_ASYNC_EVENTS
/**
* @brief Call the ISR handler from an asynchronous event
*
* @param[in] evp pointer to the event
*/
static void _event_handler_isr(event_t *evp)
{
netdev_t *dev = ((event_netdev_t*)evp)->dev;
dev->driver->isr(dev);
}
#endif /* MODULE_GNRC_NETIF_ASYNC_EVENTS */

static void *_gnrc_netif_thread(void *args)
{
gnrc_netapi_opt_t *opt;
Expand All @@ -1235,6 +1254,17 @@ static void *_gnrc_netif_thread(void *args)
gnrc_netif_acquire(netif);
dev = netif->dev;
netif->pid = sched_active_pid;
#ifdef MODULE_GNRC_NETIF_ASYNC_EVENTS
event_table_t event_table = {
.isr = {
.super = { .handler = _event_handler_isr, },
.dev = dev,
},
};
netif->event_table = &event_table;
/* set up the event queue */
event_queue_init(&netif->evq);
#endif /* MODULE_GNRC_NETIF_ASYNC_EVENTS */
/* setup the link-layer's message queue */
msg_init_queue(msg_queue, _NETIF_NETAPI_MSG_QUEUE_SIZE);
/* register the event callback with the device driver */
Expand All @@ -1254,63 +1284,92 @@ static void *_gnrc_netif_thread(void *args)
gnrc_netif_release(netif);

while (1) {
int msg_waiting = 0;
#ifdef MODULE_GNRC_NETIF_ASYNC_EVENTS
/* Using messages for ordered, synchronous IPC, and events for
* unordered, asynchronous events */
DEBUG("gnrc_netif: waiting for events\n");
/* We can not use event_loop() or event_wait() because then we would not
* wake up when a message arrives */
thread_flags_t flags = thread_flags_wait_any(
THREAD_FLAG_MSG_WAITING | THREAD_FLAG_EVENT);
/* Events will be handled before messages */
if (flags & THREAD_FLAG_EVENT) {
DEBUG("gnrc_netif: handling events\n");
event_t *evp;
while ((evp = event_get(&netif->evq))) {
DEBUG("gnrc_netif: event %p\n", (void *)evp);
evp->handler(evp);
}
}
if (flags & THREAD_FLAG_MSG_WAITING) {
/* non-blocking msg check */
msg_waiting = msg_try_receive(&msg);
}
#else /* MODULE_GNRC_NETIF_ASYNC_EVENTS */
/* Only messages used for event handling */
DEBUG("gnrc_netif: waiting for incoming messages\n");
msg_receive(&msg);
/* dispatch netdev, MAC and gnrc_netapi messages */
switch (msg.type) {
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");
res = netif->ops->send(netif, msg.content.ptr);
if (res < 0) {
DEBUG("gnrc_netif: error sending packet %p (code: %u)\n",
msg.content.ptr, res);
}
break;
case GNRC_NETAPI_MSG_TYPE_SET:
opt = msg.content.ptr;
msg_waiting = msg_receive(&msg);
#endif /* MODULE_GNRC_NETIF_ASYNC_EVENTS */
while (msg_waiting > 0) {
/* dispatch netdev, MAC and gnrc_netapi messages */
DEBUG("gnrc_netif: message %u\n", (unsigned)msg.type);
switch (msg.type) {
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");
res = netif->ops->send(netif, msg.content.ptr);
if (res < 0) {
DEBUG("gnrc_netif: error sending packet %p (code: %u)\n",
msg.content.ptr, res);
}
break;
case GNRC_NETAPI_MSG_TYPE_SET:
opt = msg.content.ptr;
#ifdef MODULE_NETOPT
DEBUG("gnrc_netif: GNRC_NETAPI_MSG_TYPE_SET received. opt=%s\n",
netopt2str(opt->opt));
DEBUG("gnrc_netif: GNRC_NETAPI_MSG_TYPE_SET received. opt=%s\n",
netopt2str(opt->opt));
#else
DEBUG("gnrc_netif: GNRC_NETAPI_MSG_TYPE_SET received. opt=%d\n",
opt->opt);
DEBUG("gnrc_netif: GNRC_NETAPI_MSG_TYPE_SET received. opt=%d\n",
opt->opt);
#endif
/* set option for device driver */
res = netif->ops->set(netif, opt);
DEBUG("gnrc_netif: response of netif->ops->set(): %i\n", res);
reply.content.value = (uint32_t)res;
msg_reply(&msg, &reply);
break;
case GNRC_NETAPI_MSG_TYPE_GET:
opt = msg.content.ptr;
/* set option for device driver */
res = netif->ops->set(netif, opt);
DEBUG("gnrc_netif: response of netif->ops->set(): %i\n", res);
reply.content.value = (uint32_t)res;
msg_reply(&msg, &reply);
break;
case GNRC_NETAPI_MSG_TYPE_GET:
opt = msg.content.ptr;
#ifdef MODULE_NETOPT
DEBUG("gnrc_netif: GNRC_NETAPI_MSG_TYPE_GET received. opt=%s\n",
netopt2str(opt->opt));
DEBUG("gnrc_netif: GNRC_NETAPI_MSG_TYPE_GET received. opt=%s\n",
netopt2str(opt->opt));
#else
DEBUG("gnrc_netif: GNRC_NETAPI_MSG_TYPE_GET received. opt=%d\n",
opt->opt);
DEBUG("gnrc_netif: GNRC_NETAPI_MSG_TYPE_GET received. opt=%d\n",
opt->opt);
#endif
/* get option from device driver */
res = netif->ops->get(netif, opt);
DEBUG("gnrc_netif: response of netif->ops->get(): %i\n", res);
reply.content.value = (uint32_t)res;
msg_reply(&msg, &reply);
break;
default:
if (netif->ops->msg_handler) {
DEBUG("gnrc_netif: delegate message of type 0x%04x to "
"netif->ops->msg_handler()\n", msg.type);
netif->ops->msg_handler(netif, &msg);
}
else {
DEBUG("gnrc_netif: unknown message type 0x%04x"
"(no message handler defined)\n", msg.type);
}
break;
/* get option from device driver */
res = netif->ops->get(netif, opt);
DEBUG("gnrc_netif: response of netif->ops->get(): %i\n", res);
reply.content.value = (uint32_t)res;
msg_reply(&msg, &reply);
break;
default:
if (netif->ops->msg_handler) {
DEBUG("gnrc_netif: delegate message of type 0x%04x to "
"netif->ops->msg_handler()\n", msg.type);
netif->ops->msg_handler(netif, &msg);
}
else {
DEBUG("gnrc_netif: unknown message type 0x%04x"
"(no message handler defined)\n", msg.type);
}
break;
}
msg_waiting = msg_try_receive(&msg);
}
}
/* never reached */
Expand All @@ -1332,12 +1391,17 @@ 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) {
#ifdef MODULE_GNRC_NETIF_ASYNC_EVENTS
event_table_t *etp = netif->event_table;
event_post(&netif->evq, (event_t *)&etp->isr);
#else /* MODULE_GNRC_NETIF_ASYNC_EVENTS */
msg_t msg = { .type = NETDEV_MSG_TYPE_EVENT,
.content = { .ptr = netif } };

if (msg_send(&msg, netif->pid) <= 0) {
puts("gnrc_netif: possibly lost interrupt.");
}
#endif /* MODULE_GNRC_NETIF_ASYNC_EVENTS */
}
else {
DEBUG("gnrc_netif: event triggered -> %i\n", event);
Expand Down

0 comments on commit 1dfdad0

Please sign in to comment.