New upstream version 18.11-rc1
[deb_dpdk.git] / test / test / test_external_mem.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <errno.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <sys/mman.h>
11 #include <sys/wait.h>
12
13 #include <rte_common.h>
14 #include <rte_debug.h>
15 #include <rte_eal.h>
16 #include <rte_errno.h>
17 #include <rte_malloc.h>
18 #include <rte_ring.h>
19 #include <rte_string_fns.h>
20
21 #include "test.h"
22
23 #define EXTERNAL_MEM_SZ (RTE_PGSIZE_4K << 10) /* 4M of data */
24
25 static int
26 test_invalid_param(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
27                 int n_pages)
28 {
29         static const char * const names[] = {
30                 NULL, /* NULL name */
31                 "",   /* empty name */
32                 "this heap name is definitely way too long to be valid"
33         };
34         const char *valid_name = "valid heap name";
35         unsigned int i;
36
37         /* check invalid name handling */
38         for (i = 0; i < RTE_DIM(names); i++) {
39                 const char *name = names[i];
40
41                 /* these calls may fail for other reasons, so check errno */
42                 if (rte_malloc_heap_create(name) >= 0 || rte_errno != EINVAL) {
43                         printf("%s():%i: Created heap with invalid name\n",
44                                         __func__, __LINE__);
45                         goto fail;
46                 }
47
48                 if (rte_malloc_heap_destroy(name) >= 0 || rte_errno != EINVAL) {
49                         printf("%s():%i: Destroyed heap with invalid name\n",
50                                         __func__, __LINE__);
51                         goto fail;
52                 }
53
54                 if (rte_malloc_heap_get_socket(name) >= 0 ||
55                                 rte_errno != EINVAL) {
56                         printf("%s():%i: Found socket for heap with invalid name\n",
57                                         __func__, __LINE__);
58                         goto fail;
59                 }
60
61                 if (rte_malloc_heap_memory_add(name, addr, len,
62                                 NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
63                         printf("%s():%i: Added memory to heap with invalid name\n",
64                                         __func__, __LINE__);
65                         goto fail;
66                 }
67                 if (rte_malloc_heap_memory_remove(name, addr, len) >= 0 ||
68                                 rte_errno != EINVAL) {
69                         printf("%s():%i: Removed memory from heap with invalid name\n",
70                                         __func__, __LINE__);
71                         goto fail;
72                 }
73
74                 if (rte_malloc_heap_memory_attach(name, addr, len) >= 0 ||
75                                 rte_errno != EINVAL) {
76                         printf("%s():%i: Attached memory to heap with invalid name\n",
77                                 __func__, __LINE__);
78                         goto fail;
79                 }
80                 if (rte_malloc_heap_memory_detach(name, addr, len) >= 0 ||
81                                 rte_errno != EINVAL) {
82                         printf("%s():%i: Detached memory from heap with invalid name\n",
83                                 __func__, __LINE__);
84                         goto fail;
85                 }
86         }
87
88         /* do same as above, but with a valid heap name */
89
90         /* skip create call */
91         if (rte_malloc_heap_destroy(valid_name) >= 0 || rte_errno != ENOENT) {
92                 printf("%s():%i: Destroyed heap with invalid name\n",
93                         __func__, __LINE__);
94                 goto fail;
95         }
96         if (rte_malloc_heap_get_socket(valid_name) >= 0 ||
97                         rte_errno != ENOENT) {
98                 printf("%s():%i: Found socket for heap with invalid name\n",
99                                 __func__, __LINE__);
100                 goto fail;
101         }
102
103         /* these calls may fail for other reasons, so check errno */
104         if (rte_malloc_heap_memory_add(valid_name, addr, len,
105                         NULL, 0, pgsz) >= 0 || rte_errno != ENOENT) {
106                 printf("%s():%i: Added memory to non-existent heap\n",
107                         __func__, __LINE__);
108                 goto fail;
109         }
110         if (rte_malloc_heap_memory_remove(valid_name, addr, len) >= 0 ||
111                         rte_errno != ENOENT) {
112                 printf("%s():%i: Removed memory from non-existent heap\n",
113                         __func__, __LINE__);
114                 goto fail;
115         }
116
117         if (rte_malloc_heap_memory_attach(valid_name, addr, len) >= 0 ||
118                         rte_errno != ENOENT) {
119                 printf("%s():%i: Attached memory to non-existent heap\n",
120                         __func__, __LINE__);
121                 goto fail;
122         }
123         if (rte_malloc_heap_memory_detach(valid_name, addr, len) >= 0 ||
124                         rte_errno != ENOENT) {
125                 printf("%s():%i: Detached memory from non-existent heap\n",
126                         __func__, __LINE__);
127                 goto fail;
128         }
129
130         /* create a valid heap but test other invalid parameters */
131         if (rte_malloc_heap_create(valid_name) != 0) {
132                 printf("%s():%i: Failed to create valid heap\n",
133                         __func__, __LINE__);
134                 goto fail;
135         }
136
137         /* zero length */
138         if (rte_malloc_heap_memory_add(valid_name, addr, 0,
139                         NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
140                 printf("%s():%i: Added memory with invalid parameters\n",
141                         __func__, __LINE__);
142                 goto fail;
143         }
144
145         if (rte_malloc_heap_memory_remove(valid_name, addr, 0) >= 0 ||
146                         rte_errno != EINVAL) {
147                 printf("%s():%i: Removed memory with invalid parameters\n",
148                         __func__, __LINE__);
149                 goto fail;
150         }
151
152         if (rte_malloc_heap_memory_attach(valid_name, addr, 0) >= 0 ||
153                         rte_errno != EINVAL) {
154                 printf("%s():%i: Attached memory with invalid parameters\n",
155                         __func__, __LINE__);
156                 goto fail;
157         }
158         if (rte_malloc_heap_memory_detach(valid_name, addr, 0) >= 0 ||
159                         rte_errno != EINVAL) {
160                 printf("%s():%i: Detached memory with invalid parameters\n",
161                         __func__, __LINE__);
162                 goto fail;
163         }
164
165         /* zero address */
166         if (rte_malloc_heap_memory_add(valid_name, NULL, len,
167                         NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
168                 printf("%s():%i: Added memory with invalid parameters\n",
169                         __func__, __LINE__);
170                 goto fail;
171         }
172
173         if (rte_malloc_heap_memory_remove(valid_name, NULL, len) >= 0 ||
174                         rte_errno != EINVAL) {
175                 printf("%s():%i: Removed memory with invalid parameters\n",
176                         __func__, __LINE__);
177                 goto fail;
178         }
179
180         if (rte_malloc_heap_memory_attach(valid_name, NULL, len) >= 0 ||
181                         rte_errno != EINVAL) {
182                 printf("%s():%i: Attached memory with invalid parameters\n",
183                         __func__, __LINE__);
184                 goto fail;
185         }
186         if (rte_malloc_heap_memory_detach(valid_name, NULL, len) >= 0 ||
187                         rte_errno != EINVAL) {
188                 printf("%s():%i: Detached memory with invalid parameters\n",
189                         __func__, __LINE__);
190                 goto fail;
191         }
192
193         /* wrong page count */
194         if (rte_malloc_heap_memory_add(valid_name, addr, len,
195                         iova, 0, pgsz) >= 0 || rte_errno != EINVAL) {
196                 printf("%s():%i: Added memory with invalid parameters\n",
197                         __func__, __LINE__);
198                 goto fail;
199         }
200         if (rte_malloc_heap_memory_add(valid_name, addr, len,
201                         iova, n_pages - 1, pgsz) >= 0 || rte_errno != EINVAL) {
202                 printf("%s():%i: Added memory with invalid parameters\n",
203                         __func__, __LINE__);
204                 goto fail;
205         }
206         if (rte_malloc_heap_memory_add(valid_name, addr, len,
207                         iova, n_pages + 1, pgsz) >= 0 || rte_errno != EINVAL) {
208                 printf("%s():%i: Added memory with invalid parameters\n",
209                         __func__, __LINE__);
210                 goto fail;
211         }
212
213         /* tests passed, destroy heap */
214         if (rte_malloc_heap_destroy(valid_name) != 0) {
215                 printf("%s():%i: Failed to destroy valid heap\n",
216                         __func__, __LINE__);
217                 goto fail;
218         }
219         return 0;
220 fail:
221         rte_malloc_heap_destroy(valid_name);
222         return -1;
223 }
224
225 static int
226 test_basic(void *addr, size_t len, size_t pgsz, rte_iova_t *iova, int n_pages)
227 {
228         const char *heap_name = "heap";
229         void *ptr = NULL;
230         int socket_id, i;
231         const struct rte_memzone *mz = NULL;
232
233         /* create heap */
234         if (rte_malloc_heap_create(heap_name) != 0) {
235                 printf("%s():%i: Failed to create malloc heap\n",
236                         __func__, __LINE__);
237                 goto fail;
238         }
239
240         /* get socket ID corresponding to this heap */
241         socket_id = rte_malloc_heap_get_socket(heap_name);
242         if (socket_id < 0) {
243                 printf("%s():%i: cannot find socket for external heap\n",
244                         __func__, __LINE__);
245                 goto fail;
246         }
247
248         /* heap is empty, so any allocation should fail */
249         ptr = rte_malloc_socket("EXTMEM", 64, 0, socket_id);
250         if (ptr != NULL) {
251                 printf("%s():%i: Allocated from empty heap\n", __func__,
252                         __LINE__);
253                 goto fail;
254         }
255
256         /* add memory to heap */
257         if (rte_malloc_heap_memory_add(heap_name, addr, len,
258                         iova, n_pages, pgsz) != 0) {
259                 printf("%s():%i: Failed to add memory to heap\n",
260                         __func__, __LINE__);
261                 goto fail;
262         }
263
264         /* check that we can get this memory from EAL now */
265         for (i = 0; i < n_pages; i++) {
266                 const struct rte_memseg *ms;
267                 void *cur = RTE_PTR_ADD(addr, pgsz * i);
268
269                 ms = rte_mem_virt2memseg(cur, NULL);
270                 if (ms == NULL) {
271                         printf("%s():%i: Failed to retrieve memseg for external mem\n",
272                                 __func__, __LINE__);
273                         goto fail;
274                 }
275                 if (ms->addr != cur) {
276                         printf("%s():%i: VA mismatch\n", __func__, __LINE__);
277                         goto fail;
278                 }
279                 if (ms->iova != iova[i]) {
280                         printf("%s():%i: IOVA mismatch\n", __func__, __LINE__);
281                         goto fail;
282                 }
283         }
284
285         /* allocate - this now should succeed */
286         ptr = rte_malloc_socket("EXTMEM", 64, 0, socket_id);
287         if (ptr == NULL) {
288                 printf("%s():%i: Failed to allocate from external heap\n",
289                         __func__, __LINE__);
290                 goto fail;
291         }
292
293         /* check if address is in expected range */
294         if (ptr < addr || ptr >= RTE_PTR_ADD(addr, len)) {
295                 printf("%s():%i: Allocated from unexpected address space\n",
296                         __func__, __LINE__);
297                 goto fail;
298         }
299
300         /* we've allocated something - removing memory should fail */
301         if (rte_malloc_heap_memory_remove(heap_name, addr, len) >= 0 ||
302                         rte_errno != EBUSY) {
303                 printf("%s():%i: Removing memory succeeded when memory is not free\n",
304                         __func__, __LINE__);
305                 goto fail;
306         }
307         if (rte_malloc_heap_destroy(heap_name) >= 0 || rte_errno != EBUSY) {
308                 printf("%s():%i: Destroying heap succeeded when memory is not free\n",
309                         __func__, __LINE__);
310                 goto fail;
311         }
312
313         /* try allocating an IOVA-contiguous memzone - this should succeed
314          * because we've set up a contiguous IOVA table.
315          */
316         mz = rte_memzone_reserve("heap_test", pgsz * 2, socket_id,
317                         RTE_MEMZONE_IOVA_CONTIG);
318         if (mz == NULL) {
319                 printf("%s():%i: Failed to reserve memzone\n",
320                         __func__, __LINE__);
321                 goto fail;
322         }
323
324         rte_malloc_dump_stats(stdout, NULL);
325         rte_malloc_dump_heaps(stdout);
326
327         /* free memory - removing it should now succeed */
328         rte_free(ptr);
329         ptr = NULL;
330
331         rte_memzone_free(mz);
332         mz = NULL;
333
334         if (rte_malloc_heap_memory_remove(heap_name, addr, len) != 0) {
335                 printf("%s():%i: Removing memory from heap failed\n",
336                         __func__, __LINE__);
337                 goto fail;
338         }
339         if (rte_malloc_heap_destroy(heap_name) != 0) {
340                 printf("%s():%i: Destroying heap failed\n",
341                         __func__, __LINE__);
342                 goto fail;
343         }
344
345         return 0;
346 fail:
347         rte_memzone_free(mz);
348         rte_free(ptr);
349         /* even if something failed, attempt to clean up */
350         rte_malloc_heap_memory_remove(heap_name, addr, len);
351         rte_malloc_heap_destroy(heap_name);
352
353         return -1;
354 }
355
356 /* we need to test attach/detach in secondary processes. */
357 static int
358 test_external_mem(void)
359 {
360         size_t len = EXTERNAL_MEM_SZ;
361         size_t pgsz = RTE_PGSIZE_4K;
362         rte_iova_t iova[len / pgsz];
363         void *addr;
364         int ret, n_pages;
365         int i;
366
367         /* create external memory area */
368         n_pages = RTE_DIM(iova);
369         addr = mmap(NULL, len, PROT_WRITE | PROT_READ,
370                         MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
371         if (addr == MAP_FAILED) {
372                 printf("%s():%i: Failed to create dummy memory area\n",
373                         __func__, __LINE__);
374                 return -1;
375         }
376         for (i = 0; i < n_pages; i++) {
377                 /* arbitrary IOVA */
378                 rte_iova_t tmp = 0x100000000 + i * pgsz;
379                 iova[i] = tmp;
380         }
381
382         ret = test_invalid_param(addr, len, pgsz, iova, n_pages);
383         ret |= test_basic(addr, len, pgsz, iova, n_pages);
384
385         munmap(addr, len);
386
387         return ret;
388 }
389
390 REGISTER_TEST_COMMAND(external_mem_autotest, test_external_mem);