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 /* Wait until service lcore not active, or for 100x SERVICE_DELAY */
124 wait_slcore_inactive(uint32_t slcore_id)
128 for (i = 0; rte_service_lcore_may_be_active(slcore_id) == 1 &&
130 rte_delay_ms(SERVICE_DELAY);
133 /* register a single dummy service */
137 /* make sure there are no remains from previous tests */
140 struct rte_service_spec service;
141 memset(&service, 0, sizeof(struct rte_service_spec));
143 TEST_ASSERT_EQUAL(-EINVAL,
144 rte_service_component_register(&service, NULL),
146 service.callback = dummy_cb;
148 TEST_ASSERT_EQUAL(-EINVAL,
149 rte_service_component_register(&service, NULL),
151 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
154 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
155 "Failed to register valid service");
157 rte_service_component_runstate_set(id, 1);
162 /* verify get_by_name() service lookup */
164 service_get_by_name(void)
169 TEST_ASSERT_EQUAL(-ENODEV,
170 rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
171 "get by name with invalid name should return -ENODEV");
172 TEST_ASSERT_EQUAL(-EINVAL,
173 rte_service_get_by_name(DUMMY_SERVICE_NAME, 0x0),
174 "get by name with NULL ptr should return -ENODEV");
176 /* register service */
177 struct rte_service_spec service;
178 memset(&service, 0, sizeof(struct rte_service_spec));
179 TEST_ASSERT_EQUAL(-EINVAL,
180 rte_service_component_register(&service, NULL),
182 service.callback = dummy_cb;
183 TEST_ASSERT_EQUAL(-EINVAL,
184 rte_service_component_register(&service, NULL),
186 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
187 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
188 "Failed to register valid service");
190 /* we unregistered all service, now registering 1, should be id 0 */
191 uint32_t service_id_as_expected = 0;
192 TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
193 "Service get_by_name should return 0 on valid inputs");
194 TEST_ASSERT_EQUAL(service_id_as_expected, sid,
195 "Service get_by_name should equal expected id");
199 /* ensure after unregister, get_by_name returns NULL */
200 TEST_ASSERT_EQUAL(-ENODEV,
201 rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
202 "get by name should return -ENODEV after unregister");
207 /* verify probe of capabilities */
209 service_probe_capability(void)
213 struct rte_service_spec service;
214 memset(&service, 0, sizeof(struct rte_service_spec));
215 service.callback = dummy_cb;
216 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
217 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
218 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
219 "Register of MT SAFE service failed");
221 /* verify flag is enabled */
222 const uint32_t sid = 0;
223 int32_t mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
224 TEST_ASSERT_EQUAL(1, mt, "MT SAFE capability flag not set.");
229 memset(&service, 0, sizeof(struct rte_service_spec));
230 service.callback = dummy_cb;
231 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
232 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
233 "Register of non-MT safe service failed");
235 /* verify flag is enabled */
236 mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
237 TEST_ASSERT_EQUAL(0, mt, "MT SAFE cap flag set on non MT SAFE service");
239 return unregister_all();
242 /* verify the service name */
246 const char *name = rte_service_get_name(0);
247 int equal = strcmp(name, DUMMY_SERVICE_NAME);
248 TEST_ASSERT_EQUAL(0, equal, "Error: Service name not correct");
250 return unregister_all();
253 /* verify service attr get */
255 service_attr_get(void)
257 /* ensure all services unregistered so cycle counts are zero */
260 struct rte_service_spec service;
261 memset(&service, 0, sizeof(struct rte_service_spec));
262 service.callback = dummy_cb;
263 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
264 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
266 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
267 "Register of service failed");
268 rte_service_component_runstate_set(id, 1);
269 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(id, 1),
270 "Error: Service start returned non-zero");
271 rte_service_set_stats_enable(id, 1);
273 uint32_t attr_id = UINT32_MAX;
274 uint64_t attr_value = 0xdead;
275 /* check error return values */
276 TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(id, attr_id,
278 "Invalid attr_id didn't return -EINVAL");
280 attr_id = RTE_SERVICE_ATTR_CYCLES;
281 TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(UINT32_MAX, attr_id,
283 "Invalid service id didn't return -EINVAL");
285 TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(id, attr_id, NULL),
286 "Invalid attr_value pointer id didn't return -EINVAL");
288 /* check correct (zero) return value and correct value (zero) */
289 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_id, &attr_value),
290 "Valid attr_get() call didn't return success");
291 TEST_ASSERT_EQUAL(0, attr_value,
292 "attr_get() call didn't set correct cycles (zero)");
293 /* check correct call count */
294 const int attr_calls = RTE_SERVICE_ATTR_CALL_COUNT;
295 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_calls, &attr_value),
296 "Valid attr_get() call didn't return success");
297 TEST_ASSERT_EQUAL(0, attr_value,
298 "attr_get() call didn't get call count (zero)");
300 /* Call service to increment cycle count */
301 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
302 "Service core add did not return zero");
303 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(id, slcore_id, 1),
304 "Enabling valid service and core failed");
305 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
306 "Starting service core failed");
308 /* wait for the service lcore to run */
311 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_id, &attr_value),
312 "Valid attr_get() call didn't return success");
313 int cycles_gt_zero = attr_value > 0;
314 TEST_ASSERT_EQUAL(1, cycles_gt_zero,
315 "attr_get() failed to get cycles (expected > zero)");
317 rte_service_lcore_stop(slcore_id);
319 wait_slcore_inactive(slcore_id);
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(1, (attr_value > 0),
324 "attr_get() call didn't get call count (zero)");
326 TEST_ASSERT_EQUAL(0, rte_service_attr_reset_all(id),
327 "Valid attr_reset_all() return success");
329 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_id, &attr_value),
330 "Valid attr_get() call didn't return success");
331 TEST_ASSERT_EQUAL(0, attr_value,
332 "attr_get() call didn't set correct cycles (zero)");
333 /* ensure call count > zero */
334 TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_calls, &attr_value),
335 "Valid attr_get() call didn't return success");
336 TEST_ASSERT_EQUAL(0, (attr_value > 0),
337 "attr_get() call didn't get call count (zero)");
339 return unregister_all();
342 /* verify service lcore attr get */
344 service_lcore_attr_get(void)
346 /* ensure all services unregistered so cycle counts are zero */
349 struct rte_service_spec service;
350 memset(&service, 0, sizeof(struct rte_service_spec));
351 service.callback = dummy_cb;
352 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
353 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
355 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
356 "Register of service failed");
357 rte_service_component_runstate_set(id, 1);
358 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(id, 1),
359 "Error: Service start returned non-zero");
360 rte_service_set_stats_enable(id, 1);
362 uint64_t lcore_attr_value = 0xdead;
363 uint32_t lcore_attr_id = UINT32_MAX;
365 /* check error return values */
366 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_attr_get(UINT32_MAX,
367 lcore_attr_id, &lcore_attr_value),
368 "Invalid lcore_id didn't return -EINVAL");
369 TEST_ASSERT_EQUAL(-ENOTSUP, rte_service_lcore_attr_get(rte_lcore_id(),
370 lcore_attr_id, &lcore_attr_value),
371 "Non-service core didn't return -ENOTSUP");
373 /* Start service core to increment loop count */
374 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
375 "Service core add did not return zero");
376 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(id, slcore_id, 1),
377 "Enabling valid service and core failed");
378 /* Ensure service is not active before starting */
379 TEST_ASSERT_EQUAL(0, rte_service_lcore_may_be_active(slcore_id),
380 "Not-active service core reported as active");
381 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
382 "Starting service core failed");
384 /* wait for the service lcore to run */
387 lcore_attr_id = RTE_SERVICE_LCORE_ATTR_LOOPS;
388 TEST_ASSERT_EQUAL(0, rte_service_lcore_attr_get(slcore_id,
389 lcore_attr_id, &lcore_attr_value),
390 "Valid lcore_attr_get() call didn't return success");
391 int loops_gt_zero = lcore_attr_value > 0;
392 TEST_ASSERT_EQUAL(1, loops_gt_zero,
393 "lcore_attr_get() failed to get loops "
394 "(expected > zero)");
396 lcore_attr_id++; // invalid lcore attr id
397 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_attr_get(slcore_id,
398 lcore_attr_id, &lcore_attr_value),
399 "Invalid lcore attr didn't return -EINVAL");
401 /* Ensure service is active */
402 TEST_ASSERT_EQUAL(1, rte_service_lcore_may_be_active(slcore_id),
403 "Active service core reported as not-active");
405 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(id, slcore_id, 0),
406 "Disabling valid service and core failed");
407 TEST_ASSERT_EQUAL(0, rte_service_lcore_stop(slcore_id),
408 "Failed to stop service lcore");
410 wait_slcore_inactive(slcore_id);
412 TEST_ASSERT_EQUAL(0, rte_service_lcore_may_be_active(slcore_id),
413 "Service lcore not stopped after waiting.");
415 TEST_ASSERT_EQUAL(0, rte_service_lcore_attr_reset_all(slcore_id),
416 "Valid lcore_attr_reset_all() didn't return success");
418 lcore_attr_id = RTE_SERVICE_LCORE_ATTR_LOOPS;
419 TEST_ASSERT_EQUAL(0, rte_service_lcore_attr_get(slcore_id,
420 lcore_attr_id, &lcore_attr_value),
421 "Valid lcore_attr_get() call didn't return success");
422 TEST_ASSERT_EQUAL(0, lcore_attr_value,
423 "lcore_attr_get() didn't get correct loop count "
426 return unregister_all();
429 /* verify service dump */
433 const uint32_t sid = 0;
434 rte_service_set_stats_enable(sid, 1);
435 rte_service_dump(stdout, 0);
436 rte_service_set_stats_enable(sid, 0);
437 rte_service_dump(stdout, 0);
438 return unregister_all();
441 /* start and stop a service */
443 service_start_stop(void)
445 const uint32_t sid = 0;
447 /* runstate_get() returns if service is running and slcore is mapped */
448 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
449 "Service core add did not return zero");
450 int ret = rte_service_map_lcore_set(sid, slcore_id, 1);
451 TEST_ASSERT_EQUAL(0, ret,
452 "Enabling service core, expected 0 got %d", ret);
454 TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
455 "Error: Service should be stopped");
457 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
458 "Error: Service stopped returned non-zero");
460 TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
461 "Error: Service is running - should be stopped");
463 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
464 "Error: Service start returned non-zero");
466 TEST_ASSERT_EQUAL(1, rte_service_runstate_get(sid),
467 "Error: Service is not running");
469 return unregister_all();
474 service_remote_launch_func(void *arg)
477 service_remote_launch_flag = 1;
481 /* enable and disable a lcore for a service */
483 service_lcore_en_dis_able(void)
485 const uint32_t sid = 0;
487 /* expected failure cases */
488 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 1),
489 "Enable on invalid core did not fail");
490 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 0),
491 "Disable on invalid core did not fail");
493 /* add service core to allow enabling */
494 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
495 "Add service core failed when not in use before");
498 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
499 "Enabling valid service and core failed");
500 TEST_ASSERT_EQUAL(1, rte_service_map_lcore_get(sid, slcore_id),
501 "Enabled core returned not-enabled");
504 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 0),
505 "Disabling valid service and lcore failed");
506 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_get(sid, slcore_id),
507 "Disabled core returned enabled");
509 /* call remote_launch to verify that app can launch ex-service lcore */
510 service_remote_launch_flag = 0;
511 rte_eal_wait_lcore(slcore_id);
512 int ret = rte_eal_remote_launch(service_remote_launch_func, NULL,
514 TEST_ASSERT_EQUAL(0, ret, "Ex-service core remote launch failed.");
515 rte_eal_wait_lcore(slcore_id);
516 TEST_ASSERT_EQUAL(1, service_remote_launch_flag,
517 "Ex-service core function call had no effect.");
519 return unregister_all();
523 service_lcore_running_check(void)
525 uint64_t tick = service_tick;
526 rte_delay_ms(SERVICE_DELAY * 100);
527 /* if (tick != service_tick) we know the lcore as polled the service */
528 return tick != service_tick;
532 service_lcore_add_del(void)
534 if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
535 !rte_lcore_is_enabled(2) || !rte_lcore_is_enabled(3))
538 /* check initial count */
539 TEST_ASSERT_EQUAL(0, rte_service_lcore_count(),
540 "Service lcore count has value before adding a lcore");
542 /* check service lcore add */
543 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
544 "Add service core failed when not in use before");
545 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_add(slcore_id),
546 "Add service core failed to refuse in-use lcore");
549 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
550 "Service core count not equal to one");
552 /* retrieve core list, checking lcore ids */
553 const uint32_t size = 4;
554 uint32_t service_core_ids[size];
555 int32_t n = rte_service_lcore_list(service_core_ids, size);
556 TEST_ASSERT_EQUAL(1, n, "Service core list return should equal 1");
557 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
558 "Service core list lcore must equal slcore_id");
560 /* recheck count, add more cores, and check count */
561 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
562 "Service core count not equal to one");
563 uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
566 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
567 "Service core add did not return zero");
568 uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
571 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
572 "Service core add did not return zero");
574 uint32_t count = rte_service_lcore_count();
575 const uint32_t cores_at_this_point = 3;
576 TEST_ASSERT_EQUAL(cores_at_this_point, count,
577 "Service core count %d, expected %d", count,
578 cores_at_this_point);
580 /* check longer service core list */
581 n = rte_service_lcore_list(service_core_ids, size);
582 TEST_ASSERT_EQUAL(3, n, "Service core list return should equal 3");
583 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
584 "Service core list[0] lcore must equal 1");
585 TEST_ASSERT_EQUAL(slcore_1, service_core_ids[1],
586 "Service core list[1] lcore must equal 2");
587 TEST_ASSERT_EQUAL(slcore_2, service_core_ids[2],
588 "Service core list[2] lcore must equal 3");
590 /* recheck count, remove lcores, check remaining lcore_id is correct */
591 TEST_ASSERT_EQUAL(3, rte_service_lcore_count(),
592 "Service core count not equal to three");
593 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_1),
594 "Service core add did not return zero");
595 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_2),
596 "Service core add did not return zero");
597 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
598 "Service core count not equal to one");
599 n = rte_service_lcore_list(service_core_ids, size);
600 TEST_ASSERT_EQUAL(1, n, "Service core list return should equal one");
601 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
602 "Service core list[0] lcore must equal %d",
605 return unregister_all();
609 service_threaded_test(int mt_safe)
613 /* add next 2 cores */
614 uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
617 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
618 "mt safe lcore add fail");
619 uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
622 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
623 "mt safe lcore add fail");
625 /* Use atomic locks to verify that two threads are in the same function
626 * at the same time. These are passed to the unit tests through the
627 * callback userdata parameter
629 uint32_t test_params[2];
630 memset(test_params, 0, sizeof(uint32_t) * 2);
632 /* register MT safe service. */
633 struct rte_service_spec service;
634 memset(&service, 0, sizeof(struct rte_service_spec));
635 service.callback_userdata = test_params;
636 snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
639 service.callback = dummy_mt_safe_cb;
640 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
642 service.callback = dummy_mt_unsafe_cb;
645 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
646 "Register of MT SAFE service failed");
648 const uint32_t sid = 0;
649 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
650 "Starting valid service failed");
651 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_1, 1),
652 "Failed to enable lcore 1 on mt safe service");
653 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_2, 1),
654 "Failed to enable lcore 2 on mt safe service");
655 rte_service_lcore_start(slcore_1);
656 rte_service_lcore_start(slcore_2);
658 /* wait for the worker threads to run */
660 rte_service_lcore_stop(slcore_1);
661 rte_service_lcore_stop(slcore_2);
663 TEST_ASSERT_EQUAL(0, test_params[1],
664 "Service run with component runstate = 0");
666 /* enable backend runstate: the service should run after this */
667 rte_service_component_runstate_set(id, 1);
669 /* initialize to pass, see callback comment for details */
673 /* wait for lcores before start() */
674 rte_eal_wait_lcore(slcore_1);
675 rte_eal_wait_lcore(slcore_2);
677 rte_service_lcore_start(slcore_1);
678 rte_service_lcore_start(slcore_2);
680 /* wait for the worker threads to run */
682 rte_service_lcore_stop(slcore_1);
683 rte_service_lcore_stop(slcore_2);
685 TEST_ASSERT_EQUAL(1, test_params[1],
686 "MT Safe service not run by two cores concurrently");
687 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
688 "Failed to stop MT Safe service");
690 rte_eal_wait_lcore(slcore_1);
691 rte_eal_wait_lcore(slcore_2);
694 /* return the value of the callback pass_test variable to caller */
695 return test_params[1];
698 /* tests an MT SAFE service with two cores. The callback function ensures that
699 * two threads access the callback concurrently.
702 service_mt_safe_poll(void)
706 if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
707 !rte_lcore_is_enabled(2))
710 TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
711 "Error: MT Safe service not run by two cores concurrently");
715 /* tests a NON mt safe service with two cores, the callback is serialized
716 * using the atomic cmpset.
719 service_mt_unsafe_poll(void)
723 if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
724 !rte_lcore_is_enabled(2))
727 TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
728 "Error: NON MT Safe service run by two cores concurrently");
733 delay_as_a_mt_safe_service(void *args)
736 uint32_t *params = args;
738 /* retrieve done flag and atomic lock to inc/dec */
739 uint32_t *done = ¶ms[0];
740 rte_atomic32_t *lock = (rte_atomic32_t *)¶ms[1];
743 rte_atomic32_inc(lock);
745 if (rte_atomic32_read(lock) > 1)
746 /* pass: second core has simultaneously incremented */
748 rte_atomic32_dec(lock);
755 delay_as_a_service(void *args)
757 uint32_t *done = (uint32_t *)args;
764 service_run_on_app_core_func(void *arg)
766 uint32_t *delay_service_id = (uint32_t *)arg;
767 return rte_service_run_iter_on_app_lcore(*delay_service_id, 1);
771 service_app_lcore_poll_impl(const int mt_safe)
773 uint32_t params[2] = {0};
775 struct rte_service_spec service;
776 memset(&service, 0, sizeof(struct rte_service_spec));
777 snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
779 service.callback = delay_as_a_mt_safe_service;
780 service.callback_userdata = params;
781 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
783 service.callback = delay_as_a_service;
784 service.callback_userdata = ¶ms;
788 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
789 "Register of app lcore delay service failed");
791 rte_service_component_runstate_set(id, 1);
792 rte_service_runstate_set(id, 1);
794 uint32_t app_core2 = rte_get_next_lcore(slcore_id, 1, 1);
795 rte_eal_wait_lcore(app_core2);
796 int app_core2_ret = rte_eal_remote_launch(service_run_on_app_core_func,
801 int app_core1_ret = service_run_on_app_core_func(&id);
803 /* flag done, then wait for the spawned 2nd core to return */
805 rte_eal_mp_wait_lcore();
807 /* core two gets launched first - and should hold the service lock */
808 TEST_ASSERT_EQUAL(0, app_core2_ret,
809 "App core2 : run service didn't return zero");
812 /* mt safe should have both cores return 0 for success */
813 TEST_ASSERT_EQUAL(0, app_core1_ret,
814 "MT Safe: App core1 didn't return 0");
816 /* core one attempts to run later - should be blocked */
817 TEST_ASSERT_EQUAL(-EBUSY, app_core1_ret,
818 "MT Unsafe: App core1 didn't return -EBUSY");
821 /* Performance test: call in a loop, and measure tsc() */
822 const uint32_t perf_iters = (1 << 12);
823 uint64_t start = rte_rdtsc();
825 for (i = 0; i < perf_iters; i++) {
826 int err = service_run_on_app_core_func(&id);
827 TEST_ASSERT_EQUAL(0, err, "perf test: returned run failure");
829 uint64_t end = rte_rdtsc();
830 printf("perf test for %s: %0.1f cycles per call\n", mt_safe ?
831 "MT Safe" : "MT Unsafe", (end - start)/(float)perf_iters);
838 service_app_lcore_mt_safe(void)
840 const int mt_safe = 1;
841 return service_app_lcore_poll_impl(mt_safe);
845 service_app_lcore_mt_unsafe(void)
847 const int mt_safe = 0;
848 return service_app_lcore_poll_impl(mt_safe);
851 /* start and stop a service core - ensuring it goes back to sleep */
853 service_lcore_start_stop(void)
855 /* start service core and service, create mapping so tick() runs */
856 const uint32_t sid = 0;
857 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
858 "Starting valid service failed");
859 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, slcore_id, 1),
860 "Enabling valid service on non-service core must fail");
863 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_start(slcore_id),
864 "Service core start without add should return EINVAL");
865 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
866 "Service core add did not return zero");
867 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
868 "Enabling valid service on valid core failed");
869 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
870 "Service core start after add failed");
871 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_start(slcore_id),
872 "Service core expected as running but was stopped");
874 /* ensures core really is running the service function */
875 TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
876 "Service core expected to poll service but it didn't");
879 TEST_ASSERT_EQUAL(-EBUSY, rte_service_lcore_stop(slcore_id),
880 "Service core running a service should return -EBUSY");
881 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
882 "Stopping valid service failed");
883 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_stop(100000),
884 "Invalid Service core stop should return -EINVAL");
885 TEST_ASSERT_EQUAL(0, rte_service_lcore_stop(slcore_id),
886 "Service core stop expected to return 0");
887 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_stop(slcore_id),
888 "Already stopped service core should return -EALREADY");
890 /* ensure service is not longer running */
891 TEST_ASSERT_EQUAL(0, service_lcore_running_check(),
892 "Service core expected to poll service but it didn't");
894 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_id),
895 "Service core del did not return zero");
897 return unregister_all();
900 /* stop a service and wait for it to become inactive */
902 service_may_be_active(void)
904 const uint32_t sid = 0;
907 /* expected failure cases */
908 TEST_ASSERT_EQUAL(-EINVAL, rte_service_may_be_active(10000),
909 "Invalid service may be active check did not fail");
911 /* start the service */
912 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
913 "Starting valid service failed");
914 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
915 "Add service core failed when not in use before");
916 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
917 "Enabling valid service on valid core failed");
918 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
919 "Service core start after add failed");
921 /* ensures core really is running the service function */
922 TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
923 "Service core expected to poll service but it didn't");
925 /* stop the service */
926 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
927 "Error: Service stop returned non-zero");
929 /* give the service 100ms to stop running */
930 for (i = 0; i < 100; i++) {
931 if (!rte_service_may_be_active(sid))
933 rte_delay_ms(SERVICE_DELAY);
936 TEST_ASSERT_EQUAL(0, rte_service_may_be_active(sid),
937 "Error: Service not stopped after 100ms");
939 return unregister_all();
942 /* check service may be active when service is running on a second lcore */
944 service_active_two_cores(void)
946 if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
947 !rte_lcore_is_enabled(2))
950 const uint32_t sid = 0;
953 uint32_t lcore = rte_get_next_lcore(/* start core */ -1,
956 uint32_t slcore = rte_get_next_lcore(/* start core */ lcore,
960 /* start the service on the second available lcore */
961 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
962 "Starting valid service failed");
963 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore),
964 "Add service core failed when not in use before");
965 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore, 1),
966 "Enabling valid service on valid core failed");
967 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore),
968 "Service core start after add failed");
970 /* ensures core really is running the service function */
971 TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
972 "Service core expected to poll service but it didn't");
974 /* ensures that service may be active reports running state */
975 TEST_ASSERT_EQUAL(1, rte_service_may_be_active(sid),
976 "Service may be active did not report running state");
978 /* stop the service */
979 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
980 "Error: Service stop returned non-zero");
982 /* give the service 100ms to stop running */
983 for (i = 0; i < 100; i++) {
984 if (!rte_service_may_be_active(sid))
986 rte_delay_ms(SERVICE_DELAY);
989 TEST_ASSERT_EQUAL(0, rte_service_may_be_active(sid),
990 "Error: Service not stopped after 100ms");
992 return unregister_all();
995 static struct unit_test_suite service_tests = {
996 .suite_name = "service core test suite",
997 .setup = testsuite_setup,
998 .teardown = testsuite_teardown,
1000 TEST_CASE_ST(dummy_register, NULL, unregister_all),
1001 TEST_CASE_ST(dummy_register, NULL, service_name),
1002 TEST_CASE_ST(dummy_register, NULL, service_get_by_name),
1003 TEST_CASE_ST(dummy_register, NULL, service_dump),
1004 TEST_CASE_ST(dummy_register, NULL, service_attr_get),
1005 TEST_CASE_ST(dummy_register, NULL, service_lcore_attr_get),
1006 TEST_CASE_ST(dummy_register, NULL, service_probe_capability),
1007 TEST_CASE_ST(dummy_register, NULL, service_start_stop),
1008 TEST_CASE_ST(dummy_register, NULL, service_lcore_add_del),
1009 TEST_CASE_ST(dummy_register, NULL, service_lcore_start_stop),
1010 TEST_CASE_ST(dummy_register, NULL, service_lcore_en_dis_able),
1011 TEST_CASE_ST(dummy_register, NULL, service_mt_unsafe_poll),
1012 TEST_CASE_ST(dummy_register, NULL, service_mt_safe_poll),
1013 TEST_CASE_ST(dummy_register, NULL, service_app_lcore_mt_safe),
1014 TEST_CASE_ST(dummy_register, NULL, service_app_lcore_mt_unsafe),
1015 TEST_CASE_ST(dummy_register, NULL, service_may_be_active),
1016 TEST_CASE_ST(dummy_register, NULL, service_active_two_cores),
1017 TEST_CASES_END() /**< NULL terminate unit test array */
1022 test_service_common(void)
1024 return unit_test_suite_runner(&service_tests);
1027 REGISTER_TEST_COMMAND(service_autotest, test_service_common);