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 CAS 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 *lock = &test_params[0];
62 uint32_t *pass_test = &test_params[1];
64 int lock_taken = __atomic_compare_exchange_n(lock, &exp, 1, 0,
65 __ATOMIC_RELAXED, __ATOMIC_RELAXED);
67 /* delay with the lock held */
69 __atomic_store_n(lock, 0, __ATOMIC_RELAXED);
71 /* 2nd thread will fail to take lock, so set pass flag */
79 static int32_t dummy_mt_safe_cb(void *args)
81 /* Atomic checks to ensure MT safe services allow > 1 thread to
82 * concurrently run the callback. The concept is as follows;
83 * 1) if lock is available, take the lock then delay
84 * 2) if first lock is taken, and a thread arrives in the CB, we know
85 * that 2 threads are running the callback at the same time: MT safe
87 uint32_t *test_params = args;
88 uint32_t *lock = &test_params[0];
89 uint32_t *pass_test = &test_params[1];
91 int lock_taken = __atomic_compare_exchange_n(lock, &exp, 1, 0,
92 __ATOMIC_RELAXED, __ATOMIC_RELAXED);
94 /* delay with the lock held */
96 __atomic_store_n(lock, 0, __ATOMIC_RELAXED);
98 /* 2nd thread will fail to take lock, so set pass flag */
105 /* unregister all services */
111 TEST_ASSERT_EQUAL(-EINVAL, rte_service_component_unregister(1000),
112 "Unregistered invalid service id");
114 uint32_t c = rte_service_get_count();
115 for (i = 0; i < c; i++) {
116 TEST_ASSERT_EQUAL(0, rte_service_component_unregister(i),
117 "Error unregistering a valid service");
120 rte_service_lcore_reset_all();
121 rte_eal_mp_wait_lcore();
126 /* Wait until service lcore not active, or for 100x SERVICE_DELAY */
128 wait_slcore_inactive(uint32_t slcore_id)
132 for (i = 0; rte_service_lcore_may_be_active(slcore_id) == 1 &&
134 rte_delay_ms(SERVICE_DELAY);
137 /* register a single dummy service */
141 /* make sure there are no remains from previous tests */
144 struct rte_service_spec service;
145 memset(&service, 0, sizeof(struct rte_service_spec));
147 TEST_ASSERT_EQUAL(-EINVAL,
148 rte_service_component_register(&service, NULL),
150 service.callback = dummy_cb;
152 TEST_ASSERT_EQUAL(-EINVAL,
153 rte_service_component_register(&service, NULL),
155 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
158 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
159 "Failed to register valid service");
161 rte_service_component_runstate_set(id, 1);
166 /* verify get_by_name() service lookup */
168 service_get_by_name(void)
173 TEST_ASSERT_EQUAL(-ENODEV,
174 rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
175 "get by name with invalid name should return -ENODEV");
176 TEST_ASSERT_EQUAL(-EINVAL,
177 rte_service_get_by_name(DUMMY_SERVICE_NAME, 0x0),
178 "get by name with NULL ptr should return -ENODEV");
180 /* register service */
181 struct rte_service_spec service;
182 memset(&service, 0, sizeof(struct rte_service_spec));
183 TEST_ASSERT_EQUAL(-EINVAL,
184 rte_service_component_register(&service, NULL),
186 service.callback = dummy_cb;
187 TEST_ASSERT_EQUAL(-EINVAL,
188 rte_service_component_register(&service, NULL),
190 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
191 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
192 "Failed to register valid service");
194 /* we unregistered all service, now registering 1, should be id 0 */
195 uint32_t service_id_as_expected = 0;
196 TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
197 "Service get_by_name should return 0 on valid inputs");
198 TEST_ASSERT_EQUAL(service_id_as_expected, sid,
199 "Service get_by_name should equal expected id");
203 /* ensure after unregister, get_by_name returns NULL */
204 TEST_ASSERT_EQUAL(-ENODEV,
205 rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
206 "get by name should return -ENODEV after unregister");
211 /* verify probe of capabilities */
213 service_probe_capability(void)
217 struct rte_service_spec service;
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 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
222 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
223 "Register of MT SAFE service failed");
225 /* verify flag is enabled */
226 const uint32_t sid = 0;
227 int32_t mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
228 TEST_ASSERT_EQUAL(1, mt, "MT SAFE capability flag not set.");
233 memset(&service, 0, sizeof(struct rte_service_spec));
234 service.callback = dummy_cb;
235 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
236 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
237 "Register of non-MT safe service failed");
239 /* verify flag is enabled */
240 mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
241 TEST_ASSERT_EQUAL(0, mt, "MT SAFE cap flag set on non MT SAFE service");
243 return unregister_all();
246 /* verify the service name */
250 const char *name = rte_service_get_name(0);
251 int equal = strcmp(name, DUMMY_SERVICE_NAME);
252 TEST_ASSERT_EQUAL(0, equal, "Error: Service name not correct");
254 return unregister_all();
257 /* verify service attr get */
259 service_attr_get(void)
261 /* ensure all services unregistered so cycle counts are zero */
264 struct rte_service_spec service;
265 memset(&service, 0, sizeof(struct rte_service_spec));
266 service.callback = dummy_cb;
267 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
268 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
270 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
271 "Register of service failed");
272 rte_service_component_runstate_set(id, 1);
273 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(id, 1),
274 "Error: Service start returned non-zero");
275 rte_service_set_stats_enable(id, 1);
277 uint32_t attr_id = UINT32_MAX;
278 uint64_t attr_value = 0xdead;
279 /* check error return values */
280 TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(id, attr_id,
282 "Invalid attr_id didn't return -EINVAL");
284 attr_id = RTE_SERVICE_ATTR_CYCLES;
285 TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(UINT32_MAX, attr_id,
287 "Invalid service id didn't return -EINVAL");
289 TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(id, attr_id, NULL),
290 "Invalid attr_value pointer id didn't return -EINVAL");
292 /* check correct (zero) return value and correct value (zero) */
293 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_id, &attr_value),
294 "Valid attr_get() call didn't return success");
295 TEST_ASSERT_EQUAL(0, attr_value,
296 "attr_get() call didn't set correct cycles (zero)");
297 /* check correct call count */
298 const int attr_calls = RTE_SERVICE_ATTR_CALL_COUNT;
299 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_calls, &attr_value),
300 "Valid attr_get() call didn't return success");
301 TEST_ASSERT_EQUAL(0, attr_value,
302 "attr_get() call didn't get call count (zero)");
304 /* Call service to increment cycle count */
305 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
306 "Service core add did not return zero");
307 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(id, slcore_id, 1),
308 "Enabling valid service and core failed");
309 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
310 "Starting service core failed");
312 /* wait for the service lcore to run */
315 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_id, &attr_value),
316 "Valid attr_get() call didn't return success");
317 int cycles_gt_zero = attr_value > 0;
318 TEST_ASSERT_EQUAL(1, cycles_gt_zero,
319 "attr_get() failed to get cycles (expected > zero)");
321 rte_service_lcore_stop(slcore_id);
323 wait_slcore_inactive(slcore_id);
325 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_calls, &attr_value),
326 "Valid attr_get() call didn't return success");
327 TEST_ASSERT_EQUAL(1, (attr_value > 0),
328 "attr_get() call didn't get call count (zero)");
330 TEST_ASSERT_EQUAL(0, rte_service_attr_reset_all(id),
331 "Valid attr_reset_all() return success");
333 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_id, &attr_value),
334 "Valid attr_get() call didn't return success");
335 TEST_ASSERT_EQUAL(0, attr_value,
336 "attr_get() call didn't set correct cycles (zero)");
337 /* ensure call count > zero */
338 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_calls, &attr_value),
339 "Valid attr_get() call didn't return success");
340 TEST_ASSERT_EQUAL(0, (attr_value > 0),
341 "attr_get() call didn't get call count (zero)");
343 return unregister_all();
346 /* verify service lcore attr get */
348 service_lcore_attr_get(void)
350 /* ensure all services unregistered so cycle counts are zero */
353 struct rte_service_spec service;
354 memset(&service, 0, sizeof(struct rte_service_spec));
355 service.callback = dummy_cb;
356 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
357 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
359 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
360 "Register of service failed");
361 rte_service_component_runstate_set(id, 1);
362 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(id, 1),
363 "Error: Service start returned non-zero");
364 rte_service_set_stats_enable(id, 1);
366 uint64_t lcore_attr_value = 0xdead;
367 uint32_t lcore_attr_id = UINT32_MAX;
369 /* check error return values */
370 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_attr_get(UINT32_MAX,
371 lcore_attr_id, &lcore_attr_value),
372 "Invalid lcore_id didn't return -EINVAL");
373 TEST_ASSERT_EQUAL(-ENOTSUP, rte_service_lcore_attr_get(rte_lcore_id(),
374 lcore_attr_id, &lcore_attr_value),
375 "Non-service core didn't return -ENOTSUP");
377 /* Start service core to increment loop count */
378 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
379 "Service core add did not return zero");
380 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(id, slcore_id, 1),
381 "Enabling valid service and core failed");
382 /* Ensure service is not active before starting */
383 TEST_ASSERT_EQUAL(0, rte_service_lcore_may_be_active(slcore_id),
384 "Not-active service core reported as active");
385 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
386 "Starting service core failed");
388 /* wait for the service lcore to run */
391 lcore_attr_id = RTE_SERVICE_LCORE_ATTR_LOOPS;
392 TEST_ASSERT_EQUAL(0, rte_service_lcore_attr_get(slcore_id,
393 lcore_attr_id, &lcore_attr_value),
394 "Valid lcore_attr_get() call didn't return success");
395 int loops_gt_zero = lcore_attr_value > 0;
396 TEST_ASSERT_EQUAL(1, loops_gt_zero,
397 "lcore_attr_get() failed to get loops "
398 "(expected > zero)");
400 lcore_attr_id++; // invalid lcore attr id
401 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_attr_get(slcore_id,
402 lcore_attr_id, &lcore_attr_value),
403 "Invalid lcore attr didn't return -EINVAL");
405 /* Ensure service is active */
406 TEST_ASSERT_EQUAL(1, rte_service_lcore_may_be_active(slcore_id),
407 "Active service core reported as not-active");
409 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(id, slcore_id, 0),
410 "Disabling valid service and core failed");
411 TEST_ASSERT_EQUAL(0, rte_service_lcore_stop(slcore_id),
412 "Failed to stop service lcore");
414 wait_slcore_inactive(slcore_id);
416 TEST_ASSERT_EQUAL(0, rte_service_lcore_may_be_active(slcore_id),
417 "Service lcore not stopped after waiting.");
419 TEST_ASSERT_EQUAL(0, rte_service_lcore_attr_reset_all(slcore_id),
420 "Valid lcore_attr_reset_all() didn't return success");
422 lcore_attr_id = RTE_SERVICE_LCORE_ATTR_LOOPS;
423 TEST_ASSERT_EQUAL(0, rte_service_lcore_attr_get(slcore_id,
424 lcore_attr_id, &lcore_attr_value),
425 "Valid lcore_attr_get() call didn't return success");
426 TEST_ASSERT_EQUAL(0, lcore_attr_value,
427 "lcore_attr_get() didn't get correct loop count "
430 return unregister_all();
433 /* verify service dump */
437 const uint32_t sid = 0;
438 rte_service_set_stats_enable(sid, 1);
439 rte_service_dump(stdout, 0);
440 rte_service_set_stats_enable(sid, 0);
441 rte_service_dump(stdout, 0);
442 return unregister_all();
445 /* start and stop a service */
447 service_start_stop(void)
449 const uint32_t sid = 0;
451 /* runstate_get() returns if service is running and slcore is mapped */
452 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
453 "Service core add did not return zero");
454 int ret = rte_service_map_lcore_set(sid, slcore_id, 1);
455 TEST_ASSERT_EQUAL(0, ret,
456 "Enabling service core, expected 0 got %d", ret);
458 TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
459 "Error: Service should be stopped");
461 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
462 "Error: Service stopped returned non-zero");
464 TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
465 "Error: Service is running - should be stopped");
467 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
468 "Error: Service start returned non-zero");
470 TEST_ASSERT_EQUAL(1, rte_service_runstate_get(sid),
471 "Error: Service is not running");
473 return unregister_all();
478 service_remote_launch_func(void *arg)
481 service_remote_launch_flag = 1;
485 /* enable and disable a lcore for a service */
487 service_lcore_en_dis_able(void)
489 const uint32_t sid = 0;
491 /* expected failure cases */
492 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 1),
493 "Enable on invalid core did not fail");
494 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 0),
495 "Disable on invalid core did not fail");
497 /* add service core to allow enabling */
498 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
499 "Add service core failed when not in use before");
502 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
503 "Enabling valid service and core failed");
504 TEST_ASSERT_EQUAL(1, rte_service_map_lcore_get(sid, slcore_id),
505 "Enabled core returned not-enabled");
508 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 0),
509 "Disabling valid service and lcore failed");
510 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_get(sid, slcore_id),
511 "Disabled core returned enabled");
513 /* call remote_launch to verify that app can launch ex-service lcore */
514 service_remote_launch_flag = 0;
515 rte_eal_wait_lcore(slcore_id);
516 int ret = rte_eal_remote_launch(service_remote_launch_func, NULL,
518 TEST_ASSERT_EQUAL(0, ret, "Ex-service core remote launch failed.");
519 rte_eal_wait_lcore(slcore_id);
520 TEST_ASSERT_EQUAL(1, service_remote_launch_flag,
521 "Ex-service core function call had no effect.");
523 return unregister_all();
527 service_lcore_running_check(void)
529 uint64_t tick = service_tick;
530 rte_delay_ms(SERVICE_DELAY * 100);
531 /* if (tick != service_tick) we know the lcore as polled the service */
532 return tick != service_tick;
536 service_lcore_add_del(void)
538 if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
539 !rte_lcore_is_enabled(2) || !rte_lcore_is_enabled(3))
542 /* check initial count */
543 TEST_ASSERT_EQUAL(0, rte_service_lcore_count(),
544 "Service lcore count has value before adding a lcore");
546 /* check service lcore add */
547 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
548 "Add service core failed when not in use before");
549 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_add(slcore_id),
550 "Add service core failed to refuse in-use lcore");
553 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
554 "Service core count not equal to one");
556 /* retrieve core list, checking lcore ids */
557 const uint32_t size = 4;
558 uint32_t service_core_ids[size];
559 int32_t n = rte_service_lcore_list(service_core_ids, size);
560 TEST_ASSERT_EQUAL(1, n, "Service core list return should equal 1");
561 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
562 "Service core list lcore must equal slcore_id");
564 /* recheck count, add more cores, and check count */
565 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
566 "Service core count not equal to one");
567 uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
570 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
571 "Service core add did not return zero");
572 uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
575 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
576 "Service core add did not return zero");
578 uint32_t count = rte_service_lcore_count();
579 const uint32_t cores_at_this_point = 3;
580 TEST_ASSERT_EQUAL(cores_at_this_point, count,
581 "Service core count %d, expected %d", count,
582 cores_at_this_point);
584 /* check longer service core list */
585 n = rte_service_lcore_list(service_core_ids, size);
586 TEST_ASSERT_EQUAL(3, n, "Service core list return should equal 3");
587 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
588 "Service core list[0] lcore must equal 1");
589 TEST_ASSERT_EQUAL(slcore_1, service_core_ids[1],
590 "Service core list[1] lcore must equal 2");
591 TEST_ASSERT_EQUAL(slcore_2, service_core_ids[2],
592 "Service core list[2] lcore must equal 3");
594 /* recheck count, remove lcores, check remaining lcore_id is correct */
595 TEST_ASSERT_EQUAL(3, rte_service_lcore_count(),
596 "Service core count not equal to three");
597 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_1),
598 "Service core add did not return zero");
599 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_2),
600 "Service core add did not return zero");
601 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
602 "Service core count not equal to one");
603 n = rte_service_lcore_list(service_core_ids, size);
604 TEST_ASSERT_EQUAL(1, n, "Service core list return should equal one");
605 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
606 "Service core list[0] lcore must equal %d",
609 return unregister_all();
613 service_threaded_test(int mt_safe)
617 /* add next 2 cores */
618 uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
621 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
622 "mt safe lcore add fail");
623 uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
626 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
627 "mt safe lcore add fail");
629 /* Use locks to verify that two threads are in the same function
630 * at the same time. These are passed to the unit tests through
631 * the callback userdata parameter.
633 uint32_t test_params[2];
634 memset(test_params, 0, sizeof(uint32_t) * 2);
636 /* register MT safe service. */
637 struct rte_service_spec service;
638 memset(&service, 0, sizeof(struct rte_service_spec));
639 service.callback_userdata = test_params;
640 snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
643 service.callback = dummy_mt_safe_cb;
644 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
646 service.callback = dummy_mt_unsafe_cb;
649 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
650 "Register of MT SAFE service failed");
652 const uint32_t sid = 0;
653 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
654 "Starting valid service failed");
655 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_1, 1),
656 "Failed to enable lcore 1 on mt safe service");
657 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_2, 1),
658 "Failed to enable lcore 2 on mt safe service");
659 rte_service_lcore_start(slcore_1);
660 rte_service_lcore_start(slcore_2);
662 /* wait for the worker threads to run */
664 rte_service_lcore_stop(slcore_1);
665 rte_service_lcore_stop(slcore_2);
667 TEST_ASSERT_EQUAL(0, test_params[1],
668 "Service run with component runstate = 0");
670 /* enable backend runstate: the service should run after this */
671 rte_service_component_runstate_set(id, 1);
673 /* initialize to pass, see callback comment for details */
677 /* wait for lcores before start() */
678 rte_eal_wait_lcore(slcore_1);
679 rte_eal_wait_lcore(slcore_2);
681 rte_service_lcore_start(slcore_1);
682 rte_service_lcore_start(slcore_2);
684 /* wait for the worker threads to run */
686 rte_service_lcore_stop(slcore_1);
687 rte_service_lcore_stop(slcore_2);
689 TEST_ASSERT_EQUAL(1, test_params[1],
690 "MT Safe service not run by two cores concurrently");
691 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
692 "Failed to stop MT Safe service");
694 rte_eal_wait_lcore(slcore_1);
695 rte_eal_wait_lcore(slcore_2);
698 /* return the value of the callback pass_test variable to caller */
699 return test_params[1];
702 /* tests an MT SAFE service with two cores. The callback function ensures that
703 * two threads access the callback concurrently.
706 service_mt_safe_poll(void)
710 if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
711 !rte_lcore_is_enabled(2))
714 TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
715 "Error: MT Safe service not run by two cores concurrently");
719 /* tests a NON mt safe service with two cores, the callback is serialized
723 service_mt_unsafe_poll(void)
727 if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
728 !rte_lcore_is_enabled(2))
731 TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
732 "Error: NON MT Safe service run by two cores concurrently");
737 delay_as_a_mt_safe_service(void *args)
740 uint32_t *params = args;
742 /* retrieve done flag and lock to add/sub */
743 uint32_t *done = ¶ms[0];
744 uint32_t *lock = ¶ms[1];
747 __atomic_add_fetch(lock, 1, __ATOMIC_RELAXED);
749 if (__atomic_load_n(lock, __ATOMIC_RELAXED) > 1)
750 /* pass: second core has simultaneously incremented */
752 __atomic_sub_fetch(lock, 1, __ATOMIC_RELAXED);
759 delay_as_a_service(void *args)
761 uint32_t *done = (uint32_t *)args;
768 service_run_on_app_core_func(void *arg)
770 uint32_t *delay_service_id = (uint32_t *)arg;
771 return rte_service_run_iter_on_app_lcore(*delay_service_id, 1);
775 service_app_lcore_poll_impl(const int mt_safe)
777 uint32_t params[2] = {0};
779 struct rte_service_spec service;
780 memset(&service, 0, sizeof(struct rte_service_spec));
781 snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
783 service.callback = delay_as_a_mt_safe_service;
784 service.callback_userdata = params;
785 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
787 service.callback = delay_as_a_service;
788 service.callback_userdata = ¶ms;
792 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
793 "Register of app lcore delay service failed");
795 rte_service_component_runstate_set(id, 1);
796 rte_service_runstate_set(id, 1);
798 uint32_t app_core2 = rte_get_next_lcore(slcore_id, 1, 1);
799 rte_eal_wait_lcore(app_core2);
800 int app_core2_ret = rte_eal_remote_launch(service_run_on_app_core_func,
805 int app_core1_ret = service_run_on_app_core_func(&id);
807 /* flag done, then wait for the spawned 2nd core to return */
809 rte_eal_mp_wait_lcore();
811 /* core two gets launched first - and should hold the service lock */
812 TEST_ASSERT_EQUAL(0, app_core2_ret,
813 "App core2 : run service didn't return zero");
816 /* mt safe should have both cores return 0 for success */
817 TEST_ASSERT_EQUAL(0, app_core1_ret,
818 "MT Safe: App core1 didn't return 0");
820 /* core one attempts to run later - should be blocked */
821 TEST_ASSERT_EQUAL(-EBUSY, app_core1_ret,
822 "MT Unsafe: App core1 didn't return -EBUSY");
825 /* Performance test: call in a loop, and measure tsc() */
826 const uint32_t perf_iters = (1 << 12);
827 uint64_t start = rte_rdtsc();
829 for (i = 0; i < perf_iters; i++) {
830 int err = service_run_on_app_core_func(&id);
831 TEST_ASSERT_EQUAL(0, err, "perf test: returned run failure");
833 uint64_t end = rte_rdtsc();
834 printf("perf test for %s: %0.1f cycles per call\n", mt_safe ?
835 "MT Safe" : "MT Unsafe", (end - start)/(float)perf_iters);
842 service_app_lcore_mt_safe(void)
844 const int mt_safe = 1;
845 return service_app_lcore_poll_impl(mt_safe);
849 service_app_lcore_mt_unsafe(void)
851 const int mt_safe = 0;
852 return service_app_lcore_poll_impl(mt_safe);
855 /* start and stop a service core - ensuring it goes back to sleep */
857 service_lcore_start_stop(void)
859 /* start service core and service, create mapping so tick() runs */
860 const uint32_t sid = 0;
861 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
862 "Starting valid service failed");
863 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, slcore_id, 1),
864 "Enabling valid service on non-service core must fail");
867 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_start(slcore_id),
868 "Service core start without add should return EINVAL");
869 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
870 "Service core add did not return zero");
871 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
872 "Enabling valid service on valid core failed");
873 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
874 "Service core start after add failed");
875 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_start(slcore_id),
876 "Service core expected as running but was stopped");
878 /* ensures core really is running the service function */
879 TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
880 "Service core expected to poll service but it didn't");
883 TEST_ASSERT_EQUAL(-EBUSY, rte_service_lcore_stop(slcore_id),
884 "Service core running a service should return -EBUSY");
885 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
886 "Stopping valid service failed");
887 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_stop(100000),
888 "Invalid Service core stop should return -EINVAL");
889 TEST_ASSERT_EQUAL(0, rte_service_lcore_stop(slcore_id),
890 "Service core stop expected to return 0");
891 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_stop(slcore_id),
892 "Already stopped service core should return -EALREADY");
894 /* ensure service is not longer running */
895 TEST_ASSERT_EQUAL(0, service_lcore_running_check(),
896 "Service core expected to poll service but it didn't");
898 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_id),
899 "Service core del did not return zero");
901 return unregister_all();
904 /* stop a service and wait for it to become inactive */
906 service_may_be_active(void)
908 const uint32_t sid = 0;
911 /* expected failure cases */
912 TEST_ASSERT_EQUAL(-EINVAL, rte_service_may_be_active(10000),
913 "Invalid service may be active check did not fail");
915 /* start the service */
916 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
917 "Starting valid service failed");
918 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
919 "Add service core failed when not in use before");
920 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
921 "Enabling valid service on valid core failed");
922 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
923 "Service core start after add failed");
925 /* ensures core really is running the service function */
926 TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
927 "Service core expected to poll service but it didn't");
929 /* stop the service */
930 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
931 "Error: Service stop returned non-zero");
933 /* give the service 100ms to stop running */
934 for (i = 0; i < 100; i++) {
935 if (!rte_service_may_be_active(sid))
937 rte_delay_ms(SERVICE_DELAY);
940 TEST_ASSERT_EQUAL(0, rte_service_may_be_active(sid),
941 "Error: Service not stopped after 100ms");
943 return unregister_all();
946 /* check service may be active when service is running on a second lcore */
948 service_active_two_cores(void)
950 if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
951 !rte_lcore_is_enabled(2))
954 const uint32_t sid = 0;
957 uint32_t lcore = rte_get_next_lcore(/* start core */ -1,
960 uint32_t slcore = rte_get_next_lcore(/* start core */ lcore,
964 /* start the service on the second available lcore */
965 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
966 "Starting valid service failed");
967 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore),
968 "Add service core failed when not in use before");
969 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore, 1),
970 "Enabling valid service on valid core failed");
971 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore),
972 "Service core start after add failed");
974 /* ensures core really is running the service function */
975 TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
976 "Service core expected to poll service but it didn't");
978 /* ensures that service may be active reports running state */
979 TEST_ASSERT_EQUAL(1, rte_service_may_be_active(sid),
980 "Service may be active did not report running state");
982 /* stop the service */
983 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
984 "Error: Service stop returned non-zero");
986 /* give the service 100ms to stop running */
987 for (i = 0; i < 100; i++) {
988 if (!rte_service_may_be_active(sid))
990 rte_delay_ms(SERVICE_DELAY);
993 TEST_ASSERT_EQUAL(0, rte_service_may_be_active(sid),
994 "Error: Service not stopped after 100ms");
996 return unregister_all();
999 static struct unit_test_suite service_tests = {
1000 .suite_name = "service core test suite",
1001 .setup = testsuite_setup,
1002 .teardown = testsuite_teardown,
1003 .unit_test_cases = {
1004 TEST_CASE_ST(dummy_register, NULL, unregister_all),
1005 TEST_CASE_ST(dummy_register, NULL, service_name),
1006 TEST_CASE_ST(dummy_register, NULL, service_get_by_name),
1007 TEST_CASE_ST(dummy_register, NULL, service_dump),
1008 TEST_CASE_ST(dummy_register, NULL, service_attr_get),
1009 TEST_CASE_ST(dummy_register, NULL, service_lcore_attr_get),
1010 TEST_CASE_ST(dummy_register, NULL, service_probe_capability),
1011 TEST_CASE_ST(dummy_register, NULL, service_start_stop),
1012 TEST_CASE_ST(dummy_register, NULL, service_lcore_add_del),
1013 TEST_CASE_ST(dummy_register, NULL, service_lcore_start_stop),
1014 TEST_CASE_ST(dummy_register, NULL, service_lcore_en_dis_able),
1015 TEST_CASE_ST(dummy_register, NULL, service_mt_unsafe_poll),
1016 TEST_CASE_ST(dummy_register, NULL, service_mt_safe_poll),
1017 TEST_CASE_ST(dummy_register, NULL, service_app_lcore_mt_safe),
1018 TEST_CASE_ST(dummy_register, NULL, service_app_lcore_mt_unsafe),
1019 TEST_CASE_ST(dummy_register, NULL, service_may_be_active),
1020 TEST_CASE_ST(dummy_register, NULL, service_active_two_cores),
1021 TEST_CASES_END() /**< NULL terminate unit test array */
1026 test_service_common(void)
1028 return unit_test_suite_runner(&service_tests);
1031 REGISTER_TEST_COMMAND(service_autotest, test_service_common);