Browse Source
v3: * Fix linker error on FreeBSD and other !CONFIG_LINUX host OSes [Peter] -----BEGIN PGP SIGNATURE----- iQEzBAABCAAdFiEEhpWov9P5fNqsNXdanKSrs4Grc8gFAl+S9O8ACgkQnKSrs4Gr c8iv7Qf9FrRvVPp6ooBpzq/3IdC7ZfPT3xOQsF7hR8OFKy8WKVRVl6B9TO/bJGES BeLIr+vrmCUjBB4HAYAqLJF1wMbBJZljyqDBR37rpWFxkSsXrUlPH+CSDuJfGE0D rqNlneAsMVWmnm9rmBvsKlSkFYD4GV7BWNZklL/AwQZMWAkjmOmzzmZ+7bWWJeMr Tm4MkVtbMwXhnFn3vohu1kUBj0bHiqmGEVN94ocEqXBWd+4+dAt6/tzH2PLSum7M 5ui31cmD8Qg33r1SraFXM6nfaqr9K+jua0eh4fQfJlWEKrLtxT6o0UKBsdyKrOFN ugtqk8V66DlUpd/3znb95tLQYAEvzQ== =6K6V -----END PGP SIGNATURE----- Merge remote-tracking branch 'remotes/stefanha-gitlab/tags/block-pull-request' into staging Pull request v3: * Fix linker error on FreeBSD and other !CONFIG_LINUX host OSes [Peter] # gpg: Signature made Fri 23 Oct 2020 16:21:19 BST # gpg: using RSA key 8695A8BFD3F97CDAAC35775A9CA4ABB381AB73C8 # gpg: Good signature from "Stefan Hajnoczi <stefanha@redhat.com>" [full] # gpg: aka "Stefan Hajnoczi <stefanha@gmail.com>" [full] # Primary key fingerprint: 8695 A8BF D3F9 7CDA AC35 775A 9CA4 ABB3 81AB 73C8 * remotes/stefanha-gitlab/tags/block-pull-request: (28 commits) iotests: add commit top->base cases to 274 block/io: fix bdrv_is_allocated_above block/io: bdrv_common_block_status_above: support bs == base block/io: bdrv_common_block_status_above: support include_base block/io: fix bdrv_co_block_status_above block/export: add vhost-user-blk multi-queue support block/export: add iothread and fixed-iothread options block: move block exports to libblockdev qemu-storage-daemon: avoid compiling blockdev_ss twice util/vhost-user-server: use static library in meson.build util/vhost-user-server: move header to include/ block/export: convert vhost-user-blk server to block export API block/export: report flush errors util/vhost-user-server: rework vu_client_trip() coroutine lifecycle util/vhost-user-server: check EOF when reading payload util/vhost-user-server: fix memory leak in vu_message_read() util/vhost-user-server: drop unused DevicePanicNotifier block/export: consolidate request structs into VuBlockReq util/vhost-user-server: drop unnecessary watch deletion util/vhost-user-server: drop unnecessary QOM cast ... Signed-off-by: Peter Maydell <peter.maydell@linaro.org>remotes/github/master
33 changed files with 1420 additions and 122 deletions
@ -1 +1,2 @@ |
|||
block_ss.add(files('export.c')) |
|||
blockdev_ss.add(files('export.c')) |
|||
blockdev_ss.add(when: ['CONFIG_LINUX', 'CONFIG_VHOST_USER'], if_true: files('vhost-user-blk-server.c')) |
|||
|
|||
@ -0,0 +1,431 @@ |
|||
/*
|
|||
* Sharing QEMU block devices via vhost-user protocal |
|||
* |
|||
* Parts of the code based on nbd/server.c. |
|||
* |
|||
* Copyright (c) Coiby Xu <coiby.xu@gmail.com>. |
|||
* Copyright (c) 2020 Red Hat, Inc. |
|||
* |
|||
* This work is licensed under the terms of the GNU GPL, version 2 or |
|||
* later. See the COPYING file in the top-level directory. |
|||
*/ |
|||
#include "qemu/osdep.h" |
|||
#include "block/block.h" |
|||
#include "contrib/libvhost-user/libvhost-user.h" |
|||
#include "standard-headers/linux/virtio_blk.h" |
|||
#include "qemu/vhost-user-server.h" |
|||
#include "vhost-user-blk-server.h" |
|||
#include "qapi/error.h" |
|||
#include "qom/object_interfaces.h" |
|||
#include "sysemu/block-backend.h" |
|||
#include "util/block-helpers.h" |
|||
|
|||
enum { |
|||
VHOST_USER_BLK_NUM_QUEUES_DEFAULT = 1, |
|||
}; |
|||
struct virtio_blk_inhdr { |
|||
unsigned char status; |
|||
}; |
|||
|
|||
typedef struct VuBlkReq { |
|||
VuVirtqElement elem; |
|||
int64_t sector_num; |
|||
size_t size; |
|||
struct virtio_blk_inhdr *in; |
|||
struct virtio_blk_outhdr out; |
|||
VuServer *server; |
|||
struct VuVirtq *vq; |
|||
} VuBlkReq; |
|||
|
|||
/* vhost user block device */ |
|||
typedef struct { |
|||
BlockExport export; |
|||
VuServer vu_server; |
|||
uint32_t blk_size; |
|||
QIOChannelSocket *sioc; |
|||
struct virtio_blk_config blkcfg; |
|||
bool writable; |
|||
} VuBlkExport; |
|||
|
|||
static void vu_blk_req_complete(VuBlkReq *req) |
|||
{ |
|||
VuDev *vu_dev = &req->server->vu_dev; |
|||
|
|||
/* IO size with 1 extra status byte */ |
|||
vu_queue_push(vu_dev, req->vq, &req->elem, req->size + 1); |
|||
vu_queue_notify(vu_dev, req->vq); |
|||
|
|||
free(req); |
|||
} |
|||
|
|||
static int coroutine_fn |
|||
vu_blk_discard_write_zeroes(BlockBackend *blk, struct iovec *iov, |
|||
uint32_t iovcnt, uint32_t type) |
|||
{ |
|||
struct virtio_blk_discard_write_zeroes desc; |
|||
ssize_t size = iov_to_buf(iov, iovcnt, 0, &desc, sizeof(desc)); |
|||
if (unlikely(size != sizeof(desc))) { |
|||
error_report("Invalid size %zd, expect %zu", size, sizeof(desc)); |
|||
return -EINVAL; |
|||
} |
|||
|
|||
uint64_t range[2] = { le64_to_cpu(desc.sector) << 9, |
|||
le32_to_cpu(desc.num_sectors) << 9 }; |
|||
if (type == VIRTIO_BLK_T_DISCARD) { |
|||
if (blk_co_pdiscard(blk, range[0], range[1]) == 0) { |
|||
return 0; |
|||
} |
|||
} else if (type == VIRTIO_BLK_T_WRITE_ZEROES) { |
|||
if (blk_co_pwrite_zeroes(blk, range[0], range[1], 0) == 0) { |
|||
return 0; |
|||
} |
|||
} |
|||
|
|||
return -EINVAL; |
|||
} |
|||
|
|||
static void coroutine_fn vu_blk_virtio_process_req(void *opaque) |
|||
{ |
|||
VuBlkReq *req = opaque; |
|||
VuServer *server = req->server; |
|||
VuVirtqElement *elem = &req->elem; |
|||
uint32_t type; |
|||
|
|||
VuBlkExport *vexp = container_of(server, VuBlkExport, vu_server); |
|||
BlockBackend *blk = vexp->export.blk; |
|||
|
|||
struct iovec *in_iov = elem->in_sg; |
|||
struct iovec *out_iov = elem->out_sg; |
|||
unsigned in_num = elem->in_num; |
|||
unsigned out_num = elem->out_num; |
|||
|
|||
/* refer to hw/block/virtio_blk.c */ |
|||
if (elem->out_num < 1 || elem->in_num < 1) { |
|||
error_report("virtio-blk request missing headers"); |
|||
goto err; |
|||
} |
|||
|
|||
if (unlikely(iov_to_buf(out_iov, out_num, 0, &req->out, |
|||
sizeof(req->out)) != sizeof(req->out))) { |
|||
error_report("virtio-blk request outhdr too short"); |
|||
goto err; |
|||
} |
|||
|
|||
iov_discard_front(&out_iov, &out_num, sizeof(req->out)); |
|||
|
|||
if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) { |
|||
error_report("virtio-blk request inhdr too short"); |
|||
goto err; |
|||
} |
|||
|
|||
/* We always touch the last byte, so just see how big in_iov is. */ |
|||
req->in = (void *)in_iov[in_num - 1].iov_base |
|||
+ in_iov[in_num - 1].iov_len |
|||
- sizeof(struct virtio_blk_inhdr); |
|||
iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr)); |
|||
|
|||
type = le32_to_cpu(req->out.type); |
|||
switch (type & ~VIRTIO_BLK_T_BARRIER) { |
|||
case VIRTIO_BLK_T_IN: |
|||
case VIRTIO_BLK_T_OUT: { |
|||
ssize_t ret = 0; |
|||
bool is_write = type & VIRTIO_BLK_T_OUT; |
|||
req->sector_num = le64_to_cpu(req->out.sector); |
|||
|
|||
if (is_write && !vexp->writable) { |
|||
req->in->status = VIRTIO_BLK_S_IOERR; |
|||
break; |
|||
} |
|||
|
|||
int64_t offset = req->sector_num * vexp->blk_size; |
|||
QEMUIOVector qiov; |
|||
if (is_write) { |
|||
qemu_iovec_init_external(&qiov, out_iov, out_num); |
|||
ret = blk_co_pwritev(blk, offset, qiov.size, &qiov, 0); |
|||
} else { |
|||
qemu_iovec_init_external(&qiov, in_iov, in_num); |
|||
ret = blk_co_preadv(blk, offset, qiov.size, &qiov, 0); |
|||
} |
|||
if (ret >= 0) { |
|||
req->in->status = VIRTIO_BLK_S_OK; |
|||
} else { |
|||
req->in->status = VIRTIO_BLK_S_IOERR; |
|||
} |
|||
break; |
|||
} |
|||
case VIRTIO_BLK_T_FLUSH: |
|||
if (blk_co_flush(blk) == 0) { |
|||
req->in->status = VIRTIO_BLK_S_OK; |
|||
} else { |
|||
req->in->status = VIRTIO_BLK_S_IOERR; |
|||
} |
|||
break; |
|||
case VIRTIO_BLK_T_GET_ID: { |
|||
size_t size = MIN(iov_size(&elem->in_sg[0], in_num), |
|||
VIRTIO_BLK_ID_BYTES); |
|||
snprintf(elem->in_sg[0].iov_base, size, "%s", "vhost_user_blk"); |
|||
req->in->status = VIRTIO_BLK_S_OK; |
|||
req->size = elem->in_sg[0].iov_len; |
|||
break; |
|||
} |
|||
case VIRTIO_BLK_T_DISCARD: |
|||
case VIRTIO_BLK_T_WRITE_ZEROES: { |
|||
int rc; |
|||
|
|||
if (!vexp->writable) { |
|||
req->in->status = VIRTIO_BLK_S_IOERR; |
|||
break; |
|||
} |
|||
|
|||
rc = vu_blk_discard_write_zeroes(blk, &elem->out_sg[1], out_num, type); |
|||
if (rc == 0) { |
|||
req->in->status = VIRTIO_BLK_S_OK; |
|||
} else { |
|||
req->in->status = VIRTIO_BLK_S_IOERR; |
|||
} |
|||
break; |
|||
} |
|||
default: |
|||
req->in->status = VIRTIO_BLK_S_UNSUPP; |
|||
break; |
|||
} |
|||
|
|||
vu_blk_req_complete(req); |
|||
return; |
|||
|
|||
err: |
|||
free(req); |
|||
} |
|||
|
|||
static void vu_blk_process_vq(VuDev *vu_dev, int idx) |
|||
{ |
|||
VuServer *server = container_of(vu_dev, VuServer, vu_dev); |
|||
VuVirtq *vq = vu_get_queue(vu_dev, idx); |
|||
|
|||
while (1) { |
|||
VuBlkReq *req; |
|||
|
|||
req = vu_queue_pop(vu_dev, vq, sizeof(VuBlkReq)); |
|||
if (!req) { |
|||
break; |
|||
} |
|||
|
|||
req->server = server; |
|||
req->vq = vq; |
|||
|
|||
Coroutine *co = |
|||
qemu_coroutine_create(vu_blk_virtio_process_req, req); |
|||
qemu_coroutine_enter(co); |
|||
} |
|||
} |
|||
|
|||
static void vu_blk_queue_set_started(VuDev *vu_dev, int idx, bool started) |
|||
{ |
|||
VuVirtq *vq; |
|||
|
|||
assert(vu_dev); |
|||
|
|||
vq = vu_get_queue(vu_dev, idx); |
|||
vu_set_queue_handler(vu_dev, vq, started ? vu_blk_process_vq : NULL); |
|||
} |
|||
|
|||
static uint64_t vu_blk_get_features(VuDev *dev) |
|||
{ |
|||
uint64_t features; |
|||
VuServer *server = container_of(dev, VuServer, vu_dev); |
|||
VuBlkExport *vexp = container_of(server, VuBlkExport, vu_server); |
|||
features = 1ull << VIRTIO_BLK_F_SIZE_MAX | |
|||
1ull << VIRTIO_BLK_F_SEG_MAX | |
|||
1ull << VIRTIO_BLK_F_TOPOLOGY | |
|||
1ull << VIRTIO_BLK_F_BLK_SIZE | |
|||
1ull << VIRTIO_BLK_F_FLUSH | |
|||
1ull << VIRTIO_BLK_F_DISCARD | |
|||
1ull << VIRTIO_BLK_F_WRITE_ZEROES | |
|||
1ull << VIRTIO_BLK_F_CONFIG_WCE | |
|||
1ull << VIRTIO_BLK_F_MQ | |
|||
1ull << VIRTIO_F_VERSION_1 | |
|||
1ull << VIRTIO_RING_F_INDIRECT_DESC | |
|||
1ull << VIRTIO_RING_F_EVENT_IDX | |
|||
1ull << VHOST_USER_F_PROTOCOL_FEATURES; |
|||
|
|||
if (!vexp->writable) { |
|||
features |= 1ull << VIRTIO_BLK_F_RO; |
|||
} |
|||
|
|||
return features; |
|||
} |
|||
|
|||
static uint64_t vu_blk_get_protocol_features(VuDev *dev) |
|||
{ |
|||
return 1ull << VHOST_USER_PROTOCOL_F_CONFIG | |
|||
1ull << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD; |
|||
} |
|||
|
|||
static int |
|||
vu_blk_get_config(VuDev *vu_dev, uint8_t *config, uint32_t len) |
|||
{ |
|||
/* TODO blkcfg must be little-endian for VIRTIO 1.0 */ |
|||
VuServer *server = container_of(vu_dev, VuServer, vu_dev); |
|||
VuBlkExport *vexp = container_of(server, VuBlkExport, vu_server); |
|||
memcpy(config, &vexp->blkcfg, len); |
|||
return 0; |
|||
} |
|||
|
|||
static int |
|||
vu_blk_set_config(VuDev *vu_dev, const uint8_t *data, |
|||
uint32_t offset, uint32_t size, uint32_t flags) |
|||
{ |
|||
VuServer *server = container_of(vu_dev, VuServer, vu_dev); |
|||
VuBlkExport *vexp = container_of(server, VuBlkExport, vu_server); |
|||
uint8_t wce; |
|||
|
|||
/* don't support live migration */ |
|||
if (flags != VHOST_SET_CONFIG_TYPE_MASTER) { |
|||
return -EINVAL; |
|||
} |
|||
|
|||
if (offset != offsetof(struct virtio_blk_config, wce) || |
|||
size != 1) { |
|||
return -EINVAL; |
|||
} |
|||
|
|||
wce = *data; |
|||
vexp->blkcfg.wce = wce; |
|||
blk_set_enable_write_cache(vexp->export.blk, wce); |
|||
return 0; |
|||
} |
|||
|
|||
/*
|
|||
* When the client disconnects, it sends a VHOST_USER_NONE request |
|||
* and vu_process_message will simple call exit which cause the VM |
|||
* to exit abruptly. |
|||
* To avoid this issue, process VHOST_USER_NONE request ahead |
|||
* of vu_process_message. |
|||
* |
|||
*/ |
|||
static int vu_blk_process_msg(VuDev *dev, VhostUserMsg *vmsg, int *do_reply) |
|||
{ |
|||
if (vmsg->request == VHOST_USER_NONE) { |
|||
dev->panic(dev, "disconnect"); |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
static const VuDevIface vu_blk_iface = { |
|||
.get_features = vu_blk_get_features, |
|||
.queue_set_started = vu_blk_queue_set_started, |
|||
.get_protocol_features = vu_blk_get_protocol_features, |
|||
.get_config = vu_blk_get_config, |
|||
.set_config = vu_blk_set_config, |
|||
.process_msg = vu_blk_process_msg, |
|||
}; |
|||
|
|||
static void blk_aio_attached(AioContext *ctx, void *opaque) |
|||
{ |
|||
VuBlkExport *vexp = opaque; |
|||
|
|||
vexp->export.ctx = ctx; |
|||
vhost_user_server_attach_aio_context(&vexp->vu_server, ctx); |
|||
} |
|||
|
|||
static void blk_aio_detach(void *opaque) |
|||
{ |
|||
VuBlkExport *vexp = opaque; |
|||
|
|||
vhost_user_server_detach_aio_context(&vexp->vu_server); |
|||
vexp->export.ctx = NULL; |
|||
} |
|||
|
|||
static void |
|||
vu_blk_initialize_config(BlockDriverState *bs, |
|||
struct virtio_blk_config *config, |
|||
uint32_t blk_size, |
|||
uint16_t num_queues) |
|||
{ |
|||
config->capacity = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
|||
config->blk_size = blk_size; |
|||
config->size_max = 0; |
|||
config->seg_max = 128 - 2; |
|||
config->min_io_size = 1; |
|||
config->opt_io_size = 1; |
|||
config->num_queues = num_queues; |
|||
config->max_discard_sectors = 32768; |
|||
config->max_discard_seg = 1; |
|||
config->discard_sector_alignment = config->blk_size >> 9; |
|||
config->max_write_zeroes_sectors = 32768; |
|||
config->max_write_zeroes_seg = 1; |
|||
} |
|||
|
|||
static void vu_blk_exp_request_shutdown(BlockExport *exp) |
|||
{ |
|||
VuBlkExport *vexp = container_of(exp, VuBlkExport, export); |
|||
|
|||
vhost_user_server_stop(&vexp->vu_server); |
|||
} |
|||
|
|||
static int vu_blk_exp_create(BlockExport *exp, BlockExportOptions *opts, |
|||
Error **errp) |
|||
{ |
|||
VuBlkExport *vexp = container_of(exp, VuBlkExport, export); |
|||
BlockExportOptionsVhostUserBlk *vu_opts = &opts->u.vhost_user_blk; |
|||
Error *local_err = NULL; |
|||
uint64_t logical_block_size; |
|||
uint16_t num_queues = VHOST_USER_BLK_NUM_QUEUES_DEFAULT; |
|||
|
|||
vexp->writable = opts->writable; |
|||
vexp->blkcfg.wce = 0; |
|||
|
|||
if (vu_opts->has_logical_block_size) { |
|||
logical_block_size = vu_opts->logical_block_size; |
|||
} else { |
|||
logical_block_size = BDRV_SECTOR_SIZE; |
|||
} |
|||
check_block_size(exp->id, "logical-block-size", logical_block_size, |
|||
&local_err); |
|||
if (local_err) { |
|||
error_propagate(errp, local_err); |
|||
return -EINVAL; |
|||
} |
|||
vexp->blk_size = logical_block_size; |
|||
blk_set_guest_block_size(exp->blk, logical_block_size); |
|||
|
|||
if (vu_opts->has_num_queues) { |
|||
num_queues = vu_opts->num_queues; |
|||
} |
|||
if (num_queues == 0) { |
|||
error_setg(errp, "num-queues must be greater than 0"); |
|||
return -EINVAL; |
|||
} |
|||
|
|||
vu_blk_initialize_config(blk_bs(exp->blk), &vexp->blkcfg, |
|||
logical_block_size, num_queues); |
|||
|
|||
blk_add_aio_context_notifier(exp->blk, blk_aio_attached, blk_aio_detach, |
|||
vexp); |
|||
|
|||
if (!vhost_user_server_start(&vexp->vu_server, vu_opts->addr, exp->ctx, |
|||
num_queues, &vu_blk_iface, errp)) { |
|||
blk_remove_aio_context_notifier(exp->blk, blk_aio_attached, |
|||
blk_aio_detach, vexp); |
|||
return -EADDRNOTAVAIL; |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static void vu_blk_exp_delete(BlockExport *exp) |
|||
{ |
|||
VuBlkExport *vexp = container_of(exp, VuBlkExport, export); |
|||
|
|||
blk_remove_aio_context_notifier(exp->blk, blk_aio_attached, blk_aio_detach, |
|||
vexp); |
|||
} |
|||
|
|||
const BlockExportDriver blk_exp_vhost_user_blk = { |
|||
.type = BLOCK_EXPORT_TYPE_VHOST_USER_BLK, |
|||
.instance_size = sizeof(VuBlkExport), |
|||
.create = vu_blk_exp_create, |
|||
.delete = vu_blk_exp_delete, |
|||
.request_shutdown = vu_blk_exp_request_shutdown, |
|||
}; |
|||
@ -0,0 +1,19 @@ |
|||
/*
|
|||
* Sharing QEMU block devices via vhost-user protocal |
|||
* |
|||
* Copyright (c) Coiby Xu <coiby.xu@gmail.com>. |
|||
* Copyright (c) 2020 Red Hat, Inc. |
|||
* |
|||
* This work is licensed under the terms of the GNU GPL, version 2 or |
|||
* later. See the COPYING file in the top-level directory. |
|||
*/ |
|||
|
|||
#ifndef VHOST_USER_BLK_SERVER_H |
|||
#define VHOST_USER_BLK_SERVER_H |
|||
|
|||
#include "block/export.h" |
|||
|
|||
/* For block/export/export.c */ |
|||
extern const BlockExportDriver blk_exp_vhost_user_blk; |
|||
|
|||
#endif /* VHOST_USER_BLK_SERVER_H */ |
|||
@ -1,3 +1,4 @@ |
|||
libvhost_user = static_library('vhost-user', |
|||
files('libvhost-user.c', 'libvhost-user-glib.c'), |
|||
build_by_default: false) |
|||
vhost_user = declare_dependency(link_with: libvhost_user) |
|||
|
|||
@ -0,0 +1,65 @@ |
|||
/*
|
|||
* Sharing QEMU devices via vhost-user protocol |
|||
* |
|||
* Copyright (c) Coiby Xu <coiby.xu@gmail.com>. |
|||
* Copyright (c) 2020 Red Hat, Inc. |
|||
* |
|||
* This work is licensed under the terms of the GNU GPL, version 2 or |
|||
* later. See the COPYING file in the top-level directory. |
|||
*/ |
|||
|
|||
#ifndef VHOST_USER_SERVER_H |
|||
#define VHOST_USER_SERVER_H |
|||
|
|||
#include "contrib/libvhost-user/libvhost-user.h" |
|||
#include "io/channel-socket.h" |
|||
#include "io/channel-file.h" |
|||
#include "io/net-listener.h" |
|||
#include "qemu/error-report.h" |
|||
#include "qapi/error.h" |
|||
#include "standard-headers/linux/virtio_blk.h" |
|||
|
|||
/* A kick fd that we monitor on behalf of libvhost-user */ |
|||
typedef struct VuFdWatch { |
|||
VuDev *vu_dev; |
|||
int fd; /*kick fd*/ |
|||
void *pvt; |
|||
vu_watch_cb cb; |
|||
QTAILQ_ENTRY(VuFdWatch) next; |
|||
} VuFdWatch; |
|||
|
|||
/**
|
|||
* VuServer: |
|||
* A vhost-user server instance with user-defined VuDevIface callbacks. |
|||
* Vhost-user device backends can be implemented using VuServer. VuDevIface |
|||
* callbacks and virtqueue kicks run in the given AioContext. |
|||
*/ |
|||
typedef struct { |
|||
QIONetListener *listener; |
|||
QEMUBH *restart_listener_bh; |
|||
AioContext *ctx; |
|||
int max_queues; |
|||
const VuDevIface *vu_iface; |
|||
|
|||
/* Protected by ctx lock */ |
|||
VuDev vu_dev; |
|||
QIOChannel *ioc; /* The I/O channel with the client */ |
|||
QIOChannelSocket *sioc; /* The underlying data channel with the client */ |
|||
QTAILQ_HEAD(, VuFdWatch) vu_fd_watches; |
|||
|
|||
Coroutine *co_trip; /* coroutine for processing VhostUserMsg */ |
|||
} VuServer; |
|||
|
|||
bool vhost_user_server_start(VuServer *server, |
|||
SocketAddress *unix_socket, |
|||
AioContext *ctx, |
|||
uint16_t max_queues, |
|||
const VuDevIface *vu_iface, |
|||
Error **errp); |
|||
|
|||
void vhost_user_server_stop(VuServer *server); |
|||
|
|||
void vhost_user_server_attach_aio_context(VuServer *server, AioContext *ctx); |
|||
void vhost_user_server_detach_aio_context(VuServer *server); |
|||
|
|||
#endif /* VHOST_USER_SERVER_H */ |
|||
@ -1,5 +1,7 @@ |
|||
block_ss.add(files( |
|||
'client.c', |
|||
'common.c', |
|||
)) |
|||
blockdev_ss.add(files( |
|||
'server.c', |
|||
)) |
|||
|
|||
@ -0,0 +1,7 @@ |
|||
#include "qemu/osdep.h" |
|||
#include "block/export.h" |
|||
|
|||
/* Only used in programs that support block exports (libblockdev.fa) */ |
|||
void blk_exp_close_all(void) |
|||
{ |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
/*
|
|||
* Block utility functions |
|||
* |
|||
* Copyright IBM, Corp. 2011 |
|||
* Copyright (c) 2020 Coiby Xu <coiby.xu@gmail.com> |
|||
* |
|||
* This work is licensed under the terms of the GNU GPL, version 2 or later. |
|||
* See the COPYING file in the top-level directory. |
|||
*/ |
|||
|
|||
#include "qemu/osdep.h" |
|||
#include "qapi/error.h" |
|||
#include "qapi/qmp/qerror.h" |
|||
#include "block-helpers.h" |
|||
|
|||
/**
|
|||
* check_block_size: |
|||
* @id: The unique ID of the object |
|||
* @name: The name of the property being validated |
|||
* @value: The block size in bytes |
|||
* @errp: A pointer to an area to store an error |
|||
* |
|||
* This function checks that the block size meets the following conditions: |
|||
* 1. At least MIN_BLOCK_SIZE |
|||
* 2. No larger than MAX_BLOCK_SIZE |
|||
* 3. A power of 2 |
|||
*/ |
|||
void check_block_size(const char *id, const char *name, int64_t value, |
|||
Error **errp) |
|||
{ |
|||
/* value of 0 means "unset" */ |
|||
if (value && (value < MIN_BLOCK_SIZE || value > MAX_BLOCK_SIZE)) { |
|||
error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, |
|||
id, name, value, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE); |
|||
return; |
|||
} |
|||
|
|||
/* We rely on power-of-2 blocksizes for bitmasks */ |
|||
if ((value & (value - 1)) != 0) { |
|||
error_setg(errp, |
|||
"Property %s.%s doesn't take value '%" PRId64 |
|||
"', it's not a power of 2", |
|||
id, name, value); |
|||
return; |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
#ifndef BLOCK_HELPERS_H |
|||
#define BLOCK_HELPERS_H |
|||
|
|||
#include "qemu/units.h" |
|||
|
|||
/* lower limit is sector size */ |
|||
#define MIN_BLOCK_SIZE INT64_C(512) |
|||
#define MIN_BLOCK_SIZE_STR "512 B" |
|||
/*
|
|||
* upper limit is arbitrary, 2 MiB looks sufficient for all sensible uses, and |
|||
* matches qcow2 cluster size limit |
|||
*/ |
|||
#define MAX_BLOCK_SIZE (2 * MiB) |
|||
#define MAX_BLOCK_SIZE_STR "2 MiB" |
|||
|
|||
void check_block_size(const char *id, const char *name, int64_t value, |
|||
Error **errp); |
|||
|
|||
#endif /* BLOCK_HELPERS_H */ |
|||
@ -0,0 +1,446 @@ |
|||
/*
|
|||
* Sharing QEMU devices via vhost-user protocol |
|||
* |
|||
* Copyright (c) Coiby Xu <coiby.xu@gmail.com>. |
|||
* Copyright (c) 2020 Red Hat, Inc. |
|||
* |
|||
* This work is licensed under the terms of the GNU GPL, version 2 or |
|||
* later. See the COPYING file in the top-level directory. |
|||
*/ |
|||
#include "qemu/osdep.h" |
|||
#include "qemu/main-loop.h" |
|||
#include "qemu/vhost-user-server.h" |
|||
#include "block/aio-wait.h" |
|||
|
|||
/*
|
|||
* Theory of operation: |
|||
* |
|||
* VuServer is started and stopped by vhost_user_server_start() and |
|||
* vhost_user_server_stop() from the main loop thread. Starting the server |
|||
* opens a vhost-user UNIX domain socket and listens for incoming connections. |
|||
* Only one connection is allowed at a time. |
|||
* |
|||
* The connection is handled by the vu_client_trip() coroutine in the |
|||
* VuServer->ctx AioContext. The coroutine consists of a vu_dispatch() loop |
|||
* where libvhost-user calls vu_message_read() to receive the next vhost-user |
|||
* protocol messages over the UNIX domain socket. |
|||
* |
|||
* When virtqueues are set up libvhost-user calls set_watch() to monitor kick |
|||
* fds. These fds are also handled in the VuServer->ctx AioContext. |
|||
* |
|||
* Both vu_client_trip() and kick fd monitoring can be stopped by shutting down |
|||
* the socket connection. Shutting down the socket connection causes |
|||
* vu_message_read() to fail since no more data can be received from the socket. |
|||
* After vu_dispatch() fails, vu_client_trip() calls vu_deinit() to stop |
|||
* libvhost-user before terminating the coroutine. vu_deinit() calls |
|||
* remove_watch() to stop monitoring kick fds and this stops virtqueue |
|||
* processing. |
|||
* |
|||
* When vu_client_trip() has finished cleaning up it schedules a BH in the main |
|||
* loop thread to accept the next client connection. |
|||
* |
|||
* When libvhost-user detects an error it calls panic_cb() and sets the |
|||
* dev->broken flag. Both vu_client_trip() and kick fd processing stop when |
|||
* the dev->broken flag is set. |
|||
* |
|||
* It is possible to switch AioContexts using |
|||
* vhost_user_server_detach_aio_context() and |
|||
* vhost_user_server_attach_aio_context(). They stop monitoring fds in the old |
|||
* AioContext and resume monitoring in the new AioContext. The vu_client_trip() |
|||
* coroutine remains in a yielded state during the switch. This is made |
|||
* possible by QIOChannel's support for spurious coroutine re-entry in |
|||
* qio_channel_yield(). The coroutine will restart I/O when re-entered from the |
|||
* new AioContext. |
|||
*/ |
|||
|
|||
static void vmsg_close_fds(VhostUserMsg *vmsg) |
|||
{ |
|||
int i; |
|||
for (i = 0; i < vmsg->fd_num; i++) { |
|||
close(vmsg->fds[i]); |
|||
} |
|||
} |
|||
|
|||
static void vmsg_unblock_fds(VhostUserMsg *vmsg) |
|||
{ |
|||
int i; |
|||
for (i = 0; i < vmsg->fd_num; i++) { |
|||
qemu_set_nonblock(vmsg->fds[i]); |
|||
} |
|||
} |
|||
|
|||
static void panic_cb(VuDev *vu_dev, const char *buf) |
|||
{ |
|||
error_report("vu_panic: %s", buf); |
|||
} |
|||
|
|||
static bool coroutine_fn |
|||
vu_message_read(VuDev *vu_dev, int conn_fd, VhostUserMsg *vmsg) |
|||
{ |
|||
struct iovec iov = { |
|||
.iov_base = (char *)vmsg, |
|||
.iov_len = VHOST_USER_HDR_SIZE, |
|||
}; |
|||
int rc, read_bytes = 0; |
|||
Error *local_err = NULL; |
|||
const size_t max_fds = G_N_ELEMENTS(vmsg->fds); |
|||
VuServer *server = container_of(vu_dev, VuServer, vu_dev); |
|||
QIOChannel *ioc = server->ioc; |
|||
|
|||
vmsg->fd_num = 0; |
|||
if (!ioc) { |
|||
error_report_err(local_err); |
|||
goto fail; |
|||
} |
|||
|
|||
assert(qemu_in_coroutine()); |
|||
do { |
|||
size_t nfds = 0; |
|||
int *fds = NULL; |
|||
|
|||
/*
|
|||
* qio_channel_readv_full may have short reads, keeping calling it |
|||
* until getting VHOST_USER_HDR_SIZE or 0 bytes in total |
|||
*/ |
|||
rc = qio_channel_readv_full(ioc, &iov, 1, &fds, &nfds, &local_err); |
|||
if (rc < 0) { |
|||
if (rc == QIO_CHANNEL_ERR_BLOCK) { |
|||
assert(local_err == NULL); |
|||
qio_channel_yield(ioc, G_IO_IN); |
|||
continue; |
|||
} else { |
|||
error_report_err(local_err); |
|||
goto fail; |
|||
} |
|||
} |
|||
|
|||
if (nfds > 0) { |
|||
if (vmsg->fd_num + nfds > max_fds) { |
|||
error_report("A maximum of %zu fds are allowed, " |
|||
"however got %zu fds now", |
|||
max_fds, vmsg->fd_num + nfds); |
|||
g_free(fds); |
|||
goto fail; |
|||
} |
|||
memcpy(vmsg->fds + vmsg->fd_num, fds, nfds * sizeof(vmsg->fds[0])); |
|||
vmsg->fd_num += nfds; |
|||
g_free(fds); |
|||
} |
|||
|
|||
if (rc == 0) { /* socket closed */ |
|||
goto fail; |
|||
} |
|||
|
|||
iov.iov_base += rc; |
|||
iov.iov_len -= rc; |
|||
read_bytes += rc; |
|||
} while (read_bytes != VHOST_USER_HDR_SIZE); |
|||
|
|||
/* qio_channel_readv_full will make socket fds blocking, unblock them */ |
|||
vmsg_unblock_fds(vmsg); |
|||
if (vmsg->size > sizeof(vmsg->payload)) { |
|||
error_report("Error: too big message request: %d, " |
|||
"size: vmsg->size: %u, " |
|||
"while sizeof(vmsg->payload) = %zu", |
|||
vmsg->request, vmsg->size, sizeof(vmsg->payload)); |
|||
goto fail; |
|||
} |
|||
|
|||
struct iovec iov_payload = { |
|||
.iov_base = (char *)&vmsg->payload, |
|||
.iov_len = vmsg->size, |
|||
}; |
|||
if (vmsg->size) { |
|||
rc = qio_channel_readv_all_eof(ioc, &iov_payload, 1, &local_err); |
|||
if (rc != 1) { |
|||
if (local_err) { |
|||
error_report_err(local_err); |
|||
} |
|||
goto fail; |
|||
} |
|||
} |
|||
|
|||
return true; |
|||
|
|||
fail: |
|||
vmsg_close_fds(vmsg); |
|||
|
|||
return false; |
|||
} |
|||
|
|||
static coroutine_fn void vu_client_trip(void *opaque) |
|||
{ |
|||
VuServer *server = opaque; |
|||
VuDev *vu_dev = &server->vu_dev; |
|||
|
|||
while (!vu_dev->broken && vu_dispatch(vu_dev)) { |
|||
/* Keep running */ |
|||
} |
|||
|
|||
vu_deinit(vu_dev); |
|||
|
|||
/* vu_deinit() should have called remove_watch() */ |
|||
assert(QTAILQ_EMPTY(&server->vu_fd_watches)); |
|||
|
|||
object_unref(OBJECT(server->sioc)); |
|||
server->sioc = NULL; |
|||
|
|||
object_unref(OBJECT(server->ioc)); |
|||
server->ioc = NULL; |
|||
|
|||
server->co_trip = NULL; |
|||
if (server->restart_listener_bh) { |
|||
qemu_bh_schedule(server->restart_listener_bh); |
|||
} |
|||
aio_wait_kick(); |
|||
} |
|||
|
|||
/*
|
|||
* a wrapper for vu_kick_cb |
|||
* |
|||
* since aio_dispatch can only pass one user data pointer to the |
|||
* callback function, pack VuDev and pvt into a struct. Then unpack it |
|||
* and pass them to vu_kick_cb |
|||
*/ |
|||
static void kick_handler(void *opaque) |
|||
{ |
|||
VuFdWatch *vu_fd_watch = opaque; |
|||
VuDev *vu_dev = vu_fd_watch->vu_dev; |
|||
|
|||
vu_fd_watch->cb(vu_dev, 0, vu_fd_watch->pvt); |
|||
|
|||
/* Stop vu_client_trip() if an error occurred in vu_fd_watch->cb() */ |
|||
if (vu_dev->broken) { |
|||
VuServer *server = container_of(vu_dev, VuServer, vu_dev); |
|||
|
|||
qio_channel_shutdown(server->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); |
|||
} |
|||
} |
|||
|
|||
static VuFdWatch *find_vu_fd_watch(VuServer *server, int fd) |
|||
{ |
|||
|
|||
VuFdWatch *vu_fd_watch, *next; |
|||
QTAILQ_FOREACH_SAFE(vu_fd_watch, &server->vu_fd_watches, next, next) { |
|||
if (vu_fd_watch->fd == fd) { |
|||
return vu_fd_watch; |
|||
} |
|||
} |
|||
return NULL; |
|||
} |
|||
|
|||
static void |
|||
set_watch(VuDev *vu_dev, int fd, int vu_evt, |
|||
vu_watch_cb cb, void *pvt) |
|||
{ |
|||
|
|||
VuServer *server = container_of(vu_dev, VuServer, vu_dev); |
|||
g_assert(vu_dev); |
|||
g_assert(fd >= 0); |
|||
g_assert(cb); |
|||
|
|||
VuFdWatch *vu_fd_watch = find_vu_fd_watch(server, fd); |
|||
|
|||
if (!vu_fd_watch) { |
|||
VuFdWatch *vu_fd_watch = g_new0(VuFdWatch, 1); |
|||
|
|||
QTAILQ_INSERT_TAIL(&server->vu_fd_watches, vu_fd_watch, next); |
|||
|
|||
vu_fd_watch->fd = fd; |
|||
vu_fd_watch->cb = cb; |
|||
qemu_set_nonblock(fd); |
|||
aio_set_fd_handler(server->ioc->ctx, fd, true, kick_handler, |
|||
NULL, NULL, vu_fd_watch); |
|||
vu_fd_watch->vu_dev = vu_dev; |
|||
vu_fd_watch->pvt = pvt; |
|||
} |
|||
} |
|||
|
|||
|
|||
static void remove_watch(VuDev *vu_dev, int fd) |
|||
{ |
|||
VuServer *server; |
|||
g_assert(vu_dev); |
|||
g_assert(fd >= 0); |
|||
|
|||
server = container_of(vu_dev, VuServer, vu_dev); |
|||
|
|||
VuFdWatch *vu_fd_watch = find_vu_fd_watch(server, fd); |
|||
|
|||
if (!vu_fd_watch) { |
|||
return; |
|||
} |
|||
aio_set_fd_handler(server->ioc->ctx, fd, true, NULL, NULL, NULL, NULL); |
|||
|
|||
QTAILQ_REMOVE(&server->vu_fd_watches, vu_fd_watch, next); |
|||
g_free(vu_fd_watch); |
|||
} |
|||
|
|||
|
|||
static void vu_accept(QIONetListener *listener, QIOChannelSocket *sioc, |
|||
gpointer opaque) |
|||
{ |
|||
VuServer *server = opaque; |
|||
|
|||
if (server->sioc) { |
|||
warn_report("Only one vhost-user client is allowed to " |
|||
"connect the server one time"); |
|||
return; |
|||
} |
|||
|
|||
if (!vu_init(&server->vu_dev, server->max_queues, sioc->fd, panic_cb, |
|||
vu_message_read, set_watch, remove_watch, server->vu_iface)) { |
|||
error_report("Failed to initialize libvhost-user"); |
|||
return; |
|||
} |
|||
|
|||
/*
|
|||
* Unset the callback function for network listener to make another |
|||
* vhost-user client keeping waiting until this client disconnects |
|||
*/ |
|||
qio_net_listener_set_client_func(server->listener, |
|||
NULL, |
|||
NULL, |
|||
NULL); |
|||
server->sioc = sioc; |
|||
/*
|
|||
* Increase the object reference, so sioc will not freed by |
|||
* qio_net_listener_channel_func which will call object_unref(OBJECT(sioc)) |
|||
*/ |
|||
object_ref(OBJECT(server->sioc)); |
|||
qio_channel_set_name(QIO_CHANNEL(sioc), "vhost-user client"); |
|||
server->ioc = QIO_CHANNEL(sioc); |
|||
object_ref(OBJECT(server->ioc)); |
|||
|
|||
/* TODO vu_message_write() spins if non-blocking! */ |
|||
qio_channel_set_blocking(server->ioc, false, NULL); |
|||
|
|||
server->co_trip = qemu_coroutine_create(vu_client_trip, server); |
|||
|
|||
aio_context_acquire(server->ctx); |
|||
vhost_user_server_attach_aio_context(server, server->ctx); |
|||
aio_context_release(server->ctx); |
|||
} |
|||
|
|||
void vhost_user_server_stop(VuServer *server) |
|||
{ |
|||
aio_context_acquire(server->ctx); |
|||
|
|||
qemu_bh_delete(server->restart_listener_bh); |
|||
server->restart_listener_bh = NULL; |
|||
|
|||
if (server->sioc) { |
|||
VuFdWatch *vu_fd_watch; |
|||
|
|||
QTAILQ_FOREACH(vu_fd_watch, &server->vu_fd_watches, next) { |
|||
aio_set_fd_handler(server->ctx, vu_fd_watch->fd, true, |
|||
NULL, NULL, NULL, vu_fd_watch); |
|||
} |
|||
|
|||
qio_channel_shutdown(server->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); |
|||
|
|||
AIO_WAIT_WHILE(server->ctx, server->co_trip); |
|||
} |
|||
|
|||
aio_context_release(server->ctx); |
|||
|
|||
if (server->listener) { |
|||
qio_net_listener_disconnect(server->listener); |
|||
object_unref(OBJECT(server->listener)); |
|||
} |
|||
} |
|||
|
|||
/*
|
|||
* Allow the next client to connect to the server. Called from a BH in the main |
|||
* loop. |
|||
*/ |
|||
static void restart_listener_bh(void *opaque) |
|||
{ |
|||
VuServer *server = opaque; |
|||
|
|||
qio_net_listener_set_client_func(server->listener, vu_accept, server, |
|||
NULL); |
|||
} |
|||
|
|||
/* Called with ctx acquired */ |
|||
void vhost_user_server_attach_aio_context(VuServer *server, AioContext *ctx) |
|||
{ |
|||
VuFdWatch *vu_fd_watch; |
|||
|
|||
server->ctx = ctx; |
|||
|
|||
if (!server->sioc) { |
|||
return; |
|||
} |
|||
|
|||
qio_channel_attach_aio_context(server->ioc, ctx); |
|||
|
|||
QTAILQ_FOREACH(vu_fd_watch, &server->vu_fd_watches, next) { |
|||
aio_set_fd_handler(ctx, vu_fd_watch->fd, true, kick_handler, NULL, |
|||
NULL, vu_fd_watch); |
|||
} |
|||
|
|||
aio_co_schedule(ctx, server->co_trip); |
|||
} |
|||
|
|||
/* Called with server->ctx acquired */ |
|||
void vhost_user_server_detach_aio_context(VuServer *server) |
|||
{ |
|||
if (server->sioc) { |
|||
VuFdWatch *vu_fd_watch; |
|||
|
|||
QTAILQ_FOREACH(vu_fd_watch, &server->vu_fd_watches, next) { |
|||
aio_set_fd_handler(server->ctx, vu_fd_watch->fd, true, |
|||
NULL, NULL, NULL, vu_fd_watch); |
|||
} |
|||
|
|||
qio_channel_detach_aio_context(server->ioc); |
|||
} |
|||
|
|||
server->ctx = NULL; |
|||
} |
|||
|
|||
bool vhost_user_server_start(VuServer *server, |
|||
SocketAddress *socket_addr, |
|||
AioContext *ctx, |
|||
uint16_t max_queues, |
|||
const VuDevIface *vu_iface, |
|||
Error **errp) |
|||
{ |
|||
QEMUBH *bh; |
|||
QIONetListener *listener; |
|||
|
|||
if (socket_addr->type != SOCKET_ADDRESS_TYPE_UNIX && |
|||
socket_addr->type != SOCKET_ADDRESS_TYPE_FD) { |
|||
error_setg(errp, "Only socket address types 'unix' and 'fd' are supported"); |
|||
return false; |
|||
} |
|||
|
|||
listener = qio_net_listener_new(); |
|||
if (qio_net_listener_open_sync(listener, socket_addr, 1, |
|||
errp) < 0) { |
|||
object_unref(OBJECT(listener)); |
|||
return false; |
|||
} |
|||
|
|||
bh = qemu_bh_new(restart_listener_bh, server); |
|||
|
|||
/* zero out unspecified fields */ |
|||
*server = (VuServer) { |
|||
.listener = listener, |
|||
.restart_listener_bh = bh, |
|||
.vu_iface = vu_iface, |
|||
.max_queues = max_queues, |
|||
.ctx = ctx, |
|||
}; |
|||
|
|||
qio_net_listener_set_name(server->listener, "vhost-user-backend-listener"); |
|||
|
|||
qio_net_listener_set_client_func(server->listener, |
|||
vu_accept, |
|||
server, |
|||
NULL); |
|||
|
|||
QTAILQ_INIT(&server->vu_fd_watches); |
|||
return true; |
|||
} |
|||
Loading…
Reference in new issue