app: various tests update
[dpdk.git] / app / test / test_memzone.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/queue.h>
39
40 #include <cmdline_parse.h>
41
42 #include <rte_random.h>
43 #include <rte_cycles.h>
44 #include <rte_memory.h>
45 #include <rte_memzone.h>
46 #include <rte_tailq.h>
47 #include <rte_eal.h>
48 #include <rte_eal_memconfig.h>
49 #include <rte_common.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", 0x1900000000ULL,
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 int 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         uint64_t len = 0;
272         void* last_addr;
273         uint64_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 &= ~((uint64_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,
302                                         (size_t)ms[memseg_idx].len)))) {
303                                 /* since the zones can now be aligned and occasionally skip
304                                  * some space, we should calculate the length based on
305                                  * reported length and start addresses difference. Addresses
306                                  * are allocated sequentially so we don't need to worry about
307                                  * them being in the right order.
308                                  */
309                                 len -= RTE_PTR_DIFF(
310                                                 config->mem_config->memzone[memzone_idx].addr,
311                                                 last_addr);
312                                 len -= config->mem_config->memzone[memzone_idx].len;
313                                 last_addr = RTE_PTR_ADD(config->mem_config->memzone[memzone_idx].addr,
314                                                 (size_t) config->mem_config->memzone[memzone_idx].len);
315                         }
316                 }
317
318                 /* we don't need to calculate offset here since length
319                  * is always cache-aligned */
320                 if (len > maxlen)
321                         maxlen = len;
322         }
323
324         if (maxlen == 0) {
325                 printf("There is no space left!\n");
326                 return 0;
327         }
328
329         mz = rte_memzone_reserve("max_zone", 0, SOCKET_ID_ANY, 0);
330         if (mz == NULL){
331                 printf("Failed to reserve a big chunk of memory\n");
332                 rte_dump_physmem_layout();
333                 rte_memzone_dump();
334                 return -1;
335         }
336
337         if (mz->len != maxlen) {
338                 printf("Memzone reserve with 0 size did not return bigest block\n");
339                 printf("Expected size = %" PRIu64 ", actual size = %" PRIu64 "\n",
340                                 maxlen, mz->len);
341                 rte_dump_physmem_layout();
342                 rte_memzone_dump();
343
344                 return -1;
345         }
346         return 0;
347 }
348
349 static int
350 test_memzone_reserve_max_aligned(void)
351 {
352         const struct rte_memzone *mz;
353         const struct rte_config *config;
354         const struct rte_memseg *ms;
355         int memseg_idx = 0;
356         int memzone_idx = 0;
357         uint64_t addr_offset, len = 0;
358         void* last_addr;
359         uint64_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 &= ~((uint64_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,
395                                         (size_t) ms[memseg_idx].len)))) {
396                                 /* since the zones can now be aligned and occasionally skip
397                                  * some space, we should calculate the length based on
398                                  * reported length and start addresses difference.
399                                  */
400                                 len -= (uintptr_t) RTE_PTR_SUB(
401                                                 config->mem_config->memzone[memzone_idx].addr,
402                                                 (uintptr_t) last_addr);
403                                 len -= config->mem_config->memzone[memzone_idx].len;
404                                 last_addr =
405                                                 RTE_PTR_ADD(config->mem_config->memzone[memzone_idx].addr,
406                                                 (size_t) config->mem_config->memzone[memzone_idx].len);
407                         }
408                 }
409
410                 /* make sure we get the alignment offset */
411                 if (len > maxlen) {
412                         addr_offset = RTE_PTR_ALIGN_CEIL((uintptr_t) last_addr, align) - (uintptr_t) last_addr;
413                         maxlen = len;
414                 }
415         }
416
417         if (maxlen == 0 || maxlen == addr_offset) {
418                 printf("There is no space left for biggest %u-aligned memzone!\n", align);
419                 return 0;
420         }
421
422         maxlen -= addr_offset;
423
424         mz = rte_memzone_reserve_aligned("max_zone_aligned", 0,
425                         SOCKET_ID_ANY, 0, align);
426         if (mz == NULL){
427                 printf("Failed to reserve a big chunk of memory\n");
428                 rte_dump_physmem_layout();
429                 rte_memzone_dump();
430                 return -1;
431         }
432
433         if (mz->len != maxlen) {
434                 printf("Memzone reserve with 0 size and alignment %u did not return"
435                                 " bigest block\n", align);
436                 printf("Expected size = %" PRIu64 ", actual size = %" PRIu64 "\n",
437                                 maxlen, mz->len);
438                 rte_dump_physmem_layout();
439                 rte_memzone_dump();
440
441                 return -1;
442         }
443         return 0;
444 }
445
446 static int
447 test_memzone_aligned(void)
448 {
449         const struct rte_memzone *memzone_aligned_32;
450         const struct rte_memzone *memzone_aligned_128;
451         const struct rte_memzone *memzone_aligned_256;
452         const struct rte_memzone *memzone_aligned_512;
453         const struct rte_memzone *memzone_aligned_1024;
454
455         /* memzone that should automatically be adjusted to align on 64 bytes */
456         memzone_aligned_32 = rte_memzone_lookup("aligned_32");
457         if (memzone_aligned_32 == NULL)
458                 memzone_aligned_32 = rte_memzone_reserve_aligned("aligned_32", 100,
459                                 SOCKET_ID_ANY, 0, 32);
460
461         /* memzone that is supposed to be aligned on a 128 byte boundary */
462         memzone_aligned_128 = rte_memzone_lookup("aligned_128");
463         if (memzone_aligned_128 == NULL)
464                 memzone_aligned_128 = rte_memzone_reserve_aligned("aligned_128", 100,
465                                 SOCKET_ID_ANY, 0, 128);
466
467         /* memzone that is supposed to be aligned on a 256 byte boundary */
468         memzone_aligned_256 = rte_memzone_lookup("aligned_256");
469         if (memzone_aligned_256 == NULL)
470                 memzone_aligned_256 = rte_memzone_reserve_aligned("aligned_256", 100,
471                                 SOCKET_ID_ANY, 0, 256);
472
473         /* memzone that is supposed to be aligned on a 512 byte boundary */
474         memzone_aligned_512 = rte_memzone_lookup("aligned_512");
475         if (memzone_aligned_512 == NULL)
476                 memzone_aligned_512 = rte_memzone_reserve_aligned("aligned_512", 100,
477                                 SOCKET_ID_ANY, 0, 512);
478
479         /* memzone that is supposed to be aligned on a 1024 byte boundary */
480         memzone_aligned_1024 = rte_memzone_lookup("aligned_1024");
481         if (memzone_aligned_1024 == NULL)
482                 memzone_aligned_1024 = rte_memzone_reserve_aligned("aligned_1024", 100,
483                                 SOCKET_ID_ANY, 0, 1024);
484
485         printf("check alignments and lengths\n");
486         if (memzone_aligned_32 == NULL) {
487                 printf("Unable to reserve 64-byte aligned memzone!\n");
488                 return -1;
489         }
490         if ((memzone_aligned_32->phys_addr & CACHE_LINE_MASK) != 0)
491                 return -1;
492         if (((uintptr_t) memzone_aligned_32->addr & CACHE_LINE_MASK) != 0)
493                 return -1;
494         if ((memzone_aligned_32->len & CACHE_LINE_MASK) != 0)
495                 return -1;
496         if (memzone_aligned_128 == NULL) {
497                 printf("Unable to reserve 128-byte aligned memzone!\n");
498                 return -1;
499         }
500         if ((memzone_aligned_128->phys_addr & 127) != 0)
501                 return -1;
502         if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
503                 return -1;
504         if ((memzone_aligned_128->len & CACHE_LINE_MASK) != 0)
505                 return -1;
506         if (memzone_aligned_256 == NULL) {
507                 printf("Unable to reserve 256-byte aligned memzone!\n");
508                 return -1;
509         }
510         if ((memzone_aligned_256->phys_addr & 255) != 0)
511                 return -1;
512         if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
513                 return -1;
514         if ((memzone_aligned_256->len & CACHE_LINE_MASK) != 0)
515                 return -1;
516         if (memzone_aligned_512 == NULL) {
517                 printf("Unable to reserve 512-byte aligned memzone!\n");
518                 return -1;
519         }
520         if ((memzone_aligned_512->phys_addr & 511) != 0)
521                 return -1;
522         if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
523                 return -1;
524         if ((memzone_aligned_512->len & CACHE_LINE_MASK) != 0)
525                 return -1;
526         if (memzone_aligned_1024 == NULL) {
527                 printf("Unable to reserve 1024-byte aligned memzone!\n");
528                 return -1;
529         }
530         if ((memzone_aligned_1024->phys_addr & 1023) != 0)
531                 return -1;
532         if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
533                 return -1;
534         if ((memzone_aligned_1024->len & CACHE_LINE_MASK) != 0)
535                 return -1;
536
537         /* check that zones don't overlap */
538         printf("check overlapping\n");
539         if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
540                                         memzone_aligned_128->phys_addr, memzone_aligned_128->len))
541                 return -1;
542         if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
543                                         memzone_aligned_256->phys_addr, memzone_aligned_256->len))
544                 return -1;
545         if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
546                                         memzone_aligned_512->phys_addr, memzone_aligned_512->len))
547                 return -1;
548         if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
549                                         memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
550                 return -1;
551         if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
552                                         memzone_aligned_256->phys_addr, memzone_aligned_256->len))
553                 return -1;
554         if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
555                                         memzone_aligned_512->phys_addr, memzone_aligned_512->len))
556                 return -1;
557         if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
558                                         memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
559                 return -1;
560         if (is_memory_overlap(memzone_aligned_256->phys_addr, memzone_aligned_256->len,
561                                         memzone_aligned_512->phys_addr, memzone_aligned_512->len))
562                 return -1;
563         if (is_memory_overlap(memzone_aligned_256->phys_addr, memzone_aligned_256->len,
564                                         memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
565                 return -1;
566         if (is_memory_overlap(memzone_aligned_512->phys_addr, memzone_aligned_512->len,
567                                         memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
568                 return -1;
569         return 0;
570 }
571
572 int
573 test_memzone(void)
574 {
575         const struct rte_memzone *memzone1;
576         const struct rte_memzone *memzone2;
577         const struct rte_memzone *memzone3;
578         const struct rte_memzone *mz;
579
580         memzone1 = rte_memzone_lookup("testzone1");
581         if (memzone1 == NULL)
582                 memzone1 = rte_memzone_reserve("testzone1", 100,
583                                 SOCKET_ID_ANY, 0);
584
585         memzone2 = rte_memzone_lookup("testzone2");
586         if (memzone2 == NULL)
587                 memzone2 = rte_memzone_reserve("testzone2", 1000,
588                                 0, 0);
589
590         memzone3 = rte_memzone_lookup("testzone3");
591         if (memzone3 == NULL)
592                 memzone3 = rte_memzone_reserve("testzone3", 1000,
593                                 1, 0);
594
595         /* memzone3 may be NULL if we don't have NUMA */
596         if (memzone1 == NULL || memzone2 == NULL)
597                 return -1;
598
599         rte_memzone_dump();
600
601         /* check cache-line alignments */
602         printf("check alignments and lengths\n");
603
604         if ((memzone1->phys_addr & CACHE_LINE_MASK) != 0)
605                 return -1;
606         if ((memzone2->phys_addr & CACHE_LINE_MASK) != 0)
607                 return -1;
608         if (memzone3 != NULL && (memzone3->phys_addr & CACHE_LINE_MASK) != 0)
609                 return -1;
610         if ((memzone1->len & CACHE_LINE_MASK) != 0 || memzone1->len == 0)
611                 return -1;
612         if ((memzone2->len & CACHE_LINE_MASK) != 0 || memzone2->len == 0)
613                 return -1;
614         if (memzone3 != NULL && ((memzone3->len & CACHE_LINE_MASK) != 0 ||
615                         memzone3->len == 0))
616                 return -1;
617
618         /* check that zones don't overlap */
619         printf("check overlapping\n");
620
621         if (is_memory_overlap(memzone1->phys_addr, memzone1->len,
622                         memzone2->phys_addr, memzone2->len))
623                 return -1;
624         if (memzone3 != NULL &&
625                         is_memory_overlap(memzone1->phys_addr, memzone1->len,
626                                         memzone3->phys_addr, memzone3->len))
627                 return -1;
628         if (memzone3 != NULL &&
629                         is_memory_overlap(memzone2->phys_addr, memzone2->len,
630                                         memzone3->phys_addr, memzone3->len))
631                 return -1;
632
633         printf("check socket ID\n");
634
635         /* memzone2 must be on socket id 0 and memzone3 on socket 1 */
636         if (memzone2->socket_id != 0)
637                 return -1;
638         if (memzone3 != NULL && memzone3->socket_id != 1)
639                 return -1;
640
641         printf("test zone lookup\n");
642         mz = rte_memzone_lookup("testzone1");
643         if (mz != memzone1)
644                 return -1;
645
646         printf("test duplcate zone name\n");
647         mz = rte_memzone_reserve("testzone1", 100,
648                         SOCKET_ID_ANY, 0);
649         if (mz != NULL)
650                 return -1;
651
652         printf("test reserving memzone with bigger size than the maximum\n");
653         if (test_memzone_reserving_zone_size_bigger_than_the_maximum() < 0)
654                 return -1;
655
656         printf("test memzone_reserve flags\n");
657         if (test_memzone_reserve_flags() < 0)
658                 return -1;
659
660         printf("test alignment for memzone_reserve\n");
661         if (test_memzone_aligned() < 0)
662                 return -1;
663
664         printf("test invalid alignment for memzone_reserve\n");
665         if (test_memzone_invalid_alignment() < 0)
666                 return -1;
667
668         printf("test reserving the largest size memzone possible\n");
669         if (test_memzone_reserve_max() < 0)
670                 return -1;
671
672         printf("test reserving the largest size aligned memzone possible\n");
673         if (test_memzone_reserve_max_aligned() < 0)
674                 return -1;
675
676         return 0;
677 }