raw/ioat: add unit tests for completion batching
[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 #include "ioat_private.h"
11
12 #define MAX_SUPPORTED_RAWDEVS 64
13 #define TEST_SKIPPED 77
14 #define COPY_LEN 1024
15
16 int ioat_rawdev_test(uint16_t dev_id); /* pre-define to keep compiler happy */
17
18 static struct rte_mempool *pool;
19 static unsigned short expected_ring_size[MAX_SUPPORTED_RAWDEVS];
20
21 #define PRINT_ERR(...) print_err(__func__, __LINE__, __VA_ARGS__)
22
23 static inline int
24 __rte_format_printf(3, 4)
25 print_err(const char *func, int lineno, const char *format, ...)
26 {
27         va_list ap;
28         int ret;
29
30         ret = fprintf(stderr, "In %s:%d - ", func, lineno);
31         va_start(ap, format);
32         ret += vfprintf(stderr, format, ap);
33         va_end(ap);
34
35         return ret;
36 }
37
38 static int
39 do_multi_copies(int dev_id, int split_batches, int split_completions)
40 {
41         struct rte_mbuf *srcs[32], *dsts[32];
42         struct rte_mbuf *completed_src[64];
43         struct rte_mbuf *completed_dst[64];
44         unsigned int i, j;
45
46         for (i = 0; i < RTE_DIM(srcs); i++) {
47                 char *src_data;
48
49                 if (split_batches && i == RTE_DIM(srcs) / 2)
50                         rte_ioat_perform_ops(dev_id);
51
52                 srcs[i] = rte_pktmbuf_alloc(pool);
53                 dsts[i] = rte_pktmbuf_alloc(pool);
54                 src_data = rte_pktmbuf_mtod(srcs[i], char *);
55
56                 for (j = 0; j < COPY_LEN; j++)
57                         src_data[j] = rand() & 0xFF;
58
59                 if (rte_ioat_enqueue_copy(dev_id,
60                                 srcs[i]->buf_iova + srcs[i]->data_off,
61                                 dsts[i]->buf_iova + dsts[i]->data_off,
62                                 COPY_LEN,
63                                 (uintptr_t)srcs[i],
64                                 (uintptr_t)dsts[i]) != 1) {
65                         PRINT_ERR("Error with rte_ioat_enqueue_copy for buffer %u\n",
66                                         i);
67                         return -1;
68                 }
69         }
70         rte_ioat_perform_ops(dev_id);
71         usleep(100);
72
73         if (split_completions) {
74                 /* gather completions in two halves */
75                 uint16_t half_len = RTE_DIM(srcs) / 2;
76                 if (rte_ioat_completed_ops(dev_id, half_len, (void *)completed_src,
77                                 (void *)completed_dst) != half_len) {
78                         PRINT_ERR("Error with rte_ioat_completed_ops - first half request\n");
79                         rte_rawdev_dump(dev_id, stdout);
80                         return -1;
81                 }
82                 if (rte_ioat_completed_ops(dev_id, half_len, (void *)&completed_src[half_len],
83                                 (void *)&completed_dst[half_len]) != half_len) {
84                         PRINT_ERR("Error with rte_ioat_completed_ops - second half request\n");
85                         rte_rawdev_dump(dev_id, stdout);
86                         return -1;
87                 }
88         } else {
89                 /* gather all completions in one go */
90                 if (rte_ioat_completed_ops(dev_id, 64, (void *)completed_src,
91                                 (void *)completed_dst) != RTE_DIM(srcs)) {
92                         PRINT_ERR("Error with rte_ioat_completed_ops\n");
93                         rte_rawdev_dump(dev_id, stdout);
94                         return -1;
95                 }
96         }
97         for (i = 0; i < RTE_DIM(srcs); i++) {
98                 char *src_data, *dst_data;
99
100                 if (completed_src[i] != srcs[i]) {
101                         PRINT_ERR("Error with source pointer %u\n", i);
102                         return -1;
103                 }
104                 if (completed_dst[i] != dsts[i]) {
105                         PRINT_ERR("Error with dest pointer %u\n", i);
106                         return -1;
107                 }
108
109                 src_data = rte_pktmbuf_mtod(srcs[i], char *);
110                 dst_data = rte_pktmbuf_mtod(dsts[i], char *);
111                 for (j = 0; j < COPY_LEN; j++)
112                         if (src_data[j] != dst_data[j]) {
113                                 PRINT_ERR("Error with copy of packet %u, byte %u\n",
114                                                 i, j);
115                                 return -1;
116                         }
117                 rte_pktmbuf_free(srcs[i]);
118                 rte_pktmbuf_free(dsts[i]);
119         }
120         return 0;
121 }
122
123 static int
124 test_enqueue_copies(int dev_id)
125 {
126         unsigned int i;
127
128         /* test doing a single copy */
129         do {
130                 struct rte_mbuf *src, *dst;
131                 char *src_data, *dst_data;
132                 struct rte_mbuf *completed[2] = {0};
133
134                 src = rte_pktmbuf_alloc(pool);
135                 dst = rte_pktmbuf_alloc(pool);
136                 src_data = rte_pktmbuf_mtod(src, char *);
137                 dst_data = rte_pktmbuf_mtod(dst, char *);
138
139                 for (i = 0; i < COPY_LEN; i++)
140                         src_data[i] = rand() & 0xFF;
141
142                 if (rte_ioat_enqueue_copy(dev_id,
143                                 src->buf_iova + src->data_off,
144                                 dst->buf_iova + dst->data_off,
145                                 COPY_LEN,
146                                 (uintptr_t)src,
147                                 (uintptr_t)dst) != 1) {
148                         PRINT_ERR("Error with rte_ioat_enqueue_copy\n");
149                         return -1;
150                 }
151                 rte_ioat_perform_ops(dev_id);
152                 usleep(10);
153
154                 if (rte_ioat_completed_ops(dev_id, 1, (void *)&completed[0],
155                                 (void *)&completed[1]) != 1) {
156                         PRINT_ERR("Error with rte_ioat_completed_ops\n");
157                         return -1;
158                 }
159                 if (completed[0] != src || completed[1] != dst) {
160                         PRINT_ERR("Error with completions: got (%p, %p), not (%p,%p)\n",
161                                         completed[0], completed[1], src, dst);
162                         return -1;
163                 }
164
165                 for (i = 0; i < COPY_LEN; i++)
166                         if (dst_data[i] != src_data[i]) {
167                                 PRINT_ERR("Data mismatch at char %u [Got %02x not %02x]\n",
168                                                 i, dst_data[i], src_data[i]);
169                                 return -1;
170                         }
171                 rte_pktmbuf_free(src);
172                 rte_pktmbuf_free(dst);
173         } while (0);
174
175         /* test doing a multiple single copies */
176         do {
177                 const uint16_t max_ops = 4;
178                 struct rte_mbuf *src, *dst;
179                 char *src_data, *dst_data;
180                 struct rte_mbuf *completed[32] = {0};
181                 const uint16_t max_completions = RTE_DIM(completed) / 2;
182
183                 src = rte_pktmbuf_alloc(pool);
184                 dst = rte_pktmbuf_alloc(pool);
185                 src_data = rte_pktmbuf_mtod(src, char *);
186                 dst_data = rte_pktmbuf_mtod(dst, char *);
187
188                 for (i = 0; i < COPY_LEN; i++)
189                         src_data[i] = rand() & 0xFF;
190
191                 /* perform the same copy <max_ops> times */
192                 for (i = 0; i < max_ops; i++) {
193                         if (rte_ioat_enqueue_copy(dev_id,
194                                         src->buf_iova + src->data_off,
195                                         dst->buf_iova + dst->data_off,
196                                         COPY_LEN,
197                                         (uintptr_t)src,
198                                         (uintptr_t)dst) != 1) {
199                                 PRINT_ERR("Error with rte_ioat_enqueue_copy\n");
200                                 return -1;
201                         }
202                         rte_ioat_perform_ops(dev_id);
203                 }
204                 usleep(10);
205
206                 if (rte_ioat_completed_ops(dev_id, max_completions, (void *)&completed[0],
207                                 (void *)&completed[max_completions]) != max_ops) {
208                         PRINT_ERR("Error with rte_ioat_completed_ops\n");
209                         return -1;
210                 }
211                 if (completed[0] != src || completed[max_completions] != dst) {
212                         PRINT_ERR("Error with completions: got (%p, %p), not (%p,%p)\n",
213                                         completed[0], completed[max_completions], src, dst);
214                         return -1;
215                 }
216
217                 for (i = 0; i < COPY_LEN; i++)
218                         if (dst_data[i] != src_data[i]) {
219                                 PRINT_ERR("Data mismatch at char %u\n", i);
220                                 return -1;
221                         }
222                 rte_pktmbuf_free(src);
223                 rte_pktmbuf_free(dst);
224         } while (0);
225
226         /* test doing multiple copies */
227         do_multi_copies(dev_id, 0, 0); /* enqueue and complete one batch at a time */
228         do_multi_copies(dev_id, 1, 0); /* enqueue 2 batches and then complete both */
229         do_multi_copies(dev_id, 0, 1); /* enqueue 1 batch, then complete in two halves */
230         return 0;
231 }
232
233 static int
234 test_enqueue_fill(int dev_id)
235 {
236         const unsigned int lengths[] = {8, 64, 1024, 50, 100, 89};
237         struct rte_mbuf *dst = rte_pktmbuf_alloc(pool);
238         char *dst_data = rte_pktmbuf_mtod(dst, char *);
239         struct rte_mbuf *completed[2] = {0};
240         uint64_t pattern = 0xfedcba9876543210;
241         unsigned int i, j;
242
243         for (i = 0; i < RTE_DIM(lengths); i++) {
244                 /* reset dst_data */
245                 memset(dst_data, 0, lengths[i]);
246
247                 /* perform the fill operation */
248                 if (rte_ioat_enqueue_fill(dev_id, pattern,
249                                 dst->buf_iova + dst->data_off, lengths[i],
250                                 (uintptr_t)dst) != 1) {
251                         PRINT_ERR("Error with rte_ioat_enqueue_fill\n");
252                         return -1;
253                 }
254
255                 rte_ioat_perform_ops(dev_id);
256                 usleep(100);
257
258                 if (rte_ioat_completed_ops(dev_id, 1, (void *)&completed[0],
259                         (void *)&completed[1]) != 1) {
260                         PRINT_ERR("Error with completed ops\n");
261                         return -1;
262                 }
263                 /* check the result */
264                 for (j = 0; j < lengths[i]; j++) {
265                         char pat_byte = ((char *)&pattern)[j % 8];
266                         if (dst_data[j] != pat_byte) {
267                                 PRINT_ERR("Error with fill operation (lengths = %u): got (%x), not (%x)\n",
268                                                 lengths[i], dst_data[j],
269                                                 pat_byte);
270                                 return -1;
271                         }
272                 }
273         }
274
275         rte_pktmbuf_free(dst);
276         return 0;
277 }
278
279 int
280 ioat_rawdev_test(uint16_t dev_id)
281 {
282 #define IOAT_TEST_RINGSIZE 512
283         struct rte_ioat_rawdev_config p = { .ring_size = -1 };
284         struct rte_rawdev_info info = { .dev_private = &p };
285         struct rte_rawdev_xstats_name *snames = NULL;
286         uint64_t *stats = NULL;
287         unsigned int *ids = NULL;
288         unsigned int nb_xstats;
289         unsigned int i;
290
291         if (dev_id >= MAX_SUPPORTED_RAWDEVS) {
292                 printf("Skipping test. Cannot test rawdevs with id's greater than %d\n",
293                                 MAX_SUPPORTED_RAWDEVS);
294                 return TEST_SKIPPED;
295         }
296
297         rte_rawdev_info_get(dev_id, &info, sizeof(p));
298         if (p.ring_size != expected_ring_size[dev_id]) {
299                 PRINT_ERR("Error, initial ring size is not as expected (Actual: %d, Expected: %d)\n",
300                                 (int)p.ring_size, expected_ring_size[dev_id]);
301                 return -1;
302         }
303
304         p.ring_size = IOAT_TEST_RINGSIZE;
305         if (rte_rawdev_configure(dev_id, &info, sizeof(p)) != 0) {
306                 PRINT_ERR("Error with rte_rawdev_configure()\n");
307                 return -1;
308         }
309         rte_rawdev_info_get(dev_id, &info, sizeof(p));
310         if (p.ring_size != IOAT_TEST_RINGSIZE) {
311                 PRINT_ERR("Error, ring size is not %d (%d)\n",
312                                 IOAT_TEST_RINGSIZE, (int)p.ring_size);
313                 return -1;
314         }
315         expected_ring_size[dev_id] = p.ring_size;
316
317         if (rte_rawdev_start(dev_id) != 0) {
318                 PRINT_ERR("Error with rte_rawdev_start()\n");
319                 return -1;
320         }
321
322         pool = rte_pktmbuf_pool_create("TEST_IOAT_POOL",
323                         256, /* n == num elements */
324                         32,  /* cache size */
325                         0,   /* priv size */
326                         2048, /* data room size */
327                         info.socket_id);
328         if (pool == NULL) {
329                 PRINT_ERR("Error with mempool creation\n");
330                 return -1;
331         }
332
333         /* allocate memory for xstats names and values */
334         nb_xstats = rte_rawdev_xstats_names_get(dev_id, NULL, 0);
335
336         snames = malloc(sizeof(*snames) * nb_xstats);
337         if (snames == NULL) {
338                 PRINT_ERR("Error allocating xstat names memory\n");
339                 goto err;
340         }
341         rte_rawdev_xstats_names_get(dev_id, snames, nb_xstats);
342
343         ids = malloc(sizeof(*ids) * nb_xstats);
344         if (ids == NULL) {
345                 PRINT_ERR("Error allocating xstat ids memory\n");
346                 goto err;
347         }
348         for (i = 0; i < nb_xstats; i++)
349                 ids[i] = i;
350
351         stats = malloc(sizeof(*stats) * nb_xstats);
352         if (stats == NULL) {
353                 PRINT_ERR("Error allocating xstat memory\n");
354                 goto err;
355         }
356
357         /* run the test cases */
358         printf("Running Copy Tests\n");
359         for (i = 0; i < 100; i++) {
360                 unsigned int j;
361
362                 if (test_enqueue_copies(dev_id) != 0)
363                         goto err;
364
365                 rte_rawdev_xstats_get(dev_id, ids, stats, nb_xstats);
366                 for (j = 0; j < nb_xstats; j++)
367                         printf("%s: %"PRIu64"   ", snames[j].name, stats[j]);
368                 printf("\r");
369         }
370         printf("\n");
371
372         /* test enqueue fill operation */
373         printf("Running Fill Tests\n");
374         for (i = 0; i < 100; i++) {
375                 unsigned int j;
376
377                 if (test_enqueue_fill(dev_id) != 0)
378                         goto err;
379
380                 rte_rawdev_xstats_get(dev_id, ids, stats, nb_xstats);
381                 for (j = 0; j < nb_xstats; j++)
382                         printf("%s: %"PRIu64"   ", snames[j].name, stats[j]);
383                 printf("\r");
384         }
385         printf("\n");
386
387         rte_rawdev_stop(dev_id);
388         if (rte_rawdev_xstats_reset(dev_id, NULL, 0) != 0) {
389                 PRINT_ERR("Error resetting xstat values\n");
390                 goto err;
391         }
392
393         rte_mempool_free(pool);
394         free(snames);
395         free(stats);
396         free(ids);
397         return 0;
398
399 err:
400         rte_rawdev_stop(dev_id);
401         rte_rawdev_xstats_reset(dev_id, NULL, 0);
402         rte_mempool_free(pool);
403         free(snames);
404         free(stats);
405         free(ids);
406         return -1;
407 }