2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2023 Alexander V. Chernikov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <tests/ktest.h>
29 #include <sys/cdefs.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
33 #include <netlink/netlink.h>
34 #include <netlink/netlink_ctl.h>
35 #include <netlink/netlink_message_writer.h>
38 #include <netlink/ktest_netlink_message_writer.h>
42 struct test_mbuf_attrs {
44 uint32_t expected_avail;
45 uint32_t expected_count;
50 #define _OUT(_field) offsetof(struct test_mbuf_attrs, _field)
51 static const struct nlattr_parser nla_p_mbuf_w[] = {
52 { .type = 1, .off = _OUT(size), .cb = nlattr_get_uint32 },
53 { .type = 2, .off = _OUT(expected_avail), .cb = nlattr_get_uint32 },
54 { .type = 3, .off = _OUT(expected_count), .cb = nlattr_get_uint32 },
55 { .type = 4, .off = _OUT(wtype), .cb = nlattr_get_uint32 },
56 { .type = 5, .off = _OUT(waitok), .cb = nlattr_get_uint32 },
59 NL_DECLARE_ATTR_PARSER(mbuf_w_parser, nla_p_mbuf_w);
62 test_mbuf_parser(struct ktest_test_context *ctx, struct nlattr *nla)
64 struct test_mbuf_attrs *attrs = npt_alloc(ctx->npt, sizeof(*attrs));
68 return (nl_parse_nested(nla, &mbuf_w_parser, ctx->npt, attrs));
73 test_mbuf_writer_allocation(struct ktest_test_context *ctx)
75 struct test_mbuf_attrs *attrs = ctx->arg;
77 struct nl_writer nw = {};
79 ret = nlmsg_get_buf_type_wrapper(&nw, attrs->size, attrs->wtype, attrs->waitok);
83 int alloc_len = nw.alloc_len;
84 KTEST_LOG(ctx, "requested %u, allocated %d", attrs->size, alloc_len);
86 /* Set cleanup callback */
87 nw.writer_target = NS_WRITER_TARGET_SOCKET;
88 nlmsg_set_callback_wrapper(&nw);
90 /* Mark enomem to avoid reallocation */
93 if (nlmsg_reserve_data(&nw, alloc_len, void *) == NULL) {
94 KTEST_LOG(ctx, "unable to get %d bytes from the writer", alloc_len);
98 /* Mark as empty to free the storage */
102 if (alloc_len < attrs->expected_avail) {
103 KTEST_LOG(ctx, "alloc_len %d, expected %u",
104 alloc_len, attrs->expected_avail);
112 test_mbuf_chain_allocation(struct ktest_test_context *ctx)
114 struct test_mbuf_attrs *attrs = ctx->arg;
115 int mflags = attrs->waitok ? M_WAITOK : M_NOWAIT;
116 struct mbuf *chain = nl_get_mbuf_chain_wrapper(attrs->size, mflags);
119 KTEST_LOG(ctx, "nl_get_mbuf_chain(%u) returned NULL", attrs->size);
123 /* Iterate and check number of mbufs and space */
124 uint32_t allocated_count = 0, allocated_size = 0;
125 for (struct mbuf *m = chain; m != NULL; m = m->m_next) {
127 allocated_size += M_SIZE(m);
131 if (attrs->expected_avail > allocated_size) {
132 KTEST_LOG(ctx, "expected/allocated avail(bytes) %u/%u"
133 " expected/allocated count %u/%u",
134 attrs->expected_avail, allocated_size,
135 attrs->expected_count, allocated_count);
139 if (attrs->expected_count > 0 && (attrs->expected_count != allocated_count)) {
140 KTEST_LOG(ctx, "expected/allocated avail(bytes) %u/%u"
141 " expected/allocated count %u/%u",
142 attrs->expected_avail, allocated_size,
143 attrs->expected_count, allocated_count);
151 static const struct ktest_test_info tests[] = {
154 .name = "test_mbuf_writer_allocation",
155 .desc = "test different mbuf sizes in the mbuf writer",
156 .func = &test_mbuf_writer_allocation,
157 .parse = &test_mbuf_parser,
160 .name = "test_mbuf_chain_allocation",
161 .desc = "verify allocation different chain sizes",
162 .func = &test_mbuf_chain_allocation,
163 .parse = &test_mbuf_parser,
167 KTEST_MODULE_DECLARE(ktest_netlink_message_writer, tests);