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