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);
169 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
170 "Failed to register valid service");
175 /* verify get_by_name() service lookup */
177 service_get_by_name(void)
181 /* ensure with no services registered returns NULL */
182 TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME),
183 "Service get by name should return NULL");
185 /* register service */
186 struct rte_service_spec service;
187 memset(&service, 0, sizeof(struct rte_service_spec));
188 TEST_ASSERT_EQUAL(-EINVAL,
189 rte_service_component_register(&service, NULL),
191 service.callback = dummy_cb;
192 TEST_ASSERT_EQUAL(-EINVAL,
193 rte_service_component_register(&service, NULL),
195 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
196 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
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_component_register(&service, NULL),
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_component_register(&service, NULL),
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 const uint32_t sid = 0;
265 rte_service_set_stats_enable(sid, 1);
266 rte_service_dump(stdout, 0);
267 rte_service_set_stats_enable(sid, 0);
268 rte_service_dump(stdout, 0);
269 return unregister_all();
272 /* start and stop a service */
274 service_start_stop(void)
276 const uint32_t sid = 0;
278 /* runstate_get() returns if service is running and slcore is mapped */
279 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
280 "Service core add did not return zero");
281 int ret = rte_service_map_lcore_set(sid, slcore_id, 1);
282 TEST_ASSERT_EQUAL(0, ret,
283 "Enabling service core, expected 0 got %d", ret);
285 TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
286 "Error: Service should be stopped");
288 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
289 "Error: Service stopped returned non-zero");
291 TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
292 "Error: Service is running - should be stopped");
294 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
295 "Error: Service start returned non-zero");
297 TEST_ASSERT_EQUAL(1, rte_service_runstate_get(sid),
298 "Error: Service is not running");
300 return unregister_all();
305 service_remote_launch_func(void *arg)
308 service_remote_launch_flag = 1;
312 /* enable and disable a lcore for a service */
314 service_lcore_en_dis_able(void)
316 const uint32_t sid = 0;
318 /* expected failure cases */
319 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 1),
320 "Enable on invalid core did not fail");
321 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 0),
322 "Disable on invalid core did not fail");
324 /* add service core to allow enabling */
325 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
326 "Add service core failed when not in use before");
329 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
330 "Enabling valid service and core failed");
331 TEST_ASSERT_EQUAL(1, rte_service_map_lcore_get(sid, slcore_id),
332 "Enabled core returned not-enabled");
335 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 0),
336 "Disabling valid service and lcore failed");
337 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_get(sid, slcore_id),
338 "Disabled core returned enabled");
340 /* call remote_launch to verify that app can launch ex-service lcore */
341 service_remote_launch_flag = 0;
342 int ret = rte_eal_remote_launch(service_remote_launch_func, NULL,
344 TEST_ASSERT_EQUAL(0, ret, "Ex-service core remote launch failed.");
345 rte_eal_mp_wait_lcore();
346 TEST_ASSERT_EQUAL(1, service_remote_launch_flag,
347 "Ex-service core function call had no effect.");
349 return unregister_all();
353 service_lcore_running_check(void)
355 uint64_t tick = service_tick;
356 rte_delay_ms(SERVICE_DELAY * 10);
357 /* if (tick != service_tick) we know the lcore as polled the service */
358 return tick != service_tick;
362 service_lcore_add_del(void)
364 /* check initial count */
365 TEST_ASSERT_EQUAL(0, rte_service_lcore_count(),
366 "Service lcore count has value before adding a lcore");
368 /* check service lcore add */
369 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
370 "Add service core failed when not in use before");
371 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_add(slcore_id),
372 "Add service core failed to refuse in-use lcore");
375 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
376 "Service core count not equal to one");
378 /* retrieve core list, checking lcore ids */
379 const uint32_t size = 4;
380 uint32_t service_core_ids[size];
381 int32_t n = rte_service_lcore_list(service_core_ids, size);
382 TEST_ASSERT_EQUAL(1, n, "Service core list return should equal 1");
383 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
384 "Service core list lcore must equal slcore_id");
386 /* recheck count, add more cores, and check count */
387 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
388 "Service core count not equal to one");
389 uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
392 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
393 "Service core add did not return zero");
394 uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
397 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
398 "Service core add did not return zero");
400 uint32_t count = rte_service_lcore_count();
401 const uint32_t cores_at_this_point = 3;
402 TEST_ASSERT_EQUAL(cores_at_this_point, count,
403 "Service core count %d, expected %d", count,
404 cores_at_this_point);
406 /* check longer service core list */
407 n = rte_service_lcore_list(service_core_ids, size);
408 TEST_ASSERT_EQUAL(3, n, "Service core list return should equal 3");
409 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
410 "Service core list[0] lcore must equal 1");
411 TEST_ASSERT_EQUAL(slcore_1, service_core_ids[1],
412 "Service core list[1] lcore must equal 2");
413 TEST_ASSERT_EQUAL(slcore_2, service_core_ids[2],
414 "Service core list[2] lcore must equal 3");
416 /* recheck count, remove lcores, check remaining lcore_id is correct */
417 TEST_ASSERT_EQUAL(3, rte_service_lcore_count(),
418 "Service core count not equal to three");
419 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_1),
420 "Service core add did not return zero");
421 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_2),
422 "Service core add did not return zero");
423 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
424 "Service core count not equal to one");
425 n = rte_service_lcore_list(service_core_ids, size);
426 TEST_ASSERT_EQUAL(1, n, "Service core list return should equal one");
427 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
428 "Service core list[0] lcore must equal %d",
431 return unregister_all();
435 service_threaded_test(int mt_safe)
439 /* add next 2 cores */
440 uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
443 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
444 "mt safe lcore add fail");
445 uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
448 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
449 "mt safe lcore add fail");
451 /* Use atomic locks to verify that two threads are in the same function
452 * at the same time. These are passed to the unit tests through the
453 * callback userdata parameter
455 uint32_t test_params[2];
456 memset(test_params, 0, sizeof(uint32_t) * 2);
458 /* register MT safe service. */
459 struct rte_service_spec service;
460 memset(&service, 0, sizeof(struct rte_service_spec));
461 service.callback_userdata = test_params;
462 snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
465 service.callback = dummy_mt_safe_cb;
466 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
468 /* initialize to pass, see callback comment for details */
470 service.callback = dummy_mt_unsafe_cb;
473 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
474 "Register of MT SAFE service failed");
476 const uint32_t sid = 0;
477 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
478 "Starting valid service failed");
479 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_1, 1),
480 "Failed to enable lcore 1 on mt safe service");
481 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_2, 1),
482 "Failed to enable lcore 2 on mt safe service");
483 rte_service_lcore_start(slcore_1);
484 rte_service_lcore_start(slcore_2);
486 /* wait for the worker threads to run */
488 rte_service_lcore_stop(slcore_1);
489 rte_service_lcore_stop(slcore_2);
491 TEST_ASSERT_EQUAL(1, test_params[1],
492 "MT Safe service not run by two cores concurrently");
494 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
495 "Failed to stop MT Safe service");
499 /* return the value of the callback pass_test variable to caller */
500 return test_params[1];
503 /* tests an MT SAFE service with two cores. The callback function ensures that
504 * two threads access the callback concurrently.
507 service_mt_safe_poll(void)
510 TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
511 "Error: MT Safe service not run by two cores concurrently");
515 /* tests a NON mt safe service with two cores, the callback is serialized
516 * using the atomic cmpset.
519 service_mt_unsafe_poll(void)
522 TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
523 "Error: NON MT Safe service run by two cores concurrently");
527 /* start and stop a service core - ensuring it goes back to sleep */
529 service_lcore_start_stop(void)
531 /* start service core and service, create mapping so tick() runs */
532 const uint32_t sid = 0;
533 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
534 "Starting valid service failed");
535 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, slcore_id, 1),
536 "Enabling valid service on non-service core must fail");
539 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_start(slcore_id),
540 "Service core start without add should return EINVAL");
541 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
542 "Service core add did not return zero");
543 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
544 "Enabling valid service on valid core failed");
545 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
546 "Service core start after add failed");
547 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_start(slcore_id),
548 "Service core expected as running but was stopped");
550 /* ensures core really is running the service function */
551 TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
552 "Service core expected to poll service but it didn't");
555 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_stop(100000),
556 "Invalid Service core stop should return -EINVAL");
557 TEST_ASSERT_EQUAL(0, rte_service_lcore_stop(slcore_id),
558 "Service core stop expected to return 0");
559 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_stop(slcore_id),
560 "Already stopped service core should return -EALREADY");
562 /* ensure service is not longer running */
563 TEST_ASSERT_EQUAL(0, service_lcore_running_check(),
564 "Service core expected to poll service but it didn't");
566 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_id),
567 "Service core del did not return zero");
569 return unregister_all();
572 static struct unit_test_suite service_tests = {
573 .suite_name = "service core test suite",
574 .setup = testsuite_setup,
575 .teardown = testsuite_teardown,
577 TEST_CASE_ST(dummy_register, NULL, unregister_all),
578 TEST_CASE_ST(dummy_register, NULL, service_name),
579 TEST_CASE_ST(dummy_register, NULL, service_get_by_name),
580 TEST_CASE_ST(dummy_register, NULL, service_dump),
581 TEST_CASE_ST(dummy_register, NULL, service_probe_capability),
582 TEST_CASE_ST(dummy_register, NULL, service_start_stop),
583 TEST_CASE_ST(dummy_register, NULL, service_lcore_add_del),
584 TEST_CASE_ST(dummy_register, NULL, service_lcore_start_stop),
585 TEST_CASE_ST(dummy_register, NULL, service_lcore_en_dis_able),
586 TEST_CASE_ST(dummy_register, NULL, service_mt_unsafe_poll),
587 TEST_CASE_ST(dummy_register, NULL, service_mt_safe_poll),
588 TEST_CASES_END() /**< NULL terminate unit test array */
593 test_service_common(void)
595 return unit_test_suite_runner(&service_tests);
598 REGISTER_TEST_COMMAND(service_autotest, test_service_common);