1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_common.h>
9 #include <rte_cycles.h>
10 #include <rte_interrupts.h>
11 #include <rte_atomic.h>
12 #include <rte_alarm.h>
16 #define US_PER_MS 1000
18 #define RTE_TEST_ALARM_TIMEOUT 10 /* ms */
19 #define RTE_TEST_CHECK_PERIOD 3 /* ms */
20 #define RTE_TEST_MAX_REPEAT 20
22 static volatile int flag;
25 test_alarm_callback(void *cb_arg)
28 printf("Callback setting flag - OK. [cb_arg = %p]\n", cb_arg);
31 static rte_atomic32_t cb_count;
34 test_multi_cb(void *arg)
36 rte_atomic32_inc(&cb_count);
37 printf("In %s - arg = %p\n", __func__, arg);
40 static volatile int recursive_error = 0;
43 test_remove_in_callback(void *arg)
45 printf("In %s - arg = %p\n", __func__, arg);
46 if (rte_eal_alarm_cancel(test_remove_in_callback, arg) ||
47 rte_eal_alarm_cancel(test_remove_in_callback, (void *)-1)) {
48 printf("Error - cancelling callback from within function succeeded!\n");
51 flag = (int)((uintptr_t)arg);
54 static volatile int flag_2;
57 test_remove_in_callback_2(void *arg)
59 if (rte_eal_alarm_cancel(test_remove_in_callback_2, arg) || rte_eal_alarm_cancel(test_remove_in_callback_2, (void *)-1)) {
60 printf("Error - cancelling callback of test_remove_in_callback_2\n");
67 test_multi_alarms(void)
73 printf("Expect 6 callbacks in order...\n");
74 /* add two alarms in order */
75 rte_eal_alarm_set(10 * US_PER_MS, test_multi_cb, (void *)1);
76 rte_eal_alarm_set(20 * US_PER_MS, test_multi_cb, (void *)2);
78 /* now add in reverse order */
79 rte_eal_alarm_set(60 * US_PER_MS, test_multi_cb, (void *)6);
80 rte_eal_alarm_set(50 * US_PER_MS, test_multi_cb, (void *)5);
81 rte_eal_alarm_set(40 * US_PER_MS, test_multi_cb, (void *)4);
82 rte_eal_alarm_set(30 * US_PER_MS, test_multi_cb, (void *)3);
86 if (cb_count.cnt != 6) {
87 printf("Missing callbacks\n");
88 /* remove any callbacks that might remain */
89 rte_eal_alarm_cancel(test_multi_cb, (void *)-1);
94 printf("Expect only callbacks with args 1 and 3...\n");
95 /* Add 3 flags, then delete one */
96 rte_eal_alarm_set(30 * US_PER_MS, test_multi_cb, (void *)3);
97 rte_eal_alarm_set(20 * US_PER_MS, test_multi_cb, (void *)2);
98 rte_eal_alarm_set(10 * US_PER_MS, test_multi_cb, (void *)1);
99 rm_count = rte_eal_alarm_cancel(test_multi_cb, (void *)2);
102 if (cb_count.cnt != 2 || rm_count != 1) {
103 printf("Error: invalid flags count or alarm removal failure"
104 " - flags value = %d, expected = %d\n",
105 (int)cb_count.cnt, 2);
106 /* remove any callbacks that might remain */
107 rte_eal_alarm_cancel(test_multi_cb, (void *)-1);
111 printf("Testing adding and then removing multiple alarms\n");
112 /* finally test that no callbacks are called if we delete them all*/
113 rte_eal_alarm_set(10 * US_PER_MS, test_multi_cb, (void *)1);
114 rte_eal_alarm_set(10 * US_PER_MS, test_multi_cb, (void *)2);
115 rte_eal_alarm_set(10 * US_PER_MS, test_multi_cb, (void *)3);
116 rm_count = rte_eal_alarm_cancel(test_alarm_callback, (void *)-1);
118 printf("Error removing non-existant alarm succeeded\n");
119 rte_eal_alarm_cancel(test_multi_cb, (void *) -1);
122 rm_count = rte_eal_alarm_cancel(test_multi_cb, (void *) -1);
124 printf("Error removing all pending alarm callbacks\n");
128 /* Test that we cannot cancel an alarm from within the callback itself
129 * Also test that we can cancel head-of-line callbacks ok.*/
132 rte_eal_alarm_set(10 * US_PER_MS, test_remove_in_callback, (void *)1);
133 rte_eal_alarm_set(20 * US_PER_MS, test_remove_in_callback, (void *)2);
134 rm_count = rte_eal_alarm_cancel(test_remove_in_callback, (void *)1);
136 printf("Error cancelling head-of-list callback\n");
141 printf("Error, cancelling head-of-list leads to premature callback\n");
145 while (flag != 2 && count++ < RTE_TEST_MAX_REPEAT)
149 printf("Error - expected callback not called\n");
150 rte_eal_alarm_cancel(test_remove_in_callback, (void *)-1);
153 if (recursive_error == 1)
156 /* Check if it can cancel all for the same callback */
157 printf("Testing canceling all for the same callback\n");
159 rte_eal_alarm_set(10 * US_PER_MS, test_remove_in_callback, (void *)1);
160 rte_eal_alarm_set(20 * US_PER_MS, test_remove_in_callback_2, (void *)2);
161 rte_eal_alarm_set(30 * US_PER_MS, test_remove_in_callback_2, (void *)3);
162 rte_eal_alarm_set(40 * US_PER_MS, test_remove_in_callback, (void *)4);
163 rm_count = rte_eal_alarm_cancel(test_remove_in_callback_2, (void *)-1);
165 printf("Error, cannot cancel all for the same callback\n");
168 rm_count = rte_eal_alarm_cancel(test_remove_in_callback, (void *)-1);
170 printf("Error, cannot cancel all for the same callback\n");
181 #ifdef RTE_EXEC_ENV_FREEBSD
182 printf("The alarm API is not supported on FreeBSD\n");
185 /* check if the callback will be called */
186 printf("check if the callback will be called\n");
188 if (rte_eal_alarm_set(RTE_TEST_ALARM_TIMEOUT * US_PER_MS,
189 test_alarm_callback, NULL) < 0) {
190 printf("fail to set alarm callback\n");
193 while (flag == 0 && count++ < RTE_TEST_MAX_REPEAT)
194 rte_delay_ms(RTE_TEST_CHECK_PERIOD);
197 printf("Callback not called\n");
201 /* check if it will fail to set alarm with wrong us value */
202 printf("check if it will fail to set alarm with wrong ms values\n");
203 if (rte_eal_alarm_set(0, test_alarm_callback,
205 printf("should not be successful with 0 us value\n");
208 if (rte_eal_alarm_set(UINT64_MAX - 1, test_alarm_callback,
210 printf("should not be successful with (UINT64_MAX-1) us value\n");
214 /* check if it will fail to set alarm with null callback parameter */
215 printf("check if it will fail to set alarm with null callback parameter\n");
216 if (rte_eal_alarm_set(RTE_TEST_ALARM_TIMEOUT, NULL, NULL) >= 0) {
217 printf("should not be successful to set alarm with null callback parameter\n");
221 /* check if it will fail to remove alarm with null callback parameter */
222 printf("check if it will fail to remove alarm with null callback parameter\n");
223 if (rte_eal_alarm_cancel(NULL, NULL) == 0) {
224 printf("should not be successful to remove alarm with null callback parameter");
228 if (test_multi_alarms() != 0)
234 REGISTER_TEST_COMMAND(alarm_autotest, test_alarm);