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