2017-05-24 14:48:22 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright(c) Intel Corporation. All rights reserved.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/virtio_blk.h>
|
|
|
|
|
|
|
|
#include "spdk/env.h"
|
|
|
|
#include "spdk/bdev.h"
|
|
|
|
#include "spdk/conf.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"
|
|
|
|
|
|
|
|
#include "vhost_internal.h"
|
|
|
|
|
|
|
|
struct spdk_vhost_blk_task {
|
|
|
|
struct spdk_bdev_io *bdev_io;
|
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
|
|
|
volatile uint8_t *status;
|
|
|
|
|
|
|
|
uint16_t req_idx;
|
|
|
|
|
2018-09-13 09:45:18 +00:00
|
|
|
/* for io wait */
|
|
|
|
struct spdk_bdev_io_wait_entry bdev_io_wait;
|
|
|
|
|
2017-10-10 20:19:40 +00:00
|
|
|
/* If set, the task is currently used for I/O processing. */
|
|
|
|
bool used;
|
|
|
|
|
2018-01-08 20:35:53 +00:00
|
|
|
/** Number of bytes that were written. */
|
|
|
|
uint32_t used_len;
|
2017-05-24 14:48:22 +00:00
|
|
|
uint16_t iovcnt;
|
2017-07-28 16:03:49 +00:00
|
|
|
struct iovec iovs[SPDK_VHOST_IOVS_MAX];
|
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;
|
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;
|
|
|
|
|
2018-09-13 09:45:18 +00:00
|
|
|
static int
|
2018-12-16 00:15:06 +00:00
|
|
|
process_blk_request(struct spdk_vhost_blk_task *task,
|
|
|
|
struct spdk_vhost_blk_session *bvsession,
|
2018-09-13 09:45:18 +00:00
|
|
|
struct spdk_vhost_virtqueue *vq);
|
|
|
|
|
2017-05-24 14:48:22 +00:00
|
|
|
static void
|
2017-10-10 20:19:40 +00:00
|
|
|
blk_task_finish(struct spdk_vhost_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
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
invalid_blk_request(struct spdk_vhost_blk_task *task, uint8_t status)
|
|
|
|
{
|
2017-08-08 16:52:33 +00:00
|
|
|
if (task->status) {
|
|
|
|
*task->status = status;
|
|
|
|
}
|
|
|
|
|
2018-12-16 00:15:06 +00:00
|
|
|
spdk_vhost_vq_used_ring_enqueue(&task->bvsession->vsession, task->vq, task->req_idx,
|
2018-01-08 20:35:53 +00:00
|
|
|
task->used_len);
|
2017-10-10 20:19:40 +00:00
|
|
|
blk_task_finish(task);
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK_DATA, "Invalid request (status=%" PRIu8")\n", status);
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process task's descriptor chain and setup data related fields.
|
|
|
|
* Return
|
|
|
|
* total size of suplied buffers
|
|
|
|
*
|
|
|
|
* FIXME: Make this function return to rd_cnt and wr_cnt
|
|
|
|
*/
|
|
|
|
static int
|
2018-12-16 00:15:06 +00:00
|
|
|
blk_iovs_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;
|
|
|
|
|
2018-12-16 00:15:06 +00:00
|
|
|
rc = spdk_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) {
|
|
|
|
SPDK_ERRLOG("%s: Invalid descriptor at index %"PRIu16".\n", vdev->name, req_idx);
|
|
|
|
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)) {
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK, "Max IOVs in request reached (req_idx = %"PRIu16").\n",
|
2017-05-24 14:48:22 +00:00
|
|
|
req_idx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-16 00:15:06 +00:00
|
|
|
if (spdk_unlikely(spdk_vhost_vring_desc_to_iov(vsession, iovs, &cnt, desc))) {
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK, "Invalid descriptor %" PRIu16" (req_idx = %"PRIu16").\n",
|
2017-05-24 14:48:22 +00:00
|
|
|
req_idx, cnt);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-07-28 15:36:53 +00:00
|
|
|
len += desc->len;
|
2017-05-24 14:48:22 +00:00
|
|
|
|
|
|
|
out_cnt += spdk_vhost_vring_desc_is_wr(desc);
|
|
|
|
|
2017-10-09 15:28:00 +00:00
|
|
|
rc = spdk_vhost_vring_desc_get_next(&desc, desc_table, desc_table_size);
|
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("%s: Descriptor chain at index %"PRIu16" terminated unexpectedly.\n",
|
|
|
|
vdev->name, req_idx);
|
|
|
|
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",
|
|
|
|
vdev->name, desc_table_size, desc_handled_cnt);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
blk_request_finish(bool success, struct spdk_vhost_blk_task *task)
|
|
|
|
{
|
|
|
|
*task->status = success ? VIRTIO_BLK_S_OK : VIRTIO_BLK_S_IOERR;
|
2018-12-16 00:15:06 +00:00
|
|
|
spdk_vhost_vq_used_ring_enqueue(&task->bvsession->vsession, task->vq, task->req_idx,
|
2018-01-08 20:35:53 +00:00
|
|
|
task->used_len);
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK, "Finished task (%p) req_idx=%d\n status: %s\n", task,
|
2017-05-24 14:48:22 +00:00
|
|
|
task->req_idx, success ? "OK" : "FAIL");
|
2017-10-10 20:19:40 +00:00
|
|
|
blk_task_finish(task);
|
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);
|
|
|
|
blk_request_finish(success, task);
|
|
|
|
}
|
|
|
|
|
2018-09-13 09:45:18 +00:00
|
|
|
static void
|
|
|
|
blk_request_resubmit(void *arg)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_blk_task *task = (struct spdk_vhost_blk_task *)arg;
|
|
|
|
int rc = 0;
|
|
|
|
|
2018-12-16 00:15:06 +00:00
|
|
|
rc = process_blk_request(task, task->bvsession, task->vq);
|
2018-09-13 09:45:18 +00:00
|
|
|
if (rc == 0) {
|
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK, "====== Task %p resubmitted ======\n", task);
|
|
|
|
} else {
|
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK, "====== Task %p failed ======\n", task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
blk_request_queue_io(struct spdk_vhost_blk_task *task)
|
|
|
|
{
|
|
|
|
int rc;
|
2019-01-06 23:15:16 +00:00
|
|
|
struct spdk_vhost_blk_session *bvsession = task->bvsession;
|
|
|
|
struct spdk_bdev *bdev = bvsession->bvdev->bdev;
|
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;
|
|
|
|
|
2019-01-06 23:15:16 +00:00
|
|
|
rc = spdk_bdev_queue_io_wait(bdev, bvsession->io_channel, &task->bdev_io_wait);
|
2018-09-13 09:45:18 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("Queue io failed in vhost_blk, rc=%d\n", rc);
|
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_IOERR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 19:19:56 +00:00
|
|
|
static int
|
2018-12-16 00:15:06 +00:00
|
|
|
process_blk_request(struct spdk_vhost_blk_task *task,
|
|
|
|
struct spdk_vhost_blk_session *bvsession,
|
2017-10-10 20:19:40 +00:00
|
|
|
struct spdk_vhost_virtqueue *vq)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
2018-12-16 00:15:06 +00:00
|
|
|
struct spdk_vhost_blk_dev *bvdev = bvsession->bvdev;
|
2017-05-24 14:48:22 +00:00
|
|
|
const 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;
|
2018-01-08 20:35:53 +00:00
|
|
|
uint32_t payload_len;
|
2019-01-09 02:09:34 +00:00
|
|
|
uint64_t flush_bytes;
|
2017-05-24 14:48:22 +00:00
|
|
|
int rc;
|
|
|
|
|
2018-12-16 00:15:06 +00:00
|
|
|
if (blk_iovs_setup(bvsession, vq, task->req_idx, task->iovs, &task->iovcnt, &payload_len)) {
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK, "Invalid request (req_idx = %"PRIu16").\n", task->req_idx);
|
2017-05-24 14:48:22 +00:00
|
|
|
/* Only READ and WRITE are supported for now. */
|
2017-07-10 19:19:56 +00:00
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_UNSUPP);
|
|
|
|
return -1;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iov = &task->iovs[0];
|
|
|
|
if (spdk_unlikely(iov->iov_len != sizeof(*req))) {
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK,
|
2017-05-24 14:48:22 +00:00
|
|
|
"First descriptor size is %zu but expected %zu (req_idx = %"PRIu16").\n",
|
2017-10-10 20:19:40 +00:00
|
|
|
iov->iov_len, sizeof(*req), task->req_idx);
|
2017-07-10 19:19:56 +00:00
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_UNSUPP);
|
|
|
|
return -1;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req = iov->iov_base;
|
|
|
|
|
|
|
|
iov = &task->iovs[task->iovcnt - 1];
|
|
|
|
if (spdk_unlikely(iov->iov_len != 1)) {
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK,
|
2017-05-24 14:48:22 +00:00
|
|
|
"Last descriptor size is %zu but expected %d (req_idx = %"PRIu16").\n",
|
2017-10-10 20:19:40 +00:00
|
|
|
iov->iov_len, 1, task->req_idx);
|
2017-07-10 19:19:56 +00:00
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_UNSUPP);
|
|
|
|
return -1;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
task->status = iov->iov_base;
|
2018-01-08 20:35:53 +00:00
|
|
|
payload_len -= sizeof(*req) + sizeof(*task->status);
|
2017-05-24 14:48:22 +00:00
|
|
|
task->iovcnt -= 2;
|
|
|
|
|
|
|
|
type = req->type;
|
|
|
|
#ifdef VIRTIO_BLK_T_BARRIER
|
|
|
|
/* Don't care about barier for now (as QEMU's virtio-blk do). */
|
|
|
|
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)) {
|
2017-05-24 14:48:22 +00:00
|
|
|
SPDK_ERRLOG("%s - passed IO buffer is not multiple of 512b (req_idx = %"PRIu16").\n",
|
2017-10-10 20:19:40 +00:00
|
|
|
type ? "WRITE" : "READ", task->req_idx);
|
2017-07-10 19:19:56 +00:00
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_UNSUPP);
|
|
|
|
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);
|
2019-01-06 23:15:16 +00:00
|
|
|
rc = spdk_bdev_readv(bvdev->bdev_desc, bvsession->io_channel,
|
2017-07-10 19:19:56 +00:00
|
|
|
&task->iovs[1], task->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);
|
2019-01-06 23:15:16 +00:00
|
|
|
rc = spdk_bdev_writev(bvdev->bdev_desc, bvsession->io_channel,
|
2017-07-10 19:19:56 +00:00
|
|
|
&task->iovs[1], task->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 {
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_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) {
|
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK, "No memory, start to queue io.\n");
|
|
|
|
blk_request_queue_io(task);
|
|
|
|
} else {
|
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_IOERR);
|
|
|
|
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);
|
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_IOERR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-01-06 23:15:16 +00:00
|
|
|
rc = spdk_bdev_unmap(bvdev->bdev_desc, bvsession->io_channel,
|
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) {
|
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK, "No memory, start to queue io.\n");
|
|
|
|
blk_request_queue_io(task);
|
|
|
|
} else {
|
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_IOERR);
|
|
|
|
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);
|
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_IOERR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Zeroed and Unmap the range, SPDK doen't support it. */
|
|
|
|
if (desc->flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) {
|
|
|
|
SPDK_NOTICELOG("Can't support Write Zeroes with Unmap flag\n");
|
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_UNSUPP);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-01-06 23:15:16 +00:00
|
|
|
rc = spdk_bdev_write_zeroes(bvdev->bdev_desc, bvsession->io_channel,
|
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) {
|
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK, "No memory, start to queue io.\n");
|
|
|
|
blk_request_queue_io(task);
|
|
|
|
} else {
|
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_IOERR);
|
|
|
|
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);
|
|
|
|
if (req->sector != 0) {
|
|
|
|
SPDK_NOTICELOG("sector must be zero for flush command\n");
|
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_IOERR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rc = spdk_bdev_flush(bvdev->bdev_desc, bvsession->io_channel,
|
|
|
|
0, flush_bytes,
|
|
|
|
blk_request_complete_cb, task);
|
|
|
|
if (rc) {
|
|
|
|
if (rc == -ENOMEM) {
|
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK, "No memory, start to queue io.\n");
|
|
|
|
blk_request_queue_io(task);
|
|
|
|
} else {
|
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_IOERR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-05-24 14:48:22 +00:00
|
|
|
case VIRTIO_BLK_T_GET_ID:
|
2018-01-08 20:35:53 +00:00
|
|
|
if (!task->iovcnt || !payload_len) {
|
2017-07-10 19:19:56 +00:00
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_UNSUPP);
|
|
|
|
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);
|
|
|
|
spdk_strcpy_pad(task->iovs[1].iov_base, spdk_bdev_get_product_name(bvdev->bdev),
|
|
|
|
task->used_len, ' ');
|
2017-05-24 14:48:22 +00:00
|
|
|
blk_request_finish(true, task);
|
|
|
|
break;
|
|
|
|
default:
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK, "Not supported request type '%"PRIu32"'.\n", type);
|
2017-07-10 19:19:56 +00:00
|
|
|
invalid_blk_request(task, VIRTIO_BLK_S_UNSUPP);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
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-16 00:15:06 +00:00
|
|
|
struct spdk_vhost_blk_dev *bvdev = bvsession->bvdev;
|
2017-10-10 20:19:40 +00:00
|
|
|
struct spdk_vhost_blk_task *task;
|
2018-12-17 15:33:30 +00:00
|
|
|
struct spdk_vhost_session *vsession = &bvsession->vsession;
|
2017-07-10 19:19:56 +00:00
|
|
|
int rc;
|
2017-05-24 14:48:22 +00:00
|
|
|
uint16_t reqs[32];
|
|
|
|
uint16_t reqs_cnt, i;
|
|
|
|
|
|
|
|
reqs_cnt = spdk_vhost_vq_avail_ring_get(vq, reqs, SPDK_COUNTOF(reqs));
|
|
|
|
if (!reqs_cnt) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < reqs_cnt; i++) {
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_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",
|
|
|
|
bvdev->vdev.name, reqs[i], vq->vring.size);
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_vq_used_ring_enqueue(vsession, vq, reqs[i], 0);
|
2017-10-10 20:19:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
task = &((struct spdk_vhost_blk_task *)vq->tasks)[reqs[i]];
|
|
|
|
if (spdk_unlikely(task->used)) {
|
|
|
|
SPDK_ERRLOG("%s: request with idx '%"PRIu16"' is already pending.\n",
|
|
|
|
bvdev->vdev.name, reqs[i]);
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_vq_used_ring_enqueue(vsession, vq, reqs[i], 0);
|
2017-10-10 20:19:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
vsession->task_cnt++;
|
2017-10-10 20:19:40 +00:00
|
|
|
|
|
|
|
task->used = true;
|
|
|
|
task->iovcnt = SPDK_COUNTOF(task->iovs);
|
|
|
|
task->status = NULL;
|
2018-01-08 20:35:53 +00:00
|
|
|
task->used_len = 0;
|
2017-10-10 20:19:40 +00:00
|
|
|
|
2018-12-16 00:15:06 +00:00
|
|
|
rc = process_blk_request(task, bvsession, vq);
|
2017-07-10 19:19:56 +00:00
|
|
|
if (rc == 0) {
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK, "====== Task %p req_idx %d submitted ======\n", task,
|
2017-07-10 19:19:56 +00:00
|
|
|
reqs[i]);
|
|
|
|
} else {
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK, "====== Task %p req_idx %d failed ======\n", task, reqs[i]);
|
2017-07-10 19:19:56 +00:00
|
|
|
}
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
for (q_idx = 0; q_idx < vsession->max_queues; q_idx++) {
|
2018-12-16 00:15:06 +00:00
|
|
|
process_vq(bvsession, &vsession->virtqueue[q_idx]);
|
2017-08-08 16:52:33 +00:00
|
|
|
}
|
2017-09-11 17:45:56 +00:00
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_session_used_signal(vsession);
|
2018-03-13 00:16:47 +00:00
|
|
|
|
|
|
|
return -1;
|
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;
|
|
|
|
|
|
|
|
if (spdk_vhost_vq_avail_ring_get(vq, &req_idx, 1) != 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
iovcnt = SPDK_COUNTOF(iovs);
|
2018-12-16 00:15:06 +00:00
|
|
|
if (blk_iovs_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;
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_BLK_DATA, "Aborting request %" PRIu16"\n", req_idx);
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_vq_used_ring_enqueue(vsession, vq, req_idx, 0);
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 00:16:47 +00:00
|
|
|
static int
|
2017-08-08 16:52:33 +00:00
|
|
|
no_bdev_vdev_worker(void *arg)
|
|
|
|
{
|
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;
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
for (q_idx = 0; q_idx < vsession->max_queues; q_idx++) {
|
2018-12-16 00:15:06 +00:00
|
|
|
no_bdev_process_vq(bvsession, &vsession->virtqueue[q_idx]);
|
2017-08-08 16:52:33 +00:00
|
|
|
}
|
2017-09-11 17:45:56 +00:00
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_session_used_signal(vsession);
|
2018-03-13 00:16:47 +00:00
|
|
|
|
2019-01-06 23:15:16 +00:00
|
|
|
if (vsession->task_cnt == 0 && bvsession->io_channel) {
|
|
|
|
spdk_put_io_channel(bvsession->io_channel);
|
|
|
|
bvsession->io_channel = NULL;
|
2018-08-14 10:49:10 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 00:16:47 +00:00
|
|
|
return -1;
|
2017-08-08 16:52:33 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 12:51:19 +00:00
|
|
|
static struct spdk_vhost_blk_session *
|
|
|
|
to_blk_session(struct spdk_vhost_session *vsession)
|
|
|
|
{
|
|
|
|
if (vsession == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vsession->vdev->backend != &vhost_blk_device_backend) {
|
|
|
|
SPDK_ERRLOG("%s: not a vhost-blk device\n", vsession->vdev->name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (struct spdk_vhost_blk_session *)vsession;
|
|
|
|
}
|
|
|
|
|
2017-05-24 14:48:22 +00:00
|
|
|
static struct spdk_vhost_blk_dev *
|
|
|
|
to_blk_dev(struct spdk_vhost_dev *vdev)
|
|
|
|
{
|
|
|
|
if (vdev == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-01-26 14:29:20 +00:00
|
|
|
if (vdev->backend != &vhost_blk_device_backend) {
|
|
|
|
SPDK_ERRLOG("%s: not a vhost-blk device\n", vdev->name);
|
2017-05-24 14:48:22 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SPDK_CONTAINEROF(vdev, struct spdk_vhost_blk_dev, vdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct spdk_bdev *
|
|
|
|
spdk_vhost_blk_get_dev(struct spdk_vhost_dev *vdev)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_blk_dev *bvdev = to_blk_dev(vdev);
|
|
|
|
|
|
|
|
assert(bvdev != NULL);
|
|
|
|
return bvdev->bdev;
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:00:26 +00:00
|
|
|
static int
|
2018-12-17 12:45:36 +00:00
|
|
|
_spdk_vhost_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;
|
2017-07-10 19:25:22 +00:00
|
|
|
|
2018-12-17 12:45:36 +00:00
|
|
|
if (vdev == NULL) {
|
|
|
|
/* Nothing to do */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vsession == NULL) {
|
|
|
|
/* All sessions have been notified, time to close the bdev */
|
|
|
|
struct spdk_vhost_blk_dev *bvdev = to_blk_dev(vdev);
|
|
|
|
|
2019-01-25 11:55:37 +00:00
|
|
|
assert(bvdev != NULL);
|
|
|
|
|
2018-12-17 12:45:36 +00:00
|
|
|
spdk_bdev_close(bvdev->bdev_desc);
|
|
|
|
bvdev->bdev_desc = NULL;
|
|
|
|
bvdev->bdev = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bvsession = (struct spdk_vhost_blk_session *)vsession;
|
|
|
|
if (bvsession->requestq_poller) {
|
2018-12-14 12:51:19 +00:00
|
|
|
spdk_poller_unregister(&bvsession->requestq_poller);
|
2018-12-16 00:15:06 +00:00
|
|
|
bvsession->requestq_poller = spdk_poller_register(no_bdev_vdev_worker, bvsession, 0);
|
2017-07-10 19:25:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 12:45:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:00:26 +00:00
|
|
|
static void
|
|
|
|
bdev_remove_cb(void *remove_ctx)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_blk_dev *bvdev = remove_ctx;
|
|
|
|
|
2019-01-13 17:24:52 +00:00
|
|
|
SPDK_WARNLOG("Controller %s: Hot-removing bdev - all further requests will fail.\n",
|
|
|
|
bvdev->vdev.name);
|
|
|
|
|
|
|
|
spdk_vhost_lock();
|
|
|
|
spdk_vhost_dev_foreach_session(&bvdev->vdev, _spdk_vhost_session_bdev_remove_cb, NULL);
|
|
|
|
spdk_vhost_unlock();
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
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
|
2018-12-14 12:51:19 +00:00
|
|
|
alloc_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;
|
|
|
|
struct spdk_vhost_blk_dev *bvdev = bvsession->bvdev;
|
2017-10-10 20:19:40 +00:00
|
|
|
struct spdk_vhost_virtqueue *vq;
|
2017-08-08 16:52:33 +00:00
|
|
|
struct spdk_vhost_blk_task *task;
|
2017-10-10 20:19:40 +00:00
|
|
|
uint32_t task_cnt;
|
2017-08-08 16:52:33 +00:00
|
|
|
uint16_t i;
|
2017-10-10 20:19:40 +00:00
|
|
|
uint32_t j;
|
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];
|
2018-04-23 11:04:18 +00:00
|
|
|
if (vq->vring.desc == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-10 20:19:40 +00:00
|
|
|
task_cnt = vq->vring.size;
|
|
|
|
if (task_cnt > SPDK_VHOST_MAX_VQ_SIZE) {
|
|
|
|
/* sanity check */
|
|
|
|
SPDK_ERRLOG("Controller %s: virtuque %"PRIu16" is too big. (size = %"PRIu32", max = %"PRIu32")\n",
|
|
|
|
bvdev->vdev.name, i, task_cnt, SPDK_VHOST_MAX_VQ_SIZE);
|
2018-12-14 12:51:19 +00:00
|
|
|
free_task_pool(bvsession);
|
2017-08-08 16:52:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2019-03-15 14:35:49 +00:00
|
|
|
vq->tasks = spdk_zmalloc(sizeof(struct spdk_vhost_blk_task) * task_cnt,
|
|
|
|
SPDK_CACHE_LINE_SIZE, NULL,
|
|
|
|
SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
|
2017-10-10 20:19:40 +00:00
|
|
|
if (vq->tasks == NULL) {
|
|
|
|
SPDK_ERRLOG("Controller %s: failed to allocate %"PRIu32" tasks for virtqueue %"PRIu16"\n",
|
|
|
|
bvdev->vdev.name, task_cnt, i);
|
2018-12-14 12:51:19 +00:00
|
|
|
free_task_pool(bvsession);
|
2017-08-08 16:52:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-10-10 20:19:40 +00:00
|
|
|
|
|
|
|
for (j = 0; j < task_cnt; j++) {
|
|
|
|
task = &((struct spdk_vhost_blk_task *)vq->tasks)[j];
|
2018-12-16 00:15:06 +00:00
|
|
|
task->bvsession = bvsession;
|
2017-10-10 20:19:40 +00:00
|
|
|
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
|
2019-01-22 11:57:09 +00:00
|
|
|
spdk_vhost_blk_start_cb(struct spdk_vhost_dev *vdev,
|
2019-03-16 23:00:57 +00:00
|
|
|
struct spdk_vhost_session *vsession, void *unused)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
2017-08-01 13:46:51 +00:00
|
|
|
struct spdk_vhost_blk_dev *bvdev;
|
2018-12-14 12:51:19 +00:00
|
|
|
struct spdk_vhost_blk_session *bvsession;
|
2018-04-23 11:04:18 +00:00
|
|
|
int i, rc = 0;
|
2017-05-24 14:48:22 +00:00
|
|
|
|
2018-12-14 12:51:19 +00:00
|
|
|
bvsession = to_blk_session(vsession);
|
|
|
|
if (bvsession == NULL) {
|
2017-08-01 13:46:51 +00:00
|
|
|
SPDK_ERRLOG("Trying to start non-blk controller as a blk one.\n");
|
2017-08-04 10:02:03 +00:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 12:51:19 +00:00
|
|
|
bvdev = to_blk_dev(vdev);
|
2019-01-25 11:55:37 +00:00
|
|
|
assert(bvdev != NULL);
|
2018-12-14 12:51:19 +00:00
|
|
|
bvsession->bvdev = bvdev;
|
|
|
|
|
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++) {
|
|
|
|
if (vsession->virtqueue[i].vring.desc == NULL) {
|
2018-04-23 11:04:18 +00:00
|
|
|
SPDK_ERRLOG("%s: queue %"PRIu32" is empty\n", vdev->name, i);
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 12:51:19 +00:00
|
|
|
rc = alloc_task_pool(bvsession);
|
2017-08-08 16:52:33 +00:00
|
|
|
if (rc != 0) {
|
2017-08-18 13:22:25 +00:00
|
|
|
SPDK_ERRLOG("%s: failed to alloc task pool.\n", bvdev->vdev.name);
|
2017-08-04 10:02:03 +00:00
|
|
|
goto out;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 10:02:03 +00:00
|
|
|
if (bvdev->bdev) {
|
2019-01-06 23:15:16 +00:00
|
|
|
bvsession->io_channel = spdk_bdev_get_io_channel(bvdev->bdev_desc);
|
|
|
|
if (!bvsession->io_channel) {
|
2018-12-14 12:51:19 +00:00
|
|
|
free_task_pool(bvsession);
|
2017-08-04 10:02:03 +00:00
|
|
|
SPDK_ERRLOG("Controller %s: IO channel allocation failed\n", vdev->name);
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 12:51:19 +00:00
|
|
|
bvsession->requestq_poller = spdk_poller_register(bvdev->bdev ? vdev_worker : no_bdev_vdev_worker,
|
2018-12-16 00:15:06 +00:00
|
|
|
bvsession, 0);
|
2018-03-30 21:00:56 +00:00
|
|
|
SPDK_INFOLOG(SPDK_LOG_VHOST, "Started poller for vhost controller %s on lcore %d\n",
|
2019-03-17 11:13:01 +00:00
|
|
|
vdev->name, spdk_env_get_current_core());
|
2017-08-04 10:02:03 +00:00
|
|
|
out:
|
2019-03-17 12:08:25 +00:00
|
|
|
spdk_vhost_session_start_done(vsession, rc);
|
2017-08-04 10:02:03 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-01-22 11:57:09 +00:00
|
|
|
static int
|
|
|
|
spdk_vhost_blk_start(struct spdk_vhost_session *vsession)
|
|
|
|
{
|
2019-03-17 11:13:01 +00:00
|
|
|
int32_t lcore;
|
2019-01-22 15:25:35 +00:00
|
|
|
int rc;
|
|
|
|
|
2019-03-17 11:13:01 +00:00
|
|
|
lcore = spdk_vhost_allocate_reactor(vsession->vdev->cpumask);
|
|
|
|
rc = spdk_vhost_session_send_event(lcore, vsession, spdk_vhost_blk_start_cb,
|
2019-01-22 15:25:35 +00:00
|
|
|
3, "start session");
|
|
|
|
|
|
|
|
if (rc != 0) {
|
2019-03-17 11:13:01 +00:00
|
|
|
spdk_vhost_free_reactor(lcore);
|
2019-01-22 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2019-01-22 11:57:09 +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;
|
2017-09-11 17:45:56 +00:00
|
|
|
int i;
|
2017-08-04 10:02:03 +00:00
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
if (vsession->task_cnt > 0) {
|
2018-03-13 00:16:47 +00:00
|
|
|
return -1;
|
2017-08-04 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-16 23:50:51 +00:00
|
|
|
if (spdk_vhost_trylock() != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
for (i = 0; i < vsession->max_queues; i++) {
|
|
|
|
vsession->virtqueue[i].next_event_time = 0;
|
|
|
|
spdk_vhost_vq_used_signal(vsession, &vsession->virtqueue[i]);
|
2017-09-11 17:45:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 12:51:19 +00:00
|
|
|
SPDK_INFOLOG(SPDK_LOG_VHOST, "Stopping poller for vhost controller %s\n", vsession->vdev->name);
|
2017-08-04 10:02:03 +00:00
|
|
|
|
2019-01-06 23:15:16 +00:00
|
|
|
if (bvsession->io_channel) {
|
|
|
|
spdk_put_io_channel(bvsession->io_channel);
|
|
|
|
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);
|
2019-03-17 12:08:25 +00:00
|
|
|
spdk_vhost_session_stop_done(vsession, 0);
|
2018-03-13 00:16:47 +00:00
|
|
|
|
2019-03-16 23:50:51 +00:00
|
|
|
spdk_vhost_unlock();
|
2018-03-13 00:16:47 +00:00
|
|
|
return -1;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 13:46:51 +00:00
|
|
|
static int
|
2019-01-22 11:57:09 +00:00
|
|
|
spdk_vhost_blk_stop_cb(struct spdk_vhost_dev *vdev,
|
2019-03-16 23:00:57 +00:00
|
|
|
struct spdk_vhost_session *vsession, void *unused)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
2018-12-14 12:51:19 +00:00
|
|
|
struct spdk_vhost_blk_session *bvsession;
|
2017-05-24 14:48:22 +00:00
|
|
|
|
2018-12-14 12:51:19 +00:00
|
|
|
bvsession = to_blk_session(vsession);
|
|
|
|
if (bvsession == NULL) {
|
2017-08-01 13:46:51 +00:00
|
|
|
SPDK_ERRLOG("Trying to stop non-blk controller as a blk one.\n");
|
2017-08-04 10:02:03 +00:00
|
|
|
goto err;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 12:51:19 +00:00
|
|
|
spdk_poller_unregister(&bvsession->requestq_poller);
|
2019-03-16 23:31:45 +00:00
|
|
|
bvsession->stop_poller = spdk_poller_register(destroy_session_poller_cb,
|
|
|
|
bvsession, 1000);
|
2017-08-01 13:46:51 +00:00
|
|
|
return 0;
|
2017-08-04 10:02:03 +00:00
|
|
|
|
|
|
|
err:
|
2019-03-17 12:08:25 +00:00
|
|
|
spdk_vhost_session_stop_done(vsession, -1);
|
2017-08-04 10:02:03 +00:00
|
|
|
return -1;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 11:57:09 +00:00
|
|
|
static int
|
|
|
|
spdk_vhost_blk_stop(struct spdk_vhost_session *vsession)
|
|
|
|
{
|
2019-03-17 11:13:01 +00:00
|
|
|
return spdk_vhost_session_send_event(vsession->lcore, vsession,
|
|
|
|
spdk_vhost_blk_stop_cb, 3, "stop session");
|
2019-01-22 11:57:09 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 14:37:50 +00:00
|
|
|
static void
|
2018-03-19 18:01:57 +00:00
|
|
|
spdk_vhost_blk_dump_info_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w)
|
2017-07-13 14:37:50 +00:00
|
|
|
{
|
|
|
|
struct spdk_bdev *bdev = spdk_vhost_blk_get_dev(vdev);
|
2018-04-18 20:34:49 +00:00
|
|
|
struct spdk_vhost_blk_dev *bvdev;
|
|
|
|
|
|
|
|
bvdev = to_blk_dev(vdev);
|
|
|
|
if (bvdev == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-13 14:37:50 +00:00
|
|
|
|
|
|
|
assert(bvdev != NULL);
|
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");
|
|
|
|
if (bdev) {
|
|
|
|
spdk_json_write_string(w, spdk_bdev_get_name(bdev));
|
|
|
|
} else {
|
|
|
|
spdk_json_write_null(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
spdk_json_write_object_end(w);
|
|
|
|
}
|
|
|
|
|
2018-03-19 18:01:57 +00:00
|
|
|
static void
|
|
|
|
spdk_vhost_blk_write_config_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w)
|
|
|
|
{
|
2018-04-18 20:34:49 +00:00
|
|
|
struct spdk_vhost_blk_dev *bvdev;
|
|
|
|
|
|
|
|
bvdev = to_blk_dev(vdev);
|
|
|
|
if (bvdev == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-19 18:01:57 +00:00
|
|
|
|
|
|
|
if (!bvdev->bdev) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
spdk_json_write_object_begin(w);
|
|
|
|
spdk_json_write_named_string(w, "method", "construct_vhost_blk_controller");
|
|
|
|
|
|
|
|
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));
|
|
|
|
spdk_json_write_named_string(w, "cpumask", spdk_cpuset_fmt(vdev->cpumask));
|
|
|
|
spdk_json_write_named_bool(w, "readonly", bvdev->readonly);
|
|
|
|
spdk_json_write_object_end(w);
|
|
|
|
|
|
|
|
spdk_json_write_object_end(w);
|
|
|
|
}
|
|
|
|
|
2018-01-26 13:09:39 +00:00
|
|
|
static int spdk_vhost_blk_destroy(struct spdk_vhost_dev *dev);
|
|
|
|
|
2018-01-23 01:51:56 +00:00
|
|
|
static int
|
|
|
|
spdk_vhost_blk_get_config(struct spdk_vhost_dev *vdev, uint8_t *config,
|
|
|
|
uint32_t len)
|
|
|
|
{
|
2018-03-02 01:42:56 +00:00
|
|
|
struct virtio_blk_config blkcfg;
|
2018-01-23 01:51:56 +00:00
|
|
|
struct spdk_vhost_blk_dev *bvdev;
|
|
|
|
struct spdk_bdev *bdev;
|
|
|
|
uint32_t blk_size;
|
|
|
|
uint64_t blkcnt;
|
|
|
|
|
|
|
|
bvdev = to_blk_dev(vdev);
|
|
|
|
if (bvdev == NULL) {
|
|
|
|
SPDK_ERRLOG("Trying to get virito_blk configuration failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bdev = bvdev->bdev;
|
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);
|
|
|
|
}
|
2018-01-23 01:51:56 +00:00
|
|
|
|
2018-03-02 01:42:56 +00:00
|
|
|
memset(&blkcfg, 0, sizeof(blkcfg));
|
|
|
|
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;
|
|
|
|
blkcfg.size_max = 131072;
|
2018-01-23 01:51:56 +00:00
|
|
|
/* -2 for REQ and RESP and -1 for region boundary splitting */
|
2018-03-02 01:42:56 +00:00
|
|
|
blkcfg.seg_max = SPDK_VHOST_IOVS_MAX - 2 - 1;
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-24 14:48:22 +00:00
|
|
|
static const struct spdk_vhost_dev_backend vhost_blk_device_backend = {
|
2017-08-09 16:59:53 +00:00
|
|
|
.virtio_features = SPDK_VHOST_FEATURES |
|
2017-05-24 14:48:22 +00:00
|
|
|
(1ULL << VIRTIO_BLK_F_SIZE_MAX) | (1ULL << VIRTIO_BLK_F_SEG_MAX) |
|
|
|
|
(1ULL << VIRTIO_BLK_F_GEOMETRY) | (1ULL << VIRTIO_BLK_F_RO) |
|
|
|
|
(1ULL << VIRTIO_BLK_F_BLK_SIZE) | (1ULL << VIRTIO_BLK_F_TOPOLOGY) |
|
|
|
|
(1ULL << VIRTIO_BLK_F_BARRIER) | (1ULL << VIRTIO_BLK_F_SCSI) |
|
2017-08-08 16:52:33 +00:00
|
|
|
(1ULL << VIRTIO_BLK_F_FLUSH) | (1ULL << VIRTIO_BLK_F_CONFIG_WCE) |
|
2018-03-02 01:42:56 +00:00
|
|
|
(1ULL << VIRTIO_BLK_F_MQ) | (1ULL << VIRTIO_BLK_F_DISCARD) |
|
|
|
|
(1ULL << VIRTIO_BLK_F_WRITE_ZEROES),
|
2017-08-09 16:59:53 +00:00
|
|
|
.disabled_features = SPDK_VHOST_DISABLED_FEATURES | (1ULL << VIRTIO_BLK_F_GEOMETRY) |
|
2017-05-24 14:48:22 +00:00
|
|
|
(1ULL << VIRTIO_BLK_F_RO) | (1ULL << VIRTIO_BLK_F_FLUSH) | (1ULL << VIRTIO_BLK_F_CONFIG_WCE) |
|
2018-03-02 01:42:56 +00:00
|
|
|
(1ULL << VIRTIO_BLK_F_BARRIER) | (1ULL << VIRTIO_BLK_F_SCSI) | (1ULL << VIRTIO_BLK_F_DISCARD) |
|
|
|
|
(1ULL << VIRTIO_BLK_F_WRITE_ZEROES),
|
2018-12-14 12:51:19 +00:00
|
|
|
.session_ctx_size = sizeof(struct spdk_vhost_blk_session) - sizeof(struct spdk_vhost_session),
|
2019-01-07 23:30:57 +00:00
|
|
|
.start_session = spdk_vhost_blk_start,
|
|
|
|
.stop_session = spdk_vhost_blk_stop,
|
2018-01-23 01:51:56 +00:00
|
|
|
.vhost_get_config = spdk_vhost_blk_get_config,
|
2018-03-19 18:01:57 +00:00
|
|
|
.dump_info_json = spdk_vhost_blk_dump_info_json,
|
|
|
|
.write_config_json = spdk_vhost_blk_write_config_json,
|
2018-01-26 13:09:39 +00:00
|
|
|
.remove_device = spdk_vhost_blk_destroy,
|
2017-05-24 14:48:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_vhost_blk_controller_construct(void)
|
|
|
|
{
|
|
|
|
struct spdk_conf_section *sp;
|
|
|
|
unsigned ctrlr_num;
|
|
|
|
char *bdev_name;
|
2017-08-10 18:11:14 +00:00
|
|
|
char *cpumask;
|
2017-05-24 14:48:22 +00:00
|
|
|
char *name;
|
2017-06-19 14:37:51 +00:00
|
|
|
bool readonly;
|
2017-05-24 14:48:22 +00:00
|
|
|
|
|
|
|
for (sp = spdk_conf_first_section(NULL); sp != NULL; sp = spdk_conf_next_section(sp)) {
|
|
|
|
if (!spdk_conf_section_match_prefix(sp, "VhostBlk")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sscanf(spdk_conf_section_get_name(sp), "VhostBlk%u", &ctrlr_num) != 1) {
|
|
|
|
SPDK_ERRLOG("Section '%s' has non-numeric suffix.\n",
|
|
|
|
spdk_conf_section_get_name(sp));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = spdk_conf_section_get_val(sp, "Name");
|
|
|
|
if (name == NULL) {
|
|
|
|
SPDK_ERRLOG("VhostBlk%u: missing Name\n", ctrlr_num);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-08-10 18:11:14 +00:00
|
|
|
cpumask = spdk_conf_section_get_val(sp, "Cpumask");
|
2017-06-19 14:37:51 +00:00
|
|
|
readonly = spdk_conf_section_get_boolval(sp, "ReadOnly", false);
|
2017-05-24 14:48:22 +00:00
|
|
|
|
|
|
|
bdev_name = spdk_conf_section_get_val(sp, "Dev");
|
|
|
|
if (bdev_name == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-19 14:37:51 +00:00
|
|
|
if (spdk_vhost_blk_construct(name, cpumask, bdev_name, readonly) < 0) {
|
2017-05-24 14:48:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-08-10 18:11:14 +00:00
|
|
|
spdk_vhost_blk_construct(const char *name, const char *cpumask, const char *dev_name, bool readonly)
|
2017-05-24 14:48:22 +00:00
|
|
|
{
|
2017-09-08 11:06:41 +00:00
|
|
|
struct spdk_vhost_blk_dev *bvdev = NULL;
|
2017-05-24 14:48:22 +00:00
|
|
|
struct spdk_bdev *bdev;
|
2018-03-02 01:42:56 +00:00
|
|
|
uint64_t features = 0;
|
2017-09-08 11:06:41 +00:00
|
|
|
int ret = 0;
|
2017-05-24 14:48:22 +00:00
|
|
|
|
2017-09-08 11:06:41 +00:00
|
|
|
spdk_vhost_lock();
|
2017-05-24 14:48:22 +00:00
|
|
|
bdev = spdk_bdev_get_by_name(dev_name);
|
|
|
|
if (bdev == NULL) {
|
|
|
|
SPDK_ERRLOG("Controller %s: bdev '%s' not found\n",
|
|
|
|
name, dev_name);
|
2018-08-10 10:04:46 +00:00
|
|
|
ret = -ENODEV;
|
2017-09-08 11:06:41 +00:00
|
|
|
goto out;
|
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
|
|
|
}
|
|
|
|
|
2017-06-29 18:23:50 +00:00
|
|
|
ret = spdk_bdev_open(bdev, true, bdev_remove_cb, bvdev, &bvdev->bdev_desc);
|
|
|
|
if (ret != 0) {
|
|
|
|
SPDK_ERRLOG("Controller %s: could not open bdev '%s', error=%d\n",
|
|
|
|
name, dev_name, ret);
|
2017-09-08 11:06:41 +00:00
|
|
|
goto out;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bvdev->bdev = bdev;
|
2017-06-19 14:37:51 +00:00
|
|
|
bvdev->readonly = readonly;
|
2018-01-26 14:29:20 +00:00
|
|
|
ret = spdk_vhost_dev_register(&bvdev->vdev, name, cpumask, &vhost_blk_device_backend);
|
2017-05-24 14:48:22 +00:00
|
|
|
if (ret != 0) {
|
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
|
|
|
}
|
|
|
|
|
2018-03-02 01:42:56 +00:00
|
|
|
if (spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_UNMAP)) {
|
|
|
|
features |= (1ULL << VIRTIO_BLK_F_DISCARD);
|
|
|
|
}
|
|
|
|
if (spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES)) {
|
|
|
|
features |= (1ULL << VIRTIO_BLK_F_WRITE_ZEROES);
|
|
|
|
}
|
|
|
|
if (readonly) {
|
|
|
|
features |= (1ULL << VIRTIO_BLK_F_RO);
|
|
|
|
}
|
2019-01-09 02:09:34 +00:00
|
|
|
if (spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_FLUSH)) {
|
|
|
|
features |= (1ULL << VIRTIO_BLK_F_FLUSH);
|
|
|
|
}
|
2018-03-02 01:42:56 +00:00
|
|
|
|
|
|
|
if (features && rte_vhost_driver_enable_features(bvdev->vdev.path, features)) {
|
|
|
|
SPDK_ERRLOG("Controller %s: failed to enable features 0x%"PRIx64"\n", name, features);
|
2017-06-19 14:37:51 +00:00
|
|
|
|
2018-01-26 13:09:39 +00:00
|
|
|
if (spdk_vhost_dev_unregister(&bvdev->vdev) != 0) {
|
2017-06-19 14:37:51 +00:00
|
|
|
SPDK_ERRLOG("Controller %s: failed to remove controller\n", name);
|
|
|
|
}
|
|
|
|
|
2018-03-02 01:42:56 +00:00
|
|
|
spdk_bdev_close(bvdev->bdev_desc);
|
2017-09-08 11:06:41 +00:00
|
|
|
ret = -1;
|
|
|
|
goto out;
|
2017-06-19 14:37:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 21:00:56 +00:00
|
|
|
SPDK_INFOLOG(SPDK_LOG_VHOST, "Controller %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
|
|
|
}
|
|
|
|
spdk_vhost_unlock();
|
|
|
|
return ret;
|
2017-05-24 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 13:09:39 +00:00
|
|
|
static int
|
2017-05-24 14:48:22 +00:00
|
|
|
spdk_vhost_blk_destroy(struct spdk_vhost_dev *vdev)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
if (!bvdev) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-01-26 13:09:39 +00:00
|
|
|
rc = spdk_vhost_dev_unregister(&bvdev->vdev);
|
2017-08-01 16:08:32 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_LOG_REGISTER_COMPONENT("vhost_blk", SPDK_LOG_VHOST_BLK)
|
|
|
|
SPDK_LOG_REGISTER_COMPONENT("vhost_blk_data", SPDK_LOG_VHOST_BLK_DATA)
|