diff options
author | John Levon <john.levon@nutanix.com> | 2021-06-10 11:35:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-10 11:35:08 +0100 |
commit | c34d6ec02bae297b9256ac45ff71da0e2d4b4bc1 (patch) | |
tree | 1ca0d1f797bb38ffa06a2f132d613fa0e3785fa3 /test/py | |
parent | b665c3982c1efe2bb896fc8b556d24bcf996435a (diff) | |
download | libvfio-user-c34d6ec02bae297b9256ac45ff71da0e2d4b4bc1.zip libvfio-user-c34d6ec02bae297b9256ac45ff71da0e2d4b4bc1.tar.gz libvfio-user-c34d6ec02bae297b9256ac45ff71da0e2d4b4bc1.tar.bz2 |
python tests: add msg() utility function (#562)
Most tests need to send a request, process it, then retrieve the reply.
Add a utility function to avoid lots of tedious boilerplate.
Signed-off-by: John Levon <john.levon@nutanix.com>
Reviewed-by: Thanos Makatos <thanos.makatos@nutanix.com>
Diffstat (limited to 'test/py')
-rw-r--r-- | test/py/libvfio_user.py | 13 | ||||
-rw-r--r-- | test/py/test_device_get_info.py | 15 | ||||
-rw-r--r-- | test/py/test_device_get_irq_info.py | 39 | ||||
-rw-r--r-- | test/py/test_device_get_region_info.py | 38 | ||||
-rw-r--r-- | test/py/test_device_set_irqs.py | 135 | ||||
-rw-r--r-- | test/py/test_dirty_pages.py | 105 | ||||
-rw-r--r-- | test/py/test_dma_map.py | 17 | ||||
-rw-r--r-- | test/py/test_irq_trigger.py | 6 | ||||
-rw-r--r-- | test/py/test_negotiate.py | 1 |
9 files changed, 98 insertions, 271 deletions
diff --git a/test/py/libvfio_user.py b/test/py/libvfio_user.py index 1105fb5..f0fc568 100644 --- a/test/py/libvfio_user.py +++ b/test/py/libvfio_user.py @@ -486,6 +486,19 @@ def get_reply(sock, expect=0): assert errno == expect return buf[16:] +def msg(ctx, sock, cmd, payload, expect=0, fds=None): + """Round trip a request and reply to the server.""" + hdr = vfio_user_header(cmd, size=len(payload)) + + if fds: + sock.sendmsg([hdr + payload], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, + struct.pack("I" * len(fds), *fds))]) + else: + sock.send(hdr + payload) + + vfu_run_ctx(ctx) + return get_reply(sock, expect=expect) + def get_pci_header(ctx): ptr = lib.vfu_pci_get_config_space(ctx) return c.cast(ptr, c.POINTER(vfu_pci_hdr_t)).contents diff --git a/test/py/test_device_get_info.py b/test/py/test_device_get_info.py index a778604..ea9251b 100644 --- a/test/py/test_device_get_info.py +++ b/test/py/test_device_get_info.py @@ -52,28 +52,19 @@ def test_device_get_info(): payload = struct.pack("II", 0, 0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_INFO, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_GET_INFO, payload, expect=errno.EINVAL) # bad argsz payload = vfio_user_device_info(argsz=8, flags=0, num_regions=0, num_irqs=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_INFO, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_GET_INFO, payload, expect=errno.EINVAL) # valid with larger argsz payload = vfio_user_device_info(argsz=32, flags=0, num_regions=0, num_irqs=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_INFO, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - result = get_reply(sock) + result = msg(ctx, sock, VFIO_USER_DEVICE_GET_INFO, payload) (argsz, flags, num_regions, num_irqs) = struct.unpack("IIII", result) diff --git a/test/py/test_device_get_irq_info.py b/test/py/test_device_get_irq_info.py index 8283083..4d6b3d7 100644 --- a/test/py/test_device_get_irq_info.py +++ b/test/py/test_device_get_irq_info.py @@ -59,28 +59,19 @@ def test_device_get_irq_info_setup(): def test_device_get_irq_info_bad_in(): payload = struct.pack("II", 0, 0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_IRQ_INFO, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload, expect=errno.EINVAL) # bad argsz payload = vfio_irq_info(argsz=8, flags=0, index=VFU_DEV_REQ_IRQ, count=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_IRQ_INFO, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload, expect=errno.EINVAL) # bad index payload = vfio_irq_info(argsz=argsz, flags=0, index=VFU_DEV_NUM_IRQS, count=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_IRQ_INFO, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload, expect=errno.EINVAL) def test_device_get_irq_info(): @@ -89,20 +80,12 @@ def test_device_get_irq_info(): payload = vfio_irq_info(argsz=argsz + 16, flags=0, index=VFU_DEV_REQ_IRQ, count=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_IRQ_INFO, size=len(payload)) - - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock) + msg(ctx, sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload) payload = vfio_irq_info(argsz=argsz, flags=0, index=VFU_DEV_REQ_IRQ, count=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_IRQ_INFO, size=len(payload)) - - sock.send(hdr + payload) - vfu_run_ctx(ctx) - result = get_reply(sock) + result = msg(ctx, sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload) info, _ = vfio_irq_info.pop_from_buffer(result) @@ -114,11 +97,7 @@ def test_device_get_irq_info(): payload = vfio_irq_info(argsz=argsz, flags=0, index=VFU_DEV_ERR_IRQ, count=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_IRQ_INFO, size=len(payload)) - - sock.send(hdr + payload) - vfu_run_ctx(ctx) - result = get_reply(sock) + result = msg(ctx, sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload) info, _ = vfio_irq_info.pop_from_buffer(result) @@ -130,11 +109,7 @@ def test_device_get_irq_info(): payload = vfio_irq_info(argsz=argsz, flags=0, index=VFU_DEV_MSIX_IRQ, count=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_IRQ_INFO, size=len(payload)) - - sock.send(hdr + payload) - vfu_run_ctx(ctx) - result = get_reply(sock) + result = msg(ctx, sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload) info, _ = vfio_irq_info.pop_from_buffer(result) diff --git a/test/py/test_device_get_region_info.py b/test/py/test_device_get_region_info.py index 4209cfb..710efd8 100644 --- a/test/py/test_device_get_region_info.py +++ b/test/py/test_device_get_region_info.py @@ -76,10 +76,8 @@ def test_device_get_region_info_short_write(): payload = struct.pack("II", 0, 0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_REGION_INFO, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, + expect=errno.EINVAL) def test_device_get_region_info_bad_argsz(): @@ -87,10 +85,8 @@ def test_device_get_region_info_bad_argsz(): index=VFU_PCI_DEV_BAR1_REGION_IDX, cap_offset=0, size=0, offset=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_REGION_INFO, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, + expect=errno.EINVAL) def test_device_get_region_info_bad_index(): @@ -98,10 +94,8 @@ def test_device_get_region_info_bad_index(): index=VFU_PCI_DEV_NUM_REGIONS, cap_offset=0, size=0, offset=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_REGION_INFO, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, + expect=errno.EINVAL) def test_device_get_region_info_larger_argsz(): @@ -109,10 +103,7 @@ def test_device_get_region_info_larger_argsz(): index=VFU_PCI_DEV_BAR1_REGION_IDX, cap_offset=0, size=0, offset=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_REGION_INFO, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - result = get_reply(sock) + result = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) assert(len(result) == argsz) @@ -133,10 +124,7 @@ def test_device_get_region_info_small_argsz_caps(): index=VFU_PCI_DEV_BAR2_REGION_IDX, cap_offset=0, size=0, offset=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_REGION_INFO, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - result = get_reply(sock) + result = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) info, _ = vfio_region_info.pop_from_buffer(result) @@ -163,10 +151,7 @@ def test_device_get_region_info_caps(): size=0, offset=0) payload = bytes(payload) + b'\0' * (80 - 32) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_REGION_INFO, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - result = get_reply(sock) + result = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) info, result = vfio_region_info.pop_from_buffer(result) cap, result = vfio_region_info_cap_sparse_mmap.pop_from_buffer(result) @@ -201,10 +186,7 @@ def test_device_get_region_info_migr(): size=0, offset=0) payload = bytes(payload) + b'\0' * (80 - 32) - hdr = vfio_user_header(VFIO_USER_DEVICE_GET_REGION_INFO, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - result = get_reply(sock) + result = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) info, result = vfio_region_info.pop_from_buffer(result) mcap, result = vfio_region_info_cap_type.pop_from_buffer(result) diff --git a/test/py/test_device_set_irqs.py b/test/py/test_device_set_irqs.py index ebc3768..a958db3 100644 --- a/test/py/test_device_set_irqs.py +++ b/test/py/test_device_set_irqs.py @@ -68,120 +68,84 @@ def test_device_set_irqs_no_irq_set(): def test_device_set_irqs_short_write(): payload = struct.pack("II", 0, 0) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) def test_device_set_irqs_bad_argsz(): payload = vfio_irq_set(argsz=3, flags=VFIO_IRQ_SET_ACTION_TRIGGER | VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_REQ_IRQ, start=0, count=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) def test_device_set_irqs_bad_index(): payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_TRIGGER | VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_NUM_IRQS, start=0, count=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) def test_device_set_irqs_bad_flags_MASK_and_UNMASK(): payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_MASK | VFIO_IRQ_SET_ACTION_UNMASK, index=VFU_DEV_MSIX_IRQ, start=0, count=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) def test_device_set_irqs_bad_flags_DATA_NONE_and_DATA_BOOL(): payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_MASK | VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_BOOL, index=VFU_DEV_MSIX_IRQ, start=0, count=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) def test_device_set_irqs_bad_start_count_range(): payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_MASK | VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_MSIX_IRQ, start=2047, count=2) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) def test_device_set_irqs_bad_start_count_range(): payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_MASK | VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_MSIX_IRQ, start=2049, count=1) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) def test_device_set_irqs_bad_action_for_err_irq(): payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_MASK | VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_ERR_IRQ, start=0, count=1) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) def test_device_set_irqs_bad_action_for_req_irq(): payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_MASK | VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_REQ_IRQ, start=0, count=1) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) def test_device_set_irqs_bad_start_for_count_0(): payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_MASK | VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_MSIX_IRQ, start=1, count=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) def test_device_set_irqs_bad_action_for_count_0(): payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_MASK | VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_MSIX_IRQ, start=0, count=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) def test_device_set_irqs_bad_action_and_data_type_for_count_0(): payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_TRIGGER | VFIO_IRQ_SET_DATA_BOOL, index=VFU_DEV_MSIX_IRQ, start=0, count=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) def test_device_set_irqs_bad_fds_for_DATA_BOOL(): payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_TRIGGER | @@ -192,11 +156,8 @@ def test_device_set_irqs_bad_fds_for_DATA_BOOL(): fd = eventfd() - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.sendmsg([hdr + payload], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, - struct.pack("I", fd))]) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL, + fds=[fd]) os.close(fd) @@ -207,11 +168,8 @@ def test_device_set_irqs_bad_fds_for_DATA_NONE(): fd = eventfd() - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.sendmsg([hdr + payload], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, - struct.pack("I", fd))]) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL, + fds=[fd]) os.close(fd) @@ -222,11 +180,8 @@ def test_device_set_irqs_bad_fds_for_count_2(): fd = eventfd() - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.sendmsg([hdr + payload], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, - struct.pack("I", fd))]) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL, + fds=[fd]) os.close(fd) @@ -235,19 +190,13 @@ def test_device_set_irqs_disable(): VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_REQ_IRQ, start=0, count=0) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload) payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_TRIGGER | VFIO_IRQ_SET_DATA_EVENTFD, index=VFU_DEV_REQ_IRQ, start=0, count=1) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload) def test_device_set_irqs_enable(): payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_TRIGGER | @@ -256,11 +205,7 @@ def test_device_set_irqs_enable(): fd = eventfd() - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.sendmsg([hdr + payload], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, - struct.pack("I", fd))]) - vfu_run_ctx(ctx) - get_reply(sock) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, fds=[fd]) def test_device_set_irqs_trigger_bool_too_small(): payload = vfio_irq_set(argsz=argsz + 1, flags=VFIO_IRQ_SET_ACTION_TRIGGER | @@ -268,10 +213,7 @@ def test_device_set_irqs_trigger_bool_too_small(): start=0, count=2) payload = bytes(payload) + struct.pack("?", False) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) def test_device_set_irqs_trigger_bool_too_large(): payload = vfio_irq_set(argsz=argsz + 3, flags=VFIO_IRQ_SET_ACTION_TRIGGER | @@ -279,10 +221,7 @@ def test_device_set_irqs_trigger_bool_too_large(): start=0, count=2) payload = bytes(payload) + struct.pack("???", False, False, False) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) def test_device_set_irqs_enable_update(): payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_TRIGGER | @@ -291,11 +230,7 @@ def test_device_set_irqs_enable_update(): fd = eventfd() - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.sendmsg([hdr + payload], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, - struct.pack("I", fd))]) - vfu_run_ctx(ctx) - get_reply(sock) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, fds=[fd]) def test_device_set_irqs_enable_trigger_none(): payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_TRIGGER | @@ -305,20 +240,13 @@ def test_device_set_irqs_enable_trigger_none(): fd1 = eventfd(initval=4) fd2 = eventfd(initval=8) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.sendmsg([hdr + payload], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, - struct.pack("II", fd1, fd2))]) - vfu_run_ctx(ctx) - get_reply(sock) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, fds=[fd1, fd2]) payload = vfio_irq_set(argsz=argsz, flags=VFIO_IRQ_SET_ACTION_TRIGGER | VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_MSIX_IRQ, start=1, count=1) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload) assert struct.unpack("Q", os.read(fd1, 8))[0] == 4 assert struct.unpack("Q", os.read(fd2, 8))[0] == 9 @@ -331,21 +259,14 @@ def test_device_set_irqs_enable_trigger_bool(): fd1 = eventfd(initval=4) fd2 = eventfd(initval=8) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.sendmsg([hdr + payload], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, - struct.pack("II", fd1, fd2))]) - vfu_run_ctx(ctx) - get_reply(sock) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, fds=[fd1, fd2]) payload = vfio_irq_set(argsz=argsz + 2, flags=VFIO_IRQ_SET_ACTION_TRIGGER | VFIO_IRQ_SET_DATA_BOOL, index=VFU_DEV_MSIX_IRQ, start=0, count=2) payload = bytes(payload) + struct.pack("??", False, True) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload) assert struct.unpack("Q", os.read(fd1, 8))[0] == 4 assert struct.unpack("Q", os.read(fd2, 8))[0] == 9 diff --git a/test/py/test_dirty_pages.py b/test/py/test_dirty_pages.py index 25a73da..193e694 100644 --- a/test/py/test_dirty_pages.py +++ b/test/py/test_dirty_pages.py @@ -77,38 +77,24 @@ def test_dirty_pages_setup(): flags=(VFIO_USER_F_DMA_REGION_READ | VFIO_USER_F_DMA_REGION_WRITE), offset=0, addr=0x10000, size=0x10000) - hdr = vfio_user_header(VFIO_USER_DMA_MAP, size=len(payload)) - - sock.sendmsg([hdr + payload], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, - struct.pack("I", f.fileno()))]) - vfu_run_ctx(ctx) - get_reply(sock) + msg(ctx, sock, VFIO_USER_DMA_MAP, payload, fds=[f.fileno()]) def test_dirty_pages_short_write(): payload = struct.pack("I", 8) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) def test_dirty_pages_bad_argsz(): payload = vfio_user_dirty_pages(argsz=4, flags=VFIO_IOMMU_DIRTY_PAGES_FLAG_START) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) def test_dirty_pages_start_no_migration(): payload = vfio_user_dirty_pages(argsz=len(vfio_user_dirty_pages()), flags=VFIO_IOMMU_DIRTY_PAGES_FLAG_START) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.ENOTSUP) + msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.ENOTSUP) def test_dirty_pages_start_bad_flags(): # @@ -121,44 +107,31 @@ def test_dirty_pages_start_bad_flags(): flags=(VFIO_IOMMU_DIRTY_PAGES_FLAG_START | VFIO_IOMMU_DIRTY_PAGES_FLAG_STOP)) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) payload = vfio_user_dirty_pages(argsz=len(vfio_user_dirty_pages()), flags=(VFIO_IOMMU_DIRTY_PAGES_FLAG_START | VFIO_IOMMU_DIRTY_PAGES_FLAG_GET_BITMAP)) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) def start_logging(): payload = vfio_user_dirty_pages(argsz=len(vfio_user_dirty_pages()), flags=VFIO_IOMMU_DIRTY_PAGES_FLAG_START) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock) - + msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload) def test_dirty_pages_start(): start_logging() - start_logging() # should be idempotent - + # should be idempotent + start_logging() def test_dirty_pages_get_short_read(): payload = vfio_user_dirty_pages(argsz=len(vfio_user_dirty_pages()), flags=VFIO_IOMMU_DIRTY_PAGES_FLAG_GET_BITMAP) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) # # This should in fact work; update when it does. @@ -170,11 +143,9 @@ def test_dirty_pages_get_sub_range(): bitmap = vfio_user_bitmap(pgsize=0x1000, size=8) br = vfio_user_bitmap_range(iova=0x11000, size=0x1000, bitmap=bitmap) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, - size=len(dirty_pages) + len(br)) - sock.send(hdr + dirty_pages + br) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.ENOTSUP) + payload = bytes(dirty_pages) + bytes(br) + + msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.ENOTSUP) def test_dirty_pages_get_bad_page_size(): argsz = len(vfio_user_dirty_pages()) + len(vfio_user_bitmap_range()) + 8 @@ -183,11 +154,9 @@ def test_dirty_pages_get_bad_page_size(): bitmap = vfio_user_bitmap(pgsize=0x2000, size=8) br = vfio_user_bitmap_range(iova=0x10000, size=0x10000, bitmap=bitmap) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, - size=len(dirty_pages) + len(br)) - sock.send(hdr + dirty_pages + br) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + payload = bytes(dirty_pages) + bytes(br) + + msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) def test_dirty_pages_get_bad_bitmap_size(): argsz = len(vfio_user_dirty_pages()) + len(vfio_user_bitmap_range()) + 8 @@ -196,11 +165,9 @@ def test_dirty_pages_get_bad_bitmap_size(): bitmap = vfio_user_bitmap(pgsize=0x1000, size=1) br = vfio_user_bitmap_range(iova=0x10000, size=0x10000, bitmap=bitmap) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, - size=len(dirty_pages) + len(br)) - sock.send(hdr + dirty_pages + br) - vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + payload = bytes(dirty_pages) + bytes(br) + + msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) def test_dirty_pages_get_short_reply(): dirty_pages = vfio_user_dirty_pages(argsz=len(vfio_user_dirty_pages()), @@ -208,11 +175,9 @@ def test_dirty_pages_get_short_reply(): bitmap = vfio_user_bitmap(pgsize=0x1000, size=8) br = vfio_user_bitmap_range(iova=0x10000, size=0x10000, bitmap=bitmap) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, - size=len(dirty_pages) + len(br)) - sock.send(hdr + dirty_pages + br) - vfu_run_ctx(ctx) - result = get_reply(sock) + payload = bytes(dirty_pages) + bytes(br) + + result = msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload) assert len(result) == len(vfio_user_dirty_pages()) @@ -231,11 +196,9 @@ def test_dirty_pages_get_unmodified(): bitmap = vfio_user_bitmap(pgsize=0x1000, size=8) br = vfio_user_bitmap_range(iova=0x10000, size=0x10000, bitmap=bitmap) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, - size=len(dirty_pages) + len(br)) - sock.send(hdr + dirty_pages + br) - vfu_run_ctx(ctx) - result = get_reply(sock) + payload = bytes(dirty_pages) + bytes(br) + + result = msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload) assert len(result) == argsz @@ -261,11 +224,9 @@ def get_dirty_page_bitmap(): bitmap = vfio_user_bitmap(pgsize=0x1000, size=8) br = vfio_user_bitmap_range(iova=0x10000, size=0x10000, bitmap=bitmap) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, - size=len(dirty_pages) + len(br)) - sock.send(hdr + dirty_pages + br) - vfu_run_ctx(ctx) - result = get_reply(sock) + payload = bytes(dirty_pages) + bytes(br) + + result = msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload) dirty_pages, result = vfio_user_dirty_pages.pop_from_buffer(result) br, result = vfio_user_bitmap_range.pop_from_buffer(result) @@ -312,18 +273,12 @@ def stop_logging(): payload = vfio_user_dirty_pages(argsz=len(vfio_user_dirty_pages()), flags=VFIO_IOMMU_DIRTY_PAGES_FLAG_STOP) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock) + msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload) payload = vfio_user_dirty_pages(argsz=len(vfio_user_dirty_pages()), flags=VFIO_IOMMU_DIRTY_PAGES_FLAG_STOP) - hdr = vfio_user_header(VFIO_USER_DIRTY_PAGES, size=len(payload)) - sock.send(hdr + payload) - vfu_run_ctx(ctx) - get_reply(sock) + msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload) def test_dirty_pages_stop(): diff --git a/test/py/test_dma_map.py b/test/py/test_dma_map.py index 3358abc..fe48c45 100644 --- a/test/py/test_dma_map.py +++ b/test/py/test_dma_map.py @@ -68,11 +68,7 @@ def test_dma_region_too_big(): 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) + msg(ctx, sock, VFIO_USER_DMA_MAP, payload, expect=errno.ENOSPC) disconnect_client(ctx, sock) @@ -85,15 +81,12 @@ def test_dma_region_too_many(): 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) + expect=errno.EINVAL else: - get_reply(sock) + expect=0 + + msg(ctx, sock, VFIO_USER_DMA_MAP, payload, expect=expect) disconnect_client(ctx, sock) diff --git a/test/py/test_irq_trigger.py b/test/py/test_irq_trigger.py index 422eb3e..02bc90a 100644 --- a/test/py/test_irq_trigger.py +++ b/test/py/test_irq_trigger.py @@ -72,11 +72,7 @@ def test_irq_trigger(): fd = eventfd(initval=4) - hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=len(payload)) - sock.sendmsg([hdr + payload], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, - struct.pack("I", fd))]) - vfu_run_ctx(ctx) - get_reply(sock) + msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, fds=[fd]) ret = vfu_irq_trigger(ctx, 8) diff --git a/test/py/test_negotiate.py b/test/py/test_negotiate.py index a3f2a8a..524f59f 100644 --- a/test/py/test_negotiate.py +++ b/test/py/test_negotiate.py @@ -41,6 +41,7 @@ def client_version_json(expect=0, json=''): payload = struct.pack("HH%dsc" % len(json), LIBVFIO_USER_MAJOR, LIBVFIO_USER_MINOR, json, b'\0') + hdr = vfio_user_header(VFIO_USER_VERSION, size=len(payload)) sock.send(hdr + payload) vfu_attach_ctx(ctx, expect=expect) |