1438d863fSJohn Levon /* 2438d863fSJohn Levon * vfio protocol over a UNIX socket. 3438d863fSJohn Levon * 4438d863fSJohn Levon * Copyright © 2018, 2021 Oracle and/or its affiliates. 5438d863fSJohn Levon * 6438d863fSJohn Levon * SPDX-License-Identifier: GPL-2.0-or-later 7438d863fSJohn Levon */ 8438d863fSJohn Levon 9438d863fSJohn Levon #include "qemu/osdep.h" 10438d863fSJohn Levon #include <sys/ioctl.h> 11438d863fSJohn Levon 12438d863fSJohn Levon #include "hw/vfio/vfio-device.h" 13438d863fSJohn Levon #include "hw/vfio-user/proxy.h" 140b3d881aSJohn Levon #include "hw/vfio-user/trace.h" 15438d863fSJohn Levon #include "qapi/error.h" 161a0c32a9SJohn Levon #include "qobject/qbool.h" 1736227628SJohn Levon #include "qobject/qdict.h" 1836227628SJohn Levon #include "qobject/qjson.h" 1936227628SJohn Levon #include "qobject/qnum.h" 20438d863fSJohn Levon #include "qemu/error-report.h" 21438d863fSJohn Levon #include "qemu/lockable.h" 220b3d881aSJohn Levon #include "qemu/main-loop.h" 231a0c32a9SJohn Levon #include "qemu/thread.h" 24438d863fSJohn Levon #include "system/iothread.h" 25438d863fSJohn Levon 26438d863fSJohn Levon static IOThread *vfio_user_iothread; 27438d863fSJohn Levon 28438d863fSJohn Levon static void vfio_user_shutdown(VFIOUserProxy *proxy); 290b3d881aSJohn Levon static VFIOUserMsg *vfio_user_getmsg(VFIOUserProxy *proxy, VFIOUserHdr *hdr, 300b3d881aSJohn Levon VFIOUserFDs *fds); 310b3d881aSJohn Levon static void vfio_user_recycle(VFIOUserProxy *proxy, VFIOUserMsg *msg); 32438d863fSJohn Levon 330b3d881aSJohn Levon static void vfio_user_recv(void *opaque); 3436227628SJohn Levon static void vfio_user_send(void *opaque); 350b3d881aSJohn Levon 360b3d881aSJohn Levon static void vfio_user_request(void *opaque); 370b3d881aSJohn Levon 380b3d881aSJohn Levon static inline void vfio_user_set_error(VFIOUserHdr *hdr, uint32_t err) 390b3d881aSJohn Levon { 400b3d881aSJohn Levon hdr->flags |= VFIO_USER_ERROR; 410b3d881aSJohn Levon hdr->error_reply = err; 420b3d881aSJohn Levon } 43438d863fSJohn Levon 44438d863fSJohn Levon /* 45438d863fSJohn Levon * Functions called by main, CPU, or iothread threads 46438d863fSJohn Levon */ 47438d863fSJohn Levon 48438d863fSJohn Levon static void vfio_user_shutdown(VFIOUserProxy *proxy) 49438d863fSJohn Levon { 50438d863fSJohn Levon qio_channel_shutdown(proxy->ioc, QIO_CHANNEL_SHUTDOWN_READ, NULL); 51438d863fSJohn Levon qio_channel_set_aio_fd_handler(proxy->ioc, proxy->ctx, NULL, 52438d863fSJohn Levon proxy->ctx, NULL, NULL); 53438d863fSJohn Levon } 54438d863fSJohn Levon 5536227628SJohn Levon /* 5636227628SJohn Levon * Same return values as qio_channel_writev_full(): 5736227628SJohn Levon * 5836227628SJohn Levon * QIO_CHANNEL_ERR_BLOCK: *errp not set 5936227628SJohn Levon * -1: *errp will be populated 6036227628SJohn Levon * otherwise: bytes written 6136227628SJohn Levon */ 6236227628SJohn Levon static ssize_t vfio_user_send_qio(VFIOUserProxy *proxy, VFIOUserMsg *msg, 6336227628SJohn Levon Error **errp) 6436227628SJohn Levon { 6536227628SJohn Levon VFIOUserFDs *fds = msg->fds; 6636227628SJohn Levon struct iovec iov = { 6736227628SJohn Levon .iov_base = msg->hdr, 6836227628SJohn Levon .iov_len = msg->hdr->size, 6936227628SJohn Levon }; 7036227628SJohn Levon size_t numfds = 0; 7136227628SJohn Levon int *fdp = NULL; 7236227628SJohn Levon ssize_t ret; 7336227628SJohn Levon 7436227628SJohn Levon if (fds != NULL && fds->send_fds != 0) { 7536227628SJohn Levon numfds = fds->send_fds; 7636227628SJohn Levon fdp = fds->fds; 7736227628SJohn Levon } 7836227628SJohn Levon 7936227628SJohn Levon ret = qio_channel_writev_full(proxy->ioc, &iov, 1, fdp, numfds, 0, errp); 8036227628SJohn Levon 8136227628SJohn Levon if (ret == -1) { 8236227628SJohn Levon vfio_user_set_error(msg->hdr, EIO); 8336227628SJohn Levon vfio_user_shutdown(proxy); 8436227628SJohn Levon } 8536227628SJohn Levon trace_vfio_user_send_write(msg->hdr->id, ret); 8636227628SJohn Levon 8736227628SJohn Levon return ret; 8836227628SJohn Levon } 8936227628SJohn Levon 900b3d881aSJohn Levon static VFIOUserMsg *vfio_user_getmsg(VFIOUserProxy *proxy, VFIOUserHdr *hdr, 910b3d881aSJohn Levon VFIOUserFDs *fds) 920b3d881aSJohn Levon { 930b3d881aSJohn Levon VFIOUserMsg *msg; 940b3d881aSJohn Levon 950b3d881aSJohn Levon msg = QTAILQ_FIRST(&proxy->free); 960b3d881aSJohn Levon if (msg != NULL) { 970b3d881aSJohn Levon QTAILQ_REMOVE(&proxy->free, msg, next); 980b3d881aSJohn Levon } else { 990b3d881aSJohn Levon msg = g_malloc0(sizeof(*msg)); 1000b3d881aSJohn Levon qemu_cond_init(&msg->cv); 1010b3d881aSJohn Levon } 1020b3d881aSJohn Levon 1030b3d881aSJohn Levon msg->hdr = hdr; 1040b3d881aSJohn Levon msg->fds = fds; 1050b3d881aSJohn Levon return msg; 1060b3d881aSJohn Levon } 1070b3d881aSJohn Levon 1080b3d881aSJohn Levon /* 1090b3d881aSJohn Levon * Recycle a message list entry to the free list. 1100b3d881aSJohn Levon */ 1110b3d881aSJohn Levon static void vfio_user_recycle(VFIOUserProxy *proxy, VFIOUserMsg *msg) 1120b3d881aSJohn Levon { 1130b3d881aSJohn Levon if (msg->type == VFIO_MSG_NONE) { 1140b3d881aSJohn Levon error_printf("vfio_user_recycle - freeing free msg\n"); 1150b3d881aSJohn Levon return; 1160b3d881aSJohn Levon } 1170b3d881aSJohn Levon 1180b3d881aSJohn Levon /* free msg buffer if no one is waiting to consume the reply */ 1190b3d881aSJohn Levon if (msg->type == VFIO_MSG_NOWAIT || msg->type == VFIO_MSG_ASYNC) { 1200b3d881aSJohn Levon g_free(msg->hdr); 1210b3d881aSJohn Levon if (msg->fds != NULL) { 1220b3d881aSJohn Levon g_free(msg->fds); 1230b3d881aSJohn Levon } 1240b3d881aSJohn Levon } 1250b3d881aSJohn Levon 1260b3d881aSJohn Levon msg->type = VFIO_MSG_NONE; 1270b3d881aSJohn Levon msg->hdr = NULL; 1280b3d881aSJohn Levon msg->fds = NULL; 1290b3d881aSJohn Levon msg->complete = false; 13036227628SJohn Levon msg->pending = false; 1310b3d881aSJohn Levon QTAILQ_INSERT_HEAD(&proxy->free, msg, next); 1320b3d881aSJohn Levon } 1330b3d881aSJohn Levon 13418e899e6SJohn Levon VFIOUserFDs *vfio_user_getfds(int numfds) 1350b3d881aSJohn Levon { 1360b3d881aSJohn Levon VFIOUserFDs *fds = g_malloc0(sizeof(*fds) + (numfds * sizeof(int))); 1370b3d881aSJohn Levon 1380b3d881aSJohn Levon fds->fds = (int *)((char *)fds + sizeof(*fds)); 1390b3d881aSJohn Levon 1400b3d881aSJohn Levon return fds; 1410b3d881aSJohn Levon } 1420b3d881aSJohn Levon 143438d863fSJohn Levon /* 144438d863fSJohn Levon * Functions only called by iothread 145438d863fSJohn Levon */ 146438d863fSJohn Levon 1470b3d881aSJohn Levon /* 1480b3d881aSJohn Levon * Process a received message. 1490b3d881aSJohn Levon */ 1500b3d881aSJohn Levon static void vfio_user_process(VFIOUserProxy *proxy, VFIOUserMsg *msg, 1510b3d881aSJohn Levon bool isreply) 1520b3d881aSJohn Levon { 1530b3d881aSJohn Levon 1540b3d881aSJohn Levon /* 1550b3d881aSJohn Levon * Replies signal a waiter, if none just check for errors 1560b3d881aSJohn Levon * and free the message buffer. 1570b3d881aSJohn Levon * 1580b3d881aSJohn Levon * Requests get queued for the BH. 1590b3d881aSJohn Levon */ 1600b3d881aSJohn Levon if (isreply) { 1610b3d881aSJohn Levon msg->complete = true; 1620b3d881aSJohn Levon if (msg->type == VFIO_MSG_WAIT) { 1630b3d881aSJohn Levon qemu_cond_signal(&msg->cv); 1640b3d881aSJohn Levon } else { 1650b3d881aSJohn Levon if (msg->hdr->flags & VFIO_USER_ERROR) { 1660b3d881aSJohn Levon error_printf("vfio_user_process: error reply on async "); 1670b3d881aSJohn Levon error_printf("request command %x error %s\n", 1680b3d881aSJohn Levon msg->hdr->command, 1690b3d881aSJohn Levon strerror(msg->hdr->error_reply)); 1700b3d881aSJohn Levon } 1710b3d881aSJohn Levon /* youngest nowait msg has been ack'd */ 1720b3d881aSJohn Levon if (proxy->last_nowait == msg) { 1730b3d881aSJohn Levon proxy->last_nowait = NULL; 1740b3d881aSJohn Levon } 1750b3d881aSJohn Levon vfio_user_recycle(proxy, msg); 1760b3d881aSJohn Levon } 1770b3d881aSJohn Levon } else { 1780b3d881aSJohn Levon QTAILQ_INSERT_TAIL(&proxy->incoming, msg, next); 1790b3d881aSJohn Levon qemu_bh_schedule(proxy->req_bh); 1800b3d881aSJohn Levon } 1810b3d881aSJohn Levon } 1820b3d881aSJohn Levon 1830b3d881aSJohn Levon /* 1840b3d881aSJohn Levon * Complete a partial message read 1850b3d881aSJohn Levon */ 1860b3d881aSJohn Levon static int vfio_user_complete(VFIOUserProxy *proxy, Error **errp) 1870b3d881aSJohn Levon { 1880b3d881aSJohn Levon VFIOUserMsg *msg = proxy->part_recv; 1890b3d881aSJohn Levon size_t msgleft = proxy->recv_left; 1900b3d881aSJohn Levon bool isreply; 1910b3d881aSJohn Levon char *data; 1920b3d881aSJohn Levon int ret; 1930b3d881aSJohn Levon 1940b3d881aSJohn Levon data = (char *)msg->hdr + (msg->hdr->size - msgleft); 1950b3d881aSJohn Levon while (msgleft > 0) { 1960b3d881aSJohn Levon ret = qio_channel_read(proxy->ioc, data, msgleft, errp); 1970b3d881aSJohn Levon 1980b3d881aSJohn Levon /* error or would block */ 1990b3d881aSJohn Levon if (ret <= 0) { 2000b3d881aSJohn Levon /* try for rest on next iternation */ 2010b3d881aSJohn Levon if (ret == QIO_CHANNEL_ERR_BLOCK) { 2020b3d881aSJohn Levon proxy->recv_left = msgleft; 2030b3d881aSJohn Levon } 2040b3d881aSJohn Levon return ret; 2050b3d881aSJohn Levon } 2060b3d881aSJohn Levon trace_vfio_user_recv_read(msg->hdr->id, ret); 2070b3d881aSJohn Levon 2080b3d881aSJohn Levon msgleft -= ret; 2090b3d881aSJohn Levon data += ret; 2100b3d881aSJohn Levon } 2110b3d881aSJohn Levon 2120b3d881aSJohn Levon /* 2130b3d881aSJohn Levon * Read complete message, process it. 2140b3d881aSJohn Levon */ 2150b3d881aSJohn Levon proxy->part_recv = NULL; 2160b3d881aSJohn Levon proxy->recv_left = 0; 2170b3d881aSJohn Levon isreply = (msg->hdr->flags & VFIO_USER_TYPE) == VFIO_USER_REPLY; 2180b3d881aSJohn Levon vfio_user_process(proxy, msg, isreply); 2190b3d881aSJohn Levon 2200b3d881aSJohn Levon /* return positive value */ 2210b3d881aSJohn Levon return 1; 2220b3d881aSJohn Levon } 2230b3d881aSJohn Levon 2240b3d881aSJohn Levon /* 2250b3d881aSJohn Levon * Receive and process one incoming message. 2260b3d881aSJohn Levon * 2270b3d881aSJohn Levon * For replies, find matching outgoing request and wake any waiters. 2280b3d881aSJohn Levon * For requests, queue in incoming list and run request BH. 2290b3d881aSJohn Levon */ 2300b3d881aSJohn Levon static int vfio_user_recv_one(VFIOUserProxy *proxy, Error **errp) 2310b3d881aSJohn Levon { 2320b3d881aSJohn Levon VFIOUserMsg *msg = NULL; 2330b3d881aSJohn Levon g_autofree int *fdp = NULL; 2340b3d881aSJohn Levon VFIOUserFDs *reqfds; 2350b3d881aSJohn Levon VFIOUserHdr hdr; 2360b3d881aSJohn Levon struct iovec iov = { 2370b3d881aSJohn Levon .iov_base = &hdr, 2380b3d881aSJohn Levon .iov_len = sizeof(hdr), 2390b3d881aSJohn Levon }; 2400b3d881aSJohn Levon bool isreply = false; 2410b3d881aSJohn Levon int i, ret; 2420b3d881aSJohn Levon size_t msgleft, numfds = 0; 2430b3d881aSJohn Levon char *data = NULL; 2440b3d881aSJohn Levon char *buf = NULL; 2450b3d881aSJohn Levon 2460b3d881aSJohn Levon /* 2470b3d881aSJohn Levon * Complete any partial reads 2480b3d881aSJohn Levon */ 2490b3d881aSJohn Levon if (proxy->part_recv != NULL) { 2500b3d881aSJohn Levon ret = vfio_user_complete(proxy, errp); 2510b3d881aSJohn Levon 2520b3d881aSJohn Levon /* still not complete, try later */ 2530b3d881aSJohn Levon if (ret == QIO_CHANNEL_ERR_BLOCK) { 2540b3d881aSJohn Levon return ret; 2550b3d881aSJohn Levon } 2560b3d881aSJohn Levon 2570b3d881aSJohn Levon if (ret <= 0) { 2580b3d881aSJohn Levon goto fatal; 2590b3d881aSJohn Levon } 2600b3d881aSJohn Levon /* else fall into reading another msg */ 2610b3d881aSJohn Levon } 2620b3d881aSJohn Levon 2630b3d881aSJohn Levon /* 2640b3d881aSJohn Levon * Read header 2650b3d881aSJohn Levon */ 2660b3d881aSJohn Levon ret = qio_channel_readv_full(proxy->ioc, &iov, 1, &fdp, &numfds, 0, 2670b3d881aSJohn Levon errp); 2680b3d881aSJohn Levon if (ret == QIO_CHANNEL_ERR_BLOCK) { 2690b3d881aSJohn Levon return ret; 2700b3d881aSJohn Levon } 2710b3d881aSJohn Levon 2720b3d881aSJohn Levon /* read error or other side closed connection */ 2730b3d881aSJohn Levon if (ret <= 0) { 2740b3d881aSJohn Levon goto fatal; 2750b3d881aSJohn Levon } 2760b3d881aSJohn Levon 2770b3d881aSJohn Levon if (ret < sizeof(hdr)) { 2780b3d881aSJohn Levon error_setg(errp, "short read of header"); 2790b3d881aSJohn Levon goto fatal; 2800b3d881aSJohn Levon } 2810b3d881aSJohn Levon 2820b3d881aSJohn Levon /* 2830b3d881aSJohn Levon * Validate header 2840b3d881aSJohn Levon */ 2850b3d881aSJohn Levon if (hdr.size < sizeof(VFIOUserHdr)) { 2860b3d881aSJohn Levon error_setg(errp, "bad header size"); 2870b3d881aSJohn Levon goto fatal; 2880b3d881aSJohn Levon } 2890b3d881aSJohn Levon switch (hdr.flags & VFIO_USER_TYPE) { 2900b3d881aSJohn Levon case VFIO_USER_REQUEST: 2910b3d881aSJohn Levon isreply = false; 2920b3d881aSJohn Levon break; 2930b3d881aSJohn Levon case VFIO_USER_REPLY: 2940b3d881aSJohn Levon isreply = true; 2950b3d881aSJohn Levon break; 2960b3d881aSJohn Levon default: 2970b3d881aSJohn Levon error_setg(errp, "unknown message type"); 2980b3d881aSJohn Levon goto fatal; 2990b3d881aSJohn Levon } 3000b3d881aSJohn Levon trace_vfio_user_recv_hdr(proxy->sockname, hdr.id, hdr.command, hdr.size, 3010b3d881aSJohn Levon hdr.flags); 3020b3d881aSJohn Levon 3030b3d881aSJohn Levon /* 3040b3d881aSJohn Levon * For replies, find the matching pending request. 3050b3d881aSJohn Levon * For requests, reap incoming FDs. 3060b3d881aSJohn Levon */ 3070b3d881aSJohn Levon if (isreply) { 3080b3d881aSJohn Levon QTAILQ_FOREACH(msg, &proxy->pending, next) { 3090b3d881aSJohn Levon if (hdr.id == msg->id) { 3100b3d881aSJohn Levon break; 3110b3d881aSJohn Levon } 3120b3d881aSJohn Levon } 3130b3d881aSJohn Levon if (msg == NULL) { 3140b3d881aSJohn Levon error_setg(errp, "unexpected reply"); 3150b3d881aSJohn Levon goto err; 3160b3d881aSJohn Levon } 3170b3d881aSJohn Levon QTAILQ_REMOVE(&proxy->pending, msg, next); 3180b3d881aSJohn Levon 3190b3d881aSJohn Levon /* 3200b3d881aSJohn Levon * Process any received FDs 3210b3d881aSJohn Levon */ 3220b3d881aSJohn Levon if (numfds != 0) { 3230b3d881aSJohn Levon if (msg->fds == NULL || msg->fds->recv_fds < numfds) { 3240b3d881aSJohn Levon error_setg(errp, "unexpected FDs"); 3250b3d881aSJohn Levon goto err; 3260b3d881aSJohn Levon } 3270b3d881aSJohn Levon msg->fds->recv_fds = numfds; 3280b3d881aSJohn Levon memcpy(msg->fds->fds, fdp, numfds * sizeof(int)); 3290b3d881aSJohn Levon } 3300b3d881aSJohn Levon } else { 3310b3d881aSJohn Levon if (numfds != 0) { 3320b3d881aSJohn Levon reqfds = vfio_user_getfds(numfds); 3330b3d881aSJohn Levon memcpy(reqfds->fds, fdp, numfds * sizeof(int)); 3340b3d881aSJohn Levon } else { 3350b3d881aSJohn Levon reqfds = NULL; 3360b3d881aSJohn Levon } 3370b3d881aSJohn Levon } 3380b3d881aSJohn Levon 3390b3d881aSJohn Levon /* 3400b3d881aSJohn Levon * Put the whole message into a single buffer. 3410b3d881aSJohn Levon */ 3420b3d881aSJohn Levon if (isreply) { 3430b3d881aSJohn Levon if (hdr.size > msg->rsize) { 3440b3d881aSJohn Levon error_setg(errp, "reply larger than recv buffer"); 3450b3d881aSJohn Levon goto err; 3460b3d881aSJohn Levon } 3470b3d881aSJohn Levon *msg->hdr = hdr; 3480b3d881aSJohn Levon data = (char *)msg->hdr + sizeof(hdr); 3490b3d881aSJohn Levon } else { 350c6ac52a4SJohn Levon if (hdr.size > proxy->max_xfer_size + sizeof(VFIOUserDMARW)) { 351c6ac52a4SJohn Levon error_setg(errp, "vfio_user_recv request larger than max"); 352c6ac52a4SJohn Levon goto err; 353c6ac52a4SJohn Levon } 3540b3d881aSJohn Levon buf = g_malloc0(hdr.size); 3550b3d881aSJohn Levon memcpy(buf, &hdr, sizeof(hdr)); 3560b3d881aSJohn Levon data = buf + sizeof(hdr); 3570b3d881aSJohn Levon msg = vfio_user_getmsg(proxy, (VFIOUserHdr *)buf, reqfds); 3580b3d881aSJohn Levon msg->type = VFIO_MSG_REQ; 3590b3d881aSJohn Levon } 3600b3d881aSJohn Levon 3610b3d881aSJohn Levon /* 3620b3d881aSJohn Levon * Read rest of message. 3630b3d881aSJohn Levon */ 3640b3d881aSJohn Levon msgleft = hdr.size - sizeof(hdr); 3650b3d881aSJohn Levon while (msgleft > 0) { 3660b3d881aSJohn Levon ret = qio_channel_read(proxy->ioc, data, msgleft, errp); 3670b3d881aSJohn Levon 3680b3d881aSJohn Levon /* prepare to complete read on next iternation */ 3690b3d881aSJohn Levon if (ret == QIO_CHANNEL_ERR_BLOCK) { 3700b3d881aSJohn Levon proxy->part_recv = msg; 3710b3d881aSJohn Levon proxy->recv_left = msgleft; 3720b3d881aSJohn Levon return ret; 3730b3d881aSJohn Levon } 3740b3d881aSJohn Levon 3750b3d881aSJohn Levon if (ret <= 0) { 3760b3d881aSJohn Levon goto fatal; 3770b3d881aSJohn Levon } 3780b3d881aSJohn Levon trace_vfio_user_recv_read(hdr.id, ret); 3790b3d881aSJohn Levon 3800b3d881aSJohn Levon msgleft -= ret; 3810b3d881aSJohn Levon data += ret; 3820b3d881aSJohn Levon } 3830b3d881aSJohn Levon 3840b3d881aSJohn Levon vfio_user_process(proxy, msg, isreply); 3850b3d881aSJohn Levon return 0; 3860b3d881aSJohn Levon 3870b3d881aSJohn Levon /* 3880b3d881aSJohn Levon * fatal means the other side closed or we don't trust the stream 3890b3d881aSJohn Levon * err means this message is corrupt 3900b3d881aSJohn Levon */ 3910b3d881aSJohn Levon fatal: 3920b3d881aSJohn Levon vfio_user_shutdown(proxy); 3930b3d881aSJohn Levon proxy->state = VFIO_PROXY_ERROR; 3940b3d881aSJohn Levon 3950b3d881aSJohn Levon /* set error if server side closed */ 3960b3d881aSJohn Levon if (ret == 0) { 3970b3d881aSJohn Levon error_setg(errp, "server closed socket"); 3980b3d881aSJohn Levon } 3990b3d881aSJohn Levon 4000b3d881aSJohn Levon err: 4010b3d881aSJohn Levon for (i = 0; i < numfds; i++) { 4020b3d881aSJohn Levon close(fdp[i]); 4030b3d881aSJohn Levon } 4040b3d881aSJohn Levon if (isreply && msg != NULL) { 4050b3d881aSJohn Levon /* force an error to keep sending thread from hanging */ 4060b3d881aSJohn Levon vfio_user_set_error(msg->hdr, EINVAL); 4070b3d881aSJohn Levon msg->complete = true; 4080b3d881aSJohn Levon qemu_cond_signal(&msg->cv); 4090b3d881aSJohn Levon } 4100b3d881aSJohn Levon return -1; 4110b3d881aSJohn Levon } 4120b3d881aSJohn Levon 4130b3d881aSJohn Levon static void vfio_user_recv(void *opaque) 4140b3d881aSJohn Levon { 4150b3d881aSJohn Levon VFIOUserProxy *proxy = opaque; 4160b3d881aSJohn Levon 4170b3d881aSJohn Levon QEMU_LOCK_GUARD(&proxy->lock); 4180b3d881aSJohn Levon 4190b3d881aSJohn Levon if (proxy->state == VFIO_PROXY_CONNECTED) { 4200b3d881aSJohn Levon Error *local_err = NULL; 4210b3d881aSJohn Levon 4220b3d881aSJohn Levon while (vfio_user_recv_one(proxy, &local_err) == 0) { 4230b3d881aSJohn Levon ; 4240b3d881aSJohn Levon } 4250b3d881aSJohn Levon 4260b3d881aSJohn Levon if (local_err != NULL) { 4270b3d881aSJohn Levon error_report_err(local_err); 4280b3d881aSJohn Levon } 4290b3d881aSJohn Levon } 4300b3d881aSJohn Levon } 4310b3d881aSJohn Levon 43236227628SJohn Levon /* 43336227628SJohn Levon * Send a single message, same return semantics as vfio_user_send_qio(). 43436227628SJohn Levon * 43536227628SJohn Levon * Sent async messages are freed, others are moved to pending queue. 43636227628SJohn Levon */ 43736227628SJohn Levon static ssize_t vfio_user_send_one(VFIOUserProxy *proxy, Error **errp) 43836227628SJohn Levon { 43936227628SJohn Levon VFIOUserMsg *msg; 44036227628SJohn Levon ssize_t ret; 44136227628SJohn Levon 44236227628SJohn Levon msg = QTAILQ_FIRST(&proxy->outgoing); 44336227628SJohn Levon ret = vfio_user_send_qio(proxy, msg, errp); 44436227628SJohn Levon if (ret < 0) { 44536227628SJohn Levon return ret; 44636227628SJohn Levon } 44736227628SJohn Levon 44836227628SJohn Levon QTAILQ_REMOVE(&proxy->outgoing, msg, next); 4491a0c32a9SJohn Levon proxy->num_outgoing--; 45036227628SJohn Levon if (msg->type == VFIO_MSG_ASYNC) { 45136227628SJohn Levon vfio_user_recycle(proxy, msg); 45236227628SJohn Levon } else { 45336227628SJohn Levon QTAILQ_INSERT_TAIL(&proxy->pending, msg, next); 45436227628SJohn Levon msg->pending = true; 45536227628SJohn Levon } 45636227628SJohn Levon 45736227628SJohn Levon return ret; 45836227628SJohn Levon } 45936227628SJohn Levon 46036227628SJohn Levon /* 46136227628SJohn Levon * Send messages from outgoing queue when the socket buffer has space. 46236227628SJohn Levon * If we deplete 'outgoing', remove ourselves from the poll list. 46336227628SJohn Levon */ 46436227628SJohn Levon static void vfio_user_send(void *opaque) 46536227628SJohn Levon { 46636227628SJohn Levon VFIOUserProxy *proxy = opaque; 46736227628SJohn Levon 46836227628SJohn Levon QEMU_LOCK_GUARD(&proxy->lock); 46936227628SJohn Levon 47036227628SJohn Levon if (proxy->state == VFIO_PROXY_CONNECTED) { 47136227628SJohn Levon while (!QTAILQ_EMPTY(&proxy->outgoing)) { 47236227628SJohn Levon Error *local_err = NULL; 47336227628SJohn Levon int ret; 47436227628SJohn Levon 47536227628SJohn Levon ret = vfio_user_send_one(proxy, &local_err); 47636227628SJohn Levon 47736227628SJohn Levon if (ret == QIO_CHANNEL_ERR_BLOCK) { 47836227628SJohn Levon return; 47936227628SJohn Levon } else if (ret == -1) { 48036227628SJohn Levon error_report_err(local_err); 48136227628SJohn Levon return; 48236227628SJohn Levon } 48336227628SJohn Levon } 48436227628SJohn Levon qio_channel_set_aio_fd_handler(proxy->ioc, proxy->ctx, 48536227628SJohn Levon vfio_user_recv, NULL, NULL, proxy); 4861a0c32a9SJohn Levon 4871a0c32a9SJohn Levon /* queue empty - send any pending multi write msgs */ 4881a0c32a9SJohn Levon if (proxy->wr_multi != NULL) { 4891a0c32a9SJohn Levon vfio_user_flush_multi(proxy); 4901a0c32a9SJohn Levon } 49136227628SJohn Levon } 49236227628SJohn Levon } 49336227628SJohn Levon 494*ea678844SJohn Levon static void vfio_user_close_cb(void *opaque) 495438d863fSJohn Levon { 496438d863fSJohn Levon VFIOUserProxy *proxy = opaque; 497438d863fSJohn Levon 498438d863fSJohn Levon QEMU_LOCK_GUARD(&proxy->lock); 499438d863fSJohn Levon 500438d863fSJohn Levon proxy->state = VFIO_PROXY_CLOSED; 501438d863fSJohn Levon qemu_cond_signal(&proxy->close_cv); 502438d863fSJohn Levon } 503438d863fSJohn Levon 504438d863fSJohn Levon 505438d863fSJohn Levon /* 506438d863fSJohn Levon * Functions called by main or CPU threads 507438d863fSJohn Levon */ 508438d863fSJohn Levon 5090b3d881aSJohn Levon /* 5100b3d881aSJohn Levon * Process incoming requests. 5110b3d881aSJohn Levon * 5120b3d881aSJohn Levon * The bus-specific callback has the form: 5130b3d881aSJohn Levon * request(opaque, msg) 5140b3d881aSJohn Levon * where 'opaque' was specified in vfio_user_set_handler 5150b3d881aSJohn Levon * and 'msg' is the inbound message. 5160b3d881aSJohn Levon * 5170b3d881aSJohn Levon * The callback is responsible for disposing of the message buffer, 5180b3d881aSJohn Levon * usually by re-using it when calling vfio_send_reply or vfio_send_error, 5190b3d881aSJohn Levon * both of which free their message buffer when the reply is sent. 5200b3d881aSJohn Levon * 5210b3d881aSJohn Levon * If the callback uses a new buffer, it needs to free the old one. 5220b3d881aSJohn Levon */ 5230b3d881aSJohn Levon static void vfio_user_request(void *opaque) 5240b3d881aSJohn Levon { 5250b3d881aSJohn Levon VFIOUserProxy *proxy = opaque; 5260b3d881aSJohn Levon VFIOUserMsgQ new, free; 5270b3d881aSJohn Levon VFIOUserMsg *msg, *m1; 5280b3d881aSJohn Levon 5290b3d881aSJohn Levon /* reap all incoming */ 5300b3d881aSJohn Levon QTAILQ_INIT(&new); 5310b3d881aSJohn Levon WITH_QEMU_LOCK_GUARD(&proxy->lock) { 5320b3d881aSJohn Levon QTAILQ_FOREACH_SAFE(msg, &proxy->incoming, next, m1) { 5330b3d881aSJohn Levon QTAILQ_REMOVE(&proxy->incoming, msg, next); 5340b3d881aSJohn Levon QTAILQ_INSERT_TAIL(&new, msg, next); 5350b3d881aSJohn Levon } 5360b3d881aSJohn Levon } 5370b3d881aSJohn Levon 5380b3d881aSJohn Levon /* process list */ 5390b3d881aSJohn Levon QTAILQ_INIT(&free); 5400b3d881aSJohn Levon QTAILQ_FOREACH_SAFE(msg, &new, next, m1) { 5410b3d881aSJohn Levon QTAILQ_REMOVE(&new, msg, next); 5420b3d881aSJohn Levon trace_vfio_user_recv_request(msg->hdr->command); 5430b3d881aSJohn Levon proxy->request(proxy->req_arg, msg); 5440b3d881aSJohn Levon QTAILQ_INSERT_HEAD(&free, msg, next); 5450b3d881aSJohn Levon } 5460b3d881aSJohn Levon 5470b3d881aSJohn Levon /* free list */ 5480b3d881aSJohn Levon WITH_QEMU_LOCK_GUARD(&proxy->lock) { 5490b3d881aSJohn Levon QTAILQ_FOREACH_SAFE(msg, &free, next, m1) { 5500b3d881aSJohn Levon vfio_user_recycle(proxy, msg); 5510b3d881aSJohn Levon } 5520b3d881aSJohn Levon } 5530b3d881aSJohn Levon } 5540b3d881aSJohn Levon 55536227628SJohn Levon /* 55636227628SJohn Levon * Messages are queued onto the proxy's outgoing list. 55736227628SJohn Levon * 55836227628SJohn Levon * It handles 3 types of messages: 55936227628SJohn Levon * 56036227628SJohn Levon * async messages - replies and posted writes 56136227628SJohn Levon * 56236227628SJohn Levon * There will be no reply from the server, so message 56336227628SJohn Levon * buffers are freed after they're sent. 56436227628SJohn Levon * 56536227628SJohn Levon * nowait messages - map/unmap during address space transactions 56636227628SJohn Levon * 56736227628SJohn Levon * These are also sent async, but a reply is expected so that 56836227628SJohn Levon * vfio_wait_reqs() can wait for the youngest nowait request. 56936227628SJohn Levon * They transition from the outgoing list to the pending list 57036227628SJohn Levon * when sent, and are freed when the reply is received. 57136227628SJohn Levon * 57236227628SJohn Levon * wait messages - all other requests 57336227628SJohn Levon * 57436227628SJohn Levon * The reply to these messages is waited for by their caller. 57536227628SJohn Levon * They also transition from outgoing to pending when sent, but 57636227628SJohn Levon * the message buffer is returned to the caller with the reply 57736227628SJohn Levon * contents. The caller is responsible for freeing these messages. 57836227628SJohn Levon * 57936227628SJohn Levon * As an optimization, if the outgoing list and the socket send 58036227628SJohn Levon * buffer are empty, the message is sent inline instead of being 58136227628SJohn Levon * added to the outgoing list. The rest of the transitions are 58236227628SJohn Levon * unchanged. 58336227628SJohn Levon */ 58436227628SJohn Levon static bool vfio_user_send_queued(VFIOUserProxy *proxy, VFIOUserMsg *msg, 58536227628SJohn Levon Error **errp) 58636227628SJohn Levon { 58736227628SJohn Levon int ret; 58836227628SJohn Levon 5891a0c32a9SJohn Levon /* older coalesced writes go first */ 5901a0c32a9SJohn Levon if (proxy->wr_multi != NULL && 5911a0c32a9SJohn Levon ((msg->hdr->flags & VFIO_USER_TYPE) == VFIO_USER_REQUEST)) { 5921a0c32a9SJohn Levon vfio_user_flush_multi(proxy); 5931a0c32a9SJohn Levon } 5941a0c32a9SJohn Levon 59536227628SJohn Levon /* 59636227628SJohn Levon * Unsent outgoing msgs - add to tail 59736227628SJohn Levon */ 59836227628SJohn Levon if (!QTAILQ_EMPTY(&proxy->outgoing)) { 59936227628SJohn Levon QTAILQ_INSERT_TAIL(&proxy->outgoing, msg, next); 6001a0c32a9SJohn Levon proxy->num_outgoing++; 60136227628SJohn Levon return true; 60236227628SJohn Levon } 60336227628SJohn Levon 60436227628SJohn Levon /* 60536227628SJohn Levon * Try inline - if blocked, queue it and kick send poller 60636227628SJohn Levon */ 60736227628SJohn Levon if (proxy->flags & VFIO_PROXY_FORCE_QUEUED) { 60836227628SJohn Levon ret = QIO_CHANNEL_ERR_BLOCK; 60936227628SJohn Levon } else { 61036227628SJohn Levon ret = vfio_user_send_qio(proxy, msg, errp); 61136227628SJohn Levon } 61236227628SJohn Levon 61336227628SJohn Levon if (ret == QIO_CHANNEL_ERR_BLOCK) { 61436227628SJohn Levon QTAILQ_INSERT_HEAD(&proxy->outgoing, msg, next); 6151a0c32a9SJohn Levon proxy->num_outgoing = 1; 61636227628SJohn Levon qio_channel_set_aio_fd_handler(proxy->ioc, proxy->ctx, 61736227628SJohn Levon vfio_user_recv, proxy->ctx, 61836227628SJohn Levon vfio_user_send, proxy); 61936227628SJohn Levon return true; 62036227628SJohn Levon } 62136227628SJohn Levon if (ret == -1) { 62236227628SJohn Levon return false; 62336227628SJohn Levon } 62436227628SJohn Levon 62536227628SJohn Levon /* 62636227628SJohn Levon * Sent - free async, add others to pending 62736227628SJohn Levon */ 62836227628SJohn Levon if (msg->type == VFIO_MSG_ASYNC) { 62936227628SJohn Levon vfio_user_recycle(proxy, msg); 63036227628SJohn Levon } else { 63136227628SJohn Levon QTAILQ_INSERT_TAIL(&proxy->pending, msg, next); 63236227628SJohn Levon msg->pending = true; 63336227628SJohn Levon } 63436227628SJohn Levon 63536227628SJohn Levon return true; 63636227628SJohn Levon } 63736227628SJohn Levon 63836227628SJohn Levon /* 63918e899e6SJohn Levon * nowait send - vfio_wait_reqs() can wait for it later 64018e899e6SJohn Levon * 64118e899e6SJohn Levon * Returns false if we did not successfully receive a reply message, in which 64218e899e6SJohn Levon * case @errp will be populated. 64318e899e6SJohn Levon * 64418e899e6SJohn Levon * In either case, ownership of @hdr and @fds is taken, and the caller must 64518e899e6SJohn Levon * *not* free them itself. 64618e899e6SJohn Levon */ 64718e899e6SJohn Levon bool vfio_user_send_nowait(VFIOUserProxy *proxy, VFIOUserHdr *hdr, 64818e899e6SJohn Levon VFIOUserFDs *fds, int rsize, Error **errp) 64918e899e6SJohn Levon { 65018e899e6SJohn Levon VFIOUserMsg *msg; 65118e899e6SJohn Levon 65218e899e6SJohn Levon QEMU_LOCK_GUARD(&proxy->lock); 65318e899e6SJohn Levon 65418e899e6SJohn Levon msg = vfio_user_getmsg(proxy, hdr, fds); 65518e899e6SJohn Levon msg->id = hdr->id; 65618e899e6SJohn Levon msg->rsize = rsize ? rsize : hdr->size; 65718e899e6SJohn Levon msg->type = VFIO_MSG_NOWAIT; 65818e899e6SJohn Levon 65918e899e6SJohn Levon if (hdr->flags & VFIO_USER_NO_REPLY) { 66018e899e6SJohn Levon error_setg_errno(errp, EINVAL, "%s on NO_REPLY message", __func__); 66118e899e6SJohn Levon vfio_user_recycle(proxy, msg); 66218e899e6SJohn Levon return false; 66318e899e6SJohn Levon } 66418e899e6SJohn Levon 66518e899e6SJohn Levon if (!vfio_user_send_queued(proxy, msg, errp)) { 66618e899e6SJohn Levon vfio_user_recycle(proxy, msg); 66718e899e6SJohn Levon return false; 66818e899e6SJohn Levon } 66918e899e6SJohn Levon 67018e899e6SJohn Levon proxy->last_nowait = msg; 67118e899e6SJohn Levon 67218e899e6SJohn Levon return true; 67318e899e6SJohn Levon } 67418e899e6SJohn Levon 67518e899e6SJohn Levon /* 67636227628SJohn Levon * Returns false if we did not successfully receive a reply message, in which 67736227628SJohn Levon * case @errp will be populated. 67836227628SJohn Levon * 67936227628SJohn Levon * In either case, the caller must free @hdr and @fds if needed. 68036227628SJohn Levon */ 6813bdb738bSJohn Levon bool vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr, 68236227628SJohn Levon VFIOUserFDs *fds, int rsize, Error **errp) 68336227628SJohn Levon { 68436227628SJohn Levon VFIOUserMsg *msg; 68536227628SJohn Levon bool ok = false; 68636227628SJohn Levon 68736227628SJohn Levon if (hdr->flags & VFIO_USER_NO_REPLY) { 68836227628SJohn Levon error_setg_errno(errp, EINVAL, "%s on NO_REPLY message", __func__); 68936227628SJohn Levon return false; 69036227628SJohn Levon } 69136227628SJohn Levon 69236227628SJohn Levon qemu_mutex_lock(&proxy->lock); 69336227628SJohn Levon 69436227628SJohn Levon msg = vfio_user_getmsg(proxy, hdr, fds); 69536227628SJohn Levon msg->id = hdr->id; 69636227628SJohn Levon msg->rsize = rsize ? rsize : hdr->size; 69736227628SJohn Levon msg->type = VFIO_MSG_WAIT; 69836227628SJohn Levon 69936227628SJohn Levon ok = vfio_user_send_queued(proxy, msg, errp); 70036227628SJohn Levon 70136227628SJohn Levon if (ok) { 70236227628SJohn Levon while (!msg->complete) { 7033358d926SJohn Levon if (!qemu_cond_timedwait(&msg->cv, &proxy->lock, 7043358d926SJohn Levon proxy->wait_time)) { 70536227628SJohn Levon VFIOUserMsgQ *list; 70636227628SJohn Levon 70736227628SJohn Levon list = msg->pending ? &proxy->pending : &proxy->outgoing; 70836227628SJohn Levon QTAILQ_REMOVE(list, msg, next); 70936227628SJohn Levon error_setg_errno(errp, ETIMEDOUT, 71036227628SJohn Levon "timed out waiting for reply"); 71136227628SJohn Levon ok = false; 71236227628SJohn Levon break; 71336227628SJohn Levon } 71436227628SJohn Levon } 71536227628SJohn Levon } 71636227628SJohn Levon 71736227628SJohn Levon vfio_user_recycle(proxy, msg); 71836227628SJohn Levon 71936227628SJohn Levon qemu_mutex_unlock(&proxy->lock); 72036227628SJohn Levon 72136227628SJohn Levon return ok; 72236227628SJohn Levon } 7230b3d881aSJohn Levon 724c6ac52a4SJohn Levon /* 725c6ac52a4SJohn Levon * async send - msg can be queued, but will be freed when sent 726c6ac52a4SJohn Levon * 727c6ac52a4SJohn Levon * Returns false on failure, in which case @errp will be populated. 728c6ac52a4SJohn Levon * 729c6ac52a4SJohn Levon * In either case, ownership of @hdr and @fds is taken, and the caller must 730c6ac52a4SJohn Levon * *not* free them itself. 731c6ac52a4SJohn Levon */ 73298a906d9SJohn Levon bool vfio_user_send_async(VFIOUserProxy *proxy, VFIOUserHdr *hdr, 733c6ac52a4SJohn Levon VFIOUserFDs *fds, Error **errp) 734c6ac52a4SJohn Levon { 735c6ac52a4SJohn Levon VFIOUserMsg *msg; 736c6ac52a4SJohn Levon 737c6ac52a4SJohn Levon QEMU_LOCK_GUARD(&proxy->lock); 738c6ac52a4SJohn Levon 739c6ac52a4SJohn Levon msg = vfio_user_getmsg(proxy, hdr, fds); 740c6ac52a4SJohn Levon msg->id = hdr->id; 741c6ac52a4SJohn Levon msg->rsize = 0; 742c6ac52a4SJohn Levon msg->type = VFIO_MSG_ASYNC; 743c6ac52a4SJohn Levon 744c6ac52a4SJohn Levon if (!(hdr->flags & (VFIO_USER_NO_REPLY | VFIO_USER_REPLY))) { 745c6ac52a4SJohn Levon error_setg_errno(errp, EINVAL, "%s on sync message", __func__); 746c6ac52a4SJohn Levon vfio_user_recycle(proxy, msg); 747c6ac52a4SJohn Levon return false; 748c6ac52a4SJohn Levon } 749c6ac52a4SJohn Levon 750c6ac52a4SJohn Levon if (!vfio_user_send_queued(proxy, msg, errp)) { 751c6ac52a4SJohn Levon vfio_user_recycle(proxy, msg); 752c6ac52a4SJohn Levon return false; 753c6ac52a4SJohn Levon } 754c6ac52a4SJohn Levon 755c6ac52a4SJohn Levon return true; 756c6ac52a4SJohn Levon } 757c6ac52a4SJohn Levon 75818e899e6SJohn Levon void vfio_user_wait_reqs(VFIOUserProxy *proxy) 75918e899e6SJohn Levon { 76018e899e6SJohn Levon VFIOUserMsg *msg; 76118e899e6SJohn Levon 76218e899e6SJohn Levon /* 76318e899e6SJohn Levon * Any DMA map/unmap requests sent in the middle 76418e899e6SJohn Levon * of a memory region transaction were sent nowait. 76518e899e6SJohn Levon * Wait for them here. 76618e899e6SJohn Levon */ 76718e899e6SJohn Levon qemu_mutex_lock(&proxy->lock); 76818e899e6SJohn Levon if (proxy->last_nowait != NULL) { 76918e899e6SJohn Levon /* 77018e899e6SJohn Levon * Change type to WAIT to wait for reply 77118e899e6SJohn Levon */ 77218e899e6SJohn Levon msg = proxy->last_nowait; 77318e899e6SJohn Levon msg->type = VFIO_MSG_WAIT; 77418e899e6SJohn Levon proxy->last_nowait = NULL; 77518e899e6SJohn Levon while (!msg->complete) { 7763358d926SJohn Levon if (!qemu_cond_timedwait(&msg->cv, &proxy->lock, 7773358d926SJohn Levon proxy->wait_time)) { 77818e899e6SJohn Levon VFIOUserMsgQ *list; 77918e899e6SJohn Levon 78018e899e6SJohn Levon list = msg->pending ? &proxy->pending : &proxy->outgoing; 78118e899e6SJohn Levon QTAILQ_REMOVE(list, msg, next); 78218e899e6SJohn Levon error_printf("vfio_wait_reqs - timed out\n"); 78318e899e6SJohn Levon break; 78418e899e6SJohn Levon } 78518e899e6SJohn Levon } 78618e899e6SJohn Levon 78718e899e6SJohn Levon if (msg->hdr->flags & VFIO_USER_ERROR) { 78818e899e6SJohn Levon error_printf("vfio_user_wait_reqs - error reply on async "); 78918e899e6SJohn Levon error_printf("request: command %x error %s\n", msg->hdr->command, 79018e899e6SJohn Levon strerror(msg->hdr->error_reply)); 79118e899e6SJohn Levon } 79218e899e6SJohn Levon 79318e899e6SJohn Levon /* 79418e899e6SJohn Levon * Change type back to NOWAIT to free 79518e899e6SJohn Levon */ 79618e899e6SJohn Levon msg->type = VFIO_MSG_NOWAIT; 79718e899e6SJohn Levon vfio_user_recycle(proxy, msg); 79818e899e6SJohn Levon } 79918e899e6SJohn Levon 80018e899e6SJohn Levon qemu_mutex_unlock(&proxy->lock); 80118e899e6SJohn Levon } 80218e899e6SJohn Levon 803c6ac52a4SJohn Levon /* 804c6ac52a4SJohn Levon * Reply to an incoming request. 805c6ac52a4SJohn Levon */ 806c6ac52a4SJohn Levon void vfio_user_send_reply(VFIOUserProxy *proxy, VFIOUserHdr *hdr, int size) 807c6ac52a4SJohn Levon { 808c6ac52a4SJohn Levon Error *local_err = NULL; 809c6ac52a4SJohn Levon 810c6ac52a4SJohn Levon if (size < sizeof(VFIOUserHdr)) { 811c6ac52a4SJohn Levon error_printf("%s: size too small", __func__); 812c6ac52a4SJohn Levon g_free(hdr); 813c6ac52a4SJohn Levon return; 814c6ac52a4SJohn Levon } 815c6ac52a4SJohn Levon 816c6ac52a4SJohn Levon /* 817c6ac52a4SJohn Levon * convert header to associated reply 818c6ac52a4SJohn Levon */ 819c6ac52a4SJohn Levon hdr->flags = VFIO_USER_REPLY; 820c6ac52a4SJohn Levon hdr->size = size; 821c6ac52a4SJohn Levon 822c6ac52a4SJohn Levon if (!vfio_user_send_async(proxy, hdr, NULL, &local_err)) { 823c6ac52a4SJohn Levon error_report_err(local_err); 824c6ac52a4SJohn Levon } 825c6ac52a4SJohn Levon } 826c6ac52a4SJohn Levon 827c6ac52a4SJohn Levon /* 828c6ac52a4SJohn Levon * Send an error reply to an incoming request. 829c6ac52a4SJohn Levon */ 830c6ac52a4SJohn Levon void vfio_user_send_error(VFIOUserProxy *proxy, VFIOUserHdr *hdr, int error) 831c6ac52a4SJohn Levon { 832c6ac52a4SJohn Levon Error *local_err = NULL; 833c6ac52a4SJohn Levon 834c6ac52a4SJohn Levon /* 835c6ac52a4SJohn Levon * convert header to associated reply 836c6ac52a4SJohn Levon */ 837c6ac52a4SJohn Levon hdr->flags = VFIO_USER_REPLY; 838c6ac52a4SJohn Levon hdr->flags |= VFIO_USER_ERROR; 839c6ac52a4SJohn Levon hdr->error_reply = error; 840c6ac52a4SJohn Levon hdr->size = sizeof(*hdr); 841c6ac52a4SJohn Levon 842c6ac52a4SJohn Levon if (!vfio_user_send_async(proxy, hdr, NULL, &local_err)) { 843c6ac52a4SJohn Levon error_report_err(local_err); 844c6ac52a4SJohn Levon } 845c6ac52a4SJohn Levon } 846c6ac52a4SJohn Levon 847c6ac52a4SJohn Levon /* 848c6ac52a4SJohn Levon * Close FDs erroneously received in an incoming request. 849c6ac52a4SJohn Levon */ 850c6ac52a4SJohn Levon void vfio_user_putfds(VFIOUserMsg *msg) 851c6ac52a4SJohn Levon { 852c6ac52a4SJohn Levon VFIOUserFDs *fds = msg->fds; 853c6ac52a4SJohn Levon int i; 854c6ac52a4SJohn Levon 855c6ac52a4SJohn Levon for (i = 0; i < fds->recv_fds; i++) { 856c6ac52a4SJohn Levon close(fds->fds[i]); 857c6ac52a4SJohn Levon } 858c6ac52a4SJohn Levon g_free(fds); 859c6ac52a4SJohn Levon msg->fds = NULL; 860c6ac52a4SJohn Levon } 861c6ac52a4SJohn Levon 86298a906d9SJohn Levon void 86398a906d9SJohn Levon vfio_user_disable_posted_writes(VFIOUserProxy *proxy) 86498a906d9SJohn Levon { 86598a906d9SJohn Levon WITH_QEMU_LOCK_GUARD(&proxy->lock) { 86698a906d9SJohn Levon proxy->flags |= VFIO_PROXY_NO_POST; 86798a906d9SJohn Levon } 86898a906d9SJohn Levon } 86998a906d9SJohn Levon 870438d863fSJohn Levon static QLIST_HEAD(, VFIOUserProxy) vfio_user_sockets = 871438d863fSJohn Levon QLIST_HEAD_INITIALIZER(vfio_user_sockets); 872438d863fSJohn Levon 873438d863fSJohn Levon VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp) 874438d863fSJohn Levon { 875438d863fSJohn Levon VFIOUserProxy *proxy; 876438d863fSJohn Levon QIOChannelSocket *sioc; 877438d863fSJohn Levon QIOChannel *ioc; 878438d863fSJohn Levon char *sockname; 879438d863fSJohn Levon 880438d863fSJohn Levon if (addr->type != SOCKET_ADDRESS_TYPE_UNIX) { 881438d863fSJohn Levon error_setg(errp, "vfio_user_connect - bad address family"); 882438d863fSJohn Levon return NULL; 883438d863fSJohn Levon } 884438d863fSJohn Levon sockname = addr->u.q_unix.path; 885438d863fSJohn Levon 886438d863fSJohn Levon sioc = qio_channel_socket_new(); 887438d863fSJohn Levon ioc = QIO_CHANNEL(sioc); 888438d863fSJohn Levon if (qio_channel_socket_connect_sync(sioc, addr, errp)) { 889438d863fSJohn Levon object_unref(OBJECT(ioc)); 890438d863fSJohn Levon return NULL; 891438d863fSJohn Levon } 892438d863fSJohn Levon qio_channel_set_blocking(ioc, false, NULL); 893438d863fSJohn Levon 894438d863fSJohn Levon proxy = g_malloc0(sizeof(VFIOUserProxy)); 895438d863fSJohn Levon proxy->sockname = g_strdup_printf("unix:%s", sockname); 896438d863fSJohn Levon proxy->ioc = ioc; 89736227628SJohn Levon 89836227628SJohn Levon /* init defaults */ 89936227628SJohn Levon proxy->max_xfer_size = VFIO_USER_DEF_MAX_XFER; 90036227628SJohn Levon proxy->max_send_fds = VFIO_USER_DEF_MAX_FDS; 90136227628SJohn Levon proxy->max_dma = VFIO_USER_DEF_MAP_MAX; 90236227628SJohn Levon proxy->dma_pgsizes = VFIO_USER_DEF_PGSIZE; 90336227628SJohn Levon proxy->max_bitmap = VFIO_USER_DEF_MAX_BITMAP; 90436227628SJohn Levon proxy->migr_pgsize = VFIO_USER_DEF_PGSIZE; 90536227628SJohn Levon 906438d863fSJohn Levon proxy->flags = VFIO_PROXY_CLIENT; 907438d863fSJohn Levon proxy->state = VFIO_PROXY_CONNECTED; 908438d863fSJohn Levon 909438d863fSJohn Levon qemu_mutex_init(&proxy->lock); 910438d863fSJohn Levon qemu_cond_init(&proxy->close_cv); 911438d863fSJohn Levon 912438d863fSJohn Levon if (vfio_user_iothread == NULL) { 913438d863fSJohn Levon vfio_user_iothread = iothread_create("VFIO user", errp); 914438d863fSJohn Levon } 915438d863fSJohn Levon 916438d863fSJohn Levon proxy->ctx = iothread_get_aio_context(vfio_user_iothread); 9170b3d881aSJohn Levon proxy->req_bh = qemu_bh_new(vfio_user_request, proxy); 918438d863fSJohn Levon 919438d863fSJohn Levon QTAILQ_INIT(&proxy->outgoing); 920438d863fSJohn Levon QTAILQ_INIT(&proxy->incoming); 921438d863fSJohn Levon QTAILQ_INIT(&proxy->free); 922438d863fSJohn Levon QTAILQ_INIT(&proxy->pending); 923438d863fSJohn Levon QLIST_INSERT_HEAD(&vfio_user_sockets, proxy, next); 924438d863fSJohn Levon 925438d863fSJohn Levon return proxy; 926438d863fSJohn Levon } 927438d863fSJohn Levon 9280b3d881aSJohn Levon void vfio_user_set_handler(VFIODevice *vbasedev, 9290b3d881aSJohn Levon void (*handler)(void *opaque, VFIOUserMsg *msg), 9300b3d881aSJohn Levon void *req_arg) 9310b3d881aSJohn Levon { 9320b3d881aSJohn Levon VFIOUserProxy *proxy = vbasedev->proxy; 9330b3d881aSJohn Levon 9340b3d881aSJohn Levon proxy->request = handler; 9350b3d881aSJohn Levon proxy->req_arg = req_arg; 9360b3d881aSJohn Levon qio_channel_set_aio_fd_handler(proxy->ioc, proxy->ctx, 9370b3d881aSJohn Levon vfio_user_recv, NULL, NULL, proxy); 9380b3d881aSJohn Levon } 9390b3d881aSJohn Levon 940438d863fSJohn Levon void vfio_user_disconnect(VFIOUserProxy *proxy) 941438d863fSJohn Levon { 942438d863fSJohn Levon VFIOUserMsg *r1, *r2; 943438d863fSJohn Levon 944438d863fSJohn Levon qemu_mutex_lock(&proxy->lock); 945438d863fSJohn Levon 946438d863fSJohn Levon /* our side is quitting */ 947438d863fSJohn Levon if (proxy->state == VFIO_PROXY_CONNECTED) { 948438d863fSJohn Levon vfio_user_shutdown(proxy); 949438d863fSJohn Levon if (!QTAILQ_EMPTY(&proxy->pending)) { 950438d863fSJohn Levon error_printf("vfio_user_disconnect: outstanding requests\n"); 951438d863fSJohn Levon } 952438d863fSJohn Levon } 953438d863fSJohn Levon object_unref(OBJECT(proxy->ioc)); 954438d863fSJohn Levon proxy->ioc = NULL; 9550b3d881aSJohn Levon qemu_bh_delete(proxy->req_bh); 9560b3d881aSJohn Levon proxy->req_bh = NULL; 957438d863fSJohn Levon 958438d863fSJohn Levon proxy->state = VFIO_PROXY_CLOSING; 959438d863fSJohn Levon QTAILQ_FOREACH_SAFE(r1, &proxy->outgoing, next, r2) { 960438d863fSJohn Levon qemu_cond_destroy(&r1->cv); 961438d863fSJohn Levon QTAILQ_REMOVE(&proxy->outgoing, r1, next); 962438d863fSJohn Levon g_free(r1); 963438d863fSJohn Levon } 964438d863fSJohn Levon QTAILQ_FOREACH_SAFE(r1, &proxy->incoming, next, r2) { 965438d863fSJohn Levon qemu_cond_destroy(&r1->cv); 966438d863fSJohn Levon QTAILQ_REMOVE(&proxy->incoming, r1, next); 967438d863fSJohn Levon g_free(r1); 968438d863fSJohn Levon } 969438d863fSJohn Levon QTAILQ_FOREACH_SAFE(r1, &proxy->pending, next, r2) { 970438d863fSJohn Levon qemu_cond_destroy(&r1->cv); 971438d863fSJohn Levon QTAILQ_REMOVE(&proxy->pending, r1, next); 972438d863fSJohn Levon g_free(r1); 973438d863fSJohn Levon } 974438d863fSJohn Levon QTAILQ_FOREACH_SAFE(r1, &proxy->free, next, r2) { 975438d863fSJohn Levon qemu_cond_destroy(&r1->cv); 976438d863fSJohn Levon QTAILQ_REMOVE(&proxy->free, r1, next); 977438d863fSJohn Levon g_free(r1); 978438d863fSJohn Levon } 979438d863fSJohn Levon 980438d863fSJohn Levon /* 981438d863fSJohn Levon * Make sure the iothread isn't blocking anywhere 982438d863fSJohn Levon * with a ref to this proxy by waiting for a BH 983438d863fSJohn Levon * handler to run after the proxy fd handlers were 984438d863fSJohn Levon * deleted above. 985438d863fSJohn Levon */ 986*ea678844SJohn Levon aio_bh_schedule_oneshot(proxy->ctx, vfio_user_close_cb, proxy); 987*ea678844SJohn Levon 988*ea678844SJohn Levon while (proxy->state != VFIO_PROXY_CLOSED) { 989438d863fSJohn Levon qemu_cond_wait(&proxy->close_cv, &proxy->lock); 990*ea678844SJohn Levon } 991438d863fSJohn Levon 992438d863fSJohn Levon /* we now hold the only ref to proxy */ 993438d863fSJohn Levon qemu_mutex_unlock(&proxy->lock); 994438d863fSJohn Levon qemu_cond_destroy(&proxy->close_cv); 995438d863fSJohn Levon qemu_mutex_destroy(&proxy->lock); 996438d863fSJohn Levon 997438d863fSJohn Levon QLIST_REMOVE(proxy, next); 998438d863fSJohn Levon if (QLIST_EMPTY(&vfio_user_sockets)) { 999438d863fSJohn Levon iothread_destroy(vfio_user_iothread); 1000438d863fSJohn Levon vfio_user_iothread = NULL; 1001438d863fSJohn Levon } 1002438d863fSJohn Levon 1003438d863fSJohn Levon g_free(proxy->sockname); 1004438d863fSJohn Levon g_free(proxy); 1005438d863fSJohn Levon } 100636227628SJohn Levon 10073bdb738bSJohn Levon void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd, 100836227628SJohn Levon uint32_t size, uint32_t flags) 100936227628SJohn Levon { 101036227628SJohn Levon static uint16_t next_id; 101136227628SJohn Levon 101236227628SJohn Levon hdr->id = qatomic_fetch_inc(&next_id); 101336227628SJohn Levon hdr->command = cmd; 101436227628SJohn Levon hdr->size = size; 101536227628SJohn Levon hdr->flags = (flags & ~VFIO_USER_TYPE) | VFIO_USER_REQUEST; 101636227628SJohn Levon hdr->error_reply = 0; 101736227628SJohn Levon } 101836227628SJohn Levon 101936227628SJohn Levon struct cap_entry { 102036227628SJohn Levon const char *name; 102136227628SJohn Levon bool (*check)(VFIOUserProxy *proxy, QObject *qobj, Error **errp); 102236227628SJohn Levon }; 102336227628SJohn Levon 102436227628SJohn Levon static bool caps_parse(VFIOUserProxy *proxy, QDict *qdict, 102536227628SJohn Levon struct cap_entry caps[], Error **errp) 102636227628SJohn Levon { 102736227628SJohn Levon QObject *qobj; 102836227628SJohn Levon struct cap_entry *p; 102936227628SJohn Levon 103036227628SJohn Levon for (p = caps; p->name != NULL; p++) { 103136227628SJohn Levon qobj = qdict_get(qdict, p->name); 103236227628SJohn Levon if (qobj != NULL) { 103336227628SJohn Levon if (!p->check(proxy, qobj, errp)) { 103436227628SJohn Levon return false; 103536227628SJohn Levon } 103636227628SJohn Levon qdict_del(qdict, p->name); 103736227628SJohn Levon } 103836227628SJohn Levon } 103936227628SJohn Levon 104036227628SJohn Levon /* warning, for now */ 104136227628SJohn Levon if (qdict_size(qdict) != 0) { 104236227628SJohn Levon warn_report("spurious capabilities"); 104336227628SJohn Levon } 104436227628SJohn Levon return true; 104536227628SJohn Levon } 104636227628SJohn Levon 104736227628SJohn Levon static bool check_migr_pgsize(VFIOUserProxy *proxy, QObject *qobj, Error **errp) 104836227628SJohn Levon { 104936227628SJohn Levon QNum *qn = qobject_to(QNum, qobj); 105036227628SJohn Levon uint64_t pgsize; 105136227628SJohn Levon 105236227628SJohn Levon if (qn == NULL || !qnum_get_try_uint(qn, &pgsize)) { 105336227628SJohn Levon error_setg(errp, "malformed %s", VFIO_USER_CAP_PGSIZE); 105436227628SJohn Levon return false; 105536227628SJohn Levon } 105636227628SJohn Levon 105736227628SJohn Levon /* must be larger than default */ 105836227628SJohn Levon if (pgsize & (VFIO_USER_DEF_PGSIZE - 1)) { 105936227628SJohn Levon error_setg(errp, "pgsize 0x%"PRIx64" too small", pgsize); 106036227628SJohn Levon return false; 106136227628SJohn Levon } 106236227628SJohn Levon 106336227628SJohn Levon proxy->migr_pgsize = pgsize; 106436227628SJohn Levon return true; 106536227628SJohn Levon } 106636227628SJohn Levon 106736227628SJohn Levon static bool check_bitmap(VFIOUserProxy *proxy, QObject *qobj, Error **errp) 106836227628SJohn Levon { 106936227628SJohn Levon QNum *qn = qobject_to(QNum, qobj); 107036227628SJohn Levon uint64_t bitmap_size; 107136227628SJohn Levon 107236227628SJohn Levon if (qn == NULL || !qnum_get_try_uint(qn, &bitmap_size)) { 107336227628SJohn Levon error_setg(errp, "malformed %s", VFIO_USER_CAP_MAX_BITMAP); 107436227628SJohn Levon return false; 107536227628SJohn Levon } 107636227628SJohn Levon 107736227628SJohn Levon /* can only lower it */ 107836227628SJohn Levon if (bitmap_size > VFIO_USER_DEF_MAX_BITMAP) { 107936227628SJohn Levon error_setg(errp, "%s too large", VFIO_USER_CAP_MAX_BITMAP); 108036227628SJohn Levon return false; 108136227628SJohn Levon } 108236227628SJohn Levon 108336227628SJohn Levon proxy->max_bitmap = bitmap_size; 108436227628SJohn Levon return true; 108536227628SJohn Levon } 108636227628SJohn Levon 108736227628SJohn Levon static struct cap_entry caps_migr[] = { 108836227628SJohn Levon { VFIO_USER_CAP_PGSIZE, check_migr_pgsize }, 108936227628SJohn Levon { VFIO_USER_CAP_MAX_BITMAP, check_bitmap }, 109036227628SJohn Levon { NULL } 109136227628SJohn Levon }; 109236227628SJohn Levon 109336227628SJohn Levon static bool check_max_fds(VFIOUserProxy *proxy, QObject *qobj, Error **errp) 109436227628SJohn Levon { 109536227628SJohn Levon QNum *qn = qobject_to(QNum, qobj); 109636227628SJohn Levon uint64_t max_send_fds; 109736227628SJohn Levon 109836227628SJohn Levon if (qn == NULL || !qnum_get_try_uint(qn, &max_send_fds) || 109936227628SJohn Levon max_send_fds > VFIO_USER_MAX_MAX_FDS) { 110036227628SJohn Levon error_setg(errp, "malformed %s", VFIO_USER_CAP_MAX_FDS); 110136227628SJohn Levon return false; 110236227628SJohn Levon } 110336227628SJohn Levon proxy->max_send_fds = max_send_fds; 110436227628SJohn Levon return true; 110536227628SJohn Levon } 110636227628SJohn Levon 110736227628SJohn Levon static bool check_max_xfer(VFIOUserProxy *proxy, QObject *qobj, Error **errp) 110836227628SJohn Levon { 110936227628SJohn Levon QNum *qn = qobject_to(QNum, qobj); 111036227628SJohn Levon uint64_t max_xfer_size; 111136227628SJohn Levon 111236227628SJohn Levon if (qn == NULL || !qnum_get_try_uint(qn, &max_xfer_size) || 111336227628SJohn Levon max_xfer_size > VFIO_USER_MAX_MAX_XFER) { 111436227628SJohn Levon error_setg(errp, "malformed %s", VFIO_USER_CAP_MAX_XFER); 111536227628SJohn Levon return false; 111636227628SJohn Levon } 111736227628SJohn Levon proxy->max_xfer_size = max_xfer_size; 111836227628SJohn Levon return true; 111936227628SJohn Levon } 112036227628SJohn Levon 112136227628SJohn Levon static bool check_pgsizes(VFIOUserProxy *proxy, QObject *qobj, Error **errp) 112236227628SJohn Levon { 112336227628SJohn Levon QNum *qn = qobject_to(QNum, qobj); 112436227628SJohn Levon uint64_t pgsizes; 112536227628SJohn Levon 112636227628SJohn Levon if (qn == NULL || !qnum_get_try_uint(qn, &pgsizes)) { 112736227628SJohn Levon error_setg(errp, "malformed %s", VFIO_USER_CAP_PGSIZES); 112836227628SJohn Levon return false; 112936227628SJohn Levon } 113036227628SJohn Levon 113136227628SJohn Levon /* must be larger than default */ 113236227628SJohn Levon if (pgsizes & (VFIO_USER_DEF_PGSIZE - 1)) { 113336227628SJohn Levon error_setg(errp, "pgsize 0x%"PRIx64" too small", pgsizes); 113436227628SJohn Levon return false; 113536227628SJohn Levon } 113636227628SJohn Levon 113736227628SJohn Levon proxy->dma_pgsizes = pgsizes; 113836227628SJohn Levon return true; 113936227628SJohn Levon } 114036227628SJohn Levon 114136227628SJohn Levon static bool check_max_dma(VFIOUserProxy *proxy, QObject *qobj, Error **errp) 114236227628SJohn Levon { 114336227628SJohn Levon QNum *qn = qobject_to(QNum, qobj); 114436227628SJohn Levon uint64_t max_dma; 114536227628SJohn Levon 114636227628SJohn Levon if (qn == NULL || !qnum_get_try_uint(qn, &max_dma)) { 114736227628SJohn Levon error_setg(errp, "malformed %s", VFIO_USER_CAP_MAP_MAX); 114836227628SJohn Levon return false; 114936227628SJohn Levon } 115036227628SJohn Levon 115136227628SJohn Levon /* can only lower it */ 115236227628SJohn Levon if (max_dma > VFIO_USER_DEF_MAP_MAX) { 115336227628SJohn Levon error_setg(errp, "%s too large", VFIO_USER_CAP_MAP_MAX); 115436227628SJohn Levon return false; 115536227628SJohn Levon } 115636227628SJohn Levon 115736227628SJohn Levon proxy->max_dma = max_dma; 115836227628SJohn Levon return true; 115936227628SJohn Levon } 116036227628SJohn Levon 116136227628SJohn Levon static bool check_migr(VFIOUserProxy *proxy, QObject *qobj, Error **errp) 116236227628SJohn Levon { 116336227628SJohn Levon QDict *qdict = qobject_to(QDict, qobj); 116436227628SJohn Levon 116536227628SJohn Levon if (qdict == NULL) { 116636227628SJohn Levon error_setg(errp, "malformed %s", VFIO_USER_CAP_MAX_FDS); 116736227628SJohn Levon return true; 116836227628SJohn Levon } 116936227628SJohn Levon return caps_parse(proxy, qdict, caps_migr, errp); 117036227628SJohn Levon } 117136227628SJohn Levon 11721a0c32a9SJohn Levon static bool check_multi(VFIOUserProxy *proxy, QObject *qobj, Error **errp) 11731a0c32a9SJohn Levon { 11741a0c32a9SJohn Levon QBool *qb = qobject_to(QBool, qobj); 11751a0c32a9SJohn Levon 11761a0c32a9SJohn Levon if (qb == NULL) { 11771a0c32a9SJohn Levon error_setg(errp, "malformed %s", VFIO_USER_CAP_MULTI); 11781a0c32a9SJohn Levon return false; 11791a0c32a9SJohn Levon } 11801a0c32a9SJohn Levon if (qbool_get_bool(qb)) { 11811a0c32a9SJohn Levon proxy->flags |= VFIO_PROXY_USE_MULTI; 11821a0c32a9SJohn Levon } 11831a0c32a9SJohn Levon return true; 11841a0c32a9SJohn Levon } 11851a0c32a9SJohn Levon 118636227628SJohn Levon static struct cap_entry caps_cap[] = { 118736227628SJohn Levon { VFIO_USER_CAP_MAX_FDS, check_max_fds }, 118836227628SJohn Levon { VFIO_USER_CAP_MAX_XFER, check_max_xfer }, 118936227628SJohn Levon { VFIO_USER_CAP_PGSIZES, check_pgsizes }, 119036227628SJohn Levon { VFIO_USER_CAP_MAP_MAX, check_max_dma }, 119136227628SJohn Levon { VFIO_USER_CAP_MIGR, check_migr }, 11921a0c32a9SJohn Levon { VFIO_USER_CAP_MULTI, check_multi }, 119336227628SJohn Levon { NULL } 119436227628SJohn Levon }; 119536227628SJohn Levon 119636227628SJohn Levon static bool check_cap(VFIOUserProxy *proxy, QObject *qobj, Error **errp) 119736227628SJohn Levon { 119836227628SJohn Levon QDict *qdict = qobject_to(QDict, qobj); 119936227628SJohn Levon 120036227628SJohn Levon if (qdict == NULL) { 120136227628SJohn Levon error_setg(errp, "malformed %s", VFIO_USER_CAP); 120236227628SJohn Levon return false; 120336227628SJohn Levon } 120436227628SJohn Levon return caps_parse(proxy, qdict, caps_cap, errp); 120536227628SJohn Levon } 120636227628SJohn Levon 120736227628SJohn Levon static struct cap_entry ver_0_0[] = { 120836227628SJohn Levon { VFIO_USER_CAP, check_cap }, 120936227628SJohn Levon { NULL } 121036227628SJohn Levon }; 121136227628SJohn Levon 121236227628SJohn Levon static bool caps_check(VFIOUserProxy *proxy, int minor, const char *caps, 121336227628SJohn Levon Error **errp) 121436227628SJohn Levon { 121536227628SJohn Levon QObject *qobj; 121636227628SJohn Levon QDict *qdict; 121736227628SJohn Levon bool ret; 121836227628SJohn Levon 121936227628SJohn Levon qobj = qobject_from_json(caps, NULL); 122036227628SJohn Levon if (qobj == NULL) { 122136227628SJohn Levon error_setg(errp, "malformed capabilities %s", caps); 122236227628SJohn Levon return false; 122336227628SJohn Levon } 122436227628SJohn Levon qdict = qobject_to(QDict, qobj); 122536227628SJohn Levon if (qdict == NULL) { 122636227628SJohn Levon error_setg(errp, "capabilities %s not an object", caps); 122736227628SJohn Levon qobject_unref(qobj); 122836227628SJohn Levon return false; 122936227628SJohn Levon } 123036227628SJohn Levon ret = caps_parse(proxy, qdict, ver_0_0, errp); 123136227628SJohn Levon 123236227628SJohn Levon qobject_unref(qobj); 123336227628SJohn Levon return ret; 123436227628SJohn Levon } 123536227628SJohn Levon 123636227628SJohn Levon static GString *caps_json(void) 123736227628SJohn Levon { 123836227628SJohn Levon QDict *dict = qdict_new(); 123936227628SJohn Levon QDict *capdict = qdict_new(); 124036227628SJohn Levon QDict *migdict = qdict_new(); 124136227628SJohn Levon GString *str; 124236227628SJohn Levon 124336227628SJohn Levon qdict_put_int(migdict, VFIO_USER_CAP_PGSIZE, VFIO_USER_DEF_PGSIZE); 124436227628SJohn Levon qdict_put_int(migdict, VFIO_USER_CAP_MAX_BITMAP, VFIO_USER_DEF_MAX_BITMAP); 124536227628SJohn Levon qdict_put_obj(capdict, VFIO_USER_CAP_MIGR, QOBJECT(migdict)); 124636227628SJohn Levon 124736227628SJohn Levon qdict_put_int(capdict, VFIO_USER_CAP_MAX_FDS, VFIO_USER_MAX_MAX_FDS); 124836227628SJohn Levon qdict_put_int(capdict, VFIO_USER_CAP_MAX_XFER, VFIO_USER_DEF_MAX_XFER); 124936227628SJohn Levon qdict_put_int(capdict, VFIO_USER_CAP_PGSIZES, VFIO_USER_DEF_PGSIZE); 125036227628SJohn Levon qdict_put_int(capdict, VFIO_USER_CAP_MAP_MAX, VFIO_USER_DEF_MAP_MAX); 12511a0c32a9SJohn Levon qdict_put_bool(capdict, VFIO_USER_CAP_MULTI, true); 125236227628SJohn Levon 125336227628SJohn Levon qdict_put_obj(dict, VFIO_USER_CAP, QOBJECT(capdict)); 125436227628SJohn Levon 125536227628SJohn Levon str = qobject_to_json(QOBJECT(dict)); 125636227628SJohn Levon qobject_unref(dict); 125736227628SJohn Levon return str; 125836227628SJohn Levon } 125936227628SJohn Levon 126036227628SJohn Levon bool vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp) 126136227628SJohn Levon { 126236227628SJohn Levon g_autofree VFIOUserVersion *msgp = NULL; 126336227628SJohn Levon GString *caps; 126436227628SJohn Levon char *reply; 126536227628SJohn Levon int size, caplen; 126636227628SJohn Levon 126736227628SJohn Levon caps = caps_json(); 126836227628SJohn Levon caplen = caps->len + 1; 126936227628SJohn Levon size = sizeof(*msgp) + caplen; 127036227628SJohn Levon msgp = g_malloc0(size); 127136227628SJohn Levon 127236227628SJohn Levon vfio_user_request_msg(&msgp->hdr, VFIO_USER_VERSION, size, 0); 127336227628SJohn Levon msgp->major = VFIO_USER_MAJOR_VER; 127436227628SJohn Levon msgp->minor = VFIO_USER_MINOR_VER; 127536227628SJohn Levon memcpy(&msgp->capabilities, caps->str, caplen); 127636227628SJohn Levon g_string_free(caps, true); 127736227628SJohn Levon trace_vfio_user_version(msgp->major, msgp->minor, msgp->capabilities); 127836227628SJohn Levon 127936227628SJohn Levon if (!vfio_user_send_wait(proxy, &msgp->hdr, NULL, 0, errp)) { 128036227628SJohn Levon return false; 128136227628SJohn Levon } 128236227628SJohn Levon 128336227628SJohn Levon if (msgp->hdr.flags & VFIO_USER_ERROR) { 128436227628SJohn Levon error_setg_errno(errp, msgp->hdr.error_reply, "version reply"); 128536227628SJohn Levon return false; 128636227628SJohn Levon } 128736227628SJohn Levon 128836227628SJohn Levon if (msgp->major != VFIO_USER_MAJOR_VER || 128936227628SJohn Levon msgp->minor > VFIO_USER_MINOR_VER) { 129036227628SJohn Levon error_setg(errp, "incompatible server version"); 129136227628SJohn Levon return false; 129236227628SJohn Levon } 129336227628SJohn Levon 129436227628SJohn Levon reply = msgp->capabilities; 129536227628SJohn Levon if (reply[msgp->hdr.size - sizeof(*msgp) - 1] != '\0') { 129636227628SJohn Levon error_setg(errp, "corrupt version reply"); 129736227628SJohn Levon return false; 129836227628SJohn Levon } 129936227628SJohn Levon 130036227628SJohn Levon if (!caps_check(proxy, msgp->minor, reply, errp)) { 130136227628SJohn Levon return false; 130236227628SJohn Levon } 130336227628SJohn Levon 130436227628SJohn Levon trace_vfio_user_version(msgp->major, msgp->minor, msgp->capabilities); 130536227628SJohn Levon return true; 130636227628SJohn Levon } 13071a0c32a9SJohn Levon 13081a0c32a9SJohn Levon void vfio_user_flush_multi(VFIOUserProxy *proxy) 13091a0c32a9SJohn Levon { 13101a0c32a9SJohn Levon VFIOUserMsg *msg; 13111a0c32a9SJohn Levon VFIOUserWRMulti *wm = proxy->wr_multi; 13121a0c32a9SJohn Levon Error *local_err = NULL; 13131a0c32a9SJohn Levon 13141a0c32a9SJohn Levon proxy->wr_multi = NULL; 13151a0c32a9SJohn Levon 13161a0c32a9SJohn Levon /* adjust size for actual # of writes */ 13171a0c32a9SJohn Levon wm->hdr.size -= (VFIO_USER_MULTI_MAX - wm->wr_cnt) * sizeof(VFIOUserWROne); 13181a0c32a9SJohn Levon 13191a0c32a9SJohn Levon msg = vfio_user_getmsg(proxy, &wm->hdr, NULL); 13201a0c32a9SJohn Levon msg->id = wm->hdr.id; 13211a0c32a9SJohn Levon msg->rsize = 0; 13221a0c32a9SJohn Levon msg->type = VFIO_MSG_ASYNC; 13231a0c32a9SJohn Levon trace_vfio_user_wrmulti("flush", wm->wr_cnt); 13241a0c32a9SJohn Levon 13251a0c32a9SJohn Levon if (!vfio_user_send_queued(proxy, msg, &local_err)) { 13261a0c32a9SJohn Levon error_report_err(local_err); 13271a0c32a9SJohn Levon vfio_user_recycle(proxy, msg); 13281a0c32a9SJohn Levon } 13291a0c32a9SJohn Levon } 13301a0c32a9SJohn Levon 13311a0c32a9SJohn Levon void vfio_user_create_multi(VFIOUserProxy *proxy) 13321a0c32a9SJohn Levon { 13331a0c32a9SJohn Levon VFIOUserWRMulti *wm; 13341a0c32a9SJohn Levon 13351a0c32a9SJohn Levon wm = g_malloc0(sizeof(*wm)); 13361a0c32a9SJohn Levon vfio_user_request_msg(&wm->hdr, VFIO_USER_REGION_WRITE_MULTI, 13371a0c32a9SJohn Levon sizeof(*wm), VFIO_USER_NO_REPLY); 13381a0c32a9SJohn Levon proxy->wr_multi = wm; 13391a0c32a9SJohn Levon } 13401a0c32a9SJohn Levon 13411a0c32a9SJohn Levon void vfio_user_add_multi(VFIOUserProxy *proxy, uint8_t index, 13421a0c32a9SJohn Levon off_t offset, uint32_t count, void *data) 13431a0c32a9SJohn Levon { 13441a0c32a9SJohn Levon VFIOUserWRMulti *wm = proxy->wr_multi; 13451a0c32a9SJohn Levon VFIOUserWROne *w1 = &wm->wrs[wm->wr_cnt]; 13461a0c32a9SJohn Levon 13471a0c32a9SJohn Levon w1->offset = offset; 13481a0c32a9SJohn Levon w1->region = index; 13491a0c32a9SJohn Levon w1->count = count; 13501a0c32a9SJohn Levon memcpy(&w1->data, data, count); 13511a0c32a9SJohn Levon 13521a0c32a9SJohn Levon wm->wr_cnt++; 13531a0c32a9SJohn Levon trace_vfio_user_wrmulti("add", wm->wr_cnt); 13541a0c32a9SJohn Levon if (wm->wr_cnt == VFIO_USER_MULTI_MAX || 13551a0c32a9SJohn Levon proxy->num_outgoing < VFIO_USER_OUT_LOW) { 13561a0c32a9SJohn Levon vfio_user_flush_multi(proxy); 13571a0c32a9SJohn Levon } 13581a0c32a9SJohn Levon } 1359