1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2023 Alexander V. Chernikov
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #include <tests/ktest.h>
29 #include <sys/cdefs.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/mbuf.h>
33 #include <netlink/netlink.h>
34 #include <netlink/netlink_ctl.h>
35 #include <netlink/netlink_message_writer.h>
36 
37 #define KTEST_CALLER
38 #include <netlink/ktest_netlink_message_writer.h>
39 
40 #ifdef INVARIANTS
41 
42 struct test_mbuf_attrs {
43 	uint32_t	size;
44 	uint32_t	expected_avail;
45 	uint32_t	expected_count;
46 	uint32_t	wtype;
47 	int		waitok;
48 };
49 
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 },
57 };
58 #undef _OUT
59 NL_DECLARE_ATTR_PARSER(mbuf_w_parser, nla_p_mbuf_w);
60 
61 static int
62 test_mbuf_parser(struct ktest_test_context *ctx, struct nlattr *nla)
63 {
64 	struct test_mbuf_attrs *attrs = npt_alloc(ctx->npt, sizeof(*attrs));
65 
66 	ctx->arg = attrs;
67 	if (attrs != NULL)
68 		return (nl_parse_nested(nla, &mbuf_w_parser, ctx->npt, attrs));
69 	return (ENOMEM);
70 }
71 
72 static int
73 test_mbuf_writer_allocation(struct ktest_test_context *ctx)
74 {
75 	struct test_mbuf_attrs *attrs = ctx->arg;
76 	bool ret;
77 	struct nl_writer nw = {};
78 
79 	ret = nlmsg_get_buf_type_wrapper(&nw, attrs->size, attrs->wtype, attrs->waitok);
80 	if (!ret)
81 		return (EINVAL);
82 
83 	int alloc_len = nw.alloc_len;
84 	KTEST_LOG(ctx, "requested %u, allocated %d", attrs->size, alloc_len);
85 
86 	/* Set cleanup callback */
87 	nw.writer_target = NS_WRITER_TARGET_SOCKET;
88 	nlmsg_set_callback_wrapper(&nw);
89 
90 	/* Mark enomem to avoid reallocation */
91 	nw.enomem = true;
92 
93 	if (nlmsg_reserve_data(&nw, alloc_len, void *) == NULL) {
94 		KTEST_LOG(ctx, "unable to get %d bytes from the writer", alloc_len);
95 		return (EINVAL);
96 	}
97 
98 	/* Mark as empty to free the storage */
99 	nw.offset = 0;
100 	nlmsg_flush(&nw);
101 
102 	if (alloc_len < attrs->expected_avail) {
103 		KTEST_LOG(ctx, "alloc_len %d, expected %u",
104 		    alloc_len, attrs->expected_avail);
105 		return (EINVAL);
106 	}
107 
108 	return (0);
109 }
110 
111 static int
112 test_mbuf_chain_allocation(struct ktest_test_context *ctx)
113 {
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);
117 
118 	if (chain == NULL) {
119 		KTEST_LOG(ctx, "nl_get_mbuf_chain(%u) returned NULL", attrs->size);
120 		return (EINVAL);
121 	}
122 
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) {
126 		allocated_count++;
127 		allocated_size += M_SIZE(m);
128 	}
129 	m_freem(chain);
130 
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);
136 		return (EINVAL);
137 	}
138 
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);
144 		return (EINVAL);
145 	}
146 
147 	return (0);
148 }
149 #endif
150 
151 static const struct ktest_test_info tests[] = {
152 #ifdef INVARIANTS
153 	{
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,
158 	},
159 	{
160 		.name = "test_mbuf_chain_allocation",
161 		.desc = "verify allocation different chain sizes",
162 		.func = &test_mbuf_chain_allocation,
163 		.parse = &test_mbuf_parser,
164 	},
165 #endif
166 };
167 KTEST_MODULE_DECLARE(ktest_netlink_message_writer, tests);
168