[v5,5/9] app/test: add basic dmadev copy tests

Message ID 20210917135429.90562-6-bruce.richardson@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series add test suite for DMA drivers |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Bruce Richardson Sept. 17, 2021, 1:54 p.m. UTC
For each dmadev instance, perform some basic copy tests to validate that
functionality.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
Reviewed-by: Kevin Laatz <kevin.laatz@intel.com>
Reviewed-by: Conor Walsh <conor.walsh@intel.com>
---
 app/test/test_dmadev.c | 175 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 175 insertions(+)

--
2.30.2
  

Patch

diff --git a/app/test/test_dmadev.c b/app/test/test_dmadev.c
index 28fbdc0b1f..2f22b6e382 100644
--- a/app/test/test_dmadev.c
+++ b/app/test/test_dmadev.c
@@ -6,6 +6,10 @@ 
 #include <inttypes.h>

 #include <rte_dmadev.h>
+#include <rte_mbuf.h>
+#include <rte_pause.h>
+#include <rte_cycles.h>
+#include <rte_random.h>
 #include <rte_bus_vdev.h>

 #include "test.h"
@@ -15,6 +19,11 @@  extern int test_dma_api(uint16_t dev_id);

 #define ERR_RETURN(...) do { print_err(__func__, __LINE__, __VA_ARGS__); return -1; } while (0)

+#define COPY_LEN 1024
+
+static struct rte_mempool *pool;
+static uint16_t id_count;
+
 static void
 __rte_format_printf(3, 4)
 print_err(const char *func, int lineno, const char *format, ...)
@@ -27,10 +36,155 @@  print_err(const char *func, int lineno, const char *format, ...)
 	va_end(ap);
 }

+static int
+runtest(const char *printable, int (*test_fn)(int dev_id, uint16_t vchan), int iterations,
+		int dev_id, uint16_t vchan, bool check_err_stats)
+{
+	struct rte_dma_stats stats;
+	int i;
+
+	rte_dma_stats_reset(dev_id, vchan);
+	printf("DMA Dev %d: Running %s Tests %s\n", dev_id, printable,
+			check_err_stats ? " " : "(errors expected)");
+	for (i = 0; i < iterations; i++) {
+		if (test_fn(dev_id, vchan) < 0)
+			return -1;
+
+		rte_dma_stats_get(dev_id, 0, &stats);
+		printf("Ops submitted: %"PRIu64"\t", stats.submitted);
+		printf("Ops completed: %"PRIu64"\t", stats.completed);
+		printf("Errors: %"PRIu64"\r", stats.errors);
+
+		if (stats.completed != stats.submitted)
+			ERR_RETURN("\nError, not all submitted jobs are reported as completed\n");
+		if (check_err_stats && stats.errors != 0)
+			ERR_RETURN("\nErrors reported during op processing, aborting tests\n");
+	}
+	printf("\n");
+	return 0;
+}
+
+static void
+await_hw(int dev_id, uint16_t vchan)
+{
+	enum rte_dma_vchan_status st;
+
+	if (rte_dma_vchan_status(dev_id, vchan, &st) < 0) {
+		/* for drivers that don't support this op, just sleep for 1 millisecond */
+		rte_delay_us_sleep(1000);
+		return;
+	}
+
+	/* for those that do, *max* end time is one second from now, but all should be faster */
+	const uint64_t end_cycles = rte_get_timer_cycles() + rte_get_timer_hz();
+	while (st == RTE_DMA_VCHAN_ACTIVE && rte_get_timer_cycles() < end_cycles) {
+		rte_pause();
+		rte_dma_vchan_status(dev_id, vchan, &st);
+	}
+}
+
+static int
+test_enqueue_copies(int dev_id, uint16_t vchan)
+{
+	unsigned int i;
+	uint16_t id;
+
+	/* test doing a single copy */
+	do {
+		struct rte_mbuf *src, *dst;
+		char *src_data, *dst_data;
+
+		src = rte_pktmbuf_alloc(pool);
+		dst = rte_pktmbuf_alloc(pool);
+		src_data = rte_pktmbuf_mtod(src, char *);
+		dst_data = rte_pktmbuf_mtod(dst, char *);
+
+		for (i = 0; i < COPY_LEN; i++)
+			src_data[i] = rte_rand() & 0xFF;
+
+		id = rte_dma_copy(dev_id, vchan, rte_pktmbuf_iova(src), rte_pktmbuf_iova(dst),
+				COPY_LEN, RTE_DMA_OP_FLAG_SUBMIT);
+		if (id != id_count)
+			ERR_RETURN("Error with rte_dma_copy, got %u, expected %u\n",
+					id, id_count);
+
+		/* give time for copy to finish, then check it was done */
+		await_hw(dev_id, vchan);
+
+		for (i = 0; i < COPY_LEN; i++)
+			if (dst_data[i] != src_data[i])
+				ERR_RETURN("Data mismatch at char %u [Got %02x not %02x]\n", i,
+						dst_data[i], src_data[i]);
+
+		/* now check completion works */
+		if (rte_dma_completed(dev_id, vchan, 1, &id, NULL) != 1)
+			ERR_RETURN("Error with rte_dma_completed\n");
+
+		if (id != id_count)
+			ERR_RETURN("Error:incorrect job id received, %u [expected %u]\n",
+					id, id_count);
+
+		rte_pktmbuf_free(src);
+		rte_pktmbuf_free(dst);
+
+		/* now check completion returns nothing more */
+		if (rte_dma_completed(dev_id, 0, 1, NULL, NULL) != 0)
+			ERR_RETURN("Error with rte_dma_completed in empty check\n");
+
+		id_count++;
+
+	} while (0);
+
+	/* test doing a multiple single copies */
+	do {
+		const uint16_t max_ops = 4;
+		struct rte_mbuf *src, *dst;
+		char *src_data, *dst_data;
+		uint16_t count;
+
+		src = rte_pktmbuf_alloc(pool);
+		dst = rte_pktmbuf_alloc(pool);
+		src_data = rte_pktmbuf_mtod(src, char *);
+		dst_data = rte_pktmbuf_mtod(dst, char *);
+
+		for (i = 0; i < COPY_LEN; i++)
+			src_data[i] = rte_rand() & 0xFF;
+
+		/* perform the same copy <max_ops> times */
+		for (i = 0; i < max_ops; i++)
+			if (rte_dma_copy(dev_id, vchan,
+					rte_pktmbuf_iova(src),
+					rte_pktmbuf_iova(dst),
+					COPY_LEN, RTE_DMA_OP_FLAG_SUBMIT) != id_count++)
+				ERR_RETURN("Error with rte_dma_copy\n");
+
+		await_hw(dev_id, vchan);
+
+		count = rte_dma_completed(dev_id, vchan, max_ops * 2, &id, NULL);
+		if (count != max_ops)
+			ERR_RETURN("Error with rte_dma_completed, got %u not %u\n",
+					count, max_ops);
+
+		if (id != id_count - 1)
+			ERR_RETURN("Error, incorrect job id returned: got %u not %u\n",
+					id, id_count - 1);
+
+		for (i = 0; i < COPY_LEN; i++)
+			if (dst_data[i] != src_data[i])
+				ERR_RETURN("Data mismatch at char %u\n", i);
+
+		rte_pktmbuf_free(src);
+		rte_pktmbuf_free(dst);
+	} while (0);
+
+	return 0;
+}
+
 static int
 test_dmadev_instance(uint16_t dev_id)
 {
 #define TEST_RINGSIZE 512
+#define CHECK_ERRS    true
 	struct rte_dma_stats stats;
 	struct rte_dma_info info;
 	const struct rte_dma_conf conf = { .nb_vchans = 1};
@@ -66,10 +220,31 @@  test_dmadev_instance(uint16_t dev_id)
 		ERR_RETURN("Error device stats are not all zero: completed = %"PRIu64", "
 				"submitted = %"PRIu64", errors = %"PRIu64"\n",
 				stats.completed, stats.submitted, stats.errors);
+	id_count = 0;
+
+	/* create a mempool for running tests */
+	pool = rte_pktmbuf_pool_create("TEST_DMADEV_POOL",
+			TEST_RINGSIZE * 2, /* n == num elements */
+			32,  /* cache size */
+			0,   /* priv size */
+			2048, /* data room size */
+			info.numa_node);
+	if (pool == NULL)
+		ERR_RETURN("Error with mempool creation\n");

+	/* run the test cases, use many iterations to ensure UINT16_MAX id wraparound */
+	if (runtest("copy", test_enqueue_copies, 640, dev_id, vchan, CHECK_ERRS) < 0)
+		goto err;
+
+	rte_mempool_free(pool);
 	rte_dma_stop(dev_id);
 	rte_dma_stats_reset(dev_id, vchan);
 	return 0;
+
+err:
+	rte_mempool_free(pool);
+	rte_dma_stop(dev_id);
+	return -1;
 }

 static int