+static int
+check_memzone_bounded(const char *name, uint32_t len, uint32_t align,
+ uint32_t bound)
+{
+ const struct rte_memzone *mz;
+ phys_addr_t bmask;
+
+ bmask = ~((phys_addr_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->phys_addr & ((phys_addr_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 & CACHE_LINE_MASK) != 0 || mz->len < len ||
+ mz->len < CACHE_LINE_SIZE) {
+ printf("%s(%s): invalid length\n",
+ __func__, mz->name);
+ return (-1);
+ }
+
+ if ((mz->phys_addr & bmask) !=
+ ((mz->phys_addr + mz->len - 1) & bmask)) {
+ printf("%s(%s): invalid memzone boundary %u crossed\n",
+ __func__, mz->name, bound);
+ return (-1);
+ }
+
+ return (0);
+}
+
+static int
+test_memzone_bounded(void)
+{
+ const struct rte_memzone *memzone_err;
+ const char *name;
+ int rc;
+
+ /* should fail as boundary is not power of two */
+ name = "bounded_error_31";
+ if ((memzone_err = rte_memzone_reserve_bounded(name,
+ 100, SOCKET_ID_ANY, 0, 32, UINT32_MAX)) != 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 */
+ name = "bounded_error_32";
+ if ((memzone_err = rte_memzone_reserve_bounded(name,
+ 100, SOCKET_ID_ANY, 0, 32, 32)) != NULL) {
+ printf("%s(%s)created a memzone with invalid boundary "
+ "conditions\n", __func__, memzone_err->name);
+ return (-1);
+ }
+
+ if ((rc = check_memzone_bounded("bounded_128", 100, 128, 128)) != 0)
+ return (rc);
+
+ if ((rc = check_memzone_bounded("bounded_256", 100, 256, 128)) != 0)
+ return (rc);
+
+ if ((rc = check_memzone_bounded("bounded_1K", 100, 64, 1024)) != 0)
+ return (rc);
+
+ if ((rc = check_memzone_bounded("bounded_1K_MAX", 0, 64, 1024)) != 0)
+ return (rc);
+
+ return (0);
+}
+
+static int
+test_memzone_reserve_memory_in_smallest_segment(void)
+{
+ const struct rte_memzone *mz;
+ const struct rte_memseg *ms, *min_ms, *prev_min_ms;
+ size_t min_len, prev_min_len;
+ const struct rte_config *config;
+ int i;
+
+ config = rte_eal_get_configuration();
+
+ min_ms = NULL; /*< smallest segment */
+ prev_min_ms = NULL; /*< second smallest segment */
+
+ /* find two smallest segments */
+ for (i = 0; i < RTE_MAX_MEMSEG; i++) {
+ ms = &config->mem_config->free_memseg[i];
+
+ if (ms->addr == NULL)
+ break;
+ if (ms->len == 0)
+ continue;
+
+ if (min_ms == NULL)
+ min_ms = ms;
+ else if (min_ms->len > ms->len) {
+ /* set last smallest to second last */
+ prev_min_ms = min_ms;
+
+ /* set new smallest */
+ min_ms = ms;
+ }
+ else if (prev_min_ms == NULL) {
+ prev_min_ms = ms;
+ }
+ }
+
+ if (min_ms == NULL || prev_min_ms == NULL) {
+ printf("Smallest segments not found!\n");
+ return -1;
+ }
+
+ min_len = min_ms->len;
+ prev_min_len = prev_min_ms->len;
+
+ /* try reserving a memzone in the smallest memseg */
+ mz = rte_memzone_reserve("smallest_mz", CACHE_LINE_SIZE,
+ SOCKET_ID_ANY, 0);
+ if (mz == NULL) {
+ printf("Failed to reserve memory from smallest memseg!\n");
+ return -1;
+ }
+ if (prev_min_ms->len != prev_min_len &&
+ min_ms->len != min_len - CACHE_LINE_SIZE) {
+ printf("Reserved memory from wrong memseg!\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+/* this test is a bit tricky, and thus warrants explanation.
+ *
+ * first, we find two smallest memsegs to conduct our experiments on.
+ *
+ * then, we bring them within alignment from each other: if second segment is
+ * twice+ as big as the first, reserve memory from that segment; if second
+ * segment is comparable in length to the first, then cut the first segment
+ * down until it becomes less than half of second segment, and then cut down
+ * the second segment to be within alignment of the first.
+ *
+ * then, we have to pass the following test: if segments are within alignment
+ * of each other (that is, the difference is less than 256 bytes, which is what
+ * our alignment will be), segment with smallest offset should be picked.
+ *
+ * we know that min_ms will be our smallest segment, so we need to make sure
+ * that we adjust the alignments so that the bigger segment has smallest
+ * alignment (in our case, smallest segment will have 64-byte alignment, while
+ * bigger segment will have 128-byte alignment).
+ */
+static int
+test_memzone_reserve_memory_with_smallest_offset(void)
+{
+ const struct rte_memseg *ms, *min_ms, *prev_min_ms;
+ size_t len, min_len, prev_min_len;
+ const struct rte_config *config;
+ int i, align;
+
+ config = rte_eal_get_configuration();
+
+ min_ms = NULL; /*< smallest segment */
+ prev_min_ms = NULL; /*< second smallest segment */
+ align = CACHE_LINE_SIZE * 4;
+
+ /* find two smallest segments */
+ for (i = 0; i < RTE_MAX_MEMSEG; i++) {
+ ms = &config->mem_config->free_memseg[i];
+
+ if (ms->addr == NULL)
+ break;
+ if (ms->len == 0)
+ continue;
+
+ if (min_ms == NULL)
+ min_ms = ms;
+ else if (min_ms->len > ms->len) {
+ /* set last smallest to second last */
+ prev_min_ms = min_ms;
+
+ /* set new smallest */
+ min_ms = ms;
+ }
+ else if (prev_min_ms == NULL) {
+ prev_min_ms = ms;
+ }
+ }
+
+ if (min_ms == NULL || prev_min_ms == NULL) {
+ printf("Smallest segments not found!\n");
+ return -1;
+ }
+
+ prev_min_len = prev_min_ms->len;
+ min_len = min_ms->len;
+
+ /* if smallest segment is bigger than half of bigger segment */
+ if (prev_min_ms->len - min_ms->len <= min_ms->len) {
+
+ len = (min_ms->len * 2) - prev_min_ms->len;
+
+ /* make sure final length is *not* aligned */
+ while (((min_ms->addr_64 + len) & (align-1)) == 0)
+ len += CACHE_LINE_SIZE;
+
+ if (rte_memzone_reserve("dummy_mz1", len, SOCKET_ID_ANY, 0) == NULL) {
+ printf("Cannot reserve memory!\n");
+ return -1;
+ }
+
+ /* check if we got memory from correct segment */
+ if (min_ms->len != min_len - len) {
+ printf("Reserved memory from wrong segment!\n");
+ return -1;
+ }
+ }
+ /* if we don't need to touch smallest segment but it's aligned */
+ else if ((min_ms->addr_64 & (align-1)) == 0) {
+ if (rte_memzone_reserve("align_mz1", CACHE_LINE_SIZE,
+ SOCKET_ID_ANY, 0) == NULL) {
+ printf("Cannot reserve memory!\n");
+ return -1;
+ }
+ if (min_ms->len != min_len - CACHE_LINE_SIZE) {
+ printf("Reserved memory from wrong segment!\n");
+ return -1;
+ }
+ }
+
+ /* if smallest segment is less than half of bigger segment */
+ if (prev_min_ms->len - min_ms->len > min_ms->len) {
+ len = prev_min_ms->len - min_ms->len - align;
+
+ /* make sure final length is aligned */
+ while (((prev_min_ms->addr_64 + len) & (align-1)) != 0)
+ len += CACHE_LINE_SIZE;
+
+ if (rte_memzone_reserve("dummy_mz2", len, SOCKET_ID_ANY, 0) == NULL) {
+ printf("Cannot reserve memory!\n");
+ return -1;
+ }
+
+ /* check if we got memory from correct segment */
+ if (prev_min_ms->len != prev_min_len - len) {
+ printf("Reserved memory from wrong segment!\n");
+ return -1;
+ }
+ }
+ len = CACHE_LINE_SIZE;
+
+
+
+ prev_min_len = prev_min_ms->len;
+ min_len = min_ms->len;
+
+ if (min_len >= prev_min_len || prev_min_len - min_len > (unsigned) align) {
+ printf("Segments are of wrong lengths!\n");
+ return -1;
+ }
+
+ /* try reserving from a bigger segment */
+ if (rte_memzone_reserve_aligned("smallest_offset", len, SOCKET_ID_ANY, 0, align) ==
+ NULL) {
+ printf("Cannot reserve memory!\n");
+ return -1;
+ }
+
+ /* check if we got memory from correct segment */
+ if (min_ms->len != min_len && prev_min_ms->len != (prev_min_len - len)) {
+ printf("Reserved memory from segment with smaller offset!\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
+test_memzone_reserve_remainder(void)
+{
+ const struct rte_memzone *mz1, *mz2;
+ const struct rte_memseg *ms, *min_ms = NULL;
+ size_t min_len;
+ const struct rte_config *config;
+ int i, align;
+
+ min_len = 0;
+ align = CACHE_LINE_SIZE;
+
+ config = rte_eal_get_configuration();
+
+ /* find minimum free contiguous length */
+ for (i = 0; i < RTE_MAX_MEMSEG; i++) {
+ ms = &config->mem_config->free_memseg[i];
+
+ if (ms->addr == NULL)
+ break;
+ if (ms->len == 0)
+ continue;
+
+ if (min_len == 0 || ms->len < min_len) {
+ min_len = ms->len;
+ min_ms = ms;
+
+ /* find maximum alignment this segment is able to hold */
+ align = CACHE_LINE_SIZE;
+ while ((ms->addr_64 & (align-1)) == 0) {
+ align <<= 1;
+ }
+ }
+ }
+
+ if (min_ms == NULL) {
+ printf("Minimal sized segment not found!\n");
+ return -1;
+ }
+
+ /* try reserving min_len bytes with alignment - this should not affect our
+ * memseg, the memory will be taken from a different one.
+ */
+ mz1 = rte_memzone_reserve_aligned("reserve_remainder_1", min_len,
+ SOCKET_ID_ANY, 0, align);
+ if (mz1 == NULL) {
+ printf("Failed to reserve %zu bytes aligned on %i bytes\n", min_len,
+ align);
+ return -1;
+ }
+ if (min_ms->len != min_len) {
+ printf("Memseg memory should not have been reserved!\n");
+ return -1;
+ }
+
+ /* try reserving min_len bytes with less alignment - this should fill up
+ * the segment.
+ */
+ mz2 = rte_memzone_reserve("reserve_remainder_2", min_len,
+ SOCKET_ID_ANY, 0);
+ if (mz2 == NULL) {
+ printf("Failed to reserve %zu bytes\n", min_len);
+ return -1;
+ }
+ if (min_ms->len != 0) {
+ printf("Memseg memory should have been reserved!\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int