4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Intel Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <rte_common.h>
34 #include <rte_hexdump.h>
36 #include <rte_malloc.h>
37 #include <rte_memcpy.h>
38 #include <rte_cycles.h>
40 #include <rte_service.h>
41 #include <rte_service_component.h>
45 /* used as the service core ID */
46 static uint32_t slcore_id;
47 /* used as timestamp to detect if a service core is running */
48 static uint64_t service_tick;
49 /* used as a flag to check if a function was run */
50 static uint32_t service_remote_launch_flag;
52 #define SERVICE_DELAY 1
54 #define DUMMY_SERVICE_NAME "dummy_service"
55 #define MT_SAFE_SERVICE_NAME "mt_safe_service"
60 slcore_id = rte_get_next_lcore(/* start core */ -1,
68 testsuite_teardown(void)
70 /* release service cores? */
73 static int32_t dummy_cb(void *args)
77 rte_delay_ms(SERVICE_DELAY);
81 static int32_t dummy_mt_unsafe_cb(void *args)
83 /* before running test, the initialization has set pass_test to 1.
84 * If the cmpset in service-cores is working correctly, the code here
85 * should never fail to take the lock. If the lock *is* taken, fail the
86 * test, because two threads are concurrently in a non-MT safe callback.
88 uint32_t *test_params = args;
89 uint32_t *atomic_lock = &test_params[0];
90 uint32_t *pass_test = &test_params[1];
91 int lock_taken = rte_atomic32_cmpset(atomic_lock, 0, 1);
93 /* delay with the lock held */
95 rte_atomic32_clear((rte_atomic32_t *)atomic_lock);
97 /* 2nd thread will fail to take lock, so set pass flag */
105 static int32_t dummy_mt_safe_cb(void *args)
107 /* Atomic checks to ensure MT safe services allow > 1 thread to
108 * concurrently run the callback. The concept is as follows;
109 * 1) if lock is available, take the lock then delay
110 * 2) if first lock is taken, and a thread arrives in the CB, we know
111 * that 2 threads are running the callback at the same time: MT safe
113 uint32_t *test_params = args;
114 uint32_t *atomic_lock = &test_params[0];
115 uint32_t *pass_test = &test_params[1];
116 int lock_taken = rte_atomic32_cmpset(atomic_lock, 0, 1);
118 /* delay with the lock held */
120 rte_atomic32_clear((rte_atomic32_t *)atomic_lock);
122 /* 2nd thread will fail to take lock, so set pass flag */
129 /* unregister all services */
134 struct rte_service_spec *dead = (struct rte_service_spec *)0xdead;
136 TEST_ASSERT_EQUAL(-EINVAL, rte_service_unregister(0),
137 "Unregistered NULL pointer");
138 TEST_ASSERT_EQUAL(-EINVAL, rte_service_unregister(dead),
139 "Unregistered invalid pointer");
141 uint32_t c = rte_service_get_count();
142 for (i = 0; i < c; i++) {
143 struct rte_service_spec *s = rte_service_get_by_id(i);
144 TEST_ASSERT_EQUAL(0, rte_service_unregister(s),
145 "Error unregistering a valid service");
148 rte_service_lcore_reset_all();
153 /* register a single dummy service */
157 /* make sure there are no remains from previous tests */
160 struct rte_service_spec service;
161 memset(&service, 0, sizeof(struct rte_service_spec));
163 TEST_ASSERT_EQUAL(-EINVAL, rte_service_register(&service),
165 service.callback = dummy_cb;
167 TEST_ASSERT_EQUAL(-EINVAL, rte_service_register(&service),
169 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
171 TEST_ASSERT_EQUAL(0, rte_service_register(&service),
172 "Failed to register valid service");
177 /* verify get_by_name() service lookup */
179 service_get_by_name(void)
183 /* ensure with no services registered returns NULL */
184 TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME),
185 "Service get by name should return NULL");
187 /* register service */
188 struct rte_service_spec service;
189 memset(&service, 0, sizeof(struct rte_service_spec));
190 TEST_ASSERT_EQUAL(-EINVAL, rte_service_register(&service),
192 service.callback = dummy_cb;
193 TEST_ASSERT_EQUAL(-EINVAL, rte_service_register(&service),
195 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
196 TEST_ASSERT_EQUAL(0, rte_service_register(&service),
197 "Failed to register valid service");
199 /* ensure with dummy services registered returns same ptr as ID */
200 struct rte_service_spec *s_by_id = rte_service_get_by_id(0);
201 TEST_ASSERT_EQUAL(s_by_id, rte_service_get_by_name(DUMMY_SERVICE_NAME),
202 "Service get_by_name should equal get_by_id()");
206 /* ensure after unregister, get_by_name returns NULL */
207 TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME),
208 "get by name should return NULL after unregister");
213 /* verify probe of capabilities */
215 service_probe_capability(void)
219 struct rte_service_spec service;
220 memset(&service, 0, sizeof(struct rte_service_spec));
221 service.callback = dummy_cb;
222 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
223 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
224 TEST_ASSERT_EQUAL(0, rte_service_register(&service),
225 "Register of MT SAFE service failed");
227 /* verify flag is enabled */
228 struct rte_service_spec *s = rte_service_get_by_id(0);
229 int32_t mt = rte_service_probe_capability(s, RTE_SERVICE_CAP_MT_SAFE);
230 TEST_ASSERT_EQUAL(1, mt, "MT SAFE capability flag not set.");
235 memset(&service, 0, sizeof(struct rte_service_spec));
236 service.callback = dummy_cb;
237 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
238 TEST_ASSERT_EQUAL(0, rte_service_register(&service),
239 "Register of non-MT safe service failed");
241 /* verify flag is enabled */
242 s = rte_service_get_by_id(0);
243 mt = rte_service_probe_capability(s, RTE_SERVICE_CAP_MT_SAFE);
244 TEST_ASSERT_EQUAL(0, mt, "MT SAFE cap flag set on non MT SAFE service");
246 return unregister_all();
249 /* verify the service name */
253 struct rte_service_spec *service = rte_service_get_by_id(0);
255 int equal = strcmp(service->name, DUMMY_SERVICE_NAME);
256 TEST_ASSERT_EQUAL(0, equal, "Error: Service name not correct");
258 return unregister_all();
261 /* verify service dump */
265 struct rte_service_spec *service = rte_service_get_by_id(0);
266 rte_service_set_stats_enable(service, 1);
267 rte_service_dump(stdout, service);
268 rte_service_set_stats_enable(service, 0);
269 rte_service_dump(stdout, service);
270 return unregister_all();
273 /* start and stop a service */
275 service_start_stop(void)
277 struct rte_service_spec *service = rte_service_get_by_id(0);
279 /* is_running() returns if service is running and slcore is mapped */
280 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
281 "Service core add did not return zero");
282 int ret = rte_service_enable_on_lcore(service, slcore_id);
283 TEST_ASSERT_EQUAL(0, ret,
284 "Enabling service core, expected 0 got %d", ret);
286 TEST_ASSERT_EQUAL(0, rte_service_is_running(service),
287 "Error: Service should be stopped");
289 TEST_ASSERT_EQUAL(0, rte_service_stop(service),
290 "Error: Service stopped returned non-zero");
292 TEST_ASSERT_EQUAL(0, rte_service_is_running(service),
293 "Error: Service is running - should be stopped");
295 TEST_ASSERT_EQUAL(0, rte_service_start(service),
296 "Error: Service start returned non-zero");
298 TEST_ASSERT_EQUAL(1, rte_service_is_running(service),
299 "Error: Service is not running");
301 return unregister_all();
306 service_remote_launch_func(void *arg)
309 service_remote_launch_flag = 1;
313 /* enable and disable a lcore for a service */
315 service_lcore_en_dis_able(void)
317 struct rte_service_spec *s = rte_service_get_by_id(0);
319 /* expected failure cases */
320 TEST_ASSERT_EQUAL(-EINVAL, rte_service_enable_on_lcore(s, 100000),
321 "Enable on invalid core did not fail");
322 TEST_ASSERT_EQUAL(-EINVAL, rte_service_disable_on_lcore(s, 100000),
323 "Disable on invalid core did not fail");
325 /* add service core to allow enabling */
326 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
327 "Add service core failed when not in use before");
330 TEST_ASSERT_EQUAL(0, rte_service_enable_on_lcore(s, slcore_id),
331 "Enabling valid service and core failed");
332 TEST_ASSERT_EQUAL(1, rte_service_get_enabled_on_lcore(s, slcore_id),
333 "Enabled core returned not-enabled");
336 TEST_ASSERT_EQUAL(0, rte_service_disable_on_lcore(s, slcore_id),
337 "Disabling valid service and lcore failed");
338 TEST_ASSERT_EQUAL(0, rte_service_get_enabled_on_lcore(s, slcore_id),
339 "Disabled core returned enabled");
341 /* call remote_launch to verify that app can launch ex-service lcore */
342 service_remote_launch_flag = 0;
343 int ret = rte_eal_remote_launch(service_remote_launch_func, NULL,
345 TEST_ASSERT_EQUAL(0, ret, "Ex-service core remote launch failed.");
346 rte_eal_mp_wait_lcore();
347 TEST_ASSERT_EQUAL(1, service_remote_launch_flag,
348 "Ex-service core function call had no effect.");
350 return unregister_all();
354 service_lcore_running_check(void)
356 uint64_t tick = service_tick;
357 rte_delay_ms(SERVICE_DELAY * 10);
358 /* if (tick != service_tick) we know the lcore as polled the service */
359 return tick != service_tick;
363 service_lcore_add_del(void)
365 /* check initial count */
366 TEST_ASSERT_EQUAL(0, rte_service_lcore_count(),
367 "Service lcore count has value before adding a lcore");
369 /* check service lcore add */
370 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
371 "Add service core failed when not in use before");
372 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_add(slcore_id),
373 "Add service core failed to refuse in-use lcore");
376 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
377 "Service core count not equal to one");
379 /* retrieve core list, checking lcore ids */
380 const uint32_t size = 4;
381 uint32_t service_core_ids[size];
382 int32_t n = rte_service_lcore_list(service_core_ids, size);
383 TEST_ASSERT_EQUAL(1, n, "Service core list return should equal 1");
384 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
385 "Service core list lcore must equal slcore_id");
387 /* recheck count, add more cores, and check count */
388 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
389 "Service core count not equal to one");
390 uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
393 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
394 "Service core add did not return zero");
395 uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
398 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
399 "Service core add did not return zero");
401 uint32_t count = rte_service_lcore_count();
402 const uint32_t cores_at_this_point = 3;
403 TEST_ASSERT_EQUAL(cores_at_this_point, count,
404 "Service core count %d, expected %d", count,
405 cores_at_this_point);
407 /* check longer service core list */
408 n = rte_service_lcore_list(service_core_ids, size);
409 TEST_ASSERT_EQUAL(3, n, "Service core list return should equal 3");
410 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
411 "Service core list[0] lcore must equal 1");
412 TEST_ASSERT_EQUAL(slcore_1, service_core_ids[1],
413 "Service core list[1] lcore must equal 2");
414 TEST_ASSERT_EQUAL(slcore_2, service_core_ids[2],
415 "Service core list[2] lcore must equal 3");
417 /* recheck count, remove lcores, check remaining lcore_id is correct */
418 TEST_ASSERT_EQUAL(3, rte_service_lcore_count(),
419 "Service core count not equal to three");
420 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_1),
421 "Service core add did not return zero");
422 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_2),
423 "Service core add did not return zero");
424 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
425 "Service core count not equal to one");
426 n = rte_service_lcore_list(service_core_ids, size);
427 TEST_ASSERT_EQUAL(1, n, "Service core list return should equal one");
428 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
429 "Service core list[0] lcore must equal %d",
432 return unregister_all();
436 service_threaded_test(int mt_safe)
440 /* add next 2 cores */
441 uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
444 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
445 "mt safe lcore add fail");
446 uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
449 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
450 "mt safe lcore add fail");
452 /* Use atomic locks to verify that two threads are in the same function
453 * at the same time. These are passed to the unit tests through the
454 * callback userdata parameter
456 uint32_t test_params[2];
457 memset(test_params, 0, sizeof(uint32_t) * 2);
459 /* register MT safe service. */
460 struct rte_service_spec service;
461 memset(&service, 0, sizeof(struct rte_service_spec));
462 service.callback_userdata = test_params;
463 snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
466 service.callback = dummy_mt_safe_cb;
467 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
469 /* initialize to pass, see callback comment for details */
471 service.callback = dummy_mt_unsafe_cb;
474 TEST_ASSERT_EQUAL(0, rte_service_register(&service),
475 "Register of MT SAFE service failed");
477 struct rte_service_spec *s = rte_service_get_by_id(0);
478 TEST_ASSERT_EQUAL(0, rte_service_start(s),
479 "Starting valid service failed");
480 TEST_ASSERT_EQUAL(0, rte_service_enable_on_lcore(s, slcore_1),
481 "Failed to enable lcore 1 on mt safe service");
482 TEST_ASSERT_EQUAL(0, rte_service_enable_on_lcore(s, slcore_2),
483 "Failed to enable lcore 2 on mt safe service");
484 rte_service_lcore_start(slcore_1);
485 rte_service_lcore_start(slcore_2);
487 /* wait for the worker threads to run */
489 rte_service_lcore_stop(slcore_1);
490 rte_service_lcore_stop(slcore_2);
492 TEST_ASSERT_EQUAL(1, test_params[1],
493 "MT Safe service not run by two cores concurrently");
495 TEST_ASSERT_EQUAL(0, rte_service_stop(s),
496 "Failed to stop MT Safe service");
500 /* return the value of the callback pass_test variable to caller */
501 return test_params[1];
504 /* tests an MT SAFE service with two cores. The callback function ensures that
505 * two threads access the callback concurrently.
508 service_mt_safe_poll(void)
511 TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
512 "Error: MT Safe service not run by two cores concurrently");
516 /* tests a NON mt safe service with two cores, the callback is serialized
517 * using the atomic cmpset.
520 service_mt_unsafe_poll(void)
523 TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
524 "Error: NON MT Safe service run by two cores concurrently");
528 /* start and stop a service core - ensuring it goes back to sleep */
530 service_lcore_start_stop(void)
532 /* start service core and service, create mapping so tick() runs */
533 struct rte_service_spec *s = rte_service_get_by_id(0);
534 TEST_ASSERT_EQUAL(0, rte_service_start(s),
535 "Starting valid service failed");
536 TEST_ASSERT_EQUAL(-EINVAL, rte_service_enable_on_lcore(s, slcore_id),
537 "Enabling valid service on non-service core must fail");
540 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_start(slcore_id),
541 "Service core start without add should return EINVAL");
542 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
543 "Service core add did not return zero");
544 TEST_ASSERT_EQUAL(0, rte_service_enable_on_lcore(s, slcore_id),
545 "Enabling valid service on valid core failed");
546 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
547 "Service core start after add failed");
548 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_start(slcore_id),
549 "Service core expected as running but was stopped");
551 /* ensures core really is running the service function */
552 TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
553 "Service core expected to poll service but it didn't");
556 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_stop(100000),
557 "Invalid Service core stop should return -EINVAL");
558 TEST_ASSERT_EQUAL(0, rte_service_lcore_stop(slcore_id),
559 "Service core stop expected to return 0");
560 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_stop(slcore_id),
561 "Already stopped service core should return -EALREADY");
563 /* ensure service is not longer running */
564 TEST_ASSERT_EQUAL(0, service_lcore_running_check(),
565 "Service core expected to poll service but it didn't");
567 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_id),
568 "Service core del did not return zero");
570 return unregister_all();
573 static struct unit_test_suite service_tests = {
574 .suite_name = "service core test suite",
575 .setup = testsuite_setup,
576 .teardown = testsuite_teardown,
578 TEST_CASE_ST(dummy_register, NULL, unregister_all),
579 TEST_CASE_ST(dummy_register, NULL, service_name),
580 TEST_CASE_ST(dummy_register, NULL, service_get_by_name),
581 TEST_CASE_ST(dummy_register, NULL, service_dump),
582 TEST_CASE_ST(dummy_register, NULL, service_probe_capability),
583 TEST_CASE_ST(dummy_register, NULL, service_start_stop),
584 TEST_CASE_ST(dummy_register, NULL, service_lcore_add_del),
585 TEST_CASE_ST(dummy_register, NULL, service_lcore_start_stop),
586 TEST_CASE_ST(dummy_register, NULL, service_lcore_en_dis_able),
587 TEST_CASE_ST(dummy_register, NULL, service_mt_unsafe_poll),
588 TEST_CASE_ST(dummy_register, NULL, service_mt_safe_poll),
589 TEST_CASES_END() /**< NULL terminate unit test array */
594 test_service_common(void)
596 return unit_test_suite_runner(&service_tests);
599 REGISTER_TEST_COMMAND(service_autotest, test_service_common);