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 "opt_netlink.h"
30 #include <tests/ktest.h>
31 #include <sys/cdefs.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
35 #include <netlink/netlink.h>
36 #include <netlink/netlink_ctl.h>
37 #include <netlink/netlink_message_writer.h>
40 #include <netlink/ktest_netlink_message_writer.h>
44 struct test_mbuf_attrs {
46 uint32_t expected_avail;
47 uint32_t expected_count;
52 #define _OUT(_field) offsetof(struct test_mbuf_attrs, _field)
53 static const struct nlattr_parser nla_p_mbuf_w[] = {
54 { .type = 1, .off = _OUT(size), .cb = nlattr_get_uint32 },
55 { .type = 2, .off = _OUT(expected_avail), .cb = nlattr_get_uint32 },
56 { .type = 3, .off = _OUT(expected_count), .cb = nlattr_get_uint32 },
57 { .type = 4, .off = _OUT(wtype), .cb = nlattr_get_uint32 },
58 { .type = 5, .off = _OUT(waitok), .cb = nlattr_get_uint32 },
61 NL_DECLARE_ATTR_PARSER(mbuf_w_parser, nla_p_mbuf_w);
64 test_mbuf_parser(struct ktest_test_context *ctx, struct nlattr *nla)
66 struct test_mbuf_attrs *attrs = npt_alloc(ctx->npt, sizeof(*attrs));
70 return (nl_parse_nested(nla, &mbuf_w_parser, ctx->npt, attrs));
75 test_mbuf_writer_allocation(struct ktest_test_context *ctx)
77 struct test_mbuf_attrs *attrs = ctx->arg;
79 struct nl_writer nw = {};
81 ret = nlmsg_get_buf_type_wrapper(&nw, attrs->size, attrs->wtype, attrs->waitok);
85 int alloc_len = nw.alloc_len;
86 KTEST_LOG(ctx, "requested %u, allocated %d", attrs->size, alloc_len);
88 /* Set cleanup callback */
89 nw.writer_target = NS_WRITER_TARGET_SOCKET;
90 nlmsg_set_callback_wrapper(&nw);
92 /* Mark enomem to avoid reallocation */
95 if (nlmsg_reserve_data(&nw, alloc_len, void *) == NULL) {
96 KTEST_LOG(ctx, "unable to get %d bytes from the writer", alloc_len);
100 /* Mark as empty to free the storage */
104 if (alloc_len < attrs->expected_avail) {
105 KTEST_LOG(ctx, "alloc_len %d, expected %u",
106 alloc_len, attrs->expected_avail);
114 test_mbuf_chain_allocation(struct ktest_test_context *ctx)
116 struct test_mbuf_attrs *attrs = ctx->arg;
117 int mflags = attrs->waitok ? M_WAITOK : M_NOWAIT;
118 struct mbuf *chain = nl_get_mbuf_chain_wrapper(attrs->size, mflags);
121 KTEST_LOG(ctx, "nl_get_mbuf_chain(%u) returned NULL", attrs->size);
125 /* Iterate and check number of mbufs and space */
126 uint32_t allocated_count = 0, allocated_size = 0;
127 for (struct mbuf *m = chain; m != NULL; m = m->m_next) {
129 allocated_size += M_SIZE(m);
133 if (attrs->expected_avail > allocated_size) {
134 KTEST_LOG(ctx, "expected/allocated avail(bytes) %u/%u"
135 " expected/allocated count %u/%u",
136 attrs->expected_avail, allocated_size,
137 attrs->expected_count, allocated_count);
141 if (attrs->expected_count > 0 && (attrs->expected_count != allocated_count)) {
142 KTEST_LOG(ctx, "expected/allocated avail(bytes) %u/%u"
143 " expected/allocated count %u/%u",
144 attrs->expected_avail, allocated_size,
145 attrs->expected_count, allocated_count);
153 static const struct ktest_test_info tests[] = {
156 .name = "test_mbuf_writer_allocation",
157 .desc = "test different mbuf sizes in the mbuf writer",
158 .func = &test_mbuf_writer_allocation,
159 .parse = &test_mbuf_parser,
162 .name = "test_mbuf_chain_allocation",
163 .desc = "verify allocation different chain sizes",
164 .func = &test_mbuf_chain_allocation,
165 .parse = &test_mbuf_parser,
169 KTEST_MODULE_DECLARE(ktest_netlink_message_writer, tests);