1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
9 #include <rte_common.h>
10 #include <rte_cycles.h>
11 #include <rte_interrupts.h>
15 #define TEST_INTERRUPT_CHECK_INTERVAL 100 /* ms */
17 /* predefined interrupt handle types */
18 enum test_interrupt_handle_type {
19 TEST_INTERRUPT_HANDLE_INVALID,
20 TEST_INTERRUPT_HANDLE_VALID,
21 TEST_INTERRUPT_HANDLE_VALID_UIO,
22 TEST_INTERRUPT_HANDLE_VALID_ALARM,
23 TEST_INTERRUPT_HANDLE_CASE1,
24 TEST_INTERRUPT_HANDLE_MAX
27 /* flag of if callback is called */
28 static volatile int flag;
29 static struct rte_intr_handle intr_handles[TEST_INTERRUPT_HANDLE_MAX];
30 static enum test_interrupt_handle_type test_intr_type =
31 TEST_INTERRUPT_HANDLE_MAX;
33 #ifdef RTE_EXEC_ENV_LINUXAPP
44 static union intr_pipefds pfds;
47 * Check if the interrupt handle is valid.
50 test_interrupt_handle_sanity_check(struct rte_intr_handle *intr_handle)
52 if (!intr_handle || intr_handle->fd < 0)
59 * Initialization for interrupt test.
62 test_interrupt_init(void)
64 if (pipe(pfds.pipefd) < 0)
67 intr_handles[TEST_INTERRUPT_HANDLE_INVALID].fd = -1;
68 intr_handles[TEST_INTERRUPT_HANDLE_INVALID].type =
69 RTE_INTR_HANDLE_UNKNOWN;
71 intr_handles[TEST_INTERRUPT_HANDLE_VALID].fd = pfds.readfd;
72 intr_handles[TEST_INTERRUPT_HANDLE_VALID].type =
73 RTE_INTR_HANDLE_UNKNOWN;
75 intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO].fd = pfds.readfd;
76 intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO].type =
79 intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM].fd = pfds.readfd;
80 intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM].type =
81 RTE_INTR_HANDLE_ALARM;
83 intr_handles[TEST_INTERRUPT_HANDLE_CASE1].fd = pfds.writefd;
84 intr_handles[TEST_INTERRUPT_HANDLE_CASE1].type = RTE_INTR_HANDLE_UIO;
90 * Deinitialization for interrupt test.
93 test_interrupt_deinit(void)
95 close(pfds.pipefd[0]);
96 close(pfds.pipefd[1]);
102 * Write the pipe to simulate an interrupt.
105 test_interrupt_trigger_interrupt(void)
107 if (write(pfds.writefd, "1", 1) < 0)
114 * Check if two interrupt handles are the same.
117 test_interrupt_handle_compare(struct rte_intr_handle *intr_handle_l,
118 struct rte_intr_handle *intr_handle_r)
120 if (!intr_handle_l || !intr_handle_r)
123 if (intr_handle_l->fd != intr_handle_r->fd ||
124 intr_handle_l->type != intr_handle_r->type)
131 /* to be implemented for bsd later */
133 test_interrupt_handle_sanity_check(struct rte_intr_handle *intr_handle)
135 RTE_SET_USED(intr_handle);
141 test_interrupt_init(void)
147 test_interrupt_deinit(void)
153 test_interrupt_trigger_interrupt(void)
159 test_interrupt_handle_compare(struct rte_intr_handle *intr_handle_l,
160 struct rte_intr_handle *intr_handle_r)
167 #endif /* RTE_EXEC_ENV_LINUXAPP */
170 * Callback for the test interrupt.
173 test_interrupt_callback(void *arg)
175 struct rte_intr_handle *intr_handle = arg;
176 if (test_intr_type >= TEST_INTERRUPT_HANDLE_MAX) {
177 printf("invalid interrupt type\n");
182 if (test_interrupt_handle_sanity_check(intr_handle) < 0) {
183 printf("null or invalid intr_handle for %s\n", __func__);
188 if (rte_intr_callback_unregister(intr_handle,
189 test_interrupt_callback, arg) >= 0) {
190 printf("%s: unexpectedly able to unregister itself\n",
196 if (test_interrupt_handle_compare(intr_handle,
197 &(intr_handles[test_intr_type])) == 0)
202 * Callback for the test interrupt.
205 test_interrupt_callback_1(void *arg)
207 struct rte_intr_handle *intr_handle = arg;
208 if (test_interrupt_handle_sanity_check(intr_handle) < 0) {
209 printf("null or invalid intr_handle for %s\n", __func__);
216 * Tests for rte_intr_enable().
219 test_interrupt_enable(void)
221 struct rte_intr_handle test_intr_handle;
223 /* check with null intr_handle */
224 if (rte_intr_enable(NULL) == 0) {
225 printf("unexpectedly enable null intr_handle successfully\n");
229 /* check with invalid intr_handle */
230 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
231 if (rte_intr_enable(&test_intr_handle) == 0) {
232 printf("unexpectedly enable invalid intr_handle "
237 /* check with valid intr_handle */
238 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
239 if (rte_intr_enable(&test_intr_handle) == 0) {
240 printf("unexpectedly enable a specific intr_handle "
245 /* check with specific valid intr_handle */
246 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM];
247 if (rte_intr_enable(&test_intr_handle) == 0) {
248 printf("unexpectedly enable a specific intr_handle "
253 /* check with valid handler and its type */
254 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_CASE1];
255 if (rte_intr_enable(&test_intr_handle) < 0) {
256 printf("fail to enable interrupt on a simulated handler\n");
260 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO];
261 if (rte_intr_enable(&test_intr_handle) == 0) {
262 printf("unexpectedly enable a specific intr_handle "
271 * Tests for rte_intr_disable().
274 test_interrupt_disable(void)
276 struct rte_intr_handle test_intr_handle;
278 /* check with null intr_handle */
279 if (rte_intr_disable(NULL) == 0) {
280 printf("unexpectedly disable null intr_handle "
285 /* check with invalid intr_handle */
286 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
287 if (rte_intr_disable(&test_intr_handle) == 0) {
288 printf("unexpectedly disable invalid intr_handle "
293 /* check with valid intr_handle */
294 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
295 if (rte_intr_disable(&test_intr_handle) == 0) {
296 printf("unexpectedly disable a specific intr_handle "
301 /* check with specific valid intr_handle */
302 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM];
303 if (rte_intr_disable(&test_intr_handle) == 0) {
304 printf("unexpectedly disable a specific intr_handle "
309 /* check with valid handler and its type */
310 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_CASE1];
311 if (rte_intr_disable(&test_intr_handle) < 0) {
312 printf("fail to disable interrupt on a simulated handler\n");
316 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO];
317 if (rte_intr_disable(&test_intr_handle) == 0) {
318 printf("unexpectedly disable a specific intr_handle "
327 * Check the full path of a specified type of interrupt simulated.
330 test_interrupt_full_path_check(enum test_interrupt_handle_type intr_type)
333 struct rte_intr_handle test_intr_handle;
336 test_intr_handle = intr_handles[intr_type];
337 test_intr_type = intr_type;
338 if (rte_intr_callback_register(&test_intr_handle,
339 test_interrupt_callback, &test_intr_handle) < 0) {
340 printf("fail to register callback\n");
344 if (test_interrupt_trigger_interrupt() < 0)
348 for (count = 0; flag == 0 && count < 3; count++)
349 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
351 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
352 if (rte_intr_callback_unregister(&test_intr_handle,
353 test_interrupt_callback, &test_intr_handle) < 0)
357 printf("callback has not been called\n");
359 } else if (flag < 0) {
360 printf("it has internal error in callback\n");
368 * Main function of testing interrupt.
374 struct rte_intr_handle test_intr_handle;
376 if (test_interrupt_init() < 0) {
377 printf("fail to initialize for testing interrupt\n");
381 printf("Check unknown valid interrupt full path\n");
382 if (test_interrupt_full_path_check(TEST_INTERRUPT_HANDLE_VALID) < 0) {
383 printf("failure occurred during checking unknown valid "
384 "interrupt full path\n");
388 printf("Check valid UIO interrupt full path\n");
389 if (test_interrupt_full_path_check(TEST_INTERRUPT_HANDLE_VALID_UIO)
391 printf("failure occurred during checking valid UIO interrupt "
396 printf("Check valid alarm interrupt full path\n");
397 if (test_interrupt_full_path_check(TEST_INTERRUPT_HANDLE_VALID_ALARM)
399 printf("failure occurred during checking valid alarm "
400 "interrupt full path\n");
404 printf("start register/unregister test\n");
405 /* check if it will fail to register cb with intr_handle = NULL */
406 if (rte_intr_callback_register(NULL, test_interrupt_callback,
408 printf("unexpectedly register successfully with null "
413 /* check if it will fail to register cb with invalid intr_handle */
414 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
415 if (rte_intr_callback_register(&test_intr_handle,
416 test_interrupt_callback, &test_intr_handle) == 0) {
417 printf("unexpectedly register successfully with invalid "
422 /* check if it will fail to register without callback */
423 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
424 if (rte_intr_callback_register(&test_intr_handle, NULL, &test_intr_handle) == 0) {
425 printf("unexpectedly register successfully with "
430 /* check if it will fail to unregister cb with intr_handle = NULL */
431 if (rte_intr_callback_unregister(NULL,
432 test_interrupt_callback, NULL) > 0) {
433 printf("unexpectedly unregister successfully with "
434 "null intr_handle\n");
438 /* check if it will fail to unregister cb with invalid intr_handle */
439 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
440 if (rte_intr_callback_unregister(&test_intr_handle,
441 test_interrupt_callback, &test_intr_handle) > 0) {
442 printf("unexpectedly unregister successfully with "
443 "invalid intr_handle\n");
447 /* check if it is ok to register the same intr_handle twice */
448 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
449 if (rte_intr_callback_register(&test_intr_handle,
450 test_interrupt_callback, &test_intr_handle) < 0) {
451 printf("it fails to register test_interrupt_callback\n");
454 if (rte_intr_callback_register(&test_intr_handle,
455 test_interrupt_callback_1, &test_intr_handle) < 0) {
456 printf("it fails to register test_interrupt_callback_1\n");
459 /* check if it will fail to unregister with invalid parameter */
460 if (rte_intr_callback_unregister(&test_intr_handle,
461 test_interrupt_callback, (void *)0xff) != 0) {
462 printf("unexpectedly unregisters successfully with "
466 if (rte_intr_callback_unregister(&test_intr_handle,
467 test_interrupt_callback, &test_intr_handle) <= 0) {
468 printf("it fails to unregister test_interrupt_callback\n");
471 if (rte_intr_callback_unregister(&test_intr_handle,
472 test_interrupt_callback_1, (void *)-1) <= 0) {
473 printf("it fails to unregister test_interrupt_callback_1 "
477 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
479 printf("start interrupt enable/disable test\n");
480 /* check interrupt enable/disable functions */
481 if (test_interrupt_enable() < 0) {
482 printf("fail to check interrupt enabling\n");
485 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
487 if (test_interrupt_disable() < 0) {
488 printf("fail to check interrupt disabling\n");
491 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
496 printf("Clearing for interrupt tests\n");
497 /* clear registered callbacks */
498 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
499 rte_intr_callback_unregister(&test_intr_handle,
500 test_interrupt_callback, (void *)-1);
501 rte_intr_callback_unregister(&test_intr_handle,
502 test_interrupt_callback_1, (void *)-1);
504 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO];
505 rte_intr_callback_unregister(&test_intr_handle,
506 test_interrupt_callback, (void *)-1);
507 rte_intr_callback_unregister(&test_intr_handle,
508 test_interrupt_callback_1, (void *)-1);
510 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM];
511 rte_intr_callback_unregister(&test_intr_handle,
512 test_interrupt_callback, (void *)-1);
513 rte_intr_callback_unregister(&test_intr_handle,
514 test_interrupt_callback_1, (void *)-1);
516 rte_delay_ms(2 * TEST_INTERRUPT_CHECK_INTERVAL);
518 test_interrupt_deinit();
523 REGISTER_TEST_COMMAND(interrupt_autotest, test_interrupt);