Re: [PATCH v1 20/24] vfio-user: dma read/write operations

2022-12-09 Thread John Levon
On Tue, Nov 08, 2022 at 03:13:42PM -0800, John Johnson wrote:

>  static void vfio_user_pci_process_req(void *opaque, VFIOUserMsg *msg)
>  {
> +VFIOPCIDevice *vdev = opaque;
> +VFIOUserHdr *hdr = msg->hdr;
> +
> +/* no incoming PCI requests pass FDs */
> +if (msg->fds != NULL) {
> +vfio_user_send_error(vdev->vbasedev.proxy, hdr, EINVAL);
> +vfio_user_putfds(msg);
> +return;
> +}
>  
> +switch (hdr->command) {
> +case VFIO_USER_DMA_READ:
> +vfio_user_dma_read(vdev, (VFIOUserDMARW *)hdr);
> +break;
> +case VFIO_USER_DMA_WRITE:
> +vfio_user_dma_write(vdev, (VFIOUserDMARW *)hdr);
> +break;
> +default:
> +error_printf("vfio_user_process_req unknown cmd %d\n", hdr->command);

__func__ or vfio_user_pci_process_req ?

regards
john



[PATCH v1 20/24] vfio-user: dma read/write operations

2022-11-08 Thread John Johnson
Messages from server to client that peform device DMA.

Signed-off-by: Elena Ufimtseva 
Signed-off-by: John G Johnson 
Signed-off-by: Jagannathan Raman 
---
 hw/vfio/pci.c   | 110 
 hw/vfio/user-protocol.h |  11 +
 hw/vfio/user.c  |  57 +
 hw/vfio/user.h  |   3 ++
 4 files changed, 181 insertions(+)

diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index ce6776b..559b20d 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -3550,6 +3550,95 @@ static void vfio_user_msix_teardown(VFIOPCIDevice *vdev)
 vdev->msix->pba_region = NULL;
 }
 
+static void vfio_user_dma_read(VFIOPCIDevice *vdev, VFIOUserDMARW *msg)
+{
+PCIDevice *pdev = >pdev;
+VFIOProxy *proxy = vdev->vbasedev.proxy;
+VFIOUserDMARW *res;
+MemTxResult r;
+size_t size;
+
+if (msg->hdr.size < sizeof(*msg)) {
+vfio_user_send_error(proxy, >hdr, EINVAL);
+return;
+}
+if (msg->count > proxy->max_xfer_size) {
+vfio_user_send_error(proxy, >hdr, E2BIG);
+return;
+}
+
+/* switch to our own message buffer */
+size = msg->count + sizeof(VFIOUserDMARW);
+res = g_malloc0(size);
+memcpy(res, msg, sizeof(*res));
+g_free(msg);
+
+r = pci_dma_read(pdev, res->offset, >data, res->count);
+
+switch (r) {
+case MEMTX_OK:
+if (res->hdr.flags & VFIO_USER_NO_REPLY) {
+g_free(res);
+return;
+}
+vfio_user_send_reply(proxy, >hdr, size);
+break;
+case MEMTX_ERROR:
+vfio_user_send_error(proxy, >hdr, EFAULT);
+break;
+case MEMTX_DECODE_ERROR:
+vfio_user_send_error(proxy, >hdr, ENODEV);
+break;
+case MEMTX_ACCESS_ERROR:
+vfio_user_send_error(proxy, >hdr, EPERM);
+break;
+default:
+error_printf("vfio_user_dma_read unknown error %d\n", r);
+vfio_user_send_error(vdev->vbasedev.proxy, >hdr, EINVAL);
+}
+}
+
+static void vfio_user_dma_write(VFIOPCIDevice *vdev, VFIOUserDMARW *msg)
+{
+PCIDevice *pdev = >pdev;
+VFIOProxy *proxy = vdev->vbasedev.proxy;
+MemTxResult r;
+
+if (msg->hdr.size < sizeof(*msg)) {
+vfio_user_send_error(proxy, >hdr, EINVAL);
+return;
+}
+/* make sure transfer count isn't larger than the message data */
+if (msg->count > msg->hdr.size - sizeof(*msg)) {
+vfio_user_send_error(proxy, >hdr, E2BIG);
+return;
+}
+
+r = pci_dma_write(pdev, msg->offset, >data, msg->count);
+
+switch (r) {
+case MEMTX_OK:
+if ((msg->hdr.flags & VFIO_USER_NO_REPLY) == 0) {
+vfio_user_send_reply(proxy, >hdr, sizeof(msg->hdr));
+} else {
+g_free(msg);
+}
+break;
+case MEMTX_ERROR:
+vfio_user_send_error(proxy, >hdr, EFAULT);
+break;
+case MEMTX_DECODE_ERROR:
+vfio_user_send_error(proxy, >hdr, ENODEV);
+break;
+case MEMTX_ACCESS_ERROR:
+vfio_user_send_error(proxy, >hdr, EPERM);
+break;
+default:
+error_printf("vfio_user_dma_write unknown error %d\n", r);
+vfio_user_send_error(vdev->vbasedev.proxy, >hdr, EINVAL);
+}
+}
+
 /*
  * Incoming request message callback.
  *
@@ -3557,9 +3646,30 @@ static void vfio_user_msix_teardown(VFIOPCIDevice *vdev)
  */
 static void vfio_user_pci_process_req(void *opaque, VFIOUserMsg *msg)
 {
+VFIOPCIDevice *vdev = opaque;
+VFIOUserHdr *hdr = msg->hdr;
+
+/* no incoming PCI requests pass FDs */
+if (msg->fds != NULL) {
+vfio_user_send_error(vdev->vbasedev.proxy, hdr, EINVAL);
+vfio_user_putfds(msg);
+return;
+}
 
+switch (hdr->command) {
+case VFIO_USER_DMA_READ:
+vfio_user_dma_read(vdev, (VFIOUserDMARW *)hdr);
+break;
+case VFIO_USER_DMA_WRITE:
+vfio_user_dma_write(vdev, (VFIOUserDMARW *)hdr);
+break;
+default:
+error_printf("vfio_user_process_req unknown cmd %d\n", hdr->command);
+vfio_user_send_error(vdev->vbasedev.proxy, hdr, ENOSYS);
+}
 }
 
+
 /*
  * Emulated devices don't use host hot reset
  */
diff --git a/hw/vfio/user-protocol.h b/hw/vfio/user-protocol.h
index e9fcf64..6afd090 100644
--- a/hw/vfio/user-protocol.h
+++ b/hw/vfio/user-protocol.h
@@ -202,6 +202,17 @@ typedef struct {
 char data[];
 } VFIOUserRegionRW;
 
+/*
+ * VFIO_USER_DMA_READ
+ * VFIO_USER_DMA_WRITE
+ */
+typedef struct {
+VFIOUserHdr hdr;
+uint64_t offset;
+uint32_t count;
+char data[];
+} VFIOUserDMARW;
+
 /*imported from struct vfio_bitmap */
 typedef struct {
 uint64_t pgsize;
diff --git a/hw/vfio/user.c b/hw/vfio/user.c
index 0c5493e..56b3616 100644
--- a/hw/vfio/user.c
+++ b/hw/vfio/user.c
@@ -379,6 +379,10 @@ static int vfio_user_recv_one(VFIOProxy *proxy)
 *msg->hdr = hdr;
 data = (char *)msg->hdr + sizeof(hdr);
 } else {
+if (hdr.size > proxy->max_xfer_size +