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