Skip to contentSkip to conversation

examples/vhost_blk: refactor vhost-blk example

PATCH Applied Jin Yu 3 participants 10 messages Feb 28, 2020 – Apr 30, 2020

lore
Share & tools
lore

Jin Yu

author
#0 lore raw

PATCH 1/1 examples/vhost_blk: refactor vhost-blk example

Decrease the code and make it easier to read. It's useful for understanding the inflight APIs and how packed ring works. Update the RST because the packed ring patch has been merged to QEMU master and ring_packed parameter changes to packed.

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage sample")
---
 doc/guides/sample_app_ug/vhost_blk.rst |    8 +-
 examples/vhost_blk/blk.c               |   13 +-
 examples/vhost_blk/vhost_blk.c         | 1139 ++++++++++--------------
 examples/vhost_blk/vhost_blk.h         |   39 +-
 4 files changed, 494 insertions(+), 705 deletions(-)
doc/guides/sample_app_ug/vhost_blk.rst +5 -3
index 39096e2e4..681de6f3e 100644
@@ -51,7 +51,7 @@ Start the VM
         -drive file=os.img,if=none,id=disk \
         -device ide-hd,drive=disk,bootindex=0 \
         -chardev socket,id=char0,reconnect=1,path=/tmp/vhost.socket \
-        -device vhost-user-blk-pci,ring_packed=1,chardev=char0,num-queues=1 \
+        -device vhost-user-blk-pci,packed=on,chardev=char0,num-queues=1 \
         ...
 
 .. note::
@@ -59,5 +59,7 @@ Start the VM
     Qemu v4.0 or newer version is required.
     reconnect=1 means live recovery support that qemu can reconnect vhost_blk
     after we restart vhost_blk example.
-    ring_packed=1 means the device support packed ring but need the guest kernel
-    version >= 5.0
+    packed=on means the device support packed ring but need the guest kernel
+    version >= 5.0.
+    Now Qemu commit 9bb73502321d46f4d320fa17aa38201445783fc4 both support the
+    vhost-blk reconnect and packed ring.
examples/vhost_blk/blk.c +6 -7
index 1b0b764b2..9048e2f8a 100644
@@ -50,7 +50,10 @@ vhost_bdev_blk_readwrite(struct vhost_block_dev *bdev,
 
 	offset = lba_512 * 512;
 
-	for (i = 0; i < task->iovs_cnt; i++) {
+	/* iovs[0] is the head and iovs[iovs_cnt - 1] is the tail
+	 * Middle is the data range
+	 */
+	for (i = 1; i < task->iovs_cnt - 1; i++) {
 		if (task->dxfer_dir == BLK_DIR_TO_DEV)
 			memcpy(bdev->data + offset, task->iovs[i].iov_base,
 			       task->iovs[i].iov_len);
@@ -83,7 +86,7 @@ vhost_bdev_process_blk_commands(struct vhost_block_dev *bdev,
 				"%s - passed IO buffer is not multiple of 512b"
 				"(req_idx = %"PRIu16").\n",
 				task->req->type ? "WRITE" : "READ",
-				task->head_idx);
+				task->req_idx);
 			return VIRTIO_BLK_S_UNSUPP;
 		}
 
@@ -98,14 +101,10 @@ vhost_bdev_process_blk_commands(struct vhost_block_dev *bdev,
 				"%s - passed IO buffer is not multiple of 512b"
 				"(req_idx = %"PRIu16").\n",
 				task->req->type ? "WRITE" : "READ",
-				task->head_idx);
+				task->req_idx);
 			return VIRTIO_BLK_S_UNSUPP;
 		}
 
-		if (task->readtype) {
-			fprintf(stderr, "type isn't right\n");
-			return VIRTIO_BLK_S_IOERR;
-		}
 		task->dxfer_dir = BLK_DIR_TO_DEV;
 		vhost_bdev_blk_readwrite(bdev, task,
 					 task->req->sector, task->data_len);
examples/vhost_blk/vhost_blk.c +467 -672

1,413 lines not on this page · raw message

examples/vhost_blk/vhost_blk.h +16 -23
index 933e2b7c5..4a8040588 100644
@@ -30,12 +30,18 @@ struct vring_packed_desc {
 #endif
 
 struct vhost_blk_queue {
-	struct rte_vhost_vring vq;
-	struct rte_vhost_ring_inflight inflight_vq;
+	struct rte_vhost_vring vring;
+	struct rte_vhost_ring_inflight inflight_ring;
+
 	uint16_t last_avail_idx;
 	uint16_t last_used_idx;
+	uint16_t id;
+
 	bool avail_wrap_counter;
 	bool used_wrap_counter;
+	bool packed_ring;
+
+	struct vhost_blk_task *tasks;
 };
 
 #define NUM_OF_BLK_QUEUES 1
@@ -43,10 +49,6 @@ struct vhost_blk_queue {
 #define min(a, b) (((a) < (b)) ? (a) : (b))
 
 struct vhost_block_dev {
-	/** ID for vhost library. */
-	int vid;
-	/** Queues for the block device */
-	struct vhost_blk_queue queues[NUM_OF_BLK_QUEUES];
 	/** Unique name for this block device. */
 	char name[64];
 
@@ -68,8 +70,10 @@ struct vhost_block_dev {
 
 struct vhost_blk_ctrlr {
 	uint8_t started;
-	uint8_t packed_ring;
-	uint8_t need_restart;
+	/** ID for vhost library. */
+	int vid;
+	/** Queues for the block device */
+	struct vhost_blk_queue queues[NUM_OF_BLK_QUEUES];
 	/** Only support 1 LUN for the example */
 	struct vhost_block_dev *bdev;
 	/** VM memory region */
@@ -85,31 +89,20 @@ enum blk_data_dir {
 };
 
 struct vhost_blk_task {
-	uint8_t readtype;
 	uint8_t req_idx;
-	uint16_t head_idx;
-	uint16_t last_idx;
+	uint16_t chain_num;
 	uint16_t inflight_idx;
 	uint16_t buffer_id;
 	uint32_t dxfer_dir;
 	uint32_t data_len;
-	struct virtio_blk_outhdr *req;
 
+	struct virtio_blk_outhdr *req;
 	volatile uint8_t *status;
-
 	struct iovec iovs[VHOST_BLK_MAX_IOVS];
 	uint32_t iovs_cnt;
-	struct vring_packed_desc *desc_packed;
-	struct vring_desc *desc_split;
-	struct rte_vhost_vring *vq;
-	struct vhost_block_dev *bdev;
-	struct vhost_blk_ctrlr *ctrlr;
-};
 
-struct inflight_blk_task {
-	struct vhost_blk_task blk_task;
-	struct rte_vhost_inflight_desc_packed *inflight_desc;
-	struct rte_vhost_inflight_info_packed *inflight_packed;
+	struct vhost_blk_queue *vq;
+	struct vhost_blk_ctrlr *ctrlr;
 };
 
 struct vhost_blk_ctrlr *g_vhost_ctrlr;
Signature · 1 line
2.17.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Jin Yu

Decrease the code and make it easier to read. It's useful for understanding the inflight APIs and how packed ring works. Update the RST because the packed ring patch has been merged to QEMU master and ring_packed parameter changes to packed.

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage sample")
Quoted 8 lines
---
 doc/guides/sample_app_ug/vhost_blk.rst |    8 +-
 examples/vhost_blk/blk.c               |   13 +-
 examples/vhost_blk/vhost_blk.c         | 1139 ++++++++++--------------
 examples/vhost_blk/vhost_blk.h         |   39 +-
 4 files changed, 494 insertions(+), 705 deletions(-)

Thanks,
Maxime

#2 lore raw
In reply to Jin Yu
Quoted 8 lines · Jin Yu

Decrease the code and make it easier to read. It's useful for understanding the inflight APIs and how packed ring works. Update the RST because the packed ring patch has been merged to QEMU master and ring_packed parameter changes to packed.

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage sample")
Quoted 7 lines
---
 doc/guides/sample_app_ug/vhost_blk.rst |    8 +-
 examples/vhost_blk/blk.c               |   13 +-
 examples/vhost_blk/vhost_blk.c         | 1139 ++++++++++--------------
 examples/vhost_blk/vhost_blk.h         |   39 +-
 4 files changed, 494 insertions(+), 705 deletions(-)

Applied to dpdk-next-virtio/master

Thanks,
Maxime

#3 lore raw
In reply to Jin Yu
Quoted 8 lines · Jin Yu

Decrease the code and make it easier to read. It's useful for understanding the inflight APIs and how packed ring works. Update the RST because the packed ring patch has been merged to QEMU master and ring_packed parameter changes to packed.

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage sample")
Quoted 7 lines
---
 doc/guides/sample_app_ug/vhost_blk.rst |    8 +-
 examples/vhost_blk/blk.c               |   13 +-
 examples/vhost_blk/vhost_blk.c         | 1139 ++++++++++--------------
 examples/vhost_blk/vhost_blk.h         |   39 +-
 4 files changed, 494 insertions(+), 705 deletions(-)

Getting following build error with 32-bit build, can you please check it:

.../examples/vhost_blk/vhost_blk.c: In function ‘desc_payload_to_iovs’:
.../examples/vhost_blk/vhost_blk.c:157:9: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
157 | vva = (void *)gpa_to_vva(ctrlr,
| ^

author
#4 lore raw

Got it. I will check and fix it.

Thanks.

Quoted 36 lines

-----Original Message-----
From: Yigit, Ferruh <[email protected]>
Sent: Thursday, April 30, 2020 1:54 AM
To: Yu, Jin <[email protected]>; Maxime Coquelin <[email protected]>; Tiwei Bie <[email protected]>; Wang, Zhihong <[email protected]>; Mcnamara, John <[email protected]>; Kovacevic, Marko <[email protected]>
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH] examples/vhost_blk: refactor vhost-blk example

Quoted 8 lines · Jin Yu

Decrease the code and make it easier to read. It's useful for understanding the inflight APIs and how packed ring works. Update the RST because the packed ring patch has been merged to QEMU master and ring_packed parameter changes to packed.

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage

sample")

Quoted 7 lines
---
 doc/guides/sample_app_ug/vhost_blk.rst |    8 +-
 examples/vhost_blk/blk.c               |   13 +-
 examples/vhost_blk/vhost_blk.c         | 1139 ++++++++++--------------
 examples/vhost_blk/vhost_blk.h         |   39 +-
 4 files changed, 494 insertions(+), 705 deletions(-)

Getting following build error with 32-bit build, can you please check it:

.../examples/vhost_blk/vhost_blk.c: In function ‘desc_payload_to_iovs’:
.../examples/vhost_blk/vhost_blk.c:157:9: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
157 | vva = (void *)gpa_to_vva(ctrlr,
| ^

#5 lore raw
Quoted 1 line · Jin

Got it. I will check and fix it.

Thanks, I will need the fix today, just send a v2. If not possible to do it today, please let me know.

Maxime

Quoted 39 lines

Thanks.

Quoted 36 lines

-----Original Message-----
From: Yigit, Ferruh <[email protected]>
Sent: Thursday, April 30, 2020 1:54 AM
To: Yu, Jin <[email protected]>; Maxime Coquelin <[email protected]>; Tiwei Bie <[email protected]>; Wang, Zhihong <[email protected]>; Mcnamara, John <[email protected]>; Kovacevic, Marko <[email protected]>
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH] examples/vhost_blk: refactor vhost-blk example

Quoted 8 lines · Jin Yu

Decrease the code and make it easier to read. It's useful for understanding the inflight APIs and how packed ring works. Update the RST because the packed ring patch has been merged to QEMU master and ring_packed parameter changes to packed.

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage

sample")

Quoted 7 lines
---
 doc/guides/sample_app_ug/vhost_blk.rst |    8 +-
 examples/vhost_blk/blk.c               |   13 +-
 examples/vhost_blk/vhost_blk.c         | 1139 ++++++++++--------------
 examples/vhost_blk/vhost_blk.h         |   39 +-
 4 files changed, 494 insertions(+), 705 deletions(-)

Getting following build error with 32-bit build, can you please check it:

.../examples/vhost_blk/vhost_blk.c: In function ‘desc_payload_to_iovs’:
.../examples/vhost_blk/vhost_blk.c:157:9: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
157 | vva = (void *)gpa_to_vva(ctrlr,
| ^

author
#6 lore raw

Thanks Maxime.
I just send the V2. Sorry for late.

Jin

Quoted 62 lines

-----Original Message-----
From: Maxime Coquelin <[email protected]>
Sent: Thursday, April 30, 2020 5:08 PM
To: Yu, Jin <[email protected]>; Yigit, Ferruh <[email protected]>; Tiwei Bie <[email protected]>; Wang, Zhihong <[email protected]>; Mcnamara, John <[email protected]>; Kovacevic, Marko <[email protected]>
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH] examples/vhost_blk: refactor vhost-blk example

Quoted 1 line · Jin

Got it. I will check and fix it.

Thanks, I will need the fix today, just send a v2. If not possible to do it today, please let me know.

Maxime

Quoted 18 lines

Thanks.

Quoted 16 lines

-----Original Message-----
From: Yigit, Ferruh <[email protected]>
Sent: Thursday, April 30, 2020 1:54 AM
To: Yu, Jin <[email protected]>; Maxime Coquelin <[email protected]>; Tiwei Bie <[email protected]>; Wang, Zhihong <[email protected]>; Mcnamara, John <[email protected]>; Kovacevic, Marko <[email protected]>
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH] examples/vhost_blk: refactor vhost-blk example

Quoted 3 lines · Jin Yu

Decrease the code and make it easier to read. It's useful for understanding the inflight APIs and how packed ring works. Update the RST because the packed ring patch has been merged to QEMU

master

Quoted 22 lines
Quoted 21 lines
Quoted 5 lines

and ring_packed parameter changes to packed.

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage

sample")

Quoted 7 lines
---
 doc/guides/sample_app_ug/vhost_blk.rst |    8 +-
 examples/vhost_blk/blk.c               |   13 +-
 examples/vhost_blk/vhost_blk.c         | 1139 ++++++++++--------------
 examples/vhost_blk/vhost_blk.h         |   39 +-
 4 files changed, 494 insertions(+), 705 deletions(-)

Getting following build error with 32-bit build, can you please check it:

.../examples/vhost_blk/vhost_blk.c: In function ‘desc_payload_to_iovs’:
.../examples/vhost_blk/vhost_blk.c:157:9: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
157 | vva = (void *)gpa_to_vva(ctrlr,
| ^

author
#7 lore raw

PATCH v2 1/1 examples/vhost_blk: refactor vhost-blk example

In reply to Jin Yu

Decrease the code and make it easier to read. It's useful for understanding the inflight APIs and how packed ring works. Update the RST because the packed ring patch has been merged to QEMU master and ring_packed parameter changes to packed.

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage sample")

---
V2 - fix build error in 32-bit

---
 doc/guides/sample_app_ug/vhost_blk.rst |    8 +-
 examples/vhost_blk/blk.c               |   13 +-
 examples/vhost_blk/vhost_blk.c         | 1139 ++++++++++--------------
 examples/vhost_blk/vhost_blk.h         |   39 +-
 4 files changed, 494 insertions(+), 705 deletions(-)
doc/guides/sample_app_ug/vhost_blk.rst +5 -3
index 39096e2e4..681de6f3e 100644
@@ -51,7 +51,7 @@ Start the VM
         -drive file=os.img,if=none,id=disk \
         -device ide-hd,drive=disk,bootindex=0 \
         -chardev socket,id=char0,reconnect=1,path=/tmp/vhost.socket \
-        -device vhost-user-blk-pci,ring_packed=1,chardev=char0,num-queues=1 \
+        -device vhost-user-blk-pci,packed=on,chardev=char0,num-queues=1 \
         ...
 
 .. note::
@@ -59,5 +59,7 @@ Start the VM
     Qemu v4.0 or newer version is required.
     reconnect=1 means live recovery support that qemu can reconnect vhost_blk
     after we restart vhost_blk example.
-    ring_packed=1 means the device support packed ring but need the guest kernel
-    version >= 5.0
+    packed=on means the device support packed ring but need the guest kernel
+    version >= 5.0.
+    Now Qemu commit 9bb73502321d46f4d320fa17aa38201445783fc4 both support the
+    vhost-blk reconnect and packed ring.
examples/vhost_blk/blk.c +6 -7
index 1b0b764b2..9048e2f8a 100644
@@ -50,7 +50,10 @@ vhost_bdev_blk_readwrite(struct vhost_block_dev *bdev,
 
 	offset = lba_512 * 512;
 
-	for (i = 0; i < task->iovs_cnt; i++) {
+	/* iovs[0] is the head and iovs[iovs_cnt - 1] is the tail
+	 * Middle is the data range
+	 */
+	for (i = 1; i < task->iovs_cnt - 1; i++) {
 		if (task->dxfer_dir == BLK_DIR_TO_DEV)
 			memcpy(bdev->data + offset, task->iovs[i].iov_base,
 			       task->iovs[i].iov_len);
@@ -83,7 +86,7 @@ vhost_bdev_process_blk_commands(struct vhost_block_dev *bdev,
 				"%s - passed IO buffer is not multiple of 512b"
 				"(req_idx = %"PRIu16").\n",
 				task->req->type ? "WRITE" : "READ",
-				task->head_idx);
+				task->req_idx);
 			return VIRTIO_BLK_S_UNSUPP;
 		}
 
@@ -98,14 +101,10 @@ vhost_bdev_process_blk_commands(struct vhost_block_dev *bdev,
 				"%s - passed IO buffer is not multiple of 512b"
 				"(req_idx = %"PRIu16").\n",
 				task->req->type ? "WRITE" : "READ",
-				task->head_idx);
+				task->req_idx);
 			return VIRTIO_BLK_S_UNSUPP;
 		}
 
-		if (task->readtype) {
-			fprintf(stderr, "type isn't right\n");
-			return VIRTIO_BLK_S_IOERR;
-		}
 		task->dxfer_dir = BLK_DIR_TO_DEV;
 		vhost_bdev_blk_readwrite(bdev, task,
 					 task->req->sector, task->data_len);
examples/vhost_blk/vhost_blk.c +467 -672

1,413 lines not on this page · raw message

examples/vhost_blk/vhost_blk.h +16 -23
index 933e2b7c5..4a8040588 100644
@@ -30,12 +30,18 @@ struct vring_packed_desc {
 #endif
 
 struct vhost_blk_queue {
-	struct rte_vhost_vring vq;
-	struct rte_vhost_ring_inflight inflight_vq;
+	struct rte_vhost_vring vring;
+	struct rte_vhost_ring_inflight inflight_ring;
+
 	uint16_t last_avail_idx;
 	uint16_t last_used_idx;
+	uint16_t id;
+
 	bool avail_wrap_counter;
 	bool used_wrap_counter;
+	bool packed_ring;
+
+	struct vhost_blk_task *tasks;
 };
 
 #define NUM_OF_BLK_QUEUES 1
@@ -43,10 +49,6 @@ struct vhost_blk_queue {
 #define min(a, b) (((a) < (b)) ? (a) : (b))
 
 struct vhost_block_dev {
-	/** ID for vhost library. */
-	int vid;
-	/** Queues for the block device */
-	struct vhost_blk_queue queues[NUM_OF_BLK_QUEUES];
 	/** Unique name for this block device. */
 	char name[64];
 
@@ -68,8 +70,10 @@ struct vhost_block_dev {
 
 struct vhost_blk_ctrlr {
 	uint8_t started;
-	uint8_t packed_ring;
-	uint8_t need_restart;
+	/** ID for vhost library. */
+	int vid;
+	/** Queues for the block device */
+	struct vhost_blk_queue queues[NUM_OF_BLK_QUEUES];
 	/** Only support 1 LUN for the example */
 	struct vhost_block_dev *bdev;
 	/** VM memory region */
@@ -85,31 +89,20 @@ enum blk_data_dir {
 };
 
 struct vhost_blk_task {
-	uint8_t readtype;
 	uint8_t req_idx;
-	uint16_t head_idx;
-	uint16_t last_idx;
+	uint16_t chain_num;
 	uint16_t inflight_idx;
 	uint16_t buffer_id;
 	uint32_t dxfer_dir;
 	uint32_t data_len;
-	struct virtio_blk_outhdr *req;
 
+	struct virtio_blk_outhdr *req;
 	volatile uint8_t *status;
-
 	struct iovec iovs[VHOST_BLK_MAX_IOVS];
 	uint32_t iovs_cnt;
-	struct vring_packed_desc *desc_packed;
-	struct vring_desc *desc_split;
-	struct rte_vhost_vring *vq;
-	struct vhost_block_dev *bdev;
-	struct vhost_blk_ctrlr *ctrlr;
-};
 
-struct inflight_blk_task {
-	struct vhost_blk_task blk_task;
-	struct rte_vhost_inflight_desc_packed *inflight_desc;
-	struct rte_vhost_inflight_info_packed *inflight_packed;
+	struct vhost_blk_queue *vq;
+	struct vhost_blk_ctrlr *ctrlr;
 };
 
 struct vhost_blk_ctrlr *g_vhost_ctrlr;
Signature · 1 line
2.17.2
Reviewed-by on v2 1/1
#8 lore raw
Quoted 7 lines · Jin Yu

Decrease the code and make it easier to read. It's useful for understanding the inflight APIs and how packed ring works. Update the RST because the packed ring patch has been merged to QEMU master and ring_packed parameter changes to packed.

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage sample")

I don't think the Fixes tag makes sense, so I removed it.

Quoted 1 line

Next time, please rebase your patch on top of next-virtio/master or net- next/master before posting.

Quoted 9 lines

---
V2 - fix build error in 32-bit

---
 doc/guides/sample_app_ug/vhost_blk.rst |    8 +-
 examples/vhost_blk/blk.c               |   13 +-
 examples/vhost_blk/vhost_blk.c         | 1139 ++++++++++--------------
 examples/vhost_blk/vhost_blk.h         |   39 +-
 4 files changed, 494 insertions(+), 705 deletions(-)

Thanks,
Maxime

#9 lore raw
In reply to Jin Yu
Quoted 9 lines · Jin Yu

Decrease the code and make it easier to read. It's useful for understanding the inflight APIs and how packed ring works. Update the RST because the packed ring patch has been merged to QEMU master and ring_packed parameter changes to packed.

Fixes c19beb3f38cd ("examples/vhost_blk: introduce vhost storage sample")
Quoted 8 lines

---
V2 - fix build error in 32-bit

---
 doc/guides/sample_app_ug/vhost_blk.rst |    8 +-
 examples/vhost_blk/blk.c               |   13 +-
 examples/vhost_blk/vhost_blk.c         | 1139 ++++++++++--------------
 examples/vhost_blk/vhost_blk.h         |   39 +-
 4 files changed, 494 insertions(+), 705 deletions(-)

Applied to dpdk-next-virtio/master.

Thanks,
Maxime