4 * Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <semaphore.h>
40 #include <linux/virtio_scsi.h>
41 #include <linux/virtio_ring.h>
43 #include <rte_atomic.h>
44 #include <rte_cycles.h>
46 #include <rte_malloc.h>
47 #include <rte_vhost.h>
49 #include "vhost_scsi.h"
50 #include "scsi_spec.h"
52 #define VIRTIO_SCSI_FEATURES ((1 << VIRTIO_F_NOTIFY_ON_EMPTY) |\
53 (1 << VIRTIO_RING_F_EVENT_IDX) |\
54 (1 << VIRTIO_SCSI_F_INOUT) |\
55 (1 << VIRTIO_SCSI_F_CHANGE))
57 /* Path to folder where character device will be created. Can be set by user. */
58 static char dev_pathname[PATH_MAX] = "";
60 static struct vhost_scsi_ctrlr *g_vhost_ctrlr;
61 static int g_should_stop;
62 static sem_t exit_sem;
64 static struct vhost_scsi_ctrlr *
65 vhost_scsi_ctrlr_find(__rte_unused const char *ctrlr_name)
67 /* currently we only support 1 socket file fd */
71 static uint64_t gpa_to_vva(int vid, uint64_t gpa)
74 struct vhost_scsi_ctrlr *ctrlr;
77 ret = rte_vhost_get_ifname(vid, path, PATH_MAX);
79 fprintf(stderr, "Cannot get socket name\n");
83 ctrlr = vhost_scsi_ctrlr_find(path);
85 fprintf(stderr, "Controller is not ready\n");
86 assert(ctrlr != NULL);
89 assert(ctrlr->mem != NULL);
91 return rte_vhost_gpa_to_vva(ctrlr->mem, gpa);
94 static struct vring_desc *
95 descriptor_get_next(struct vring_desc *vq_desc, struct vring_desc *cur_desc)
97 return &vq_desc[cur_desc->next];
101 descriptor_has_next(struct vring_desc *cur_desc)
103 return !!(cur_desc->flags & VRING_DESC_F_NEXT);
107 descriptor_is_wr(struct vring_desc *cur_desc)
109 return !!(cur_desc->flags & VRING_DESC_F_WRITE);
113 submit_completion(struct vhost_scsi_task *task)
115 struct rte_vhost_vring *vq;
116 struct vring_used *used;
120 /* Fill out the next entry in the "used" ring. id = the
121 * index of the descriptor that contained the SCSI request.
122 * len = the total amount of data transferred for the SCSI
123 * request. We must report the correct len, for variable
124 * length SCSI CDBs, where we may return less data than
125 * allocated by the guest VM.
127 used->ring[used->idx & (vq->size - 1)].id = task->req_idx;
128 used->ring[used->idx & (vq->size - 1)].len = task->data_len;
131 /* Send an interrupt back to the guest VM so that it knows
132 * a completion is ready to be processed.
134 eventfd_write(vq->callfd, (eventfd_t)1);
138 vhost_process_read_payload_chain(struct vhost_scsi_task *task)
143 task->resp = (void *)(uintptr_t)gpa_to_vva(task->bdev->vid,
146 while (descriptor_has_next(task->desc)) {
147 task->desc = descriptor_get_next(task->vq->desc, task->desc);
148 data = (void *)(uintptr_t)gpa_to_vva(task->bdev->vid,
150 task->iovs[task->iovs_cnt].iov_base = data;
151 task->iovs[task->iovs_cnt].iov_len = task->desc->len;
152 task->data_len += task->desc->len;
158 vhost_process_write_payload_chain(struct vhost_scsi_task *task)
165 data = (void *)(uintptr_t)gpa_to_vva(task->bdev->vid,
167 task->iovs[task->iovs_cnt].iov_base = data;
168 task->iovs[task->iovs_cnt].iov_len = task->desc->len;
169 task->data_len += task->desc->len;
171 task->desc = descriptor_get_next(task->vq->desc, task->desc);
172 } while (descriptor_has_next(task->desc));
174 task->resp = (void *)(uintptr_t)gpa_to_vva(task->bdev->vid,
178 static struct vhost_block_dev *
179 vhost_scsi_bdev_construct(const char *bdev_name, const char *bdev_serial,
180 uint32_t blk_size, uint64_t blk_cnt,
183 struct vhost_block_dev *bdev;
185 bdev = rte_zmalloc(NULL, sizeof(*bdev), RTE_CACHE_LINE_SIZE);
189 strncpy(bdev->name, bdev_name, sizeof(bdev->name));
190 strncpy(bdev->product_name, bdev_serial, sizeof(bdev->product_name));
191 bdev->blocklen = blk_size;
192 bdev->blockcnt = blk_cnt;
193 bdev->write_cache = wce_enable;
195 /* use memory as disk storage space */
196 bdev->data = rte_zmalloc(NULL, blk_cnt * blk_size, 0);
198 fprintf(stderr, "no enough reseverd huge memory for disk\n");
206 process_requestq(struct vhost_scsi_ctrlr *ctrlr, uint32_t q_idx)
209 struct vhost_scsi_queue *scsi_vq;
210 struct rte_vhost_vring *vq;
212 scsi_vq = &ctrlr->bdev->queues[q_idx];
214 ret = rte_vhost_get_vhost_vring(ctrlr->bdev->vid, q_idx, vq);
217 while (vq->avail->idx != scsi_vq->last_used_idx) {
220 struct vhost_scsi_task *task;
222 last_idx = scsi_vq->last_used_idx & (vq->size - 1);
223 req_idx = vq->avail->ring[last_idx];
225 task = rte_zmalloc(NULL, sizeof(*task), 0);
226 assert(task != NULL);
229 task->bdev = ctrlr->bdev;
231 task->req_idx = req_idx;
232 task->desc = &task->vq->desc[task->req_idx];
234 /* does not support indirect descriptors */
235 assert((task->desc->flags & VRING_DESC_F_INDIRECT) == 0);
236 scsi_vq->last_used_idx++;
238 task->req = (void *)(uintptr_t)gpa_to_vva(task->bdev->vid,
241 task->desc = descriptor_get_next(task->vq->desc, task->desc);
242 if (!descriptor_has_next(task->desc)) {
243 task->dxfer_dir = SCSI_DIR_NONE;
244 task->resp = (void *)(uintptr_t)
245 gpa_to_vva(task->bdev->vid,
248 } else if (!descriptor_is_wr(task->desc)) {
249 task->dxfer_dir = SCSI_DIR_TO_DEV;
250 vhost_process_write_payload_chain(task);
252 task->dxfer_dir = SCSI_DIR_FROM_DEV;
253 vhost_process_read_payload_chain(task);
256 ret = vhost_bdev_process_scsi_commands(ctrlr->bdev, task);
258 /* invalid response */
259 task->resp->response = VIRTIO_SCSI_S_BAD_TARGET;
262 task->resp->response = VIRTIO_SCSI_S_OK;
263 task->resp->status = 0;
264 task->resp->resid = 0;
266 submit_completion(task);
271 /* Main framework for processing IOs */
273 ctrlr_worker(void *arg)
276 struct vhost_scsi_ctrlr *ctrlr = (struct vhost_scsi_ctrlr *)arg;
280 thread = pthread_self();
283 pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
285 num = rte_vhost_get_vring_num(ctrlr->bdev->vid);
286 fprintf(stdout, "Ctrlr Worker Thread Started with %u Vring\n", num);
288 if (num != NUM_OF_SCSI_QUEUES) {
289 fprintf(stderr, "Only 1 IO queue are supported\n");
293 while (!g_should_stop && ctrlr->bdev != NULL) {
294 /* At least 3 vrings, currently only can support 1 IO queue
295 * Queue 2 for IO queue, does not support TMF and hotplug
296 * for the example application now
298 for (idx = 2; idx < num; idx++)
299 process_requestq(ctrlr, idx);
302 fprintf(stdout, "Ctrlr Worker Thread Exiting\n");
311 struct vhost_scsi_ctrlr *ctrlr;
312 struct vhost_scsi_queue *scsi_vq;
313 struct rte_vhost_vring *vq;
317 ret = rte_vhost_get_ifname(vid, path, PATH_MAX);
319 fprintf(stderr, "Cannot get socket name\n");
323 ctrlr = vhost_scsi_ctrlr_find(path);
325 fprintf(stderr, "Controller is not ready\n");
329 ret = rte_vhost_get_mem_table(vid, &ctrlr->mem);
331 fprintf(stderr, "Get Controller memory region failed\n");
334 assert(ctrlr->mem != NULL);
336 /* hardcoded block device information with 128MiB */
337 ctrlr->bdev = vhost_scsi_bdev_construct("malloc0", "vhost_scsi_malloc0",
342 ctrlr->bdev->vid = vid;
344 /* Disable Notifications */
345 for (i = 0; i < NUM_OF_SCSI_QUEUES; i++) {
346 rte_vhost_enable_guest_notification(vid, i, 0);
347 /* restore used index */
348 scsi_vq = &ctrlr->bdev->queues[i];
350 ret = rte_vhost_get_vhost_vring(ctrlr->bdev->vid, i, vq);
352 scsi_vq->last_used_idx = vq->used->idx;
353 scsi_vq->last_avail_idx = vq->used->idx;
357 fprintf(stdout, "New Device %s, Device ID %d\n", path, vid);
358 if (pthread_create(&tid, NULL, &ctrlr_worker, ctrlr) < 0) {
359 fprintf(stderr, "Worker Thread Started Failed\n");
367 destroy_device(int vid)
370 struct vhost_scsi_ctrlr *ctrlr;
372 rte_vhost_get_ifname(vid, path, PATH_MAX);
373 fprintf(stdout, "Destroy %s Device ID %d\n", path, vid);
374 ctrlr = vhost_scsi_ctrlr_find(path);
376 fprintf(stderr, "Destroy Ctrlr Failed\n");
385 static const struct vhost_device_ops vhost_scsi_device_ops = {
386 .new_device = new_device,
387 .destroy_device = destroy_device,
390 static struct vhost_scsi_ctrlr *
391 vhost_scsi_ctrlr_construct(const char *ctrlr_name)
394 struct vhost_scsi_ctrlr *ctrlr;
398 /* always use current directory */
399 path = getcwd(cwd, PATH_MAX);
401 fprintf(stderr, "Cannot get current working directory\n");
404 snprintf(dev_pathname, sizeof(dev_pathname), "%s/%s", path, ctrlr_name);
406 if (access(dev_pathname, F_OK) != -1) {
407 if (unlink(dev_pathname) != 0)
408 rte_exit(EXIT_FAILURE, "Cannot remove %s.\n",
412 if (rte_vhost_driver_register(dev_pathname, 0) != 0) {
413 fprintf(stderr, "socket %s already exists\n", dev_pathname);
417 fprintf(stdout, "socket file: %s created\n", dev_pathname);
419 ret = rte_vhost_driver_set_features(dev_pathname, VIRTIO_SCSI_FEATURES);
421 fprintf(stderr, "Set vhost driver features failed\n");
425 ctrlr = rte_zmalloc(NULL, sizeof(*ctrlr), RTE_CACHE_LINE_SIZE);
429 rte_vhost_driver_callback_register(dev_pathname,
430 &vhost_scsi_device_ops);
436 signal_handler(__rte_unused int signum)
439 if (access(dev_pathname, F_OK) == 0)
440 unlink(dev_pathname);
444 int main(int argc, char *argv[])
448 signal(SIGINT, signal_handler);
451 ret = rte_eal_init(argc, argv);
453 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
455 g_vhost_ctrlr = vhost_scsi_ctrlr_construct("vhost.socket");
456 if (g_vhost_ctrlr == NULL) {
457 fprintf(stderr, "Construct vhost scsi controller failed\n");
461 if (sem_init(&exit_sem, 0, 0) < 0) {
462 fprintf(stderr, "Error init exit_sem\n");
466 rte_vhost_driver_start(dev_pathname);
468 /* loop for exit the application */