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