diff --git a/sys/Makefile.include b/sys/Makefile.include index 875f9c6bcd..058ed847e2 100644 --- a/sys/Makefile.include +++ b/sys/Makefile.include @@ -21,6 +21,10 @@ ifneq (,$(filter gnrc_pktbuf,$(USEMODULE))) include $(RIOTBASE)/sys/net/gnrc/pktbuf/Makefile.include endif +ifneq (,$(filter gnrc_pktbuf_static,$(USEMODULE))) + include $(RIOTBASE)/sys/net/gnrc/pktbuf_static/Makefile.include +endif + ifneq (,$(filter malloc_thread_safe,$(USEMODULE))) include $(RIOTBASE)/sys/malloc_thread_safe/Makefile.include endif diff --git a/sys/net/gnrc/pktbuf_static/Makefile.include b/sys/net/gnrc/pktbuf_static/Makefile.include new file mode 100644 index 0000000000..6509bb8e78 --- /dev/null +++ b/sys/net/gnrc/pktbuf_static/Makefile.include @@ -0,0 +1,2 @@ +USEMODULE_INCLUDES_gnrc_pktbuf_static := $(LAST_MAKEFILEDIR)/include +USEMODULE_INCLUDES += $(USEMODULE_INCLUDES_gnrc_pktbuf_static) diff --git a/sys/net/gnrc/pktbuf_static/gnrc_pktbuf_static.c b/sys/net/gnrc/pktbuf_static/gnrc_pktbuf_static.c index 3124b6d557..30fc9f2050 100644 --- a/sys/net/gnrc/pktbuf_static/gnrc_pktbuf_static.c +++ b/sys/net/gnrc/pktbuf_static/gnrc_pktbuf_static.c @@ -31,17 +31,11 @@ #include "net/gnrc/pkt.h" #include "pktbuf_internal.h" +#include "pktbuf_static.h" #define ENABLE_DEBUG 0 #include "debug.h" -#define _ALIGNMENT_MASK (sizeof(_unused_t) - 1) - -typedef struct _unused { - struct _unused *next; - unsigned int size; -} _unused_t; - /* The static buffer needs to be aligned to word size, so that its start * address can be casted to `_unused_t *` safely. Just allocating an array of * (word sized) uintptr_t is a trivial way to do this */ @@ -59,12 +53,6 @@ static gnrc_pktsnip_t *_create_snip(gnrc_pktsnip_t *next, const void *data, size gnrc_nettype_t type); static void *_pktbuf_alloc(size_t size); -/* fits size to byte alignment */ -static inline size_t _align(size_t size) -{ - return (size + _ALIGNMENT_MASK) & ~(_ALIGNMENT_MASK); -} - static inline void _set_pktsnip(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *next, void *data, size_t size, gnrc_nettype_t type) { diff --git a/sys/net/gnrc/pktbuf_static/include/pktbuf_static.h b/sys/net/gnrc/pktbuf_static/include/pktbuf_static.h new file mode 100644 index 0000000000..dce6f8736e --- /dev/null +++ b/sys/net/gnrc/pktbuf_static/include/pktbuf_static.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2014-2015 Martine S. Lenders + * Copyright (C) 2014-2021 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. + */ + +/** + * @ingroup net_gnrc_pktbuf + * @brief Internal definitions of the static implementation of + * @ref net_gnrc_pktbuf + * @{ + * + * @file + * @brief Definitions of types and their alignment for usage in tests + * + * @author Martine Lenders + */ +#ifndef PKTBUF_STATIC_H +#define PKTBUF_STATIC_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Mask to align packet buffer allocations with size of @ref _unused_t + */ +#define GNRC_PKTBUF_STATIC_ALIGN_MASK (sizeof(_unused_t) - 1) + +/** + * @brief Marks an unused section of the packet buffer arena array + */ +typedef struct _unused { + struct _unused *next; /**< the next unused section */ + unsigned int size; /**< the size of the unused section */ +} _unused_t; + +/** + * @brief Calculates the required space of a number of bytes including + * alignment to the size of @ref _unused_t + */ +static inline size_t _align(size_t size) +{ + return (size + GNRC_PKTBUF_STATIC_ALIGN_MASK) & + ~(GNRC_PKTBUF_STATIC_ALIGN_MASK); +} + +#ifdef __cplusplus +} +#endif + +#endif /* PKTBUF_STATIC_H */ +/** @} */ diff --git a/tests/gnrc_ndp/Makefile b/tests/gnrc_ndp/Makefile index 566a815e6e..835da6d9ca 100644 --- a/tests/gnrc_ndp/Makefile +++ b/tests/gnrc_ndp/Makefile @@ -11,6 +11,8 @@ USEMODULE += netdev_test CFLAGS += -DGNRC_NETTYPE_NDP=GNRC_NETTYPE_TEST CFLAGS += -DTEST_SUITES +INCLUDES += -I$(RIOTBASE)/sys/net/gnrc/pktbuf_static/include + include $(RIOTBASE)/Makefile.include # Set GNRC_PKTBUF_SIZE via CFLAGS if not being set via Kconfig. diff --git a/tests/gnrc_ndp/main.c b/tests/gnrc_ndp/main.c index b303de8db6..63ef496327 100644 --- a/tests/gnrc_ndp/main.c +++ b/tests/gnrc_ndp/main.c @@ -38,6 +38,8 @@ #include "sched.h" #include "test_utils/expect.h" +#include "pktbuf_static.h" + #include "net/gnrc/ndp.h" #define TEST_CUR_HL (194U) @@ -81,9 +83,8 @@ static void set_up(void) static void fill_pktbuf(void) { gnrc_pktsnip_t *pkt = gnrc_pktbuf_add(NULL, NULL, - /* 24 = sizeof(gnrc_pktsnip_t) + - * potential alignment */ - CONFIG_GNRC_PKTBUF_SIZE - 24U, + CONFIG_GNRC_PKTBUF_SIZE - + _align(sizeof(gnrc_pktsnip_t)), GNRC_NETTYPE_UNDEF); TEST_ASSERT_NOT_NULL(pkt); TEST_ASSERT(gnrc_pktbuf_is_sane()); @@ -496,9 +497,10 @@ static void test_nbr_sol_send__src_NOT_NULL(void) static void test_nbr_sol_send__pktbuf_full1(void) { /* don't be able to fit any more data into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding */ + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - 24, + CONFIG_GNRC_PKTBUF_SIZE - + _align(sizeof(gnrc_pktsnip_t)), GNRC_NETTYPE_UNDEF); TEST_ASSERT_NOT_NULL(tmp); gnrc_ndp_nbr_sol_send(&test_tgt, test_netif, &test_src, &test_dst, NULL); @@ -510,10 +512,11 @@ static void test_nbr_sol_send__pktbuf_full1(void) static void test_nbr_sol_send__pktbuf_full2(void) { /* just be able to fit the SLLAO into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding * - 16 == size of SLLAO for IEEE 802.15.4 */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - (2 * 24) - 16, + CONFIG_GNRC_PKTBUF_SIZE - + (2 * _align(sizeof(gnrc_pktsnip_t))) - 16, GNRC_NETTYPE_UNDEF); TEST_ASSERT_NOT_NULL(tmp); gnrc_ndp_nbr_sol_send(&test_tgt, test_netif, &test_src, &test_dst, NULL); @@ -525,10 +528,11 @@ static void test_nbr_sol_send__pktbuf_full2(void) static void test_nbr_sol_send__pktbuf_full3(void) { /* just be able to fit the SLLAO and NS into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding * - 16 == size of SLLAO for IEEE 802.15.4 */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - (3 * 24) - 16 - + CONFIG_GNRC_PKTBUF_SIZE - + (3 * _align(sizeof(gnrc_pktsnip_t))) - 16 - sizeof(ndp_nbr_sol_t), GNRC_NETTYPE_UNDEF); TEST_ASSERT_NOT_NULL(tmp); @@ -541,10 +545,11 @@ static void test_nbr_sol_send__pktbuf_full3(void) static void test_nbr_sol_send__pktbuf_full4(void) { /* just be able to fit the SLLAO, NS, and IPv6 header into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding * - 16 == size of SLLAO for IEEE 802.15.4 */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - (4 * 24) - 16 - + CONFIG_GNRC_PKTBUF_SIZE - + (4 * _align(sizeof(gnrc_pktsnip_t))) - 16 - sizeof(ndp_nbr_sol_t) - sizeof(ipv6_hdr_t), GNRC_NETTYPE_UNDEF); @@ -707,9 +712,10 @@ static void test_nbr_adv_send__src_tgt_specified_dst_supply_tl2a_ext_opts(void) static void test_nbr_adv_send__pktbuf_full1(void) { /* don't be able to fit any more data into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding */ + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - 24, + CONFIG_GNRC_PKTBUF_SIZE - + _align(sizeof(gnrc_pktsnip_t)), GNRC_NETTYPE_UNDEF); TEST_ASSERT_NOT_NULL(tmp); gnrc_ndp_nbr_adv_send(&test_src, test_netif, &test_dst, true, NULL); @@ -721,10 +727,11 @@ static void test_nbr_adv_send__pktbuf_full1(void) static void test_nbr_adv_send__pktbuf_full2(void) { /* just be able to fit the TLLAO into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding * - 16 == size of TLLAO for IEEE 802.15.4 */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - (2 * 24) - 16, + CONFIG_GNRC_PKTBUF_SIZE - + (2 * _align(sizeof(gnrc_pktsnip_t))) - 16, GNRC_NETTYPE_UNDEF); TEST_ASSERT_NOT_NULL(tmp); gnrc_ndp_nbr_adv_send(&test_src, test_netif, &test_dst, true, NULL); @@ -736,10 +743,11 @@ static void test_nbr_adv_send__pktbuf_full2(void) static void test_nbr_adv_send__pktbuf_full3(void) { /* just be able to fit the TLLAO and NA into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding * - 16 == size of TLLAO for IEEE 802.15.4 */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - (3 * 24) - 16 - + CONFIG_GNRC_PKTBUF_SIZE - + (3 * _align(sizeof(gnrc_pktsnip_t))) - 16 - sizeof(ndp_nbr_adv_t), GNRC_NETTYPE_UNDEF); TEST_ASSERT_NOT_NULL(tmp); @@ -752,10 +760,11 @@ static void test_nbr_adv_send__pktbuf_full3(void) static void test_nbr_adv_send__pktbuf_full4(void) { /* just be able to fit the TLLAO, NA, and IPv6 header into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding * - 16 == size of TLLAO for IEEE 802.15.4 */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - (4 * 24) - 16 - + CONFIG_GNRC_PKTBUF_SIZE - + (4 * _align(sizeof(gnrc_pktsnip_t))) - 16 - sizeof(ndp_nbr_adv_t) - sizeof(ipv6_hdr_t), GNRC_NETTYPE_UNDEF); @@ -830,9 +839,10 @@ static void test_rtr_sol_send__dst_global(void) static void test_rtr_sol_send__pktbuf_full1(void) { /* don't be able to fit any more data into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding */ + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - 24, + CONFIG_GNRC_PKTBUF_SIZE - + _align(sizeof(gnrc_pktsnip_t)), GNRC_NETTYPE_UNDEF); TEST_ASSERT_NOT_NULL(tmp); gnrc_ndp_rtr_sol_send(test_netif, &test_dst); @@ -844,10 +854,11 @@ static void test_rtr_sol_send__pktbuf_full1(void) static void test_rtr_sol_send__pktbuf_full2(void) { /* just be able to fit the SLLAO into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding * - 16 == size of SLLAO for IEEE 802.15.4 */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - (2 * 24) - 16, + CONFIG_GNRC_PKTBUF_SIZE - + (2 * _align(sizeof(gnrc_pktsnip_t))) - 16, GNRC_NETTYPE_UNDEF); TEST_ASSERT_NOT_NULL(tmp); gnrc_ndp_rtr_sol_send(test_netif, &test_dst); @@ -859,10 +870,11 @@ static void test_rtr_sol_send__pktbuf_full2(void) static void test_rtr_sol_send__pktbuf_full3(void) { /* just be able to fit the SLLAO and RS into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding * - 16 == size of SLLAO for IEEE 802.15.4 */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - (3 * 24) - 16 - + CONFIG_GNRC_PKTBUF_SIZE - + (3 * _align(sizeof(gnrc_pktsnip_t))) - 16 - sizeof(ndp_rtr_sol_t), GNRC_NETTYPE_UNDEF); TEST_ASSERT_NOT_NULL(tmp); @@ -875,10 +887,11 @@ static void test_rtr_sol_send__pktbuf_full3(void) static void test_rtr_sol_send__pktbuf_full4(void) { /* just be able to fit the SLLAO, RS, and IPv6 header into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding * - 16 == size of SLLAO for IEEE 802.15.4 */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - (4 * 24) - 16 - + CONFIG_GNRC_PKTBUF_SIZE - + (4 * _align(sizeof(gnrc_pktsnip_t))) - 16 - sizeof(ndp_rtr_sol_t) - sizeof(ipv6_hdr_t), GNRC_NETTYPE_UNDEF); @@ -1029,9 +1042,10 @@ static void test_rtr_adv_send__src_dst_fin_ext_opts(void) static void test_rtr_adv_send__pktbuf_full1(void) { /* don't be able to fit any more data into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding */ + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - 24, + CONFIG_GNRC_PKTBUF_SIZE - + _align(sizeof(gnrc_pktsnip_t)), GNRC_NETTYPE_UNDEF); TEST_ASSERT_NOT_NULL(tmp); gnrc_ndp_rtr_adv_send(test_netif, &test_src, &test_dst, false, NULL); @@ -1043,10 +1057,11 @@ static void test_rtr_adv_send__pktbuf_full1(void) static void test_rtr_adv_send__pktbuf_full2(void) { /* just be able to fit the SLLAO into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding * - 16 == size of SLLAO for IEEE 802.15.4 */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - (2 * 24) - 16, + CONFIG_GNRC_PKTBUF_SIZE - + (2 * _align(sizeof(gnrc_pktsnip_t))) - 16, GNRC_NETTYPE_UNDEF); TEST_ASSERT_NOT_NULL(tmp); gnrc_ndp_rtr_adv_send(test_netif, &test_src, &test_dst, false, NULL); @@ -1058,10 +1073,11 @@ static void test_rtr_adv_send__pktbuf_full2(void) static void test_rtr_adv_send__pktbuf_full3(void) { /* just be able to fit the SLLAO and RA into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding * - 16 == size of SLLAO for IEEE 802.15.4 */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - (3 * 24) - 16 - + CONFIG_GNRC_PKTBUF_SIZE - + (3 * _align(sizeof(gnrc_pktsnip_t))) - 16 - sizeof(ndp_rtr_adv_t), GNRC_NETTYPE_UNDEF); TEST_ASSERT_NOT_NULL(tmp); @@ -1074,10 +1090,11 @@ static void test_rtr_adv_send__pktbuf_full3(void) static void test_rtr_adv_send__pktbuf_full4(void) { /* just be able to fit the SLLAO, RA, and IPv6 header into packet buffer - * - 24 == sizeof(gnrc_pktsnip_t) + pktbuf internal padding + * - sizeof(gnrc_pktsnip_t) + pktbuf internal padding * - 16 == size of SLLAO for IEEE 802.15.4 */ gnrc_pktsnip_t *tmp = gnrc_pktbuf_add(NULL, NULL, - CONFIG_GNRC_PKTBUF_SIZE - (4 * 24) - 16 - + CONFIG_GNRC_PKTBUF_SIZE - + (4 * _align(sizeof(gnrc_pktsnip_t))) - 16 - sizeof(ndp_rtr_adv_t) - sizeof(ipv6_hdr_t), GNRC_NETTYPE_UNDEF);