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.
37 #include <rte_common.h>
38 #include <rte_cycles.h>
39 #include <rte_interrupts.h>
40 #include <rte_common.h>
41 #include <rte_atomic.h>
42 #include <rte_alarm.h>
46 #define US_PER_MS 1000
48 #define RTE_TEST_ALARM_TIMEOUT 3000 /* ms */
49 #define RTE_TEST_CHECK_PERIOD 1000 /* ms */
51 static volatile int flag;
54 test_alarm_callback(void *cb_arg)
57 printf("Callback setting flag - OK. [cb_arg = %p]\n", cb_arg);
60 static rte_atomic32_t cb_count;
63 test_multi_cb(void *arg)
65 rte_atomic32_inc(&cb_count);
66 printf("In %s - arg = %p\n", __func__, arg);
69 static volatile int recursive_error = 0;
72 test_remove_in_callback(void *arg)
74 printf("In %s - arg = %p\n", __func__, arg);
75 if (rte_eal_alarm_cancel(test_remove_in_callback, arg) ||
76 rte_eal_alarm_cancel(test_remove_in_callback, (void *)-1)) {
77 printf("Error - cancelling callback from within function succeeded!\n");
80 flag = (int)((uintptr_t)arg);
83 static volatile int flag_2;
86 test_remove_in_callback_2(void *arg)
88 if (rte_eal_alarm_cancel(test_remove_in_callback_2, arg) || rte_eal_alarm_cancel(test_remove_in_callback_2, (void *)-1)) {
89 printf("Error - cancelling callback of test_remove_in_callback_2\n");
96 test_multi_alarms(void)
101 printf("Expect 6 callbacks in order...\n");
102 /* add two alarms in order */
103 rte_eal_alarm_set(1000 * US_PER_MS, test_multi_cb, (void *)1);
104 rte_eal_alarm_set(2000 * US_PER_MS, test_multi_cb, (void *)2);
106 /* now add in reverse order */
107 rte_eal_alarm_set(6000 * US_PER_MS, test_multi_cb, (void *)6);
108 rte_eal_alarm_set(5000 * US_PER_MS, test_multi_cb, (void *)5);
109 rte_eal_alarm_set(4000 * US_PER_MS, test_multi_cb, (void *)4);
110 rte_eal_alarm_set(3000 * US_PER_MS, test_multi_cb, (void *)3);
112 /* wait for expiry */
114 if (cb_count.cnt != 6) {
115 printf("Missing callbacks\n");
116 /* remove any callbacks that might remain */
117 rte_eal_alarm_cancel(test_multi_cb, (void *)-1);
122 printf("Expect only callbacks with args 1 and 3...\n");
123 /* Add 3 flags, then delete one */
124 rte_eal_alarm_set(3000 * US_PER_MS, test_multi_cb, (void *)3);
125 rte_eal_alarm_set(2000 * US_PER_MS, test_multi_cb, (void *)2);
126 rte_eal_alarm_set(1000 * US_PER_MS, test_multi_cb, (void *)1);
127 rm_count = rte_eal_alarm_cancel(test_multi_cb, (void *)2);
130 if (cb_count.cnt != 2 || rm_count != 1) {
131 printf("Error: invalid flags count or alarm removal failure"
132 " - flags value = %d, expected = %d\n",
133 (int)cb_count.cnt, 2);
134 /* remove any callbacks that might remain */
135 rte_eal_alarm_cancel(test_multi_cb, (void *)-1);
139 printf("Testing adding and then removing multiple alarms\n");
140 /* finally test that no callbacks are called if we delete them all*/
141 rte_eal_alarm_set(1000 * US_PER_MS, test_multi_cb, (void *)1);
142 rte_eal_alarm_set(1000 * US_PER_MS, test_multi_cb, (void *)2);
143 rte_eal_alarm_set(1000 * US_PER_MS, test_multi_cb, (void *)3);
144 rm_count = rte_eal_alarm_cancel(test_alarm_callback, (void *)-1);
146 printf("Error removing non-existant alarm succeeded\n");
147 rte_eal_alarm_cancel(test_multi_cb, (void *) -1);
150 rm_count = rte_eal_alarm_cancel(test_multi_cb, (void *) -1);
152 printf("Error removing all pending alarm callbacks\n");
156 /* Test that we cannot cancel an alarm from within the callback itself
157 * Also test that we can cancel head-of-line callbacks ok.*/
160 rte_eal_alarm_set(1000 * US_PER_MS, test_remove_in_callback, (void *)1);
161 rte_eal_alarm_set(2000 * US_PER_MS, test_remove_in_callback, (void *)2);
162 rm_count = rte_eal_alarm_cancel(test_remove_in_callback, (void *)1);
164 printf("Error cancelling head-of-list callback\n");
169 printf("Error, cancelling head-of-list leads to premature callback\n");
174 printf("Error - expected callback not called\n");
175 rte_eal_alarm_cancel(test_remove_in_callback, (void *)-1);
178 if (recursive_error == 1)
181 /* Check if it can cancel all for the same callback */
182 printf("Testing canceling all for the same callback\n");
184 rte_eal_alarm_set(1000 * US_PER_MS, test_remove_in_callback, (void *)1);
185 rte_eal_alarm_set(2000 * US_PER_MS, test_remove_in_callback_2, (void *)2);
186 rte_eal_alarm_set(3000 * US_PER_MS, test_remove_in_callback_2, (void *)3);
187 rte_eal_alarm_set(4000 * US_PER_MS, test_remove_in_callback, (void *)4);
188 rm_count = rte_eal_alarm_cancel(test_remove_in_callback_2, (void *)-1);
190 printf("Error, cannot cancel all for the same callback\n");
193 rm_count = rte_eal_alarm_cancel(test_remove_in_callback, (void *)-1);
195 printf("Error, cannot cancel all for the same callback\n");
207 /* check if the callback will be called */
208 printf("check if the callback will be called\n");
210 if (rte_eal_alarm_set(RTE_TEST_ALARM_TIMEOUT * US_PER_MS,
211 test_alarm_callback, NULL) < 0) {
212 printf("fail to set alarm callback\n");
215 while (flag == 0 && count ++ < 6)
216 rte_delay_ms(RTE_TEST_CHECK_PERIOD);
219 printf("Callback not called\n");
223 /* check if it will fail to set alarm with wrong us value */
224 printf("check if it will fail to set alarm with wrong ms values\n");
225 if (rte_eal_alarm_set(0, test_alarm_callback,
227 printf("should not be successful with 0 us value\n");
230 if (rte_eal_alarm_set(UINT64_MAX - 1, test_alarm_callback,
232 printf("should not be successful with (UINT64_MAX-1) us value\n");
236 /* check if it will fail to set alarm with null callback parameter */
237 printf("check if it will fail to set alarm with null callback parameter\n");
238 if (rte_eal_alarm_set(RTE_TEST_ALARM_TIMEOUT, NULL, NULL) >= 0) {
239 printf("should not be successful to set alarm with null callback parameter\n");
243 /* check if it will fail to remove alarm with null callback parameter */
244 printf("check if it will fail to remove alarm with null callback parameter\n");
245 if (rte_eal_alarm_cancel(NULL, NULL) == 0) {
246 printf("should not be successful to remove alarm with null callback parameter");
250 if (test_multi_alarms() != 0)
256 static struct test_command alarm_cmd = {
257 .command = "alarm_autotest",
258 .callback = test_alarm,
260 REGISTER_TEST_COMMAND(alarm_cmd);