1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
10 #include <semaphore.h>
11 #include <linux/virtio_scsi.h>
12 #include <linux/virtio_ring.h>
14 #include <rte_atomic.h>
15 #include <rte_cycles.h>
17 #include <rte_malloc.h>
18 #include <rte_vhost.h>
20 #include "vhost_scsi.h"
21 #include "scsi_spec.h"
23 #define VIRTIO_SCSI_FEATURES ((1 << VIRTIO_F_NOTIFY_ON_EMPTY) |\
24 (1 << VIRTIO_RING_F_EVENT_IDX) |\
25 (1 << VIRTIO_SCSI_F_INOUT) |\
26 (1 << VIRTIO_SCSI_F_CHANGE))
28 /* Path to folder where character device will be created. Can be set by user. */
29 static char dev_pathname[PATH_MAX] = "";
31 static struct vhost_scsi_ctrlr *g_vhost_ctrlr;
32 static int g_should_stop;
33 static sem_t exit_sem;
35 static struct vhost_scsi_ctrlr *
36 vhost_scsi_ctrlr_find(__rte_unused const char *ctrlr_name)
38 /* currently we only support 1 socket file fd */
42 static uint64_t gpa_to_vva(int vid, uint64_t gpa)
45 struct vhost_scsi_ctrlr *ctrlr;
48 ret = rte_vhost_get_ifname(vid, path, PATH_MAX);
50 fprintf(stderr, "Cannot get socket name\n");
54 ctrlr = vhost_scsi_ctrlr_find(path);
56 fprintf(stderr, "Controller is not ready\n");
57 assert(ctrlr != NULL);
60 assert(ctrlr->mem != NULL);
62 return rte_vhost_gpa_to_vva(ctrlr->mem, gpa);
65 static struct vring_desc *
66 descriptor_get_next(struct vring_desc *vq_desc, struct vring_desc *cur_desc)
68 return &vq_desc[cur_desc->next];
72 descriptor_has_next(struct vring_desc *cur_desc)
74 return !!(cur_desc->flags & VRING_DESC_F_NEXT);
78 descriptor_is_wr(struct vring_desc *cur_desc)
80 return !!(cur_desc->flags & VRING_DESC_F_WRITE);
84 submit_completion(struct vhost_scsi_task *task, uint32_t q_idx)
86 struct rte_vhost_vring *vq;
87 struct vring_used *used;
91 /* Fill out the next entry in the "used" ring. id = the
92 * index of the descriptor that contained the SCSI request.
93 * len = the total amount of data transferred for the SCSI
94 * request. We must report the correct len, for variable
95 * length SCSI CDBs, where we may return less data than
96 * allocated by the guest VM.
98 used->ring[used->idx & (vq->size - 1)].id = task->req_idx;
99 used->ring[used->idx & (vq->size - 1)].len = task->data_len;
102 /* Send an interrupt back to the guest VM so that it knows
103 * a completion is ready to be processed.
105 rte_vhost_vring_call(task->bdev->vid, q_idx);
109 vhost_process_read_payload_chain(struct vhost_scsi_task *task)
114 task->resp = (void *)(uintptr_t)gpa_to_vva(task->bdev->vid,
117 while (descriptor_has_next(task->desc)) {
118 task->desc = descriptor_get_next(task->vq->desc, task->desc);
119 data = (void *)(uintptr_t)gpa_to_vva(task->bdev->vid,
121 task->iovs[task->iovs_cnt].iov_base = data;
122 task->iovs[task->iovs_cnt].iov_len = task->desc->len;
123 task->data_len += task->desc->len;
129 vhost_process_write_payload_chain(struct vhost_scsi_task *task)
136 data = (void *)(uintptr_t)gpa_to_vva(task->bdev->vid,
138 task->iovs[task->iovs_cnt].iov_base = data;
139 task->iovs[task->iovs_cnt].iov_len = task->desc->len;
140 task->data_len += task->desc->len;
142 task->desc = descriptor_get_next(task->vq->desc, task->desc);
143 } while (descriptor_has_next(task->desc));
145 task->resp = (void *)(uintptr_t)gpa_to_vva(task->bdev->vid,
149 static struct vhost_block_dev *
150 vhost_scsi_bdev_construct(const char *bdev_name, const char *bdev_serial,
151 uint32_t blk_size, uint64_t blk_cnt,
154 struct vhost_block_dev *bdev;
156 bdev = rte_zmalloc(NULL, sizeof(*bdev), RTE_CACHE_LINE_SIZE);
160 strncpy(bdev->name, bdev_name, sizeof(bdev->name));
161 strncpy(bdev->product_name, bdev_serial, sizeof(bdev->product_name));
162 bdev->blocklen = blk_size;
163 bdev->blockcnt = blk_cnt;
164 bdev->write_cache = wce_enable;
166 /* use memory as disk storage space */
167 bdev->data = rte_zmalloc(NULL, blk_cnt * blk_size, 0);
169 fprintf(stderr, "no enough reseverd huge memory for disk\n");
177 process_requestq(struct vhost_scsi_ctrlr *ctrlr, uint32_t q_idx)
180 struct vhost_scsi_queue *scsi_vq;
181 struct rte_vhost_vring *vq;
183 scsi_vq = &ctrlr->bdev->queues[q_idx];
185 ret = rte_vhost_get_vhost_vring(ctrlr->bdev->vid, q_idx, vq);
188 while (vq->avail->idx != scsi_vq->last_used_idx) {
191 struct vhost_scsi_task *task;
193 last_idx = scsi_vq->last_used_idx & (vq->size - 1);
194 req_idx = vq->avail->ring[last_idx];
196 task = rte_zmalloc(NULL, sizeof(*task), 0);
197 assert(task != NULL);
200 task->bdev = ctrlr->bdev;
202 task->req_idx = req_idx;
203 task->desc = &task->vq->desc[task->req_idx];
205 /* does not support indirect descriptors */
206 assert((task->desc->flags & VRING_DESC_F_INDIRECT) == 0);
207 scsi_vq->last_used_idx++;
209 task->req = (void *)(uintptr_t)gpa_to_vva(task->bdev->vid,
212 task->desc = descriptor_get_next(task->vq->desc, task->desc);
213 if (!descriptor_has_next(task->desc)) {
214 task->dxfer_dir = SCSI_DIR_NONE;
215 task->resp = (void *)(uintptr_t)
216 gpa_to_vva(task->bdev->vid,
219 } else if (!descriptor_is_wr(task->desc)) {
220 task->dxfer_dir = SCSI_DIR_TO_DEV;
221 vhost_process_write_payload_chain(task);
223 task->dxfer_dir = SCSI_DIR_FROM_DEV;
224 vhost_process_read_payload_chain(task);
227 ret = vhost_bdev_process_scsi_commands(ctrlr->bdev, task);
229 /* invalid response */
230 task->resp->response = VIRTIO_SCSI_S_BAD_TARGET;
233 task->resp->response = VIRTIO_SCSI_S_OK;
234 task->resp->status = 0;
235 task->resp->resid = 0;
237 submit_completion(task, q_idx);
242 /* Main framework for processing IOs */
244 ctrlr_worker(void *arg)
247 struct vhost_scsi_ctrlr *ctrlr = (struct vhost_scsi_ctrlr *)arg;
251 thread = pthread_self();
254 pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
256 num = rte_vhost_get_vring_num(ctrlr->bdev->vid);
257 fprintf(stdout, "Ctrlr Worker Thread Started with %u Vring\n", num);
259 if (num != NUM_OF_SCSI_QUEUES) {
260 fprintf(stderr, "Only 1 IO queue are supported\n");
264 while (!g_should_stop && ctrlr->bdev != NULL) {
265 /* At least 3 vrings, currently only can support 1 IO queue
266 * Queue 2 for IO queue, does not support TMF and hotplug
267 * for the example application now
269 for (idx = 2; idx < num; idx++)
270 process_requestq(ctrlr, idx);
273 fprintf(stdout, "Ctrlr Worker Thread Exiting\n");
282 struct vhost_scsi_ctrlr *ctrlr;
283 struct vhost_scsi_queue *scsi_vq;
284 struct rte_vhost_vring *vq;
288 ret = rte_vhost_get_ifname(vid, path, PATH_MAX);
290 fprintf(stderr, "Cannot get socket name\n");
294 ctrlr = vhost_scsi_ctrlr_find(path);
296 fprintf(stderr, "Controller is not ready\n");
300 ret = rte_vhost_get_mem_table(vid, &ctrlr->mem);
302 fprintf(stderr, "Get Controller memory region failed\n");
305 assert(ctrlr->mem != NULL);
307 /* hardcoded block device information with 128MiB */
308 ctrlr->bdev = vhost_scsi_bdev_construct("malloc0", "vhost_scsi_malloc0",
313 ctrlr->bdev->vid = vid;
315 /* Disable Notifications */
316 for (i = 0; i < NUM_OF_SCSI_QUEUES; i++) {
317 rte_vhost_enable_guest_notification(vid, i, 0);
318 /* restore used index */
319 scsi_vq = &ctrlr->bdev->queues[i];
321 ret = rte_vhost_get_vhost_vring(ctrlr->bdev->vid, i, vq);
323 scsi_vq->last_used_idx = vq->used->idx;
324 scsi_vq->last_avail_idx = vq->used->idx;
328 fprintf(stdout, "New Device %s, Device ID %d\n", path, vid);
329 if (pthread_create(&tid, NULL, &ctrlr_worker, ctrlr) < 0) {
330 fprintf(stderr, "Worker Thread Started Failed\n");
338 destroy_device(int vid)
341 struct vhost_scsi_ctrlr *ctrlr;
343 rte_vhost_get_ifname(vid, path, PATH_MAX);
344 fprintf(stdout, "Destroy %s Device ID %d\n", path, vid);
345 ctrlr = vhost_scsi_ctrlr_find(path);
347 fprintf(stderr, "Destroy Ctrlr Failed\n");
356 static const struct vhost_device_ops vhost_scsi_device_ops = {
357 .new_device = new_device,
358 .destroy_device = destroy_device,
361 static struct vhost_scsi_ctrlr *
362 vhost_scsi_ctrlr_construct(const char *ctrlr_name)
365 struct vhost_scsi_ctrlr *ctrlr;
369 /* always use current directory */
370 path = getcwd(cwd, PATH_MAX);
372 fprintf(stderr, "Cannot get current working directory\n");
375 snprintf(dev_pathname, sizeof(dev_pathname), "%s/%s", path, ctrlr_name);
377 if (access(dev_pathname, F_OK) != -1) {
378 if (unlink(dev_pathname) != 0)
379 rte_exit(EXIT_FAILURE, "Cannot remove %s.\n",
383 if (rte_vhost_driver_register(dev_pathname, 0) != 0) {
384 fprintf(stderr, "socket %s already exists\n", dev_pathname);
388 fprintf(stdout, "socket file: %s created\n", dev_pathname);
390 ret = rte_vhost_driver_set_features(dev_pathname, VIRTIO_SCSI_FEATURES);
392 fprintf(stderr, "Set vhost driver features failed\n");
396 ctrlr = rte_zmalloc(NULL, sizeof(*ctrlr), RTE_CACHE_LINE_SIZE);
400 rte_vhost_driver_callback_register(dev_pathname,
401 &vhost_scsi_device_ops);
407 signal_handler(__rte_unused int signum)
410 if (access(dev_pathname, F_OK) == 0)
411 unlink(dev_pathname);
415 int main(int argc, char *argv[])
419 signal(SIGINT, signal_handler);
422 ret = rte_eal_init(argc, argv);
424 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
426 g_vhost_ctrlr = vhost_scsi_ctrlr_construct("vhost.socket");
427 if (g_vhost_ctrlr == NULL) {
428 fprintf(stderr, "Construct vhost scsi controller failed\n");
432 if (sem_init(&exit_sem, 0, 0) < 0) {
433 fprintf(stderr, "Error init exit_sem\n");
437 rte_vhost_driver_start(dev_pathname);
439 /* loop for exit the application */