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