1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
5 #include <rte_common.h>
6 #include <rte_hexdump.h>
8 #include <rte_malloc.h>
9 #include <rte_memcpy.h>
10 #include <rte_cycles.h>
12 #include <rte_service.h>
13 #include <rte_service_component.h>
17 /* used as the service core ID */
18 static uint32_t slcore_id;
19 /* used as timestamp to detect if a service core is running */
20 static uint64_t service_tick;
21 /* used as a flag to check if a function was run */
22 static uint32_t service_remote_launch_flag;
24 #define SERVICE_DELAY 1
26 #define DUMMY_SERVICE_NAME "dummy_service"
27 #define MT_SAFE_SERVICE_NAME "mt_safe_service"
32 slcore_id = rte_get_next_lcore(/* start core */ -1,
40 testsuite_teardown(void)
42 /* release service cores? */
45 static int32_t dummy_cb(void *args)
49 rte_delay_ms(SERVICE_DELAY);
53 static int32_t dummy_mt_unsafe_cb(void *args)
55 /* before running test, the initialization has set pass_test to 1.
56 * If the cmpset in service-cores is working correctly, the code here
57 * should never fail to take the lock. If the lock *is* taken, fail the
58 * test, because two threads are concurrently in a non-MT safe callback.
60 uint32_t *test_params = args;
61 uint32_t *atomic_lock = &test_params[0];
62 uint32_t *pass_test = &test_params[1];
63 int lock_taken = rte_atomic32_cmpset(atomic_lock, 0, 1);
65 /* delay with the lock held */
67 rte_atomic32_clear((rte_atomic32_t *)atomic_lock);
69 /* 2nd thread will fail to take lock, so set pass flag */
77 static int32_t dummy_mt_safe_cb(void *args)
79 /* Atomic checks to ensure MT safe services allow > 1 thread to
80 * concurrently run the callback. The concept is as follows;
81 * 1) if lock is available, take the lock then delay
82 * 2) if first lock is taken, and a thread arrives in the CB, we know
83 * that 2 threads are running the callback at the same time: MT safe
85 uint32_t *test_params = args;
86 uint32_t *atomic_lock = &test_params[0];
87 uint32_t *pass_test = &test_params[1];
88 int lock_taken = rte_atomic32_cmpset(atomic_lock, 0, 1);
90 /* delay with the lock held */
92 rte_atomic32_clear((rte_atomic32_t *)atomic_lock);
94 /* 2nd thread will fail to take lock, so set pass flag */
101 /* unregister all services */
107 TEST_ASSERT_EQUAL(-EINVAL, rte_service_component_unregister(1000),
108 "Unregistered invalid service id");
110 uint32_t c = rte_service_get_count();
111 for (i = 0; i < c; i++) {
112 TEST_ASSERT_EQUAL(0, rte_service_component_unregister(i),
113 "Error unregistering a valid service");
116 rte_service_lcore_reset_all();
117 rte_eal_mp_wait_lcore();
122 /* register a single dummy service */
126 /* make sure there are no remains from previous tests */
129 struct rte_service_spec service;
130 memset(&service, 0, sizeof(struct rte_service_spec));
132 TEST_ASSERT_EQUAL(-EINVAL,
133 rte_service_component_register(&service, NULL),
135 service.callback = dummy_cb;
137 TEST_ASSERT_EQUAL(-EINVAL,
138 rte_service_component_register(&service, NULL),
140 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
143 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
144 "Failed to register valid service");
146 rte_service_component_runstate_set(id, 1);
151 /* verify get_by_name() service lookup */
153 service_get_by_name(void)
158 TEST_ASSERT_EQUAL(-ENODEV,
159 rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
160 "get by name with invalid name should return -ENODEV");
161 TEST_ASSERT_EQUAL(-EINVAL,
162 rte_service_get_by_name(DUMMY_SERVICE_NAME, 0x0),
163 "get by name with NULL ptr should return -ENODEV");
165 /* register service */
166 struct rte_service_spec service;
167 memset(&service, 0, sizeof(struct rte_service_spec));
168 TEST_ASSERT_EQUAL(-EINVAL,
169 rte_service_component_register(&service, NULL),
171 service.callback = dummy_cb;
172 TEST_ASSERT_EQUAL(-EINVAL,
173 rte_service_component_register(&service, NULL),
175 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
176 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
177 "Failed to register valid service");
179 /* we unregistered all service, now registering 1, should be id 0 */
180 uint32_t service_id_as_expected = 0;
181 TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
182 "Service get_by_name should return 0 on valid inputs");
183 TEST_ASSERT_EQUAL(service_id_as_expected, sid,
184 "Service get_by_name should equal expected id");
188 /* ensure after unregister, get_by_name returns NULL */
189 TEST_ASSERT_EQUAL(-ENODEV,
190 rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
191 "get by name should return -ENODEV after unregister");
196 /* verify probe of capabilities */
198 service_probe_capability(void)
202 struct rte_service_spec service;
203 memset(&service, 0, sizeof(struct rte_service_spec));
204 service.callback = dummy_cb;
205 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
206 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
207 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
208 "Register of MT SAFE service failed");
210 /* verify flag is enabled */
211 const uint32_t sid = 0;
212 int32_t mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
213 TEST_ASSERT_EQUAL(1, mt, "MT SAFE capability flag not set.");
218 memset(&service, 0, sizeof(struct rte_service_spec));
219 service.callback = dummy_cb;
220 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
221 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
222 "Register of non-MT safe service failed");
224 /* verify flag is enabled */
225 mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
226 TEST_ASSERT_EQUAL(0, mt, "MT SAFE cap flag set on non MT SAFE service");
228 return unregister_all();
231 /* verify the service name */
235 const char *name = rte_service_get_name(0);
236 int equal = strcmp(name, DUMMY_SERVICE_NAME);
237 TEST_ASSERT_EQUAL(0, equal, "Error: Service name not correct");
239 return unregister_all();
242 /* verify service attr get */
244 service_attr_get(void)
246 /* ensure all services unregistered so cycle counts are zero */
249 struct rte_service_spec service;
250 memset(&service, 0, sizeof(struct rte_service_spec));
251 service.callback = dummy_cb;
252 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
253 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
255 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
256 "Register of service failed");
257 rte_service_component_runstate_set(id, 1);
258 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(id, 1),
259 "Error: Service start returned non-zero");
260 rte_service_set_stats_enable(id, 1);
262 uint32_t attr_id = UINT32_MAX;
263 uint64_t attr_value = 0xdead;
264 /* check error return values */
265 TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(id, attr_id,
267 "Invalid attr_id didn't return -EINVAL");
269 attr_id = RTE_SERVICE_ATTR_CYCLES;
270 TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(UINT32_MAX, attr_id,
272 "Invalid service id didn't return -EINVAL");
274 TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(id, attr_id, NULL),
275 "Invalid attr_value pointer id didn't return -EINVAL");
277 /* check correct (zero) return value and correct value (zero) */
278 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_id, &attr_value),
279 "Valid attr_get() call didn't return success");
280 TEST_ASSERT_EQUAL(0, attr_value,
281 "attr_get() call didn't set correct cycles (zero)");
282 /* check correct call count */
283 const int attr_calls = RTE_SERVICE_ATTR_CALL_COUNT;
284 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_calls, &attr_value),
285 "Valid attr_get() call didn't return success");
286 TEST_ASSERT_EQUAL(0, attr_value,
287 "attr_get() call didn't get call count (zero)");
289 /* Call service to increment cycle count */
290 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
291 "Service core add did not return zero");
292 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(id, slcore_id, 1),
293 "Enabling valid service and core failed");
294 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
295 "Starting service core failed");
297 /* wait for the service lcore to run */
300 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_id, &attr_value),
301 "Valid attr_get() call didn't return success");
302 int cycles_gt_zero = attr_value > 0;
303 TEST_ASSERT_EQUAL(1, cycles_gt_zero,
304 "attr_get() failed to get cycles (expected > zero)");
306 rte_service_lcore_stop(slcore_id);
308 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_calls, &attr_value),
309 "Valid attr_get() call didn't return success");
310 TEST_ASSERT_EQUAL(1, (attr_value > 0),
311 "attr_get() call didn't get call count (zero)");
313 TEST_ASSERT_EQUAL(0, rte_service_attr_reset_all(id),
314 "Valid attr_reset_all() return success");
316 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_id, &attr_value),
317 "Valid attr_get() call didn't return success");
318 TEST_ASSERT_EQUAL(0, attr_value,
319 "attr_get() call didn't set correct cycles (zero)");
320 /* ensure call count > zero */
321 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_calls, &attr_value),
322 "Valid attr_get() call didn't return success");
323 TEST_ASSERT_EQUAL(0, (attr_value > 0),
324 "attr_get() call didn't get call count (zero)");
326 return unregister_all();
329 /* verify service lcore attr get */
331 service_lcore_attr_get(void)
333 /* ensure all services unregistered so cycle counts are zero */
336 struct rte_service_spec service;
337 memset(&service, 0, sizeof(struct rte_service_spec));
338 service.callback = dummy_cb;
339 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
340 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
342 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
343 "Register of service failed");
344 rte_service_component_runstate_set(id, 1);
345 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(id, 1),
346 "Error: Service start returned non-zero");
347 rte_service_set_stats_enable(id, 1);
349 uint64_t lcore_attr_value = 0xdead;
350 uint32_t lcore_attr_id = UINT32_MAX;
352 /* check error return values */
353 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_attr_get(UINT32_MAX,
354 lcore_attr_id, &lcore_attr_value),
355 "Invalid lcore_id didn't return -EINVAL");
356 TEST_ASSERT_EQUAL(-ENOTSUP, rte_service_lcore_attr_get(rte_lcore_id(),
357 lcore_attr_id, &lcore_attr_value),
358 "Non-service core didn't return -ENOTSUP");
360 /* Start service core to increment loop count */
361 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
362 "Service core add did not return zero");
363 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(id, slcore_id, 1),
364 "Enabling valid service and core failed");
365 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
366 "Starting service core failed");
368 /* wait for the service lcore to run */
371 lcore_attr_id = RTE_SERVICE_LCORE_ATTR_LOOPS;
372 TEST_ASSERT_EQUAL(0, rte_service_lcore_attr_get(slcore_id,
373 lcore_attr_id, &lcore_attr_value),
374 "Valid lcore_attr_get() call didn't return success");
375 int loops_gt_zero = lcore_attr_value > 0;
376 TEST_ASSERT_EQUAL(1, loops_gt_zero,
377 "lcore_attr_get() failed to get loops "
378 "(expected > zero)");
380 lcore_attr_id++; // invalid lcore attr id
381 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_attr_get(slcore_id,
382 lcore_attr_id, &lcore_attr_value),
383 "Invalid lcore attr didn't return -EINVAL");
385 rte_service_lcore_stop(slcore_id);
387 TEST_ASSERT_EQUAL(0, rte_service_lcore_attr_reset_all(slcore_id),
388 "Valid lcore_attr_reset_all() didn't return success");
390 lcore_attr_id = RTE_SERVICE_LCORE_ATTR_LOOPS;
391 TEST_ASSERT_EQUAL(0, rte_service_lcore_attr_get(slcore_id,
392 lcore_attr_id, &lcore_attr_value),
393 "Valid lcore_attr_get() call didn't return success");
394 TEST_ASSERT_EQUAL(0, lcore_attr_value,
395 "lcore_attr_get() didn't get correct loop count "
398 return unregister_all();
401 /* verify service dump */
405 const uint32_t sid = 0;
406 rte_service_set_stats_enable(sid, 1);
407 rte_service_dump(stdout, 0);
408 rte_service_set_stats_enable(sid, 0);
409 rte_service_dump(stdout, 0);
410 return unregister_all();
413 /* start and stop a service */
415 service_start_stop(void)
417 const uint32_t sid = 0;
419 /* runstate_get() returns if service is running and slcore is mapped */
420 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
421 "Service core add did not return zero");
422 int ret = rte_service_map_lcore_set(sid, slcore_id, 1);
423 TEST_ASSERT_EQUAL(0, ret,
424 "Enabling service core, expected 0 got %d", ret);
426 TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
427 "Error: Service should be stopped");
429 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
430 "Error: Service stopped returned non-zero");
432 TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
433 "Error: Service is running - should be stopped");
435 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
436 "Error: Service start returned non-zero");
438 TEST_ASSERT_EQUAL(1, rte_service_runstate_get(sid),
439 "Error: Service is not running");
441 return unregister_all();
446 service_remote_launch_func(void *arg)
449 service_remote_launch_flag = 1;
453 /* enable and disable a lcore for a service */
455 service_lcore_en_dis_able(void)
457 const uint32_t sid = 0;
459 /* expected failure cases */
460 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 1),
461 "Enable on invalid core did not fail");
462 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 0),
463 "Disable on invalid core did not fail");
465 /* add service core to allow enabling */
466 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
467 "Add service core failed when not in use before");
470 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
471 "Enabling valid service and core failed");
472 TEST_ASSERT_EQUAL(1, rte_service_map_lcore_get(sid, slcore_id),
473 "Enabled core returned not-enabled");
476 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 0),
477 "Disabling valid service and lcore failed");
478 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_get(sid, slcore_id),
479 "Disabled core returned enabled");
481 /* call remote_launch to verify that app can launch ex-service lcore */
482 service_remote_launch_flag = 0;
483 rte_eal_wait_lcore(slcore_id);
484 int ret = rte_eal_remote_launch(service_remote_launch_func, NULL,
486 TEST_ASSERT_EQUAL(0, ret, "Ex-service core remote launch failed.");
487 rte_eal_wait_lcore(slcore_id);
488 TEST_ASSERT_EQUAL(1, service_remote_launch_flag,
489 "Ex-service core function call had no effect.");
491 return unregister_all();
495 service_lcore_running_check(void)
497 uint64_t tick = service_tick;
498 rte_delay_ms(SERVICE_DELAY * 100);
499 /* if (tick != service_tick) we know the lcore as polled the service */
500 return tick != service_tick;
504 service_lcore_add_del(void)
506 if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
507 !rte_lcore_is_enabled(2) || !rte_lcore_is_enabled(3))
510 /* check initial count */
511 TEST_ASSERT_EQUAL(0, rte_service_lcore_count(),
512 "Service lcore count has value before adding a lcore");
514 /* check service lcore add */
515 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
516 "Add service core failed when not in use before");
517 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_add(slcore_id),
518 "Add service core failed to refuse in-use lcore");
521 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
522 "Service core count not equal to one");
524 /* retrieve core list, checking lcore ids */
525 const uint32_t size = 4;
526 uint32_t service_core_ids[size];
527 int32_t n = rte_service_lcore_list(service_core_ids, size);
528 TEST_ASSERT_EQUAL(1, n, "Service core list return should equal 1");
529 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
530 "Service core list lcore must equal slcore_id");
532 /* recheck count, add more cores, and check count */
533 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
534 "Service core count not equal to one");
535 uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
538 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
539 "Service core add did not return zero");
540 uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
543 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
544 "Service core add did not return zero");
546 uint32_t count = rte_service_lcore_count();
547 const uint32_t cores_at_this_point = 3;
548 TEST_ASSERT_EQUAL(cores_at_this_point, count,
549 "Service core count %d, expected %d", count,
550 cores_at_this_point);
552 /* check longer service core list */
553 n = rte_service_lcore_list(service_core_ids, size);
554 TEST_ASSERT_EQUAL(3, n, "Service core list return should equal 3");
555 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
556 "Service core list[0] lcore must equal 1");
557 TEST_ASSERT_EQUAL(slcore_1, service_core_ids[1],
558 "Service core list[1] lcore must equal 2");
559 TEST_ASSERT_EQUAL(slcore_2, service_core_ids[2],
560 "Service core list[2] lcore must equal 3");
562 /* recheck count, remove lcores, check remaining lcore_id is correct */
563 TEST_ASSERT_EQUAL(3, rte_service_lcore_count(),
564 "Service core count not equal to three");
565 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_1),
566 "Service core add did not return zero");
567 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_2),
568 "Service core add did not return zero");
569 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
570 "Service core count not equal to one");
571 n = rte_service_lcore_list(service_core_ids, size);
572 TEST_ASSERT_EQUAL(1, n, "Service core list return should equal one");
573 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
574 "Service core list[0] lcore must equal %d",
577 return unregister_all();
581 service_threaded_test(int mt_safe)
585 /* add next 2 cores */
586 uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
589 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
590 "mt safe lcore add fail");
591 uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
594 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
595 "mt safe lcore add fail");
597 /* Use atomic locks to verify that two threads are in the same function
598 * at the same time. These are passed to the unit tests through the
599 * callback userdata parameter
601 uint32_t test_params[2];
602 memset(test_params, 0, sizeof(uint32_t) * 2);
604 /* register MT safe service. */
605 struct rte_service_spec service;
606 memset(&service, 0, sizeof(struct rte_service_spec));
607 service.callback_userdata = test_params;
608 snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
611 service.callback = dummy_mt_safe_cb;
612 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
614 service.callback = dummy_mt_unsafe_cb;
617 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
618 "Register of MT SAFE service failed");
620 const uint32_t sid = 0;
621 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
622 "Starting valid service failed");
623 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_1, 1),
624 "Failed to enable lcore 1 on mt safe service");
625 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_2, 1),
626 "Failed to enable lcore 2 on mt safe service");
627 rte_service_lcore_start(slcore_1);
628 rte_service_lcore_start(slcore_2);
630 /* wait for the worker threads to run */
632 rte_service_lcore_stop(slcore_1);
633 rte_service_lcore_stop(slcore_2);
635 TEST_ASSERT_EQUAL(0, test_params[1],
636 "Service run with component runstate = 0");
638 /* enable backend runstate: the service should run after this */
639 rte_service_component_runstate_set(id, 1);
641 /* initialize to pass, see callback comment for details */
645 /* wait for lcores before start() */
646 rte_eal_wait_lcore(slcore_1);
647 rte_eal_wait_lcore(slcore_2);
649 rte_service_lcore_start(slcore_1);
650 rte_service_lcore_start(slcore_2);
652 /* wait for the worker threads to run */
654 rte_service_lcore_stop(slcore_1);
655 rte_service_lcore_stop(slcore_2);
657 TEST_ASSERT_EQUAL(1, test_params[1],
658 "MT Safe service not run by two cores concurrently");
659 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
660 "Failed to stop MT Safe service");
662 rte_eal_wait_lcore(slcore_1);
663 rte_eal_wait_lcore(slcore_2);
666 /* return the value of the callback pass_test variable to caller */
667 return test_params[1];
670 /* tests an MT SAFE service with two cores. The callback function ensures that
671 * two threads access the callback concurrently.
674 service_mt_safe_poll(void)
678 if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
679 !rte_lcore_is_enabled(2))
682 TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
683 "Error: MT Safe service not run by two cores concurrently");
687 /* tests a NON mt safe service with two cores, the callback is serialized
688 * using the atomic cmpset.
691 service_mt_unsafe_poll(void)
695 if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
696 !rte_lcore_is_enabled(2))
699 TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
700 "Error: NON MT Safe service run by two cores concurrently");
705 delay_as_a_mt_safe_service(void *args)
708 uint32_t *params = args;
710 /* retrieve done flag and atomic lock to inc/dec */
711 uint32_t *done = ¶ms[0];
712 rte_atomic32_t *lock = (rte_atomic32_t *)¶ms[1];
715 rte_atomic32_inc(lock);
717 if (rte_atomic32_read(lock) > 1)
718 /* pass: second core has simultaneously incremented */
720 rte_atomic32_dec(lock);
727 delay_as_a_service(void *args)
729 uint32_t *done = (uint32_t *)args;
736 service_run_on_app_core_func(void *arg)
738 uint32_t *delay_service_id = (uint32_t *)arg;
739 return rte_service_run_iter_on_app_lcore(*delay_service_id, 1);
743 service_app_lcore_poll_impl(const int mt_safe)
745 uint32_t params[2] = {0};
747 struct rte_service_spec service;
748 memset(&service, 0, sizeof(struct rte_service_spec));
749 snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
751 service.callback = delay_as_a_mt_safe_service;
752 service.callback_userdata = params;
753 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
755 service.callback = delay_as_a_service;
756 service.callback_userdata = ¶ms;
760 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
761 "Register of app lcore delay service failed");
763 rte_service_component_runstate_set(id, 1);
764 rte_service_runstate_set(id, 1);
766 uint32_t app_core2 = rte_get_next_lcore(slcore_id, 1, 1);
767 rte_eal_wait_lcore(app_core2);
768 int app_core2_ret = rte_eal_remote_launch(service_run_on_app_core_func,
773 int app_core1_ret = service_run_on_app_core_func(&id);
775 /* flag done, then wait for the spawned 2nd core to return */
777 rte_eal_mp_wait_lcore();
779 /* core two gets launched first - and should hold the service lock */
780 TEST_ASSERT_EQUAL(0, app_core2_ret,
781 "App core2 : run service didn't return zero");
784 /* mt safe should have both cores return 0 for success */
785 TEST_ASSERT_EQUAL(0, app_core1_ret,
786 "MT Safe: App core1 didn't return 0");
788 /* core one attempts to run later - should be blocked */
789 TEST_ASSERT_EQUAL(-EBUSY, app_core1_ret,
790 "MT Unsafe: App core1 didn't return -EBUSY");
793 /* Performance test: call in a loop, and measure tsc() */
794 const uint32_t perf_iters = (1 << 12);
795 uint64_t start = rte_rdtsc();
797 for (i = 0; i < perf_iters; i++) {
798 int err = service_run_on_app_core_func(&id);
799 TEST_ASSERT_EQUAL(0, err, "perf test: returned run failure");
801 uint64_t end = rte_rdtsc();
802 printf("perf test for %s: %0.1f cycles per call\n", mt_safe ?
803 "MT Safe" : "MT Unsafe", (end - start)/(float)perf_iters);
810 service_app_lcore_mt_safe(void)
812 const int mt_safe = 1;
813 return service_app_lcore_poll_impl(mt_safe);
817 service_app_lcore_mt_unsafe(void)
819 const int mt_safe = 0;
820 return service_app_lcore_poll_impl(mt_safe);
823 /* start and stop a service core - ensuring it goes back to sleep */
825 service_lcore_start_stop(void)
827 /* start service core and service, create mapping so tick() runs */
828 const uint32_t sid = 0;
829 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
830 "Starting valid service failed");
831 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, slcore_id, 1),
832 "Enabling valid service on non-service core must fail");
835 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_start(slcore_id),
836 "Service core start without add should return EINVAL");
837 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
838 "Service core add did not return zero");
839 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
840 "Enabling valid service on valid core failed");
841 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
842 "Service core start after add failed");
843 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_start(slcore_id),
844 "Service core expected as running but was stopped");
846 /* ensures core really is running the service function */
847 TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
848 "Service core expected to poll service but it didn't");
851 TEST_ASSERT_EQUAL(-EBUSY, rte_service_lcore_stop(slcore_id),
852 "Service core running a service should return -EBUSY");
853 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
854 "Stopping valid service failed");
855 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_stop(100000),
856 "Invalid Service core stop should return -EINVAL");
857 TEST_ASSERT_EQUAL(0, rte_service_lcore_stop(slcore_id),
858 "Service core stop expected to return 0");
859 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_stop(slcore_id),
860 "Already stopped service core should return -EALREADY");
862 /* ensure service is not longer running */
863 TEST_ASSERT_EQUAL(0, service_lcore_running_check(),
864 "Service core expected to poll service but it didn't");
866 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_id),
867 "Service core del did not return zero");
869 return unregister_all();
872 /* stop a service and wait for it to become inactive */
874 service_may_be_active(void)
876 const uint32_t sid = 0;
879 /* expected failure cases */
880 TEST_ASSERT_EQUAL(-EINVAL, rte_service_may_be_active(10000),
881 "Invalid service may be active check did not fail");
883 /* start the service */
884 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
885 "Starting valid service failed");
886 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
887 "Add service core failed when not in use before");
888 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
889 "Enabling valid service on valid core failed");
890 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
891 "Service core start after add failed");
893 /* ensures core really is running the service function */
894 TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
895 "Service core expected to poll service but it didn't");
897 /* stop the service */
898 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
899 "Error: Service stop returned non-zero");
901 /* give the service 100ms to stop running */
902 for (i = 0; i < 100; i++) {
903 if (!rte_service_may_be_active(sid))
905 rte_delay_ms(SERVICE_DELAY);
908 TEST_ASSERT_EQUAL(0, rte_service_may_be_active(sid),
909 "Error: Service not stopped after 100ms");
911 return unregister_all();
914 /* check service may be active when service is running on a second lcore */
916 service_active_two_cores(void)
918 if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
919 !rte_lcore_is_enabled(2))
922 const uint32_t sid = 0;
925 uint32_t lcore = rte_get_next_lcore(/* start core */ -1,
928 uint32_t slcore = rte_get_next_lcore(/* start core */ lcore,
932 /* start the service on the second available lcore */
933 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
934 "Starting valid service failed");
935 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore),
936 "Add service core failed when not in use before");
937 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore, 1),
938 "Enabling valid service on valid core failed");
939 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore),
940 "Service core start after add failed");
942 /* ensures core really is running the service function */
943 TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
944 "Service core expected to poll service but it didn't");
946 /* ensures that service may be active reports running state */
947 TEST_ASSERT_EQUAL(1, rte_service_may_be_active(sid),
948 "Service may be active did not report running state");
950 /* stop the service */
951 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
952 "Error: Service stop returned non-zero");
954 /* give the service 100ms to stop running */
955 for (i = 0; i < 100; i++) {
956 if (!rte_service_may_be_active(sid))
958 rte_delay_ms(SERVICE_DELAY);
961 TEST_ASSERT_EQUAL(0, rte_service_may_be_active(sid),
962 "Error: Service not stopped after 100ms");
964 return unregister_all();
967 static struct unit_test_suite service_tests = {
968 .suite_name = "service core test suite",
969 .setup = testsuite_setup,
970 .teardown = testsuite_teardown,
972 TEST_CASE_ST(dummy_register, NULL, unregister_all),
973 TEST_CASE_ST(dummy_register, NULL, service_name),
974 TEST_CASE_ST(dummy_register, NULL, service_get_by_name),
975 TEST_CASE_ST(dummy_register, NULL, service_dump),
976 TEST_CASE_ST(dummy_register, NULL, service_attr_get),
977 TEST_CASE_ST(dummy_register, NULL, service_lcore_attr_get),
978 TEST_CASE_ST(dummy_register, NULL, service_probe_capability),
979 TEST_CASE_ST(dummy_register, NULL, service_start_stop),
980 TEST_CASE_ST(dummy_register, NULL, service_lcore_add_del),
981 TEST_CASE_ST(dummy_register, NULL, service_lcore_start_stop),
982 TEST_CASE_ST(dummy_register, NULL, service_lcore_en_dis_able),
983 TEST_CASE_ST(dummy_register, NULL, service_mt_unsafe_poll),
984 TEST_CASE_ST(dummy_register, NULL, service_mt_safe_poll),
985 TEST_CASE_ST(dummy_register, NULL, service_app_lcore_mt_safe),
986 TEST_CASE_ST(dummy_register, NULL, service_app_lcore_mt_unsafe),
987 TEST_CASE_ST(dummy_register, NULL, service_may_be_active),
988 TEST_CASE_ST(dummy_register, NULL, service_active_two_cores),
989 TEST_CASES_END() /**< NULL terminate unit test array */
994 test_service_common(void)
996 return unit_test_suite_runner(&service_tests);
999 REGISTER_TEST_COMMAND(service_autotest, test_service_common);