From 47568c65dec7fcb33a344f764636b58afbd13a93 Mon Sep 17 00:00:00 2001 From: John Levon Date: Fri, 6 Jan 2023 20:02:43 +0000 Subject: [PATCH] util: add spdk_iov_memset() And use it in a couple of places. Signed-off-by: John Levon Change-Id: I4b86cef0e9489c1435c0206dd6c5cda4ffe4d33a Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/16191 Tested-by: SPDK CI Jenkins Reviewed-by: Ben Walker Reviewed-by: Jim Harris Reviewed-by: Aleksey Marchuk --- CHANGELOG.md | 2 ++ include/spdk/util.h | 34 +++++++++++-------- lib/nvmf/ctrlr.c | 15 +------- lib/util/iov.c | 13 +++++++ lib/util/spdk_util.map | 1 + test/unit/lib/nvmf/subsystem.c/subsystem_ut.c | 11 ++++-- test/unit/lib/util/iov.c/iov_ut.c | 23 +++++++++++++ 7 files changed, 68 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3593a671..74cb9924b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,6 +100,8 @@ caused a function callback in file descriptor group to execute. A new API `spdk_strcpy_replace` was added to replace all occurrences of the search string with the replacement string. +New API `spdk_iov_memset()` was added to memset an iovec. + ### json Added API `spdk_json_write_double` and `spdk_json_write_named_double` to allow diff --git a/include/spdk/util.h b/include/spdk/util.h index 12041c537..6b674fd22 100644 --- a/include/spdk/util.h +++ b/include/spdk/util.h @@ -104,20 +104,6 @@ spdk_divide_round_up(uint64_t num, uint64_t divisor) return (num + divisor - 1) / divisor; } -/** - * Copy the data described by the source iovec to the destination iovec. - * - * \return The number of bytes copied. - */ -size_t spdk_iovcpy(struct iovec *siov, size_t siovcnt, struct iovec *diov, size_t diovcnt); - -/** - * Same as spdk_iovcpy(), but the src/dst buffers might overlap. - * - * \return The number of bytes copied. - */ -size_t spdk_iovmove(struct iovec *siov, size_t siovcnt, struct iovec *diov, size_t diovcnt); - /** * An iovec iterator. Can be allocated on the stack. */ @@ -157,6 +143,26 @@ size_t spdk_ioviter_first(struct spdk_ioviter *iter, */ size_t spdk_ioviter_next(struct spdk_ioviter *iter, void **src, void **dst); +/** + * Operate like memset across an iovec. + */ +void +spdk_iov_memset(struct iovec *iovs, int iovcnt, int c); + +/** + * Copy the data described by the source iovec to the destination iovec. + * + * \return The number of bytes copied. + */ +size_t spdk_iovcpy(struct iovec *siov, size_t siovcnt, struct iovec *diov, size_t diovcnt); + +/** + * Same as spdk_iovcpy(), but the src/dst buffers might overlap. + * + * \return The number of bytes copied. + */ +size_t spdk_iovmove(struct iovec *siov, size_t siovcnt, struct iovec *diov, size_t diovcnt); + /** * Copy iovs contents to buf through memcpy. */ diff --git a/lib/nvmf/ctrlr.c b/lib/nvmf/ctrlr.c index eab00ddcc..504a8af1b 100644 --- a/lib/nvmf/ctrlr.c +++ b/lib/nvmf/ctrlr.c @@ -60,19 +60,6 @@ struct copy_iovs_ctx { size_t cur_iov_offset; }; -static void -_clear_iovs(struct iovec *iovs, int iovcnt) -{ - int iov_idx = 0; - struct iovec *iov; - - while (iov_idx < iovcnt) { - iov = &iovs[iov_idx]; - memset(iov->iov_base, 0, iov->iov_len); - iov_idx++; - } -} - static void _init_copy_iovs_ctx(struct copy_iovs_ctx *copy_ctx, struct iovec *iovs, int iovcnt) { @@ -3570,7 +3557,7 @@ nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req) } if (req->data && spdk_nvme_opc_get_data_transfer(cmd->opc) == SPDK_NVME_DATA_CONTROLLER_TO_HOST) { - _clear_iovs(req->iov, req->iovcnt); + spdk_iov_memset(req->iov, req->iovcnt, 0); } if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { diff --git a/lib/util/iov.c b/lib/util/iov.c index 0d3d28280..ee735bf14 100644 --- a/lib/util/iov.c +++ b/lib/util/iov.c @@ -5,6 +5,19 @@ #include "spdk/util.h" +void +spdk_iov_memset(struct iovec *iovs, int iovcnt, int c) +{ + int iov_idx = 0; + struct iovec *iov; + + while (iov_idx < iovcnt) { + iov = &iovs[iov_idx]; + memset(iov->iov_base, c, iov->iov_len); + iov_idx++; + } +} + size_t spdk_ioviter_first(struct spdk_ioviter *iter, struct iovec *siov, size_t siovcnt, diff --git a/lib/util/spdk_util.map b/lib/util/spdk_util.map index e3ab94774..21f79a0a6 100644 --- a/lib/util/spdk_util.map +++ b/lib/util/spdk_util.map @@ -135,6 +135,7 @@ spdk_iovmove; spdk_ioviter_first; spdk_ioviter_next; + spdk_iov_memset; spdk_copy_iovs_to_buf; spdk_copy_buf_to_iovs; spdk_memset_s; diff --git a/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c b/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c index 31aaeefae..206e94035 100644 --- a/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c +++ b/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c @@ -1512,10 +1512,15 @@ test_nvmf_ns_reservation_report(void) struct spdk_nvme_reservation_status_extended_data *status_data; struct spdk_nvmf_registrant *reg; - req.data = calloc(1, sizeof(*status_data) + sizeof(*ctrlr_data) * 2); + req.length = sizeof(*status_data) + sizeof(*ctrlr_data) * 2; + req.data = calloc(1, req.length); reg = calloc(2, sizeof(struct spdk_nvmf_registrant)); SPDK_CU_ASSERT_FATAL(req.data != NULL && reg != NULL); + req.iov[0].iov_base = req.data; + req.iov[0].iov_len = req.length; + req.iovcnt = 1; + req.cmd = &cmd; req.rsp = &rsp; ns.gen = 1; @@ -1554,7 +1559,7 @@ test_nvmf_ns_reservation_report(void) CU_ASSERT(!spdk_uuid_compare((struct spdk_uuid *)ctrlr_data->hostid, ®[1].hostid)); /* extended controller data structure */ - memset(req.data, 0, sizeof(*status_data) + sizeof(*ctrlr_data) * 2); + spdk_iov_memset(req.iov, req.iovcnt, 0); memset(req.rsp, 0, sizeof(*req.rsp)); cmd.nvme_cmd.cdw11_bits.resv_report.eds = false; @@ -1563,7 +1568,7 @@ test_nvmf_ns_reservation_report(void) CU_ASSERT(req.rsp->nvme_cpl.status.sct == SPDK_NVME_SCT_GENERIC); /* Transfer length invalid */ - memset(req.data, 0, sizeof(*status_data) + sizeof(*ctrlr_data) * 2); + spdk_iov_memset(req.iov, req.iovcnt, 0); memset(req.rsp, 0, sizeof(*req.rsp)); cmd.nvme_cmd.cdw11_bits.resv_report.eds = true; cmd.nvme_cmd.cdw10 = 0; diff --git a/test/unit/lib/util/iov.c/iov_ut.c b/test/unit/lib/util/iov.c/iov_ut.c index 54e2896fd..404d3d03f 100644 --- a/test/unit/lib/util/iov.c/iov_ut.c +++ b/test/unit/lib/util/iov.c/iov_ut.c @@ -245,6 +245,28 @@ test_buf_to_iovs(void) CU_ASSERT(_check_val(ddata, 64, 7) == 0); } +static void +test_memset(void) +{ + struct iovec iov[4]; + uint8_t iov_buffer[64]; + + memset(&iov_buffer, 1, sizeof(iov_buffer)); + + iov[0].iov_base = iov_buffer; + iov[0].iov_len = 5; + iov[1].iov_base = iov[0].iov_base + iov[0].iov_len; + iov[1].iov_len = 15; + iov[2].iov_base = iov[1].iov_base + iov[1].iov_len; + iov[2].iov_len = 21; + iov[3].iov_base = iov[2].iov_base + iov[2].iov_len; + iov[3].iov_len = 23; + + spdk_iov_memset(iov, 4, 0); + + CU_ASSERT(_check_val(iov_buffer, 64, 0) == 0); +} + int main(int argc, char **argv) { @@ -261,6 +283,7 @@ main(int argc, char **argv) CU_ADD_TEST(suite, test_complex_iov); CU_ADD_TEST(suite, test_iovs_to_buf); CU_ADD_TEST(suite, test_buf_to_iovs); + CU_ADD_TEST(suite, test_memset); CU_basic_set_mode(CU_BRM_VERBOSE);