aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libvfio-user.h2
-rw-r--r--lib/common.h1
-rw-r--r--lib/dma.c11
-rw-r--r--lib/dma.h3
-rw-r--r--lib/libvfio-user.c7
-rw-r--r--lib/private.h10
-rw-r--r--test/py/libvfio_user.py4
-rw-r--r--test/py/test_dma_map.py101
8 files changed, 129 insertions, 10 deletions
diff --git a/include/libvfio-user.h b/include/libvfio-user.h
index 009ba83..c9e8ca2 100644
--- a/include/libvfio-user.h
+++ b/include/libvfio-user.h
@@ -58,8 +58,6 @@ extern "C" {
#define LIB_VFIO_USER_MAJOR 0
#define LIB_VFIO_USER_MINOR 1
-#define VFU_DMA_REGIONS 0x10
-
/* DMA addresses cannot be directly de-referenced. */
typedef void *vfu_dma_addr_t;
diff --git a/lib/common.h b/lib/common.h
index a56a0f0..599759f 100644
--- a/lib/common.h
+++ b/lib/common.h
@@ -43,6 +43,7 @@
#define UNUSED __attribute__((unused))
#define EXPORT __attribute__((visibility("default")))
+#define ONE_TB (1024UL * 1024 * 1024 * 1024)
#define PAGE_SIZE (size_t)sysconf(_SC_PAGE_SIZE)
#define PAGE_ALIGNED(x) (((x) & ((typeof(x))(PAGE_SIZE) - 1)) == 0)
diff --git a/lib/dma.c b/lib/dma.c
index 32014f0..b76c18c 100644
--- a/lib/dma.c
+++ b/lib/dma.c
@@ -71,7 +71,7 @@ fds_are_same_file(int fd1, int fd2)
}
dma_controller_t *
-dma_controller_create(vfu_ctx_t *vfu_ctx, int max_regions)
+dma_controller_create(vfu_ctx_t *vfu_ctx, size_t max_regions, size_t max_size)
{
dma_controller_t *dma;
@@ -83,7 +83,8 @@ dma_controller_create(vfu_ctx_t *vfu_ctx, int max_regions)
}
dma->vfu_ctx = vfu_ctx;
- dma->max_regions = max_regions;
+ dma->max_regions = (int)max_regions;
+ dma->max_size = max_size;
dma->nregions = 0;
memset(dma->regions, 0, max_regions * sizeof(dma->regions[0]));
dma->dirty_pgsize = 0;
@@ -298,6 +299,12 @@ MOCK_DEFINE(dma_controller_add_region)(dma_controller_t *dma,
snprintf(rstr, sizeof(rstr), "[%p, %p) fd=%d offset=%#lx prot=%#x",
dma_addr, (char *)dma_addr + size, fd, offset, prot);
+ if (size > dma->max_size) {
+ vfu_log(dma->vfu_ctx, LOG_ERR, "DMA region size %zu > max %zu",
+ size, dma->max_size);
+ return ERROR_INT(ENOSPC);
+ }
+
for (idx = 0; idx < dma->nregions; idx++) {
region = &dma->regions[idx];
diff --git a/lib/dma.h b/lib/dma.h
index 9798de9..082ca46 100644
--- a/lib/dma.h
+++ b/lib/dma.h
@@ -92,6 +92,7 @@ typedef struct {
typedef struct dma_controller {
int max_regions;
+ size_t max_size;
int nregions;
struct vfu_ctx *vfu_ctx;
size_t dirty_pgsize; // Dirty page granularity
@@ -99,7 +100,7 @@ typedef struct dma_controller {
} dma_controller_t;
dma_controller_t *
-dma_controller_create(vfu_ctx_t *vfu_ctx, int max_regions);
+dma_controller_create(vfu_ctx_t *vfu_ctx, size_t max_regions, size_t max_size);
void
dma_controller_remove_all_regions(dma_controller_t *dma,
diff --git a/lib/libvfio-user.c b/lib/libvfio-user.c
index c569714..452b15f 100644
--- a/lib/libvfio-user.c
+++ b/lib/libvfio-user.c
@@ -671,9 +671,7 @@ handle_dirty_pages_get(vfu_ctx_t *vfu_ctx, vfu_msg_t *msg)
return -1;
}
- /*
- * FIXME: this is unbounded until we can limit the maximum DMA region size.
- */
+ /* NB: this is bound by MAX_DMA_SIZE. */
argsz = sizeof(*dirty_pages_out) + sizeof(*range_out) +
range_in->bitmap.size;
@@ -1506,7 +1504,8 @@ vfu_setup_device_dma(vfu_ctx_t *vfu_ctx, vfu_dma_register_cb_t *dma_register,
assert(vfu_ctx != NULL);
// Create the internal DMA controller.
- vfu_ctx->dma = dma_controller_create(vfu_ctx, VFU_DMA_REGIONS);
+ vfu_ctx->dma = dma_controller_create(vfu_ctx, MAX_DMA_REGIONS,
+ MAX_DMA_SIZE);
if (vfu_ctx->dma == NULL) {
return ERROR_INT(errno);
}
diff --git a/lib/private.h b/lib/private.h
index c9a8af7..c7c0627 100644
--- a/lib/private.h
+++ b/lib/private.h
@@ -35,8 +35,16 @@
#include <errno.h>
-#include "pci_caps.h"
#include "common.h"
+#include "pci_caps.h"
+
+/*
+ * The main reason we limit the size of an individual DMA region from the client
+ * is to limit the size of the dirty bitmaps: this corresponds to 256MB at a 4K
+ * page size.
+ */
+#define MAX_DMA_SIZE (8 * ONE_TB)
+#define MAX_DMA_REGIONS 16
#define SERVER_MAX_MSG_SIZE 65536
diff --git a/test/py/libvfio_user.py b/test/py/libvfio_user.py
index a664da0..b1ebd4b 100644
--- a/test/py/libvfio_user.py
+++ b/test/py/libvfio_user.py
@@ -108,6 +108,10 @@ SERVER_MAX_FDS = 8
SERVER_MAX_MSG_SIZE = 65536
+MAX_DMA_REGIONS = 16
+ONE_TB = (1024 * 1024 * 1024 * 1024)
+MAX_DMA_SIZE = (8 * ONE_TB)
+
# enum vfio_user_command
VFIO_USER_VERSION = 1
VFIO_USER_DMA_MAP = 2
diff --git a/test/py/test_dma_map.py b/test/py/test_dma_map.py
new file mode 100644
index 0000000..3358abc
--- /dev/null
+++ b/test/py/test_dma_map.py
@@ -0,0 +1,101 @@
+#
+# Copyright (c) 2021 Nutanix Inc. All rights reserved.
+#
+# Authors: John Levon <john.levon@nutanix.com>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+# * Neither the name of Nutanix nor the names of its contributors may be
+# used to endorse or promote products derived from this software without
+# specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICESLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+# DAMAGE.
+#
+
+from libvfio_user import *
+import errno
+
+#
+# NB: this is currently very incomplete
+#
+
+ctx = None
+
+@vfu_dma_register_cb_t
+def dma_register(ctx, info):
+ pass
+
+@vfu_dma_unregister_cb_t
+def dma_unregister(ctx, info):
+ pass
+ return 0
+
+def test_dma_map_setup():
+ global ctx
+
+ ctx = vfu_create_ctx(flags=LIBVFIO_USER_FLAG_ATTACH_NB)
+ assert ctx != None
+
+ ret = vfu_pci_init(ctx)
+ assert ret == 0
+
+ ret = vfu_setup_device_dma(ctx, dma_register, dma_unregister)
+ assert ret == 0
+
+ ret = vfu_realize_ctx(ctx)
+ assert ret == 0
+
+def test_dma_region_too_big():
+ sock = connect_client(ctx)
+
+ payload = vfio_user_dma_map(argsz=len(vfio_user_dma_map()),
+ flags=(VFIO_USER_F_DMA_REGION_READ |
+ VFIO_USER_F_DMA_REGION_WRITE),
+ offset=0, addr=0x10000, size=MAX_DMA_SIZE + 4096)
+
+ hdr = vfio_user_header(VFIO_USER_DMA_MAP, size=len(payload))
+
+ sock.send(hdr + payload)
+ vfu_run_ctx(ctx)
+ get_reply(sock, expect=errno.ENOSPC)
+
+ disconnect_client(ctx, sock)
+
+def test_dma_region_too_many():
+ sock = connect_client(ctx)
+
+ for i in range(1, MAX_DMA_REGIONS + 2):
+ payload = vfio_user_dma_map(argsz=len(vfio_user_dma_map()),
+ flags=(VFIO_USER_F_DMA_REGION_READ |
+ VFIO_USER_F_DMA_REGION_WRITE),
+ offset=0, addr=0x1000 * i, size=4096)
+
+ hdr = vfio_user_header(VFIO_USER_DMA_MAP, size=len(payload))
+
+ sock.send(hdr + payload)
+ vfu_run_ctx(ctx)
+
+ if i == MAX_DMA_REGIONS + 1:
+ get_reply(sock, expect=errno.EINVAL)
+ else:
+ get_reply(sock)
+
+ disconnect_client(ctx, sock)
+
+def test_dirty_pages_cleanup():
+ vfu_destroy_ctx(ctx)