4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <rte_common.h>
39 #include <rte_cycles.h>
40 #include <rte_interrupts.h>
44 #define TEST_INTERRUPT_CHECK_INTERVAL 1000 /* ms */
46 /* predefined interrupt handle types */
47 enum test_interrupt_handle_type {
48 TEST_INTERRUPT_HANDLE_INVALID,
49 TEST_INTERRUPT_HANDLE_VALID,
50 TEST_INTERRUPT_HANDLE_VALID_UIO,
51 TEST_INTERRUPT_HANDLE_VALID_ALARM,
52 TEST_INTERRUPT_HANDLE_CASE1,
53 TEST_INTERRUPT_HANDLE_MAX
56 /* flag of if callback is called */
57 static volatile int flag;
58 static struct rte_intr_handle intr_handles[TEST_INTERRUPT_HANDLE_MAX];
59 static enum test_interrupt_handle_type test_intr_type =
60 TEST_INTERRUPT_HANDLE_MAX;
62 #ifdef RTE_EXEC_ENV_LINUXAPP
73 static union intr_pipefds pfds;
76 * Check if the interrupt handle is valid.
79 test_interrupt_handle_sanity_check(struct rte_intr_handle *intr_handle)
81 if (!intr_handle || intr_handle->fd < 0)
88 * Initialization for interrupt test.
91 test_interrupt_init(void)
93 if (pipe(pfds.pipefd) < 0)
96 intr_handles[TEST_INTERRUPT_HANDLE_INVALID].fd = -1;
97 intr_handles[TEST_INTERRUPT_HANDLE_INVALID].type =
98 RTE_INTR_HANDLE_UNKNOWN;
100 intr_handles[TEST_INTERRUPT_HANDLE_VALID].fd = pfds.readfd;
101 intr_handles[TEST_INTERRUPT_HANDLE_VALID].type =
102 RTE_INTR_HANDLE_UNKNOWN;
104 intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO].fd = pfds.readfd;
105 intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO].type =
108 intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM].fd = pfds.readfd;
109 intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM].type =
110 RTE_INTR_HANDLE_ALARM;
112 intr_handles[TEST_INTERRUPT_HANDLE_CASE1].fd = pfds.writefd;
113 intr_handles[TEST_INTERRUPT_HANDLE_CASE1].type = RTE_INTR_HANDLE_UIO;
119 * Deinitialization for interrupt test.
122 test_interrupt_deinit(void)
124 close(pfds.pipefd[0]);
125 close(pfds.pipefd[1]);
131 * Write the pipe to simulate an interrupt.
134 test_interrupt_trigger_interrupt(void)
136 if (write(pfds.writefd, "1", 1) < 0)
143 * Check if two interrupt handles are the same.
146 test_interrupt_handle_compare(struct rte_intr_handle *intr_handle_l,
147 struct rte_intr_handle *intr_handle_r)
149 if (!intr_handle_l || !intr_handle_r)
152 if (intr_handle_l->fd != intr_handle_r->fd ||
153 intr_handle_l->type != intr_handle_r->type)
160 /* to be implemented for bsd later */
162 test_interrupt_handle_sanity_check(struct rte_intr_handle *intr_handle)
164 RTE_SET_USED(intr_handle);
170 test_interrupt_init(void)
176 test_interrupt_deinit(void)
182 test_interrupt_trigger_interrupt(void)
188 test_interrupt_handle_compare(struct rte_intr_handle *intr_handle_l,
189 struct rte_intr_handle *intr_handle_r)
196 #endif /* RTE_EXEC_ENV_LINUXAPP */
199 * Callback for the test interrupt.
202 test_interrupt_callback(struct rte_intr_handle *intr_handle, void *arg)
204 if (test_intr_type >= TEST_INTERRUPT_HANDLE_MAX) {
205 printf("invalid interrupt type\n");
210 if (test_interrupt_handle_sanity_check(intr_handle) < 0) {
211 printf("null or invalid intr_handle for %s\n", __func__);
216 if (rte_intr_callback_unregister(intr_handle,
217 test_interrupt_callback, arg) >= 0) {
218 printf("%s: unexpectedly able to unregister itself\n",
224 if (test_interrupt_handle_compare(intr_handle,
225 &(intr_handles[test_intr_type])) == 0)
230 * Callback for the test interrupt.
233 test_interrupt_callback_1(struct rte_intr_handle *intr_handle,
234 __attribute__((unused)) void *arg)
236 if (test_interrupt_handle_sanity_check(intr_handle) < 0) {
237 printf("null or invalid intr_handle for %s\n", __func__);
244 * Tests for rte_intr_enable().
247 test_interrupt_enable(void)
249 struct rte_intr_handle test_intr_handle;
251 /* check with null intr_handle */
252 if (rte_intr_enable(NULL) == 0) {
253 printf("unexpectedly enable null intr_handle successfully\n");
257 /* check with invalid intr_handle */
258 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
259 if (rte_intr_enable(&test_intr_handle) == 0) {
260 printf("unexpectedly enable invalid intr_handle "
265 /* check with valid intr_handle */
266 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
267 if (rte_intr_enable(&test_intr_handle) == 0) {
268 printf("unexpectedly enable a specific intr_handle "
273 /* check with specific valid intr_handle */
274 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM];
275 if (rte_intr_enable(&test_intr_handle) == 0) {
276 printf("unexpectedly enable a specific intr_handle "
281 /* check with valid handler and its type */
282 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_CASE1];
283 if (rte_intr_enable(&test_intr_handle) < 0) {
284 printf("fail to enable interrupt on a simulated handler\n");
288 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO];
289 if (rte_intr_enable(&test_intr_handle) == 0) {
290 printf("unexpectedly enable a specific intr_handle "
299 * Tests for rte_intr_disable().
302 test_interrupt_disable(void)
304 struct rte_intr_handle test_intr_handle;
306 /* check with null intr_handle */
307 if (rte_intr_disable(NULL) == 0) {
308 printf("unexpectedly disable null intr_handle "
313 /* check with invalid intr_handle */
314 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
315 if (rte_intr_disable(&test_intr_handle) == 0) {
316 printf("unexpectedly disable invalid intr_handle "
321 /* check with valid intr_handle */
322 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
323 if (rte_intr_disable(&test_intr_handle) == 0) {
324 printf("unexpectedly disable a specific intr_handle "
329 /* check with specific valid intr_handle */
330 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM];
331 if (rte_intr_disable(&test_intr_handle) == 0) {
332 printf("unexpectedly disable a specific intr_handle "
337 /* check with valid handler and its type */
338 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_CASE1];
339 if (rte_intr_disable(&test_intr_handle) < 0) {
340 printf("fail to disable interrupt on a simulated handler\n");
344 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO];
345 if (rte_intr_disable(&test_intr_handle) == 0) {
346 printf("unexpectedly disable a specific intr_handle "
355 * Check the full path of a specified type of interrupt simulated.
358 test_interrupt_full_path_check(enum test_interrupt_handle_type intr_type)
361 struct rte_intr_handle test_intr_handle;
364 test_intr_handle = intr_handles[intr_type];
365 test_intr_type = intr_type;
366 if (rte_intr_callback_register(&test_intr_handle,
367 test_interrupt_callback, NULL) < 0) {
368 printf("fail to register callback\n");
372 if (test_interrupt_trigger_interrupt() < 0)
375 /* check flag in 3 seconds */
376 for (count = 0; flag == 0 && count < 3; count++)
377 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
379 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
380 if (rte_intr_callback_unregister(&test_intr_handle,
381 test_interrupt_callback, NULL) < 0)
385 printf("callback has not been called\n");
387 } else if (flag < 0) {
388 printf("it has internal error in callback\n");
396 * Main function of testing interrupt.
402 struct rte_intr_handle test_intr_handle;
404 if (test_interrupt_init() < 0) {
405 printf("fail to initialize for testing interrupt\n");
409 printf("Check unknown valid interrupt full path\n");
410 if (test_interrupt_full_path_check(TEST_INTERRUPT_HANDLE_VALID) < 0) {
411 printf("failure occured during checking unknown valid "
412 "interrupt full path\n");
416 printf("Check valid UIO interrupt full path\n");
417 if (test_interrupt_full_path_check(TEST_INTERRUPT_HANDLE_VALID_UIO)
419 printf("failure occured during checking valid UIO interrupt "
424 printf("Check valid alarm interrupt full path\n");
425 if (test_interrupt_full_path_check(TEST_INTERRUPT_HANDLE_VALID_ALARM)
427 printf("failure occured during checking valid alarm "
428 "interrupt full path\n");
432 printf("start register/unregister test\n");
433 /* check if it will fail to register cb with intr_handle = NULL */
434 if (rte_intr_callback_register(NULL, test_interrupt_callback,
436 printf("unexpectedly register successfully with null "
441 /* check if it will fail to register cb with invalid intr_handle */
442 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
443 if (rte_intr_callback_register(&test_intr_handle,
444 test_interrupt_callback, NULL) == 0) {
445 printf("unexpectedly register successfully with invalid "
450 /* check if it will fail to register without callback */
451 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
452 if (rte_intr_callback_register(&test_intr_handle, NULL, NULL) == 0) {
453 printf("unexpectedly register successfully with "
458 /* check if it will fail to unregister cb with intr_handle = NULL */
459 if (rte_intr_callback_unregister(NULL,
460 test_interrupt_callback, NULL) > 0) {
461 printf("unexpectedly unregister successfully with "
462 "null intr_handle\n");
466 /* check if it will fail to unregister cb with invalid intr_handle */
467 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
468 if (rte_intr_callback_unregister(&test_intr_handle,
469 test_interrupt_callback, NULL) > 0) {
470 printf("unexpectedly unregister successfully with "
471 "invalid intr_handle\n");
475 /* check if it is ok to register the same intr_handle twice */
476 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
477 if (rte_intr_callback_register(&test_intr_handle,
478 test_interrupt_callback, NULL) < 0) {
479 printf("it fails to register test_interrupt_callback\n");
482 if (rte_intr_callback_register(&test_intr_handle,
483 test_interrupt_callback_1, NULL) < 0) {
484 printf("it fails to register test_interrupt_callback_1\n");
487 /* check if it will fail to unregister with invalid parameter */
488 if (rte_intr_callback_unregister(&test_intr_handle,
489 test_interrupt_callback, (void *)0xff) != 0) {
490 printf("unexpectedly unregisters successfully with "
494 if (rte_intr_callback_unregister(&test_intr_handle,
495 test_interrupt_callback, NULL) <= 0) {
496 printf("it fails to unregister test_interrupt_callback\n");
499 if (rte_intr_callback_unregister(&test_intr_handle,
500 test_interrupt_callback_1, (void *)-1) <= 0) {
501 printf("it fails to unregister test_interrupt_callback_1 "
505 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
507 printf("start interrupt enable/disable test\n");
508 /* check interrupt enable/disable functions */
509 if (test_interrupt_enable() < 0) {
510 printf("fail to check interrupt enabling\n");
513 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
515 if (test_interrupt_disable() < 0) {
516 printf("fail to check interrupt disabling\n");
519 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
524 printf("Clearing for interrupt tests\n");
525 /* clear registered callbacks */
526 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
527 rte_intr_callback_unregister(&test_intr_handle,
528 test_interrupt_callback, (void *)-1);
529 rte_intr_callback_unregister(&test_intr_handle,
530 test_interrupt_callback_1, (void *)-1);
532 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_UIO];
533 rte_intr_callback_unregister(&test_intr_handle,
534 test_interrupt_callback, (void *)-1);
535 rte_intr_callback_unregister(&test_intr_handle,
536 test_interrupt_callback_1, (void *)-1);
538 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID_ALARM];
539 rte_intr_callback_unregister(&test_intr_handle,
540 test_interrupt_callback, (void *)-1);
541 rte_intr_callback_unregister(&test_intr_handle,
542 test_interrupt_callback_1, (void *)-1);
544 rte_delay_ms(2 * TEST_INTERRUPT_CHECK_INTERVAL);
546 test_interrupt_deinit();
551 static struct test_command interrupt_cmd = {
552 .command = "interrupt_autotest",
553 .callback = test_interrupt,
555 REGISTER_TEST_COMMAND(interrupt_cmd);