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