test mbuf attach
[dpdk.git] / app / 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         rte_eal_mp_wait_lcore();
118
119         return TEST_SUCCESS;
120 }
121
122 /* register a single dummy service */
123 static int
124 dummy_register(void)
125 {
126         /* make sure there are no remains from previous tests */
127         unregister_all();
128
129         struct rte_service_spec service;
130         memset(&service, 0, sizeof(struct rte_service_spec));
131
132         TEST_ASSERT_EQUAL(-EINVAL,
133                         rte_service_component_register(&service, NULL),
134                         "Invalid callback");
135         service.callback = dummy_cb;
136
137         TEST_ASSERT_EQUAL(-EINVAL,
138                         rte_service_component_register(&service, NULL),
139                         "Invalid name");
140         snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
141
142         uint32_t id;
143         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
144                         "Failed to register valid service");
145
146         rte_service_component_runstate_set(id, 1);
147
148         return TEST_SUCCESS;
149 }
150
151 /* verify get_by_name() service lookup */
152 static int
153 service_get_by_name(void)
154 {
155         unregister_all();
156
157         uint32_t sid;
158         TEST_ASSERT_EQUAL(-ENODEV,
159                         rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
160                         "get by name with invalid name should return -ENODEV");
161         TEST_ASSERT_EQUAL(-EINVAL,
162                         rte_service_get_by_name(DUMMY_SERVICE_NAME, 0x0),
163                         "get by name with NULL ptr should return -ENODEV");
164
165         /* register service */
166         struct rte_service_spec service;
167         memset(&service, 0, sizeof(struct rte_service_spec));
168         TEST_ASSERT_EQUAL(-EINVAL,
169                         rte_service_component_register(&service, NULL),
170                         "Invalid callback");
171         service.callback = dummy_cb;
172         TEST_ASSERT_EQUAL(-EINVAL,
173                         rte_service_component_register(&service, NULL),
174                         "Invalid name");
175         snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
176         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
177                         "Failed to register valid service");
178
179         /* we unregistered all service, now registering 1, should be id 0 */
180         uint32_t service_id_as_expected = 0;
181         TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
182                         "Service get_by_name should return 0 on valid inputs");
183         TEST_ASSERT_EQUAL(service_id_as_expected, sid,
184                         "Service get_by_name should equal expected id");
185
186         unregister_all();
187
188         /* ensure after unregister, get_by_name returns NULL */
189         TEST_ASSERT_EQUAL(-ENODEV,
190                         rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
191                         "get by name should return -ENODEV after unregister");
192
193         return TEST_SUCCESS;
194 }
195
196 /* verify probe of capabilities */
197 static int
198 service_probe_capability(void)
199 {
200         unregister_all();
201
202         struct rte_service_spec service;
203         memset(&service, 0, sizeof(struct rte_service_spec));
204         service.callback = dummy_cb;
205         snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
206         service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
207         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
208                         "Register of MT SAFE service failed");
209
210         /* verify flag is enabled */
211         const uint32_t sid = 0;
212         int32_t mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
213         TEST_ASSERT_EQUAL(1, mt, "MT SAFE capability flag not set.");
214
215
216         unregister_all();
217
218         memset(&service, 0, sizeof(struct rte_service_spec));
219         service.callback = dummy_cb;
220         snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
221         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
222                         "Register of non-MT safe service failed");
223
224         /* verify flag is enabled */
225         mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
226         TEST_ASSERT_EQUAL(0, mt, "MT SAFE cap flag set on non MT SAFE service");
227
228         return unregister_all();
229 }
230
231 /* verify the service name */
232 static int
233 service_name(void)
234 {
235         const char *name = rte_service_get_name(0);
236         int equal = strcmp(name, DUMMY_SERVICE_NAME);
237         TEST_ASSERT_EQUAL(0, equal, "Error: Service name not correct");
238
239         return unregister_all();
240 }
241
242 /* verify service attr get */
243 static int
244 service_attr_get(void)
245 {
246         /* ensure all services unregistered so cycle counts are zero */
247         unregister_all();
248
249         struct rte_service_spec service;
250         memset(&service, 0, sizeof(struct rte_service_spec));
251         service.callback = dummy_cb;
252         snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
253         service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
254         uint32_t id;
255         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
256                         "Register of  service failed");
257         rte_service_component_runstate_set(id, 1);
258         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(id, 1),
259                         "Error: Service start returned non-zero");
260         rte_service_set_stats_enable(id, 1);
261
262         uint32_t attr_id = UINT32_MAX;
263         uint64_t attr_value = 0xdead;
264         /* check error return values */
265         TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(id, attr_id,
266                                                         &attr_value),
267                         "Invalid attr_id didn't return -EINVAL");
268
269         attr_id = RTE_SERVICE_ATTR_CYCLES;
270         TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(UINT32_MAX, attr_id,
271                                                         &attr_value),
272                         "Invalid service id didn't return -EINVAL");
273
274         TEST_ASSERT_EQUAL(-EINVAL, rte_service_attr_get(id, attr_id, NULL),
275                         "Invalid attr_value pointer id didn't return -EINVAL");
276
277         /* check correct (zero) return value and correct value (zero) */
278         TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_id, &attr_value),
279                         "Valid attr_get() call didn't return success");
280         TEST_ASSERT_EQUAL(0, attr_value,
281                         "attr_get() call didn't set correct cycles (zero)");
282         /* check correct call count */
283         const int attr_calls = RTE_SERVICE_ATTR_CALL_COUNT;
284         TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_calls, &attr_value),
285                         "Valid attr_get() call didn't return success");
286         TEST_ASSERT_EQUAL(0, attr_value,
287                         "attr_get() call didn't get call count (zero)");
288
289         /* Call service to increment cycle count */
290         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
291                         "Service core add did not return zero");
292         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(id, slcore_id, 1),
293                         "Enabling valid service and core failed");
294         TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
295                         "Starting service core failed");
296
297         /* wait for the service lcore to run */
298         rte_delay_ms(200);
299
300         TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_id, &attr_value),
301                         "Valid attr_get() call didn't return success");
302         int cycles_gt_zero = attr_value > 0;
303         TEST_ASSERT_EQUAL(1, cycles_gt_zero,
304                         "attr_get() failed to get cycles (expected > zero)");
305
306         rte_service_lcore_stop(slcore_id);
307
308         TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_calls, &attr_value),
309                         "Valid attr_get() call didn't return success");
310         TEST_ASSERT_EQUAL(1, (attr_value > 0),
311                         "attr_get() call didn't get call count (zero)");
312
313         TEST_ASSERT_EQUAL(0, rte_service_attr_reset_all(id),
314                         "Valid attr_reset_all() return success");
315
316         TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_id, &attr_value),
317                         "Valid attr_get() call didn't return success");
318         TEST_ASSERT_EQUAL(0, attr_value,
319                         "attr_get() call didn't set correct cycles (zero)");
320         /* ensure call count > zero */
321         TEST_ASSERT_EQUAL(0, rte_service_attr_get(id, attr_calls, &attr_value),
322                         "Valid attr_get() call didn't return success");
323         TEST_ASSERT_EQUAL(0, (attr_value > 0),
324                         "attr_get() call didn't get call count (zero)");
325
326         return unregister_all();
327 }
328
329 /* verify service lcore attr get */
330 static int
331 service_lcore_attr_get(void)
332 {
333         /* ensure all services unregistered so cycle counts are zero */
334         unregister_all();
335
336         struct rte_service_spec service;
337         memset(&service, 0, sizeof(struct rte_service_spec));
338         service.callback = dummy_cb;
339         snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
340         service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
341         uint32_t id;
342         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
343                         "Register of  service failed");
344         rte_service_component_runstate_set(id, 1);
345         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(id, 1),
346                         "Error: Service start returned non-zero");
347         rte_service_set_stats_enable(id, 1);
348
349         uint64_t lcore_attr_value = 0xdead;
350         uint32_t lcore_attr_id = UINT32_MAX;
351
352         /* check error return values */
353         TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_attr_get(UINT32_MAX,
354                         lcore_attr_id, &lcore_attr_value),
355                         "Invalid lcore_id didn't return -EINVAL");
356         TEST_ASSERT_EQUAL(-ENOTSUP, rte_service_lcore_attr_get(rte_lcore_id(),
357                         lcore_attr_id, &lcore_attr_value),
358                         "Non-service core didn't return -ENOTSUP");
359
360         /* Start service core to increment loop count */
361         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
362                         "Service core add did not return zero");
363         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(id, slcore_id, 1),
364                         "Enabling valid service and core failed");
365         TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
366                         "Starting service core failed");
367
368         /* wait for the service lcore to run */
369         rte_delay_ms(200);
370
371         lcore_attr_id = RTE_SERVICE_LCORE_ATTR_LOOPS;
372         TEST_ASSERT_EQUAL(0, rte_service_lcore_attr_get(slcore_id,
373                         lcore_attr_id, &lcore_attr_value),
374                         "Valid lcore_attr_get() call didn't return success");
375         int loops_gt_zero = lcore_attr_value > 0;
376         TEST_ASSERT_EQUAL(1, loops_gt_zero,
377                         "lcore_attr_get() failed to get loops "
378                         "(expected > zero)");
379
380         lcore_attr_id++;  // invalid lcore attr id
381         TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_attr_get(slcore_id,
382                         lcore_attr_id, &lcore_attr_value),
383                         "Invalid lcore attr didn't return -EINVAL");
384
385         rte_service_lcore_stop(slcore_id);
386
387         TEST_ASSERT_EQUAL(0, rte_service_lcore_attr_reset_all(slcore_id),
388                           "Valid lcore_attr_reset_all() didn't return success");
389
390         lcore_attr_id = RTE_SERVICE_LCORE_ATTR_LOOPS;
391         TEST_ASSERT_EQUAL(0, rte_service_lcore_attr_get(slcore_id,
392                         lcore_attr_id, &lcore_attr_value),
393                         "Valid lcore_attr_get() call didn't return success");
394         TEST_ASSERT_EQUAL(0, lcore_attr_value,
395                         "lcore_attr_get() didn't get correct loop count "
396                         "(zero)");
397
398         return unregister_all();
399 }
400
401 /* verify service dump */
402 static int
403 service_dump(void)
404 {
405         const uint32_t sid = 0;
406         rte_service_set_stats_enable(sid, 1);
407         rte_service_dump(stdout, 0);
408         rte_service_set_stats_enable(sid, 0);
409         rte_service_dump(stdout, 0);
410         return unregister_all();
411 }
412
413 /* start and stop a service */
414 static int
415 service_start_stop(void)
416 {
417         const uint32_t sid = 0;
418
419         /* runstate_get() returns if service is running and slcore is mapped */
420         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
421                         "Service core add did not return zero");
422         int ret = rte_service_map_lcore_set(sid, slcore_id, 1);
423         TEST_ASSERT_EQUAL(0, ret,
424                         "Enabling service core, expected 0 got %d", ret);
425
426         TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
427                         "Error: Service should be stopped");
428
429         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
430                         "Error: Service stopped returned non-zero");
431
432         TEST_ASSERT_EQUAL(0, rte_service_runstate_get(sid),
433                         "Error: Service is running - should be stopped");
434
435         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
436                         "Error: Service start returned non-zero");
437
438         TEST_ASSERT_EQUAL(1, rte_service_runstate_get(sid),
439                         "Error: Service is not running");
440
441         return unregister_all();
442 }
443
444
445 static int
446 service_remote_launch_func(void *arg)
447 {
448         RTE_SET_USED(arg);
449         service_remote_launch_flag = 1;
450         return 0;
451 }
452
453 /* enable and disable a lcore for a service */
454 static int
455 service_lcore_en_dis_able(void)
456 {
457         const uint32_t sid = 0;
458
459         /* expected failure cases */
460         TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 1),
461                         "Enable on invalid core did not fail");
462         TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, 100000, 0),
463                         "Disable on invalid core did not fail");
464
465         /* add service core to allow enabling */
466         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
467                         "Add service core failed when not in use before");
468
469         /* valid enable */
470         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
471                         "Enabling valid service and core failed");
472         TEST_ASSERT_EQUAL(1, rte_service_map_lcore_get(sid, slcore_id),
473                         "Enabled core returned not-enabled");
474
475         /* valid disable */
476         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 0),
477                         "Disabling valid service and lcore failed");
478         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_get(sid, slcore_id),
479                         "Disabled core returned enabled");
480
481         /* call remote_launch to verify that app can launch ex-service lcore */
482         service_remote_launch_flag = 0;
483         rte_eal_wait_lcore(slcore_id);
484         int ret = rte_eal_remote_launch(service_remote_launch_func, NULL,
485                                         slcore_id);
486         TEST_ASSERT_EQUAL(0, ret, "Ex-service core remote launch failed.");
487         rte_eal_wait_lcore(slcore_id);
488         TEST_ASSERT_EQUAL(1, service_remote_launch_flag,
489                         "Ex-service core function call had no effect.");
490
491         return unregister_all();
492 }
493
494 static int
495 service_lcore_running_check(void)
496 {
497         uint64_t tick = service_tick;
498         rte_delay_ms(SERVICE_DELAY * 100);
499         /* if (tick != service_tick) we know the lcore as polled the service */
500         return tick != service_tick;
501 }
502
503 static int
504 service_lcore_add_del(void)
505 {
506         if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
507             !rte_lcore_is_enabled(2) || !rte_lcore_is_enabled(3))
508                 return TEST_SKIPPED;
509
510         /* check initial count */
511         TEST_ASSERT_EQUAL(0, rte_service_lcore_count(),
512                         "Service lcore count has value before adding a lcore");
513
514         /* check service lcore add */
515         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
516                         "Add service core failed when not in use before");
517         TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_add(slcore_id),
518                         "Add service core failed to refuse in-use lcore");
519
520         /* check count */
521         TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
522                         "Service core count not equal to one");
523
524         /* retrieve core list, checking lcore ids */
525         const uint32_t size = 4;
526         uint32_t service_core_ids[size];
527         int32_t n = rte_service_lcore_list(service_core_ids, size);
528         TEST_ASSERT_EQUAL(1, n, "Service core list return should equal 1");
529         TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
530                                 "Service core list lcore must equal slcore_id");
531
532         /* recheck count, add more cores, and check count */
533         TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
534                         "Service core count not equal to one");
535         uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
536                                                /* skip master */ 1,
537                                                /* wrap */ 0);
538         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
539                         "Service core add did not return zero");
540         uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
541                                                /* skip master */ 1,
542                                                /* wrap */ 0);
543         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
544                         "Service core add did not return zero");
545
546         uint32_t count = rte_service_lcore_count();
547         const uint32_t cores_at_this_point = 3;
548         TEST_ASSERT_EQUAL(cores_at_this_point, count,
549                         "Service core count %d, expected %d", count,
550                         cores_at_this_point);
551
552         /* check longer service core list */
553         n = rte_service_lcore_list(service_core_ids, size);
554         TEST_ASSERT_EQUAL(3, n, "Service core list return should equal 3");
555         TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
556                                 "Service core list[0] lcore must equal 1");
557         TEST_ASSERT_EQUAL(slcore_1, service_core_ids[1],
558                                 "Service core list[1] lcore must equal 2");
559         TEST_ASSERT_EQUAL(slcore_2, service_core_ids[2],
560                                 "Service core list[2] lcore must equal 3");
561
562         /* recheck count, remove lcores, check remaining lcore_id is correct */
563         TEST_ASSERT_EQUAL(3, rte_service_lcore_count(),
564                         "Service core count not equal to three");
565         TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_1),
566                         "Service core add did not return zero");
567         TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_2),
568                         "Service core add did not return zero");
569         TEST_ASSERT_EQUAL(1, rte_service_lcore_count(),
570                         "Service core count not equal to one");
571         n = rte_service_lcore_list(service_core_ids, size);
572         TEST_ASSERT_EQUAL(1, n, "Service core list return should equal one");
573         TEST_ASSERT_EQUAL(slcore_id, service_core_ids[0],
574                                 "Service core list[0] lcore must equal %d",
575                                 slcore_id);
576
577         return unregister_all();
578 }
579
580 static int
581 service_threaded_test(int mt_safe)
582 {
583         unregister_all();
584
585         /* add next 2 cores */
586         uint32_t slcore_1 = rte_get_next_lcore(/* start core */ -1,
587                                                /* skip master */ 1,
588                                                /* wrap */ 0);
589         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_1),
590                         "mt safe lcore add fail");
591         uint32_t slcore_2 = rte_get_next_lcore(/* start core */ slcore_1,
592                                                /* skip master */ 1,
593                                                /* wrap */ 0);
594         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
595                         "mt safe lcore add fail");
596
597         /* Use atomic locks to verify that two threads are in the same function
598          * at the same time. These are passed to the unit tests through the
599          * callback userdata parameter
600          */
601         uint32_t test_params[2];
602         memset(test_params, 0, sizeof(uint32_t) * 2);
603
604         /* register MT safe service. */
605         struct rte_service_spec service;
606         memset(&service, 0, sizeof(struct rte_service_spec));
607         service.callback_userdata = test_params;
608         snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
609
610         if (mt_safe) {
611                 service.callback = dummy_mt_safe_cb;
612                 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
613         } else
614                 service.callback = dummy_mt_unsafe_cb;
615
616         uint32_t id;
617         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
618                         "Register of MT SAFE service failed");
619
620         const uint32_t sid = 0;
621         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
622                         "Starting valid service failed");
623         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_1, 1),
624                         "Failed to enable lcore 1 on mt safe service");
625         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_2, 1),
626                         "Failed to enable lcore 2 on mt safe service");
627         rte_service_lcore_start(slcore_1);
628         rte_service_lcore_start(slcore_2);
629
630         /* wait for the worker threads to run */
631         rte_delay_ms(500);
632         rte_service_lcore_stop(slcore_1);
633         rte_service_lcore_stop(slcore_2);
634
635         TEST_ASSERT_EQUAL(0, test_params[1],
636                         "Service run with component runstate = 0");
637
638         /* enable backend runstate: the service should run after this */
639         rte_service_component_runstate_set(id, 1);
640
641         /* initialize to pass, see callback comment for details */
642         if (!mt_safe)
643                 test_params[1] = 1;
644
645         /* wait for lcores before start() */
646         rte_eal_wait_lcore(slcore_1);
647         rte_eal_wait_lcore(slcore_2);
648
649         rte_service_lcore_start(slcore_1);
650         rte_service_lcore_start(slcore_2);
651
652         /* wait for the worker threads to run */
653         rte_delay_ms(500);
654         rte_service_lcore_stop(slcore_1);
655         rte_service_lcore_stop(slcore_2);
656
657         TEST_ASSERT_EQUAL(1, test_params[1],
658                         "MT Safe service not run by two cores concurrently");
659         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
660                         "Failed to stop MT Safe service");
661
662         rte_eal_wait_lcore(slcore_1);
663         rte_eal_wait_lcore(slcore_2);
664         unregister_all();
665
666         /* return the value of the callback pass_test variable to caller */
667         return test_params[1];
668 }
669
670 /* tests an MT SAFE service with two cores. The callback function ensures that
671  * two threads access the callback concurrently.
672  */
673 static int
674 service_mt_safe_poll(void)
675 {
676         int mt_safe = 1;
677
678         if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
679             !rte_lcore_is_enabled(2))
680                 return TEST_SKIPPED;
681
682         TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
683                         "Error: MT Safe service not run by two cores concurrently");
684         return TEST_SUCCESS;
685 }
686
687 /* tests a NON mt safe service with two cores, the callback is serialized
688  * using the atomic cmpset.
689  */
690 static int
691 service_mt_unsafe_poll(void)
692 {
693         int mt_safe = 0;
694
695         if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
696             !rte_lcore_is_enabled(2))
697                 return TEST_SKIPPED;
698
699         TEST_ASSERT_EQUAL(1, service_threaded_test(mt_safe),
700                         "Error: NON MT Safe service run by two cores concurrently");
701         return TEST_SUCCESS;
702 }
703
704 static int32_t
705 delay_as_a_mt_safe_service(void *args)
706 {
707         RTE_SET_USED(args);
708         uint32_t *params = args;
709
710         /* retrieve done flag and atomic lock to inc/dec */
711         uint32_t *done = &params[0];
712         rte_atomic32_t *lock = (rte_atomic32_t *)&params[1];
713
714         while (!*done) {
715                 rte_atomic32_inc(lock);
716                 rte_delay_us(500);
717                 if (rte_atomic32_read(lock) > 1)
718                         /* pass: second core has simultaneously incremented */
719                         *done = 1;
720                 rte_atomic32_dec(lock);
721         }
722
723         return 0;
724 }
725
726 static int32_t
727 delay_as_a_service(void *args)
728 {
729         uint32_t *done = (uint32_t *)args;
730         while (!*done)
731                 rte_delay_ms(5);
732         return 0;
733 }
734
735 static int
736 service_run_on_app_core_func(void *arg)
737 {
738         uint32_t *delay_service_id = (uint32_t *)arg;
739         return rte_service_run_iter_on_app_lcore(*delay_service_id, 1);
740 }
741
742 static int
743 service_app_lcore_poll_impl(const int mt_safe)
744 {
745         uint32_t params[2] = {0};
746
747         struct rte_service_spec service;
748         memset(&service, 0, sizeof(struct rte_service_spec));
749         snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
750         if (mt_safe) {
751                 service.callback = delay_as_a_mt_safe_service;
752                 service.callback_userdata = params;
753                 service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
754         } else {
755                 service.callback = delay_as_a_service;
756                 service.callback_userdata = &params;
757         }
758
759         uint32_t id;
760         TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
761                         "Register of app lcore delay service failed");
762
763         rte_service_component_runstate_set(id, 1);
764         rte_service_runstate_set(id, 1);
765
766         uint32_t app_core2 = rte_get_next_lcore(slcore_id, 1, 1);
767         rte_eal_wait_lcore(app_core2);
768         int app_core2_ret = rte_eal_remote_launch(service_run_on_app_core_func,
769                                                   &id, app_core2);
770
771         rte_delay_ms(100);
772
773         int app_core1_ret = service_run_on_app_core_func(&id);
774
775         /* flag done, then wait for the spawned 2nd core to return */
776         params[0] = 1;
777         rte_eal_mp_wait_lcore();
778
779         /* core two gets launched first - and should hold the service lock */
780         TEST_ASSERT_EQUAL(0, app_core2_ret,
781                         "App core2 : run service didn't return zero");
782
783         if (mt_safe) {
784                 /* mt safe should have both cores return 0 for success */
785                 TEST_ASSERT_EQUAL(0, app_core1_ret,
786                                 "MT Safe: App core1 didn't return 0");
787         } else {
788                 /* core one attempts to run later - should be blocked */
789                 TEST_ASSERT_EQUAL(-EBUSY, app_core1_ret,
790                                 "MT Unsafe: App core1 didn't return -EBUSY");
791         }
792
793         /* Performance test: call in a loop, and measure tsc() */
794         const uint32_t perf_iters = (1 << 12);
795         uint64_t start = rte_rdtsc();
796         uint32_t i;
797         for (i = 0; i < perf_iters; i++) {
798                 int err = service_run_on_app_core_func(&id);
799                 TEST_ASSERT_EQUAL(0, err, "perf test: returned run failure");
800         }
801         uint64_t end = rte_rdtsc();
802         printf("perf test for %s: %0.1f cycles per call\n", mt_safe ?
803                 "MT Safe" : "MT Unsafe", (end - start)/(float)perf_iters);
804
805         unregister_all();
806         return TEST_SUCCESS;
807 }
808
809 static int
810 service_app_lcore_mt_safe(void)
811 {
812         const int mt_safe = 1;
813         return service_app_lcore_poll_impl(mt_safe);
814 }
815
816 static int
817 service_app_lcore_mt_unsafe(void)
818 {
819         const int mt_safe = 0;
820         return service_app_lcore_poll_impl(mt_safe);
821 }
822
823 /* start and stop a service core - ensuring it goes back to sleep */
824 static int
825 service_lcore_start_stop(void)
826 {
827         /* start service core and service, create mapping so tick() runs */
828         const uint32_t sid = 0;
829         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
830                         "Starting valid service failed");
831         TEST_ASSERT_EQUAL(-EINVAL, rte_service_map_lcore_set(sid, slcore_id, 1),
832                         "Enabling valid service on non-service core must fail");
833
834         /* core start */
835         TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_start(slcore_id),
836                         "Service core start without add should return EINVAL");
837         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
838                         "Service core add did not return zero");
839         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
840                         "Enabling valid service on valid core failed");
841         TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
842                         "Service core start after add failed");
843         TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_start(slcore_id),
844                         "Service core expected as running but was stopped");
845
846         /* ensures core really is running the service function */
847         TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
848                         "Service core expected to poll service but it didn't");
849
850         /* core stop */
851         TEST_ASSERT_EQUAL(-EBUSY, rte_service_lcore_stop(slcore_id),
852                         "Service core running a service should return -EBUSY");
853         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
854                         "Stopping valid service failed");
855         TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_stop(100000),
856                         "Invalid Service core stop should return -EINVAL");
857         TEST_ASSERT_EQUAL(0, rte_service_lcore_stop(slcore_id),
858                         "Service core stop expected to return 0");
859         TEST_ASSERT_EQUAL(-EALREADY, rte_service_lcore_stop(slcore_id),
860                         "Already stopped service core should return -EALREADY");
861
862         /* ensure service is not longer running */
863         TEST_ASSERT_EQUAL(0, service_lcore_running_check(),
864                         "Service core expected to poll service but it didn't");
865
866         TEST_ASSERT_EQUAL(0, rte_service_lcore_del(slcore_id),
867                         "Service core del did not return zero");
868
869         return unregister_all();
870 }
871
872 /* stop a service and wait for it to become inactive */
873 static int
874 service_may_be_active(void)
875 {
876         const uint32_t sid = 0;
877         int i;
878
879         /* expected failure cases */
880         TEST_ASSERT_EQUAL(-EINVAL, rte_service_may_be_active(10000),
881                         "Invalid service may be active check did not fail");
882
883         /* start the service */
884         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
885                         "Starting valid service failed");
886         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_id),
887                         "Add service core failed when not in use before");
888         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore_id, 1),
889                         "Enabling valid service on valid core failed");
890         TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore_id),
891                         "Service core start after add failed");
892
893         /* ensures core really is running the service function */
894         TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
895                         "Service core expected to poll service but it didn't");
896
897         /* stop the service */
898         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
899                         "Error: Service stop returned non-zero");
900
901         /* give the service 100ms to stop running */
902         for (i = 0; i < 100; i++) {
903                 if (!rte_service_may_be_active(sid))
904                         break;
905                 rte_delay_ms(SERVICE_DELAY);
906         }
907
908         TEST_ASSERT_EQUAL(0, rte_service_may_be_active(sid),
909                           "Error: Service not stopped after 100ms");
910
911         return unregister_all();
912 }
913
914 /* check service may be active when service is running on a second lcore */
915 static int
916 service_active_two_cores(void)
917 {
918         if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1) ||
919             !rte_lcore_is_enabled(2))
920                 return TEST_SKIPPED;
921
922         const uint32_t sid = 0;
923         int i;
924
925         uint32_t lcore = rte_get_next_lcore(/* start core */ -1,
926                                             /* skip master */ 1,
927                                             /* wrap */ 0);
928         uint32_t slcore = rte_get_next_lcore(/* start core */ lcore,
929                                              /* skip master */ 1,
930                                              /* wrap */ 0);
931
932         /* start the service on the second available lcore */
933         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 1),
934                         "Starting valid service failed");
935         TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore),
936                         "Add service core failed when not in use before");
937         TEST_ASSERT_EQUAL(0, rte_service_map_lcore_set(sid, slcore, 1),
938                         "Enabling valid service on valid core failed");
939         TEST_ASSERT_EQUAL(0, rte_service_lcore_start(slcore),
940                         "Service core start after add failed");
941
942         /* ensures core really is running the service function */
943         TEST_ASSERT_EQUAL(1, service_lcore_running_check(),
944                         "Service core expected to poll service but it didn't");
945
946         /* ensures that service may be active reports running state */
947         TEST_ASSERT_EQUAL(1, rte_service_may_be_active(sid),
948                         "Service may be active did not report running state");
949
950         /* stop the service */
951         TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
952                         "Error: Service stop returned non-zero");
953
954         /* give the service 100ms to stop running */
955         for (i = 0; i < 100; i++) {
956                 if (!rte_service_may_be_active(sid))
957                         break;
958                 rte_delay_ms(SERVICE_DELAY);
959         }
960
961         TEST_ASSERT_EQUAL(0, rte_service_may_be_active(sid),
962                           "Error: Service not stopped after 100ms");
963
964         return unregister_all();
965 }
966
967 static struct unit_test_suite service_tests  = {
968         .suite_name = "service core test suite",
969         .setup = testsuite_setup,
970         .teardown = testsuite_teardown,
971         .unit_test_cases = {
972                 TEST_CASE_ST(dummy_register, NULL, unregister_all),
973                 TEST_CASE_ST(dummy_register, NULL, service_name),
974                 TEST_CASE_ST(dummy_register, NULL, service_get_by_name),
975                 TEST_CASE_ST(dummy_register, NULL, service_dump),
976                 TEST_CASE_ST(dummy_register, NULL, service_attr_get),
977                 TEST_CASE_ST(dummy_register, NULL, service_lcore_attr_get),
978                 TEST_CASE_ST(dummy_register, NULL, service_probe_capability),
979                 TEST_CASE_ST(dummy_register, NULL, service_start_stop),
980                 TEST_CASE_ST(dummy_register, NULL, service_lcore_add_del),
981                 TEST_CASE_ST(dummy_register, NULL, service_lcore_start_stop),
982                 TEST_CASE_ST(dummy_register, NULL, service_lcore_en_dis_able),
983                 TEST_CASE_ST(dummy_register, NULL, service_mt_unsafe_poll),
984                 TEST_CASE_ST(dummy_register, NULL, service_mt_safe_poll),
985                 TEST_CASE_ST(dummy_register, NULL, service_app_lcore_mt_safe),
986                 TEST_CASE_ST(dummy_register, NULL, service_app_lcore_mt_unsafe),
987                 TEST_CASE_ST(dummy_register, NULL, service_may_be_active),
988                 TEST_CASE_ST(dummy_register, NULL, service_active_two_cores),
989                 TEST_CASES_END() /**< NULL terminate unit test array */
990         }
991 };
992
993 static int
994 test_service_common(void)
995 {
996         return unit_test_suite_runner(&service_tests);
997 }
998
999 REGISTER_TEST_COMMAND(service_autotest, test_service_common);