2019-03-25 11:15:15 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Freie Universität Berlin
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Martine Lenders <m.lenders@fu-berlin.de>
|
|
|
|
*/
|
|
|
|
|
2020-10-21 15:58:33 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2019-03-25 11:15:15 +01:00
|
|
|
#include "net/gnrc/pktqueue.h"
|
|
|
|
#include "net/gnrc/netif/conf.h"
|
|
|
|
#include "net/gnrc/netif/internal.h"
|
|
|
|
#include "net/gnrc/netif/pktq.h"
|
|
|
|
|
2021-08-16 11:02:05 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
|
|
|
#include "debug.h"
|
|
|
|
|
2022-05-31 23:37:29 +02:00
|
|
|
static mutex_t _pool_lock = MUTEX_INIT;
|
2019-03-25 11:15:15 +01:00
|
|
|
static gnrc_pktqueue_t _pool[CONFIG_GNRC_NETIF_PKTQ_POOL_SIZE];
|
|
|
|
|
2022-05-31 23:37:29 +02:00
|
|
|
static gnrc_pktqueue_t *_get_free_entry(gnrc_pktsnip_t *pkt)
|
2019-03-25 11:15:15 +01:00
|
|
|
{
|
2022-05-31 23:37:29 +02:00
|
|
|
gnrc_pktqueue_t *entry = NULL;
|
|
|
|
|
|
|
|
mutex_lock(&_pool_lock);
|
2019-03-25 11:15:15 +01:00
|
|
|
for (unsigned i = 0; i < CONFIG_GNRC_NETIF_PKTQ_POOL_SIZE; i++) {
|
|
|
|
if (_pool[i].pkt == NULL) {
|
2022-05-31 23:37:29 +02:00
|
|
|
_pool[i].pkt = pkt;
|
|
|
|
entry = &_pool[i];
|
|
|
|
break;
|
2019-03-25 11:15:15 +01:00
|
|
|
}
|
|
|
|
}
|
2022-05-31 23:37:29 +02:00
|
|
|
mutex_unlock(&_pool_lock);
|
|
|
|
|
|
|
|
return entry;
|
2019-03-25 11:15:15 +01:00
|
|
|
}
|
|
|
|
|
2021-03-15 15:13:27 +01:00
|
|
|
unsigned gnrc_netif_pktq_usage(void)
|
|
|
|
{
|
|
|
|
unsigned res = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < CONFIG_GNRC_NETIF_PKTQ_POOL_SIZE; i++) {
|
|
|
|
if (_pool[i].pkt != NULL) {
|
|
|
|
res++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-03-25 11:15:15 +01:00
|
|
|
int gnrc_netif_pktq_put(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
|
|
|
|
{
|
|
|
|
assert(netif != NULL);
|
|
|
|
assert(pkt != NULL);
|
|
|
|
|
2022-05-31 23:37:29 +02:00
|
|
|
gnrc_pktqueue_t *entry = _get_free_entry(pkt);
|
2019-03-25 11:15:15 +01:00
|
|
|
|
|
|
|
if (entry == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
gnrc_pktqueue_add(&netif->send_queue.queue, entry);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gnrc_netif_pktq_sched_get(gnrc_netif_t *netif)
|
|
|
|
{
|
2021-08-16 11:02:05 +02:00
|
|
|
#if CONFIG_GNRC_NETIF_PKTQ_TIMER_US > 0
|
2019-03-25 11:15:15 +01:00
|
|
|
assert(netif != NULL);
|
|
|
|
netif->send_queue.dequeue_msg.type = GNRC_NETIF_PKTQ_DEQUEUE_MSG;
|
|
|
|
/* Prevent timer from firing while we add this.
|
|
|
|
* Otherwise the system might crash: The timer handler sets
|
|
|
|
* netif->send_queue.dequeue_msg.sender_pid to KERNEL_PID_ISR while
|
|
|
|
* the message is added to the timer, causing the next round of the timer
|
|
|
|
* handler to try to send the message to IPC, leaving the system in an
|
|
|
|
* invalid state. */
|
|
|
|
unsigned state = irq_disable();
|
|
|
|
xtimer_set_msg(&netif->send_queue.dequeue_timer,
|
|
|
|
CONFIG_GNRC_NETIF_PKTQ_TIMER_US,
|
|
|
|
&netif->send_queue.dequeue_msg, netif->pid);
|
|
|
|
irq_restore(state);
|
2021-08-16 11:02:05 +02:00
|
|
|
#elif CONFIG_GNRC_NETIF_PKTQ_TIMER_US == 0
|
|
|
|
assert(netif != NULL);
|
|
|
|
netif->send_queue.dequeue_msg.type = GNRC_NETIF_PKTQ_DEQUEUE_MSG;
|
|
|
|
if (msg_send(&netif->send_queue.dequeue_msg, netif->pid) < 0) {
|
|
|
|
DEBUG("gnrc_netif_pktq: couldn't schedule packet (msg queue is full)\n");
|
|
|
|
}
|
|
|
|
#else
|
2019-03-25 11:15:15 +01:00
|
|
|
(void)netif;
|
|
|
|
#endif /* CONFIG_GNRC_NETIF_PKTQ_TIMER_US >= 0 */
|
|
|
|
}
|
|
|
|
|
|
|
|
int gnrc_netif_pktq_push_back(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
|
|
|
|
{
|
|
|
|
assert(netif != NULL);
|
|
|
|
assert(pkt != NULL);
|
|
|
|
|
2022-05-31 23:37:29 +02:00
|
|
|
gnrc_pktqueue_t *entry = _get_free_entry(pkt);
|
2019-03-25 11:15:15 +01:00
|
|
|
|
|
|
|
if (entry == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
LL_PREPEND(netif->send_queue.queue, entry);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|