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 "opt_netlink.h"
29 
30 #include <tests/ktest.h>
31 #include <sys/cdefs.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/mbuf.h>
35 #include <netlink/netlink.h>
36 #include <netlink/netlink_ctl.h>
37 #include <netlink/netlink_message_writer.h>
38 
39 #define KTEST_CALLER
40 #include <netlink/ktest_netlink_message_writer.h>
41 
42 #ifdef INVARIANTS
43 
44 struct test_mbuf_attrs {
45 	uint32_t	size;
46 	uint32_t	expected_avail;
47 	uint32_t	expected_count;
48 	uint32_t	wtype;
49 	int		waitok;
50 };
51 
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 },
59 };
60 #undef _OUT
61 NL_DECLARE_ATTR_PARSER(mbuf_w_parser, nla_p_mbuf_w);
62 
63 static int
64 test_mbuf_parser(struct ktest_test_context *ctx, struct nlattr *nla)
65 {
66 	struct test_mbuf_attrs *attrs = npt_alloc(ctx->npt, sizeof(*attrs));
67 
68 	ctx->arg = attrs;
69 	if (attrs != NULL)
70 		return (nl_parse_nested(nla, &mbuf_w_parser, ctx->npt, attrs));
71 	return (ENOMEM);
72 }
73 
74 static int
75 test_mbuf_writer_allocation(struct ktest_test_context *ctx)
76 {
77 	struct test_mbuf_attrs *attrs = ctx->arg;
78 	bool ret;
79 	struct nl_writer nw = {};
80 
81 	ret = nlmsg_get_buf_type_wrapper(&nw, attrs->size, attrs->wtype, attrs->waitok);
82 	if (!ret)
83 		return (EINVAL);
84 
85 	int alloc_len = nw.alloc_len;
86 	KTEST_LOG(ctx, "requested %u, allocated %d", attrs->size, alloc_len);
87 
88 	/* Set cleanup callback */
89 	nw.writer_target = NS_WRITER_TARGET_SOCKET;
90 	nlmsg_set_callback_wrapper(&nw);
91 
92 	/* Mark enomem to avoid reallocation */
93 	nw.enomem = true;
94 
95 	if (nlmsg_reserve_data(&nw, alloc_len, void *) == NULL) {
96 		KTEST_LOG(ctx, "unable to get %d bytes from the writer", alloc_len);
97 		return (EINVAL);
98 	}
99 
100 	/* Mark as empty to free the storage */
101 	nw.offset = 0;
102 	nlmsg_flush(&nw);
103 
104 	if (alloc_len < attrs->expected_avail) {
105 		KTEST_LOG(ctx, "alloc_len %d, expected %u",
106 		    alloc_len, attrs->expected_avail);
107 		return (EINVAL);
108 	}
109 
110 	return (0);
111 }
112 
113 static int
114 test_mbuf_chain_allocation(struct ktest_test_context *ctx)
115 {
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);
119 
120 	if (chain == NULL) {
121 		KTEST_LOG(ctx, "nl_get_mbuf_chain(%u) returned NULL", attrs->size);
122 		return (EINVAL);
123 	}
124 
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) {
128 		allocated_count++;
129 		allocated_size += M_SIZE(m);
130 	}
131 	m_freem(chain);
132 
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);
138 		return (EINVAL);
139 	}
140 
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);
146 		return (EINVAL);
147 	}
148 
149 	return (0);
150 }
151 #endif
152 
153 static const struct ktest_test_info tests[] = {
154 #ifdef INVARIANTS
155 	{
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,
160 	},
161 	{
162 		.name = "test_mbuf_chain_allocation",
163 		.desc = "verify allocation different chain sizes",
164 		.func = &test_mbuf_chain_allocation,
165 		.parse = &test_mbuf_parser,
166 	},
167 #endif
168 };
169 KTEST_MODULE_DECLARE(ktest_netlink_message_writer, tests);
170