raw/ioat: include extra info in test errors
[dpdk.git] / drivers / raw / ioat / ioat_rawdev_test.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4
5 #include <unistd.h>
6 #include <inttypes.h>
7 #include <rte_mbuf.h>
8 #include "rte_rawdev.h"
9 #include "rte_ioat_rawdev.h"
10
11 #define MAX_SUPPORTED_RAWDEVS 64
12 #define TEST_SKIPPED 77
13
14 int ioat_rawdev_test(uint16_t dev_id); /* pre-define to keep compiler happy */
15
16 static struct rte_mempool *pool;
17 static unsigned short expected_ring_size[MAX_SUPPORTED_RAWDEVS];
18
19 #define PRINT_ERR(...) print_err(__func__, __LINE__, __VA_ARGS__)
20
21 static inline int
22 __rte_format_printf(3, 4)
23 print_err(const char *func, int lineno, const char *format, ...)
24 {
25         va_list ap;
26         int ret;
27
28         ret = fprintf(stderr, "In %s:%d - ", func, lineno);
29         va_start(ap, format);
30         ret += vfprintf(stderr, format, ap);
31         va_end(ap);
32
33         return ret;
34 }
35
36 static int
37 test_enqueue_copies(int dev_id)
38 {
39         const unsigned int length = 1024;
40         unsigned int i;
41
42         do {
43                 struct rte_mbuf *src, *dst;
44                 char *src_data, *dst_data;
45                 struct rte_mbuf *completed[2] = {0};
46
47                 /* test doing a single copy */
48                 src = rte_pktmbuf_alloc(pool);
49                 dst = rte_pktmbuf_alloc(pool);
50                 src->data_len = src->pkt_len = length;
51                 dst->data_len = dst->pkt_len = length;
52                 src_data = rte_pktmbuf_mtod(src, char *);
53                 dst_data = rte_pktmbuf_mtod(dst, char *);
54
55                 for (i = 0; i < length; i++)
56                         src_data[i] = rand() & 0xFF;
57
58                 if (rte_ioat_enqueue_copy(dev_id,
59                                 src->buf_iova + src->data_off,
60                                 dst->buf_iova + dst->data_off,
61                                 length,
62                                 (uintptr_t)src,
63                                 (uintptr_t)dst,
64                                 0 /* no fence */) != 1) {
65                         PRINT_ERR("Error with rte_ioat_enqueue_copy\n");
66                         return -1;
67                 }
68                 rte_ioat_do_copies(dev_id);
69                 usleep(10);
70
71                 if (rte_ioat_completed_copies(dev_id, 1, (void *)&completed[0],
72                                 (void *)&completed[1]) != 1) {
73                         PRINT_ERR("Error with rte_ioat_completed_copies\n");
74                         return -1;
75                 }
76                 if (completed[0] != src || completed[1] != dst) {
77                         PRINT_ERR("Error with completions: got (%p, %p), not (%p,%p)\n",
78                                         completed[0], completed[1], src, dst);
79                         return -1;
80                 }
81
82                 for (i = 0; i < length; i++)
83                         if (dst_data[i] != src_data[i]) {
84                                 PRINT_ERR("Data mismatch at char %u\n", i);
85                                 return -1;
86                         }
87                 rte_pktmbuf_free(src);
88                 rte_pktmbuf_free(dst);
89         } while (0);
90
91         /* test doing multiple copies */
92         do {
93                 struct rte_mbuf *srcs[32], *dsts[32];
94                 struct rte_mbuf *completed_src[64];
95                 struct rte_mbuf *completed_dst[64];
96                 unsigned int j;
97
98                 for (i = 0; i < RTE_DIM(srcs); i++) {
99                         char *src_data;
100
101                         srcs[i] = rte_pktmbuf_alloc(pool);
102                         dsts[i] = rte_pktmbuf_alloc(pool);
103                         srcs[i]->data_len = srcs[i]->pkt_len = length;
104                         dsts[i]->data_len = dsts[i]->pkt_len = length;
105                         src_data = rte_pktmbuf_mtod(srcs[i], char *);
106
107                         for (j = 0; j < length; j++)
108                                 src_data[j] = rand() & 0xFF;
109
110                         if (rte_ioat_enqueue_copy(dev_id,
111                                         srcs[i]->buf_iova + srcs[i]->data_off,
112                                         dsts[i]->buf_iova + dsts[i]->data_off,
113                                         length,
114                                         (uintptr_t)srcs[i],
115                                         (uintptr_t)dsts[i],
116                                         0 /* nofence */) != 1) {
117                                 PRINT_ERR("Error with rte_ioat_enqueue_copy for buffer %u\n",
118                                                 i);
119                                 return -1;
120                         }
121                 }
122                 rte_ioat_do_copies(dev_id);
123                 usleep(100);
124
125                 if (rte_ioat_completed_copies(dev_id, 64, (void *)completed_src,
126                                 (void *)completed_dst) != RTE_DIM(srcs)) {
127                         PRINT_ERR("Error with rte_ioat_completed_copies\n");
128                         return -1;
129                 }
130                 for (i = 0; i < RTE_DIM(srcs); i++) {
131                         char *src_data, *dst_data;
132
133                         if (completed_src[i] != srcs[i]) {
134                                 PRINT_ERR("Error with source pointer %u\n", i);
135                                 return -1;
136                         }
137                         if (completed_dst[i] != dsts[i]) {
138                                 PRINT_ERR("Error with dest pointer %u\n", i);
139                                 return -1;
140                         }
141
142                         src_data = rte_pktmbuf_mtod(srcs[i], char *);
143                         dst_data = rte_pktmbuf_mtod(dsts[i], char *);
144                         for (j = 0; j < length; j++)
145                                 if (src_data[j] != dst_data[j]) {
146                                         PRINT_ERR("Error with copy of packet %u, byte %u\n",
147                                                         i, j);
148                                         return -1;
149                                 }
150                         rte_pktmbuf_free(srcs[i]);
151                         rte_pktmbuf_free(dsts[i]);
152                 }
153
154         } while (0);
155
156         return 0;
157 }
158
159 int
160 ioat_rawdev_test(uint16_t dev_id)
161 {
162 #define IOAT_TEST_RINGSIZE 512
163         struct rte_ioat_rawdev_config p = { .ring_size = -1 };
164         struct rte_rawdev_info info = { .dev_private = &p };
165         struct rte_rawdev_xstats_name *snames = NULL;
166         uint64_t *stats = NULL;
167         unsigned int *ids = NULL;
168         unsigned int nb_xstats;
169         unsigned int i;
170
171         if (dev_id >= MAX_SUPPORTED_RAWDEVS) {
172                 printf("Skipping test. Cannot test rawdevs with id's greater than %d\n",
173                                 MAX_SUPPORTED_RAWDEVS);
174                 return TEST_SKIPPED;
175         }
176
177         rte_rawdev_info_get(dev_id, &info, sizeof(p));
178         if (p.ring_size != expected_ring_size[dev_id]) {
179                 PRINT_ERR("Error, initial ring size is not as expected (Actual: %d, Expected: %d)\n",
180                                 (int)p.ring_size, expected_ring_size[dev_id]);
181                 return -1;
182         }
183
184         p.ring_size = IOAT_TEST_RINGSIZE;
185         if (rte_rawdev_configure(dev_id, &info, sizeof(p)) != 0) {
186                 PRINT_ERR("Error with rte_rawdev_configure()\n");
187                 return -1;
188         }
189         rte_rawdev_info_get(dev_id, &info, sizeof(p));
190         if (p.ring_size != IOAT_TEST_RINGSIZE) {
191                 PRINT_ERR("Error, ring size is not %d (%d)\n",
192                                 IOAT_TEST_RINGSIZE, (int)p.ring_size);
193                 return -1;
194         }
195         expected_ring_size[dev_id] = p.ring_size;
196
197         if (rte_rawdev_start(dev_id) != 0) {
198                 PRINT_ERR("Error with rte_rawdev_start()\n");
199                 return -1;
200         }
201
202         pool = rte_pktmbuf_pool_create("TEST_IOAT_POOL",
203                         256, /* n == num elements */
204                         32,  /* cache size */
205                         0,   /* priv size */
206                         2048, /* data room size */
207                         info.socket_id);
208         if (pool == NULL) {
209                 PRINT_ERR("Error with mempool creation\n");
210                 return -1;
211         }
212
213         /* allocate memory for xstats names and values */
214         nb_xstats = rte_rawdev_xstats_names_get(dev_id, NULL, 0);
215
216         snames = malloc(sizeof(*snames) * nb_xstats);
217         if (snames == NULL) {
218                 PRINT_ERR("Error allocating xstat names memory\n");
219                 goto err;
220         }
221         rte_rawdev_xstats_names_get(dev_id, snames, nb_xstats);
222
223         ids = malloc(sizeof(*ids) * nb_xstats);
224         if (ids == NULL) {
225                 PRINT_ERR("Error allocating xstat ids memory\n");
226                 goto err;
227         }
228         for (i = 0; i < nb_xstats; i++)
229                 ids[i] = i;
230
231         stats = malloc(sizeof(*stats) * nb_xstats);
232         if (stats == NULL) {
233                 PRINT_ERR("Error allocating xstat memory\n");
234                 goto err;
235         }
236
237         /* run the test cases */
238         for (i = 0; i < 100; i++) {
239                 unsigned int j;
240
241                 if (test_enqueue_copies(dev_id) != 0)
242                         goto err;
243
244                 rte_rawdev_xstats_get(dev_id, ids, stats, nb_xstats);
245                 for (j = 0; j < nb_xstats; j++)
246                         printf("%s: %"PRIu64"   ", snames[j].name, stats[j]);
247                 printf("\r");
248         }
249         printf("\n");
250
251         rte_rawdev_stop(dev_id);
252         if (rte_rawdev_xstats_reset(dev_id, NULL, 0) != 0) {
253                 PRINT_ERR("Error resetting xstat values\n");
254                 goto err;
255         }
256
257         rte_mempool_free(pool);
258         free(snames);
259         free(stats);
260         free(ids);
261         return 0;
262
263 err:
264         rte_rawdev_stop(dev_id);
265         rte_rawdev_xstats_reset(dev_id, NULL, 0);
266         rte_mempool_free(pool);
267         free(snames);
268         free(stats);
269         free(ids);
270         return -1;
271 }