2022-06-03 19:15:11 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
2022-11-01 20:26:26 +00:00
|
|
|
* Copyright (C) 2017 Intel Corporation. All rights reserved.
|
2017-05-24 14:48:22 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/virtio_blk.h>
|
|
|
|
|
|
|
|
#include "spdk/env.h"
|
|
|
|
#include "spdk/bdev.h"
|
2019-05-06 01:24:57 +00:00
|
|
|
#include "spdk/bdev_module.h"
|
2018-06-11 20:32:15 +00:00
|
|
|
#include "spdk/thread.h"
|
2017-05-24 14:48:22 +00:00
|
|
|
#include "spdk/likely.h"
|
|
|
|
#include "spdk/string.h"
|
|
|
|
#include "spdk/util.h"
|
|
|
|
#include "spdk/vhost.h"
|
2022-11-03 11:33:25 +00:00
|
|
|
#include "spdk/json.h"
|
2017-05-24 14:48:22 +00:00
|
|
|
|
|
|
|
#include "vhost_internal.h"
|
2020-03-26 14:05:19 +00:00
|
|
|
#include <rte_version.h>
|
2017-05-24 14:48:22 +00:00
|
|
|
|
2019-12-03 21:17:35 +00:00
|
|
|
/* Minimal set of features supported by every SPDK VHOST-BLK device */
|
|
|
|
#define SPDK_VHOST_BLK_FEATURES_BASE (SPDK_VHOST_FEATURES | \
|
2019-12-03 20:58:42 +00:00
|
|
|
(1ULL << VIRTIO_BLK_F_SIZE_MAX) | (1ULL << VIRTIO_BLK_F_SEG_MAX) | \
|
2019-12-03 21:17:35 +00:00
|
|
|
(1ULL << VIRTIO_BLK_F_GEOMETRY) | (1ULL << VIRTIO_BLK_F_BLK_SIZE) | \
|
|
|
|
(1ULL << VIRTIO_BLK_F_TOPOLOGY) | (1ULL << VIRTIO_BLK_F_BARRIER) | \
|
|
|
|
(1ULL << VIRTIO_BLK_F_SCSI) | (1ULL << VIRTIO_BLK_F_CONFIG_WCE) | \
|
|
|
|
(1ULL << VIRTIO_BLK_F_MQ))
|
2019-12-03 20:58:42 +00:00
|
|
|
|
|
|
|
/* Not supported features */
|
|
|
|
#define SPDK_VHOST_BLK_DISABLED_FEATURES (SPDK_VHOST_DISABLED_FEATURES | \
|
|
|
|
(1ULL << VIRTIO_BLK_F_GEOMETRY) | (1ULL << VIRTIO_BLK_F_CONFIG_WCE) | \
|
|
|
|
(1ULL << VIRTIO_BLK_F_BARRIER) | (1ULL << VIRTIO_BLK_F_SCSI))
|
|
|
|
|
2019-12-25 10:01:18 +00:00
|
|
|
/* Vhost-blk support protocol features */
|
2019-10-14 16:43:37 +00:00
|
|
|
#define SPDK_VHOST_BLK_PROTOCOL_FEATURES ((1ULL << VHOST_USER_PROTOCOL_F_CONFIG) | \
|
|
|
|
(1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))
|
2019-12-25 10:01:18 +00:00
|
|
|
|
2021-09-17 08:23:35 +00:00
|
|
|
#define VIRTIO_BLK_DEFAULT_TRANSPORT "vhost_user_blk"
|
|
|
|
|
2022-04-12 07:26:56 +00:00
|
|
|
struct spdk_vhost_user_blk_task {
|
|
|
|
struct spdk_vhost_blk_task blk_task;
|
2018-12-16 00:15:06 +00:00
|
|
|
struct spdk_vhost_blk_session *bvsession;
|
2017-08-31 14:38:35 +00:00
|
|
|
struct spdk_vhost_virtqueue *vq;
|
2017-08-08 16:52:33 +00:00
|
|
|
|
2017-05-24 14:48:22 +00:00
|
|
|
uint16_t req_idx;
|
2019-08-05 10:43:38 +00:00
|
|
|
uint16_t num_descs;
|
|
|
|
uint16_t buffer_id;
|
2020-08-17 16:31:32 +00:00
|
|
|
uint16_t inflight_head;
|
2017-05-24 14:48:22 +00:00
|
|
|
|
2017-10-10 20:19:40 +00:00
|
|
|
/* If set, the task is currently used for I/O processing. */
|
|
|
|
bool used;
|
2017-05-24 14:48:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct spdk_vhost_blk_dev {
|
|
|
|
struct spdk_vhost_dev vdev;
|
|
|
|
struct spdk_bdev *bdev;
|
2017-06-29 18:23:50 +00:00
|
|
|
struct spdk_bdev_desc *bdev_desc;
|
2021-09-17 08:23:35 +00:00
|
|
|
const struct spdk_virtio_blk_transport_ops *ops;
|
|
|
|
|
2020-04-24 14:19:23 +00:00
|
|
|
/* dummy_io_channel is used to hold a bdev reference */
|
|
|
|
struct spdk_io_channel *dummy_io_channel;
|
2018-12-14 12:51:19 +00:00
|
|
|
bool readonly;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct spdk_vhost_blk_session {
|
|
|
|
/* The parent session must be the very first field in this struct */
|
|
|
|
struct spdk_vhost_session vsession;
|
|
|
|
struct spdk_vhost_blk_dev *bvdev;
|
2017-05-24 14:48:22 +00:00
|
|
|
struct spdk_poller *requestq_poller;
|
2019-01-06 23:15:16 +00:00
|
|
|
struct spdk_io_channel *io_channel;
|
2019-03-16 23:31:45 +00:00
|
|
|
struct spdk_poller *stop_poller;
|
2017-05-24 14:48:22 +00:00
|
|
|
};
|
|
|
|
|
2018-01-26 14:29:20 +00:00
|
|
|
/* forward declaration */
|
|
|
|
static const struct spdk_vhost_dev_backend vhost_blk_device_backend;
|
|
|
|
|
2022-06-22 21:35:04 +00:00
|
|
|
static void vhost_user_blk_request_finish(uint8_t status, struct spdk_vhost_blk_task *task,
|
|
|
|
void *cb_arg);
|
2022-04-20 11:42:59 +00:00
|
|
|
|
|
|
|
static int
|
2022-04-12 07:26:56 +00:00
|
|
|
vhost_user_process_blk_request(struct spdk_vhost_user_blk_task *user_task)
|
2022-04-20 11:42:59 +00:00
|
|
|
{
|
2022-04-12 07:26:56 +00:00
|
|
|
struct spdk_vhost_blk_session *bvsession = user_task->bvsession;
|
2022-04-20 11:42:59 +00:00
|
|
|
struct spdk_vhost_dev *vdev = &bvsession->bvdev->vdev;
|
|
|
|
|
2021-09-17 08:23:35 +00:00
|
|
|
return virtio_blk_process_request(vdev, bvsession->io_channel, &user_task->blk_task,
|
|
|
|
vhost_user_blk_request_finish, NULL);
|
2022-04-20 11:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct spdk_vhost_blk_dev *
|
|
|
|
to_blk_dev(struct spdk_vhost_dev *vdev)
|
|
|
|
{
|
|
|
|
if (vdev == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-05-13 08:37:06 +00:00
|
|
|
if (vdev->backend->type != VHOST_BACKEND_BLK) {
|
2022-04-20 11:42:59 +00:00
|
|
|
SPDK_ERRLOG("%s: not a vhost-blk device\n", vdev->name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SPDK_CONTAINEROF(vdev, struct spdk_vhost_blk_dev, vdev);
|
|
|
|
}
|
2018-09-13 09:45:18 +00:00
|
|
|
|
2022-04-25 13:01:41 +00:00
|
|
|
struct spdk_bdev *
|
|
|
|
vhost_blk_get_bdev(struct spdk_vhost_dev *vdev)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_blk_dev *bvdev = to_blk_dev(vdev);
|
|
|
|
|
|
|
|
assert(bvdev != NULL);
|
|
|
|
|
|
|
|
return bvdev->bdev;
|
|
|
|
}
|
|
|
|
|
2020-10-24 05:30:44 +00:00
|
|
|
static struct spdk_vhost_blk_session *
|
|
|
|
to_blk_session(struct spdk_vhost_session *vsession)
|
|
|
|
{
|
2022-05-13 08:37:06 +00:00
|
|
|
assert(vsession->vdev->backend->type == VHOST_BACKEND_BLK);
|
2020-10-24 05:30:44 +00:00
|
|
|
return (struct spdk_vhost_blk_session *)vsession;
|
|
|
|
}
|
|
|
|
|
2017-05-24 14:48:22 +00:00
|
|
|
static void
|
2022-04-12 07:26:56 +00:00
|
|
|
blk_task_finish(struct spdk_vhost_user_blk_task *task)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
2018-12-16 00:15:06 +00:00
|
|
|
assert(task->bvsession->vsession.task_cnt > 0);
|
|
|
|
task->bvsession->vsession.task_cnt--;
|
2017-10-10 20:19:40 +00:00
|
|
|
task->used = false;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 09:09:05 +00:00
|
|
|
static void
|
2022-04-12 07:26:56 +00:00
|
|
|
blk_task_init(struct spdk_vhost_user_blk_task *task)
|
2020-01-21 09:09:05 +00:00
|
|
|
{
|
2022-04-12 07:26:56 +00:00
|
|
|
struct spdk_vhost_blk_task *blk_task = &task->blk_task;
|
|
|
|
|
2020-01-21 09:09:05 +00:00
|
|
|
task->used = true;
|
2022-04-12 07:26:56 +00:00
|
|
|
blk_task->iovcnt = SPDK_COUNTOF(blk_task->iovs);
|
|
|
|
blk_task->status = NULL;
|
|
|
|
blk_task->used_len = 0;
|
|
|
|
blk_task->payload_size = 0;
|
2020-01-21 09:09:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 10:43:38 +00:00
|
|
|
static void
|
2022-04-12 07:26:56 +00:00
|
|
|
blk_task_enqueue(struct spdk_vhost_user_blk_task *task)
|
2019-08-05 10:43:38 +00:00
|
|
|
{
|
|
|
|
if (task->vq->packed.packed_ring) {
|
|
|
|
vhost_vq_packed_ring_enqueue(&task->bvsession->vsession, task->vq,
|
|
|
|
task->num_descs,
|
2022-04-12 07:26:56 +00:00
|
|
|
task->buffer_id, task->blk_task.used_len,
|
2020-08-17 16:31:32 +00:00
|
|
|
task->inflight_head);
|
2019-08-05 10:43:38 +00:00
|
|
|
} else {
|
|
|
|
vhost_vq_used_ring_enqueue(&task->bvsession->vsession, task->vq,
|
2022-04-12 07:26:56 +00:00
|
|
|
task->req_idx, task->blk_task.used_len);
|
2019-08-05 10:43:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 14:48:22 +00:00
|
|
|
static void
|
2021-09-17 08:23:35 +00:00
|
|
|
vhost_user_blk_request_finish(uint8_t status, struct spdk_vhost_blk_task *task, void *cb_arg)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
2022-04-12 07:26:56 +00:00
|
|
|
struct spdk_vhost_user_blk_task *user_task;
|
|
|
|
|
|
|
|
user_task = SPDK_CONTAINEROF(task, struct spdk_vhost_user_blk_task, blk_task);
|
|
|
|
|
|
|
|
blk_task_enqueue(user_task);
|
2022-04-05 12:43:23 +00:00
|
|
|
|
|
|
|
SPDK_DEBUGLOG(vhost_blk, "Finished task (%p) req_idx=%d\n status: %" PRIu8"\n",
|
2022-04-12 07:26:56 +00:00
|
|
|
user_task, user_task->req_idx, status);
|
|
|
|
blk_task_finish(user_task);
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 08:47:53 +00:00
|
|
|
static void
|
|
|
|
blk_request_finish(uint8_t status, struct spdk_vhost_blk_task *task)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (task->status) {
|
|
|
|
*task->status = status;
|
|
|
|
}
|
|
|
|
|
2021-09-17 08:23:35 +00:00
|
|
|
task->cb(status, task, task->cb_arg);
|
2021-09-17 08:47:53 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 14:48:22 +00:00
|
|
|
/*
|
|
|
|
* Process task's descriptor chain and setup data related fields.
|
|
|
|
* Return
|
2021-11-25 01:40:58 +00:00
|
|
|
* total size of supplied buffers
|
2017-05-24 14:48:22 +00:00
|
|
|
*
|
|
|
|
* FIXME: Make this function return to rd_cnt and wr_cnt
|
|
|
|
*/
|
|
|
|
static int
|
2019-08-05 10:43:38 +00:00
|
|
|
blk_iovs_split_queue_setup(struct spdk_vhost_blk_session *bvsession,
|
|
|
|
struct spdk_vhost_virtqueue *vq,
|
|
|
|
uint16_t req_idx, struct iovec *iovs, uint16_t *iovs_cnt, uint32_t *length)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
2018-12-16 00:15:06 +00:00
|
|
|
struct spdk_vhost_session *vsession = &bvsession->vsession;
|
|
|
|
struct spdk_vhost_dev *vdev = vsession->vdev;
|
2017-10-09 15:28:00 +00:00
|
|
|
struct vring_desc *desc, *desc_table;
|
2017-05-24 14:48:22 +00:00
|
|
|
uint16_t out_cnt = 0, cnt = 0;
|
2017-10-09 15:28:00 +00:00
|
|
|
uint32_t desc_table_size, len = 0;
|
2018-12-05 10:46:08 +00:00
|
|
|
uint32_t desc_handled_cnt;
|
2017-10-09 15:28:00 +00:00
|
|
|
int rc;
|
|
|
|
|
2019-07-20 21:06:19 +00:00
|
|
|
rc = vhost_vq_get_desc(vsession, vq, req_idx, &desc, &desc_table, &desc_table_size);
|
2017-10-09 15:28:00 +00:00
|
|
|
if (rc != 0) {
|
2019-07-20 14:59:04 +00:00
|
|
|
SPDK_ERRLOG("%s: invalid descriptor at index %"PRIu16".\n", vdev->name, req_idx);
|
2017-10-09 15:28:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-05-24 14:48:22 +00:00
|
|
|
|
2018-12-05 10:46:08 +00:00
|
|
|
desc_handled_cnt = 0;
|
2017-05-24 14:48:22 +00:00
|
|
|
while (1) {
|
|
|
|
/*
|
|
|
|
* Maximum cnt reached?
|
|
|
|
* Should not happen if request is well formatted, otherwise this is a BUG.
|
|
|
|
*/
|
|
|
|
if (spdk_unlikely(cnt == *iovs_cnt)) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "%s: max IOVs in request reached (req_idx = %"PRIu16").\n",
|
2019-07-20 14:59:04 +00:00
|
|
|
vsession->name, req_idx);
|
2017-05-24 14:48:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-07-20 21:06:19 +00:00
|
|
|
if (spdk_unlikely(vhost_vring_desc_to_iov(vsession, iovs, &cnt, desc))) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "%s: invalid descriptor %" PRIu16" (req_idx = %"PRIu16").\n",
|
2019-07-20 14:59:04 +00:00
|
|
|
vsession->name, req_idx, cnt);
|
2017-05-24 14:48:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-07-28 15:36:53 +00:00
|
|
|
len += desc->len;
|
2017-05-24 14:48:22 +00:00
|
|
|
|
2019-07-20 21:06:19 +00:00
|
|
|
out_cnt += vhost_vring_desc_is_wr(desc);
|
2017-05-24 14:48:22 +00:00
|
|
|
|
2019-07-20 21:06:19 +00:00
|
|
|
rc = vhost_vring_desc_get_next(&desc, desc_table, desc_table_size);
|
2017-10-09 15:28:00 +00:00
|
|
|
if (rc != 0) {
|
2019-07-20 14:59:04 +00:00
|
|
|
SPDK_ERRLOG("%s: descriptor chain at index %"PRIu16" terminated unexpectedly.\n",
|
|
|
|
vsession->name, req_idx);
|
2017-10-09 15:28:00 +00:00
|
|
|
return -1;
|
|
|
|
} else if (desc == NULL) {
|
2017-05-24 14:48:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-12-05 10:46:08 +00:00
|
|
|
|
|
|
|
desc_handled_cnt++;
|
|
|
|
if (spdk_unlikely(desc_handled_cnt > desc_table_size)) {
|
|
|
|
/* Break a cycle and report an error, if any. */
|
|
|
|
SPDK_ERRLOG("%s: found a cycle in the descriptor chain: desc_table_size = %d, desc_handled_cnt = %d.\n",
|
2019-07-20 14:59:04 +00:00
|
|
|
vsession->name, desc_table_size, desc_handled_cnt);
|
2018-12-05 10:46:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There must be least two descriptors.
|
|
|
|
* First contain request so it must be readable.
|
|
|
|
* Last descriptor contain buffer for response so it must be writable.
|
|
|
|
*/
|
|
|
|
if (spdk_unlikely(out_cnt == 0 || cnt < 2)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*length = len;
|
|
|
|
*iovs_cnt = cnt;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-18 14:30:31 +00:00
|
|
|
static int
|
|
|
|
blk_iovs_packed_desc_setup(struct spdk_vhost_session *vsession,
|
|
|
|
struct spdk_vhost_virtqueue *vq, uint16_t req_idx,
|
|
|
|
struct vring_packed_desc *desc_table, uint16_t desc_table_size,
|
|
|
|
struct iovec *iovs, uint16_t *iovs_cnt, uint32_t *length)
|
|
|
|
{
|
|
|
|
struct vring_packed_desc *desc;
|
|
|
|
uint16_t cnt = 0, out_cnt = 0;
|
|
|
|
uint32_t len = 0;
|
|
|
|
|
|
|
|
if (desc_table == NULL) {
|
|
|
|
desc = &vq->vring.desc_packed[req_idx];
|
|
|
|
} else {
|
|
|
|
req_idx = 0;
|
|
|
|
desc = desc_table;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
/*
|
|
|
|
* Maximum cnt reached?
|
|
|
|
* Should not happen if request is well formatted, otherwise this is a BUG.
|
|
|
|
*/
|
|
|
|
if (spdk_unlikely(cnt == *iovs_cnt)) {
|
|
|
|
SPDK_ERRLOG("%s: max IOVs in request reached (req_idx = %"PRIu16").\n",
|
|
|
|
vsession->name, req_idx);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spdk_unlikely(vhost_vring_packed_desc_to_iov(vsession, iovs, &cnt, desc))) {
|
|
|
|
SPDK_ERRLOG("%s: invalid descriptor %" PRIu16" (req_idx = %"PRIu16").\n",
|
|
|
|
vsession->name, req_idx, cnt);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
len += desc->len;
|
|
|
|
out_cnt += vhost_vring_packed_desc_is_wr(desc);
|
|
|
|
|
|
|
|
/* desc is NULL means we reach the last desc of this request */
|
|
|
|
vhost_vring_packed_desc_get_next(&desc, &req_idx, vq, desc_table, desc_table_size);
|
|
|
|
if (desc == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There must be least two descriptors.
|
|
|
|
* First contain request so it must be readable.
|
|
|
|
* Last descriptor contain buffer for response so it must be writable.
|
|
|
|
*/
|
|
|
|
if (spdk_unlikely(out_cnt == 0 || cnt < 2)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*length = len;
|
|
|
|
*iovs_cnt = cnt;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:43:38 +00:00
|
|
|
static int
|
|
|
|
blk_iovs_packed_queue_setup(struct spdk_vhost_blk_session *bvsession,
|
2020-08-18 14:30:31 +00:00
|
|
|
struct spdk_vhost_virtqueue *vq, uint16_t req_idx,
|
|
|
|
struct iovec *iovs, uint16_t *iovs_cnt, uint32_t *length)
|
2019-08-05 10:43:38 +00:00
|
|
|
{
|
|
|
|
struct spdk_vhost_session *vsession = &bvsession->vsession;
|
|
|
|
struct spdk_vhost_dev *vdev = vsession->vdev;
|
|
|
|
struct vring_packed_desc *desc = NULL, *desc_table;
|
2020-08-18 14:30:31 +00:00
|
|
|
uint32_t desc_table_size;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = vhost_vq_get_desc_packed(vsession, vq, req_idx, &desc,
|
|
|
|
&desc_table, &desc_table_size);
|
|
|
|
if (spdk_unlikely(rc != 0)) {
|
|
|
|
SPDK_ERRLOG("%s: Invalid descriptor at index %"PRIu16".\n", vdev->name, req_idx);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return blk_iovs_packed_desc_setup(vsession, vq, req_idx, desc_table, desc_table_size,
|
|
|
|
iovs, iovs_cnt, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
blk_iovs_inflight_queue_setup(struct spdk_vhost_blk_session *bvsession,
|
|
|
|
struct spdk_vhost_virtqueue *vq, uint16_t req_idx,
|
|
|
|
struct iovec *iovs, uint16_t *iovs_cnt, uint32_t *length)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_session *vsession = &bvsession->vsession;
|
|
|
|
struct spdk_vhost_dev *vdev = vsession->vdev;
|
|
|
|
spdk_vhost_inflight_desc *inflight_desc;
|
|
|
|
struct vring_packed_desc *desc_table;
|
2019-08-05 10:43:38 +00:00
|
|
|
uint16_t out_cnt = 0, cnt = 0;
|
|
|
|
uint32_t desc_table_size, len = 0;
|
|
|
|
int rc = 0;
|
|
|
|
|
2020-08-18 14:30:31 +00:00
|
|
|
rc = vhost_inflight_queue_get_desc(vsession, vq->vring_inflight.inflight_packed->desc,
|
|
|
|
req_idx, &inflight_desc, &desc_table, &desc_table_size);
|
2019-08-05 10:43:38 +00:00
|
|
|
if (spdk_unlikely(rc != 0)) {
|
|
|
|
SPDK_ERRLOG("%s: Invalid descriptor at index %"PRIu16".\n", vdev->name, req_idx);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc_table != NULL) {
|
2020-08-18 14:30:31 +00:00
|
|
|
return blk_iovs_packed_desc_setup(vsession, vq, req_idx, desc_table, desc_table_size,
|
|
|
|
iovs, iovs_cnt, length);
|
2019-08-05 10:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
/*
|
|
|
|
* Maximum cnt reached?
|
|
|
|
* Should not happen if request is well formatted, otherwise this is a BUG.
|
|
|
|
*/
|
|
|
|
if (spdk_unlikely(cnt == *iovs_cnt)) {
|
|
|
|
SPDK_ERRLOG("%s: max IOVs in request reached (req_idx = %"PRIu16").\n",
|
|
|
|
vsession->name, req_idx);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-08-18 14:30:31 +00:00
|
|
|
if (spdk_unlikely(vhost_vring_inflight_desc_to_iov(vsession, iovs, &cnt, inflight_desc))) {
|
2019-08-05 10:43:38 +00:00
|
|
|
SPDK_ERRLOG("%s: invalid descriptor %" PRIu16" (req_idx = %"PRIu16").\n",
|
|
|
|
vsession->name, req_idx, cnt);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-08-18 14:30:31 +00:00
|
|
|
len += inflight_desc->len;
|
|
|
|
out_cnt += vhost_vring_inflight_desc_is_wr(inflight_desc);
|
2019-08-05 10:43:38 +00:00
|
|
|
|
2020-08-18 14:30:31 +00:00
|
|
|
/* Without F_NEXT means it's the last desc */
|
|
|
|
if ((inflight_desc->flags & VRING_DESC_F_NEXT) == 0) {
|
2019-08-05 10:43:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-08-18 14:30:31 +00:00
|
|
|
|
|
|
|
inflight_desc = &vq->vring_inflight.inflight_packed->desc[inflight_desc->next];
|
2019-08-05 10:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There must be least two descriptors.
|
|
|
|
* First contain request so it must be readable.
|
|
|
|
* Last descriptor contain buffer for response so it must be writable.
|
|
|
|
*/
|
|
|
|
if (spdk_unlikely(out_cnt == 0 || cnt < 2)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*length = len;
|
|
|
|
*iovs_cnt = cnt;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-24 14:48:22 +00:00
|
|
|
static void
|
|
|
|
blk_request_complete_cb(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_blk_task *task = cb_arg;
|
|
|
|
|
|
|
|
spdk_bdev_free_io(bdev_io);
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(success ? VIRTIO_BLK_S_OK : VIRTIO_BLK_S_IOERR, task);
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 09:45:18 +00:00
|
|
|
static void
|
|
|
|
blk_request_resubmit(void *arg)
|
|
|
|
{
|
2022-04-12 07:26:56 +00:00
|
|
|
struct spdk_vhost_blk_task *task = arg;
|
2018-09-13 09:45:18 +00:00
|
|
|
int rc = 0;
|
|
|
|
|
2021-09-17 08:23:35 +00:00
|
|
|
rc = virtio_blk_process_request(task->bdev_io_wait_vdev, task->bdev_io_wait_ch, task,
|
|
|
|
task->cb, task->cb_arg);
|
2018-09-13 09:45:18 +00:00
|
|
|
if (rc == 0) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "====== Task %p resubmitted ======\n", task);
|
2018-09-13 09:45:18 +00:00
|
|
|
} else {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "====== Task %p failed ======\n", task);
|
2018-09-13 09:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2022-04-21 13:15:30 +00:00
|
|
|
blk_request_queue_io(struct spdk_vhost_dev *vdev, struct spdk_io_channel *ch,
|
|
|
|
struct spdk_vhost_blk_task *task)
|
2018-09-13 09:45:18 +00:00
|
|
|
{
|
|
|
|
int rc;
|
2022-04-25 13:01:41 +00:00
|
|
|
struct spdk_bdev *bdev = vhost_blk_get_bdev(vdev);
|
2018-09-13 09:45:18 +00:00
|
|
|
|
|
|
|
task->bdev_io_wait.bdev = bdev;
|
|
|
|
task->bdev_io_wait.cb_fn = blk_request_resubmit;
|
|
|
|
task->bdev_io_wait.cb_arg = task;
|
2022-04-21 13:15:30 +00:00
|
|
|
task->bdev_io_wait_ch = ch;
|
|
|
|
task->bdev_io_wait_vdev = vdev;
|
2018-09-13 09:45:18 +00:00
|
|
|
|
2022-04-21 13:15:30 +00:00
|
|
|
rc = spdk_bdev_queue_io_wait(bdev, ch, &task->bdev_io_wait);
|
2018-09-13 09:45:18 +00:00
|
|
|
if (rc != 0) {
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_IOERR, task);
|
2018-09-13 09:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 08:23:35 +00:00
|
|
|
int
|
2021-09-17 08:47:53 +00:00
|
|
|
virtio_blk_process_request(struct spdk_vhost_dev *vdev, struct spdk_io_channel *ch,
|
2021-09-17 08:23:35 +00:00
|
|
|
struct spdk_vhost_blk_task *task, virtio_blk_request_cb cb, void *cb_arg)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
2022-04-20 11:42:59 +00:00
|
|
|
struct spdk_vhost_blk_dev *bvdev = to_blk_dev(vdev);
|
2022-04-09 10:50:08 +00:00
|
|
|
struct virtio_blk_outhdr req;
|
2018-03-02 01:42:56 +00:00
|
|
|
struct virtio_blk_discard_write_zeroes *desc;
|
2017-05-24 14:48:22 +00:00
|
|
|
struct iovec *iov;
|
|
|
|
uint32_t type;
|
2019-01-09 02:09:34 +00:00
|
|
|
uint64_t flush_bytes;
|
2021-01-04 13:22:19 +00:00
|
|
|
uint32_t payload_len;
|
2022-03-17 15:15:27 +00:00
|
|
|
uint16_t iovcnt;
|
2017-05-24 14:48:22 +00:00
|
|
|
int rc;
|
|
|
|
|
2021-09-17 08:23:35 +00:00
|
|
|
task->cb = cb;
|
|
|
|
task->cb_arg = cb_arg;
|
|
|
|
|
2017-05-24 14:48:22 +00:00
|
|
|
iov = &task->iovs[0];
|
2022-04-09 10:50:08 +00:00
|
|
|
if (spdk_unlikely(iov->iov_len != sizeof(req))) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk,
|
2022-04-21 12:55:41 +00:00
|
|
|
"First descriptor size is %zu but expected %zu (task = %p).\n",
|
|
|
|
iov->iov_len, sizeof(req), task);
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_UNSUPP, task);
|
2017-07-10 19:19:56 +00:00
|
|
|
return -1;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2022-04-09 10:50:08 +00:00
|
|
|
/* Some SeaBIOS versions don't align the virtio_blk_outhdr on an 8-byte boundary, which
|
|
|
|
* triggers ubsan errors. So copy this small 16-byte structure to the stack to workaround
|
|
|
|
* this problem.
|
|
|
|
*/
|
|
|
|
memcpy(&req, iov->iov_base, sizeof(req));
|
2017-05-24 14:48:22 +00:00
|
|
|
|
|
|
|
iov = &task->iovs[task->iovcnt - 1];
|
|
|
|
if (spdk_unlikely(iov->iov_len != 1)) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk,
|
2022-04-21 12:55:41 +00:00
|
|
|
"Last descriptor size is %zu but expected %d (task = %p).\n",
|
|
|
|
iov->iov_len, 1, task);
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_UNSUPP, task);
|
2017-07-10 19:19:56 +00:00
|
|
|
return -1;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:22:19 +00:00
|
|
|
payload_len = task->payload_size;
|
2017-05-24 14:48:22 +00:00
|
|
|
task->status = iov->iov_base;
|
2022-04-09 10:50:08 +00:00
|
|
|
payload_len -= sizeof(req) + sizeof(*task->status);
|
2022-03-17 15:15:27 +00:00
|
|
|
iovcnt = task->iovcnt - 2;
|
2017-05-24 14:48:22 +00:00
|
|
|
|
2022-04-09 10:50:08 +00:00
|
|
|
type = req.type;
|
2017-05-24 14:48:22 +00:00
|
|
|
#ifdef VIRTIO_BLK_T_BARRIER
|
2021-11-25 01:40:58 +00:00
|
|
|
/* Don't care about barrier for now (as QEMU's virtio-blk do). */
|
2017-05-24 14:48:22 +00:00
|
|
|
type &= ~VIRTIO_BLK_T_BARRIER;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case VIRTIO_BLK_T_IN:
|
|
|
|
case VIRTIO_BLK_T_OUT:
|
2018-12-05 11:48:05 +00:00
|
|
|
if (spdk_unlikely(payload_len == 0 || (payload_len & (512 - 1)) != 0)) {
|
2022-04-21 12:55:41 +00:00
|
|
|
SPDK_ERRLOG("%s - passed IO buffer is not multiple of 512b (task = %p).\n",
|
|
|
|
type ? "WRITE" : "READ", task);
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_UNSUPP, task);
|
2017-07-10 19:19:56 +00:00
|
|
|
return -1;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type == VIRTIO_BLK_T_IN) {
|
2018-01-08 20:35:53 +00:00
|
|
|
task->used_len = payload_len + sizeof(*task->status);
|
2022-04-20 11:42:59 +00:00
|
|
|
rc = spdk_bdev_readv(bvdev->bdev_desc, ch,
|
2022-04-09 10:50:08 +00:00
|
|
|
&task->iovs[1], iovcnt, req.sector * 512,
|
2018-01-08 20:35:53 +00:00
|
|
|
payload_len, blk_request_complete_cb, task);
|
2017-06-19 14:37:51 +00:00
|
|
|
} else if (!bvdev->readonly) {
|
2018-01-08 20:35:53 +00:00
|
|
|
task->used_len = sizeof(*task->status);
|
2022-04-20 11:42:59 +00:00
|
|
|
rc = spdk_bdev_writev(bvdev->bdev_desc, ch,
|
2022-04-09 10:50:08 +00:00
|
|
|
&task->iovs[1], iovcnt, req.sector * 512,
|
2018-01-08 20:35:53 +00:00
|
|
|
payload_len, blk_request_complete_cb, task);
|
2017-06-19 14:37:51 +00:00
|
|
|
} else {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "Device is in read-only mode!\n");
|
2017-06-19 14:37:51 +00:00
|
|
|
rc = -1;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rc) {
|
2018-09-13 09:45:18 +00:00
|
|
|
if (rc == -ENOMEM) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "No memory, start to queue io.\n");
|
2022-04-21 13:15:30 +00:00
|
|
|
blk_request_queue_io(vdev, ch, task);
|
2018-09-13 09:45:18 +00:00
|
|
|
} else {
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_IOERR, task);
|
2018-09-13 09:45:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-03-02 01:42:56 +00:00
|
|
|
case VIRTIO_BLK_T_DISCARD:
|
|
|
|
desc = task->iovs[1].iov_base;
|
|
|
|
if (payload_len != sizeof(*desc)) {
|
|
|
|
SPDK_NOTICELOG("Invalid discard payload size: %u\n", payload_len);
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_IOERR, task);
|
2018-03-02 01:42:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-12-14 05:59:25 +00:00
|
|
|
if (desc->flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) {
|
|
|
|
SPDK_ERRLOG("UNMAP flag is only used for WRITE ZEROES command\n");
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_UNSUPP, task);
|
2020-12-14 05:59:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-04-20 11:42:59 +00:00
|
|
|
rc = spdk_bdev_unmap(bvdev->bdev_desc, ch,
|
2018-03-02 01:42:56 +00:00
|
|
|
desc->sector * 512, desc->num_sectors * 512,
|
|
|
|
blk_request_complete_cb, task);
|
|
|
|
if (rc) {
|
|
|
|
if (rc == -ENOMEM) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "No memory, start to queue io.\n");
|
2022-04-21 13:15:30 +00:00
|
|
|
blk_request_queue_io(vdev, ch, task);
|
2018-03-02 01:42:56 +00:00
|
|
|
} else {
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_IOERR, task);
|
2018-03-02 01:42:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VIRTIO_BLK_T_WRITE_ZEROES:
|
|
|
|
desc = task->iovs[1].iov_base;
|
|
|
|
if (payload_len != sizeof(*desc)) {
|
|
|
|
SPDK_NOTICELOG("Invalid write zeroes payload size: %u\n", payload_len);
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_IOERR, task);
|
2018-03-02 01:42:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-12-14 05:59:25 +00:00
|
|
|
/* Unmap this range, SPDK doesn't support it, kernel will enable this flag by default
|
2021-11-25 01:40:58 +00:00
|
|
|
* without checking unmap feature is negotiated or not, the flag isn't mandatory, so
|
2020-12-14 05:59:25 +00:00
|
|
|
* just print a warning.
|
|
|
|
*/
|
2018-03-02 01:42:56 +00:00
|
|
|
if (desc->flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) {
|
2020-12-14 05:59:25 +00:00
|
|
|
SPDK_WARNLOG("Ignore the unmap flag for WRITE ZEROES from %"PRIx64", len %"PRIx64"\n",
|
|
|
|
(uint64_t)desc->sector * 512, (uint64_t)desc->num_sectors * 512);
|
2018-03-02 01:42:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 11:42:59 +00:00
|
|
|
rc = spdk_bdev_write_zeroes(bvdev->bdev_desc, ch,
|
2018-03-02 01:42:56 +00:00
|
|
|
desc->sector * 512, desc->num_sectors * 512,
|
|
|
|
blk_request_complete_cb, task);
|
|
|
|
if (rc) {
|
|
|
|
if (rc == -ENOMEM) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "No memory, start to queue io.\n");
|
2022-04-21 13:15:30 +00:00
|
|
|
blk_request_queue_io(vdev, ch, task);
|
2018-03-02 01:42:56 +00:00
|
|
|
} else {
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_IOERR, task);
|
2018-03-02 01:42:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2019-01-09 02:09:34 +00:00
|
|
|
case VIRTIO_BLK_T_FLUSH:
|
|
|
|
flush_bytes = spdk_bdev_get_num_blocks(bvdev->bdev) * spdk_bdev_get_block_size(bvdev->bdev);
|
2022-04-09 10:50:08 +00:00
|
|
|
if (req.sector != 0) {
|
2019-01-09 02:09:34 +00:00
|
|
|
SPDK_NOTICELOG("sector must be zero for flush command\n");
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_IOERR, task);
|
2019-01-09 02:09:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2022-04-20 11:42:59 +00:00
|
|
|
rc = spdk_bdev_flush(bvdev->bdev_desc, ch,
|
2019-01-09 02:09:34 +00:00
|
|
|
0, flush_bytes,
|
|
|
|
blk_request_complete_cb, task);
|
|
|
|
if (rc) {
|
|
|
|
if (rc == -ENOMEM) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "No memory, start to queue io.\n");
|
2022-04-21 13:15:30 +00:00
|
|
|
blk_request_queue_io(vdev, ch, task);
|
2019-01-09 02:09:34 +00:00
|
|
|
} else {
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_IOERR, task);
|
2019-01-09 02:09:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-05-24 14:48:22 +00:00
|
|
|
case VIRTIO_BLK_T_GET_ID:
|
2022-03-17 15:15:27 +00:00
|
|
|
if (!iovcnt || !payload_len) {
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_UNSUPP, task);
|
2017-07-10 19:19:56 +00:00
|
|
|
return -1;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
2018-01-08 20:35:53 +00:00
|
|
|
task->used_len = spdk_min((size_t)VIRTIO_BLK_ID_BYTES, task->iovs[1].iov_len);
|
2021-05-21 16:18:58 +00:00
|
|
|
spdk_strcpy_pad(task->iovs[1].iov_base, spdk_bdev_get_name(bvdev->bdev),
|
2018-01-08 20:35:53 +00:00
|
|
|
task->used_len, ' ');
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_OK, task);
|
2017-05-24 14:48:22 +00:00
|
|
|
break;
|
|
|
|
default:
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "Not supported request type '%"PRIu32"'.\n", type);
|
2022-04-05 12:43:23 +00:00
|
|
|
blk_request_finish(VIRTIO_BLK_S_UNSUPP, task);
|
2017-07-10 19:19:56 +00:00
|
|
|
return -1;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 19:19:56 +00:00
|
|
|
return 0;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 10:22:26 +00:00
|
|
|
static void
|
|
|
|
process_blk_task(struct spdk_vhost_virtqueue *vq, uint16_t req_idx)
|
|
|
|
{
|
2022-04-12 07:26:56 +00:00
|
|
|
struct spdk_vhost_user_blk_task *task;
|
|
|
|
struct spdk_vhost_blk_task *blk_task;
|
2021-01-04 13:22:19 +00:00
|
|
|
int rc;
|
2019-08-05 10:43:38 +00:00
|
|
|
|
2021-01-04 13:42:24 +00:00
|
|
|
assert(vq->packed.packed_ring == false);
|
|
|
|
|
2022-04-12 07:26:56 +00:00
|
|
|
task = &((struct spdk_vhost_user_blk_task *)vq->tasks)[req_idx];
|
|
|
|
blk_task = &task->blk_task;
|
2021-01-04 13:42:24 +00:00
|
|
|
if (spdk_unlikely(task->used)) {
|
|
|
|
SPDK_ERRLOG("%s: request with idx '%"PRIu16"' is already pending.\n",
|
|
|
|
task->bvsession->vsession.name, req_idx);
|
2022-04-12 07:26:56 +00:00
|
|
|
blk_task->used_len = 0;
|
2021-01-04 13:42:24 +00:00
|
|
|
blk_task_enqueue(task);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
task->bvsession->vsession.task_cnt++;
|
|
|
|
|
|
|
|
blk_task_init(task);
|
|
|
|
|
2022-04-12 07:26:56 +00:00
|
|
|
rc = blk_iovs_split_queue_setup(task->bvsession, vq, task->req_idx,
|
|
|
|
blk_task->iovs, &blk_task->iovcnt, &blk_task->payload_size);
|
2021-01-04 13:42:24 +00:00
|
|
|
|
|
|
|
if (rc) {
|
|
|
|
SPDK_DEBUGLOG(vhost_blk, "Invalid request (req_idx = %"PRIu16").\n", task->req_idx);
|
|
|
|
/* Only READ and WRITE are supported for now. */
|
2021-09-17 08:23:35 +00:00
|
|
|
vhost_user_blk_request_finish(VIRTIO_BLK_S_UNSUPP, blk_task, NULL);
|
2021-01-04 13:42:24 +00:00
|
|
|
return;
|
2019-08-05 10:43:38 +00:00
|
|
|
}
|
2020-03-18 10:22:26 +00:00
|
|
|
|
2022-04-20 11:42:59 +00:00
|
|
|
if (vhost_user_process_blk_request(task) == 0) {
|
2021-01-04 13:42:24 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "====== Task %p req_idx %d submitted ======\n", task,
|
|
|
|
req_idx);
|
|
|
|
} else {
|
|
|
|
SPDK_ERRLOG("====== Task %p req_idx %d failed ======\n", task, req_idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_packed_blk_task(struct spdk_vhost_virtqueue *vq, uint16_t req_idx)
|
|
|
|
{
|
2022-04-12 07:26:56 +00:00
|
|
|
struct spdk_vhost_user_blk_task *task;
|
|
|
|
struct spdk_vhost_blk_task *blk_task;
|
2021-01-04 13:42:24 +00:00
|
|
|
uint16_t task_idx = req_idx, num_descs;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert(vq->packed.packed_ring);
|
|
|
|
|
|
|
|
/* Packed ring used the buffer_id as the task_idx to get task struct.
|
|
|
|
* In kernel driver, it uses the vq->free_head to set the buffer_id so the value
|
|
|
|
* must be in the range of 0 ~ vring.size. The free_head value must be unique
|
|
|
|
* in the outstanding requests.
|
|
|
|
* We can't use the req_idx as the task_idx because the desc can be reused in
|
|
|
|
* the next phase even when it's not completed in the previous phase. For example,
|
|
|
|
* At phase 0, last_used_idx was 2 and desc0 was not completed.Then after moving
|
|
|
|
* phase 1, last_avail_idx is updated to 1. In this case, req_idx can not be used
|
|
|
|
* as task_idx because we will know task[0]->used is true at phase 1.
|
|
|
|
* The split queue is quite different, the desc would insert into the free list when
|
|
|
|
* device completes the request, the driver gets the desc from the free list which
|
|
|
|
* ensures the req_idx is unique in the outstanding requests.
|
|
|
|
*/
|
|
|
|
task_idx = vhost_vring_packed_desc_get_buffer_id(vq, req_idx, &num_descs);
|
|
|
|
|
2022-04-12 07:26:56 +00:00
|
|
|
task = &((struct spdk_vhost_user_blk_task *)vq->tasks)[task_idx];
|
|
|
|
blk_task = &task->blk_task;
|
2020-03-18 10:22:26 +00:00
|
|
|
if (spdk_unlikely(task->used)) {
|
|
|
|
SPDK_ERRLOG("%s: request with idx '%"PRIu16"' is already pending.\n",
|
2019-08-05 10:43:38 +00:00
|
|
|
task->bvsession->vsession.name, task_idx);
|
2022-04-12 07:26:56 +00:00
|
|
|
blk_task->used_len = 0;
|
2019-08-05 10:43:38 +00:00
|
|
|
blk_task_enqueue(task);
|
2020-03-18 10:22:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:42:24 +00:00
|
|
|
task->req_idx = req_idx;
|
|
|
|
task->num_descs = num_descs;
|
|
|
|
task->buffer_id = task_idx;
|
2020-08-17 16:31:32 +00:00
|
|
|
|
2021-01-04 13:42:24 +00:00
|
|
|
rte_vhost_set_inflight_desc_packed(task->bvsession->vsession.vid, vq->vring_idx,
|
|
|
|
req_idx, (req_idx + num_descs - 1) % vq->vring.size,
|
|
|
|
&task->inflight_head);
|
2019-08-05 10:43:38 +00:00
|
|
|
|
2020-03-18 10:22:26 +00:00
|
|
|
task->bvsession->vsession.task_cnt++;
|
|
|
|
|
|
|
|
blk_task_init(task);
|
|
|
|
|
2022-04-12 07:26:56 +00:00
|
|
|
rc = blk_iovs_packed_queue_setup(task->bvsession, vq, task->req_idx, blk_task->iovs,
|
|
|
|
&blk_task->iovcnt,
|
|
|
|
&blk_task->payload_size);
|
2021-01-04 13:22:19 +00:00
|
|
|
if (rc) {
|
|
|
|
SPDK_DEBUGLOG(vhost_blk, "Invalid request (req_idx = %"PRIu16").\n", task->req_idx);
|
|
|
|
/* Only READ and WRITE are supported for now. */
|
2021-09-17 08:23:35 +00:00
|
|
|
vhost_user_blk_request_finish(VIRTIO_BLK_S_UNSUPP, blk_task, NULL);
|
2021-01-04 13:22:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-20 11:42:59 +00:00
|
|
|
if (vhost_user_process_blk_request(task) == 0) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "====== Task %p req_idx %d submitted ======\n", task,
|
2019-08-05 10:43:38 +00:00
|
|
|
task_idx);
|
2020-03-18 10:22:26 +00:00
|
|
|
} else {
|
2019-08-05 10:43:38 +00:00
|
|
|
SPDK_ERRLOG("====== Task %p req_idx %d failed ======\n", task, task_idx);
|
2020-03-18 10:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 14:30:31 +00:00
|
|
|
static void
|
|
|
|
process_packed_inflight_blk_task(struct spdk_vhost_virtqueue *vq,
|
|
|
|
uint16_t req_idx)
|
|
|
|
{
|
|
|
|
spdk_vhost_inflight_desc *desc_array = vq->vring_inflight.inflight_packed->desc;
|
|
|
|
spdk_vhost_inflight_desc *desc = &desc_array[req_idx];
|
2022-04-12 07:26:56 +00:00
|
|
|
struct spdk_vhost_user_blk_task *task;
|
|
|
|
struct spdk_vhost_blk_task *blk_task;
|
2020-08-18 14:30:31 +00:00
|
|
|
uint16_t task_idx, num_descs;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
task_idx = desc_array[desc->last].id;
|
|
|
|
num_descs = desc->num;
|
|
|
|
/* In packed ring reconnection, we use the last_used_idx as the
|
|
|
|
* initial value. So when we process the inflight descs we still
|
|
|
|
* need to update the available ring index.
|
|
|
|
*/
|
|
|
|
vq->last_avail_idx += num_descs;
|
|
|
|
if (vq->last_avail_idx >= vq->vring.size) {
|
|
|
|
vq->last_avail_idx -= vq->vring.size;
|
|
|
|
vq->packed.avail_phase = !vq->packed.avail_phase;
|
|
|
|
}
|
|
|
|
|
2022-04-12 07:26:56 +00:00
|
|
|
task = &((struct spdk_vhost_user_blk_task *)vq->tasks)[task_idx];
|
|
|
|
blk_task = &task->blk_task;
|
2020-08-18 14:30:31 +00:00
|
|
|
if (spdk_unlikely(task->used)) {
|
|
|
|
SPDK_ERRLOG("%s: request with idx '%"PRIu16"' is already pending.\n",
|
|
|
|
task->bvsession->vsession.name, task_idx);
|
2022-04-12 07:26:56 +00:00
|
|
|
blk_task->used_len = 0;
|
2020-08-18 14:30:31 +00:00
|
|
|
blk_task_enqueue(task);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
task->req_idx = req_idx;
|
|
|
|
task->num_descs = num_descs;
|
|
|
|
task->buffer_id = task_idx;
|
|
|
|
/* It's for cleaning inflight entries */
|
|
|
|
task->inflight_head = req_idx;
|
|
|
|
|
|
|
|
task->bvsession->vsession.task_cnt++;
|
|
|
|
|
|
|
|
blk_task_init(task);
|
|
|
|
|
2022-04-12 07:26:56 +00:00
|
|
|
rc = blk_iovs_inflight_queue_setup(task->bvsession, vq, task->req_idx, blk_task->iovs,
|
|
|
|
&blk_task->iovcnt,
|
|
|
|
&blk_task->payload_size);
|
2020-08-18 14:30:31 +00:00
|
|
|
if (rc) {
|
|
|
|
SPDK_DEBUGLOG(vhost_blk, "Invalid request (req_idx = %"PRIu16").\n", task->req_idx);
|
|
|
|
/* Only READ and WRITE are supported for now. */
|
2021-09-17 08:23:35 +00:00
|
|
|
vhost_user_blk_request_finish(VIRTIO_BLK_S_UNSUPP, blk_task, NULL);
|
2020-08-18 14:30:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-20 11:42:59 +00:00
|
|
|
if (vhost_user_process_blk_request(task) == 0) {
|
2020-08-18 14:30:31 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "====== Task %p req_idx %d submitted ======\n", task,
|
|
|
|
task_idx);
|
|
|
|
} else {
|
|
|
|
SPDK_ERRLOG("====== Task %p req_idx %d failed ======\n", task, task_idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 06:27:11 +00:00
|
|
|
static int
|
2019-10-14 16:43:37 +00:00
|
|
|
submit_inflight_desc(struct spdk_vhost_blk_session *bvsession,
|
|
|
|
struct spdk_vhost_virtqueue *vq)
|
|
|
|
{
|
2021-11-24 08:06:24 +00:00
|
|
|
struct spdk_vhost_session *vsession;
|
|
|
|
spdk_vhost_resubmit_info *resubmit;
|
2019-10-14 16:43:37 +00:00
|
|
|
spdk_vhost_resubmit_desc *resubmit_list;
|
|
|
|
uint16_t req_idx;
|
2022-06-29 06:27:11 +00:00
|
|
|
int i, resubmit_cnt;
|
2019-10-14 16:43:37 +00:00
|
|
|
|
2021-11-24 08:06:24 +00:00
|
|
|
resubmit = vq->vring_inflight.resubmit_inflight;
|
|
|
|
if (spdk_likely(resubmit == NULL || resubmit->resubmit_list == NULL ||
|
|
|
|
resubmit->resubmit_num == 0)) {
|
2022-06-29 06:27:11 +00:00
|
|
|
return 0;
|
2019-10-14 16:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resubmit_list = resubmit->resubmit_list;
|
2021-11-24 08:06:24 +00:00
|
|
|
vsession = &bvsession->vsession;
|
|
|
|
|
|
|
|
for (i = resubmit->resubmit_num - 1; i >= 0; --i) {
|
|
|
|
req_idx = resubmit_list[i].index;
|
|
|
|
SPDK_DEBUGLOG(vhost_blk, "====== Start processing resubmit request idx %"PRIu16"======\n",
|
2019-10-14 16:43:37 +00:00
|
|
|
req_idx);
|
|
|
|
|
|
|
|
if (spdk_unlikely(req_idx >= vq->vring.size)) {
|
|
|
|
SPDK_ERRLOG("%s: request idx '%"PRIu16"' exceeds virtqueue size (%"PRIu16").\n",
|
2020-03-19 10:56:26 +00:00
|
|
|
vsession->name, req_idx, vq->vring.size);
|
2019-10-14 16:43:37 +00:00
|
|
|
vhost_vq_used_ring_enqueue(vsession, vq, req_idx, 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-18 14:30:31 +00:00
|
|
|
if (vq->packed.packed_ring) {
|
|
|
|
process_packed_inflight_blk_task(vq, req_idx);
|
|
|
|
} else {
|
|
|
|
process_blk_task(vq, req_idx);
|
|
|
|
}
|
2019-10-14 16:43:37 +00:00
|
|
|
}
|
2022-06-29 06:27:11 +00:00
|
|
|
resubmit_cnt = resubmit->resubmit_num;
|
2021-11-24 08:06:24 +00:00
|
|
|
resubmit->resubmit_num = 0;
|
2022-06-29 06:27:11 +00:00
|
|
|
return resubmit_cnt;
|
2019-10-14 16:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 10:40:11 +00:00
|
|
|
static int
|
2018-12-16 00:15:06 +00:00
|
|
|
process_vq(struct spdk_vhost_blk_session *bvsession, struct spdk_vhost_virtqueue *vq)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
2018-12-17 15:33:30 +00:00
|
|
|
struct spdk_vhost_session *vsession = &bvsession->vsession;
|
2019-08-05 10:43:38 +00:00
|
|
|
uint16_t reqs[SPDK_VHOST_VQ_MAX_SUBMISSIONS];
|
2017-05-24 14:48:22 +00:00
|
|
|
uint16_t reqs_cnt, i;
|
2022-06-29 06:27:11 +00:00
|
|
|
int resubmit_cnt = 0;
|
2019-10-14 16:43:37 +00:00
|
|
|
|
2022-06-29 06:27:11 +00:00
|
|
|
resubmit_cnt = submit_inflight_desc(bvsession, vq);
|
2017-05-24 14:48:22 +00:00
|
|
|
|
2019-07-20 21:06:19 +00:00
|
|
|
reqs_cnt = vhost_vq_avail_ring_get(vq, reqs, SPDK_COUNTOF(reqs));
|
2017-05-24 14:48:22 +00:00
|
|
|
if (!reqs_cnt) {
|
2022-06-29 06:27:11 +00:00
|
|
|
return resubmit_cnt;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < reqs_cnt; i++) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "====== Starting processing request idx %"PRIu16"======\n",
|
2017-07-10 19:19:56 +00:00
|
|
|
reqs[i]);
|
2017-10-10 20:19:40 +00:00
|
|
|
|
|
|
|
if (spdk_unlikely(reqs[i] >= vq->vring.size)) {
|
|
|
|
SPDK_ERRLOG("%s: request idx '%"PRIu16"' exceeds virtqueue size (%"PRIu16").\n",
|
2019-07-20 14:59:04 +00:00
|
|
|
vsession->name, reqs[i], vq->vring.size);
|
2019-07-20 21:06:19 +00:00
|
|
|
vhost_vq_used_ring_enqueue(vsession, vq, reqs[i], 0);
|
2017-10-10 20:19:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:43:38 +00:00
|
|
|
rte_vhost_set_inflight_desc_split(vsession->vid, vq->vring_idx, reqs[i]);
|
2017-10-10 20:19:40 +00:00
|
|
|
|
2020-03-18 10:22:26 +00:00
|
|
|
process_blk_task(vq, reqs[i]);
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
2022-05-11 10:40:11 +00:00
|
|
|
|
|
|
|
return reqs_cnt;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 10:40:11 +00:00
|
|
|
static int
|
2019-08-05 10:43:38 +00:00
|
|
|
process_packed_vq(struct spdk_vhost_blk_session *bvsession, struct spdk_vhost_virtqueue *vq)
|
|
|
|
{
|
|
|
|
uint16_t i = 0;
|
2022-05-11 10:40:11 +00:00
|
|
|
uint16_t count = 0;
|
2022-06-29 06:27:11 +00:00
|
|
|
int resubmit_cnt = 0;
|
2019-08-05 10:43:38 +00:00
|
|
|
|
2022-06-29 06:27:11 +00:00
|
|
|
resubmit_cnt = submit_inflight_desc(bvsession, vq);
|
2020-08-18 14:30:31 +00:00
|
|
|
|
2019-08-05 10:43:38 +00:00
|
|
|
while (i++ < SPDK_VHOST_VQ_MAX_SUBMISSIONS &&
|
|
|
|
vhost_vq_packed_ring_is_avail(vq)) {
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "====== Starting processing request idx %"PRIu16"======\n",
|
2019-08-05 10:43:38 +00:00
|
|
|
vq->last_avail_idx);
|
2022-05-11 10:40:11 +00:00
|
|
|
count++;
|
2021-01-04 13:42:24 +00:00
|
|
|
process_packed_blk_task(vq, vq->last_avail_idx);
|
2019-08-05 10:43:38 +00:00
|
|
|
}
|
2022-05-11 10:40:11 +00:00
|
|
|
|
2022-06-29 06:27:11 +00:00
|
|
|
return count > 0 ? count : resubmit_cnt;
|
2019-08-05 10:43:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 05:30:44 +00:00
|
|
|
static int
|
|
|
|
_vdev_vq_worker(struct spdk_vhost_virtqueue *vq)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_session *vsession = vq->vsession;
|
|
|
|
struct spdk_vhost_blk_session *bvsession = to_blk_session(vsession);
|
|
|
|
bool packed_ring;
|
2022-05-11 10:40:11 +00:00
|
|
|
int rc = 0;
|
2020-10-24 05:30:44 +00:00
|
|
|
|
|
|
|
packed_ring = vq->packed.packed_ring;
|
|
|
|
if (packed_ring) {
|
2022-05-11 10:40:11 +00:00
|
|
|
rc = process_packed_vq(bvsession, vq);
|
2020-10-24 05:30:44 +00:00
|
|
|
} else {
|
2022-05-11 10:40:11 +00:00
|
|
|
rc = process_vq(bvsession, vq);
|
2020-10-24 05:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vhost_session_vq_used_signal(vq);
|
|
|
|
|
2022-05-11 10:40:11 +00:00
|
|
|
return rc;
|
2020-10-24 05:30:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-24 04:41:30 +00:00
|
|
|
static int
|
|
|
|
vdev_vq_worker(void *arg)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_virtqueue *vq = arg;
|
|
|
|
|
|
|
|
return _vdev_vq_worker(vq);
|
|
|
|
}
|
|
|
|
|
2018-03-13 00:16:47 +00:00
|
|
|
static int
|
2017-08-08 16:52:33 +00:00
|
|
|
vdev_worker(void *arg)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
2018-12-16 00:15:06 +00:00
|
|
|
struct spdk_vhost_blk_session *bvsession = arg;
|
|
|
|
struct spdk_vhost_session *vsession = &bvsession->vsession;
|
2017-08-08 16:52:33 +00:00
|
|
|
uint16_t q_idx;
|
2022-05-11 10:40:11 +00:00
|
|
|
int rc = 0;
|
2017-08-08 16:52:33 +00:00
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
for (q_idx = 0; q_idx < vsession->max_queues; q_idx++) {
|
2022-05-11 10:40:11 +00:00
|
|
|
rc += _vdev_vq_worker(&vsession->virtqueue[q_idx]);
|
2017-08-08 16:52:33 +00:00
|
|
|
}
|
2017-09-11 17:45:56 +00:00
|
|
|
|
2022-05-11 10:40:11 +00:00
|
|
|
return rc > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
|
2017-08-08 16:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-12-16 00:15:06 +00:00
|
|
|
no_bdev_process_vq(struct spdk_vhost_blk_session *bvsession, struct spdk_vhost_virtqueue *vq)
|
2017-08-08 16:52:33 +00:00
|
|
|
{
|
2018-12-16 00:15:06 +00:00
|
|
|
struct spdk_vhost_session *vsession = &bvsession->vsession;
|
2017-07-28 16:03:49 +00:00
|
|
|
struct iovec iovs[SPDK_VHOST_IOVS_MAX];
|
2017-05-24 14:48:22 +00:00
|
|
|
uint32_t length;
|
|
|
|
uint16_t iovcnt, req_idx;
|
|
|
|
|
2019-07-20 21:06:19 +00:00
|
|
|
if (vhost_vq_avail_ring_get(vq, &req_idx, 1) != 1) {
|
2017-05-24 14:48:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
iovcnt = SPDK_COUNTOF(iovs);
|
2019-08-05 10:43:38 +00:00
|
|
|
if (blk_iovs_split_queue_setup(bvsession, vq, req_idx, iovs, &iovcnt, &length) == 0) {
|
2017-05-24 14:48:22 +00:00
|
|
|
*(volatile uint8_t *)iovs[iovcnt - 1].iov_base = VIRTIO_BLK_S_IOERR;
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk_data, "Aborting request %" PRIu16"\n", req_idx);
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2019-07-20 21:06:19 +00:00
|
|
|
vhost_vq_used_ring_enqueue(vsession, vq, req_idx, 0);
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 10:43:38 +00:00
|
|
|
static void
|
|
|
|
no_bdev_process_packed_vq(struct spdk_vhost_blk_session *bvsession, struct spdk_vhost_virtqueue *vq)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_session *vsession = &bvsession->vsession;
|
2022-04-12 07:26:56 +00:00
|
|
|
struct spdk_vhost_user_blk_task *task;
|
|
|
|
struct spdk_vhost_blk_task *blk_task;
|
2019-08-05 10:43:38 +00:00
|
|
|
uint32_t length;
|
|
|
|
uint16_t req_idx = vq->last_avail_idx;
|
|
|
|
uint16_t task_idx, num_descs;
|
|
|
|
|
|
|
|
if (!vhost_vq_packed_ring_is_avail(vq)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
task_idx = vhost_vring_packed_desc_get_buffer_id(vq, req_idx, &num_descs);
|
2022-04-12 07:26:56 +00:00
|
|
|
task = &((struct spdk_vhost_user_blk_task *)vq->tasks)[task_idx];
|
|
|
|
blk_task = &task->blk_task;
|
2019-08-05 10:43:38 +00:00
|
|
|
if (spdk_unlikely(task->used)) {
|
|
|
|
SPDK_ERRLOG("%s: request with idx '%"PRIu16"' is already pending.\n",
|
|
|
|
vsession->name, req_idx);
|
|
|
|
vhost_vq_packed_ring_enqueue(vsession, vq, num_descs,
|
2022-04-12 07:26:56 +00:00
|
|
|
task->buffer_id, blk_task->used_len,
|
2020-08-17 16:31:32 +00:00
|
|
|
task->inflight_head);
|
2019-08-05 10:43:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
task->req_idx = req_idx;
|
|
|
|
task->num_descs = num_descs;
|
|
|
|
task->buffer_id = task_idx;
|
|
|
|
blk_task_init(task);
|
|
|
|
|
2022-04-12 07:26:56 +00:00
|
|
|
if (blk_iovs_packed_queue_setup(bvsession, vq, task->req_idx, blk_task->iovs, &blk_task->iovcnt,
|
2019-08-05 10:43:38 +00:00
|
|
|
&length)) {
|
2022-04-12 07:26:56 +00:00
|
|
|
*(volatile uint8_t *)(blk_task->iovs[blk_task->iovcnt - 1].iov_base) = VIRTIO_BLK_S_IOERR;
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk_data, "Aborting request %" PRIu16"\n", req_idx);
|
2019-08-05 10:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
task->used = false;
|
|
|
|
vhost_vq_packed_ring_enqueue(vsession, vq, num_descs,
|
2022-04-12 07:26:56 +00:00
|
|
|
task->buffer_id, blk_task->used_len,
|
2020-08-17 16:31:32 +00:00
|
|
|
task->inflight_head);
|
2019-08-05 10:43:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 00:16:47 +00:00
|
|
|
static int
|
2020-10-24 05:30:44 +00:00
|
|
|
_no_bdev_vdev_vq_worker(struct spdk_vhost_virtqueue *vq)
|
2017-08-08 16:52:33 +00:00
|
|
|
{
|
2020-10-24 05:30:44 +00:00
|
|
|
struct spdk_vhost_session *vsession = vq->vsession;
|
|
|
|
struct spdk_vhost_blk_session *bvsession = to_blk_session(vsession);
|
2019-08-05 10:43:38 +00:00
|
|
|
bool packed_ring;
|
2017-08-08 16:52:33 +00:00
|
|
|
|
2020-10-24 05:30:44 +00:00
|
|
|
packed_ring = vq->packed.packed_ring;
|
|
|
|
if (packed_ring) {
|
|
|
|
no_bdev_process_packed_vq(bvsession, vq);
|
|
|
|
} else {
|
|
|
|
no_bdev_process_vq(bvsession, vq);
|
2017-08-08 16:52:33 +00:00
|
|
|
}
|
2017-09-11 17:45:56 +00:00
|
|
|
|
2020-10-24 05:30:44 +00:00
|
|
|
vhost_session_vq_used_signal(vq);
|
2018-03-13 00:16:47 +00:00
|
|
|
|
2019-01-06 23:15:16 +00:00
|
|
|
if (vsession->task_cnt == 0 && bvsession->io_channel) {
|
2022-01-14 12:33:54 +00:00
|
|
|
vhost_blk_put_io_channel(bvsession->io_channel);
|
2019-01-06 23:15:16 +00:00
|
|
|
bvsession->io_channel = NULL;
|
2018-08-14 10:49:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 09:51:27 +00:00
|
|
|
return SPDK_POLLER_BUSY;
|
2017-08-08 16:52:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 04:41:30 +00:00
|
|
|
static int
|
|
|
|
no_bdev_vdev_vq_worker(void *arg)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_virtqueue *vq = arg;
|
|
|
|
|
|
|
|
return _no_bdev_vdev_vq_worker(vq);
|
|
|
|
}
|
|
|
|
|
2020-10-24 05:30:44 +00:00
|
|
|
static int
|
|
|
|
no_bdev_vdev_worker(void *arg)
|
2018-12-14 12:51:19 +00:00
|
|
|
{
|
2020-10-24 05:30:44 +00:00
|
|
|
struct spdk_vhost_blk_session *bvsession = arg;
|
|
|
|
struct spdk_vhost_session *vsession = &bvsession->vsession;
|
|
|
|
uint16_t q_idx;
|
|
|
|
|
|
|
|
for (q_idx = 0; q_idx < vsession->max_queues; q_idx++) {
|
|
|
|
_no_bdev_vdev_vq_worker(&vsession->virtqueue[q_idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SPDK_POLLER_BUSY;
|
2018-12-14 12:51:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 04:41:30 +00:00
|
|
|
static void
|
|
|
|
vhost_blk_session_unregister_interrupts(struct spdk_vhost_blk_session *bvsession)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_session *vsession = &bvsession->vsession;
|
|
|
|
struct spdk_vhost_virtqueue *vq;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
SPDK_DEBUGLOG(vhost_blk, "unregister virtqueues interrupt\n");
|
|
|
|
for (i = 0; i < vsession->max_queues; i++) {
|
|
|
|
vq = &vsession->virtqueue[i];
|
|
|
|
if (vq->intr == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SPDK_DEBUGLOG(vhost_blk, "unregister vq[%d]'s kickfd is %d\n",
|
|
|
|
i, vq->vring.kickfd);
|
|
|
|
spdk_interrupt_unregister(&vq->intr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vhost_blk_session_register_interrupts(struct spdk_vhost_blk_session *bvsession,
|
2021-08-31 14:25:54 +00:00
|
|
|
spdk_interrupt_fn fn, const char *name)
|
2020-10-24 04:41:30 +00:00
|
|
|
{
|
|
|
|
struct spdk_vhost_session *vsession = &bvsession->vsession;
|
|
|
|
struct spdk_vhost_virtqueue *vq = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
SPDK_DEBUGLOG(vhost_blk, "Register virtqueues interrupt\n");
|
|
|
|
for (i = 0; i < vsession->max_queues; i++) {
|
|
|
|
vq = &vsession->virtqueue[i];
|
|
|
|
SPDK_DEBUGLOG(vhost_blk, "Register vq[%d]'s kickfd is %d\n",
|
|
|
|
i, vq->vring.kickfd);
|
|
|
|
|
2021-08-31 14:25:54 +00:00
|
|
|
vq->intr = spdk_interrupt_register(vq->vring.kickfd, fn, vq, name);
|
2020-10-24 04:41:30 +00:00
|
|
|
if (vq->intr == NULL) {
|
|
|
|
SPDK_ERRLOG("Fail to register req notifier handler.\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
vhost_blk_session_unregister_interrupts(bvsession);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-12-31 13:07:31 +00:00
|
|
|
static void
|
|
|
|
vhost_blk_poller_set_interrupt_mode(struct spdk_poller *poller, void *cb_arg, bool interrupt_mode)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_blk_session *bvsession = cb_arg;
|
|
|
|
|
2022-01-11 11:28:51 +00:00
|
|
|
vhost_user_session_set_interrupt_mode(&bvsession->vsession, interrupt_mode);
|
2020-12-31 13:07:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 12:46:23 +00:00
|
|
|
static void
|
|
|
|
bdev_event_cpl_cb(struct spdk_vhost_dev *vdev, void *ctx)
|
|
|
|
{
|
|
|
|
enum spdk_bdev_event_type type = (enum spdk_bdev_event_type)(uintptr_t)ctx;
|
|
|
|
struct spdk_vhost_blk_dev *bvdev;
|
|
|
|
|
|
|
|
if (type == SPDK_BDEV_EVENT_REMOVE) {
|
|
|
|
/* All sessions have been notified, time to close the bdev */
|
|
|
|
bvdev = to_blk_dev(vdev);
|
|
|
|
assert(bvdev != NULL);
|
|
|
|
spdk_put_io_channel(bvdev->dummy_io_channel);
|
|
|
|
spdk_bdev_close(bvdev->bdev_desc);
|
|
|
|
bvdev->bdev_desc = NULL;
|
|
|
|
bvdev->bdev = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:05:19 +00:00
|
|
|
static int
|
|
|
|
vhost_session_bdev_resize_cb(struct spdk_vhost_dev *vdev,
|
|
|
|
struct spdk_vhost_session *vsession,
|
|
|
|
void *ctx)
|
|
|
|
{
|
|
|
|
#if RTE_VERSION >= RTE_VERSION_NUM(20, 02, 0, 0)
|
|
|
|
SPDK_NOTICELOG("bdev send slave msg to vid(%d)\n", vsession->vid);
|
|
|
|
rte_vhost_slave_config_change(vsession->vid, false);
|
|
|
|
#else
|
|
|
|
SPDK_NOTICELOG("bdev does not support resize until DPDK submodule version >= 20.02\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-03-30 12:46:23 +00:00
|
|
|
vhost_user_blk_resize_cb(struct spdk_vhost_dev *vdev, bdev_event_cb_complete cb, void *cb_arg)
|
2020-03-26 14:05:19 +00:00
|
|
|
{
|
2022-03-30 12:46:23 +00:00
|
|
|
vhost_user_dev_foreach_session(vdev, vhost_session_bdev_resize_cb,
|
|
|
|
cb, cb_arg);
|
2020-03-26 14:05:19 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 10:00:26 +00:00
|
|
|
static int
|
2022-03-30 12:46:23 +00:00
|
|
|
vhost_user_session_bdev_remove_cb(struct spdk_vhost_dev *vdev,
|
|
|
|
struct spdk_vhost_session *vsession,
|
|
|
|
void *ctx)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
2018-12-17 12:45:36 +00:00
|
|
|
struct spdk_vhost_blk_session *bvsession;
|
2020-10-24 04:41:30 +00:00
|
|
|
int rc;
|
2017-07-10 19:25:22 +00:00
|
|
|
|
2020-12-31 13:07:31 +00:00
|
|
|
bvsession = to_blk_session(vsession);
|
2018-12-17 12:45:36 +00:00
|
|
|
if (bvsession->requestq_poller) {
|
2018-12-14 12:51:19 +00:00
|
|
|
spdk_poller_unregister(&bvsession->requestq_poller);
|
2020-12-31 13:07:31 +00:00
|
|
|
if (vsession->virtqueue[0].intr) {
|
|
|
|
vhost_blk_session_unregister_interrupts(bvsession);
|
2021-08-31 14:25:54 +00:00
|
|
|
rc = vhost_blk_session_register_interrupts(bvsession, no_bdev_vdev_vq_worker,
|
|
|
|
"no_bdev_vdev_vq_worker");
|
2020-12-31 13:07:31 +00:00
|
|
|
if (rc) {
|
|
|
|
SPDK_ERRLOG("%s: Interrupt register failed\n", vsession->name);
|
|
|
|
return rc;
|
|
|
|
}
|
2020-10-24 04:41:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-31 13:07:31 +00:00
|
|
|
bvsession->requestq_poller = SPDK_POLLER_REGISTER(no_bdev_vdev_worker, bvsession, 0);
|
|
|
|
spdk_poller_register_interrupt(bvsession->requestq_poller, vhost_blk_poller_set_interrupt_mode,
|
|
|
|
bvsession);
|
2020-10-24 04:41:30 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 12:45:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:00:26 +00:00
|
|
|
static void
|
2022-03-30 12:46:23 +00:00
|
|
|
vhost_user_bdev_remove_cb(struct spdk_vhost_dev *vdev, bdev_event_cb_complete cb, void *cb_arg)
|
2017-07-26 10:00:26 +00:00
|
|
|
{
|
2019-07-20 14:59:04 +00:00
|
|
|
SPDK_WARNLOG("%s: hot-removing bdev - all further requests will fail.\n",
|
2022-03-30 12:46:23 +00:00
|
|
|
vdev->name);
|
2019-01-13 17:24:52 +00:00
|
|
|
|
2022-03-30 12:46:23 +00:00
|
|
|
vhost_user_dev_foreach_session(vdev, vhost_user_session_bdev_remove_cb,
|
|
|
|
cb, cb_arg);
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 12:46:23 +00:00
|
|
|
static void
|
|
|
|
vhost_user_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_vhost_dev *vdev,
|
|
|
|
bdev_event_cb_complete cb, void *cb_arg)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case SPDK_BDEV_EVENT_REMOVE:
|
|
|
|
vhost_user_bdev_remove_cb(vdev, cb, cb_arg);
|
|
|
|
break;
|
|
|
|
case SPDK_BDEV_EVENT_RESIZE:
|
|
|
|
vhost_user_blk_resize_cb(vdev, cb, cb_arg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:05:19 +00:00
|
|
|
static void
|
|
|
|
bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev,
|
|
|
|
void *event_ctx)
|
|
|
|
{
|
2022-03-30 12:46:23 +00:00
|
|
|
struct spdk_vhost_dev *vdev = (struct spdk_vhost_dev *)event_ctx;
|
2021-09-17 08:23:35 +00:00
|
|
|
struct spdk_vhost_blk_dev *bvdev = to_blk_dev(vdev);
|
2022-03-30 12:46:23 +00:00
|
|
|
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_DEBUGLOG(vhost_blk, "Bdev event: type %d, name %s\n",
|
2020-03-26 14:05:19 +00:00
|
|
|
type,
|
|
|
|
bdev->name);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case SPDK_BDEV_EVENT_REMOVE:
|
|
|
|
case SPDK_BDEV_EVENT_RESIZE:
|
2021-09-17 08:23:35 +00:00
|
|
|
bvdev->ops->bdev_event(type, vdev, bdev_event_cpl_cb, (void *)type);
|
2020-03-26 14:05:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 16:52:33 +00:00
|
|
|
static void
|
2018-12-14 12:51:19 +00:00
|
|
|
free_task_pool(struct spdk_vhost_blk_session *bvsession)
|
2017-08-08 16:52:33 +00:00
|
|
|
{
|
2018-12-14 12:51:19 +00:00
|
|
|
struct spdk_vhost_session *vsession = &bvsession->vsession;
|
2017-10-10 20:19:40 +00:00
|
|
|
struct spdk_vhost_virtqueue *vq;
|
|
|
|
uint16_t i;
|
2017-08-08 16:52:33 +00:00
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
for (i = 0; i < vsession->max_queues; i++) {
|
|
|
|
vq = &vsession->virtqueue[i];
|
2017-10-10 20:19:40 +00:00
|
|
|
if (vq->tasks == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-08 16:52:33 +00:00
|
|
|
|
2019-04-08 21:39:30 +00:00
|
|
|
spdk_free(vq->tasks);
|
2017-10-10 20:19:40 +00:00
|
|
|
vq->tasks = NULL;
|
2017-08-08 16:52:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2022-10-11 09:16:28 +00:00
|
|
|
alloc_vq_task_pool(struct spdk_vhost_session *vsession, uint16_t qid)
|
2017-08-08 16:52:33 +00:00
|
|
|
{
|
2022-10-11 09:16:28 +00:00
|
|
|
struct spdk_vhost_blk_session *bvsession = to_blk_session(vsession);
|
2017-10-10 20:19:40 +00:00
|
|
|
struct spdk_vhost_virtqueue *vq;
|
2022-04-12 07:26:56 +00:00
|
|
|
struct spdk_vhost_user_blk_task *task;
|
2017-10-10 20:19:40 +00:00
|
|
|
uint32_t task_cnt;
|
|
|
|
uint32_t j;
|
2017-08-08 16:52:33 +00:00
|
|
|
|
2022-10-11 09:16:28 +00:00
|
|
|
if (qid >= SPDK_VHOST_MAX_VQUEUES) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-04-23 11:04:18 +00:00
|
|
|
|
2022-10-11 09:16:28 +00:00
|
|
|
vq = &vsession->virtqueue[qid];
|
|
|
|
if (vq->vring.desc == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-10 20:19:40 +00:00
|
|
|
|
2022-10-11 09:16:28 +00:00
|
|
|
task_cnt = vq->vring.size;
|
|
|
|
if (task_cnt > SPDK_VHOST_MAX_VQ_SIZE) {
|
|
|
|
/* sanity check */
|
|
|
|
SPDK_ERRLOG("%s: virtqueue %"PRIu16" is too big. (size = %"PRIu32", max = %"PRIu32")\n",
|
|
|
|
vsession->name, qid, task_cnt, SPDK_VHOST_MAX_VQ_SIZE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
vq->tasks = spdk_zmalloc(sizeof(struct spdk_vhost_user_blk_task) * task_cnt,
|
|
|
|
SPDK_CACHE_LINE_SIZE, NULL,
|
|
|
|
SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
|
|
|
|
if (vq->tasks == NULL) {
|
|
|
|
SPDK_ERRLOG("%s: failed to allocate %"PRIu32" tasks for virtqueue %"PRIu16"\n",
|
|
|
|
vsession->name, task_cnt, qid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < task_cnt; j++) {
|
|
|
|
task = &((struct spdk_vhost_user_blk_task *)vq->tasks)[j];
|
|
|
|
task->bvsession = bvsession;
|
|
|
|
task->req_idx = j;
|
|
|
|
task->vq = vq;
|
2017-08-08 16:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-24 14:48:22 +00:00
|
|
|
static int
|
2022-09-01 10:15:40 +00:00
|
|
|
vhost_blk_start(struct spdk_vhost_dev *vdev,
|
|
|
|
struct spdk_vhost_session *vsession, void *unused)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
2019-07-20 15:03:04 +00:00
|
|
|
struct spdk_vhost_blk_session *bvsession = to_blk_session(vsession);
|
2017-08-01 13:46:51 +00:00
|
|
|
struct spdk_vhost_blk_dev *bvdev;
|
2018-04-23 11:04:18 +00:00
|
|
|
int i, rc = 0;
|
2017-05-24 14:48:22 +00:00
|
|
|
|
lib/vhost: don't restart device multiple times
We will stop/start the device multiple times when a new
vring is added, and also stop/start the device when
set vring's callfd, actually we only need to start
the device after a I/O queue is enabled, DPDK rte_vhost
will not help us to start the device in some scenarios,
so this is controlled in SPDK.
Now we improve the workaround to make it consistent with
vhost-user specification.
For each SET_VRING_KICK message, we will setup the new
added vring, and then we try to start the device.
For each SET_VRING_CALL message, we will add one more
interrupt count, previously this is done when enable
the vring, which is not accurate.
For each GET_VRING_BASE message, we will stop the
device before the first message.
With above changes, we will start/stop the device once,
any new added vrings after starting the device will be
polled in next `vdev_worker` poller.
Change-Id: I5a87c73d34ce7c5f96db7502a68c5fa2cb2e4f74
Signed-off-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/14928
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Mellanox Build Bot
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
2022-10-11 09:30:09 +00:00
|
|
|
/* return if start is already in progress */
|
|
|
|
if (bvsession->requestq_poller) {
|
|
|
|
SPDK_INFOLOG(vhost, "%s: start in progress\n", vsession->name);
|
|
|
|
return -EINPROGRESS;
|
|
|
|
}
|
2018-12-14 12:51:19 +00:00
|
|
|
|
2018-04-23 11:04:18 +00:00
|
|
|
/* validate all I/O queues are in a contiguous index range */
|
2018-12-13 10:51:34 +00:00
|
|
|
for (i = 0; i < vsession->max_queues; i++) {
|
2019-08-05 10:43:38 +00:00
|
|
|
/* vring.desc and vring.desc_packed are in a union struct
|
|
|
|
* so q->vring.desc can replace q->vring.desc_packed.
|
|
|
|
*/
|
2018-12-13 10:51:34 +00:00
|
|
|
if (vsession->virtqueue[i].vring.desc == NULL) {
|
2019-07-20 14:59:04 +00:00
|
|
|
SPDK_ERRLOG("%s: queue %"PRIu32" is empty\n", vsession->name, i);
|
2022-09-01 14:09:52 +00:00
|
|
|
return -1;
|
2018-04-23 11:04:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
lib/vhost: don't restart device multiple times
We will stop/start the device multiple times when a new
vring is added, and also stop/start the device when
set vring's callfd, actually we only need to start
the device after a I/O queue is enabled, DPDK rte_vhost
will not help us to start the device in some scenarios,
so this is controlled in SPDK.
Now we improve the workaround to make it consistent with
vhost-user specification.
For each SET_VRING_KICK message, we will setup the new
added vring, and then we try to start the device.
For each SET_VRING_CALL message, we will add one more
interrupt count, previously this is done when enable
the vring, which is not accurate.
For each GET_VRING_BASE message, we will stop the
device before the first message.
With above changes, we will start/stop the device once,
any new added vrings after starting the device will be
polled in next `vdev_worker` poller.
Change-Id: I5a87c73d34ce7c5f96db7502a68c5fa2cb2e4f74
Signed-off-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/14928
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Mellanox Build Bot
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
2022-10-11 09:30:09 +00:00
|
|
|
bvdev = to_blk_dev(vdev);
|
|
|
|
assert(bvdev != NULL);
|
|
|
|
bvsession->bvdev = bvdev;
|
|
|
|
|
2017-08-04 10:02:03 +00:00
|
|
|
if (bvdev->bdev) {
|
2022-01-14 12:33:54 +00:00
|
|
|
bvsession->io_channel = vhost_blk_get_io_channel(vdev);
|
2019-01-06 23:15:16 +00:00
|
|
|
if (!bvsession->io_channel) {
|
2018-12-14 12:51:19 +00:00
|
|
|
free_task_pool(bvsession);
|
2019-07-20 14:59:04 +00:00
|
|
|
SPDK_ERRLOG("%s: I/O channel allocation failed\n", vsession->name);
|
2022-09-01 14:09:52 +00:00
|
|
|
return -1;
|
2017-08-04 10:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 04:41:30 +00:00
|
|
|
if (spdk_interrupt_mode_is_enabled()) {
|
2021-08-31 14:25:54 +00:00
|
|
|
if (bvdev->bdev) {
|
|
|
|
rc = vhost_blk_session_register_interrupts(bvsession,
|
|
|
|
vdev_vq_worker,
|
|
|
|
"vdev_vq_worker");
|
|
|
|
} else {
|
|
|
|
rc = vhost_blk_session_register_interrupts(bvsession,
|
|
|
|
no_bdev_vdev_vq_worker,
|
|
|
|
"no_bdev_vdev_vq_worker");
|
|
|
|
}
|
|
|
|
|
2020-10-24 04:41:30 +00:00
|
|
|
if (rc) {
|
|
|
|
SPDK_ERRLOG("%s: Interrupt register failed\n", vsession->name);
|
2022-09-01 14:09:52 +00:00
|
|
|
return rc;
|
2020-10-24 04:41:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 08:39:29 +00:00
|
|
|
if (bvdev->bdev) {
|
|
|
|
bvsession->requestq_poller = SPDK_POLLER_REGISTER(vdev_worker, bvsession, 0);
|
|
|
|
} else {
|
|
|
|
bvsession->requestq_poller = SPDK_POLLER_REGISTER(no_bdev_vdev_worker, bvsession, 0);
|
|
|
|
}
|
2020-12-31 13:07:31 +00:00
|
|
|
SPDK_INFOLOG(vhost, "%s: started poller on lcore %d\n",
|
|
|
|
vsession->name, spdk_env_get_current_core());
|
|
|
|
|
|
|
|
spdk_poller_register_interrupt(bvsession->requestq_poller, vhost_blk_poller_set_interrupt_mode,
|
|
|
|
bvsession);
|
|
|
|
|
lib/vhost: don't restart device multiple times
We will stop/start the device multiple times when a new
vring is added, and also stop/start the device when
set vring's callfd, actually we only need to start
the device after a I/O queue is enabled, DPDK rte_vhost
will not help us to start the device in some scenarios,
so this is controlled in SPDK.
Now we improve the workaround to make it consistent with
vhost-user specification.
For each SET_VRING_KICK message, we will setup the new
added vring, and then we try to start the device.
For each SET_VRING_CALL message, we will add one more
interrupt count, previously this is done when enable
the vring, which is not accurate.
For each GET_VRING_BASE message, we will stop the
device before the first message.
With above changes, we will start/stop the device once,
any new added vrings after starting the device will be
polled in next `vdev_worker` poller.
Change-Id: I5a87c73d34ce7c5f96db7502a68c5fa2cb2e4f74
Signed-off-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/14928
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Mellanox Build Bot
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
2022-10-11 09:30:09 +00:00
|
|
|
return 0;
|
2017-08-04 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 00:16:47 +00:00
|
|
|
static int
|
2018-12-14 12:51:19 +00:00
|
|
|
destroy_session_poller_cb(void *arg)
|
2017-08-04 10:02:03 +00:00
|
|
|
{
|
2018-12-14 12:51:19 +00:00
|
|
|
struct spdk_vhost_blk_session *bvsession = arg;
|
|
|
|
struct spdk_vhost_session *vsession = &bvsession->vsession;
|
2022-10-28 08:57:47 +00:00
|
|
|
struct spdk_vhost_user_dev *user_dev = to_user_dev(vsession->vdev);
|
2017-09-11 17:45:56 +00:00
|
|
|
int i;
|
2017-08-04 10:02:03 +00:00
|
|
|
|
2022-10-28 08:57:47 +00:00
|
|
|
if (vsession->task_cnt > 0 || (pthread_mutex_trylock(&user_dev->lock) != 0)) {
|
2021-11-24 20:05:55 +00:00
|
|
|
assert(vsession->stop_retry_count > 0);
|
|
|
|
vsession->stop_retry_count--;
|
|
|
|
if (vsession->stop_retry_count == 0) {
|
|
|
|
SPDK_ERRLOG("%s: Timedout when destroy session (task_cnt %d)\n", vsession->name,
|
|
|
|
vsession->task_cnt);
|
|
|
|
spdk_poller_unregister(&bvsession->stop_poller);
|
2022-01-11 12:11:22 +00:00
|
|
|
vhost_user_session_stop_done(vsession, -ETIMEDOUT);
|
2021-11-24 20:05:55 +00:00
|
|
|
}
|
2017-08-04 10:02:03 +00:00
|
|
|
|
2020-05-04 09:51:27 +00:00
|
|
|
return SPDK_POLLER_BUSY;
|
2019-03-16 23:50:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
for (i = 0; i < vsession->max_queues; i++) {
|
|
|
|
vsession->virtqueue[i].next_event_time = 0;
|
2019-07-20 21:06:19 +00:00
|
|
|
vhost_vq_used_signal(vsession, &vsession->virtqueue[i]);
|
2017-09-11 17:45:56 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_INFOLOG(vhost, "%s: stopping poller on lcore %d\n",
|
2019-07-20 14:59:04 +00:00
|
|
|
vsession->name, spdk_env_get_current_core());
|
2017-08-04 10:02:03 +00:00
|
|
|
|
2019-01-06 23:15:16 +00:00
|
|
|
if (bvsession->io_channel) {
|
2022-01-14 12:33:54 +00:00
|
|
|
vhost_blk_put_io_channel(bvsession->io_channel);
|
2019-01-06 23:15:16 +00:00
|
|
|
bvsession->io_channel = NULL;
|
2017-08-04 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 12:51:19 +00:00
|
|
|
free_task_pool(bvsession);
|
2019-03-16 23:31:45 +00:00
|
|
|
spdk_poller_unregister(&bvsession->stop_poller);
|
2022-01-11 12:11:22 +00:00
|
|
|
vhost_user_session_stop_done(vsession, 0);
|
2018-03-13 00:16:47 +00:00
|
|
|
|
2022-10-28 08:57:47 +00:00
|
|
|
pthread_mutex_unlock(&user_dev->lock);
|
2020-05-04 09:51:27 +00:00
|
|
|
return SPDK_POLLER_BUSY;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 13:46:51 +00:00
|
|
|
static int
|
2022-11-07 02:30:39 +00:00
|
|
|
vhost_blk_stop(struct spdk_vhost_dev *vdev,
|
|
|
|
struct spdk_vhost_session *vsession, void *unused)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
2019-07-20 15:03:04 +00:00
|
|
|
struct spdk_vhost_blk_session *bvsession = to_blk_session(vsession);
|
2017-05-24 14:48:22 +00:00
|
|
|
|
lib/vhost: don't restart device multiple times
We will stop/start the device multiple times when a new
vring is added, and also stop/start the device when
set vring's callfd, actually we only need to start
the device after a I/O queue is enabled, DPDK rte_vhost
will not help us to start the device in some scenarios,
so this is controlled in SPDK.
Now we improve the workaround to make it consistent with
vhost-user specification.
For each SET_VRING_KICK message, we will setup the new
added vring, and then we try to start the device.
For each SET_VRING_CALL message, we will add one more
interrupt count, previously this is done when enable
the vring, which is not accurate.
For each GET_VRING_BASE message, we will stop the
device before the first message.
With above changes, we will start/stop the device once,
any new added vrings after starting the device will be
polled in next `vdev_worker` poller.
Change-Id: I5a87c73d34ce7c5f96db7502a68c5fa2cb2e4f74
Signed-off-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/14928
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Mellanox Build Bot
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
2022-10-11 09:30:09 +00:00
|
|
|
/* return if stop is already in progress */
|
|
|
|
if (bvsession->stop_poller) {
|
|
|
|
return -EINPROGRESS;
|
|
|
|
}
|
|
|
|
|
2018-12-14 12:51:19 +00:00
|
|
|
spdk_poller_unregister(&bvsession->requestq_poller);
|
2020-10-24 04:41:30 +00:00
|
|
|
|
|
|
|
if (vsession->virtqueue[0].intr) {
|
|
|
|
vhost_blk_session_unregister_interrupts(bvsession);
|
|
|
|
}
|
|
|
|
|
2022-01-11 12:11:22 +00:00
|
|
|
/* vhost_user_session_send_event timeout is 3 seconds, here set retry within 4 seconds */
|
2021-11-24 20:05:55 +00:00
|
|
|
bvsession->vsession.stop_retry_count = 4000;
|
2020-04-14 06:49:46 +00:00
|
|
|
bvsession->stop_poller = SPDK_POLLER_REGISTER(destroy_session_poller_cb,
|
2019-03-16 23:31:45 +00:00
|
|
|
bvsession, 1000);
|
2017-08-01 13:46:51 +00:00
|
|
|
return 0;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 14:37:50 +00:00
|
|
|
static void
|
2019-07-20 21:06:19 +00:00
|
|
|
vhost_blk_dump_info_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w)
|
2017-07-13 14:37:50 +00:00
|
|
|
{
|
2018-04-18 20:34:49 +00:00
|
|
|
struct spdk_vhost_blk_dev *bvdev;
|
|
|
|
|
|
|
|
bvdev = to_blk_dev(vdev);
|
2017-07-13 14:37:50 +00:00
|
|
|
assert(bvdev != NULL);
|
2020-06-02 22:13:48 +00:00
|
|
|
|
2019-02-01 05:45:08 +00:00
|
|
|
spdk_json_write_named_object_begin(w, "block");
|
2017-07-13 14:37:50 +00:00
|
|
|
|
2019-02-01 05:45:08 +00:00
|
|
|
spdk_json_write_named_bool(w, "readonly", bvdev->readonly);
|
2017-07-13 14:37:50 +00:00
|
|
|
|
|
|
|
spdk_json_write_name(w, "bdev");
|
2020-06-02 22:13:48 +00:00
|
|
|
if (bvdev->bdev) {
|
|
|
|
spdk_json_write_string(w, spdk_bdev_get_name(bvdev->bdev));
|
2017-07-13 14:37:50 +00:00
|
|
|
} else {
|
|
|
|
spdk_json_write_null(w);
|
|
|
|
}
|
2021-09-17 08:23:35 +00:00
|
|
|
spdk_json_write_named_string(w, "transport", bvdev->ops->name);
|
2017-07-13 14:37:50 +00:00
|
|
|
|
|
|
|
spdk_json_write_object_end(w);
|
|
|
|
}
|
|
|
|
|
2018-03-19 18:01:57 +00:00
|
|
|
static void
|
2019-07-20 21:06:19 +00:00
|
|
|
vhost_blk_write_config_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w)
|
2018-03-19 18:01:57 +00:00
|
|
|
{
|
2018-04-18 20:34:49 +00:00
|
|
|
struct spdk_vhost_blk_dev *bvdev;
|
|
|
|
|
|
|
|
bvdev = to_blk_dev(vdev);
|
2019-07-20 15:03:04 +00:00
|
|
|
assert(bvdev != NULL);
|
2020-06-02 22:13:48 +00:00
|
|
|
|
2018-03-19 18:01:57 +00:00
|
|
|
if (!bvdev->bdev) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
spdk_json_write_object_begin(w);
|
2019-09-30 10:51:55 +00:00
|
|
|
spdk_json_write_named_string(w, "method", "vhost_create_blk_controller");
|
2018-03-19 18:01:57 +00:00
|
|
|
|
|
|
|
spdk_json_write_named_object_begin(w, "params");
|
|
|
|
spdk_json_write_named_string(w, "ctrlr", vdev->name);
|
|
|
|
spdk_json_write_named_string(w, "dev_name", spdk_bdev_get_name(bvdev->bdev));
|
2020-03-05 21:36:13 +00:00
|
|
|
spdk_json_write_named_string(w, "cpumask",
|
|
|
|
spdk_cpuset_fmt(spdk_thread_get_cpumask(vdev->thread)));
|
2018-03-19 18:01:57 +00:00
|
|
|
spdk_json_write_named_bool(w, "readonly", bvdev->readonly);
|
2021-09-17 08:23:35 +00:00
|
|
|
spdk_json_write_named_string(w, "transport", bvdev->ops->name);
|
2018-03-19 18:01:57 +00:00
|
|
|
spdk_json_write_object_end(w);
|
|
|
|
|
|
|
|
spdk_json_write_object_end(w);
|
|
|
|
}
|
|
|
|
|
2019-07-20 21:06:19 +00:00
|
|
|
static int vhost_blk_destroy(struct spdk_vhost_dev *dev);
|
2018-01-26 13:09:39 +00:00
|
|
|
|
2018-01-23 01:51:56 +00:00
|
|
|
static int
|
2019-07-20 21:06:19 +00:00
|
|
|
vhost_blk_get_config(struct spdk_vhost_dev *vdev, uint8_t *config,
|
|
|
|
uint32_t len)
|
2018-01-23 01:51:56 +00:00
|
|
|
{
|
2018-03-02 01:42:56 +00:00
|
|
|
struct virtio_blk_config blkcfg;
|
2018-01-23 01:51:56 +00:00
|
|
|
struct spdk_bdev *bdev;
|
|
|
|
uint32_t blk_size;
|
|
|
|
uint64_t blkcnt;
|
|
|
|
|
2020-02-21 01:50:12 +00:00
|
|
|
memset(&blkcfg, 0, sizeof(blkcfg));
|
2022-04-25 13:01:41 +00:00
|
|
|
bdev = vhost_blk_get_bdev(vdev);
|
2018-10-16 04:57:01 +00:00
|
|
|
if (bdev == NULL) {
|
|
|
|
/* We can't just return -1 here as this GET_CONFIG message might
|
|
|
|
* be caused by a QEMU VM reboot. Returning -1 will indicate an
|
|
|
|
* error to QEMU, who might then decide to terminate itself.
|
|
|
|
* We don't want that. A simple reboot shouldn't break the system.
|
|
|
|
*
|
|
|
|
* Presenting a block device with block size 0 and block count 0
|
|
|
|
* doesn't cause any problems on QEMU side and the virtio-pci
|
|
|
|
* device is even still available inside the VM, but there will
|
|
|
|
* be no block device created for it - the kernel drivers will
|
|
|
|
* silently reject it.
|
|
|
|
*/
|
|
|
|
blk_size = 0;
|
|
|
|
blkcnt = 0;
|
|
|
|
} else {
|
|
|
|
blk_size = spdk_bdev_get_block_size(bdev);
|
|
|
|
blkcnt = spdk_bdev_get_num_blocks(bdev);
|
2019-05-06 01:24:57 +00:00
|
|
|
if (spdk_bdev_get_buf_align(bdev) > 1) {
|
|
|
|
blkcfg.size_max = SPDK_BDEV_LARGE_BUF_MAX_SIZE;
|
2022-11-14 04:28:38 +00:00
|
|
|
blkcfg.seg_max = spdk_min(SPDK_VHOST_IOVS_MAX - 2 - 1, SPDK_BDEV_IO_NUM_CHILD_IOV - 2 - 1);
|
2019-05-06 01:24:57 +00:00
|
|
|
} else {
|
|
|
|
blkcfg.size_max = 131072;
|
|
|
|
/* -2 for REQ and RESP and -1 for region boundary splitting */
|
|
|
|
blkcfg.seg_max = SPDK_VHOST_IOVS_MAX - 2 - 1;
|
|
|
|
}
|
2018-10-16 04:57:01 +00:00
|
|
|
}
|
2018-01-23 01:51:56 +00:00
|
|
|
|
2018-03-02 01:42:56 +00:00
|
|
|
blkcfg.blk_size = blk_size;
|
2018-01-23 01:51:56 +00:00
|
|
|
/* minimum I/O size in blocks */
|
2018-03-02 01:42:56 +00:00
|
|
|
blkcfg.min_io_size = 1;
|
2018-01-23 01:51:56 +00:00
|
|
|
/* expressed in 512 Bytes sectors */
|
2018-03-02 01:42:56 +00:00
|
|
|
blkcfg.capacity = (blkcnt * blk_size) / 512;
|
2018-01-23 01:51:56 +00:00
|
|
|
/* QEMU can overwrite this value when started */
|
2018-03-02 01:42:56 +00:00
|
|
|
blkcfg.num_queues = SPDK_VHOST_MAX_VQUEUES;
|
|
|
|
|
|
|
|
if (bdev && spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_UNMAP)) {
|
|
|
|
/* 16MiB, expressed in 512 Bytes */
|
|
|
|
blkcfg.max_discard_sectors = 32768;
|
|
|
|
blkcfg.max_discard_seg = 1;
|
|
|
|
blkcfg.discard_sector_alignment = blk_size / 512;
|
|
|
|
}
|
|
|
|
if (bdev && spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES)) {
|
|
|
|
blkcfg.max_write_zeroes_sectors = 32768;
|
|
|
|
blkcfg.max_write_zeroes_seg = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(config, &blkcfg, spdk_min(len, sizeof(blkcfg)));
|
2018-01-23 01:51:56 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-05 11:04:19 +00:00
|
|
|
static int
|
|
|
|
vhost_blk_set_coalescing(struct spdk_vhost_dev *vdev, uint32_t delay_base_us,
|
|
|
|
uint32_t iops_threshold)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_blk_dev *bvdev = to_blk_dev(vdev);
|
|
|
|
|
|
|
|
return bvdev->ops->set_coalescing(vdev, delay_base_us, iops_threshold);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vhost_blk_get_coalescing(struct spdk_vhost_dev *vdev, uint32_t *delay_base_us,
|
|
|
|
uint32_t *iops_threshold)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_blk_dev *bvdev = to_blk_dev(vdev);
|
|
|
|
|
|
|
|
bvdev->ops->get_coalescing(vdev, delay_base_us, iops_threshold);
|
|
|
|
}
|
|
|
|
|
2022-03-03 10:24:34 +00:00
|
|
|
static const struct spdk_vhost_user_dev_backend vhost_blk_user_device_backend = {
|
2018-12-14 12:51:19 +00:00
|
|
|
.session_ctx_size = sizeof(struct spdk_vhost_blk_session) - sizeof(struct spdk_vhost_session),
|
2019-07-20 21:06:19 +00:00
|
|
|
.start_session = vhost_blk_start,
|
|
|
|
.stop_session = vhost_blk_stop,
|
2022-10-11 09:16:28 +00:00
|
|
|
.alloc_vq_tasks = alloc_vq_task_pool,
|
2022-03-03 10:24:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct spdk_vhost_dev_backend vhost_blk_device_backend = {
|
2022-05-13 08:37:06 +00:00
|
|
|
.type = VHOST_BACKEND_BLK,
|
2019-07-20 21:06:19 +00:00
|
|
|
.vhost_get_config = vhost_blk_get_config,
|
|
|
|
.dump_info_json = vhost_blk_dump_info_json,
|
|
|
|
.write_config_json = vhost_blk_write_config_json,
|
|
|
|
.remove_device = vhost_blk_destroy,
|
2022-12-05 11:04:19 +00:00
|
|
|
.set_coalescing = vhost_blk_set_coalescing,
|
|
|
|
.get_coalescing = vhost_blk_get_coalescing,
|
2017-05-24 14:48:22 +00:00
|
|
|
};
|
|
|
|
|
2021-09-17 08:23:35 +00:00
|
|
|
int
|
|
|
|
virtio_blk_construct_ctrlr(struct spdk_vhost_dev *vdev, const char *address,
|
|
|
|
struct spdk_cpuset *cpumask, const struct spdk_json_val *params,
|
|
|
|
const struct spdk_vhost_user_dev_backend *user_backend)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_blk_dev *bvdev = to_blk_dev(vdev);
|
|
|
|
|
|
|
|
return bvdev->ops->create_ctrlr(vdev, cpumask, address, params, (void *)user_backend);
|
|
|
|
}
|
|
|
|
|
2017-05-24 14:48:22 +00:00
|
|
|
int
|
2020-03-30 11:16:24 +00:00
|
|
|
spdk_vhost_blk_construct(const char *name, const char *cpumask, const char *dev_name,
|
2021-09-17 08:23:35 +00:00
|
|
|
const char *transport, const struct spdk_json_val *params)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
2017-09-08 11:06:41 +00:00
|
|
|
struct spdk_vhost_blk_dev *bvdev = NULL;
|
2019-12-03 20:58:42 +00:00
|
|
|
struct spdk_vhost_dev *vdev;
|
2017-05-24 14:48:22 +00:00
|
|
|
struct spdk_bdev *bdev;
|
2021-09-17 08:23:35 +00:00
|
|
|
const char *transport_name = VIRTIO_BLK_DEFAULT_TRANSPORT;
|
2017-09-08 11:06:41 +00:00
|
|
|
int ret = 0;
|
2017-05-24 14:48:22 +00:00
|
|
|
|
2019-04-08 21:39:30 +00:00
|
|
|
bvdev = calloc(1, sizeof(*bvdev));
|
2017-05-24 14:48:22 +00:00
|
|
|
if (bvdev == NULL) {
|
2018-08-10 10:04:46 +00:00
|
|
|
ret = -ENOMEM;
|
2017-09-08 11:06:41 +00:00
|
|
|
goto out;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 08:23:35 +00:00
|
|
|
if (transport != NULL) {
|
|
|
|
transport_name = transport;
|
|
|
|
}
|
|
|
|
|
|
|
|
bvdev->ops = virtio_blk_get_transport_ops(transport_name);
|
|
|
|
if (!bvdev->ops) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
SPDK_ERRLOG("Transport type '%s' unavailable.\n", transport_name);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-10-01 04:47:53 +00:00
|
|
|
ret = spdk_bdev_open_ext(dev_name, true, bdev_event_cb, bvdev, &bvdev->bdev_desc);
|
|
|
|
if (ret != 0) {
|
|
|
|
SPDK_ERRLOG("%s: could not open bdev '%s', error=%d\n",
|
|
|
|
name, dev_name, ret);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
bdev = spdk_bdev_desc_get_bdev(bvdev->bdev_desc);
|
|
|
|
|
2019-12-03 20:58:42 +00:00
|
|
|
vdev = &bvdev->vdev;
|
2019-12-03 21:17:35 +00:00
|
|
|
vdev->virtio_features = SPDK_VHOST_BLK_FEATURES_BASE;
|
2019-12-03 20:58:42 +00:00
|
|
|
vdev->disabled_features = SPDK_VHOST_BLK_DISABLED_FEATURES;
|
2019-12-25 10:01:18 +00:00
|
|
|
vdev->protocol_features = SPDK_VHOST_BLK_PROTOCOL_FEATURES;
|
2020-03-30 11:16:24 +00:00
|
|
|
|
2019-12-03 21:17:35 +00:00
|
|
|
if (spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_UNMAP)) {
|
|
|
|
vdev->virtio_features |= (1ULL << VIRTIO_BLK_F_DISCARD);
|
2019-12-03 20:58:42 +00:00
|
|
|
}
|
2019-12-03 21:17:35 +00:00
|
|
|
if (spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES)) {
|
|
|
|
vdev->virtio_features |= (1ULL << VIRTIO_BLK_F_WRITE_ZEROES);
|
2019-12-03 20:58:42 +00:00
|
|
|
}
|
2021-09-17 08:23:35 +00:00
|
|
|
|
2019-12-03 21:17:35 +00:00
|
|
|
if (spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_FLUSH)) {
|
|
|
|
vdev->virtio_features |= (1ULL << VIRTIO_BLK_F_FLUSH);
|
2019-12-03 20:58:42 +00:00
|
|
|
}
|
2019-10-02 13:15:51 +00:00
|
|
|
|
2020-04-24 14:19:23 +00:00
|
|
|
/*
|
2022-01-14 12:33:54 +00:00
|
|
|
* When starting qemu with multiqueue enable, the vhost device will
|
2020-04-24 14:19:23 +00:00
|
|
|
* be started/stopped many times, related to the queues num, as the
|
2022-01-14 12:33:54 +00:00
|
|
|
* exact number of queues used for this device is not known at the time.
|
|
|
|
* The target has to stop and start the device once got a valid IO queue.
|
2022-11-02 15:09:36 +00:00
|
|
|
* When stopping and starting the vhost device, the backend bdev io device
|
2020-04-24 14:19:23 +00:00
|
|
|
* will be deleted and created repeatedly.
|
|
|
|
* Hold a bdev reference so that in the struct spdk_vhost_blk_dev, so that
|
|
|
|
* the io device will not be deleted.
|
|
|
|
*/
|
|
|
|
bvdev->dummy_io_channel = spdk_bdev_get_io_channel(bvdev->bdev_desc);
|
|
|
|
|
2017-05-24 14:48:22 +00:00
|
|
|
bvdev->bdev = bdev;
|
2021-09-17 08:23:35 +00:00
|
|
|
bvdev->readonly = false;
|
|
|
|
ret = vhost_dev_register(vdev, name, cpumask, params, &vhost_blk_device_backend,
|
2022-03-03 10:24:34 +00:00
|
|
|
&vhost_blk_user_device_backend);
|
2017-05-24 14:48:22 +00:00
|
|
|
if (ret != 0) {
|
2020-04-24 14:19:23 +00:00
|
|
|
spdk_put_io_channel(bvdev->dummy_io_channel);
|
2017-06-29 18:23:50 +00:00
|
|
|
spdk_bdev_close(bvdev->bdev_desc);
|
2017-09-08 11:06:41 +00:00
|
|
|
goto out;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_INFOLOG(vhost, "%s: using bdev '%s'\n", name, dev_name);
|
2017-09-08 11:06:41 +00:00
|
|
|
out:
|
|
|
|
if (ret != 0 && bvdev) {
|
2019-04-08 21:39:30 +00:00
|
|
|
free(bvdev);
|
2017-09-08 11:06:41 +00:00
|
|
|
}
|
|
|
|
return ret;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 08:23:35 +00:00
|
|
|
int
|
|
|
|
virtio_blk_destroy_ctrlr(struct spdk_vhost_dev *vdev)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_blk_dev *bvdev = to_blk_dev(vdev);
|
|
|
|
|
|
|
|
return bvdev->ops->destroy_ctrlr(vdev);
|
|
|
|
}
|
|
|
|
|
2018-01-26 13:09:39 +00:00
|
|
|
static int
|
2019-07-20 21:06:19 +00:00
|
|
|
vhost_blk_destroy(struct spdk_vhost_dev *vdev)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
|
|
|
struct spdk_vhost_blk_dev *bvdev = to_blk_dev(vdev);
|
2017-08-01 16:08:32 +00:00
|
|
|
int rc;
|
2017-05-24 14:48:22 +00:00
|
|
|
|
2019-07-20 15:03:04 +00:00
|
|
|
assert(bvdev != NULL);
|
2020-04-24 14:19:23 +00:00
|
|
|
|
2019-07-20 21:06:19 +00:00
|
|
|
rc = vhost_dev_unregister(&bvdev->vdev);
|
2017-08-01 16:08:32 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-06-17 19:34:24 +00:00
|
|
|
/* if the bdev is removed, don't need call spdk_put_io_channel. */
|
|
|
|
if (bvdev->bdev) {
|
|
|
|
spdk_put_io_channel(bvdev->dummy_io_channel);
|
|
|
|
}
|
|
|
|
|
2017-12-12 07:40:39 +00:00
|
|
|
if (bvdev->bdev_desc) {
|
|
|
|
spdk_bdev_close(bvdev->bdev_desc);
|
|
|
|
bvdev->bdev_desc = NULL;
|
|
|
|
}
|
2017-05-24 14:48:22 +00:00
|
|
|
bvdev->bdev = NULL;
|
|
|
|
|
2019-04-08 21:39:30 +00:00
|
|
|
free(bvdev);
|
2017-05-24 14:48:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-14 12:33:54 +00:00
|
|
|
struct spdk_io_channel *
|
|
|
|
vhost_blk_get_io_channel(struct spdk_vhost_dev *vdev)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_blk_dev *bvdev = to_blk_dev(vdev);
|
|
|
|
|
|
|
|
return spdk_bdev_get_io_channel(bvdev->bdev_desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
vhost_blk_put_io_channel(struct spdk_io_channel *ch)
|
|
|
|
{
|
|
|
|
spdk_put_io_channel(ch);
|
|
|
|
}
|
|
|
|
|
2021-09-17 08:23:35 +00:00
|
|
|
static struct spdk_virtio_blk_transport *
|
|
|
|
vhost_user_blk_create(const struct spdk_json_val *params)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct spdk_virtio_blk_transport *vhost_user_blk;
|
|
|
|
|
|
|
|
vhost_user_blk = calloc(1, sizeof(*vhost_user_blk));
|
|
|
|
if (!vhost_user_blk) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = vhost_user_init();
|
|
|
|
if (ret != 0) {
|
|
|
|
free(vhost_user_blk);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vhost_user_blk;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vhost_user_blk_destroy(struct spdk_virtio_blk_transport *transport,
|
|
|
|
spdk_vhost_fini_cb cb_fn)
|
|
|
|
{
|
|
|
|
vhost_user_fini(cb_fn);
|
|
|
|
free(transport);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rpc_vhost_blk {
|
|
|
|
bool readonly;
|
|
|
|
bool packed_ring;
|
|
|
|
bool packed_ring_recovery;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct spdk_json_object_decoder rpc_construct_vhost_blk[] = {
|
|
|
|
{"readonly", offsetof(struct rpc_vhost_blk, readonly), spdk_json_decode_bool, true},
|
|
|
|
{"packed_ring", offsetof(struct rpc_vhost_blk, packed_ring), spdk_json_decode_bool, true},
|
|
|
|
{"packed_ring_recovery", offsetof(struct rpc_vhost_blk, packed_ring_recovery), spdk_json_decode_bool, true},
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
vhost_user_blk_create_ctrlr(struct spdk_vhost_dev *vdev, struct spdk_cpuset *cpumask,
|
|
|
|
const char *address, const struct spdk_json_val *params, void *custom_opts)
|
|
|
|
{
|
|
|
|
struct rpc_vhost_blk req = {0};
|
|
|
|
struct spdk_vhost_blk_dev *bvdev = to_blk_dev(vdev);
|
|
|
|
|
|
|
|
if (spdk_json_decode_object_relaxed(params, rpc_construct_vhost_blk,
|
|
|
|
SPDK_COUNTOF(rpc_construct_vhost_blk),
|
|
|
|
&req)) {
|
|
|
|
SPDK_DEBUGLOG(vhost_blk, "spdk_json_decode_object failed\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vdev->packed_ring_recovery = false;
|
|
|
|
|
|
|
|
if (req.packed_ring) {
|
|
|
|
vdev->virtio_features |= (uint64_t)req.packed_ring << VIRTIO_F_RING_PACKED;
|
|
|
|
vdev->packed_ring_recovery = req.packed_ring_recovery;
|
|
|
|
}
|
|
|
|
if (req.readonly) {
|
|
|
|
vdev->virtio_features |= (1ULL << VIRTIO_BLK_F_RO);
|
|
|
|
bvdev->readonly = req.readonly;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vhost_user_dev_register(vdev, address, cpumask, custom_opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vhost_user_blk_destroy_ctrlr(struct spdk_vhost_dev *vdev)
|
|
|
|
{
|
|
|
|
return vhost_user_dev_unregister(vdev);
|
|
|
|
}
|
|
|
|
|
2022-11-03 11:33:25 +00:00
|
|
|
static void
|
|
|
|
vhost_user_blk_dump_opts(struct spdk_virtio_blk_transport *transport, struct spdk_json_write_ctx *w)
|
|
|
|
{
|
|
|
|
assert(w != NULL);
|
|
|
|
|
|
|
|
spdk_json_write_named_string(w, "name", transport->ops->name);
|
|
|
|
}
|
|
|
|
|
2021-09-17 08:23:35 +00:00
|
|
|
static const struct spdk_virtio_blk_transport_ops vhost_user_blk = {
|
|
|
|
.name = "vhost_user_blk",
|
|
|
|
|
2022-11-03 11:33:25 +00:00
|
|
|
.dump_opts = vhost_user_blk_dump_opts,
|
2021-09-17 08:23:35 +00:00
|
|
|
|
|
|
|
.create = vhost_user_blk_create,
|
|
|
|
.destroy = vhost_user_blk_destroy,
|
|
|
|
|
|
|
|
.create_ctrlr = vhost_user_blk_create_ctrlr,
|
|
|
|
.destroy_ctrlr = vhost_user_blk_destroy_ctrlr,
|
|
|
|
|
|
|
|
.bdev_event = vhost_user_bdev_event_cb,
|
2022-12-05 11:04:19 +00:00
|
|
|
.set_coalescing = vhost_user_set_coalescing,
|
|
|
|
.get_coalescing = vhost_user_get_coalescing,
|
2021-09-17 08:23:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SPDK_VIRTIO_BLK_TRANSPORT_REGISTER(vhost_user_blk, &vhost_user_blk);
|
|
|
|
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_LOG_REGISTER_COMPONENT(vhost_blk)
|
|
|
|
SPDK_LOG_REGISTER_COMPONENT(vhost_blk_data)
|