xref: /qemu/block/export/vhost-user-blk-server.c (revision cbc20bfb)
1 /*
2  * Sharing QEMU block devices via vhost-user protocal
3  *
4  * Parts of the code based on nbd/server.c.
5  *
6  * Copyright (c) Coiby Xu <coiby.xu@gmail.com>.
7  * Copyright (c) 2020 Red Hat, Inc.
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or
10  * later.  See the COPYING file in the top-level directory.
11  */
12 #include "qemu/osdep.h"
13 #include "block/block.h"
14 #include "contrib/libvhost-user/libvhost-user.h"
15 #include "standard-headers/linux/virtio_blk.h"
16 #include "qemu/vhost-user-server.h"
17 #include "vhost-user-blk-server.h"
18 #include "qapi/error.h"
19 #include "qom/object_interfaces.h"
20 #include "sysemu/block-backend.h"
21 #include "util/block-helpers.h"
22 
23 enum {
24     VHOST_USER_BLK_MAX_QUEUES = 1,
25 };
26 struct virtio_blk_inhdr {
27     unsigned char status;
28 };
29 
30 typedef struct VuBlkReq {
31     VuVirtqElement elem;
32     int64_t sector_num;
33     size_t size;
34     struct virtio_blk_inhdr *in;
35     struct virtio_blk_outhdr out;
36     VuServer *server;
37     struct VuVirtq *vq;
38 } VuBlkReq;
39 
40 /* vhost user block device */
41 typedef struct {
42     BlockExport export;
43     VuServer vu_server;
44     uint32_t blk_size;
45     QIOChannelSocket *sioc;
46     struct virtio_blk_config blkcfg;
47     bool writable;
48 } VuBlkExport;
49 
50 static void vu_blk_req_complete(VuBlkReq *req)
51 {
52     VuDev *vu_dev = &req->server->vu_dev;
53 
54     /* IO size with 1 extra status byte */
55     vu_queue_push(vu_dev, req->vq, &req->elem, req->size + 1);
56     vu_queue_notify(vu_dev, req->vq);
57 
58     free(req);
59 }
60 
61 static int coroutine_fn
62 vu_blk_discard_write_zeroes(BlockBackend *blk, struct iovec *iov,
63                             uint32_t iovcnt, uint32_t type)
64 {
65     struct virtio_blk_discard_write_zeroes desc;
66     ssize_t size = iov_to_buf(iov, iovcnt, 0, &desc, sizeof(desc));
67     if (unlikely(size != sizeof(desc))) {
68         error_report("Invalid size %zd, expect %zu", size, sizeof(desc));
69         return -EINVAL;
70     }
71 
72     uint64_t range[2] = { le64_to_cpu(desc.sector) << 9,
73                           le32_to_cpu(desc.num_sectors) << 9 };
74     if (type == VIRTIO_BLK_T_DISCARD) {
75         if (blk_co_pdiscard(blk, range[0], range[1]) == 0) {
76             return 0;
77         }
78     } else if (type == VIRTIO_BLK_T_WRITE_ZEROES) {
79         if (blk_co_pwrite_zeroes(blk, range[0], range[1], 0) == 0) {
80             return 0;
81         }
82     }
83 
84     return -EINVAL;
85 }
86 
87 static void coroutine_fn vu_blk_virtio_process_req(void *opaque)
88 {
89     VuBlkReq *req = opaque;
90     VuServer *server = req->server;
91     VuVirtqElement *elem = &req->elem;
92     uint32_t type;
93 
94     VuBlkExport *vexp = container_of(server, VuBlkExport, vu_server);
95     BlockBackend *blk = vexp->export.blk;
96 
97     struct iovec *in_iov = elem->in_sg;
98     struct iovec *out_iov = elem->out_sg;
99     unsigned in_num = elem->in_num;
100     unsigned out_num = elem->out_num;
101 
102     /* refer to hw/block/virtio_blk.c */
103     if (elem->out_num < 1 || elem->in_num < 1) {
104         error_report("virtio-blk request missing headers");
105         goto err;
106     }
107 
108     if (unlikely(iov_to_buf(out_iov, out_num, 0, &req->out,
109                             sizeof(req->out)) != sizeof(req->out))) {
110         error_report("virtio-blk request outhdr too short");
111         goto err;
112     }
113 
114     iov_discard_front(&out_iov, &out_num, sizeof(req->out));
115 
116     if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) {
117         error_report("virtio-blk request inhdr too short");
118         goto err;
119     }
120 
121     /* We always touch the last byte, so just see how big in_iov is.  */
122     req->in = (void *)in_iov[in_num - 1].iov_base
123               + in_iov[in_num - 1].iov_len
124               - sizeof(struct virtio_blk_inhdr);
125     iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr));
126 
127     type = le32_to_cpu(req->out.type);
128     switch (type & ~VIRTIO_BLK_T_BARRIER) {
129     case VIRTIO_BLK_T_IN:
130     case VIRTIO_BLK_T_OUT: {
131         ssize_t ret = 0;
132         bool is_write = type & VIRTIO_BLK_T_OUT;
133         req->sector_num = le64_to_cpu(req->out.sector);
134 
135         if (is_write && !vexp->writable) {
136             req->in->status = VIRTIO_BLK_S_IOERR;
137             break;
138         }
139 
140         int64_t offset = req->sector_num * vexp->blk_size;
141         QEMUIOVector qiov;
142         if (is_write) {
143             qemu_iovec_init_external(&qiov, out_iov, out_num);
144             ret = blk_co_pwritev(blk, offset, qiov.size, &qiov, 0);
145         } else {
146             qemu_iovec_init_external(&qiov, in_iov, in_num);
147             ret = blk_co_preadv(blk, offset, qiov.size, &qiov, 0);
148         }
149         if (ret >= 0) {
150             req->in->status = VIRTIO_BLK_S_OK;
151         } else {
152             req->in->status = VIRTIO_BLK_S_IOERR;
153         }
154         break;
155     }
156     case VIRTIO_BLK_T_FLUSH:
157         if (blk_co_flush(blk) == 0) {
158             req->in->status = VIRTIO_BLK_S_OK;
159         } else {
160             req->in->status = VIRTIO_BLK_S_IOERR;
161         }
162         break;
163     case VIRTIO_BLK_T_GET_ID: {
164         size_t size = MIN(iov_size(&elem->in_sg[0], in_num),
165                           VIRTIO_BLK_ID_BYTES);
166         snprintf(elem->in_sg[0].iov_base, size, "%s", "vhost_user_blk");
167         req->in->status = VIRTIO_BLK_S_OK;
168         req->size = elem->in_sg[0].iov_len;
169         break;
170     }
171     case VIRTIO_BLK_T_DISCARD:
172     case VIRTIO_BLK_T_WRITE_ZEROES: {
173         int rc;
174 
175         if (!vexp->writable) {
176             req->in->status = VIRTIO_BLK_S_IOERR;
177             break;
178         }
179 
180         rc = vu_blk_discard_write_zeroes(blk, &elem->out_sg[1], out_num, type);
181         if (rc == 0) {
182             req->in->status = VIRTIO_BLK_S_OK;
183         } else {
184             req->in->status = VIRTIO_BLK_S_IOERR;
185         }
186         break;
187     }
188     default:
189         req->in->status = VIRTIO_BLK_S_UNSUPP;
190         break;
191     }
192 
193     vu_blk_req_complete(req);
194     return;
195 
196 err:
197     free(req);
198 }
199 
200 static void vu_blk_process_vq(VuDev *vu_dev, int idx)
201 {
202     VuServer *server = container_of(vu_dev, VuServer, vu_dev);
203     VuVirtq *vq = vu_get_queue(vu_dev, idx);
204 
205     while (1) {
206         VuBlkReq *req;
207 
208         req = vu_queue_pop(vu_dev, vq, sizeof(VuBlkReq));
209         if (!req) {
210             break;
211         }
212 
213         req->server = server;
214         req->vq = vq;
215 
216         Coroutine *co =
217             qemu_coroutine_create(vu_blk_virtio_process_req, req);
218         qemu_coroutine_enter(co);
219     }
220 }
221 
222 static void vu_blk_queue_set_started(VuDev *vu_dev, int idx, bool started)
223 {
224     VuVirtq *vq;
225 
226     assert(vu_dev);
227 
228     vq = vu_get_queue(vu_dev, idx);
229     vu_set_queue_handler(vu_dev, vq, started ? vu_blk_process_vq : NULL);
230 }
231 
232 static uint64_t vu_blk_get_features(VuDev *dev)
233 {
234     uint64_t features;
235     VuServer *server = container_of(dev, VuServer, vu_dev);
236     VuBlkExport *vexp = container_of(server, VuBlkExport, vu_server);
237     features = 1ull << VIRTIO_BLK_F_SIZE_MAX |
238                1ull << VIRTIO_BLK_F_SEG_MAX |
239                1ull << VIRTIO_BLK_F_TOPOLOGY |
240                1ull << VIRTIO_BLK_F_BLK_SIZE |
241                1ull << VIRTIO_BLK_F_FLUSH |
242                1ull << VIRTIO_BLK_F_DISCARD |
243                1ull << VIRTIO_BLK_F_WRITE_ZEROES |
244                1ull << VIRTIO_BLK_F_CONFIG_WCE |
245                1ull << VIRTIO_F_VERSION_1 |
246                1ull << VIRTIO_RING_F_INDIRECT_DESC |
247                1ull << VIRTIO_RING_F_EVENT_IDX |
248                1ull << VHOST_USER_F_PROTOCOL_FEATURES;
249 
250     if (!vexp->writable) {
251         features |= 1ull << VIRTIO_BLK_F_RO;
252     }
253 
254     return features;
255 }
256 
257 static uint64_t vu_blk_get_protocol_features(VuDev *dev)
258 {
259     return 1ull << VHOST_USER_PROTOCOL_F_CONFIG |
260            1ull << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD;
261 }
262 
263 static int
264 vu_blk_get_config(VuDev *vu_dev, uint8_t *config, uint32_t len)
265 {
266     /* TODO blkcfg must be little-endian for VIRTIO 1.0 */
267     VuServer *server = container_of(vu_dev, VuServer, vu_dev);
268     VuBlkExport *vexp = container_of(server, VuBlkExport, vu_server);
269     memcpy(config, &vexp->blkcfg, len);
270     return 0;
271 }
272 
273 static int
274 vu_blk_set_config(VuDev *vu_dev, const uint8_t *data,
275                     uint32_t offset, uint32_t size, uint32_t flags)
276 {
277     VuServer *server = container_of(vu_dev, VuServer, vu_dev);
278     VuBlkExport *vexp = container_of(server, VuBlkExport, vu_server);
279     uint8_t wce;
280 
281     /* don't support live migration */
282     if (flags != VHOST_SET_CONFIG_TYPE_MASTER) {
283         return -EINVAL;
284     }
285 
286     if (offset != offsetof(struct virtio_blk_config, wce) ||
287         size != 1) {
288         return -EINVAL;
289     }
290 
291     wce = *data;
292     vexp->blkcfg.wce = wce;
293     blk_set_enable_write_cache(vexp->export.blk, wce);
294     return 0;
295 }
296 
297 /*
298  * When the client disconnects, it sends a VHOST_USER_NONE request
299  * and vu_process_message will simple call exit which cause the VM
300  * to exit abruptly.
301  * To avoid this issue,  process VHOST_USER_NONE request ahead
302  * of vu_process_message.
303  *
304  */
305 static int vu_blk_process_msg(VuDev *dev, VhostUserMsg *vmsg, int *do_reply)
306 {
307     if (vmsg->request == VHOST_USER_NONE) {
308         dev->panic(dev, "disconnect");
309         return true;
310     }
311     return false;
312 }
313 
314 static const VuDevIface vu_blk_iface = {
315     .get_features          = vu_blk_get_features,
316     .queue_set_started     = vu_blk_queue_set_started,
317     .get_protocol_features = vu_blk_get_protocol_features,
318     .get_config            = vu_blk_get_config,
319     .set_config            = vu_blk_set_config,
320     .process_msg           = vu_blk_process_msg,
321 };
322 
323 static void blk_aio_attached(AioContext *ctx, void *opaque)
324 {
325     VuBlkExport *vexp = opaque;
326     vhost_user_server_attach_aio_context(&vexp->vu_server, ctx);
327 }
328 
329 static void blk_aio_detach(void *opaque)
330 {
331     VuBlkExport *vexp = opaque;
332     vhost_user_server_detach_aio_context(&vexp->vu_server);
333 }
334 
335 static void
336 vu_blk_initialize_config(BlockDriverState *bs,
337                            struct virtio_blk_config *config, uint32_t blk_size)
338 {
339     config->capacity = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
340     config->blk_size = blk_size;
341     config->size_max = 0;
342     config->seg_max = 128 - 2;
343     config->min_io_size = 1;
344     config->opt_io_size = 1;
345     config->num_queues = VHOST_USER_BLK_MAX_QUEUES;
346     config->max_discard_sectors = 32768;
347     config->max_discard_seg = 1;
348     config->discard_sector_alignment = config->blk_size >> 9;
349     config->max_write_zeroes_sectors = 32768;
350     config->max_write_zeroes_seg = 1;
351 }
352 
353 static void vu_blk_exp_request_shutdown(BlockExport *exp)
354 {
355     VuBlkExport *vexp = container_of(exp, VuBlkExport, export);
356 
357     vhost_user_server_stop(&vexp->vu_server);
358 }
359 
360 static int vu_blk_exp_create(BlockExport *exp, BlockExportOptions *opts,
361                              Error **errp)
362 {
363     VuBlkExport *vexp = container_of(exp, VuBlkExport, export);
364     BlockExportOptionsVhostUserBlk *vu_opts = &opts->u.vhost_user_blk;
365     Error *local_err = NULL;
366     uint64_t logical_block_size;
367 
368     vexp->writable = opts->writable;
369     vexp->blkcfg.wce = 0;
370 
371     if (vu_opts->has_logical_block_size) {
372         logical_block_size = vu_opts->logical_block_size;
373     } else {
374         logical_block_size = BDRV_SECTOR_SIZE;
375     }
376     check_block_size(exp->id, "logical-block-size", logical_block_size,
377                      &local_err);
378     if (local_err) {
379         error_propagate(errp, local_err);
380         return -EINVAL;
381     }
382     vexp->blk_size = logical_block_size;
383     blk_set_guest_block_size(exp->blk, logical_block_size);
384     vu_blk_initialize_config(blk_bs(exp->blk), &vexp->blkcfg,
385                                logical_block_size);
386 
387     blk_set_allow_aio_context_change(exp->blk, true);
388     blk_add_aio_context_notifier(exp->blk, blk_aio_attached, blk_aio_detach,
389                                  vexp);
390 
391     if (!vhost_user_server_start(&vexp->vu_server, vu_opts->addr, exp->ctx,
392                                  VHOST_USER_BLK_MAX_QUEUES, &vu_blk_iface,
393                                  errp)) {
394         blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
395                                         blk_aio_detach, vexp);
396         return -EADDRNOTAVAIL;
397     }
398 
399     return 0;
400 }
401 
402 static void vu_blk_exp_delete(BlockExport *exp)
403 {
404     VuBlkExport *vexp = container_of(exp, VuBlkExport, export);
405 
406     blk_remove_aio_context_notifier(exp->blk, blk_aio_attached, blk_aio_detach,
407                                     vexp);
408 }
409 
410 const BlockExportDriver blk_exp_vhost_user_blk = {
411     .type               = BLOCK_EXPORT_TYPE_VHOST_USER_BLK,
412     .instance_size      = sizeof(VuBlkExport),
413     .create             = vu_blk_exp_create,
414     .delete             = vu_blk_exp_delete,
415     .request_shutdown   = vu_blk_exp_request_shutdown,
416 };
417