Many drivers allocate small control-path messages, which consist of a header structure followed by a variable-length array, using malloc and free, which adds error handling and the risk of leaks on error paths.
Add RTE_DEFINE_FLEX and RTE_DEFINE_FLEX_BYTES macros to define such buffers on the stack instead, (similar to equivalent macros in Linux kernel). The buffer is a union of a byte array and the structure type, giving correct alignment and type-safe access via a pointer, with the byte array first so that zero-initialization covers the whole buffer. Signed-off-by: Bruce Richardson <[email protected]> --- app/test/test_common.c | 54 ++++++++++++++++++++++++++++++++++++ lib/eal/include/rte_common.h | 46 ++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/app/test/test_common.c b/app/test/test_common.c index 290bf763ee..148d31aee5 100644 --- a/app/test/test_common.c +++ b/app/test/test_common.c @@ -796,6 +796,59 @@ test_fls(void) return 0; } +struct flex_test_elem { + uint64_t a; + uint16_t b; +}; + +struct flex_test_hdr { + uint16_t num; + struct flex_test_elem elems[]; +}; + +struct flex_test_hdr_one { + uint8_t num; + struct { + uint8_t len; + struct flex_test_elem elems[1]; + } list; +}; + +static int +test_define_flex(void) +{ + RTE_DEFINE_FLEX(struct flex_test_hdr, hdr, elems, 4); + RTE_DEFINE_FLEX(struct flex_test_hdr_one, hdr_one, list.elems, 3); + RTE_DEFINE_FLEX_BYTES(struct flex_test_hdr, hdr_bytes, 100); + + RTE_TEST_ASSERT(sizeof(hdr_u) >= sizeof(*hdr) + 4 * sizeof(hdr->elems[0]), + "RTE_DEFINE_FLEX size, flexible array"); + RTE_TEST_ASSERT(sizeof(hdr_one_u) >= sizeof(*hdr_one) + 3 * sizeof(hdr_one->list.elems[0]), + "RTE_DEFINE_FLEX size, nested one-element array"); + RTE_TEST_ASSERT(sizeof(hdr_bytes_u) >= 100, "RTE_DEFINE_FLEX_BYTES size"); + + RTE_TEST_ASSERT((uintptr_t)hdr % alignof(struct flex_test_hdr) == 0, + "RTE_DEFINE_FLEX alignment"); + RTE_TEST_ASSERT((uintptr_t)hdr_one % alignof(struct flex_test_hdr_one) == 0, + "RTE_DEFINE_FLEX alignment, nested array"); + RTE_TEST_ASSERT((uintptr_t)hdr_bytes % alignof(struct flex_test_hdr) == 0, + "RTE_DEFINE_FLEX_BYTES alignment"); + + for (size_t i = 0; i < sizeof(hdr_u.raw); i++) + RTE_TEST_ASSERT(hdr_u.raw[i] == 0, "RTE_DEFINE_FLEX not zeroed"); + for (size_t i = 0; i < sizeof(hdr_one_u.raw); i++) + RTE_TEST_ASSERT(hdr_one_u.raw[i] == 0, "RTE_DEFINE_FLEX not zeroed, nested array"); + for (size_t i = 0; i < sizeof(hdr_bytes_u.raw); i++) + RTE_TEST_ASSERT(hdr_bytes_u.raw[i] == 0, "RTE_DEFINE_FLEX_BYTES not zeroed"); + + hdr->num = 4; + for (uint16_t i = 0; i < hdr->num; i++) + hdr->elems[i].b = i; + RTE_TEST_ASSERT_EQUAL(hdr->elems[3].b, 3, "RTE_DEFINE_FLEX element access"); + + return 0; +} + static struct unit_test_suite common_test_suite = { .suite_name = "common autotest", .setup = NULL, @@ -805,6 +858,7 @@ static struct unit_test_suite common_test_suite = { TEST_CASE(test_ptr_align_edge_cases), TEST_CASE(test_align), TEST_CASE(test_macros), + TEST_CASE(test_define_flex), TEST_CASE(test_misc), TEST_CASE(test_bsf), TEST_CASE(test_log2), diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h index fed373f412..4f55b776f8 100644 --- a/lib/eal/include/rte_common.h +++ b/lib/eal/include/rte_common.h @@ -1114,6 +1114,52 @@ __extension__ typedef uint64_t RTE_MARKER64[0]; */ #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field)) +/** + * Define a zero-initialized, local buffer of a given size in bytes, + * accessed via a pointer to a structure type. + * + * This is intended for structures ending in a variable-length array, + * e.g. messages with a header followed by a list of elements, + * allowing them to be placed on the stack rather than dynamically allocated. + * The buffer is suitably aligned for the structure type. + * + * @param type + * The type of the structure at the start of the buffer. + * @param name + * The name of the pointer variable to define, of type "type * const". + * @param size + * The size of the buffer in bytes, a compile-time constant + * which must be no smaller than the size of the structure type. + */ +#define RTE_DEFINE_FLEX_BYTES(type, name, size) \ + static_assert((size) >= sizeof(type), "buffer smaller than " RTE_STR(type)); \ + union { \ + uint8_t raw[size]; /* first member, so "{0}" zeroes the whole buffer */ \ + type obj; \ + } name ## _u = {0}; \ + type * const name = &name ## _u.obj + +/** + * Define a zero-initialized, local buffer for a structure ending in an array, + * with space for a given number of array elements beyond the size of the structure. + * + * The buffer is sized as "sizeof(type) + count * sizeof(element)", so for structures + * whose array is declared with a single element, e.g. "elem[1]", + * "count" elements are available in addition to that one. + * + * @param type + * The type of the structure. + * @param name + * The name of the pointer variable to define, of type "type * const". + * @param member + * The array member at the end of the structure. + * @param count + * The number of extra array elements to allow for, a compile-time constant. + */ +#define RTE_DEFINE_FLEX(type, name, member, count) \ + RTE_DEFINE_FLEX_BYTES(type, name, \ + sizeof(type) + RTE_SIZEOF_FIELD(type, member[0]) * (count)) + #define _RTE_STR(x) #x /** Take a macro value and get a string version of it */ #define RTE_STR(x) _RTE_STR(x) -- 2.55.0

