New upstream version 18.02
[deb_dpdk.git] / test / test / test_memzone.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <string.h>
8 #include <inttypes.h>
9 #include <sys/queue.h>
10
11 #include <rte_random.h>
12 #include <rte_cycles.h>
13 #include <rte_memory.h>
14 #include <rte_memzone.h>
15 #include <rte_eal.h>
16 #include <rte_eal_memconfig.h>
17 #include <rte_common.h>
18 #include <rte_string_fns.h>
19 #include <rte_errno.h>
20 #include <rte_malloc.h>
21 #include "../../lib/librte_eal/common/malloc_elem.h"
22
23 #include "test.h"
24
25 /*
26  * Memzone
27  * =======
28  *
29  * - Search for three reserved zones or reserve them if they do not exist:
30  *
31  *   - One is on any socket id.
32  *   - The second is on socket 0.
33  *   - The last one is on socket 1 (if socket 1 exists).
34  *
35  * - Check that the zones exist.
36  *
37  * - Check that the zones are cache-aligned.
38  *
39  * - Check that zones do not overlap.
40  *
41  * - Check that the zones are on the correct socket id.
42  *
43  * - Check that a lookup of the first zone returns the same pointer.
44  *
45  * - Check that it is not possible to create another zone with the
46  *   same name as an existing zone.
47  *
48  * - Check flags for specific huge page size reservation
49  */
50
51 #define TEST_MEMZONE_NAME(suffix) "MZ_TEST_" suffix
52
53 /* Test if memory overlaps: return 1 if true, or 0 if false. */
54 static int
55 is_memory_overlap(rte_iova_t ptr1, size_t len1, rte_iova_t ptr2, size_t len2)
56 {
57         if (ptr2 >= ptr1 && (ptr2 - ptr1) < len1)
58                 return 1;
59         else if (ptr2 < ptr1 && (ptr1 - ptr2) < len2)
60                 return 1;
61         return 0;
62 }
63
64 static int
65 test_memzone_invalid_alignment(void)
66 {
67         const struct rte_memzone * mz;
68
69         mz = rte_memzone_lookup(TEST_MEMZONE_NAME("invalid_alignment"));
70         if (mz != NULL) {
71                 printf("Zone with invalid alignment has been reserved\n");
72                 return -1;
73         }
74
75         mz = rte_memzone_reserve_aligned(TEST_MEMZONE_NAME("invalid_alignment"),
76                                          100, SOCKET_ID_ANY, 0, 100);
77         if (mz != NULL) {
78                 printf("Zone with invalid alignment has been reserved\n");
79                 return -1;
80         }
81         return 0;
82 }
83
84 static int
85 test_memzone_reserving_zone_size_bigger_than_the_maximum(void)
86 {
87         const struct rte_memzone * mz;
88
89         mz = rte_memzone_lookup(
90                         TEST_MEMZONE_NAME("zone_size_bigger_than_the_maximum"));
91         if (mz != NULL) {
92                 printf("zone_size_bigger_than_the_maximum has been reserved\n");
93                 return -1;
94         }
95
96         mz = rte_memzone_reserve(
97                         TEST_MEMZONE_NAME("zone_size_bigger_than_the_maximum"),
98                         (size_t)-1, SOCKET_ID_ANY, 0);
99         if (mz != NULL) {
100                 printf("It is impossible to reserve such big a memzone\n");
101                 return -1;
102         }
103
104         return 0;
105 }
106
107 static int
108 test_memzone_reserve_flags(void)
109 {
110         const struct rte_memzone *mz;
111         const struct rte_memseg *ms;
112         int hugepage_2MB_avail = 0;
113         int hugepage_1GB_avail = 0;
114         int hugepage_16MB_avail = 0;
115         int hugepage_16GB_avail = 0;
116         const size_t size = 100;
117         int i = 0;
118         ms = rte_eal_get_physmem_layout();
119         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
120                 if (ms[i].hugepage_sz == RTE_PGSIZE_2M)
121                         hugepage_2MB_avail = 1;
122                 if (ms[i].hugepage_sz == RTE_PGSIZE_1G)
123                         hugepage_1GB_avail = 1;
124                 if (ms[i].hugepage_sz == RTE_PGSIZE_16M)
125                         hugepage_16MB_avail = 1;
126                 if (ms[i].hugepage_sz == RTE_PGSIZE_16G)
127                         hugepage_16GB_avail = 1;
128         }
129         /* Display the availability of 2MB ,1GB, 16MB, 16GB pages */
130         if (hugepage_2MB_avail)
131                 printf("2MB Huge pages available\n");
132         if (hugepage_1GB_avail)
133                 printf("1GB Huge pages available\n");
134         if (hugepage_16MB_avail)
135                 printf("16MB Huge pages available\n");
136         if (hugepage_16GB_avail)
137                 printf("16GB Huge pages available\n");
138         /*
139          * If 2MB pages available, check that a small memzone is correctly
140          * reserved from 2MB huge pages when requested by the RTE_MEMZONE_2MB flag.
141          * Also check that RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an
142          * available page size (i.e 1GB ) when 2MB pages are unavailable.
143          */
144         if (hugepage_2MB_avail) {
145                 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_2M"),
146                                 size, SOCKET_ID_ANY, RTE_MEMZONE_2MB);
147                 if (mz == NULL) {
148                         printf("MEMZONE FLAG 2MB\n");
149                         return -1;
150                 }
151                 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
152                         printf("hugepage_sz not equal 2M\n");
153                         return -1;
154                 }
155                 if (rte_memzone_free(mz)) {
156                         printf("Fail memzone free\n");
157                         return -1;
158                 }
159
160                 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
161                                 size, SOCKET_ID_ANY,
162                                 RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
163                 if (mz == NULL) {
164                         printf("MEMZONE FLAG 2MB\n");
165                         return -1;
166                 }
167                 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
168                         printf("hugepage_sz not equal 2M\n");
169                         return -1;
170                 }
171                 if (rte_memzone_free(mz)) {
172                         printf("Fail memzone free\n");
173                         return -1;
174                 }
175
176                 /* Check if 1GB huge pages are unavailable, that function fails unless
177                  * HINT flag is indicated
178                  */
179                 if (!hugepage_1GB_avail) {
180                         mz = rte_memzone_reserve(
181                                         TEST_MEMZONE_NAME("flag_zone_1G_HINT"),
182                                         size, SOCKET_ID_ANY,
183                                         RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
184                         if (mz == NULL) {
185                                 printf("MEMZONE FLAG 1GB & HINT\n");
186                                 return -1;
187                         }
188                         if (mz->hugepage_sz != RTE_PGSIZE_2M) {
189                                 printf("hugepage_sz not equal 2M\n");
190                                 return -1;
191                         }
192                         if (rte_memzone_free(mz)) {
193                                 printf("Fail memzone free\n");
194                                 return -1;
195                         }
196
197                         mz = rte_memzone_reserve(
198                                         TEST_MEMZONE_NAME("flag_zone_1G"), size,
199                                         SOCKET_ID_ANY, RTE_MEMZONE_1GB);
200                         if (mz != NULL) {
201                                 printf("MEMZONE FLAG 1GB\n");
202                                 return -1;
203                         }
204                 }
205         }
206
207         /*As with 2MB tests above for 1GB huge page requests*/
208         if (hugepage_1GB_avail) {
209                 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_1G"),
210                                 size, SOCKET_ID_ANY, RTE_MEMZONE_1GB);
211                 if (mz == NULL) {
212                         printf("MEMZONE FLAG 1GB\n");
213                         return -1;
214                 }
215                 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
216                         printf("hugepage_sz not equal 1G\n");
217                         return -1;
218                 }
219                 if (rte_memzone_free(mz)) {
220                         printf("Fail memzone free\n");
221                         return -1;
222                 }
223
224                 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_1G_HINT"),
225                                 size, SOCKET_ID_ANY,
226                                 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
227                 if (mz == NULL) {
228                         printf("MEMZONE FLAG 1GB\n");
229                         return -1;
230                 }
231                 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
232                         printf("hugepage_sz not equal 1G\n");
233                         return -1;
234                 }
235                 if (rte_memzone_free(mz)) {
236                         printf("Fail memzone free\n");
237                         return -1;
238                 }
239
240                 /* Check if 1GB huge pages are unavailable, that function fails unless
241                  * HINT flag is indicated
242                  */
243                 if (!hugepage_2MB_avail) {
244                         mz = rte_memzone_reserve(
245                                         TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
246                                         size, SOCKET_ID_ANY,
247                                         RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
248                         if (mz == NULL){
249                                 printf("MEMZONE FLAG 2MB & HINT\n");
250                                 return -1;
251                         }
252                         if (mz->hugepage_sz != RTE_PGSIZE_1G) {
253                                 printf("hugepage_sz not equal 1G\n");
254                                 return -1;
255                         }
256                         if (rte_memzone_free(mz)) {
257                                 printf("Fail memzone free\n");
258                                 return -1;
259                         }
260                         mz = rte_memzone_reserve(
261                                         TEST_MEMZONE_NAME("flag_zone_2M"), size,
262                                         SOCKET_ID_ANY, RTE_MEMZONE_2MB);
263                         if (mz != NULL) {
264                                 printf("MEMZONE FLAG 2MB\n");
265                                 return -1;
266                         }
267                 }
268
269                 if (hugepage_2MB_avail && hugepage_1GB_avail) {
270                         mz = rte_memzone_reserve(
271                                         TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
272                                         size, SOCKET_ID_ANY,
273                                         RTE_MEMZONE_2MB|RTE_MEMZONE_1GB);
274                         if (mz == NULL) {
275                                 printf("BOTH SIZES SET\n");
276                                 return -1;
277                         }
278                         if (mz->hugepage_sz != RTE_PGSIZE_1G &&
279                                         mz->hugepage_sz != RTE_PGSIZE_2M) {
280                                 printf("Wrong size when both sizes set\n");
281                                 return -1;
282                         }
283                         if (rte_memzone_free(mz)) {
284                                 printf("Fail memzone free\n");
285                                 return -1;
286                         }
287                 }
288         }
289         /*
290          * This option is for IBM Power. If 16MB pages available, check
291          * that a small memzone is correctly reserved from 16MB huge pages
292          * when requested by the RTE_MEMZONE_16MB flag. Also check that
293          * RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an available
294          * page size (i.e 16GB ) when 16MB pages are unavailable.
295          */
296         if (hugepage_16MB_avail) {
297                 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_16M"),
298                                 size, SOCKET_ID_ANY, RTE_MEMZONE_16MB);
299                 if (mz == NULL) {
300                         printf("MEMZONE FLAG 16MB\n");
301                         return -1;
302                 }
303                 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
304                         printf("hugepage_sz not equal 16M\n");
305                         return -1;
306                 }
307                 if (rte_memzone_free(mz)) {
308                         printf("Fail memzone free\n");
309                         return -1;
310                 }
311
312                 mz = rte_memzone_reserve(
313                                 TEST_MEMZONE_NAME("flag_zone_16M_HINT"), size,
314                                 SOCKET_ID_ANY,
315                                 RTE_MEMZONE_16MB|RTE_MEMZONE_SIZE_HINT_ONLY);
316                 if (mz == NULL) {
317                         printf("MEMZONE FLAG 16MB\n");
318                         return -1;
319                 }
320                 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
321                         printf("hugepage_sz not equal 16M\n");
322                         return -1;
323                 }
324                 if (rte_memzone_free(mz)) {
325                         printf("Fail memzone free\n");
326                         return -1;
327                 }
328
329                 /* Check if 1GB huge pages are unavailable, that function fails
330                  * unless HINT flag is indicated
331                  */
332                 if (!hugepage_16GB_avail) {
333                         mz = rte_memzone_reserve(
334                                         TEST_MEMZONE_NAME("flag_zone_16G_HINT"),
335                                         size, SOCKET_ID_ANY,
336                                         RTE_MEMZONE_16GB |
337                                         RTE_MEMZONE_SIZE_HINT_ONLY);
338                         if (mz == NULL) {
339                                 printf("MEMZONE FLAG 16GB & HINT\n");
340                                 return -1;
341                         }
342                         if (mz->hugepage_sz != RTE_PGSIZE_16M) {
343                                 printf("hugepage_sz not equal 16M\n");
344                                 return -1;
345                         }
346                         if (rte_memzone_free(mz)) {
347                                 printf("Fail memzone free\n");
348                                 return -1;
349                         }
350
351                         mz = rte_memzone_reserve(
352                                         TEST_MEMZONE_NAME("flag_zone_16G"),
353                                         size,
354                                         SOCKET_ID_ANY, RTE_MEMZONE_16GB);
355                         if (mz != NULL) {
356                                 printf("MEMZONE FLAG 16GB\n");
357                                 return -1;
358                         }
359                 }
360         }
361         /*As with 16MB tests above for 16GB huge page requests*/
362         if (hugepage_16GB_avail) {
363                 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_16G"),
364                                 size, SOCKET_ID_ANY, RTE_MEMZONE_16GB);
365                 if (mz == NULL) {
366                         printf("MEMZONE FLAG 16GB\n");
367                         return -1;
368                 }
369                 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
370                         printf("hugepage_sz not equal 16G\n");
371                         return -1;
372                 }
373                 if (rte_memzone_free(mz)) {
374                         printf("Fail memzone free\n");
375                         return -1;
376                 }
377
378                 mz = rte_memzone_reserve(
379                                 TEST_MEMZONE_NAME("flag_zone_16G_HINT"), size,
380                                 SOCKET_ID_ANY,
381                                 RTE_MEMZONE_16GB|RTE_MEMZONE_SIZE_HINT_ONLY);
382                 if (mz == NULL) {
383                         printf("MEMZONE FLAG 16GB\n");
384                         return -1;
385                 }
386                 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
387                         printf("hugepage_sz not equal 16G\n");
388                         return -1;
389                 }
390                 if (rte_memzone_free(mz)) {
391                         printf("Fail memzone free\n");
392                         return -1;
393                 }
394
395                 /* Check if 1GB huge pages are unavailable, that function fails
396                  * unless HINT flag is indicated
397                  */
398                 if (!hugepage_16MB_avail) {
399                         mz = rte_memzone_reserve(
400                                         TEST_MEMZONE_NAME("flag_zone_16M_HINT"),
401                                         size, SOCKET_ID_ANY,
402                                         RTE_MEMZONE_16MB |
403                                         RTE_MEMZONE_SIZE_HINT_ONLY);
404                         if (mz == NULL) {
405                                 printf("MEMZONE FLAG 16MB & HINT\n");
406                                 return -1;
407                         }
408                         if (mz->hugepage_sz != RTE_PGSIZE_16G) {
409                                 printf("hugepage_sz not equal 16G\n");
410                                 return -1;
411                         }
412                         if (rte_memzone_free(mz)) {
413                                 printf("Fail memzone free\n");
414                                 return -1;
415                         }
416                         mz = rte_memzone_reserve(
417                                         TEST_MEMZONE_NAME("flag_zone_16M"),
418                                         size, SOCKET_ID_ANY, RTE_MEMZONE_16MB);
419                         if (mz != NULL) {
420                                 printf("MEMZONE FLAG 16MB\n");
421                                 return -1;
422                         }
423                 }
424
425                 if (hugepage_16MB_avail && hugepage_16GB_avail) {
426                         mz = rte_memzone_reserve(
427                                         TEST_MEMZONE_NAME("flag_zone_16M_HINT"),
428                                         size, SOCKET_ID_ANY,
429                                         RTE_MEMZONE_16MB|RTE_MEMZONE_16GB);
430                         if (mz == NULL) {
431                                 printf("BOTH SIZES SET\n");
432                                 return -1;
433                         }
434                         if (mz->hugepage_sz != RTE_PGSIZE_16G &&
435                                         mz->hugepage_sz != RTE_PGSIZE_16M) {
436                                 printf("Wrong size when both sizes set\n");
437                                 return -1;
438                         }
439                         if (rte_memzone_free(mz)) {
440                                 printf("Fail memzone free\n");
441                                 return -1;
442                         }
443                 }
444         }
445         return 0;
446 }
447
448
449 /* Find the heap with the greatest free block size */
450 static size_t
451 find_max_block_free_size(const unsigned _align)
452 {
453         struct rte_malloc_socket_stats stats;
454         unsigned i, align = _align;
455         size_t len = 0;
456
457         for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
458                 rte_malloc_get_socket_stats(i, &stats);
459                 if (stats.greatest_free_size > len)
460                         len = stats.greatest_free_size;
461         }
462
463         if (align < RTE_CACHE_LINE_SIZE)
464                 align = RTE_CACHE_LINE_ROUNDUP(align+1);
465
466         if (len <= MALLOC_ELEM_OVERHEAD + align)
467                 return 0;
468
469         return len - MALLOC_ELEM_OVERHEAD - align;
470 }
471
472 static int
473 test_memzone_reserve_max(void)
474 {
475         const struct rte_memzone *mz;
476         size_t maxlen;
477
478         maxlen = find_max_block_free_size(0);
479
480         if (maxlen == 0) {
481                 printf("There is no space left!\n");
482                 return 0;
483         }
484
485         mz = rte_memzone_reserve(TEST_MEMZONE_NAME("max_zone"), 0,
486                         SOCKET_ID_ANY, 0);
487         if (mz == NULL){
488                 printf("Failed to reserve a big chunk of memory - %s\n",
489                                 rte_strerror(rte_errno));
490                 rte_dump_physmem_layout(stdout);
491                 rte_memzone_dump(stdout);
492                 return -1;
493         }
494
495         if (mz->len != maxlen) {
496                 printf("Memzone reserve with 0 size did not return bigest block\n");
497                 printf("Expected size = %zu, actual size = %zu\n", maxlen, mz->len);
498                 rte_dump_physmem_layout(stdout);
499                 rte_memzone_dump(stdout);
500                 return -1;
501         }
502
503         if (rte_memzone_free(mz)) {
504                 printf("Fail memzone free\n");
505                 return -1;
506         }
507
508         return 0;
509 }
510
511 static int
512 test_memzone_reserve_max_aligned(void)
513 {
514         const struct rte_memzone *mz;
515         size_t maxlen = 0;
516
517         /* random alignment */
518         rte_srand((unsigned)rte_rdtsc());
519         const unsigned align = 1 << ((rte_rand() % 8) + 5); /* from 128 up to 4k alignment */
520
521         maxlen = find_max_block_free_size(align);
522
523         if (maxlen == 0) {
524                 printf("There is no space left for biggest %u-aligned memzone!\n", align);
525                 return 0;
526         }
527
528         mz = rte_memzone_reserve_aligned(TEST_MEMZONE_NAME("max_zone_aligned"),
529                         0, SOCKET_ID_ANY, 0, align);
530         if (mz == NULL){
531                 printf("Failed to reserve a big chunk of memory - %s\n",
532                                 rte_strerror(rte_errno));
533                 rte_dump_physmem_layout(stdout);
534                 rte_memzone_dump(stdout);
535                 return -1;
536         }
537
538         if (mz->len != maxlen) {
539                 printf("Memzone reserve with 0 size and alignment %u did not return"
540                                 " bigest block\n", align);
541                 printf("Expected size = %zu, actual size = %zu\n",
542                                 maxlen, mz->len);
543                 rte_dump_physmem_layout(stdout);
544                 rte_memzone_dump(stdout);
545                 return -1;
546         }
547
548         if (rte_memzone_free(mz)) {
549                 printf("Fail memzone free\n");
550                 return -1;
551         }
552
553         return 0;
554 }
555
556 static int
557 test_memzone_aligned(void)
558 {
559         const struct rte_memzone *memzone_aligned_32;
560         const struct rte_memzone *memzone_aligned_128;
561         const struct rte_memzone *memzone_aligned_256;
562         const struct rte_memzone *memzone_aligned_512;
563         const struct rte_memzone *memzone_aligned_1024;
564
565         /* memzone that should automatically be adjusted to align on 64 bytes */
566         memzone_aligned_32 = rte_memzone_reserve_aligned(
567                         TEST_MEMZONE_NAME("aligned_32"), 100, SOCKET_ID_ANY, 0,
568                         32);
569
570         /* memzone that is supposed to be aligned on a 128 byte boundary */
571         memzone_aligned_128 = rte_memzone_reserve_aligned(
572                         TEST_MEMZONE_NAME("aligned_128"), 100, SOCKET_ID_ANY, 0,
573                         128);
574
575         /* memzone that is supposed to be aligned on a 256 byte boundary */
576         memzone_aligned_256 = rte_memzone_reserve_aligned(
577                         TEST_MEMZONE_NAME("aligned_256"), 100, SOCKET_ID_ANY, 0,
578                         256);
579
580         /* memzone that is supposed to be aligned on a 512 byte boundary */
581         memzone_aligned_512 = rte_memzone_reserve_aligned(
582                         TEST_MEMZONE_NAME("aligned_512"), 100, SOCKET_ID_ANY, 0,
583                         512);
584
585         /* memzone that is supposed to be aligned on a 1024 byte boundary */
586         memzone_aligned_1024 = rte_memzone_reserve_aligned(
587                         TEST_MEMZONE_NAME("aligned_1024"), 100, SOCKET_ID_ANY,
588                         0, 1024);
589
590         printf("check alignments and lengths\n");
591         if (memzone_aligned_32 == NULL) {
592                 printf("Unable to reserve 64-byte aligned memzone!\n");
593                 return -1;
594         }
595         if ((memzone_aligned_32->iova & RTE_CACHE_LINE_MASK) != 0)
596                 return -1;
597         if (((uintptr_t) memzone_aligned_32->addr & RTE_CACHE_LINE_MASK) != 0)
598                 return -1;
599         if ((memzone_aligned_32->len & RTE_CACHE_LINE_MASK) != 0)
600                 return -1;
601
602         if (memzone_aligned_128 == NULL) {
603                 printf("Unable to reserve 128-byte aligned memzone!\n");
604                 return -1;
605         }
606         if ((memzone_aligned_128->iova & 127) != 0)
607                 return -1;
608         if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
609                 return -1;
610         if ((memzone_aligned_128->len & RTE_CACHE_LINE_MASK) != 0)
611                 return -1;
612
613         if (memzone_aligned_256 == NULL) {
614                 printf("Unable to reserve 256-byte aligned memzone!\n");
615                 return -1;
616         }
617         if ((memzone_aligned_256->iova & 255) != 0)
618                 return -1;
619         if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
620                 return -1;
621         if ((memzone_aligned_256->len & RTE_CACHE_LINE_MASK) != 0)
622                 return -1;
623
624         if (memzone_aligned_512 == NULL) {
625                 printf("Unable to reserve 512-byte aligned memzone!\n");
626                 return -1;
627         }
628         if ((memzone_aligned_512->iova & 511) != 0)
629                 return -1;
630         if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
631                 return -1;
632         if ((memzone_aligned_512->len & RTE_CACHE_LINE_MASK) != 0)
633                 return -1;
634
635         if (memzone_aligned_1024 == NULL) {
636                 printf("Unable to reserve 1024-byte aligned memzone!\n");
637                 return -1;
638         }
639         if ((memzone_aligned_1024->iova & 1023) != 0)
640                 return -1;
641         if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
642                 return -1;
643         if ((memzone_aligned_1024->len & RTE_CACHE_LINE_MASK) != 0)
644                 return -1;
645
646         /* check that zones don't overlap */
647         printf("check overlapping\n");
648         if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
649                                         memzone_aligned_128->iova, memzone_aligned_128->len))
650                 return -1;
651         if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
652                                         memzone_aligned_256->iova, memzone_aligned_256->len))
653                 return -1;
654         if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
655                                         memzone_aligned_512->iova, memzone_aligned_512->len))
656                 return -1;
657         if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
658                                         memzone_aligned_1024->iova, memzone_aligned_1024->len))
659                 return -1;
660         if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
661                                         memzone_aligned_256->iova, memzone_aligned_256->len))
662                 return -1;
663         if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
664                                         memzone_aligned_512->iova, memzone_aligned_512->len))
665                 return -1;
666         if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
667                                         memzone_aligned_1024->iova, memzone_aligned_1024->len))
668                 return -1;
669         if (is_memory_overlap(memzone_aligned_256->iova, memzone_aligned_256->len,
670                                         memzone_aligned_512->iova, memzone_aligned_512->len))
671                 return -1;
672         if (is_memory_overlap(memzone_aligned_256->iova, memzone_aligned_256->len,
673                                         memzone_aligned_1024->iova, memzone_aligned_1024->len))
674                 return -1;
675         if (is_memory_overlap(memzone_aligned_512->iova, memzone_aligned_512->len,
676                                         memzone_aligned_1024->iova, memzone_aligned_1024->len))
677                 return -1;
678
679         /* free all used zones */
680         if (rte_memzone_free(memzone_aligned_32)) {
681                 printf("Fail memzone free\n");
682                 return -1;
683         }
684         if (rte_memzone_free(memzone_aligned_128)) {
685                 printf("Fail memzone free\n");
686                 return -1;
687         }
688         if (rte_memzone_free(memzone_aligned_256)) {
689                 printf("Fail memzone free\n");
690                 return -1;
691         }
692         if (rte_memzone_free(memzone_aligned_512)) {
693                 printf("Fail memzone free\n");
694                 return -1;
695         }
696         if (rte_memzone_free(memzone_aligned_1024)) {
697                 printf("Fail memzone free\n");
698                 return -1;
699         }
700         return 0;
701 }
702
703 static int
704 check_memzone_bounded(const char *name, uint32_t len,  uint32_t align,
705         uint32_t bound)
706 {
707         const struct rte_memzone *mz;
708         rte_iova_t bmask;
709
710         bmask = ~((rte_iova_t)bound - 1);
711
712         if ((mz = rte_memzone_reserve_bounded(name, len, SOCKET_ID_ANY, 0,
713                         align, bound)) == NULL) {
714                 printf("%s(%s): memzone creation failed\n",
715                         __func__, name);
716                 return -1;
717         }
718
719         if ((mz->iova & ((rte_iova_t)align - 1)) != 0) {
720                 printf("%s(%s): invalid phys addr alignment\n",
721                         __func__, mz->name);
722                 return -1;
723         }
724
725         if (((uintptr_t) mz->addr & ((uintptr_t)align - 1)) != 0) {
726                 printf("%s(%s): invalid virtual addr alignment\n",
727                         __func__, mz->name);
728                 return -1;
729         }
730
731         if ((mz->len & RTE_CACHE_LINE_MASK) != 0 || mz->len < len ||
732                         mz->len < RTE_CACHE_LINE_SIZE) {
733                 printf("%s(%s): invalid length\n",
734                         __func__, mz->name);
735                 return -1;
736         }
737
738         if ((mz->iova & bmask) !=
739                         ((mz->iova + mz->len - 1) & bmask)) {
740                 printf("%s(%s): invalid memzone boundary %u crossed\n",
741                         __func__, mz->name, bound);
742                 return -1;
743         }
744
745         if (rte_memzone_free(mz)) {
746                 printf("Fail memzone free\n");
747                 return -1;
748         }
749
750         return 0;
751 }
752
753 static int
754 test_memzone_bounded(void)
755 {
756         const struct rte_memzone *memzone_err;
757         int rc;
758
759         /* should fail as boundary is not power of two */
760         memzone_err = rte_memzone_reserve_bounded(
761                         TEST_MEMZONE_NAME("bounded_error_31"), 100,
762                         SOCKET_ID_ANY, 0, 32, UINT32_MAX);
763         if (memzone_err != NULL) {
764                 printf("%s(%s)created a memzone with invalid boundary "
765                         "conditions\n", __func__, memzone_err->name);
766                 return -1;
767         }
768
769         /* should fail as len is greater then boundary */
770         memzone_err = rte_memzone_reserve_bounded(
771                         TEST_MEMZONE_NAME("bounded_error_32"), 100,
772                         SOCKET_ID_ANY, 0, 32, 32);
773         if (memzone_err != NULL) {
774                 printf("%s(%s)created a memzone with invalid boundary "
775                         "conditions\n", __func__, memzone_err->name);
776                 return -1;
777         }
778
779         rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_128"), 100, 128,
780                         128);
781         if (rc != 0)
782                 return rc;
783
784         rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_256"), 100, 256,
785                         128);
786         if (rc != 0)
787                 return rc;
788
789         rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_1K"), 100, 64,
790                         1024);
791         if (rc != 0)
792                 return rc;
793
794         rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_1K_MAX"), 0, 64,
795                         1024);
796         if (rc != 0)
797                 return rc;
798
799         return 0;
800 }
801
802 static int
803 test_memzone_free(void)
804 {
805         const struct rte_memzone *mz[RTE_MAX_MEMZONE + 1];
806         int i;
807         char name[20];
808
809         mz[0] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone0"), 2000,
810                         SOCKET_ID_ANY, 0);
811         mz[1] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone1"), 4000,
812                         SOCKET_ID_ANY, 0);
813
814         if (mz[0] > mz[1])
815                 return -1;
816         if (!rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone0")))
817                 return -1;
818         if (!rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone1")))
819                 return -1;
820
821         if (rte_memzone_free(mz[0])) {
822                 printf("Fail memzone free - tempzone0\n");
823                 return -1;
824         }
825         if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone0"))) {
826                 printf("Found previously free memzone - tempzone0\n");
827                 return -1;
828         }
829         mz[2] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone2"), 2000,
830                         SOCKET_ID_ANY, 0);
831
832         if (mz[2] > mz[1]) {
833                 printf("tempzone2 should have gotten the free entry from tempzone0\n");
834                 return -1;
835         }
836         if (rte_memzone_free(mz[2])) {
837                 printf("Fail memzone free - tempzone2\n");
838                 return -1;
839         }
840         if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone2"))) {
841                 printf("Found previously free memzone - tempzone2\n");
842                 return -1;
843         }
844         if (rte_memzone_free(mz[1])) {
845                 printf("Fail memzone free - tempzone1\n");
846                 return -1;
847         }
848         if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone1"))) {
849                 printf("Found previously free memzone - tempzone1\n");
850                 return -1;
851         }
852
853         i = 0;
854         do {
855                 snprintf(name, sizeof(name), TEST_MEMZONE_NAME("tempzone%u"),
856                                 i);
857                 mz[i] = rte_memzone_reserve(name, 1, SOCKET_ID_ANY, 0);
858         } while (mz[i++] != NULL);
859
860         if (rte_memzone_free(mz[0])) {
861                 printf("Fail memzone free - tempzone0\n");
862                 return -1;
863         }
864         mz[0] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone0new"), 0,
865                         SOCKET_ID_ANY, 0);
866
867         if (mz[0] == NULL) {
868                 printf("Fail to create memzone - tempzone0new - when MAX memzones were "
869                                 "created and one was free\n");
870                 return -1;
871         }
872
873         for (i = i - 2; i >= 0; i--) {
874                 if (rte_memzone_free(mz[i])) {
875                         printf("Fail memzone free - tempzone%d\n", i);
876                         return -1;
877                 }
878         }
879
880         return 0;
881 }
882
883 static int
884 test_memzone_basic(void)
885 {
886         const struct rte_memzone *memzone1;
887         const struct rte_memzone *memzone2;
888         const struct rte_memzone *memzone3;
889         const struct rte_memzone *memzone4;
890         const struct rte_memzone *mz;
891         int memzone_cnt_after, memzone_cnt_expected;
892         int memzone_cnt_before =
893                         rte_eal_get_configuration()->mem_config->memzone_cnt;
894
895         memzone1 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone1"), 100,
896                                 SOCKET_ID_ANY, 0);
897
898         memzone2 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone2"), 1000,
899                                 0, 0);
900
901         memzone3 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone3"), 1000,
902                                 1, 0);
903
904         memzone4 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone4"), 1024,
905                                 SOCKET_ID_ANY, 0);
906
907         /* memzone3 may be NULL if we don't have NUMA */
908         if (memzone1 == NULL || memzone2 == NULL || memzone4 == NULL)
909                 return -1;
910
911         /* check how many memzones we are expecting */
912         memzone_cnt_expected = memzone_cnt_before +
913                         (memzone1 != NULL) + (memzone2 != NULL) +
914                         (memzone3 != NULL) + (memzone4 != NULL);
915
916         memzone_cnt_after =
917                         rte_eal_get_configuration()->mem_config->memzone_cnt;
918
919         if (memzone_cnt_after != memzone_cnt_expected)
920                 return -1;
921
922
923         rte_memzone_dump(stdout);
924
925         /* check cache-line alignments */
926         printf("check alignments and lengths\n");
927
928         if ((memzone1->iova & RTE_CACHE_LINE_MASK) != 0)
929                 return -1;
930         if ((memzone2->iova & RTE_CACHE_LINE_MASK) != 0)
931                 return -1;
932         if (memzone3 != NULL && (memzone3->iova & RTE_CACHE_LINE_MASK) != 0)
933                 return -1;
934         if ((memzone1->len & RTE_CACHE_LINE_MASK) != 0 || memzone1->len == 0)
935                 return -1;
936         if ((memzone2->len & RTE_CACHE_LINE_MASK) != 0 || memzone2->len == 0)
937                 return -1;
938         if (memzone3 != NULL && ((memzone3->len & RTE_CACHE_LINE_MASK) != 0 ||
939                         memzone3->len == 0))
940                 return -1;
941         if (memzone4->len != 1024)
942                 return -1;
943
944         /* check that zones don't overlap */
945         printf("check overlapping\n");
946
947         if (is_memory_overlap(memzone1->iova, memzone1->len,
948                         memzone2->iova, memzone2->len))
949                 return -1;
950         if (memzone3 != NULL &&
951                         is_memory_overlap(memzone1->iova, memzone1->len,
952                                         memzone3->iova, memzone3->len))
953                 return -1;
954         if (memzone3 != NULL &&
955                         is_memory_overlap(memzone2->iova, memzone2->len,
956                                         memzone3->iova, memzone3->len))
957                 return -1;
958
959         printf("check socket ID\n");
960
961         /* memzone2 must be on socket id 0 and memzone3 on socket 1 */
962         if (memzone2->socket_id != 0)
963                 return -1;
964         if (memzone3 != NULL && memzone3->socket_id != 1)
965                 return -1;
966
967         printf("test zone lookup\n");
968         mz = rte_memzone_lookup(TEST_MEMZONE_NAME("testzone1"));
969         if (mz != memzone1)
970                 return -1;
971
972         printf("test duplcate zone name\n");
973         mz = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone1"), 100,
974                         SOCKET_ID_ANY, 0);
975         if (mz != NULL)
976                 return -1;
977
978         if (rte_memzone_free(memzone1)) {
979                 printf("Fail memzone free - memzone1\n");
980                 return -1;
981         }
982         if (rte_memzone_free(memzone2)) {
983                 printf("Fail memzone free - memzone2\n");
984                 return -1;
985         }
986         if (memzone3 && rte_memzone_free(memzone3)) {
987                 printf("Fail memzone free - memzone3\n");
988                 return -1;
989         }
990         if (rte_memzone_free(memzone4)) {
991                 printf("Fail memzone free - memzone4\n");
992                 return -1;
993         }
994
995         memzone_cnt_after =
996                         rte_eal_get_configuration()->mem_config->memzone_cnt;
997         if (memzone_cnt_after != memzone_cnt_before)
998                 return -1;
999
1000         return 0;
1001 }
1002
1003 static int test_memzones_left;
1004 static int memzone_walk_cnt;
1005 static void memzone_walk_clb(const struct rte_memzone *mz,
1006                              void *arg __rte_unused)
1007 {
1008         memzone_walk_cnt++;
1009         if (!strncmp(TEST_MEMZONE_NAME(""), mz->name, RTE_MEMZONE_NAMESIZE))
1010                 test_memzones_left++;
1011 }
1012
1013 static int
1014 test_memzone(void)
1015 {
1016         /* take note of how many memzones were allocated before running */
1017         int memzone_cnt = rte_eal_get_configuration()->mem_config->memzone_cnt;
1018
1019         printf("test basic memzone API\n");
1020         if (test_memzone_basic() < 0)
1021                 return -1;
1022
1023         printf("test free memzone\n");
1024         if (test_memzone_free() < 0)
1025                 return -1;
1026
1027         printf("test reserving memzone with bigger size than the maximum\n");
1028         if (test_memzone_reserving_zone_size_bigger_than_the_maximum() < 0)
1029                 return -1;
1030
1031         printf("test memzone_reserve flags\n");
1032         if (test_memzone_reserve_flags() < 0)
1033                 return -1;
1034
1035         printf("test alignment for memzone_reserve\n");
1036         if (test_memzone_aligned() < 0)
1037                 return -1;
1038
1039         printf("test boundary alignment for memzone_reserve\n");
1040         if (test_memzone_bounded() < 0)
1041                 return -1;
1042
1043         printf("test invalid alignment for memzone_reserve\n");
1044         if (test_memzone_invalid_alignment() < 0)
1045                 return -1;
1046
1047         printf("test reserving the largest size memzone possible\n");
1048         if (test_memzone_reserve_max() < 0)
1049                 return -1;
1050
1051         printf("test reserving the largest size aligned memzone possible\n");
1052         if (test_memzone_reserve_max_aligned() < 0)
1053                 return -1;
1054
1055         printf("check memzone cleanup\n");
1056         memzone_walk_cnt = 0;
1057         test_memzones_left = 0;
1058         rte_memzone_walk(memzone_walk_clb, NULL);
1059         if (memzone_walk_cnt != memzone_cnt || test_memzones_left > 0) {
1060                 printf("there are some memzones left after test\n");
1061                 rte_memzone_dump(stdout);
1062                 return -1;
1063         }
1064
1065         return 0;
1066 }
1067
1068 REGISTER_TEST_COMMAND(memzone_autotest, test_memzone);