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,
164 rte_service_component_register(&service, NULL),
166 service.callback = dummy_cb;
168 TEST_ASSERT_EQUAL(-EINVAL,
169 rte_service_component_register(&service, NULL),
171 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
173 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
174 "Failed to register valid service");
179 /* verify get_by_name() service lookup */
181 service_get_by_name(void)
185 /* ensure with no services registered returns NULL */
186 TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME),
187 "Service get by name should return NULL");
189 /* register service */
190 struct rte_service_spec service;
191 memset(&service, 0, sizeof(struct rte_service_spec));
192 TEST_ASSERT_EQUAL(-EINVAL,
193 rte_service_component_register(&service, NULL),
195 service.callback = dummy_cb;
196 TEST_ASSERT_EQUAL(-EINVAL,
197 rte_service_component_register(&service, NULL),
199 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
200 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
201 "Failed to register valid service");
203 /* ensure with dummy services registered returns same ptr as ID */
204 struct rte_service_spec *s_by_id = rte_service_get_by_id(0);
205 TEST_ASSERT_EQUAL(s_by_id, rte_service_get_by_name(DUMMY_SERVICE_NAME),
206 "Service get_by_name should equal get_by_id()");
210 /* ensure after unregister, get_by_name returns NULL */
211 TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME),
212 "get by name should return NULL after unregister");
217 /* verify probe of capabilities */
219 service_probe_capability(void)
223 struct rte_service_spec service;
224 memset(&service, 0, sizeof(struct rte_service_spec));
225 service.callback = dummy_cb;
226 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
227 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
228 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
229 "Register of MT SAFE service failed");
231 /* verify flag is enabled */
232 const uint32_t sid = 0;
233 int32_t mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
234 TEST_ASSERT_EQUAL(1, mt, "MT SAFE capability flag not set.");
239 memset(&service, 0, sizeof(struct rte_service_spec));
240 service.callback = dummy_cb;
241 snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
242 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
243 "Register of non-MT safe service failed");
245 /* verify flag is enabled */
246 mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
247 TEST_ASSERT_EQUAL(0, mt, "MT SAFE cap flag set on non MT SAFE service");
249 return unregister_all();
252 /* verify the service name */
256 struct rte_service_spec *service = rte_service_get_by_id(0);
258 int equal = strcmp(service->name, DUMMY_SERVICE_NAME);
259 TEST_ASSERT_EQUAL(0, equal, "Error: Service name not correct");
261 return unregister_all();
264 /* verify service dump */
268 const uint32_t sid = 0;
269 rte_service_set_stats_enable(sid, 1);
270 rte_service_dump(stdout, 0);
271 rte_service_set_stats_enable(sid, 0);
272 rte_service_dump(stdout, 0);
273 return unregister_all();
276 /* start and stop a service */
278 service_start_stop(void)
280 const uint32_t sid = 0;
282 /* runstate_get() returns if service is running and slcore is mapped */
283 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
284 "Service core add did not return zero");
285 int ret = rte_service_map_lcore_set(sid, slcore_id, 1);
286 TEST_ASSERT_EQUAL(0, ret,
287 "Enabling service core, expected 0 got %d", ret);
289 TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
290 "Error: Service should be stopped");
292 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
293 "Error: Service stopped returned non-zero");
295 TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
296 "Error: Service is running - should be stopped");
298 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
299 "Error: Service start returned non-zero");
301 TEST_ASSERT_EQUAL(1, rte_service_runstate_get(sid),
302 "Error: Service is not running");
304 return unregister_all();
309 service_remote_launch_func(void *arg)
312 service_remote_launch_flag = 1;
316 /* enable and disable a lcore for a service */
318 service_lcore_en_dis_able(void)
320 const uint32_t sid = 0;
322 /* expected failure cases */
323 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 1),
324 "Enable on invalid core did not fail");
325 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 0),
326 "Disable on invalid core did not fail");
328 /* add service core to allow enabling */
329 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
330 "Add service core failed when not in use before");
333 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
334 "Enabling valid service and core failed");
335 TEST_ASSERT_EQUAL(1, rte_service_map_lcore_get(sid, slcore_id),
336 "Enabled core returned not-enabled");
339 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 0),
340 "Disabling valid service and lcore failed");
341 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_get(sid, slcore_id),
342 "Disabled core returned enabled");
344 /* call remote_launch to verify that app can launch ex-service lcore */
345 service_remote_launch_flag = 0;
346 int ret = rte_eal_remote_launch(service_remote_launch_func, NULL,
348 TEST_ASSERT_EQUAL(0, ret, "Ex-service core remote launch failed.");
349 rte_eal_mp_wait_lcore();
350 TEST_ASSERT_EQUAL(1, service_remote_launch_flag,
351 "Ex-service core function call had no effect.");
353 return unregister_all();
357 service_lcore_running_check(void)
359 uint64_t tick = service_tick;
360 rte_delay_ms(SERVICE_DELAY * 10);
361 /* if (tick != service_tick) we know the lcore as polled the service */
362 return tick != service_tick;
366 service_lcore_add_del(void)
368 /* check initial count */
369 TEST_ASSERT_EQUAL(0, rte_service_lcore_count(),
370 "Service lcore count has value before adding a lcore");
372 /* check service lcore add */
373 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
374 "Add service core failed when not in use before");
375 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_add(slcore_id),
376 "Add service core failed to refuse in-use lcore");
379 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
380 "Service core count not equal to one");
382 /* retrieve core list, checking lcore ids */
383 const uint32_t size = 4;
384 uint32_t service_core_ids[size];
385 int32_t n = rte_service_lcore_list(service_core_ids, size);
386 TEST_ASSERT_EQUAL(1, n, "Service core list return should equal 1");
387 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
388 "Service core list lcore must equal slcore_id");
390 /* recheck count, add more cores, and check count */
391 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
392 "Service core count not equal to one");
393 uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
396 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
397 "Service core add did not return zero");
398 uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
401 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
402 "Service core add did not return zero");
404 uint32_t count = rte_service_lcore_count();
405 const uint32_t cores_at_this_point = 3;
406 TEST_ASSERT_EQUAL(cores_at_this_point, count,
407 "Service core count %d, expected %d", count,
408 cores_at_this_point);
410 /* check longer service core list */
411 n = rte_service_lcore_list(service_core_ids, size);
412 TEST_ASSERT_EQUAL(3, n, "Service core list return should equal 3");
413 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
414 "Service core list[0] lcore must equal 1");
415 TEST_ASSERT_EQUAL(slcore_1, service_core_ids[1],
416 "Service core list[1] lcore must equal 2");
417 TEST_ASSERT_EQUAL(slcore_2, service_core_ids[2],
418 "Service core list[2] lcore must equal 3");
420 /* recheck count, remove lcores, check remaining lcore_id is correct */
421 TEST_ASSERT_EQUAL(3, rte_service_lcore_count(),
422 "Service core count not equal to three");
423 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_1),
424 "Service core add did not return zero");
425 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_2),
426 "Service core add did not return zero");
427 TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
428 "Service core count not equal to one");
429 n = rte_service_lcore_list(service_core_ids, size);
430 TEST_ASSERT_EQUAL(1, n, "Service core list return should equal one");
431 TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
432 "Service core list[0] lcore must equal %d",
435 return unregister_all();
439 service_threaded_test(int mt_safe)
443 /* add next 2 cores */
444 uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
447 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
448 "mt safe lcore add fail");
449 uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
452 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
453 "mt safe lcore add fail");
455 /* Use atomic locks to verify that two threads are in the same function
456 * at the same time. These are passed to the unit tests through the
457 * callback userdata parameter
459 uint32_t test_params[2];
460 memset(test_params, 0, sizeof(uint32_t) * 2);
462 /* register MT safe service. */
463 struct rte_service_spec service;
464 memset(&service, 0, sizeof(struct rte_service_spec));
465 service.callback_userdata = test_params;
466 snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
469 service.callback = dummy_mt_safe_cb;
470 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
472 /* initialize to pass, see callback comment for details */
474 service.callback = dummy_mt_unsafe_cb;
477 TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
478 "Register of MT SAFE service failed");
480 const uint32_t sid = 0;
481 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
482 "Starting valid service failed");
483 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_1, 1),
484 "Failed to enable lcore 1 on mt safe service");
485 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_2, 1),
486 "Failed to enable lcore 2 on mt safe service");
487 rte_service_lcore_start(slcore_1);
488 rte_service_lcore_start(slcore_2);
490 /* wait for the worker threads to run */
492 rte_service_lcore_stop(slcore_1);
493 rte_service_lcore_stop(slcore_2);
495 TEST_ASSERT_EQUAL(1, test_params[1],
496 "MT Safe service not run by two cores concurrently");
498 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
499 "Failed to stop MT Safe service");
503 /* return the value of the callback pass_test variable to caller */
504 return test_params[1];
507 /* tests an MT SAFE service with two cores. The callback function ensures that
508 * two threads access the callback concurrently.
511 service_mt_safe_poll(void)
514 TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
515 "Error: MT Safe service not run by two cores concurrently");
519 /* tests a NON mt safe service with two cores, the callback is serialized
520 * using the atomic cmpset.
523 service_mt_unsafe_poll(void)
526 TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
527 "Error: NON MT Safe service run by two cores concurrently");
531 /* start and stop a service core - ensuring it goes back to sleep */
533 service_lcore_start_stop(void)
535 /* start service core and service, create mapping so tick() runs */
536 const uint32_t sid = 0;
537 TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
538 "Starting valid service failed");
539 TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, slcore_id, 1),
540 "Enabling valid service on non-service core must fail");
543 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_start(slcore_id),
544 "Service core start without add should return EINVAL");
545 TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
546 "Service core add did not return zero");
547 TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
548 "Enabling valid service on valid core failed");
549 TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
550 "Service core start after add failed");
551 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_start(slcore_id),
552 "Service core expected as running but was stopped");
554 /* ensures core really is running the service function */
555 TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
556 "Service core expected to poll service but it didn't");
559 TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_stop(100000),
560 "Invalid Service core stop should return -EINVAL");
561 TEST_ASSERT_EQUAL(0, rte_service_lcore_stop(slcore_id),
562 "Service core stop expected to return 0");
563 TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_stop(slcore_id),
564 "Already stopped service core should return -EALREADY");
566 /* ensure service is not longer running */
567 TEST_ASSERT_EQUAL(0, service_lcore_running_check(),
568 "Service core expected to poll service but it didn't");
570 TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_id),
571 "Service core del did not return zero");
573 return unregister_all();
576 static struct unit_test_suite service_tests = {
577 .suite_name = "service core test suite",
578 .setup = testsuite_setup,
579 .teardown = testsuite_teardown,
581 TEST_CASE_ST(dummy_register, NULL, unregister_all),
582 TEST_CASE_ST(dummy_register, NULL, service_name),
583 TEST_CASE_ST(dummy_register, NULL, service_get_by_name),
584 TEST_CASE_ST(dummy_register, NULL, service_dump),
585 TEST_CASE_ST(dummy_register, NULL, service_probe_capability),
586 TEST_CASE_ST(dummy_register, NULL, service_start_stop),
587 TEST_CASE_ST(dummy_register, NULL, service_lcore_add_del),
588 TEST_CASE_ST(dummy_register, NULL, service_lcore_start_stop),
589 TEST_CASE_ST(dummy_register, NULL, service_lcore_en_dis_able),
590 TEST_CASE_ST(dummy_register, NULL, service_mt_unsafe_poll),
591 TEST_CASE_ST(dummy_register, NULL, service_mt_safe_poll),
592 TEST_CASES_END() /**< NULL terminate unit test array */
597 test_service_common(void)
599 return unit_test_suite_runner(&service_tests);
602 REGISTER_TEST_COMMAND(service_autotest, test_service_common);