net/virtio: fix incorrect cast of void *
[dpdk.git] / test / test / test_service_cores.c
index e650b20..311c704 100644 (file)
@@ -166,9 +166,12 @@ dummy_register(void)
                        "Invalid name");
        snprintf(service.name, sizeof(service.name), DUMMY_SERVICE_NAME);
 
-       TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
+       uint32_t id;
+       TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
                        "Failed to register valid service");
 
+       rte_service_component_runstate_set(id, 1);
+
        return TEST_SUCCESS;
 }
 
@@ -178,9 +181,13 @@ service_get_by_name(void)
 {
        unregister_all();
 
-       /* ensure with no services registered returns NULL */
-       TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME),
-                       "Service get by name should return NULL");
+       uint32_t sid;
+       TEST_ASSERT_EQUAL(-ENODEV,
+                       rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
+                       "get by name with invalid name should return -ENODEV");
+       TEST_ASSERT_EQUAL(-EINVAL,
+                       rte_service_get_by_name(DUMMY_SERVICE_NAME, 0x0),
+                       "get by name with NULL ptr should return -ENODEV");
 
        /* register service */
        struct rte_service_spec service;
@@ -196,16 +203,19 @@ service_get_by_name(void)
        TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
                        "Failed to register valid service");
 
-       /* ensure with dummy services registered returns same ptr as ID */
-       struct rte_service_spec *s_by_id = rte_service_get_by_id(0);
-       TEST_ASSERT_EQUAL(s_by_id, rte_service_get_by_name(DUMMY_SERVICE_NAME),
-                       "Service get_by_name should equal get_by_id()");
+       /* we unregistered all service, now registering 1, should be id 0 */
+       uint32_t service_id_as_expected = 0;
+       TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
+                       "Service get_by_name should return 0 on valid inputs");
+       TEST_ASSERT_EQUAL(service_id_as_expected, sid,
+                       "Service get_by_name should equal expected id");
 
        unregister_all();
 
        /* ensure after unregister, get_by_name returns NULL */
-       TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME),
-                       "get by name should return NULL after unregister");
+       TEST_ASSERT_EQUAL(-ENODEV,
+                       rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
+                       "get by name should return -ENODEV after unregister");
 
        return TEST_SUCCESS;
 }
@@ -249,9 +259,8 @@ service_probe_capability(void)
 static int
 service_name(void)
 {
-       struct rte_service_spec *service = rte_service_get_by_id(0);
-
-       int equal = strcmp(service->name, DUMMY_SERVICE_NAME);
+       const char *name = rte_service_get_name(0);
+       int equal = strcmp(name, DUMMY_SERVICE_NAME);
        TEST_ASSERT_EQUAL(0, equal, "Error: Service name not correct");
 
        return unregister_all();
@@ -464,13 +473,11 @@ service_threaded_test(int mt_safe)
        if (mt_safe) {
                service.callback = dummy_mt_safe_cb;
                service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
-       } else {
-               /* initialize to pass, see callback comment for details */
-               test_params[1] = 1;
+       } else
                service.callback = dummy_mt_unsafe_cb;
-       }
 
-       TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
+       uint32_t id;
+       TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
                        "Register of MT SAFE service failed");
 
        const uint32_t sid = 0;
@@ -488,9 +495,26 @@ service_threaded_test(int mt_safe)
        rte_service_lcore_stop(slcore_1);
        rte_service_lcore_stop(slcore_2);
 
+       TEST_ASSERT_EQUAL(0, test_params[1],
+                       "Service run with component runstate = 0");
+
+       /* enable backend runstate: the service should run after this */
+       rte_service_component_runstate_set(id, 1);
+
+       /* initialize to pass, see callback comment for details */
+       if (!mt_safe)
+               test_params[1] = 1;
+
+       rte_service_lcore_start(slcore_1);
+       rte_service_lcore_start(slcore_2);
+
+       /* wait for the worker threads to run */
+       rte_delay_ms(500);
+       rte_service_lcore_stop(slcore_1);
+       rte_service_lcore_stop(slcore_2);
+
        TEST_ASSERT_EQUAL(1, test_params[1],
                        "MT Safe service not run by two cores concurrently");
-
        TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
                        "Failed to stop MT Safe service");
 
@@ -524,6 +548,113 @@ service_mt_unsafe_poll(void)
        return TEST_SUCCESS;
 }
 
+static int32_t
+delay_as_a_mt_safe_service(void *args)
+{
+       RTE_SET_USED(args);
+       uint32_t *params = args;
+
+       /* retrieve done flag and atomic lock to inc/dec */
+       uint32_t *done = &params[0];
+       rte_atomic32_t *lock = (rte_atomic32_t *)&params[1];
+
+       while (!*done) {
+               rte_atomic32_inc(lock);
+               rte_delay_us(500);
+               if (rte_atomic32_read(lock) > 1)
+                       /* pass: second core has simultaneously incremented */
+                       *done = 1;
+               rte_atomic32_dec(lock);
+       }
+
+       return 0;
+}
+
+static int32_t
+delay_as_a_service(void *args)
+{
+       uint32_t *done = (uint32_t *)args;
+       while (!*done)
+               rte_delay_ms(5);
+       return 0;
+}
+
+static int
+service_run_on_app_core_func(void *arg)
+{
+       uint32_t *delay_service_id = (uint32_t *)arg;
+       return rte_service_run_iter_on_app_lcore(*delay_service_id, 1);
+}
+
+static int
+service_app_lcore_poll_impl(const int mt_safe)
+{
+       uint32_t params[2] = {0};
+
+       struct rte_service_spec service;
+       memset(&service, 0, sizeof(struct rte_service_spec));
+       snprintf(service.name, sizeof(service.name), MT_SAFE_SERVICE_NAME);
+       if (mt_safe) {
+               service.callback = delay_as_a_mt_safe_service;
+               service.callback_userdata = params;
+               service.capabilities |= RTE_SERVICE_CAP_MT_SAFE;
+       } else {
+               service.callback = delay_as_a_service;
+               service.callback_userdata = &params;
+       }
+
+       uint32_t id;
+       TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, &id),
+                       "Register of app lcore delay service failed");
+
+       rte_service_component_runstate_set(id, 1);
+       rte_service_runstate_set(id, 1);
+
+       uint32_t app_core2 = rte_get_next_lcore(slcore_id, 1, 1);
+       int app_core2_ret = rte_eal_remote_launch(service_run_on_app_core_func,
+                                                 &id, app_core2);
+
+       rte_delay_ms(100);
+
+       int app_core1_ret = service_run_on_app_core_func(&id);
+
+       /* flag done, then wait for the spawned 2nd core to return */
+       params[0] = 1;
+       rte_eal_mp_wait_lcore();
+
+       /* core two gets launched first - and should hold the service lock */
+       TEST_ASSERT_EQUAL(0, app_core2_ret,
+                       "App core2 : run service didn't return zero");
+
+       if (mt_safe) {
+               /* mt safe should have both cores return 0 for success */
+               TEST_ASSERT_EQUAL(0, app_core1_ret,
+                               "MT Safe: App core1 didn't return 0");
+       } else {
+               /* core one attempts to run later - should be blocked */
+               TEST_ASSERT_EQUAL(-EBUSY, app_core1_ret,
+                               "MT Unsafe: App core1 didn't return -EBUSY");
+       }
+
+       unregister_all();
+
+       return TEST_SUCCESS;
+}
+
+static int
+service_app_lcore_mt_safe(void)
+{
+       const int mt_safe = 1;
+       return service_app_lcore_poll_impl(mt_safe);
+}
+
+static int
+service_app_lcore_mt_unsafe(void)
+{
+       const int mt_safe = 0;
+       return service_app_lcore_poll_impl(mt_safe);
+}
+
 /* start and stop a service core - ensuring it goes back to sleep */
 static int
 service_lcore_start_stop(void)
@@ -552,6 +683,10 @@ service_lcore_start_stop(void)
                        "Service core expected to poll service but it didn't");
 
        /* core stop */
+       TEST_ASSERT_EQUAL(-EBUSY, rte_service_lcore_stop(slcore_id),
+                       "Service core running a service should return -EBUSY");
+       TEST_ASSERT_EQUAL(0, rte_service_runstate_set(sid, 0),
+                       "Stopping valid service failed");
        TEST_ASSERT_EQUAL(-EINVAL, rte_service_lcore_stop(100000),
                        "Invalid Service core stop should return -EINVAL");
        TEST_ASSERT_EQUAL(0, rte_service_lcore_stop(slcore_id),
@@ -585,6 +720,8 @@ static struct unit_test_suite service_tests  = {
                TEST_CASE_ST(dummy_register, NULL, service_lcore_en_dis_able),
                TEST_CASE_ST(dummy_register, NULL, service_mt_unsafe_poll),
                TEST_CASE_ST(dummy_register, NULL, service_mt_safe_poll),
+               TEST_CASE_ST(dummy_register, NULL, service_app_lcore_mt_safe),
+               TEST_CASE_ST(dummy_register, NULL, service_app_lcore_mt_unsafe),
                TEST_CASES_END() /**< NULL terminate unit test array */
        }
 };