xref: /freebsd/sys/tests/framework/kern_testfrwk.c (revision b0b1dbdd)
1 /*-
2  * Copyright (c) 2015
3  *	Netflix Incorporated, All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/callout.h>
36 #include <sys/module.h>
37 #include <sys/kernel.h>
38 #include <sys/ktr.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/sdt.h>
43 #include <sys/sysctl.h>
44 #include <sys/taskqueue.h>
45 #include <sys/smp.h>
46 #include <sys/queue.h>
47 #include <tests/kern_testfrwk.h>
48 #ifdef SMP
49 #include <machine/cpu.h>
50 #endif
51 
52 struct kern_test_list {
53 	TAILQ_ENTRY(kern_test_list) next;
54 	char name[TEST_NAME_LEN];
55 	kerntfunc func;
56 };
57 
58 TAILQ_HEAD(ktestlist, kern_test_list);
59 
60 struct kern_test_entry {
61 	TAILQ_ENTRY(kern_test_entry) next;
62 	struct kern_test_list *kt_e;
63 	struct kern_test kt_data;
64 };
65 
66 TAILQ_HEAD(ktestqueue, kern_test_entry);
67 
68 MALLOC_DEFINE(M_KTFRWK, "kern_tfrwk", "Kernel Test Framework");
69 struct kern_totfrwk {
70 	struct taskqueue *kfrwk_tq;
71 	struct task kfrwk_que;
72 	struct ktestlist kfrwk_testlist;
73 	struct ktestqueue kfrwk_testq;
74 	struct mtx kfrwk_mtx;
75 	int kfrwk_waiting;
76 };
77 
78 struct kern_totfrwk kfrwk;
79 static int ktest_frwk_inited = 0;
80 
81 #define KTFRWK_MUTEX_INIT() mtx_init(&kfrwk.kfrwk_mtx, "kern_test_frwk", "tfrwk", MTX_DEF)
82 
83 #define KTFRWK_DESTROY() mtx_destroy(&kfrwk.kfrwk_mtx)
84 
85 #define KTFRWK_LOCK() mtx_lock(&kfrwk.kfrwk_mtx)
86 
87 #define KTFRWK_UNLOCK()	mtx_unlock(&kfrwk.kfrwk_mtx)
88 
89 static void
90 kfrwk_task(void *context, int pending)
91 {
92 	struct kern_totfrwk *tf;
93 	struct kern_test_entry *wk;
94 	int free_mem = 0;
95 	struct kern_test kt_data;
96 	kerntfunc ktf;
97 
98 	memset(&kt_data, 0, sizeof(kt_data));
99 	ktf = NULL;
100 	tf = (struct kern_totfrwk *)context;
101 	KTFRWK_LOCK();
102 	wk = TAILQ_FIRST(&tf->kfrwk_testq);
103 	if (wk) {
104 		wk->kt_data.tot_threads_running--;
105 		tf->kfrwk_waiting--;
106 		memcpy(&kt_data, &wk->kt_data, sizeof(kt_data));
107 		if (wk->kt_data.tot_threads_running == 0) {
108 			TAILQ_REMOVE(&tf->kfrwk_testq, wk, next);
109 			free_mem = 1;
110 		} else {
111 			/* Wake one of my colleages up to help too */
112 			taskqueue_enqueue(tf->kfrwk_tq, &tf->kfrwk_que);
113 		}
114 		if (wk->kt_e) {
115 			ktf = wk->kt_e->func;
116 		}
117 	}
118 	KTFRWK_UNLOCK();
119 	if (wk && free_mem) {
120 		free(wk, M_KTFRWK);
121 	}
122 	/* Execute the test */
123 	if (ktf) {
124 		(*ktf) (&kt_data);
125 	}
126 	/* We are done */
127 	atomic_add_int(&tf->kfrwk_waiting, 1);
128 }
129 
130 static int
131 kerntest_frwk_init(void)
132 {
133 	u_int ncpus = mp_ncpus ? mp_ncpus : MAXCPU;
134 
135 	KTFRWK_MUTEX_INIT();
136 	TAILQ_INIT(&kfrwk.kfrwk_testq);
137 	TAILQ_INIT(&kfrwk.kfrwk_testlist);
138 	/* Now lets start up a number of tasks to do the work */
139 	TASK_INIT(&kfrwk.kfrwk_que, 0, kfrwk_task, &kfrwk);
140 	kfrwk.kfrwk_tq = taskqueue_create_fast("sbtls_task", M_NOWAIT,
141 	    taskqueue_thread_enqueue, &kfrwk.kfrwk_tq);
142 	if (kfrwk.kfrwk_tq == NULL) {
143 		printf("Can't start taskqueue for Kernel Test Framework\n");
144 		panic("Taskqueue init fails for kfrwk");
145 	}
146 	taskqueue_start_threads(&kfrwk.kfrwk_tq, ncpus, PI_NET, "[kt_frwk task]");
147 	kfrwk.kfrwk_waiting = ncpus;
148 	ktest_frwk_inited = 1;
149 	return (0);
150 }
151 
152 static int
153 kerntest_frwk_fini(void)
154 {
155 	KTFRWK_LOCK();
156 	if (!TAILQ_EMPTY(&kfrwk.kfrwk_testlist)) {
157 		/* Still modules registered */
158 		KTFRWK_UNLOCK();
159 		return (EBUSY);
160 	}
161 	ktest_frwk_inited = 0;
162 	KTFRWK_UNLOCK();
163 	taskqueue_free(kfrwk.kfrwk_tq);
164 	/* Ok lets destroy the mutex on the way outs */
165 	KTFRWK_DESTROY();
166 	return (0);
167 }
168 
169 
170 static int kerntest_execute(SYSCTL_HANDLER_ARGS);
171 
172 SYSCTL_NODE(_kern, OID_AUTO, testfrwk, CTLFLAG_RW, 0, "Kernel Test Framework");
173 SYSCTL_PROC(_kern_testfrwk, OID_AUTO, runtest, (CTLTYPE_STRUCT | CTLFLAG_RW),
174     0, 0, kerntest_execute, "IU", "Execute a kernel test");
175 
176 int
177 kerntest_execute(SYSCTL_HANDLER_ARGS)
178 {
179 	struct kern_test kt;
180 	struct kern_test_list *li, *te = NULL;
181 	struct kern_test_entry *kte = NULL;
182 	int error = 0;
183 
184 	if (ktest_frwk_inited == 0) {
185 		return (ENOENT);
186 	}
187 	/* Find the entry if possible */
188 	error = SYSCTL_IN(req, &kt, sizeof(struct kern_test));
189 	if (error) {
190 		return (error);
191 	}
192 	if (kt.num_threads <= 0) {
193 		return (EINVAL);
194 	}
195 	/* Grab some memory */
196 	kte = malloc(sizeof(struct kern_test_entry), M_KTFRWK, M_WAITOK);
197 	if (kte == NULL) {
198 		error = ENOMEM;
199 		goto out;
200 	}
201 	KTFRWK_LOCK();
202 	TAILQ_FOREACH(li, &kfrwk.kfrwk_testlist, next) {
203 		if (strcmp(li->name, kt.name) == 0) {
204 			te = li;
205 			break;
206 		}
207 	}
208 	if (te == NULL) {
209 		printf("Can't find the test %s\n", kt.name);
210 		error = ENOENT;
211 		free(kte, M_KTFRWK);
212 		goto out;
213 	}
214 	/* Ok we have a test item to run, can we? */
215 	if (!TAILQ_EMPTY(&kfrwk.kfrwk_testq)) {
216 		/* We don't know if there is enough threads */
217 		error = EAGAIN;
218 		free(kte, M_KTFRWK);
219 		goto out;
220 	}
221 	if (kfrwk.kfrwk_waiting < kt.num_threads) {
222 		error = E2BIG;
223 		free(kte, M_KTFRWK);
224 		goto out;
225 	}
226 	kt.tot_threads_running = kt.num_threads;
227 	/* Ok it looks like we can do it, lets get an entry */
228 	kte->kt_e = li;
229 	memcpy(&kte->kt_data, &kt, sizeof(kt));
230 	TAILQ_INSERT_TAIL(&kfrwk.kfrwk_testq, kte, next);
231 	taskqueue_enqueue(kfrwk.kfrwk_tq, &kfrwk.kfrwk_que);
232 out:
233 	KTFRWK_UNLOCK();
234 	return (error);
235 }
236 
237 int
238 kern_testframework_register(const char *name, kerntfunc func)
239 {
240 	int error = 0;
241 	struct kern_test_list *li, *te = NULL;
242 	int len;
243 
244 	len = strlen(name);
245 	if (len >= TEST_NAME_LEN) {
246 		return (E2BIG);
247 	}
248 	te = malloc(sizeof(struct kern_test_list), M_KTFRWK, M_WAITOK);
249 	if (te == NULL) {
250 		error = ENOMEM;
251 		goto out;
252 	}
253 	KTFRWK_LOCK();
254 	/* First does it already exist? */
255 	TAILQ_FOREACH(li, &kfrwk.kfrwk_testlist, next) {
256 		if (strcmp(li->name, name) == 0) {
257 			error = EALREADY;
258 			free(te, M_KTFRWK);
259 			goto out;
260 		}
261 	}
262 	/* Ok we can do it, lets add it to the list */
263 	te->func = func;
264 	strcpy(te->name, name);
265 	TAILQ_INSERT_TAIL(&kfrwk.kfrwk_testlist, te, next);
266 out:
267 	KTFRWK_UNLOCK();
268 	return (error);
269 }
270 
271 int
272 kern_testframework_deregister(const char *name)
273 {
274 	struct kern_test_list *li, *te = NULL;
275 	u_int ncpus = mp_ncpus ? mp_ncpus : MAXCPU;
276 	int error = 0;
277 
278 	KTFRWK_LOCK();
279 	/* First does it already exist? */
280 	TAILQ_FOREACH(li, &kfrwk.kfrwk_testlist, next) {
281 		if (strcmp(li->name, name) == 0) {
282 			te = li;
283 			break;
284 		}
285 	}
286 	if (te == NULL) {
287 		/* It is not registered so no problem */
288 		goto out;
289 	}
290 	if (ncpus != kfrwk.kfrwk_waiting) {
291 		/* We are busy executing something -- can't unload */
292 		error = EBUSY;
293 		goto out;
294 	}
295 	if (!TAILQ_EMPTY(&kfrwk.kfrwk_testq)) {
296 		/* Something still to execute */
297 		error = EBUSY;
298 		goto out;
299 	}
300 	/* Ok we can remove the dude safely */
301 	TAILQ_REMOVE(&kfrwk.kfrwk_testlist, te, next);
302 	memset(te, 0, sizeof(struct kern_test_list));
303 	free(te, M_KTFRWK);
304 out:
305 	KTFRWK_UNLOCK();
306 	return (error);
307 }
308 
309 static int
310 kerntest_mod_init(module_t mod, int type, void *data)
311 {
312 	int err;
313 
314 	switch (type) {
315 	case MOD_LOAD:
316 		err = kerntest_frwk_init();
317 		break;
318 	case MOD_QUIESCE:
319 		KTFRWK_LOCK();
320 		if (TAILQ_EMPTY(&kfrwk.kfrwk_testlist)) {
321 			err = 0;
322 		} else {
323 			err = EBUSY;
324 		}
325 		KTFRWK_UNLOCK();
326 		break;
327 	case MOD_UNLOAD:
328 		err = kerntest_frwk_fini();
329 		break;
330 	default:
331 		return (EOPNOTSUPP);
332 	}
333 	return (err);
334 }
335 
336 static moduledata_t kern_test_framework = {
337 	.name = "kernel_testfrwk",
338 	.evhand = kerntest_mod_init,
339 	.priv = 0
340 };
341 
342 MODULE_VERSION(kern_testframework, 1);
343 DECLARE_MODULE(kern_testframework, kern_test_framework, SI_SUB_PSEUDO, SI_ORDER_ANY);
344