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_VALID_DEV_EVENT,
24 TEST_INTERRUPT_HANDLE_CASE1,
25 TEST_INTERRUPT_HANDLE_MAX
28 /* flag of if callback is called */
29 static volatile int flag;
30 static struct rte_intr_handle intr_handles[TEST_INTERRUPT_HANDLE_MAX];
31 static enum test_interrupt_handle_type test_intr_type =
32 TEST_INTERRUPT_HANDLE_MAX;
34 #ifdef RTE_EXEC_ENV_LINUX
45 static union intr_pipefds pfds;
48 * Check if the interrupt handle is valid.
51 test_interrupt_handle_sanity_check(struct rte_intr_handle *intr_handle)
53 if (!intr_handle || intr_handle->fd < 0)
60 * Initialization for interrupt test.
63 test_interrupt_init(void)
65 if (pipe(pfds.pipefd) < 0)
68 intr_handles[TEST_INTERRUPT_HANDLE_INVALID].fd = -1;
69 intr_handles[TEST_INTERRUPT_HANDLE_INVALID].type =
70 RTE_INTR_HANDLE_UNKNOWN;
72 intr_handles[TEST_INTERRUPT_HANDLE_VALID].fd = pfds.readfd;
73 intr_handles[TEST_INTERRUPT_HANDLE_VALID].type =
74 RTE_INTR_HANDLE_UNKNOWN;
76 intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO].fd = pfds.readfd;
77 intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO].type =
80 intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM].fd = pfds.readfd;
81 intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM].type =
82 RTE_INTR_HANDLE_ALARM;
84 intr_handles[TEST_INTERRUPT_HANDLE_VALID_DEV_EVENT].fd = pfds.readfd;
85 intr_handles[TEST_INTERRUPT_HANDLE_VALID_DEV_EVENT].type =
86 RTE_INTR_HANDLE_DEV_EVENT;
88 intr_handles[TEST_INTERRUPT_HANDLE_CASE1].fd = pfds.writefd;
89 intr_handles[TEST_INTERRUPT_HANDLE_CASE1].type = RTE_INTR_HANDLE_UIO;
95 * Deinitialization for interrupt test.
98 test_interrupt_deinit(void)
100 close(pfds.pipefd[0]);
101 close(pfds.pipefd[1]);
107 * Write the pipe to simulate an interrupt.
110 test_interrupt_trigger_interrupt(void)
112 if (write(pfds.writefd, "1", 1) < 0)
119 * Check if two interrupt handles are the same.
122 test_interrupt_handle_compare(struct rte_intr_handle *intr_handle_l,
123 struct rte_intr_handle *intr_handle_r)
125 if (!intr_handle_l || !intr_handle_r)
128 if (intr_handle_l->fd != intr_handle_r->fd ||
129 intr_handle_l->type != intr_handle_r->type)
136 /* to be implemented for bsd later */
138 test_interrupt_handle_sanity_check(struct rte_intr_handle *intr_handle)
140 RTE_SET_USED(intr_handle);
146 test_interrupt_init(void)
152 test_interrupt_deinit(void)
158 test_interrupt_trigger_interrupt(void)
164 test_interrupt_handle_compare(struct rte_intr_handle *intr_handle_l,
165 struct rte_intr_handle *intr_handle_r)
172 #endif /* RTE_EXEC_ENV_LINUX */
175 * Callback for the test interrupt.
178 test_interrupt_callback(void *arg)
180 struct rte_intr_handle *intr_handle = arg;
181 if (test_intr_type >= TEST_INTERRUPT_HANDLE_MAX) {
182 printf("invalid interrupt type\n");
187 if (test_interrupt_handle_sanity_check(intr_handle) < 0) {
188 printf("null or invalid intr_handle for %s\n", __func__);
193 if (rte_intr_callback_unregister(intr_handle,
194 test_interrupt_callback, arg) >= 0) {
195 printf("%s: unexpectedly able to unregister itself\n",
201 if (test_interrupt_handle_compare(intr_handle,
202 &(intr_handles[test_intr_type])) == 0)
207 * Callback for the test interrupt.
210 test_interrupt_callback_1(void *arg)
212 struct rte_intr_handle *intr_handle = arg;
213 if (test_interrupt_handle_sanity_check(intr_handle) < 0) {
214 printf("null or invalid intr_handle for %s\n", __func__);
221 * Tests for rte_intr_enable().
224 test_interrupt_enable(void)
226 struct rte_intr_handle test_intr_handle;
228 /* check with null intr_handle */
229 if (rte_intr_enable(NULL) == 0) {
230 printf("unexpectedly enable null intr_handle successfully\n");
234 /* check with invalid intr_handle */
235 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
236 if (rte_intr_enable(&test_intr_handle) == 0) {
237 printf("unexpectedly enable invalid intr_handle "
242 /* check with valid intr_handle */
243 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
244 if (rte_intr_enable(&test_intr_handle) == 0) {
245 printf("unexpectedly enable a specific intr_handle "
250 /* check with specific valid intr_handle */
251 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM];
252 if (rte_intr_enable(&test_intr_handle) == 0) {
253 printf("unexpectedly enable a specific intr_handle "
258 /* check with specific valid intr_handle */
259 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_DEV_EVENT];
260 if (rte_intr_enable(&test_intr_handle) == 0) {
261 printf("unexpectedly enable a specific intr_handle "
266 /* check with valid handler and its type */
267 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_CASE1];
268 if (rte_intr_enable(&test_intr_handle) < 0) {
269 printf("fail to enable interrupt on a simulated handler\n");
273 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO];
274 if (rte_intr_enable(&test_intr_handle) == 0) {
275 printf("unexpectedly enable a specific intr_handle "
284 * Tests for rte_intr_disable().
287 test_interrupt_disable(void)
289 struct rte_intr_handle test_intr_handle;
291 /* check with null intr_handle */
292 if (rte_intr_disable(NULL) == 0) {
293 printf("unexpectedly disable null intr_handle "
298 /* check with invalid intr_handle */
299 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
300 if (rte_intr_disable(&test_intr_handle) == 0) {
301 printf("unexpectedly disable invalid intr_handle "
306 /* check with valid intr_handle */
307 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
308 if (rte_intr_disable(&test_intr_handle) == 0) {
309 printf("unexpectedly disable a specific intr_handle "
314 /* check with specific valid intr_handle */
315 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM];
316 if (rte_intr_disable(&test_intr_handle) == 0) {
317 printf("unexpectedly disable a specific intr_handle "
322 /* check with specific valid intr_handle */
323 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_DEV_EVENT];
324 if (rte_intr_disable(&test_intr_handle) == 0) {
325 printf("unexpectedly disable a specific intr_handle "
330 /* check with valid handler and its type */
331 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_CASE1];
332 if (rte_intr_disable(&test_intr_handle) < 0) {
333 printf("fail to disable interrupt on a simulated handler\n");
337 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO];
338 if (rte_intr_disable(&test_intr_handle) == 0) {
339 printf("unexpectedly disable a specific intr_handle "
348 * Check the full path of a specified type of interrupt simulated.
351 test_interrupt_full_path_check(enum test_interrupt_handle_type intr_type)
354 struct rte_intr_handle test_intr_handle;
357 test_intr_handle = intr_handles[intr_type];
358 test_intr_type = intr_type;
359 if (rte_intr_callback_register(&test_intr_handle,
360 test_interrupt_callback, &test_intr_handle) < 0) {
361 printf("fail to register callback\n");
365 if (test_interrupt_trigger_interrupt() < 0)
369 for (count = 0; flag == 0 && count < 3; count++)
370 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
372 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
374 rte_intr_callback_unregister(&test_intr_handle,
375 test_interrupt_callback,
376 &test_intr_handle)) < 0) {
377 if (count != -EAGAIN)
382 printf("callback has not been called\n");
384 } else if (flag < 0) {
385 printf("it has internal error in callback\n");
393 * Main function of testing interrupt.
399 struct rte_intr_handle test_intr_handle;
401 if (test_interrupt_init() < 0) {
402 printf("fail to initialize for testing interrupt\n");
406 printf("Check unknown valid interrupt full path\n");
407 if (test_interrupt_full_path_check(TEST_INTERRUPT_HANDLE_VALID) < 0) {
408 printf("failure occurred during checking unknown valid "
409 "interrupt full path\n");
413 printf("Check valid UIO interrupt full path\n");
414 if (test_interrupt_full_path_check(TEST_INTERRUPT_HANDLE_VALID_UIO)
416 printf("failure occurred during checking valid UIO interrupt "
421 printf("Check valid device event interrupt full path\n");
422 if (test_interrupt_full_path_check(
423 TEST_INTERRUPT_HANDLE_VALID_DEV_EVENT) < 0) {
424 printf("failure occurred during checking valid device event "
425 "interrupt full path\n");
429 printf("Check valid alarm interrupt full path\n");
430 if (test_interrupt_full_path_check(
431 TEST_INTERRUPT_HANDLE_VALID_ALARM) < 0) {
432 printf("failure occurred during checking valid alarm "
433 "interrupt full path\n");
437 printf("start register/unregister test\n");
438 /* check if it will fail to register cb with intr_handle = NULL */
439 if (rte_intr_callback_register(NULL, test_interrupt_callback,
441 printf("unexpectedly register successfully with null "
446 /* check if it will fail to register cb with invalid intr_handle */
447 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
448 if (rte_intr_callback_register(&test_intr_handle,
449 test_interrupt_callback, &test_intr_handle) == 0) {
450 printf("unexpectedly register successfully with invalid "
455 /* check if it will fail to register without callback */
456 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
457 if (rte_intr_callback_register(&test_intr_handle, NULL, &test_intr_handle) == 0) {
458 printf("unexpectedly register successfully with "
463 /* check if it will fail to unregister cb with intr_handle = NULL */
464 if (rte_intr_callback_unregister(NULL,
465 test_interrupt_callback, NULL) > 0) {
466 printf("unexpectedly unregister successfully with "
467 "null intr_handle\n");
471 /* check if it will fail to unregister cb with invalid intr_handle */
472 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
473 if (rte_intr_callback_unregister(&test_intr_handle,
474 test_interrupt_callback, &test_intr_handle) > 0) {
475 printf("unexpectedly unregister successfully with "
476 "invalid intr_handle\n");
480 /* check if it is ok to register the same intr_handle twice */
481 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
482 if (rte_intr_callback_register(&test_intr_handle,
483 test_interrupt_callback, &test_intr_handle) < 0) {
484 printf("it fails to register test_interrupt_callback\n");
487 if (rte_intr_callback_register(&test_intr_handle,
488 test_interrupt_callback_1, &test_intr_handle) < 0) {
489 printf("it fails to register test_interrupt_callback_1\n");
492 /* check if it will fail to unregister with invalid parameter */
493 if (rte_intr_callback_unregister(&test_intr_handle,
494 test_interrupt_callback, (void *)0xff) != 0) {
495 printf("unexpectedly unregisters successfully with "
499 if (rte_intr_callback_unregister(&test_intr_handle,
500 test_interrupt_callback, &test_intr_handle) <= 0) {
501 printf("it fails to unregister test_interrupt_callback\n");
504 if (rte_intr_callback_unregister(&test_intr_handle,
505 test_interrupt_callback_1, (void *)-1) <= 0) {
506 printf("it fails to unregister test_interrupt_callback_1 "
510 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
512 printf("start interrupt enable/disable test\n");
513 /* check interrupt enable/disable functions */
514 if (test_interrupt_enable() < 0) {
515 printf("fail to check interrupt enabling\n");
518 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
520 if (test_interrupt_disable() < 0) {
521 printf("fail to check interrupt disabling\n");
524 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
529 printf("Clearing for interrupt tests\n");
530 /* clear registered callbacks */
531 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
532 rte_intr_callback_unregister(&test_intr_handle,
533 test_interrupt_callback, (void *)-1);
534 rte_intr_callback_unregister(&test_intr_handle,
535 test_interrupt_callback_1, (void *)-1);
537 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO];
538 rte_intr_callback_unregister(&test_intr_handle,
539 test_interrupt_callback, (void *)-1);
540 rte_intr_callback_unregister(&test_intr_handle,
541 test_interrupt_callback_1, (void *)-1);
543 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM];
544 rte_intr_callback_unregister(&test_intr_handle,
545 test_interrupt_callback, (void *)-1);
546 rte_intr_callback_unregister(&test_intr_handle,
547 test_interrupt_callback_1, (void *)-1);
549 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_DEV_EVENT];
550 rte_intr_callback_unregister(&test_intr_handle,
551 test_interrupt_callback, (void *)-1);
552 rte_intr_callback_unregister(&test_intr_handle,
553 test_interrupt_callback_1, (void *)-1);
555 rte_delay_ms(2 * TEST_INTERRUPT_CHECK_INTERVAL);
557 test_interrupt_deinit();
562 REGISTER_TEST_COMMAND(interrupt_autotest, test_interrupt);