test/threads: add unit test
[dpdk.git] / app / test / test_graph.c
index dfb71b4..1a2d1e6 100644 (file)
@@ -1,6 +1,9 @@
 /* SPDX-License-Identifier: BSD-3-Clause
  * Copyright(C) 2020 Marvell International Ltd.
  */
+
+#include "test.h"
+
 #include <assert.h>
 #include <inttypes.h>
 #include <signal.h>
 #include <unistd.h>
 
 #include <rte_errno.h>
+
+#ifdef RTE_EXEC_ENV_WINDOWS
+static int
+test_node_list_dump(void)
+{
+       printf("node_list_dump not supported on Windows, skipping test\n");
+       return TEST_SKIPPED;
+}
+
+#else
+
 #include <rte_graph.h>
 #include <rte_graph_worker.h>
 #include <rte_mbuf.h>
+#include <rte_mbuf_dyn.h>
 #include <rte_random.h>
 
-#include "test.h"
-
 static uint16_t test_node_worker_source(struct rte_graph *graph,
                                        struct rte_node *node, void **objs,
                                        uint16_t nb_objs);
@@ -39,6 +52,16 @@ static uint16_t test_node3_worker(struct rte_graph *graph,
 #define MBUFF_SIZE 512
 #define MAX_NODES  4
 
+typedef uint64_t graph_dynfield_t;
+static int graph_dynfield_offset = -1;
+
+static inline graph_dynfield_t *
+graph_field(struct rte_mbuf *mbuf)
+{
+       return RTE_MBUF_DYNFIELD(mbuf, \
+                       graph_dynfield_offset, graph_dynfield_t *);
+}
+
 static struct rte_mbuf mbuf[MAX_NODES + 1][MBUFF_SIZE];
 static void *mbuf_p[MAX_NODES + 1][MBUFF_SIZE];
 static rte_graph_t graph_id;
@@ -162,9 +185,9 @@ test_node_worker_source(struct rte_graph *graph, struct rte_node *node,
        next_stream = rte_node_next_stream_get(graph, node, next, obj_node0);
        for (i = 0; i < obj_node0; i++) {
                data = &mbuf[0][i];
-               data->udata64 = ((uint64_t)tm->test_node[0].idx << 32) | i;
+               *graph_field(data) = ((uint64_t)tm->test_node[0].idx << 32) | i;
                if ((i + 1) == obj_node0)
-                       data->udata64 |= (1 << 16);
+                       *graph_field(data) |= (1 << 16);
                next_stream[i] = &mbuf[0][i];
        }
        rte_node_next_stream_put(graph, node, next, obj_node0);
@@ -175,9 +198,9 @@ test_node_worker_source(struct rte_graph *graph, struct rte_node *node,
        next_stream = rte_node_next_stream_get(graph, node, next, obj_node1);
        for (i = 0; i < obj_node1; i++) {
                data = &mbuf[0][obj_node0 + i];
-               data->udata64 = ((uint64_t)tm->test_node[1].idx << 32) | i;
+               *graph_field(data) = ((uint64_t)tm->test_node[1].idx << 32) | i;
                if ((i + 1) == obj_node1)
-                       data->udata64 |= (1 << 16);
+                       *graph_field(data) |= (1 << 16);
                next_stream[i] = &mbuf[0][obj_node0 + i];
        }
 
@@ -205,23 +228,23 @@ test_node0_worker(struct rte_graph *graph, struct rte_node *node, void **objs,
 
                for (i = 0; i < nb_objs; i++) {
                        data = (struct rte_mbuf *)objs[i];
-                       if ((data->udata64 >> 32) != tm->test_node[0].idx) {
+                       if ((*graph_field(data) >> 32) != tm->test_node[0].idx) {
                                printf("Data idx miss match at node 0, expected"
                                       " = %u got = %u\n",
                                       tm->test_node[0].idx,
-                                      (uint32_t)(data->udata64 >> 32));
+                                      (uint32_t)(*graph_field(data) >> 32));
                                goto end;
                        }
 
-                       if ((data->udata64 & 0xffff) != (i - count)) {
+                       if ((*graph_field(data) & 0xffff) != (i - count)) {
                                printf("Expected buff count miss match at "
                                       "node 0\n");
                                goto end;
                        }
 
-                       if (data->udata64 & (0x1 << 16))
+                       if (*graph_field(data) & (0x1 << 16))
                                count = i + 1;
-                       if (data->udata64 & (0x1 << 17))
+                       if (*graph_field(data) & (0x1 << 17))
                                second_pass = 1;
                }
 
@@ -233,12 +256,12 @@ test_node0_worker(struct rte_graph *graph, struct rte_node *node, void **objs,
                obj_node0 = nb_objs * obj_node0 * 0.01;
                for (i = 0; i < obj_node0; i++) {
                        data = &mbuf[1][i];
-                       data->udata64 =
+                       *graph_field(data) =
                                ((uint64_t)tm->test_node[1].idx << 32) | i;
                        if ((i + 1) == obj_node0)
-                               data->udata64 |= (1 << 16);
+                               *graph_field(data) |= (1 << 16);
                        if (second_pass)
-                               data->udata64 |= (1 << 17);
+                               *graph_field(data) |= (1 << 17);
                }
                rte_node_enqueue(graph, node, 0, (void **)&mbuf_p[1][0],
                                 obj_node0);
@@ -246,12 +269,12 @@ test_node0_worker(struct rte_graph *graph, struct rte_node *node, void **objs,
                obj_node1 = nb_objs - obj_node0;
                for (i = 0; i < obj_node1; i++) {
                        data = &mbuf[1][obj_node0 + i];
-                       data->udata64 =
+                       *graph_field(data) =
                                ((uint64_t)tm->test_node[2].idx << 32) | i;
                        if ((i + 1) == obj_node1)
-                               data->udata64 |= (1 << 16);
+                               *graph_field(data) |= (1 << 16);
                        if (second_pass)
-                               data->udata64 |= (1 << 17);
+                               *graph_field(data) |= (1 << 17);
                }
                rte_node_enqueue(graph, node, 1, (void **)&mbuf_p[1][obj_node0],
                                 obj_node1);
@@ -285,22 +308,22 @@ test_node1_worker(struct rte_graph *graph, struct rte_node *node, void **objs,
        fn_calls[2] += 1;
        for (i = 0; i < nb_objs; i++) {
                data = (struct rte_mbuf *)objs[i];
-               if ((data->udata64 >> 32) != tm->test_node[1].idx) {
+               if ((*graph_field(data) >> 32) != tm->test_node[1].idx) {
                        printf("Data idx miss match at node 1, expected = %u"
                               " got = %u\n",
                               tm->test_node[1].idx,
-                              (uint32_t)(data->udata64 >> 32));
+                              (uint32_t)(*graph_field(data) >> 32));
                        goto end;
                }
 
-               if ((data->udata64 & 0xffff) != (i - count)) {
+               if ((*graph_field(data) & 0xffff) != (i - count)) {
                        printf("Expected buff count miss match at node 1\n");
                        goto end;
                }
 
-               if (data->udata64 & (0x1 << 16))
+               if (*graph_field(data) & (0x1 << 16))
                        count = i + 1;
-               if (data->udata64 & (0x1 << 17))
+               if (*graph_field(data) & (0x1 << 17))
                        second_pass = 1;
        }
 
@@ -312,11 +335,11 @@ test_node1_worker(struct rte_graph *graph, struct rte_node *node, void **objs,
        obj_node0 = nb_objs;
        for (i = 0; i < obj_node0; i++) {
                data = &mbuf[2][i];
-               data->udata64 = ((uint64_t)tm->test_node[2].idx << 32) | i;
+               *graph_field(data) = ((uint64_t)tm->test_node[2].idx << 32) | i;
                if ((i + 1) == obj_node0)
-                       data->udata64 |= (1 << 16);
+                       *graph_field(data) |= (1 << 16);
                if (second_pass)
-                       data->udata64 |= (1 << 17);
+                       *graph_field(data) |= (1 << 17);
        }
        rte_node_enqueue(graph, node, 0, (void **)&mbuf_p[2][0], obj_node0);
 
@@ -339,22 +362,22 @@ test_node2_worker(struct rte_graph *graph, struct rte_node *node, void **objs,
        fn_calls[3] += 1;
        for (i = 0; i < nb_objs; i++) {
                data = (struct rte_mbuf *)objs[i];
-               if ((data->udata64 >> 32) != tm->test_node[2].idx) {
+               if ((*graph_field(data) >> 32) != tm->test_node[2].idx) {
                        printf("Data idx miss match at node 2, expected = %u"
                               " got = %u\n",
                               tm->test_node[2].idx,
-                              (uint32_t)(data->udata64 >> 32));
+                              (uint32_t)(*graph_field(data) >> 32));
                        goto end;
                }
 
-               if ((data->udata64 & 0xffff) != (i - count)) {
+               if ((*graph_field(data) & 0xffff) != (i - count)) {
                        printf("Expected buff count miss match at node 2\n");
                        goto end;
                }
 
-               if (data->udata64 & (0x1 << 16))
+               if (*graph_field(data) & (0x1 << 16))
                        count = i + 1;
-               if (data->udata64 & (0x1 << 17))
+               if (*graph_field(data) & (0x1 << 17))
                        second_pass = 1;
        }
 
@@ -367,10 +390,10 @@ test_node2_worker(struct rte_graph *graph, struct rte_node *node, void **objs,
                obj_node0 = nb_objs;
                for (i = 0; i < obj_node0; i++) {
                        data = &mbuf[3][i];
-                       data->udata64 =
+                       *graph_field(data) =
                                ((uint64_t)tm->test_node[3].idx << 32) | i;
                        if ((i + 1) == obj_node0)
-                               data->udata64 |= (1 << 16);
+                               *graph_field(data) |= (1 << 16);
                }
                rte_node_enqueue(graph, node, 0, (void **)&mbuf_p[3][0],
                                 obj_node0);
@@ -395,22 +418,22 @@ test_node3_worker(struct rte_graph *graph, struct rte_node *node, void **objs,
        fn_calls[4] += 1;
        for (i = 0; i < nb_objs; i++) {
                data = (struct rte_mbuf *)objs[i];
-               if ((data->udata64 >> 32) != tm->test_node[3].idx) {
+               if ((*graph_field(data) >> 32) != tm->test_node[3].idx) {
                        printf("Data idx miss match at node 3, expected = %u"
                               " got = %u\n",
                               tm->test_node[3].idx,
-                              (uint32_t)(data->udata64 >> 32));
+                              (uint32_t)(*graph_field(data) >> 32));
                        goto end;
                }
 
-               if ((data->udata64 & 0xffff) != (i - count)) {
+               if ((*graph_field(data) & 0xffff) != (i - count)) {
                        printf("Expected buff count miss match at node 3\n");
                        goto end;
                }
 
-               if (data->udata64 & (0x1 << 16))
+               if (*graph_field(data) & (0x1 << 16))
                        count = i + 1;
-               if (data->udata64 & (0x1 << 17))
+               if (*graph_field(data) & (0x1 << 17))
                        second_pass = 1;
        }
 
@@ -426,11 +449,11 @@ test_node3_worker(struct rte_graph *graph, struct rte_node *node, void **objs,
                obj_node0 = nb_objs * 2;
                for (i = 0; i < obj_node0; i++) {
                        data = &mbuf[4][i];
-                       data->udata64 =
+                       *graph_field(data) =
                                ((uint64_t)tm->test_node[0].idx << 32) | i;
-                       data->udata64 |= (1 << 17);
+                       *graph_field(data) |= (1 << 17);
                        if ((i + 1) == obj_node0)
-                               data->udata64 |= (1 << 16);
+                               *graph_field(data) |= (1 << 16);
                }
                rte_node_enqueue(graph, node, 0, (void **)&mbuf_p[4][0],
                                 obj_node0);
@@ -497,6 +520,7 @@ test_lookup_functions(void)
                        printf("Test number of edges for node = %s failed Expected = %d, got %d\n",
                               tm->test_node[i].node.name,
                               tm->test_node[i].node.nb_edges, count);
+                       free(next_edges);
                        return -1;
                }
 
@@ -506,6 +530,7 @@ test_lookup_functions(void)
                                printf("Edge name miss match, expected = %s got = %s\n",
                                       tm->test_node[i].node.next_nodes[j],
                                       next_edges[j]);
+                               free(next_edges);
                                return -1;
                        }
                }
@@ -525,6 +550,12 @@ test_node_clone(void)
        node_id = rte_node_from_name("test_node00");
        tm->test_node[0].idx = node_id;
 
+       dummy_id = rte_node_clone(node_id, "test_node00");
+       if (rte_node_is_invalid(dummy_id)) {
+               printf("Got invalid id when clone, Expecting fail\n");
+               return -1;
+       }
+
        /* Clone with same name, should fail */
        dummy_id = rte_node_clone(node_id, "test_node00");
        if (!rte_node_is_invalid(dummy_id)) {
@@ -763,6 +794,18 @@ graph_setup(void)
 {
        int i, j;
 
+       static const struct rte_mbuf_dynfield graph_dynfield_desc = {
+               .name = "test_graph_dynfield",
+               .size = sizeof(graph_dynfield_t),
+               .align = __alignof__(graph_dynfield_t),
+       };
+       graph_dynfield_offset =
+               rte_mbuf_dynfield_register(&graph_dynfield_desc);
+       if (graph_dynfield_offset < 0) {
+               printf("Cannot register mbuf field\n");
+               return TEST_FAILED;
+       }
+
        for (i = 0; i <= MAX_NODES; i++) {
                for (j = 0; j < MBUFF_SIZE; j++)
                        mbuf_p[i][j] = &mbuf[i][j];
@@ -816,4 +859,7 @@ test_node_list_dump(void)
 
        return TEST_SUCCESS;
 }
+
+#endif /* !RTE_EXEC_ENV_WINDOWS */
+
 REGISTER_TEST_COMMAND(node_list_dump, test_node_list_dump);