From ca0022f9da9a643b794c3d8886ef3ed2a71d0f0a Mon Sep 17 00:00:00 2001 From: Shuhei Matsumoto Date: Tue, 26 Feb 2019 11:26:42 +0900 Subject: [PATCH] iscsi: Unify calculate mapped length and build iovecs to flush PDUs To know the mapped length by iovecs, pdu length was got first and then the size of partial written was reduced separately. This patch unifies these two operations into spdk_iscsi_build_iovs(). Change-Id: Ic6f5eecc902b8e209ef00c010915f476ca16c002 Signed-off-by: Shuhei Matsumoto Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/446175 Tested-by: SPDK CI Jenkins Reviewed-by: Ben Walker Reviewed-by: Jim Harris Reviewed-by: Ziye Yang --- lib/iscsi/conn.c | 15 +++++---------- lib/iscsi/iscsi.c | 12 +++++++++++- lib/iscsi/iscsi.h | 3 ++- test/unit/lib/iscsi/conn.c/conn_ut.c | 2 +- test/unit/lib/iscsi/iscsi.c/iscsi_ut.c | 25 +++++++++++++++++-------- 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/lib/iscsi/conn.c b/lib/iscsi/conn.c index f9531d696..1693cbb85 100644 --- a/lib/iscsi/conn.c +++ b/lib/iscsi/conn.c @@ -1153,8 +1153,8 @@ spdk_iscsi_conn_flush_pdus_internal(struct spdk_iscsi_conn *conn) struct iovec *iov = iovs; int iovcnt = 0; int bytes = 0; - int total_length = 0; - uint32_t writev_offset; + uint32_t total_length = 0; + uint32_t mapped_length = 0; struct spdk_iscsi_pdu *pdu; int pdu_length; @@ -1173,17 +1173,12 @@ spdk_iscsi_conn_flush_pdus_internal(struct spdk_iscsi_conn *conn) * But extra overhead is negligible. */ while (pdu != NULL && ((num_iovs - iovcnt) >= 5)) { - pdu_length = spdk_iscsi_get_pdu_length(pdu, - conn->header_digest, - conn->data_digest); - iovcnt += spdk_iscsi_build_iovs(conn, &iovs[iovcnt], pdu); - total_length += pdu_length; + iovcnt += spdk_iscsi_build_iovs(conn, &iovs[iovcnt], pdu, + &mapped_length); + total_length += mapped_length; pdu = TAILQ_NEXT(pdu, tailq); } - writev_offset = TAILQ_FIRST(&conn->write_pdu_list)->writev_offset; - total_length -= writev_offset; - spdk_trace_record(TRACE_ISCSI_FLUSH_WRITEBUF_START, conn->id, total_length, 0, iovcnt); bytes = spdk_sock_writev(conn->sock, iov, iovcnt); diff --git a/lib/iscsi/iscsi.c b/lib/iscsi/iscsi.c index 2c1bab467..1525c1082 100644 --- a/lib/iscsi/iscsi.c +++ b/lib/iscsi/iscsi.c @@ -565,13 +565,14 @@ spdk_iscsi_read_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu **_pdu) int spdk_iscsi_build_iovs(struct spdk_iscsi_conn *conn, struct iovec *iovs, - struct spdk_iscsi_pdu *pdu) + struct spdk_iscsi_pdu *pdu, uint32_t *_mapped_length) { int iovcnt = 0; int enable_digest; uint32_t total_ahs_len; uint32_t data_len; uint32_t iov_offset; + uint32_t mapped_length = 0; total_ahs_len = pdu->bhs.total_ahs_len; data_len = DGET24(pdu->bhs.data_segment_len); @@ -591,6 +592,7 @@ spdk_iscsi_build_iovs(struct spdk_iscsi_conn *conn, struct iovec *iovs, } else { iovs[iovcnt].iov_base = (uint8_t *)&pdu->bhs + iov_offset; iovs[iovcnt].iov_len = ISCSI_BHS_LEN - iov_offset; + mapped_length += ISCSI_BHS_LEN - iov_offset; iov_offset = 0; iovcnt++; } @@ -601,6 +603,7 @@ spdk_iscsi_build_iovs(struct spdk_iscsi_conn *conn, struct iovec *iovs, } else { iovs[iovcnt].iov_base = pdu->ahs + iov_offset; iovs[iovcnt].iov_len = 4 * total_ahs_len - iov_offset; + mapped_length += 4 * total_ahs_len - iov_offset; iov_offset = 0; iovcnt++; } @@ -613,6 +616,7 @@ spdk_iscsi_build_iovs(struct spdk_iscsi_conn *conn, struct iovec *iovs, } else { iovs[iovcnt].iov_base = pdu->header_digest + iov_offset; iovs[iovcnt].iov_len = ISCSI_DIGEST_LEN - iov_offset; + mapped_length += ISCSI_DIGEST_LEN - iov_offset; iov_offset = 0; iovcnt++; } @@ -625,6 +629,7 @@ spdk_iscsi_build_iovs(struct spdk_iscsi_conn *conn, struct iovec *iovs, } else { iovs[iovcnt].iov_base = pdu->data + iov_offset; iovs[iovcnt].iov_len = data_len - iov_offset; + mapped_length += data_len - iov_offset; iov_offset = 0; iovcnt++; } @@ -635,10 +640,15 @@ spdk_iscsi_build_iovs(struct spdk_iscsi_conn *conn, struct iovec *iovs, if (iov_offset < ISCSI_DIGEST_LEN) { iovs[iovcnt].iov_base = pdu->data_digest + iov_offset; iovs[iovcnt].iov_len = ISCSI_DIGEST_LEN - iov_offset; + mapped_length += ISCSI_DIGEST_LEN - iov_offset; iovcnt++; } } + if (_mapped_length != NULL) { + *_mapped_length = mapped_length; + } + return iovcnt; } diff --git a/lib/iscsi/iscsi.h b/lib/iscsi/iscsi.h index 459b46b57..91b436955 100644 --- a/lib/iscsi/iscsi.h +++ b/lib/iscsi/iscsi.h @@ -405,7 +405,8 @@ void spdk_iscsi_task_response(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task); int spdk_iscsi_execute(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu); int spdk_iscsi_build_iovs(struct spdk_iscsi_conn *conn, - struct iovec *iovs, struct spdk_iscsi_pdu *pdu); + struct iovec *iovs, struct spdk_iscsi_pdu *pdu, + uint32_t *mapped_length); int spdk_iscsi_read_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu **_pdu); void spdk_iscsi_task_mgmt_response(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task); diff --git a/test/unit/lib/iscsi/conn.c/conn_ut.c b/test/unit/lib/iscsi/conn.c/conn_ut.c index 1e047f239..aa7362333 100644 --- a/test/unit/lib/iscsi/conn.c/conn_ut.c +++ b/test/unit/lib/iscsi/conn.c/conn_ut.c @@ -139,7 +139,7 @@ DEFINE_STUB_V(spdk_clear_all_transfer_task, DEFINE_STUB(spdk_iscsi_build_iovs, int, (struct spdk_iscsi_conn *conn, struct iovec *iov, - struct spdk_iscsi_pdu *pdu), + struct spdk_iscsi_pdu *pdu, uint32_t *mapped_length), 0); DEFINE_STUB(spdk_iscsi_is_deferred_free_pdu, bool, diff --git a/test/unit/lib/iscsi/iscsi.c/iscsi_ut.c b/test/unit/lib/iscsi/iscsi.c/iscsi_ut.c index 5180f7545..631ca917d 100644 --- a/test/unit/lib/iscsi/iscsi.c/iscsi_ut.c +++ b/test/unit/lib/iscsi/iscsi.c/iscsi_ut.c @@ -1209,6 +1209,7 @@ build_iovs_test(void) struct spdk_iscsi_pdu pdu = {}; struct iovec iovs[5] = {}; uint8_t *data; + uint32_t mapped_length = 0; int rc; conn.header_digest = true; @@ -1223,7 +1224,7 @@ build_iovs_test(void) pdu.bhs.opcode = ISCSI_OP_SCSI; pdu.writev_offset = 0; - rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu); + rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu, &mapped_length); CU_ASSERT(rc == 4); CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs); CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN); @@ -1233,9 +1234,10 @@ build_iovs_test(void) CU_ASSERT(iovs[2].iov_len == 512); CU_ASSERT(iovs[3].iov_base == (void *)pdu.data_digest); CU_ASSERT(iovs[3].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN); pdu.writev_offset = ISCSI_BHS_LEN / 2; - rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu); + rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu, &mapped_length); CU_ASSERT(rc == 4); CU_ASSERT(iovs[0].iov_base == (void *)((uint8_t *)&pdu.bhs + ISCSI_BHS_LEN / 2)); CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN / 2); @@ -1245,9 +1247,10 @@ build_iovs_test(void) CU_ASSERT(iovs[2].iov_len == 512); CU_ASSERT(iovs[3].iov_base == (void *)pdu.data_digest); CU_ASSERT(iovs[3].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == ISCSI_BHS_LEN / 2 + ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN); pdu.writev_offset = ISCSI_BHS_LEN; - rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu); + rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu, &mapped_length); CU_ASSERT(rc == 3); CU_ASSERT(iovs[0].iov_base == (void *)pdu.header_digest); CU_ASSERT(iovs[0].iov_len == ISCSI_DIGEST_LEN); @@ -1255,9 +1258,10 @@ build_iovs_test(void) CU_ASSERT(iovs[1].iov_len == 512); CU_ASSERT(iovs[2].iov_base == (void *)pdu.data_digest); CU_ASSERT(iovs[2].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN); pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN / 2; - rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu); + rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu, &mapped_length); CU_ASSERT(rc == 3); CU_ASSERT(iovs[0].iov_base == (void *)((uint8_t *)pdu.header_digest + ISCSI_DIGEST_LEN / 2)); CU_ASSERT(iovs[0].iov_len == ISCSI_DIGEST_LEN / 2); @@ -1265,30 +1269,35 @@ build_iovs_test(void) CU_ASSERT(iovs[1].iov_len == 512); CU_ASSERT(iovs[2].iov_base == (void *)pdu.data_digest); CU_ASSERT(iovs[2].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == ISCSI_DIGEST_LEN / 2 + 512 + ISCSI_DIGEST_LEN); pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN; - rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu); + rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu, &mapped_length); CU_ASSERT(rc == 2); CU_ASSERT(iovs[0].iov_base == (void *)pdu.data); CU_ASSERT(iovs[0].iov_len == 512); CU_ASSERT(iovs[1].iov_base == (void *)pdu.data_digest); CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == 512 + ISCSI_DIGEST_LEN); pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512; - rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu); + rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu, &mapped_length); CU_ASSERT(rc == 1); CU_ASSERT(iovs[0].iov_base == (void *)pdu.data_digest); CU_ASSERT(iovs[0].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == ISCSI_DIGEST_LEN); pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN / 2; - rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu); + rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu, &mapped_length); CU_ASSERT(rc == 1); CU_ASSERT(iovs[0].iov_base == (void *)((uint8_t *)pdu.data_digest + ISCSI_DIGEST_LEN / 2)); CU_ASSERT(iovs[0].iov_len == ISCSI_DIGEST_LEN / 2); + CU_ASSERT(mapped_length == ISCSI_DIGEST_LEN / 2); pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN; - rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu); + rc = spdk_iscsi_build_iovs(&conn, iovs, &pdu, &mapped_length); CU_ASSERT(rc == 0); + CU_ASSERT(mapped_length == 0); free(data); }