1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2019 Intel Corporation
6 * This work is largely based on the "vhost-user-blk" implementation by
7 * SPDK(https://github.com/spdk/spdk).
18 #include <rte_atomic.h>
19 #include <rte_cycles.h>
21 #include <rte_malloc.h>
22 #include <rte_byteorder.h>
23 #include <rte_string_fns.h>
25 #include "vhost_blk.h"
29 vhost_strcpy_pad(void *dst, const char *src, size_t size, int pad)
35 memcpy(dst, src, len);
36 memset((char *)dst + len, pad, size - len);
38 memcpy(dst, src, size);
43 vhost_bdev_blk_readwrite(struct vhost_block_dev *bdev,
44 struct vhost_blk_task *task,
45 uint64_t lba_512, __rte_unused uint32_t xfer_len)
51 offset = lba_512 * 512;
53 /* iovs[0] is the head and iovs[iovs_cnt - 1] is the tail
54 * Middle is the data range
56 for (i = 1; i < task->iovs_cnt - 1; i++) {
57 if (task->dxfer_dir == BLK_DIR_TO_DEV)
58 memcpy(bdev->data + offset, task->iovs[i].iov_base,
59 task->iovs[i].iov_len);
61 memcpy(task->iovs[i].iov_base, bdev->data + offset,
62 task->iovs[i].iov_len);
63 offset += task->iovs[i].iov_len;
64 nbytes += task->iovs[i].iov_len;
71 vhost_bdev_process_blk_commands(struct vhost_block_dev *bdev,
72 struct vhost_blk_task *task)
76 if (unlikely(task->data_len > (bdev->blockcnt * bdev->blocklen))) {
77 fprintf(stderr, "read or write beyond capacity\n");
78 return VIRTIO_BLK_S_UNSUPP;
81 switch (task->req->type) {
83 if (unlikely(task->data_len == 0 ||
84 (task->data_len & (512 - 1)) != 0)) {
86 "%s - passed IO buffer is not multiple of 512b"
87 "(req_idx = %"PRIu16").\n",
88 task->req->type ? "WRITE" : "READ",
90 return VIRTIO_BLK_S_UNSUPP;
93 task->dxfer_dir = BLK_DIR_FROM_DEV;
94 vhost_bdev_blk_readwrite(bdev, task,
95 task->req->sector, task->data_len);
97 case VIRTIO_BLK_T_OUT:
98 if (unlikely(task->data_len == 0 ||
99 (task->data_len & (512 - 1)) != 0)) {
101 "%s - passed IO buffer is not multiple of 512b"
102 "(req_idx = %"PRIu16").\n",
103 task->req->type ? "WRITE" : "READ",
105 return VIRTIO_BLK_S_UNSUPP;
108 task->dxfer_dir = BLK_DIR_TO_DEV;
109 vhost_bdev_blk_readwrite(bdev, task,
110 task->req->sector, task->data_len);
112 case VIRTIO_BLK_T_GET_ID:
113 if (!task->iovs_cnt || task->data_len)
114 return VIRTIO_BLK_S_UNSUPP;
115 used_len = RTE_MIN((size_t)VIRTIO_BLK_ID_BYTES, task->data_len);
116 vhost_strcpy_pad(task->iovs[0].iov_base,
117 bdev->product_name, used_len, ' ');
120 fprintf(stderr, "unsupported cmd\n");
121 return VIRTIO_BLK_S_UNSUPP;
124 return VIRTIO_BLK_S_OK;