+static int
+check_memzone_bounded(const char *name, uint32_t len, uint32_t align,
+ uint32_t bound)
+{
+ const struct rte_memzone *mz;
+ rte_iova_t bmask;
+
+ bmask = ~((rte_iova_t)bound - 1);
+
+ if ((mz = rte_memzone_reserve_bounded(name, len, SOCKET_ID_ANY, 0,
+ align, bound)) == NULL) {
+ printf("%s(%s): memzone creation failed\n",
+ __func__, name);
+ return -1;
+ }
+
+ if ((mz->iova & ((rte_iova_t)align - 1)) != 0) {
+ printf("%s(%s): invalid phys addr alignment\n",
+ __func__, mz->name);
+ return -1;
+ }
+
+ if (((uintptr_t) mz->addr & ((uintptr_t)align - 1)) != 0) {
+ printf("%s(%s): invalid virtual addr alignment\n",
+ __func__, mz->name);
+ return -1;
+ }
+
+ if ((mz->len & RTE_CACHE_LINE_MASK) != 0 || mz->len < len ||
+ mz->len < RTE_CACHE_LINE_SIZE) {
+ printf("%s(%s): invalid length\n",
+ __func__, mz->name);
+ return -1;
+ }
+
+ if ((mz->iova & bmask) !=
+ ((mz->iova + mz->len - 1) & bmask)) {
+ printf("%s(%s): invalid memzone boundary %u crossed\n",
+ __func__, mz->name, bound);
+ return -1;
+ }
+
+ if (rte_memzone_free(mz)) {
+ printf("Fail memzone free\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
+test_memzone_bounded(void)
+{
+ const struct rte_memzone *memzone_err;
+ int rc;
+
+ /* should fail as boundary is not power of two */
+ memzone_err = rte_memzone_reserve_bounded(
+ TEST_MEMZONE_NAME("bounded_error_31"), 100,
+ SOCKET_ID_ANY, 0, 32, UINT32_MAX);
+ if (memzone_err != NULL) {
+ printf("%s(%s)created a memzone with invalid boundary "
+ "conditions\n", __func__, memzone_err->name);
+ return -1;
+ }
+
+ /* should fail as len is greater then boundary */
+ memzone_err = rte_memzone_reserve_bounded(
+ TEST_MEMZONE_NAME("bounded_error_32"), 100,
+ SOCKET_ID_ANY, 0, 32, 32);
+ if (memzone_err != NULL) {
+ printf("%s(%s)created a memzone with invalid boundary "
+ "conditions\n", __func__, memzone_err->name);
+ return -1;
+ }
+
+ rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_128"), 100, 128,
+ 128);
+ if (rc != 0)
+ return rc;
+
+ rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_256"), 100, 256,
+ 128);
+ if (rc != 0)
+ return rc;
+
+ rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_1K"), 100, 64,
+ 1024);
+ if (rc != 0)
+ return rc;
+
+ rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_1K_MAX"), 0, 64,
+ 1024);
+ if (rc != 0)
+ return rc;
+
+ return 0;
+}
+
+static int
+test_memzone_free(void)
+{
+ const struct rte_memzone *mz[RTE_MAX_MEMZONE + 1];
+ int i;
+ char name[20];
+
+ mz[0] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone0"), 2000,
+ SOCKET_ID_ANY, 0);
+ mz[1] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone1"), 4000,
+ SOCKET_ID_ANY, 0);
+
+ if (mz[0] > mz[1])
+ return -1;
+ if (!rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone0")))
+ return -1;
+ if (!rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone1")))
+ return -1;
+
+ if (rte_memzone_free(mz[0])) {
+ printf("Fail memzone free - tempzone0\n");
+ return -1;
+ }
+ if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone0"))) {
+ printf("Found previously free memzone - tempzone0\n");
+ return -1;
+ }
+ mz[2] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone2"), 2000,
+ SOCKET_ID_ANY, 0);
+
+ if (mz[2] > mz[1]) {
+ printf("tempzone2 should have gotten the free entry from tempzone0\n");
+ return -1;
+ }
+ if (rte_memzone_free(mz[2])) {
+ printf("Fail memzone free - tempzone2\n");
+ return -1;
+ }
+ if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone2"))) {
+ printf("Found previously free memzone - tempzone2\n");
+ return -1;
+ }
+ if (rte_memzone_free(mz[1])) {
+ printf("Fail memzone free - tempzone1\n");
+ return -1;
+ }
+ if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone1"))) {
+ printf("Found previously free memzone - tempzone1\n");
+ return -1;
+ }
+
+ i = 0;
+ do {
+ snprintf(name, sizeof(name), TEST_MEMZONE_NAME("tempzone%u"),
+ i);
+ mz[i] = rte_memzone_reserve(name, 1, SOCKET_ID_ANY, 0);
+ } while (mz[i++] != NULL);
+
+ if (rte_memzone_free(mz[0])) {
+ printf("Fail memzone free - tempzone0\n");
+ return -1;
+ }
+ mz[0] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone0new"), 0,
+ SOCKET_ID_ANY, 0);
+
+ if (mz[0] == NULL) {
+ printf("Fail to create memzone - tempzone0new - when MAX memzones were "
+ "created and one was free\n");
+ return -1;
+ }
+
+ for (i = i - 2; i >= 0; i--) {
+ if (rte_memzone_free(mz[i])) {
+ printf("Fail memzone free - tempzone%d\n", i);
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+static int test_memzones_left;
+static int memzone_walk_cnt;
+static void memzone_walk_clb(const struct rte_memzone *mz,
+ void *arg __rte_unused)
+{
+ memzone_walk_cnt++;
+ if (!strncmp(TEST_MEMZONE_NAME(""), mz->name, RTE_MEMZONE_NAMESIZE))
+ test_memzones_left++;
+}
+
+static int
+test_memzone_basic(void)