4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Intel Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <rte_common.h>
34 #include <rte_hexdump.h>
36 #include <rte_malloc.h>
37 #include <rte_memcpy.h>
38 #include <rte_cycles.h>
40 #include <rte_service.h>
41 #include <rte_service_component.h>
45 /* used as the service core ID */
46 static uint32_t slcore_id;
47 /* used as timestamp to detect if a service core is running */
48 static uint64_t service_tick;
49 /* used as a flag to check if a function was run */
50 static uint32_t service_remote_launch_flag;
52 #define SERVICE_DELAY 1
54 #define DUMMY_SERVICE_NAME "dummy_service"
55 #define MT_SAFE_SERVICE_NAME "mt_safe_service"
60 slcore_id = rte_get_next_lcore(/* start core */ -1,
68 testsuite_teardown(void)
70 /* release service cores? */
73 static int32_t dummy_cb(void *args)
77 rte_delay_ms(SERVICE_DELAY);
81 static int32_t dummy_mt_unsafe_cb(void *args)
83 /* before running test, the initialization has set pass_test to 1.
84 * If the cmpset in service-cores is working correctly, the code here
85 * should never fail to take the lock. If the lock *is* taken, fail the
86 * test, because two threads are concurrently in a non-MT safe callback.
88 uint32_t *test_params = args;
89 uint32_t *atomic_lock = &test_params[0];
90 uint32_t *pass_test = &test_params[1];
91 int lock_taken = rte_atomic32_cmpset(atomic_lock, 0, 1);
93 /* delay with the lock held */
95 rte_atomic32_clear((rte_atomic32_t *)atomic_lock);
97 /* 2nd thread will fail to take lock, so set pass flag */
105 static int32_t dummy_mt_safe_cb(void *args)
107 /* Atomic checks to ensure MT safe services allow > 1 thread to
108 * concurrently run the callback. The concept is as follows;
109 * 1) if lock is available, take the lock then delay
110 * 2) if first lock is taken, and a thread arrives in the CB, we know
111 * that 2 threads are running the callback at the same time: MT safe
113 uint32_t *test_params = args;
114 uint32_t *atomic_lock = &test_params[0];
115 uint32_t *pass_test = &test_params[1];
116 int lock_taken = rte_atomic32_cmpset(atomic_lock, 0, 1);
118 /* delay with the lock held */
120 rte_atomic32_clear((rte_atomic32_t *)atomic_lock);
122 /* 2nd thread will fail to take lock, so set pass flag */
129 /* unregister all services */
135 TEST_ASSERT_EQUAL(-EINVAL, rte_service_component_unregister(1000),
136 "Unregistered invalid service id");
138 uint32_t c = rte_service_get_count();
139 for (i = 0; i < c; i++) {
140 TEST_ASSERT_EQUAL(0, rte_service_component_unregister(i),
141 "Error unregistering a valid service");
144 rte_service_lcore_reset_all();
149 /* register a single dummy service */
153 /* make sure there are no remains from previous tests */
156 struct rte_service_spec service;
157 memset(&service, 0, sizeof(struct rte_service_spec));
159 TEST_ASSERT_EQUAL(-EINVAL,
160 rte_service_component_register(&service, NULL),
162 service.callback = dummy_cb;
164 TEST_ASSERT_EQUAL(-EINVAL,
165 rte_service_component_register(&service, NULL),
167 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
170 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
171 "Failed to register valid service");
173 rte_service_component_runstate_set(id, 1);
178 /* verify get_by_name() service lookup */
180 service_get_by_name(void)
185 TEST_ASSERT_EQUAL(-ENODEV,
186 rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
187 "get by name with invalid name should return -ENODEV");
188 TEST_ASSERT_EQUAL(-EINVAL,
189 rte_service_get_by_name(DUMMY_SERVICE_NAME, 0x0),
190 "get by name with NULL ptr should return -ENODEV");
192 /* register service */
193 struct rte_service_spec service;
194 memset(&service, 0, sizeof(struct rte_service_spec));
195 TEST_ASSERT_EQUAL(-EINVAL,
196 rte_service_component_register(&service, NULL),
198 service.callback = dummy_cb;
199 TEST_ASSERT_EQUAL(-EINVAL,
200 rte_service_component_register(&service, NULL),
202 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
203 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
204 "Failed to register valid service");
206 /* we unregistered all service, now registering 1, should be id 0 */
207 uint32_t service_id_as_expected = 0;
208 TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
209 "Service get_by_name should return 0 on valid inputs");
210 TEST_ASSERT_EQUAL(service_id_as_expected, sid,
211 "Service get_by_name should equal expected id");
215 /* ensure after unregister, get_by_name returns NULL */
216 TEST_ASSERT_EQUAL(-ENODEV,
217 rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
218 "get by name should return -ENODEV after unregister");
223 /* verify probe of capabilities */
225 service_probe_capability(void)
229 struct rte_service_spec service;
230 memset(&service, 0, sizeof(struct rte_service_spec));
231 service.callback = dummy_cb;
232 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
233 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
234 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
235 "Register of MT SAFE service failed");
237 /* verify flag is enabled */
238 const uint32_t sid = 0;
239 int32_t mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
240 TEST_ASSERT_EQUAL(1, mt, "MT SAFE capability flag not set.");
245 memset(&service, 0, sizeof(struct rte_service_spec));
246 service.callback = dummy_cb;
247 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
248 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
249 "Register of non-MT safe service failed");
251 /* verify flag is enabled */
252 mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
253 TEST_ASSERT_EQUAL(0, mt, "MT SAFE cap flag set on non MT SAFE service");
255 return unregister_all();
258 /* verify the service name */
262 const char *name = rte_service_get_name(0);
263 int equal = strcmp(name, DUMMY_SERVICE_NAME);
264 TEST_ASSERT_EQUAL(0, equal, "Error: Service name not correct");
266 return unregister_all();
269 /* verify service dump */
273 const uint32_t sid = 0;
274 rte_service_set_stats_enable(sid, 1);
275 rte_service_dump(stdout, 0);
276 rte_service_set_stats_enable(sid, 0);
277 rte_service_dump(stdout, 0);
278 return unregister_all();
281 /* start and stop a service */
283 service_start_stop(void)
285 const uint32_t sid = 0;
287 /* runstate_get() returns if service is running and slcore is mapped */
288 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
289 "Service core add did not return zero");
290 int ret = rte_service_map_lcore_set(sid, slcore_id, 1);
291 TEST_ASSERT_EQUAL(0, ret,
292 "Enabling service core, expected 0 got %d", ret);
294 TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
295 "Error: Service should be stopped");
297 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
298 "Error: Service stopped returned non-zero");
300 TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
301 "Error: Service is running - should be stopped");
303 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
304 "Error: Service start returned non-zero");
306 TEST_ASSERT_EQUAL(1, rte_service_runstate_get(sid),
307 "Error: Service is not running");
309 return unregister_all();
314 service_remote_launch_func(void *arg)
317 service_remote_launch_flag = 1;
321 /* enable and disable a lcore for a service */
323 service_lcore_en_dis_able(void)
325 const uint32_t sid = 0;
327 /* expected failure cases */
328 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 1),
329 "Enable on invalid core did not fail");
330 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 0),
331 "Disable on invalid core did not fail");
333 /* add service core to allow enabling */
334 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
335 "Add service core failed when not in use before");
338 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
339 "Enabling valid service and core failed");
340 TEST_ASSERT_EQUAL(1, rte_service_map_lcore_get(sid, slcore_id),
341 "Enabled core returned not-enabled");
344 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 0),
345 "Disabling valid service and lcore failed");
346 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_get(sid, slcore_id),
347 "Disabled core returned enabled");
349 /* call remote_launch to verify that app can launch ex-service lcore */
350 service_remote_launch_flag = 0;
351 int ret = rte_eal_remote_launch(service_remote_launch_func, NULL,
353 TEST_ASSERT_EQUAL(0, ret, "Ex-service core remote launch failed.");
354 rte_eal_mp_wait_lcore();
355 TEST_ASSERT_EQUAL(1, service_remote_launch_flag,
356 "Ex-service core function call had no effect.");
358 return unregister_all();
362 service_lcore_running_check(void)
364 uint64_t tick = service_tick;
365 rte_delay_ms(SERVICE_DELAY * 10);
366 /* if (tick != service_tick) we know the lcore as polled the service */
367 return tick != service_tick;
371 service_lcore_add_del(void)
373 /* check initial count */
374 TEST_ASSERT_EQUAL(0, rte_service_lcore_count(),
375 "Service lcore count has value before adding a lcore");
377 /* check service lcore add */
378 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
379 "Add service core failed when not in use before");
380 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_add(slcore_id),
381 "Add service core failed to refuse in-use lcore");
384 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
385 "Service core count not equal to one");
387 /* retrieve core list, checking lcore ids */
388 const uint32_t size = 4;
389 uint32_t service_core_ids[size];
390 int32_t n = rte_service_lcore_list(service_core_ids, size);
391 TEST_ASSERT_EQUAL(1, n, "Service core list return should equal 1");
392 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
393 "Service core list lcore must equal slcore_id");
395 /* recheck count, add more cores, and check count */
396 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
397 "Service core count not equal to one");
398 uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
401 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
402 "Service core add did not return zero");
403 uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
406 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
407 "Service core add did not return zero");
409 uint32_t count = rte_service_lcore_count();
410 const uint32_t cores_at_this_point = 3;
411 TEST_ASSERT_EQUAL(cores_at_this_point, count,
412 "Service core count %d, expected %d", count,
413 cores_at_this_point);
415 /* check longer service core list */
416 n = rte_service_lcore_list(service_core_ids, size);
417 TEST_ASSERT_EQUAL(3, n, "Service core list return should equal 3");
418 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
419 "Service core list[0] lcore must equal 1");
420 TEST_ASSERT_EQUAL(slcore_1, service_core_ids[1],
421 "Service core list[1] lcore must equal 2");
422 TEST_ASSERT_EQUAL(slcore_2, service_core_ids[2],
423 "Service core list[2] lcore must equal 3");
425 /* recheck count, remove lcores, check remaining lcore_id is correct */
426 TEST_ASSERT_EQUAL(3, rte_service_lcore_count(),
427 "Service core count not equal to three");
428 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_1),
429 "Service core add did not return zero");
430 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_2),
431 "Service core add did not return zero");
432 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
433 "Service core count not equal to one");
434 n = rte_service_lcore_list(service_core_ids, size);
435 TEST_ASSERT_EQUAL(1, n, "Service core list return should equal one");
436 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
437 "Service core list[0] lcore must equal %d",
440 return unregister_all();
444 service_threaded_test(int mt_safe)
448 /* add next 2 cores */
449 uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
452 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
453 "mt safe lcore add fail");
454 uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
457 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
458 "mt safe lcore add fail");
460 /* Use atomic locks to verify that two threads are in the same function
461 * at the same time. These are passed to the unit tests through the
462 * callback userdata parameter
464 uint32_t test_params[2];
465 memset(test_params, 0, sizeof(uint32_t) * 2);
467 /* register MT safe service. */
468 struct rte_service_spec service;
469 memset(&service, 0, sizeof(struct rte_service_spec));
470 service.callback_userdata = test_params;
471 snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
474 service.callback = dummy_mt_safe_cb;
475 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
477 service.callback = dummy_mt_unsafe_cb;
480 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
481 "Register of MT SAFE service failed");
483 const uint32_t sid = 0;
484 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
485 "Starting valid service failed");
486 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_1, 1),
487 "Failed to enable lcore 1 on mt safe service");
488 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_2, 1),
489 "Failed to enable lcore 2 on mt safe service");
490 rte_service_lcore_start(slcore_1);
491 rte_service_lcore_start(slcore_2);
493 /* wait for the worker threads to run */
495 rte_service_lcore_stop(slcore_1);
496 rte_service_lcore_stop(slcore_2);
498 TEST_ASSERT_EQUAL(0, test_params[1],
499 "Service run with component runstate = 0");
501 /* enable backend runstate: the service should run after this */
502 rte_service_component_runstate_set(id, 1);
504 /* initialize to pass, see callback comment for details */
508 rte_service_lcore_start(slcore_1);
509 rte_service_lcore_start(slcore_2);
511 /* wait for the worker threads to run */
513 rte_service_lcore_stop(slcore_1);
514 rte_service_lcore_stop(slcore_2);
516 TEST_ASSERT_EQUAL(1, test_params[1],
517 "MT Safe service not run by two cores concurrently");
518 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
519 "Failed to stop MT Safe service");
523 /* return the value of the callback pass_test variable to caller */
524 return test_params[1];
527 /* tests an MT SAFE service with two cores. The callback function ensures that
528 * two threads access the callback concurrently.
531 service_mt_safe_poll(void)
534 TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
535 "Error: MT Safe service not run by two cores concurrently");
539 /* tests a NON mt safe service with two cores, the callback is serialized
540 * using the atomic cmpset.
543 service_mt_unsafe_poll(void)
546 TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
547 "Error: NON MT Safe service run by two cores concurrently");
551 /* start and stop a service core - ensuring it goes back to sleep */
553 service_lcore_start_stop(void)
555 /* start service core and service, create mapping so tick() runs */
556 const uint32_t sid = 0;
557 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
558 "Starting valid service failed");
559 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, slcore_id, 1),
560 "Enabling valid service on non-service core must fail");
563 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_start(slcore_id),
564 "Service core start without add should return EINVAL");
565 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
566 "Service core add did not return zero");
567 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
568 "Enabling valid service on valid core failed");
569 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
570 "Service core start after add failed");
571 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_start(slcore_id),
572 "Service core expected as running but was stopped");
574 /* ensures core really is running the service function */
575 TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
576 "Service core expected to poll service but it didn't");
579 TEST_ASSERT_EQUAL(-EBUSY, rte_service_lcore_stop(slcore_id),
580 "Service core running a service should return -EBUSY");
581 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
582 "Stopping valid service failed");
583 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_stop(100000),
584 "Invalid Service core stop should return -EINVAL");
585 TEST_ASSERT_EQUAL(0, rte_service_lcore_stop(slcore_id),
586 "Service core stop expected to return 0");
587 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_stop(slcore_id),
588 "Already stopped service core should return -EALREADY");
590 /* ensure service is not longer running */
591 TEST_ASSERT_EQUAL(0, service_lcore_running_check(),
592 "Service core expected to poll service but it didn't");
594 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_id),
595 "Service core del did not return zero");
597 return unregister_all();
600 static struct unit_test_suite service_tests = {
601 .suite_name = "service core test suite",
602 .setup = testsuite_setup,
603 .teardown = testsuite_teardown,
605 TEST_CASE_ST(dummy_register, NULL, unregister_all),
606 TEST_CASE_ST(dummy_register, NULL, service_name),
607 TEST_CASE_ST(dummy_register, NULL, service_get_by_name),
608 TEST_CASE_ST(dummy_register, NULL, service_dump),
609 TEST_CASE_ST(dummy_register, NULL, service_probe_capability),
610 TEST_CASE_ST(dummy_register, NULL, service_start_stop),
611 TEST_CASE_ST(dummy_register, NULL, service_lcore_add_del),
612 TEST_CASE_ST(dummy_register, NULL, service_lcore_start_stop),
613 TEST_CASE_ST(dummy_register, NULL, service_lcore_en_dis_able),
614 TEST_CASE_ST(dummy_register, NULL, service_mt_unsafe_poll),
615 TEST_CASE_ST(dummy_register, NULL, service_mt_safe_poll),
616 TEST_CASES_END() /**< NULL terminate unit test array */
621 test_service_common(void)
623 return unit_test_suite_runner(&service_tests);
626 REGISTER_TEST_COMMAND(service_autotest, test_service_common);