service: add attribute get function
[dpdk.git] / test / test / test_service_cores.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include <rte_common.h>
6 #include <rte_hexdump.h>
7 #include <rte_mbuf.h>
8 #include <rte_malloc.h>
9 #include <rte_memcpy.h>
10 #include <rte_cycles.h>
11
12 #include <rte_service.h>
13 #include <rte_service_component.h>
14
15 #include "test.h"
16
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;
23
24 #define SERVICE_DELAY 1
25
26 #define DUMMY_SERVICE_NAME "dummy_service"
27 #define MT_SAFE_SERVICE_NAME "mt_safe_service"
28
29 static int
30 testsuite_setup(void)
31 {
32         slcore_id = rte_get_next_lcore(/* start core */ -1,
33                                        /* skip master */ 1,
34                                        /* wrap */ 0);
35
36         return TEST_SUCCESS;
37 }
38
39 static void
40 testsuite_teardown(void)
41 {
42         /* release service cores? */
43 }
44
45 static int32_t dummy_cb(void *args)
46 {
47         RTE_SET_USED(args);
48         service_tick++;
49         rte_delay_ms(SERVICE_DELAY);
50         return 0;
51 }
52
53 static int32_t dummy_mt_unsafe_cb(void *args)
54 {
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.
59          */
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);
64         if (lock_taken) {
65                 /* delay with the lock held */
66                 rte_delay_ms(250);
67                 rte_atomic32_clear((rte_atomic32_t *)atomic_lock);
68         } else {
69                 /* 2nd thread will fail to take lock, so set pass flag */
70                 *pass_test = 0;
71         }
72
73         return 0;
74 }
75
76
77 static int32_t dummy_mt_safe_cb(void *args)
78 {
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
84          */
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);
89         if (lock_taken) {
90                 /* delay with the lock held */
91                 rte_delay_ms(250);
92                 rte_atomic32_clear((rte_atomic32_t *)atomic_lock);
93         } else {
94                 /* 2nd thread will fail to take lock, so set pass flag */
95                 *pass_test = 1;
96         }
97
98         return 0;
99 }
100
101 /* unregister all services */
102 static int
103 unregister_all(void)
104 {
105         uint32_t i;
106
107         TEST_ASSERT_EQUAL(-EINVAL, rte_service_component_unregister(1000),
108                         "Unregistered invalid service id");
109
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");
114         }
115
116         rte_service_lcore_reset_all();
117
118         return TEST_SUCCESS;
119 }
120
121 /* register a single dummy service */
122 static int
123 dummy_register(void)
124 {
125         /* make sure there are no remains from previous tests */
126         unregister_all();
127
128         struct rte_service_spec service;
129         memset(&service, 0, sizeof(struct rte_service_spec));
130
131         TEST_ASSERT_EQUAL(-EINVAL,
132                         rte_service_component_register(&service, NULL),
133                         "Invalid callback");
134         service.callback = dummy_cb;
135
136         TEST_ASSERT_EQUAL(-EINVAL,
137                         rte_service_component_register(&service, NULL),
138                         "Invalid name");
139         snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
140
141         uint32_t id;
142         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
143                         "Failed to register valid service");
144
145         rte_service_component_runstate_set(id, 1);
146
147         return TEST_SUCCESS;
148 }
149
150 /* verify get_by_name() service lookup */
151 static int
152 service_get_by_name(void)
153 {
154         unregister_all();
155
156         uint32_t sid;
157         TEST_ASSERT_EQUAL(-ENODEV,
158                         rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
159                         "get by name with invalid name should return -ENODEV");
160         TEST_ASSERT_EQUAL(-EINVAL,
161                         rte_service_get_by_name(DUMMY_SERVICE_NAME, 0x0),
162                         "get by name with NULL ptr should return -ENODEV");
163
164         /* register service */
165         struct rte_service_spec service;
166         memset(&service, 0, sizeof(struct rte_service_spec));
167         TEST_ASSERT_EQUAL(-EINVAL,
168                         rte_service_component_register(&service, NULL),
169                         "Invalid callback");
170         service.callback = dummy_cb;
171         TEST_ASSERT_EQUAL(-EINVAL,
172                         rte_service_component_register(&service, NULL),
173                         "Invalid name");
174         snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
175         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
176                         "Failed to register valid service");
177
178         /* we unregistered all service, now registering 1, should be id 0 */
179         uint32_t service_id_as_expected = 0;
180         TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
181                         "Service get_by_name should return 0 on valid inputs");
182         TEST_ASSERT_EQUAL(service_id_as_expected, sid,
183                         "Service get_by_name should equal expected id");
184
185         unregister_all();
186
187         /* ensure after unregister, get_by_name returns NULL */
188         TEST_ASSERT_EQUAL(-ENODEV,
189                         rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
190                         "get by name should return -ENODEV after unregister");
191
192         return TEST_SUCCESS;
193 }
194
195 /* verify probe of capabilities */
196 static int
197 service_probe_capability(void)
198 {
199         unregister_all();
200
201         struct rte_service_spec service;
202         memset(&service, 0, sizeof(struct rte_service_spec));
203         service.callback = dummy_cb;
204         snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
205         service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
206         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
207                         "Register of MT SAFE service failed");
208
209         /* verify flag is enabled */
210         const uint32_t sid = 0;
211         int32_t mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
212         TEST_ASSERT_EQUAL(1, mt, "MT SAFE capability flag not set.");
213
214
215         unregister_all();
216
217         memset(&service, 0, sizeof(struct rte_service_spec));
218         service.callback = dummy_cb;
219         snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
220         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
221                         "Register of non-MT safe service failed");
222
223         /* verify flag is enabled */
224         mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
225         TEST_ASSERT_EQUAL(0, mt, "MT SAFE cap flag set on non MT SAFE service");
226
227         return unregister_all();
228 }
229
230 /* verify the service name */
231 static int
232 service_name(void)
233 {
234         const char *name = rte_service_get_name(0);
235         int equal = strcmp(name, DUMMY_SERVICE_NAME);
236         TEST_ASSERT_EQUAL(0, equal, "Error: Service name not correct");
237
238         return unregister_all();
239 }
240
241 /* verify service attr get */
242 static int
243 service_attr_get(void)
244 {
245         struct rte_service_spec service;
246         memset(&service, 0, sizeof(struct rte_service_spec));
247         service.callback = dummy_cb;
248         snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
249         service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
250         uint32_t id;
251         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
252                         "Register of  service failed");
253         rte_service_component_runstate_set(id, 1);
254         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(id, 1),
255                         "Error: Service start returned non-zero");
256         rte_service_set_stats_enable(id, 1);
257
258         uint32_t attr_id = UINT32_MAX;
259         uint32_t attr_value = 0xdead;
260         /* check error return values */
261         TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(id, attr_id,
262                                                         &attr_value),
263                         "Invalid attr_id didn't return -EINVAL");
264
265         attr_id = RTE_SERVICE_ATTR_CYCLES;
266         TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(UINT32_MAX, attr_id,
267                                                         &attr_value),
268                         "Invalid service id didn't return -EINVAL");
269
270         TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(id, attr_id, NULL),
271                         "Invalid attr_value pointer id didn't return -EINVAL");
272
273         /* check correct (zero) return value and correct value (zero) */
274         TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_id, &attr_value),
275                         "Valid attr_get() call didn't return success");
276         TEST_ASSERT_EQUAL(0, attr_value,
277                         "attr_get() call didn't set correct cycles (zero)");
278
279         /* Call service to increment cycle count */
280         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
281                         "Service core add did not return zero");
282         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(id, slcore_id, 1),
283                         "Enabling valid service and core failed");
284         TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
285                         "Starting service core failed");
286
287         /* wait for the service lcore to run */
288         rte_delay_ms(200);
289
290         TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_id, &attr_value),
291                         "Valid attr_get() call didn't return success");
292         int cycles_gt_zero = attr_value > 0;
293         TEST_ASSERT_EQUAL(1, cycles_gt_zero,
294                         "attr_get() failed to get cycles (expected > zero)");
295
296         rte_service_lcore_stop(slcore_id);
297
298         return unregister_all();
299 }
300
301 /* verify service dump */
302 static int
303 service_dump(void)
304 {
305         const uint32_t sid = 0;
306         rte_service_set_stats_enable(sid, 1);
307         rte_service_dump(stdout, 0);
308         rte_service_set_stats_enable(sid, 0);
309         rte_service_dump(stdout, 0);
310         return unregister_all();
311 }
312
313 /* start and stop a service */
314 static int
315 service_start_stop(void)
316 {
317         const uint32_t sid = 0;
318
319         /* runstate_get() returns if service is running and slcore is mapped */
320         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
321                         "Service core add did not return zero");
322         int ret = rte_service_map_lcore_set(sid, slcore_id, 1);
323         TEST_ASSERT_EQUAL(0, ret,
324                         "Enabling service core, expected 0 got %d", ret);
325
326         TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
327                         "Error: Service should be stopped");
328
329         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
330                         "Error: Service stopped returned non-zero");
331
332         TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
333                         "Error: Service is running - should be stopped");
334
335         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
336                         "Error: Service start returned non-zero");
337
338         TEST_ASSERT_EQUAL(1, rte_service_runstate_get(sid),
339                         "Error: Service is not running");
340
341         return unregister_all();
342 }
343
344
345 static int
346 service_remote_launch_func(void *arg)
347 {
348         RTE_SET_USED(arg);
349         service_remote_launch_flag = 1;
350         return 0;
351 }
352
353 /* enable and disable a lcore for a service */
354 static int
355 service_lcore_en_dis_able(void)
356 {
357         const uint32_t sid = 0;
358
359         /* expected failure cases */
360         TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 1),
361                         "Enable on invalid core did not fail");
362         TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 0),
363                         "Disable on invalid core did not fail");
364
365         /* add service core to allow enabling */
366         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
367                         "Add service core failed when not in use before");
368
369         /* valid enable */
370         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
371                         "Enabling valid service and core failed");
372         TEST_ASSERT_EQUAL(1, rte_service_map_lcore_get(sid, slcore_id),
373                         "Enabled core returned not-enabled");
374
375         /* valid disable */
376         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 0),
377                         "Disabling valid service and lcore failed");
378         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_get(sid, slcore_id),
379                         "Disabled core returned enabled");
380
381         /* call remote_launch to verify that app can launch ex-service lcore */
382         service_remote_launch_flag = 0;
383         rte_eal_wait_lcore(slcore_id);
384         int ret = rte_eal_remote_launch(service_remote_launch_func, NULL,
385                                         slcore_id);
386         TEST_ASSERT_EQUAL(0, ret, "Ex-service core remote launch failed.");
387         rte_eal_mp_wait_lcore();
388         TEST_ASSERT_EQUAL(1, service_remote_launch_flag,
389                         "Ex-service core function call had no effect.");
390
391         return unregister_all();
392 }
393
394 static int
395 service_lcore_running_check(void)
396 {
397         uint64_t tick = service_tick;
398         rte_delay_ms(SERVICE_DELAY * 100);
399         /* if (tick != service_tick) we know the lcore as polled the service */
400         return tick != service_tick;
401 }
402
403 static int
404 service_lcore_add_del(void)
405 {
406         /* check initial count */
407         TEST_ASSERT_EQUAL(0, rte_service_lcore_count(),
408                         "Service lcore count has value before adding a lcore");
409
410         /* check service lcore add */
411         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
412                         "Add service core failed when not in use before");
413         TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_add(slcore_id),
414                         "Add service core failed to refuse in-use lcore");
415
416         /* check count */
417         TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
418                         "Service core count not equal to one");
419
420         /* retrieve core list, checking lcore ids */
421         const uint32_t size = 4;
422         uint32_t service_core_ids[size];
423         int32_t n = rte_service_lcore_list(service_core_ids, size);
424         TEST_ASSERT_EQUAL(1, n, "Service core list return should equal 1");
425         TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
426                                 "Service core list lcore must equal slcore_id");
427
428         /* recheck count, add more cores, and check count */
429         TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
430                         "Service core count not equal to one");
431         uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
432                                                /* skip master */ 1,
433                                                /* wrap */ 0);
434         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
435                         "Service core add did not return zero");
436         uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
437                                                /* skip master */ 1,
438                                                /* wrap */ 0);
439         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
440                         "Service core add did not return zero");
441
442         uint32_t count = rte_service_lcore_count();
443         const uint32_t cores_at_this_point = 3;
444         TEST_ASSERT_EQUAL(cores_at_this_point, count,
445                         "Service core count %d, expected %d", count,
446                         cores_at_this_point);
447
448         /* check longer service core list */
449         n = rte_service_lcore_list(service_core_ids, size);
450         TEST_ASSERT_EQUAL(3, n, "Service core list return should equal 3");
451         TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
452                                 "Service core list[0] lcore must equal 1");
453         TEST_ASSERT_EQUAL(slcore_1, service_core_ids[1],
454                                 "Service core list[1] lcore must equal 2");
455         TEST_ASSERT_EQUAL(slcore_2, service_core_ids[2],
456                                 "Service core list[2] lcore must equal 3");
457
458         /* recheck count, remove lcores, check remaining lcore_id is correct */
459         TEST_ASSERT_EQUAL(3, rte_service_lcore_count(),
460                         "Service core count not equal to three");
461         TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_1),
462                         "Service core add did not return zero");
463         TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_2),
464                         "Service core add did not return zero");
465         TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
466                         "Service core count not equal to one");
467         n = rte_service_lcore_list(service_core_ids, size);
468         TEST_ASSERT_EQUAL(1, n, "Service core list return should equal one");
469         TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
470                                 "Service core list[0] lcore must equal %d",
471                                 slcore_id);
472
473         return unregister_all();
474 }
475
476 static int
477 service_threaded_test(int mt_safe)
478 {
479         unregister_all();
480
481         /* add next 2 cores */
482         uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
483                                                /* skip master */ 1,
484                                                /* wrap */ 0);
485         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
486                         "mt safe lcore add fail");
487         uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
488                                                /* skip master */ 1,
489                                                /* wrap */ 0);
490         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
491                         "mt safe lcore add fail");
492
493         /* Use atomic locks to verify that two threads are in the same function
494          * at the same time. These are passed to the unit tests through the
495          * callback userdata parameter
496          */
497         uint32_t test_params[2];
498         memset(test_params, 0, sizeof(uint32_t) * 2);
499
500         /* register MT safe service. */
501         struct rte_service_spec service;
502         memset(&service, 0, sizeof(struct rte_service_spec));
503         service.callback_userdata = test_params;
504         snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
505
506         if (mt_safe) {
507                 service.callback = dummy_mt_safe_cb;
508                 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
509         } else
510                 service.callback = dummy_mt_unsafe_cb;
511
512         uint32_t id;
513         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
514                         "Register of MT SAFE service failed");
515
516         const uint32_t sid = 0;
517         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
518                         "Starting valid service failed");
519         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_1, 1),
520                         "Failed to enable lcore 1 on mt safe service");
521         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_2, 1),
522                         "Failed to enable lcore 2 on mt safe service");
523         rte_service_lcore_start(slcore_1);
524         rte_service_lcore_start(slcore_2);
525
526         /* wait for the worker threads to run */
527         rte_delay_ms(500);
528         rte_service_lcore_stop(slcore_1);
529         rte_service_lcore_stop(slcore_2);
530
531         TEST_ASSERT_EQUAL(0, test_params[1],
532                         "Service run with component runstate = 0");
533
534         /* enable backend runstate: the service should run after this */
535         rte_service_component_runstate_set(id, 1);
536
537         /* initialize to pass, see callback comment for details */
538         if (!mt_safe)
539                 test_params[1] = 1;
540
541         /* wait for lcores before start() */
542         rte_eal_wait_lcore(slcore_1);
543         rte_eal_wait_lcore(slcore_2);
544
545         rte_service_lcore_start(slcore_1);
546         rte_service_lcore_start(slcore_2);
547
548         /* wait for the worker threads to run */
549         rte_delay_ms(500);
550         rte_service_lcore_stop(slcore_1);
551         rte_service_lcore_stop(slcore_2);
552
553         TEST_ASSERT_EQUAL(1, test_params[1],
554                         "MT Safe service not run by two cores concurrently");
555         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
556                         "Failed to stop MT Safe service");
557
558         rte_eal_wait_lcore(slcore_1);
559         rte_eal_wait_lcore(slcore_2);
560         unregister_all();
561
562         /* return the value of the callback pass_test variable to caller */
563         return test_params[1];
564 }
565
566 /* tests an MT SAFE service with two cores. The callback function ensures that
567  * two threads access the callback concurrently.
568  */
569 static int
570 service_mt_safe_poll(void)
571 {
572         int mt_safe = 1;
573         TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
574                         "Error: MT Safe service not run by two cores concurrently");
575         return TEST_SUCCESS;
576 }
577
578 /* tests a NON mt safe service with two cores, the callback is serialized
579  * using the atomic cmpset.
580  */
581 static int
582 service_mt_unsafe_poll(void)
583 {
584         int mt_safe = 0;
585         TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
586                         "Error: NON MT Safe service run by two cores concurrently");
587         return TEST_SUCCESS;
588 }
589
590 static int32_t
591 delay_as_a_mt_safe_service(void *args)
592 {
593         RTE_SET_USED(args);
594         uint32_t *params = args;
595
596         /* retrieve done flag and atomic lock to inc/dec */
597         uint32_t *done = &params[0];
598         rte_atomic32_t *lock = (rte_atomic32_t *)&params[1];
599
600         while (!*done) {
601                 rte_atomic32_inc(lock);
602                 rte_delay_us(500);
603                 if (rte_atomic32_read(lock) > 1)
604                         /* pass: second core has simultaneously incremented */
605                         *done = 1;
606                 rte_atomic32_dec(lock);
607         }
608
609         return 0;
610 }
611
612 static int32_t
613 delay_as_a_service(void *args)
614 {
615         uint32_t *done = (uint32_t *)args;
616         while (!*done)
617                 rte_delay_ms(5);
618         return 0;
619 }
620
621 static int
622 service_run_on_app_core_func(void *arg)
623 {
624         uint32_t *delay_service_id = (uint32_t *)arg;
625         return rte_service_run_iter_on_app_lcore(*delay_service_id, 1);
626 }
627
628 static int
629 service_app_lcore_poll_impl(const int mt_safe)
630 {
631         uint32_t params[2] = {0};
632
633         struct rte_service_spec service;
634         memset(&service, 0, sizeof(struct rte_service_spec));
635         snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
636         if (mt_safe) {
637                 service.callback = delay_as_a_mt_safe_service;
638                 service.callback_userdata = params;
639                 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
640         } else {
641                 service.callback = delay_as_a_service;
642                 service.callback_userdata = &params;
643         }
644
645         uint32_t id;
646         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
647                         "Register of app lcore delay service failed");
648
649         rte_service_component_runstate_set(id, 1);
650         rte_service_runstate_set(id, 1);
651
652         uint32_t app_core2 = rte_get_next_lcore(slcore_id, 1, 1);
653         rte_eal_wait_lcore(app_core2);
654         int app_core2_ret = rte_eal_remote_launch(service_run_on_app_core_func,
655                                                   &id, app_core2);
656
657         rte_delay_ms(100);
658
659         int app_core1_ret = service_run_on_app_core_func(&id);
660
661         /* flag done, then wait for the spawned 2nd core to return */
662         params[0] = 1;
663         rte_eal_mp_wait_lcore();
664
665         /* core two gets launched first - and should hold the service lock */
666         TEST_ASSERT_EQUAL(0, app_core2_ret,
667                         "App core2 : run service didn't return zero");
668
669         if (mt_safe) {
670                 /* mt safe should have both cores return 0 for success */
671                 TEST_ASSERT_EQUAL(0, app_core1_ret,
672                                 "MT Safe: App core1 didn't return 0");
673         } else {
674                 /* core one attempts to run later - should be blocked */
675                 TEST_ASSERT_EQUAL(-EBUSY, app_core1_ret,
676                                 "MT Unsafe: App core1 didn't return -EBUSY");
677         }
678
679         unregister_all();
680
681         return TEST_SUCCESS;
682 }
683
684 static int
685 service_app_lcore_mt_safe(void)
686 {
687         const int mt_safe = 1;
688         return service_app_lcore_poll_impl(mt_safe);
689 }
690
691 static int
692 service_app_lcore_mt_unsafe(void)
693 {
694         const int mt_safe = 0;
695         return service_app_lcore_poll_impl(mt_safe);
696 }
697
698 /* start and stop a service core - ensuring it goes back to sleep */
699 static int
700 service_lcore_start_stop(void)
701 {
702         /* start service core and service, create mapping so tick() runs */
703         const uint32_t sid = 0;
704         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
705                         "Starting valid service failed");
706         TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, slcore_id, 1),
707                         "Enabling valid service on non-service core must fail");
708
709         /* core start */
710         TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_start(slcore_id),
711                         "Service core start without add should return EINVAL");
712         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
713                         "Service core add did not return zero");
714         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
715                         "Enabling valid service on valid core failed");
716         TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
717                         "Service core start after add failed");
718         TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_start(slcore_id),
719                         "Service core expected as running but was stopped");
720
721         /* ensures core really is running the service function */
722         TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
723                         "Service core expected to poll service but it didn't");
724
725         /* core stop */
726         TEST_ASSERT_EQUAL(-EBUSY, rte_service_lcore_stop(slcore_id),
727                         "Service core running a service should return -EBUSY");
728         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
729                         "Stopping valid service failed");
730         TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_stop(100000),
731                         "Invalid Service core stop should return -EINVAL");
732         TEST_ASSERT_EQUAL(0, rte_service_lcore_stop(slcore_id),
733                         "Service core stop expected to return 0");
734         TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_stop(slcore_id),
735                         "Already stopped service core should return -EALREADY");
736
737         /* ensure service is not longer running */
738         TEST_ASSERT_EQUAL(0, service_lcore_running_check(),
739                         "Service core expected to poll service but it didn't");
740
741         TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_id),
742                         "Service core del did not return zero");
743
744         return unregister_all();
745 }
746
747 static struct unit_test_suite service_tests  = {
748         .suite_name = "service core test suite",
749         .setup = testsuite_setup,
750         .teardown = testsuite_teardown,
751         .unit_test_cases = {
752                 TEST_CASE_ST(dummy_register, NULL, unregister_all),
753                 TEST_CASE_ST(dummy_register, NULL, service_name),
754                 TEST_CASE_ST(dummy_register, NULL, service_get_by_name),
755                 TEST_CASE_ST(dummy_register, NULL, service_dump),
756                 TEST_CASE_ST(dummy_register, NULL, service_attr_get),
757                 TEST_CASE_ST(dummy_register, NULL, service_probe_capability),
758                 TEST_CASE_ST(dummy_register, NULL, service_start_stop),
759                 TEST_CASE_ST(dummy_register, NULL, service_lcore_add_del),
760                 TEST_CASE_ST(dummy_register, NULL, service_lcore_start_stop),
761                 TEST_CASE_ST(dummy_register, NULL, service_lcore_en_dis_able),
762                 TEST_CASE_ST(dummy_register, NULL, service_mt_unsafe_poll),
763                 TEST_CASE_ST(dummy_register, NULL, service_mt_safe_poll),
764                 TEST_CASE_ST(dummy_register, NULL, service_app_lcore_mt_safe),
765                 TEST_CASE_ST(dummy_register, NULL, service_app_lcore_mt_unsafe),
766                 TEST_CASES_END() /**< NULL terminate unit test array */
767         }
768 };
769
770 static int
771 test_service_common(void)
772 {
773         return unit_test_suite_runner(&service_tests);
774 }
775
776 REGISTER_TEST_COMMAND(service_autotest, test_service_common);