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_LINUXAPP
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_LINUXAPP */
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);
373 if (rte_intr_callback_unregister(&test_intr_handle,
374 test_interrupt_callback, &test_intr_handle) < 0)
378 printf("callback has not been called\n");
380 } else if (flag < 0) {
381 printf("it has internal error in callback\n");
389 * Main function of testing interrupt.
395 struct rte_intr_handle test_intr_handle;
397 if (test_interrupt_init() < 0) {
398 printf("fail to initialize for testing interrupt\n");
402 printf("Check unknown valid interrupt full path\n");
403 if (test_interrupt_full_path_check(TEST_INTERRUPT_HANDLE_VALID) < 0) {
404 printf("failure occurred during checking unknown valid "
405 "interrupt full path\n");
409 printf("Check valid UIO interrupt full path\n");
410 if (test_interrupt_full_path_check(TEST_INTERRUPT_HANDLE_VALID_UIO)
412 printf("failure occurred during checking valid UIO interrupt "
417 printf("Check valid device event interrupt full path\n");
418 if (test_interrupt_full_path_check(
419 TEST_INTERRUPT_HANDLE_VALID_DEV_EVENT) < 0) {
420 printf("failure occurred during checking valid device event "
421 "interrupt full path\n");
425 printf("Check valid alarm interrupt full path\n");
426 if (test_interrupt_full_path_check(
427 TEST_INTERRUPT_HANDLE_VALID_ALARM) < 0) {
428 printf("failure occurred during checking valid alarm "
429 "interrupt full path\n");
433 printf("start register/unregister test\n");
434 /* check if it will fail to register cb with intr_handle = NULL */
435 if (rte_intr_callback_register(NULL, test_interrupt_callback,
437 printf("unexpectedly register successfully with null "
442 /* check if it will fail to register cb with invalid intr_handle */
443 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
444 if (rte_intr_callback_register(&test_intr_handle,
445 test_interrupt_callback, &test_intr_handle) == 0) {
446 printf("unexpectedly register successfully with invalid "
451 /* check if it will fail to register without callback */
452 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
453 if (rte_intr_callback_register(&test_intr_handle, NULL, &test_intr_handle) == 0) {
454 printf("unexpectedly register successfully with "
459 /* check if it will fail to unregister cb with intr_handle = NULL */
460 if (rte_intr_callback_unregister(NULL,
461 test_interrupt_callback, NULL) > 0) {
462 printf("unexpectedly unregister successfully with "
463 "null intr_handle\n");
467 /* check if it will fail to unregister cb with invalid intr_handle */
468 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
469 if (rte_intr_callback_unregister(&test_intr_handle,
470 test_interrupt_callback, &test_intr_handle) > 0) {
471 printf("unexpectedly unregister successfully with "
472 "invalid intr_handle\n");
476 /* check if it is ok to register the same intr_handle twice */
477 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
478 if (rte_intr_callback_register(&test_intr_handle,
479 test_interrupt_callback, &test_intr_handle) < 0) {
480 printf("it fails to register test_interrupt_callback\n");
483 if (rte_intr_callback_register(&test_intr_handle,
484 test_interrupt_callback_1, &test_intr_handle) < 0) {
485 printf("it fails to register test_interrupt_callback_1\n");
488 /* check if it will fail to unregister with invalid parameter */
489 if (rte_intr_callback_unregister(&test_intr_handle,
490 test_interrupt_callback, (void *)0xff) != 0) {
491 printf("unexpectedly unregisters successfully with "
495 if (rte_intr_callback_unregister(&test_intr_handle,
496 test_interrupt_callback, &test_intr_handle) <= 0) {
497 printf("it fails to unregister test_interrupt_callback\n");
500 if (rte_intr_callback_unregister(&test_intr_handle,
501 test_interrupt_callback_1, (void *)-1) <= 0) {
502 printf("it fails to unregister test_interrupt_callback_1 "
506 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
508 printf("start interrupt enable/disable test\n");
509 /* check interrupt enable/disable functions */
510 if (test_interrupt_enable() < 0) {
511 printf("fail to check interrupt enabling\n");
514 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
516 if (test_interrupt_disable() < 0) {
517 printf("fail to check interrupt disabling\n");
520 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
525 printf("Clearing for interrupt tests\n");
526 /* clear registered callbacks */
527 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
528 rte_intr_callback_unregister(&test_intr_handle,
529 test_interrupt_callback, (void *)-1);
530 rte_intr_callback_unregister(&test_intr_handle,
531 test_interrupt_callback_1, (void *)-1);
533 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO];
534 rte_intr_callback_unregister(&test_intr_handle,
535 test_interrupt_callback, (void *)-1);
536 rte_intr_callback_unregister(&test_intr_handle,
537 test_interrupt_callback_1, (void *)-1);
539 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM];
540 rte_intr_callback_unregister(&test_intr_handle,
541 test_interrupt_callback, (void *)-1);
542 rte_intr_callback_unregister(&test_intr_handle,
543 test_interrupt_callback_1, (void *)-1);
545 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_DEV_EVENT];
546 rte_intr_callback_unregister(&test_intr_handle,
547 test_interrupt_callback, (void *)-1);
548 rte_intr_callback_unregister(&test_intr_handle,
549 test_interrupt_callback_1, (void *)-1);
551 rte_delay_ms(2 * TEST_INTERRUPT_CHECK_INTERVAL);
553 test_interrupt_deinit();
558 REGISTER_TEST_COMMAND(interrupt_autotest, test_interrupt);