4 * Copyright(c) 2010-2013 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 <cmdline_parse.h>
40 #include <rte_common.h>
41 #include <rte_cycles.h>
42 #include <rte_interrupts.h>
46 #define TEST_INTERRUPT_CHECK_INTERVAL 1000 /* ms */
48 enum test_interrupt_handl_type {
49 TEST_INTERRUPT_HANDLE_INVALID,
50 TEST_INTERRUPT_HANDLE_VALID,
51 TEST_INTERRUPT_HANDLE_CASE1,
52 TEST_INTERRUPT_HANDLE_MAX
55 static volatile int flag;
56 static struct rte_intr_handle intr_handles[TEST_INTERRUPT_HANDLE_MAX];
58 #ifdef RTE_EXEC_ENV_LINUXAPP
69 static union intr_pipefds pfds;
72 test_interrupt_handle_sanity_check(struct rte_intr_handle *intr_handle)
74 if (!intr_handle || intr_handle->fd < 0)
81 test_interrupt_init(void)
83 if (pipe(pfds.pipefd) < 0)
86 intr_handles[TEST_INTERRUPT_HANDLE_INVALID].fd = -1;
87 intr_handles[TEST_INTERRUPT_HANDLE_INVALID].type = RTE_INTR_HANDLE_UNKNOWN;
89 intr_handles[TEST_INTERRUPT_HANDLE_VALID].fd = pfds.readfd;
90 intr_handles[TEST_INTERRUPT_HANDLE_VALID].type = RTE_INTR_HANDLE_UNKNOWN;
92 intr_handles[TEST_INTERRUPT_HANDLE_CASE1].fd = pfds.readfd;
93 intr_handles[TEST_INTERRUPT_HANDLE_CASE1].type = RTE_INTR_HANDLE_ALARM;
99 test_interrupt_deinit(void)
101 close(pfds.pipefd[0]);
102 close(pfds.pipefd[1]);
108 test_interrupt_trigger_interrupt(void)
110 if (write(pfds.writefd, "1", 1) < 0)
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 baremetal 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 test_interrupt_callback(struct rte_intr_handle *intr_handle, void *arg)
172 if (test_interrupt_handle_sanity_check(intr_handle) < 0) {
173 printf("null or invalid intr_handle for %s\n", __func__);
178 if (rte_intr_callback_unregister(intr_handle,
179 test_interrupt_callback, arg) >= 0) {
180 printf("%s: unexpectedly able to unregister itself\n",
186 if (test_interrupt_handle_compare(intr_handle,
187 &(intr_handles[TEST_INTERRUPT_HANDLE_VALID])) == 0) {
193 test_interrupt_callback_1(struct rte_intr_handle *intr_handle,
194 __attribute__((unused)) void *arg)
196 if (test_interrupt_handle_sanity_check(intr_handle) < 0) {
197 printf("null or invalid intr_handle for %s\n", __func__);
204 test_interrupt_enable(void)
206 struct rte_intr_handle test_intr_handle;
208 /* check with null intr_handle */
209 if (rte_intr_enable(NULL) == 0) {
210 printf("unexpectedly enable null intr_handle successfully\n");
214 /* check with invalid intr_handle */
215 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
216 if (rte_intr_enable(&test_intr_handle) == 0) {
217 printf("unexpectedly enable invalid intr_handle "
222 /* check with valid intr_handle */
223 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
224 if (rte_intr_enable(&test_intr_handle) == 0) {
225 printf("unexpectedly enable a specific intr_handle "
230 /* check with specific valid intr_handle */
231 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_CASE1];
232 if (rte_intr_enable(&test_intr_handle) == 0) {
233 printf("unexpectedly enable a specific intr_handle "
242 test_interrupt_disable(void)
244 struct rte_intr_handle test_intr_handle;
246 /* check with null intr_handle */
247 if (rte_intr_disable(NULL) == 0) {
248 printf("unexpectedly disable null intr_handle "
253 /* check with invalid intr_handle */
254 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
255 if (rte_intr_disable(&test_intr_handle) == 0) {
256 printf("unexpectedly disable invalid intr_handle "
261 /* check with valid intr_handle */
262 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
263 if (rte_intr_disable(&test_intr_handle) == 0) {
264 printf("unexpectedly disable a specific intr_handle "
269 /* check with specific valid intr_handle */
270 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_CASE1];
271 if (rte_intr_disable(&test_intr_handle) == 0) {
272 printf("unexpectedly disable a specific intr_handle "
284 struct rte_intr_handle test_intr_handle;
286 if (test_interrupt_init() < 0) {
287 printf("fail to do test init\n");
291 printf("check if callback registered can be called\n");
295 /* check if callback registered can be called */
297 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
298 if (rte_intr_callback_register(&test_intr_handle,
299 test_interrupt_callback, NULL) < 0) {
300 printf("fail to register callback\n");
303 /* trigger an interrupt and then check if the callback can be called */
304 if (test_interrupt_trigger_interrupt() < 0) {
305 printf("fail to trigger an interrupt\n");
308 /* check flag in 3 seconds */
309 for (count = 0; flag == 0 && count < 3; count++)
310 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
312 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
314 if ((ret = rte_intr_callback_unregister(&test_intr_handle,
315 test_interrupt_callback, NULL)) < 0) {
316 printf("rte_intr_callback_unregister() failed with error "
324 printf("registered callback has not been called\n");
326 } else if (flag < 0) {
327 printf("registered callback failed\n");
332 printf("start register/unregister test\n");
334 /* check if it will fail to register cb with intr_handle = NULL */
335 if (rte_intr_callback_register(NULL, test_interrupt_callback,
337 printf("unexpectedly register successfully with null "
342 /* check if it will fail to register cb with invalid intr_handle */
343 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
344 if (rte_intr_callback_register(&test_intr_handle,
345 test_interrupt_callback, NULL) == 0) {
346 printf("unexpectedly register successfully with invalid "
351 /* check if it will fail to register without callback */
352 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
353 if (rte_intr_callback_register(&test_intr_handle, NULL, NULL) == 0) {
354 printf("unexpectedly register successfully with "
359 /* check if it will fail to unregister cb with intr_handle = NULL */
360 if (rte_intr_callback_unregister(NULL,
361 test_interrupt_callback, NULL) > 0) {
362 printf("unexpectedly unregister successfully with "
363 "null intr_handle\n");
367 /* check if it will fail to unregister cb with invalid intr_handle */
368 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_INVALID];
369 if (rte_intr_callback_unregister(&test_intr_handle,
370 test_interrupt_callback, NULL) > 0) {
371 printf("unexpectedly unregister successfully with "
372 "invalid intr_handle\n");
376 /* check if it is ok to register the same intr_handle twice */
377 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
378 if (rte_intr_callback_register(&test_intr_handle,
379 test_interrupt_callback, NULL) < 0) {
380 printf("it fails to register test_interrupt_callback\n");
383 if (rte_intr_callback_register(&test_intr_handle,
384 test_interrupt_callback_1, NULL) < 0) {
385 printf("it fails to register test_interrupt_callback_1\n");
388 /* check if it will fail to unregister with invalid parameter */
389 if (rte_intr_callback_unregister(&test_intr_handle,
390 test_interrupt_callback, (void *)0xff) != 0) {
391 printf("unexpectedly unregisters successfully with invalid arg\n");
394 if (rte_intr_callback_unregister(&test_intr_handle,
395 test_interrupt_callback, NULL) <= 0) {
396 printf("it fails to unregister test_interrupt_callback\n");
399 if (rte_intr_callback_unregister(&test_intr_handle,
400 test_interrupt_callback_1, (void *)-1) <= 0) {
401 printf("it fails to unregister test_interrupt_callback_1 "
405 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
407 printf("start interrupt enable/disable test\n");
409 /* check interrupt enable/disable functions */
410 if (test_interrupt_enable() < 0)
412 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
414 if (test_interrupt_disable() < 0)
416 rte_delay_ms(TEST_INTERRUPT_CHECK_INTERVAL);
421 /* clear registered callbacks */
422 test_intr_handle = intr_handles[TEST_INTERRUPT_HANDLE_VALID];
423 rte_intr_callback_unregister(&test_intr_handle,
424 test_interrupt_callback, (void *)-1);
425 rte_intr_callback_unregister(&test_intr_handle,
426 test_interrupt_callback_1, (void *)-1);
428 rte_delay_ms(2 * TEST_INTERRUPT_CHECK_INTERVAL);
430 test_interrupt_deinit();