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