Merge pull request #3234 from authmillenon/ng_pktqueue/api/take-out-prio

ng_pktqueue: remove priority queue dependency
dev/timer
Martine Lenders 8 years ago
commit 0103fa2e91

@ -118,7 +118,7 @@ extern "C" {
* </a>.
*/
typedef struct {
ng_pktqueue_t pkts; /**< Packets waiting for address resolution */
ng_pktqueue_t *pkts; /**< Packets waiting for address resolution */
ng_ipv6_addr_t ipv6_addr; /**< IPv6 address of the neighbor */
uint8_t l2_addr[NG_IPV6_NC_L2_ADDR_MAX];/**< Link layer address of the neighbor */
uint8_t l2_addr_len; /**< Length of ng_ipv6_nc_t::l2_addr */

@ -25,7 +25,7 @@
#include <stdlib.h>
#include "net/ng_pkt.h"
#include "priority_queue.h"
#include "utlist.h"
#ifdef __cplusplus
extern "C" {
@ -36,102 +36,53 @@ extern "C" {
*
* @extends priority_queue_node_t
*/
typedef struct ng_pktqueue_node {
struct ng_pktqueue_node *next; /**< next node in queue */
uint32_t priority; /**< priority of the node */
ng_pktsnip_t *data; /**< pointer to the data */
} ng_pktqueue_node_t;
/**
* @brief data type for packet queues
*
* @extends priority_queue_t
*/
typedef struct {
ng_pktqueue_node_t *first; /**< first node in the queue */
typedef struct ng_pktqueue {
struct ng_pktqueue *next; /**< next node in queue */
ng_pktsnip_t *pkt; /**< pointer to the packet */
} ng_pktqueue_t;
/**
* @brief Static initializer for ng_pktqueue_node_t
*/
#define NG_PKTQUEUE_NODE_INIT { NULL, NULL, 0 }
/**
* @brief Initializes a packet queue node.
* @details For initialization of variables use NG_PKTQUEUE_NODE_INIT instead.
* Only use this function for dynamically allocated packet queue nodes.
* @brief add @p node into @p queue based on its priority
*
* @param[out] node pre-allocated ng_pktqueue_node_t object. must not be NULL.
*/
static inline void ng_pktqueue_node_init(ng_pktqueue_node_t *node)
{
priority_queue_node_init((priority_queue_node_t *)node);
}
/**
* @brief Static initializer for ng_pktqueue_t.
*/
#define NG_PKTQUEUE_INIT PRIORITY_QUEUE_INIT
/**
* @brief Initialize a packet queue object.
* @details For initialization of variables use NG_PKTQUEUE_INIT
* instead. Only use this function for dynamically allocated
* packet queues.
* @param[out] queue pre-allocated ng_pktqueue_t object, must not be NULL.
* @details The new node will be appended after objects with the same
* priority.
*
* @param[in,out] queue the queue, may not be NULL
* @param[in] node the node to add.
*/
static inline void ng_pktqueue_init(ng_pktqueue_t *queue)
static inline void ng_pktqueue_add(ng_pktqueue_t **queue, ng_pktqueue_t *node)
{
priority_queue_init((priority_queue_t *)queue);
LL_APPEND(*queue, node);
}
/**
* @brief get the packet queue's head without removing it
* @brief remove @p node from @p queue
*
* @param[out] queue the queue
* @param[in] queue the queue, may not be NULL
* @param[in] node the node to remove
*
* @return the head
* @return @p node.
*/
static inline ng_pktqueue_node_t *ng_pktqueue_get_head(ng_pktqueue_t *queue)
static inline ng_pktqueue_t *ng_pktqueue_remove(ng_pktqueue_t **queue, ng_pktqueue_t *node)
{
return queue->first;
if (node) {
LL_DELETE(*queue, node);
node->next = NULL;
}
return node;
}
/**
* @brief remove the packet queue's head
*
* @param[in] queue the queue
* @param[in] queue the queue, may not be NULL
*
* @return the old head
*/
static inline ng_pktqueue_node_t *ng_pktqueue_remove_head(ng_pktqueue_t *queue)
{
return (ng_pktqueue_node_t *)priority_queue_remove_head((priority_queue_t *)queue);
}
/**
* @brief add @p node into @p queue based on its priority
*
* @details The new node will be appended after objects with the same
* priority.
*
* @param[in] queue the queue
* @param[in] node the node to add
*/
static inline void ng_pktqueue_add(ng_pktqueue_t *queue, ng_pktqueue_node_t *node)
{
priority_queue_add((priority_queue_t *)queue, (priority_queue_node_t *) node);
}
/**
* @brief remove @p node from @p queue
*
* @param[in] queue the queue
* @param[in] node the node to remove
*/
static inline void ng_pktqueue_remove(ng_pktqueue_t *queue, ng_pktqueue_node_t *node)
static inline ng_pktqueue_t *ng_pktqueue_remove_head(ng_pktqueue_t **queue)
{
priority_queue_remove((priority_queue_t *)queue, (priority_queue_node_t *) node);
return ng_pktqueue_remove(queue, *queue);
}
#ifdef __cplusplus

@ -20,6 +20,7 @@
#include "net/ng_ipv6/nc.h"
#include "net/ng_ipv6/netif.h"
#include "net/ng_ndp.h"
#include "net/ng_pktbuf.h"
#include "thread.h"
#include "timex.h"
#include "vtimer.h"
@ -98,7 +99,7 @@ ng_ipv6_nc_t *ng_ipv6_nc_add(kernel_pid_t iface, const ng_ipv6_addr_t *ipv6_addr
/* Otherwise, fill free entry with your fresh information */
free_entry->iface = iface;
ng_pktqueue_init(&(free_entry->pkts));
free_entry->pkts = NULL;
memcpy(&(free_entry->ipv6_addr), ipv6_addr, sizeof(ng_ipv6_addr_t));
DEBUG("ipv6_nc: Register %s for interface %" PRIkernel_pid,
ng_ipv6_addr_to_str(addr_str, ipv6_addr, sizeof(addr_str)),
@ -133,6 +134,14 @@ void ng_ipv6_nc_remove(kernel_pid_t iface, const ng_ipv6_addr_t *ipv6_addr)
ng_ipv6_addr_to_str(addr_str, ipv6_addr, sizeof(addr_str)),
iface);
while (entry->pkts != NULL) {
#ifdef MODULE_NG_PKTBUF
ng_pktbuf_release(entry->pkts->pkt);
#endif
entry->pkts->pkt = NULL;
ng_pktqueue_remove_head(&entry->pkts);
}
ng_ipv6_addr_set_unspecified(&(entry->ipv6_addr));
entry->iface = KERNEL_PID_UNDEF;
entry->flags = 0;

@ -38,7 +38,7 @@
static char addr_str[NG_IPV6_ADDR_MAX_STR_LEN];
#endif
static ng_pktqueue_node_t _pkt_nodes[NG_IPV6_NC_SIZE * 2];
static ng_pktqueue_t _pkt_nodes[NG_IPV6_NC_SIZE * 2];
static ng_ipv6_nc_t *_last_router = NULL; /* last router chosen as default
* router. Only used if reachability
* is suspect (i. e. incomplete or
@ -73,7 +73,7 @@ static inline void _send_delayed(vtimer_t *t, timex_t interval, ng_pktsnip_t *pk
}
/* packet queue node allocation */
static ng_pktqueue_node_t *_alloc_pkt_node(ng_pktsnip_t *pkt);
static ng_pktqueue_t *_alloc_pkt_node(ng_pktsnip_t *pkt);
void ng_ndp_nbr_sol_handle(kernel_pid_t iface, ng_pktsnip_t *pkt,
ng_ipv6_hdr_t *ipv6, ng_ndp_nbr_sol_t *nbr_sol,
@ -197,7 +197,7 @@ void ng_ndp_nbr_adv_handle(kernel_pid_t iface, ng_pktsnip_t *pkt,
}
if (ng_ipv6_nc_get_state(nc_entry) == NG_IPV6_NC_STATE_INCOMPLETE) {
ng_pktqueue_node_t *queued_pkt;
ng_pktqueue_t *queued_pkt;
if (_pkt_has_l2addr(netif_hdr) && (l2tgt_len == 0)) {
/* link-layer has addresses, but no TLLAO supplied: discard silently
@ -225,8 +225,8 @@ void ng_ndp_nbr_adv_handle(kernel_pid_t iface, ng_pktsnip_t *pkt,
}
while ((queued_pkt = ng_pktqueue_remove_head(&nc_entry->pkts)) != NULL) {
ng_netapi_send(ng_ipv6_pid, queued_pkt->data);
queued_pkt->data = NULL;
ng_netapi_send(ng_ipv6_pid, queued_pkt->pkt);
queued_pkt->pkt = NULL;
}
}
else {
@ -326,24 +326,11 @@ void ng_ndp_retrans_nbr_sol(ng_ipv6_nc_t *nc_entry)
}
}
else if (nc_entry->probes_remaining <= 1) {
ng_pktqueue_node_t *queue_node;
/* No need to call ng_ipv6_nc_remove() we know already were the
* entry is */
DEBUG("ndp: Remove nc entry %s for interface %" PRIkernel_pid "\n",
ng_ipv6_addr_to_str(addr_str, &nc_entry->ipv6_addr, sizeof(addr_str)),
nc_entry->iface);
while ((queue_node = ng_pktqueue_remove_head(&nc_entry->pkts))) {
ng_pktbuf_release(queue_node->data);
queue_node->data = NULL;
}
ng_ipv6_addr_set_unspecified(&(nc_entry->ipv6_addr));
nc_entry->iface = KERNEL_PID_UNDEF;
nc_entry->flags = 0;
nc_entry->probes_remaining = 0;
ng_ipv6_nc_remove(nc_entry->iface, &nc_entry->ipv6_addr);
}
}
@ -474,7 +461,7 @@ kernel_pid_t ng_ndp_next_hop_l2addr(uint8_t *l2addr, uint8_t *l2addr_len,
return nc_entry->iface;
}
else if (nc_entry == NULL) {
ng_pktqueue_node_t *pkt_node;
ng_pktqueue_t *pkt_node;
ng_ipv6_addr_t dst_sol;
nc_entry = ng_ipv6_nc_add(iface, next_hop_ip, NULL, 0,
@ -492,7 +479,7 @@ kernel_pid_t ng_ndp_next_hop_l2addr(uint8_t *l2addr, uint8_t *l2addr_len,
}
else {
/* prevent packet from being released by IPv6 */
ng_pktbuf_hold(pkt_node->data, 1);
ng_pktbuf_hold(pkt_node->pkt, 1);
ng_pktqueue_add(&nc_entry->pkts, pkt_node);
}
@ -816,12 +803,11 @@ ng_pktsnip_t *ng_ndp_opt_tl2a_build(const uint8_t *l2addr, uint8_t l2addr_len,
/* internal functions */
/* packet queue node allocation */
static ng_pktqueue_node_t *_alloc_pkt_node(ng_pktsnip_t *pkt)
static ng_pktqueue_t *_alloc_pkt_node(ng_pktsnip_t *pkt)
{
for (size_t i = 0; i < sizeof(_pkt_nodes); i++) {
if (_pkt_nodes[i].data == NULL) {
ng_pktqueue_node_init(_pkt_nodes + i);
_pkt_nodes[i].data = pkt;
for (size_t i = 0; i < sizeof(_pkt_nodes) / sizeof(ng_pktqueue_t); i++) {
if ((_pkt_nodes[i].pkt == NULL) && (_pkt_nodes[i].next == NULL)) {
_pkt_nodes[i].pkt = pkt;
return &(_pkt_nodes[i]);
}

@ -18,142 +18,149 @@
#include "net/ng_pkt.h"
#include "net/ng_pktqueue.h"
#include "unittests-constants.h"
#include "tests-pktqueue.h"
#define Q_LEN (4)
#define PKT_INIT_ELEM(len, data, next) \
{ 1, (next), (data), (len), NG_NETTYPE_UNDEF }
#define PKT_INIT_ELEM_STATIC_DATA(data, next) PKT_INIT_ELEM(sizeof(data), data, next)
#define PKTQUEUE_INIT_ELEM(pkt) { NULL, pkt }
static ng_pktqueue_t q = NG_PKTQUEUE_INIT;
static ng_pktqueue_node_t qe[Q_LEN];
static ng_pktqueue_t *root;
static void set_up(void)
{
ng_pktqueue_init(&q);
for (unsigned i = 0; i < Q_LEN; ++i) {
ng_pktqueue_node_init(&(qe[i]));
}
root = NULL;
}
static void test_pktqueue_remove_head_empty(void)
{
ng_pktqueue_t *root = &q;
ng_pktqueue_node_t *res;
res = ng_pktqueue_remove_head(root);
TEST_ASSERT_NULL(res);
}
static void test_pktqueue_remove_head_one(void)
static void test_pktqueue_add_one(void)
{
ng_pktqueue_t *root = &q;
ng_pktqueue_node_t *elem = &(qe[1]), *res;
ng_pktsnip_t pkt = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING8, NULL);
ng_pktqueue_t elem = PKTQUEUE_INIT_ELEM(&pkt);
elem->data = (ng_pktsnip_t *)62801;
ng_pktqueue_add(&root, &elem);
ng_pktqueue_add(root, elem);
res = ng_pktqueue_remove_head(root);
TEST_ASSERT(res == elem);
TEST_ASSERT(((ng_pktsnip_t *)62801) == res->data);
res = ng_pktqueue_remove_head(root);
TEST_ASSERT_NULL(res);
TEST_ASSERT(root == &elem);
TEST_ASSERT_EQUAL_INT(1, root->pkt->users);
TEST_ASSERT_NULL(root->pkt->next);
TEST_ASSERT_EQUAL_STRING(TEST_STRING8, root->pkt->data);
TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING8), root->pkt->size);
TEST_ASSERT_EQUAL_INT(NG_NETTYPE_UNDEF, root->pkt->type);
}
static void test_pktqueue_add_one(void)
static void test_pktqueue_add_two(void)
{
ng_pktqueue_t *root = &q;
ng_pktqueue_node_t *elem = &(qe[1]);
elem->data = (ng_pktsnip_t *)7317;
elem->priority = 713643658;
ng_pktqueue_add(root, elem);
TEST_ASSERT(root->first == elem);
TEST_ASSERT(((ng_pktsnip_t *)7317) == root->first->data);
TEST_ASSERT_EQUAL_INT(713643658, root->first->priority);
TEST_ASSERT_NULL(root->first->next);
ng_pktsnip_t pkt1 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING8, NULL);
ng_pktsnip_t pkt2 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING16, NULL);
ng_pktqueue_t elem1 = PKTQUEUE_INIT_ELEM(&pkt1);
ng_pktqueue_t elem2 = PKTQUEUE_INIT_ELEM(&pkt2);
ng_pktqueue_add(&root, &elem1);
ng_pktqueue_add(&root, &elem2);
TEST_ASSERT(root == &elem1);
TEST_ASSERT(root->next == &elem2);
TEST_ASSERT_EQUAL_INT(1, root->pkt->users);
TEST_ASSERT_NULL(root->pkt->next);
TEST_ASSERT_EQUAL_STRING(TEST_STRING8, root->pkt->data);
TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING8), root->pkt->size);
TEST_ASSERT_EQUAL_INT(NG_NETTYPE_UNDEF, root->pkt->type);
TEST_ASSERT_EQUAL_INT(1, root->next->pkt->users);
TEST_ASSERT_NULL(root->next->pkt->next);
TEST_ASSERT_EQUAL_STRING(TEST_STRING16, root->next->pkt->data);
TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING16), root->next->pkt->size);
TEST_ASSERT_EQUAL_INT(NG_NETTYPE_UNDEF, root->next->pkt->type);
}
static void test_pktqueue_add_two_equal(void)
static void test_pktqueue_remove(void)
{
ng_pktqueue_t *root = &q;
ng_pktqueue_node_t *elem1 = &(qe[1]), *elem2 = &(qe[2]);
elem1->data = (ng_pktsnip_t *)27088;
elem1->priority = 14202;
elem2->data = (ng_pktsnip_t *)4356;
elem2->priority = 14202;
ng_pktqueue_add(root, elem1);
ng_pktqueue_add(root, elem2);
TEST_ASSERT(root->first == elem1);
TEST_ASSERT(((ng_pktsnip_t *)27088) == root->first->data);
TEST_ASSERT_EQUAL_INT(14202, root->first->priority);
TEST_ASSERT(root->first->next == elem2);
TEST_ASSERT(((ng_pktsnip_t *)4356) == root->first->next->data);
TEST_ASSERT_EQUAL_INT(14202, root->first->next->priority);
TEST_ASSERT_NULL(root->first->next->next);
ng_pktsnip_t pkt1 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING8, NULL);
ng_pktsnip_t pkt2 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING16, NULL);
ng_pktqueue_t *res;
ng_pktqueue_t elem1 = PKTQUEUE_INIT_ELEM(&pkt1);
ng_pktqueue_t elem2 = PKTQUEUE_INIT_ELEM(&pkt2);
ng_pktqueue_add(&root, &elem1);
ng_pktqueue_add(&root, &elem2);
res = ng_pktqueue_remove(&root, &elem2);
TEST_ASSERT(res == &elem2);
TEST_ASSERT(root == &elem1);
TEST_ASSERT_EQUAL_INT(1, res->pkt->users);
TEST_ASSERT_NULL(res->pkt->next);
TEST_ASSERT_EQUAL_STRING(TEST_STRING16, res->pkt->data);
TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING16), res->pkt->size);
TEST_ASSERT_EQUAL_INT(NG_NETTYPE_UNDEF, res->pkt->type);
res = ng_pktqueue_remove(&root, &elem1);
TEST_ASSERT_NULL(root);
TEST_ASSERT(res == &elem1);
TEST_ASSERT_EQUAL_INT(NG_NETTYPE_UNDEF, res->pkt->type);
TEST_ASSERT_EQUAL_INT(1, res->pkt->users);
TEST_ASSERT_NULL(res->pkt->next);
TEST_ASSERT_EQUAL_STRING(TEST_STRING8, res->pkt->data);
TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING8), res->pkt->size);
TEST_ASSERT_EQUAL_INT(NG_NETTYPE_UNDEF, res->pkt->type);
res = ng_pktqueue_remove(&root, NULL);
TEST_ASSERT_NULL(root);
TEST_ASSERT_NULL(res);
}
static void test_pktqueue_add_two_distinct(void)
static void test_pktqueue_remove_head_empty(void)
{
ng_pktqueue_t *root = &q;
ng_pktqueue_node_t *elem1 = &(qe[1]), *elem2 = &(qe[2]);
ng_pktqueue_t *res;
elem1->data = (ng_pktsnip_t *)46421;
elem1->priority = 4567;
res = ng_pktqueue_remove_head(&root);
elem2->data = (ng_pktsnip_t *)43088;
elem2->priority = 1234;
ng_pktqueue_add(root, elem1);
ng_pktqueue_add(root, elem2);
TEST_ASSERT(root->first == elem2);
TEST_ASSERT(((ng_pktsnip_t *)43088) == root->first->data);
TEST_ASSERT_EQUAL_INT(1234, root->first->priority);
TEST_ASSERT(root->first->next == elem1);
TEST_ASSERT(((ng_pktsnip_t *)46421) == root->first->next->data);
TEST_ASSERT_EQUAL_INT(4567, root->first->next->priority);
TEST_ASSERT_NULL(root->first->next->next);
TEST_ASSERT_NULL(root);
TEST_ASSERT_NULL(res);
}
static void test_pktqueue_remove_one(void)
static void test_pktqueue_remove_head(void)
{
ng_pktqueue_t *root = &q;
ng_pktqueue_node_t *elem1 = &(qe[1]), *elem2 = &(qe[2]), *elem3 = &(qe[3]);
ng_pktqueue_add(root, elem1);
ng_pktqueue_add(root, elem2);
ng_pktqueue_add(root, elem3);
ng_pktqueue_remove(root, elem2);
TEST_ASSERT(root->first == elem1);
TEST_ASSERT(root->first->next == elem3);
TEST_ASSERT_NULL(root->first->next->next);
ng_pktsnip_t pkt1 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING8, NULL);
ng_pktsnip_t pkt2 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING16, NULL);
ng_pktqueue_t *res;
ng_pktqueue_t elem1 = PKTQUEUE_INIT_ELEM(&pkt1);
ng_pktqueue_t elem2 = PKTQUEUE_INIT_ELEM(&pkt2);
ng_pktqueue_add(&root, &elem1);
ng_pktqueue_add(&root, &elem2);
res = ng_pktqueue_remove_head(&root);
TEST_ASSERT(res == &elem1);
TEST_ASSERT(root == &elem2);
TEST_ASSERT_EQUAL_INT(1, res->pkt->users);
TEST_ASSERT_NULL(res->pkt->next);
TEST_ASSERT_EQUAL_STRING(TEST_STRING8, res->pkt->data);
TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING8), res->pkt->size);
TEST_ASSERT_EQUAL_INT(NG_NETTYPE_UNDEF, res->pkt->type);
res = ng_pktqueue_remove_head(&root);
TEST_ASSERT_NULL(root);
TEST_ASSERT(res == &elem2);
TEST_ASSERT_EQUAL_INT(NG_NETTYPE_UNDEF, res->pkt->type);
TEST_ASSERT_EQUAL_INT(1, res->pkt->users);
TEST_ASSERT_NULL(res->pkt->next);
TEST_ASSERT_EQUAL_STRING(TEST_STRING16, res->pkt->data);
TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING16), res->pkt->size);
TEST_ASSERT_EQUAL_INT(NG_NETTYPE_UNDEF, res->pkt->type);
}
Test *tests_pktqueue_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_pktqueue_remove_head_empty),
new_TestFixture(test_pktqueue_remove_head_one),
new_TestFixture(test_pktqueue_add_one),
new_TestFixture(test_pktqueue_add_two_equal),
new_TestFixture(test_pktqueue_add_two_distinct),
new_TestFixture(test_pktqueue_remove_one),
new_TestFixture(test_pktqueue_add_two),
new_TestFixture(test_pktqueue_remove),
new_TestFixture(test_pktqueue_remove_head_empty),
new_TestFixture(test_pktqueue_remove_head),
};
EMB_UNIT_TESTCALLER(pktqueue_tests, set_up, NULL, fixtures);

Loading…
Cancel
Save