Blame SOURCES/kvm-iscsi-Implement-copy-offloading.patch

357786
From e059872c2b30d6065411e7b11d6841efa69d57c4 Mon Sep 17 00:00:00 2001
357786
From: Fam Zheng <famz@redhat.com>
357786
Date: Fri, 29 Jun 2018 06:11:48 +0200
357786
Subject: [PATCH 44/57] iscsi: Implement copy offloading
357786
357786
RH-Author: Fam Zheng <famz@redhat.com>
357786
Message-id: <20180629061153.12687-9-famz@redhat.com>
357786
Patchwork-id: 81159
357786
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH v2 08/13] iscsi: Implement copy offloading
357786
Bugzilla: 1482537
357786
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
RH-Acked-by: Max Reitz <mreitz@redhat.com>
357786
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
357786
357786
Issue EXTENDED COPY (LID1) command to implement the copy_range API.
357786
357786
The parameter data construction code is modified from libiscsi's
357786
iscsi-dd.c.
357786
357786
Signed-off-by: Fam Zheng <famz@redhat.com>
357786
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
Message-id: 20180601092648.24614-9-famz@redhat.com
357786
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
(cherry picked from commit 604dfaaa3270081da689991afe83d94d3e8231df)
357786
Signed-off-by: Fam Zheng <famz@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 block/iscsi.c            | 219 +++++++++++++++++++++++++++++++++++++++++++++++
357786
 include/scsi/constants.h |   4 +
357786
 2 files changed, 223 insertions(+)
357786
357786
diff --git a/block/iscsi.c b/block/iscsi.c
357786
index 338f3dd..fbcd5bb 100644
357786
--- a/block/iscsi.c
357786
+++ b/block/iscsi.c
357786
@@ -2187,6 +2187,221 @@ static void coroutine_fn iscsi_co_invalidate_cache(BlockDriverState *bs,
357786
     iscsi_allocmap_invalidate(iscsilun);
357786
 }
357786
 
357786
+static int coroutine_fn iscsi_co_copy_range_from(BlockDriverState *bs,
357786
+                                                 BdrvChild *src,
357786
+                                                 uint64_t src_offset,
357786
+                                                 BdrvChild *dst,
357786
+                                                 uint64_t dst_offset,
357786
+                                                 uint64_t bytes,
357786
+                                                 BdrvRequestFlags flags)
357786
+{
357786
+    return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes, flags);
357786
+}
357786
+
357786
+static struct scsi_task *iscsi_xcopy_task(int param_len)
357786
+{
357786
+    struct scsi_task *task;
357786
+
357786
+    task = g_new0(struct scsi_task, 1);
357786
+
357786
+    task->cdb[0]     = EXTENDED_COPY;
357786
+    task->cdb[10]    = (param_len >> 24) & 0xFF;
357786
+    task->cdb[11]    = (param_len >> 16) & 0xFF;
357786
+    task->cdb[12]    = (param_len >> 8) & 0xFF;
357786
+    task->cdb[13]    = param_len & 0xFF;
357786
+    task->cdb_size   = 16;
357786
+    task->xfer_dir   = SCSI_XFER_WRITE;
357786
+    task->expxferlen = param_len;
357786
+
357786
+    return task;
357786
+}
357786
+
357786
+static void iscsi_populate_target_desc(unsigned char *desc, IscsiLun *lun)
357786
+{
357786
+    struct scsi_inquiry_device_designator *dd = lun->dd;
357786
+
357786
+    memset(desc, 0, 32);
357786
+    desc[0] = 0xE4; /* IDENT_DESCR_TGT_DESCR */
357786
+    desc[4] = dd->code_set;
357786
+    desc[5] = (dd->designator_type & 0xF)
357786
+        | ((dd->association & 3) << 4);
357786
+    desc[7] = dd->designator_length;
357786
+    memcpy(desc + 8, dd->designator, dd->designator_length);
357786
+
357786
+    desc[28] = 0;
357786
+    desc[29] = (lun->block_size >> 16) & 0xFF;
357786
+    desc[30] = (lun->block_size >> 8) & 0xFF;
357786
+    desc[31] = lun->block_size & 0xFF;
357786
+}
357786
+
357786
+static void iscsi_xcopy_desc_hdr(uint8_t *hdr, int dc, int cat, int src_index,
357786
+                                 int dst_index)
357786
+{
357786
+    hdr[0] = 0x02; /* BLK_TO_BLK_SEG_DESCR */
357786
+    hdr[1] = ((dc << 1) | cat) & 0xFF;
357786
+    hdr[2] = (XCOPY_BLK2BLK_SEG_DESC_SIZE >> 8) & 0xFF;
357786
+    /* don't account for the first 4 bytes in descriptor header*/
357786
+    hdr[3] = (XCOPY_BLK2BLK_SEG_DESC_SIZE - 4 /* SEG_DESC_SRC_INDEX_OFFSET */) & 0xFF;
357786
+    hdr[4] = (src_index >> 8) & 0xFF;
357786
+    hdr[5] = src_index & 0xFF;
357786
+    hdr[6] = (dst_index >> 8) & 0xFF;
357786
+    hdr[7] = dst_index & 0xFF;
357786
+}
357786
+
357786
+static void iscsi_xcopy_populate_desc(uint8_t *desc, int dc, int cat,
357786
+                                      int src_index, int dst_index, int num_blks,
357786
+                                      uint64_t src_lba, uint64_t dst_lba)
357786
+{
357786
+    iscsi_xcopy_desc_hdr(desc, dc, cat, src_index, dst_index);
357786
+
357786
+    /* The caller should verify the request size */
357786
+    assert(num_blks < 65536);
357786
+    desc[10] = (num_blks >> 8) & 0xFF;
357786
+    desc[11] = num_blks & 0xFF;
357786
+    desc[12] = (src_lba >> 56) & 0xFF;
357786
+    desc[13] = (src_lba >> 48) & 0xFF;
357786
+    desc[14] = (src_lba >> 40) & 0xFF;
357786
+    desc[15] = (src_lba >> 32) & 0xFF;
357786
+    desc[16] = (src_lba >> 24) & 0xFF;
357786
+    desc[17] = (src_lba >> 16) & 0xFF;
357786
+    desc[18] = (src_lba >> 8) & 0xFF;
357786
+    desc[19] = src_lba & 0xFF;
357786
+    desc[20] = (dst_lba >> 56) & 0xFF;
357786
+    desc[21] = (dst_lba >> 48) & 0xFF;
357786
+    desc[22] = (dst_lba >> 40) & 0xFF;
357786
+    desc[23] = (dst_lba >> 32) & 0xFF;
357786
+    desc[24] = (dst_lba >> 24) & 0xFF;
357786
+    desc[25] = (dst_lba >> 16) & 0xFF;
357786
+    desc[26] = (dst_lba >> 8) & 0xFF;
357786
+    desc[27] = dst_lba & 0xFF;
357786
+}
357786
+
357786
+static void iscsi_xcopy_populate_header(unsigned char *buf, int list_id, int str,
357786
+                                        int list_id_usage, int prio,
357786
+                                        int tgt_desc_len,
357786
+                                        int seg_desc_len, int inline_data_len)
357786
+{
357786
+    buf[0] = list_id;
357786
+    buf[1] = ((str & 1) << 5) | ((list_id_usage & 3) << 3) | (prio & 7);
357786
+    buf[2] = (tgt_desc_len >> 8) & 0xFF;
357786
+    buf[3] = tgt_desc_len & 0xFF;
357786
+    buf[8] = (seg_desc_len >> 24) & 0xFF;
357786
+    buf[9] = (seg_desc_len >> 16) & 0xFF;
357786
+    buf[10] = (seg_desc_len >> 8) & 0xFF;
357786
+    buf[11] = seg_desc_len & 0xFF;
357786
+    buf[12] = (inline_data_len >> 24) & 0xFF;
357786
+    buf[13] = (inline_data_len >> 16) & 0xFF;
357786
+    buf[14] = (inline_data_len >> 8) & 0xFF;
357786
+    buf[15] = inline_data_len & 0xFF;
357786
+}
357786
+
357786
+static void iscsi_xcopy_data(struct iscsi_data *data,
357786
+                             IscsiLun *src, int64_t src_lba,
357786
+                             IscsiLun *dst, int64_t dst_lba,
357786
+                             uint16_t num_blocks)
357786
+{
357786
+    uint8_t *buf;
357786
+    const int src_offset = XCOPY_DESC_OFFSET;
357786
+    const int dst_offset = XCOPY_DESC_OFFSET + IDENT_DESCR_TGT_DESCR_SIZE;
357786
+    const int seg_offset = dst_offset + IDENT_DESCR_TGT_DESCR_SIZE;
357786
+
357786
+    data->size = XCOPY_DESC_OFFSET +
357786
+                 IDENT_DESCR_TGT_DESCR_SIZE * 2 +
357786
+                 XCOPY_BLK2BLK_SEG_DESC_SIZE;
357786
+    data->data = g_malloc0(data->size);
357786
+    buf = data->data;
357786
+
357786
+    /* Initialise the parameter list header */
357786
+    iscsi_xcopy_populate_header(buf, 1, 0, 2 /* LIST_ID_USAGE_DISCARD */,
357786
+                                0, 2 * IDENT_DESCR_TGT_DESCR_SIZE,
357786
+                                XCOPY_BLK2BLK_SEG_DESC_SIZE,
357786
+                                0);
357786
+
357786
+    /* Initialise CSCD list with one src + one dst descriptor */
357786
+    iscsi_populate_target_desc(&buf[src_offset], src);
357786
+    iscsi_populate_target_desc(&buf[dst_offset], dst);
357786
+
357786
+    /* Initialise one segment descriptor */
357786
+    iscsi_xcopy_populate_desc(&buf[seg_offset], 0, 0, 0, 1, num_blocks,
357786
+                              src_lba, dst_lba);
357786
+}
357786
+
357786
+static int coroutine_fn iscsi_co_copy_range_to(BlockDriverState *bs,
357786
+                                               BdrvChild *src,
357786
+                                               uint64_t src_offset,
357786
+                                               BdrvChild *dst,
357786
+                                               uint64_t dst_offset,
357786
+                                               uint64_t bytes,
357786
+                                               BdrvRequestFlags flags)
357786
+{
357786
+    IscsiLun *dst_lun = dst->bs->opaque;
357786
+    IscsiLun *src_lun;
357786
+    struct IscsiTask iscsi_task;
357786
+    struct iscsi_data data;
357786
+    int r = 0;
357786
+    int block_size;
357786
+
357786
+    if (src->bs->drv->bdrv_co_copy_range_to != iscsi_co_copy_range_to) {
357786
+        return -ENOTSUP;
357786
+    }
357786
+    src_lun = src->bs->opaque;
357786
+
357786
+    if (!src_lun->dd || !dst_lun->dd) {
357786
+        return -ENOTSUP;
357786
+    }
357786
+    if (!is_byte_request_lun_aligned(dst_offset, bytes, dst_lun)) {
357786
+        return -ENOTSUP;
357786
+    }
357786
+    if (!is_byte_request_lun_aligned(src_offset, bytes, src_lun)) {
357786
+        return -ENOTSUP;
357786
+    }
357786
+    if (dst_lun->block_size != src_lun->block_size ||
357786
+        !dst_lun->block_size) {
357786
+        return -ENOTSUP;
357786
+    }
357786
+
357786
+    block_size = dst_lun->block_size;
357786
+    if (bytes / block_size > 65535) {
357786
+        return -ENOTSUP;
357786
+    }
357786
+
357786
+    iscsi_xcopy_data(&data,
357786
+                     src_lun, src_offset / block_size,
357786
+                     dst_lun, dst_offset / block_size,
357786
+                     bytes / block_size);
357786
+
357786
+    iscsi_co_init_iscsitask(dst_lun, &iscsi_task);
357786
+
357786
+    qemu_mutex_lock(&dst_lun->mutex);
357786
+    iscsi_task.task = iscsi_xcopy_task(data.size);
357786
+retry:
357786
+    if (iscsi_scsi_command_async(dst_lun->iscsi, dst_lun->lun,
357786
+                                 iscsi_task.task, iscsi_co_generic_cb,
357786
+                                 &data,
357786
+                                 &iscsi_task) != 0) {
357786
+        r = -EIO;
357786
+        goto out_unlock;
357786
+    }
357786
+
357786
+    iscsi_co_wait_for_task(&iscsi_task, dst_lun);
357786
+
357786
+    if (iscsi_task.do_retry) {
357786
+        iscsi_task.complete = 0;
357786
+        goto retry;
357786
+    }
357786
+
357786
+    if (iscsi_task.status != SCSI_STATUS_GOOD) {
357786
+        r = iscsi_task.err_code;
357786
+        goto out_unlock;
357786
+    }
357786
+
357786
+out_unlock:
357786
+    g_free(iscsi_task.task);
357786
+    qemu_mutex_unlock(&dst_lun->mutex);
357786
+    g_free(iscsi_task.err_str);
357786
+    return r;
357786
+}
357786
+
357786
 static QemuOptsList iscsi_create_opts = {
357786
     .name = "iscsi-create-opts",
357786
     .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head),
357786
@@ -2221,6 +2436,8 @@ static BlockDriver bdrv_iscsi = {
357786
 
357786
     .bdrv_co_block_status  = iscsi_co_block_status,
357786
     .bdrv_co_pdiscard      = iscsi_co_pdiscard,
357786
+    .bdrv_co_copy_range_from = iscsi_co_copy_range_from,
357786
+    .bdrv_co_copy_range_to  = iscsi_co_copy_range_to,
357786
     .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
357786
     .bdrv_co_readv         = iscsi_co_readv,
357786
     .bdrv_co_writev_flags  = iscsi_co_writev_flags,
357786
@@ -2256,6 +2473,8 @@ static BlockDriver bdrv_iser = {
357786
 
357786
     .bdrv_co_block_status  = iscsi_co_block_status,
357786
     .bdrv_co_pdiscard      = iscsi_co_pdiscard,
357786
+    .bdrv_co_copy_range_from = iscsi_co_copy_range_from,
357786
+    .bdrv_co_copy_range_to  = iscsi_co_copy_range_to,
357786
     .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
357786
     .bdrv_co_readv         = iscsi_co_readv,
357786
     .bdrv_co_writev_flags  = iscsi_co_writev_flags,
357786
diff --git a/include/scsi/constants.h b/include/scsi/constants.h
357786
index a141dd7..083a8e8 100644
357786
--- a/include/scsi/constants.h
357786
+++ b/include/scsi/constants.h
357786
@@ -311,4 +311,8 @@
357786
 #define MMC_PROFILE_HDDVD_RW_DL         0x005A
357786
 #define MMC_PROFILE_INVALID             0xFFFF
357786
 
357786
+#define XCOPY_DESC_OFFSET 16
357786
+#define IDENT_DESCR_TGT_DESCR_SIZE 32
357786
+#define XCOPY_BLK2BLK_SEG_DESC_SIZE 28
357786
+
357786
 #endif
357786
-- 
357786
1.8.3.1
357786