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