From e7f1ad0efff1360cf130e1ed272b5f5633691e82 Mon Sep 17 00:00:00 2001 From: Mattias Nissler Date: Tue, 29 Aug 2023 05:13:51 -0700 Subject: [PATCH 1/3] Introduce client object in python tests Thus far, the client end of the socket is the only piece of client state tracked in tests, for which a global `socket` variable has been used. In preparation to add more state, replace the `socket` global with a `client` global object that groups all client state. Signed-off-by: Mattias Nissler --- test/py/libvfio_user.py | 74 ++++++++++++++----- test/py/test_destroy.py | 4 +- test/py/test_device_get_info.py | 10 +-- test/py/test_device_get_irq_info.py | 22 +++--- test/py/test_device_get_region_info.py | 49 ++++++------ .../test_device_get_region_info_zero_size.py | 12 +-- test/py/test_device_get_region_io_fds.py | 34 ++++----- test/py/test_device_set_irqs.py | 66 ++++++++--------- test/py/test_dirty_pages.py | 50 ++++++------- test/py/test_dma_map.py | 36 ++++----- test/py/test_dma_unmap.py | 44 +++++------ test/py/test_irq_trigger.py | 8 +- test/py/test_migration.py | 30 ++++---- test/py/test_negotiate.py | 8 +- test/py/test_pci_caps.py | 72 +++++++++--------- test/py/test_pci_ext_caps.py | 52 ++++++------- test/py/test_quiesce.py | 62 ++++++++-------- test/py/test_request_errors.py | 48 ++++++------ test/py/test_setup_region.py | 36 ++++----- test/py/test_sgl_get_put.py | 18 ++--- test/py/test_shadow_ioeventfd.py | 4 +- 21 files changed, 388 insertions(+), 351 deletions(-) diff --git a/test/py/libvfio_user.py b/test/py/libvfio_user.py index 6d60798d..147aadf5 100644 --- a/test/py/libvfio_user.py +++ b/test/py/libvfio_user.py @@ -32,6 +32,7 @@ # from types import SimpleNamespace +import collections.abc import ctypes as c import array import errno @@ -685,25 +686,60 @@ def connect_sock(): return sock -def connect_client(ctx): - sock = connect_sock() - - json = b'{ "capabilities": { "max_msg_fds": 8 } }' - # struct vfio_user_version - 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=0) - payload = get_reply(sock, expect=0) - return sock - - -def disconnect_client(ctx, sock): - sock.close() - - # notice client closed connection - vfu_run_ctx(ctx, errno.ENOTCONN) +class Client: + """Models a VFIO-user client connected to the server under test.""" + + def __init__(self, sock=None): + self.sock = sock + self.client_cmd_socket = None + + def connect(self, ctx, capabilities={}): + self.sock = connect_sock() + + effective_caps = { + "capabilities": { + "max_data_xfer_size": VFIO_USER_DEFAULT_MAX_DATA_XFER_SIZE, + "max_msg_fds": 8, + "twin_socket": False, + }, + } + + def update(target, overrides): + for k, v in overrides.items(): + if isinstance(v, collections.abc.Mapping): + target[k] = target.get(k, {}) + update(target[k], v) + else: + target[k] = v + + update(effective_caps, capabilities) + caps_json = json.dumps(effective_caps) + + # struct vfio_user_version + payload = struct.pack("HH%dsc" % len(caps_json), LIBVFIO_USER_MAJOR, + LIBVFIO_USER_MINOR, caps_json.encode(), b'\0') + hdr = vfio_user_header(VFIO_USER_VERSION, size=len(payload)) + self.sock.send(hdr + payload) + vfu_attach_ctx(ctx, expect=0) + fds, payload = get_reply_fds(self.sock, expect=0) + self.client_cmd_socket = socket.socket(fileno=fds[0]) if fds else None + return self.sock + + def disconnect(self, ctx): + self.sock.close() + self.sock = None + if self.client_cmd_socket is not None: + self.client_cmd_socket.close() + self.client_cmd_socket = None + + # notice client closed connection + vfu_run_ctx(ctx, errno.ENOTCONN) + + +def connect_client(*args, **kwargs): + client = Client() + client.connect(*args, **kwargs) + return client def get_reply(sock, expect=0): diff --git a/test/py/test_destroy.py b/test/py/test_destroy.py index ffe45065..94968ab7 100644 --- a/test/py/test_destroy.py +++ b/test/py/test_destroy.py @@ -35,10 +35,10 @@ def setup_function(function): - global ctx, sock + global ctx, client ctx = prepare_ctx_for_dma() assert ctx is not None - sock = connect_client(ctx) + client = connect_client(ctx) def teardown_function(function): diff --git a/test/py/test_device_get_info.py b/test/py/test_device_get_info.py index 0a266148..73371996 100644 --- a/test/py/test_device_get_info.py +++ b/test/py/test_device_get_info.py @@ -49,11 +49,11 @@ def test_device_get_info(): # test short write - sock = connect_client(ctx) + client = connect_client(ctx) payload = struct.pack("II", 0, 0) - msg(ctx, sock, VFIO_USER_DEVICE_GET_INFO, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_INFO, payload, expect=errno.EINVAL) # bad argsz @@ -61,7 +61,7 @@ def test_device_get_info(): payload = vfio_user_device_info(argsz=8, flags=0, num_regions=0, num_irqs=0) - msg(ctx, sock, VFIO_USER_DEVICE_GET_INFO, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_INFO, payload, expect=errno.EINVAL) # valid with larger argsz @@ -69,7 +69,7 @@ def test_device_get_info(): payload = vfio_user_device_info(argsz=32, flags=0, num_regions=0, num_irqs=0) - result = msg(ctx, sock, VFIO_USER_DEVICE_GET_INFO, payload) + result = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_INFO, payload) (argsz, flags, num_regions, num_irqs) = struct.unpack("IIII", result) @@ -78,7 +78,7 @@ def test_device_get_info(): assert num_regions == VFU_PCI_DEV_NUM_REGIONS assert num_irqs == VFU_DEV_NUM_IRQS - disconnect_client(ctx, sock) + client.disconnect(ctx) vfu_destroy_ctx(ctx) diff --git a/test/py/test_device_get_irq_info.py b/test/py/test_device_get_irq_info.py index 0a9b089d..8ac67599 100644 --- a/test/py/test_device_get_irq_info.py +++ b/test/py/test_device_get_irq_info.py @@ -31,13 +31,13 @@ import errno ctx = None -sock = None +client = None argsz = len(vfio_irq_info()) def test_device_get_irq_info_setup(): - global ctx, sock + global ctx, client ctx = vfu_create_ctx(flags=LIBVFIO_USER_FLAG_ATTACH_NB) assert ctx is not None @@ -55,27 +55,27 @@ def test_device_get_irq_info_setup(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) def test_device_get_irq_info_bad_in(): payload = struct.pack("II", 0, 0) - msg(ctx, sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload, + msg(ctx, client.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) - msg(ctx, sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload, + msg(ctx, client.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) - msg(ctx, sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload, expect=errno.EINVAL) @@ -86,12 +86,12 @@ def test_device_get_irq_info(): payload = vfio_irq_info(argsz=argsz + 16, flags=0, index=VFU_DEV_REQ_IRQ, count=0) - msg(ctx, sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload) + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload) payload = vfio_irq_info(argsz=argsz, flags=0, index=VFU_DEV_REQ_IRQ, count=0) - result = msg(ctx, sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload) + result = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload) info, _ = vfio_irq_info.pop_from_buffer(result) @@ -103,7 +103,7 @@ def test_device_get_irq_info(): payload = vfio_irq_info(argsz=argsz, flags=0, index=VFU_DEV_ERR_IRQ, count=0) - result = msg(ctx, sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload) + result = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload) info, _ = vfio_irq_info.pop_from_buffer(result) @@ -115,7 +115,7 @@ def test_device_get_irq_info(): payload = vfio_irq_info(argsz=argsz, flags=0, index=VFU_DEV_MSIX_IRQ, count=0) - result = msg(ctx, sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload) + result = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_IRQ_INFO, payload) info, _ = vfio_irq_info.pop_from_buffer(result) @@ -126,7 +126,7 @@ def test_device_get_irq_info(): def test_device_get_irq_info_cleanup(): - disconnect_client(ctx, sock) + client.disconnect(ctx) vfu_destroy_ctx(ctx) diff --git a/test/py/test_device_get_region_info.py b/test/py/test_device_get_region_info.py index 62d67406..f847cb47 100644 --- a/test/py/test_device_get_region_info.py +++ b/test/py/test_device_get_region_info.py @@ -32,7 +32,7 @@ import tempfile ctx = None -sock = None +client = None argsz = len(vfio_region_info()) migr_region_size = 2 << PAGE_SHIFT @@ -40,7 +40,7 @@ def test_device_get_region_info_setup(): - global ctx, sock + global ctx, client ctx = vfu_create_ctx(flags=LIBVFIO_USER_FLAG_ATTACH_NB) assert ctx is not None @@ -89,14 +89,14 @@ def test_device_get_region_info_setup(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) def test_device_get_region_info_short_write(): payload = struct.pack("II", 0, 0) - msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, expect=errno.EINVAL) @@ -106,7 +106,7 @@ def test_device_get_region_info_bad_argsz(): index=VFU_PCI_DEV_BAR1_REGION_IDX, cap_offset=0, size=0, offset=0) - msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, expect=errno.EINVAL) @@ -116,7 +116,7 @@ def test_device_get_region_info_bad_index(): index=VFU_PCI_DEV_NUM_REGIONS, cap_offset=0, size=0, offset=0) - msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, expect=errno.EINVAL) @@ -126,7 +126,7 @@ def test_device_get_region_info_larger_argsz(): index=VFU_PCI_DEV_BAR1_REGION_IDX, cap_offset=0, size=0, offset=0) - result = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) + result = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) assert len(result) == argsz @@ -142,13 +142,13 @@ def test_device_get_region_info_larger_argsz(): def test_device_get_region_info_small_argsz_caps(): - global sock + global client payload = vfio_region_info(argsz=argsz, flags=0, index=VFU_PCI_DEV_BAR2_REGION_IDX, cap_offset=0, size=0, offset=0) - result = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) + result = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) info, _ = vfio_region_info.pop_from_buffer(result) @@ -167,20 +167,21 @@ def test_device_get_region_info_small_argsz_caps(): assert info.offset == 0x8000 # skip reading the SCM_RIGHTS - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_device_get_region_info_caps(): - global sock + global client - sock = connect_client(ctx) + client = connect_client(ctx) payload = vfio_region_info(argsz=80, flags=0, index=VFU_PCI_DEV_BAR2_REGION_IDX, cap_offset=0, size=0, offset=0) payload = bytes(payload) + b'\0' * (80 - 32) - fds, result = msg_fds(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) + fds, result = msg_fds(ctx, client.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) @@ -203,20 +204,20 @@ def test_device_get_region_info_caps(): assert area2.size == 0x2000 assert len(fds) == 1 - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_device_get_region_info_migr(): - global sock + global client - sock = connect_client(ctx) + client = connect_client(ctx) payload = vfio_region_info(argsz=80, flags=0, index=VFU_PCI_DEV_MIGR_REGION_IDX, cap_offset=0, size=0, offset=0) payload = bytes(payload) + b'\0' * (80 - 32) - result = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) + result = msg(ctx, client.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) @@ -241,7 +242,7 @@ def test_device_get_region_info_migr(): assert area.size == migr_mmap_areas[0][1] # skip reading the SCM_RIGHTS - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_device_get_region_info_cleanup(): @@ -260,13 +261,13 @@ def test_device_get_pci_config_space_info_implicit_pci_init(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) payload = vfio_region_info(argsz=argsz + 8, flags=0, index=VFU_PCI_DEV_CFG_REGION_IDX, cap_offset=0, size=0, offset=0) - result = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) + result = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) assert len(result) == argsz @@ -281,7 +282,7 @@ def test_device_get_pci_config_space_info_implicit_pci_init(): assert info.size == PCI_CFG_SPACE_EXP_SIZE assert info.offset == 0 - disconnect_client(ctx, sock) + client.disconnect(ctx) vfu_destroy_ctx(ctx) @@ -296,13 +297,13 @@ def test_device_get_pci_config_space_info_implicit_no_pci_init(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) payload = vfio_region_info(argsz=argsz + 8, flags=0, index=VFU_PCI_DEV_CFG_REGION_IDX, cap_offset=0, size=0, offset=0) - result = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) + result = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) assert len(result) == argsz @@ -315,7 +316,7 @@ def test_device_get_pci_config_space_info_implicit_no_pci_init(): assert info.size == PCI_CFG_SPACE_SIZE assert info.offset == 0 - disconnect_client(ctx, sock) + client.disconnect(ctx) vfu_destroy_ctx(ctx) diff --git a/test/py/test_device_get_region_info_zero_size.py b/test/py/test_device_get_region_info_zero_size.py index 42a6ae0b..146e812f 100644 --- a/test/py/test_device_get_region_info_zero_size.py +++ b/test/py/test_device_get_region_info_zero_size.py @@ -30,13 +30,13 @@ from libvfio_user import * ctx = None -sock = None +client = None argsz = len(vfio_region_info()) def test_device_get_region_info_setup(): - global ctx, sock + global ctx, client ctx = vfu_create_ctx(flags=LIBVFIO_USER_FLAG_ATTACH_NB) assert ctx is not None @@ -44,13 +44,13 @@ def test_device_get_region_info_setup(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) def test_device_get_region_info_zero_sized_region(): """Tests that a zero-sized region has no caps.""" - global sock + global client for index in [VFU_PCI_DEV_BAR1_REGION_IDX, VFU_PCI_DEV_MIGR_REGION_IDX]: payload = vfio_region_info(argsz=argsz, flags=0, @@ -59,9 +59,9 @@ def test_device_get_region_info_zero_sized_region(): hdr = vfio_user_header(VFIO_USER_DEVICE_GET_REGION_INFO, size=len(payload)) - sock.send(hdr + payload) + client.sock.send(hdr + payload) vfu_run_ctx(ctx) - result = get_reply(sock) + result = get_reply(client.sock) assert len(result) == argsz diff --git a/test/py/test_device_get_region_io_fds.py b/test/py/test_device_get_region_io_fds.py index 63b433f7..47ce3251 100644 --- a/test/py/test_device_get_region_io_fds.py +++ b/test/py/test_device_get_region_io_fds.py @@ -34,12 +34,12 @@ import struct ctx = None -sock = None +client = None fds = [] def test_device_get_region_io_fds_setup(): - global ctx, sock + global ctx, client ctx = vfu_create_ctx(flags=LIBVFIO_USER_FLAG_ATTACH_NB) assert ctx is not None @@ -71,7 +71,7 @@ def test_device_get_region_io_fds_setup(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) for i in range(0, 6): tmp = eventfd(0, 0) fds.append(tmp) @@ -86,7 +86,7 @@ def test_device_get_region_io_fds_bad_flags(): len(vfio_user_sub_region_ioeventfd()) * 5, flags=1, index=VFU_PCI_DEV_BAR2_REGION_IDX, count=0) - msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, expect=errno.EINVAL) @@ -97,7 +97,7 @@ def test_device_get_region_io_fds_bad_count(): len(vfio_user_sub_region_ioeventfd()) * 5, flags=0, index=VFU_PCI_DEV_BAR2_REGION_IDX, count=1) - msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, expect=errno.EINVAL) @@ -107,7 +107,7 @@ def test_device_get_region_io_fds_buffer_too_small(): argsz=len(vfio_user_region_io_fds_reply()) - 1, flags=0, index=VFU_PCI_DEV_BAR2_REGION_IDX, count=1) - msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, expect=errno.EINVAL) @@ -118,7 +118,7 @@ def test_device_get_region_io_fds_buffer_too_large(): index=VFU_PCI_DEV_BAR2_REGION_IDX, count=1) - msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, expect=errno.EINVAL) @@ -127,7 +127,7 @@ def test_device_get_region_io_fds_no_fds(): payload = vfio_user_region_io_fds_request(argsz=512, flags=0, index=VFU_PCI_DEV_BAR1_REGION_IDX, count=0) - ret = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, + ret = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, expect=0) reply, ret = vfio_user_region_io_fds_reply.pop_from_buffer(ret) @@ -143,7 +143,7 @@ def test_device_get_region_io_fds_no_regions_setup(): payload = vfio_user_region_io_fds_request(argsz=512, flags=0, index=VFU_PCI_DEV_BAR3_REGION_IDX, count=0) - msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, expect=errno.EINVAL) @@ -152,7 +152,7 @@ def test_device_get_region_io_fds_region_no_mmap(): payload = vfio_user_region_io_fds_request(argsz=512, flags=0, index=VFU_PCI_DEV_BAR5_REGION_IDX, count=0) - ret = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, + ret = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, expect=0) reply, ret = vfio_user_region_io_fds_reply.pop_from_buffer(ret) @@ -168,7 +168,7 @@ def test_device_get_region_io_fds_region_out_of_range(): payload = vfio_user_region_io_fds_request(argsz=512, flags=0, index=512, count=0) - msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, expect=errno.EINVAL) @@ -179,7 +179,7 @@ def test_device_get_region_io_fds_fds_read_write(): len(vfio_user_sub_region_ioeventfd()) * 10, flags=0, index=VFU_PCI_DEV_BAR2_REGION_IDX, count=0) - newfds, ret = msg_fds(ctx, sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, + newfds, ret = msg_fds(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, expect=0) assert len(newfds) == 6 @@ -209,7 +209,7 @@ def test_device_get_region_io_fds_full(): len(vfio_user_sub_region_ioeventfd()) * 6, flags=0, index=VFU_PCI_DEV_BAR2_REGION_IDX, count=0) - newfds, ret = msg_fds(ctx, sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, + newfds, ret = msg_fds(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, expect=0) reply, ret = vfio_user_region_io_fds_reply.pop_from_buffer(ret) @@ -238,7 +238,7 @@ def test_device_get_region_io_fds_fds_read_write_nothing(): argsz=len(vfio_user_region_io_fds_reply()), flags=0, index=VFU_PCI_DEV_BAR2_REGION_IDX, count=0) - newfds, ret = msg_fds(ctx, sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, + newfds, ret = msg_fds(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, expect=0) assert len(newfds) == 0 @@ -263,7 +263,7 @@ def test_device_get_region_io_fds_fds_read_write_dupe_fd(): len(vfio_user_sub_region_ioeventfd()) * 8, flags=0, index=VFU_PCI_DEV_BAR2_REGION_IDX, count=0) - newfds, ret = msg_fds(ctx, sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, + newfds, ret = msg_fds(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, expect=0) reply, ret = vfio_user_region_io_fds_reply.pop_from_buffer(ret) assert len(newfds) == 7 @@ -337,7 +337,7 @@ def test_device_get_region_io_fds_invalid_fd(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) fds = [] @@ -370,7 +370,7 @@ def test_device_get_region_io_fds_invalid_fd(): len(vfio_user_sub_region_ioeventfd()) * 5, flags=0, index=VFU_PCI_DEV_BAR0_REGION_IDX, count=0) - newfds, ret = msg_fds(ctx, sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, + newfds, ret = msg_fds(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, expect=0) # two unique fds diff --git a/test/py/test_device_set_irqs.py b/test/py/test_device_set_irqs.py index 1aead710..a2a27018 100644 --- a/test/py/test_device_set_irqs.py +++ b/test/py/test_device_set_irqs.py @@ -35,13 +35,13 @@ import os ctx = None -sock = None +client = None argsz = len(vfio_irq_set()) def test_device_set_irqs_setup(): - global ctx, sock + global ctx, client ctx = vfu_create_ctx(flags=LIBVFIO_USER_FLAG_ATTACH_NB) assert ctx is not None @@ -62,20 +62,20 @@ def test_device_set_irqs_setup(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) def test_device_set_irqs_no_irq_set(): hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=0) - sock.send(hdr) + client.sock.send(hdr) vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + get_reply(client.sock, expect=errno.EINVAL) def test_device_set_irqs_short_write(): payload = struct.pack("II", 0, 0) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -84,7 +84,7 @@ def test_device_set_irqs_bad_argsz(): VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_REQ_IRQ, start=0, count=0) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -93,7 +93,7 @@ def test_device_set_irqs_bad_index(): VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_NUM_IRQS, start=0, count=0) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -102,7 +102,7 @@ def test_device_set_irqs_bad_flags_MASK_and_UNMASK(): VFIO_IRQ_SET_ACTION_UNMASK, index=VFU_DEV_MSIX_IRQ, start=0, count=0) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -111,7 +111,7 @@ def test_device_set_irqs_bad_flags_DATA_NONE_and_DATA_BOOL(): VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_BOOL, index=VFU_DEV_MSIX_IRQ, start=0, count=0) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -120,7 +120,7 @@ def test_device_set_irqs_bad_start_count_range(): VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_MSIX_IRQ, start=2047, count=2) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -129,7 +129,7 @@ def test_device_set_irqs_bad_start_count_range2(): VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_MSIX_IRQ, start=2049, count=1) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -138,7 +138,7 @@ def test_device_set_irqs_bad_action_for_err_irq(): VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_ERR_IRQ, start=0, count=1) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -147,7 +147,7 @@ def test_device_set_irqs_bad_action_for_req_irq(): VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_REQ_IRQ, start=0, count=1) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -156,7 +156,7 @@ def test_device_set_irqs_bad_start_count_range_for_err_irq(): VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_ERR_IRQ, start=0, count=2) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -165,7 +165,7 @@ def test_device_set_irqs_bad_start_count_range_for_req_irq(): VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_REQ_IRQ, start=0, count=2) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -174,7 +174,7 @@ def test_device_set_irqs_bad_start_for_count_0(): VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_MSIX_IRQ, start=1, count=0) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -183,7 +183,7 @@ def test_device_set_irqs_bad_action_for_count_0(): VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_MSIX_IRQ, start=0, count=0) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -192,7 +192,7 @@ def test_device_set_irqs_bad_action_and_data_type_for_count_0(): VFIO_IRQ_SET_DATA_BOOL, index=VFU_DEV_MSIX_IRQ, start=0, count=0) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -205,7 +205,7 @@ def test_device_set_irqs_bad_fds_for_DATA_BOOL(): fd = eventfd() - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL, fds=[fd]) os.close(fd) @@ -218,7 +218,7 @@ def test_device_set_irqs_bad_fds_for_DATA_NONE(): fd = eventfd() - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL, fds=[fd]) os.close(fd) @@ -231,7 +231,7 @@ def test_device_set_irqs_bad_fds_for_count_2(): fd = eventfd() - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL, fds=[fd]) os.close(fd) @@ -242,13 +242,13 @@ def test_device_set_irqs_disable(): VFIO_IRQ_SET_DATA_NONE, index=VFU_DEV_REQ_IRQ, start=0, count=0) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload) + msg(ctx, client.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) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload) + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload) def test_device_set_irqs_enable(): @@ -258,7 +258,7 @@ def test_device_set_irqs_enable(): fd = eventfd() - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, fds=[fd]) + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, fds=[fd]) def test_device_set_irqs_trigger_bool_too_small(): @@ -267,7 +267,7 @@ def test_device_set_irqs_trigger_bool_too_small(): start=0, count=2) payload = bytes(payload) + struct.pack("?", False) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -277,7 +277,7 @@ def test_device_set_irqs_trigger_bool_too_large(): start=0, count=2) payload = bytes(payload) + struct.pack("???", False, False, False) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, expect=errno.EINVAL) @@ -288,7 +288,7 @@ def test_device_set_irqs_enable_update(): fd = eventfd() - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, fds=[fd]) + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, fds=[fd]) def test_device_set_irqs_enable_trigger_none(): @@ -299,13 +299,13 @@ def test_device_set_irqs_enable_trigger_none(): fd1 = eventfd(initval=4) fd2 = eventfd(initval=8) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, fds=[fd1, fd2]) + msg(ctx, client.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) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload) + msg(ctx, client.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 @@ -319,14 +319,14 @@ def test_device_set_irqs_enable_trigger_bool(): fd1 = eventfd(initval=4) fd2 = eventfd(initval=8) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, fds=[fd1, fd2]) + msg(ctx, client.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) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload) + msg(ctx, client.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 @@ -341,7 +341,7 @@ def test_irq_state(mock_irq_state): index=VFU_DEV_MSIX_IRQ, start=0, count=1) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload) + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload) mock_irq_state.assert_called_with(ANY, 0, 1, True) diff --git a/test/py/test_dirty_pages.py b/test/py/test_dirty_pages.py index b3d4e342..f3e42198 100644 --- a/test/py/test_dirty_pages.py +++ b/test/py/test_dirty_pages.py @@ -56,7 +56,7 @@ def quiesce_cb(ctx): def test_dirty_pages_setup(): - global ctx, sock + global ctx, client ctx = vfu_create_ctx(flags=LIBVFIO_USER_FLAG_ATTACH_NB) assert ctx is not None @@ -82,7 +82,7 @@ def test_dirty_pages_setup(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) f = tempfile.TemporaryFile() f.truncate(0x10 << PAGE_SHIFT) @@ -91,19 +91,19 @@ def test_dirty_pages_setup(): flags=(VFIO_USER_F_DMA_REGION_READ | VFIO_USER_F_DMA_REGION_WRITE), offset=0, addr=0x10 << PAGE_SHIFT, size=0x20 << PAGE_SHIFT) - msg(ctx, sock, VFIO_USER_DMA_MAP, payload, fds=[f.fileno()]) + msg(ctx, client.sock, VFIO_USER_DMA_MAP, payload, fds=[f.fileno()]) 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=0x40 << PAGE_SHIFT, size=0x10 << PAGE_SHIFT) - msg(ctx, sock, VFIO_USER_DMA_MAP, payload) + msg(ctx, client.sock, VFIO_USER_DMA_MAP, payload) def test_dirty_pages_short_write(): payload = struct.pack("I", 8) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) @@ -111,7 +111,7 @@ def test_dirty_pages_bad_argsz(): payload = vfio_user_dirty_pages(argsz=4, flags=VFIO_IOMMU_DIRTY_PAGES_FLAG_START) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) @@ -119,7 +119,7 @@ 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) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.ENOTSUP) @@ -137,14 +137,14 @@ def test_dirty_pages_start_bad_flags(): flags=(VFIO_IOMMU_DIRTY_PAGES_FLAG_START | VFIO_IOMMU_DIRTY_PAGES_FLAG_STOP)) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, + msg(ctx, client.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)) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) @@ -152,7 +152,7 @@ def start_logging(): payload = vfio_user_dirty_pages(argsz=len(vfio_user_dirty_pages()), flags=VFIO_IOMMU_DIRTY_PAGES_FLAG_START) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload) + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload) def test_dirty_pages_start(): @@ -165,7 +165,7 @@ 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) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) @@ -182,7 +182,7 @@ def test_dirty_pages_get_sub_range(): payload = bytes(dirty_pages) + bytes(br) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.ENOTSUP) @@ -196,7 +196,7 @@ def test_dirty_pages_get_bad_page_size(): payload = bytes(dirty_pages) + bytes(br) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) @@ -210,7 +210,7 @@ def test_dirty_pages_get_bad_bitmap_size(): payload = bytes(dirty_pages) + bytes(br) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) @@ -224,7 +224,7 @@ def test_dirty_pages_get_bad_argsz(): payload = bytes(dirty_pages) + bytes(br) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) @@ -237,7 +237,7 @@ def test_dirty_pages_get_short_reply(): payload = bytes(dirty_pages) + bytes(br) - result = msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload) + result = msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload) assert len(result) == len(vfio_user_dirty_pages()) @@ -260,7 +260,7 @@ def test_get_dirty_page_bitmap_unmapped(): payload = bytes(dirty_pages) + bytes(br) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload, expect=errno.EINVAL) @@ -275,7 +275,7 @@ def test_dirty_pages_get_unmodified(): payload = bytes(dirty_pages) + bytes(br) - result = msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload) + result = msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload) assert len(result) == argsz @@ -304,7 +304,7 @@ def get_dirty_page_bitmap(): payload = bytes(dirty_pages) + bytes(br) - result = msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload) + result = msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload) _, result = vfio_user_dirty_pages.pop_from_buffer(result) _, result = vfio_user_bitmap_range.pop_from_buffer(result) @@ -433,7 +433,7 @@ def test_dirty_pages_stop(): payload = vfio_user_dirty_pages(argsz=len(vfio_user_dirty_pages()), flags=VFIO_IOMMU_DIRTY_PAGES_FLAG_STOP) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload) + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload) def test_dirty_pages_start_with_quiesce(): @@ -444,13 +444,13 @@ def test_dirty_pages_start_with_quiesce(): payload = vfio_user_dirty_pages(argsz=len(vfio_user_dirty_pages()), flags=VFIO_IOMMU_DIRTY_PAGES_FLAG_START) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, rsp=False, busy=True) + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload, rsp=False, busy=True) ret = vfu_device_quiesced(ctx, 0) assert ret == 0 # now should be able to get the reply - get_reply(sock, expect=0) + get_reply(client.sock, expect=0) quiesce_errno = 0 @@ -480,19 +480,19 @@ def test_dirty_pages_stop_with_quiesce(): payload = vfio_user_dirty_pages(argsz=len(vfio_user_dirty_pages()), flags=VFIO_IOMMU_DIRTY_PAGES_FLAG_STOP) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload, rsp=False, busy=True) + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload, rsp=False, busy=True) ret = vfu_device_quiesced(ctx, 0) assert ret == 0 # now should be able to get the reply - get_reply(sock, expect=0) + get_reply(client.sock, expect=0) quiesce_errno = 0 def test_dirty_pages_cleanup(): - disconnect_client(ctx, sock) + client.disconnect(ctx) vfu_destroy_ctx(ctx) # ex: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab: diff --git a/test/py/test_dma_map.py b/test/py/test_dma_map.py index 12d1f6d6..65a998e7 100644 --- a/test/py/test_dma_map.py +++ b/test/py/test_dma_map.py @@ -42,31 +42,31 @@ def setup_function(function): - global ctx, sock + global ctx, client ctx = prepare_ctx_for_dma() assert ctx is not None - sock = connect_client(ctx) + client = connect_client(ctx) def teardown_function(function): - global ctx, sock - disconnect_client(ctx, sock) + global ctx, client + client.disconnect(ctx) vfu_destroy_ctx(ctx) def test_dma_region_too_big(): - global ctx, sock + global ctx, client 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=0x10 << PAGE_SHIFT, size=MAX_DMA_SIZE + PAGE_SIZE) - msg(ctx, sock, VFIO_USER_DMA_MAP, payload, expect=errno.ENOSPC) + msg(ctx, client.sock, VFIO_USER_DMA_MAP, payload, expect=errno.ENOSPC) def test_dma_region_too_many(): - global ctx, sock + global ctx, client for i in range(1, MAX_DMA_REGIONS + 2): payload = vfio_user_dma_map(argsz=len(vfio_user_dma_map()), @@ -79,7 +79,7 @@ def test_dma_region_too_many(): else: expect = 0 - msg(ctx, sock, VFIO_USER_DMA_MAP, payload, expect=expect) + msg(ctx, client.sock, VFIO_USER_DMA_MAP, payload, expect=expect) @patch('libvfio_user.quiesce_cb', side_effect=fail_with_errno(errno.EBUSY)) @@ -90,14 +90,14 @@ def test_dma_map_busy(mock_dma_register, mock_quiesce): quiescing, and then eventually quiesces, the DMA map operation succeeds. """ - global ctx, sock + global ctx, client 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=0x10 << PAGE_SHIFT, size=PAGE_SIZE) - msg(ctx, sock, VFIO_USER_DMA_MAP, payload, rsp=False, + msg(ctx, client.sock, VFIO_USER_DMA_MAP, payload, rsp=False, busy=True) assert mock_dma_register.call_count == 0 @@ -111,7 +111,7 @@ def test_dma_map_busy(mock_dma_register, mock_quiesce): mmap.PROT_READ | mmap.PROT_WRITE) mock_dma_register.assert_called_once_with(ctx, dma_info) - get_reply(sock) + get_reply(client.sock) ret = vfu_run_ctx(ctx) assert ret == 0 @@ -139,13 +139,13 @@ def test_dma_map_reply_fail(mock_dma_register, mock_quiesce, mock_reset): """Tests mapping a DMA region where the quiesce callback returns 0 and replying fails.""" - global ctx, sock + global ctx, client # The only chance we have to allow the message to be received but for the # reply to fail is in the DMA map callback, where the message has been # received but reply hasn't been sent yet. def side_effect(ctx, info): - sock.close() + client.sock.close() mock_dma_register.side_effect = side_effect @@ -156,13 +156,13 @@ def side_effect(ctx, info): VFIO_USER_F_DMA_REGION_WRITE), offset=0, addr=0x10 << PAGE_SHIFT, size=PAGE_SIZE) - msg(ctx, sock, VFIO_USER_DMA_MAP, payload, rsp=False) + msg(ctx, client.sock, VFIO_USER_DMA_MAP, payload, rsp=False) vfu_run_ctx(ctx, errno.ENOTCONN) # TODO not sure whether the following is worth it? try: - get_reply(sock) + get_reply(client.sock) except OSError as e: assert e.errno == errno.EBADF else: @@ -186,7 +186,7 @@ def test_dma_map_busy_reply_fail(mock_dma_register, mock_quiesce, mock_reset): replying fails. """ - global ctx, sock + global ctx, client # Send a DMA map command. payload = vfio_user_dma_map( @@ -195,13 +195,13 @@ def test_dma_map_busy_reply_fail(mock_dma_register, mock_quiesce, mock_reset): VFIO_USER_F_DMA_REGION_WRITE), offset=0, addr=0x10 << PAGE_SHIFT, size=PAGE_SIZE) - msg(ctx, sock, VFIO_USER_DMA_MAP, payload, rsp=False, + msg(ctx, client.sock, VFIO_USER_DMA_MAP, payload, rsp=False, busy=True) mock_quiesce.assert_called_once_with(ctx) # pretend there's a connection failure while the device is still quiescing - sock.close() + client.sock.close() mock_dma_register.assert_not_called() mock_reset.assert_not_called() diff --git a/test/py/test_dma_unmap.py b/test/py/test_dma_unmap.py index 063dedcf..a1fa94b3 100644 --- a/test/py/test_dma_unmap.py +++ b/test/py/test_dma_unmap.py @@ -33,40 +33,40 @@ from libvfio_user import * ctx = None -sock = None +client = None def setup_function(function): - global ctx, sock + global ctx, client ctx = prepare_ctx_for_dma() assert ctx is not None ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) def teardown_function(function): - global ctx, sock - disconnect_client(ctx, sock) + global ctx, client + client.disconnect(ctx) vfu_destroy_ctx(ctx) def setup_dma_regions(dma_regions=[(0x0, PAGE_SIZE)]): - global ctx, sock + global ctx, client for dma_region in dma_regions: payload = struct.pack("II", 0, 0) 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=dma_region[0], size=dma_region[1]) - msg(ctx, sock, VFIO_USER_DMA_MAP, payload) + msg(ctx, client.sock, VFIO_USER_DMA_MAP, payload) def test_dma_unmap_short_write(): payload = struct.pack("II", 0, 0) - msg(ctx, sock, VFIO_USER_DMA_UNMAP, payload, + msg(ctx, client.sock, VFIO_USER_DMA_UNMAP, payload, expect=errno.EINVAL) @@ -74,7 +74,7 @@ def test_dma_unmap_bad_argsz(): payload = vfio_user_dma_unmap(argsz=8, flags=0, addr=PAGE_SIZE, size=PAGE_SIZE) - msg(ctx, sock, VFIO_USER_DMA_UNMAP, payload, + msg(ctx, client.sock, VFIO_USER_DMA_UNMAP, payload, expect=errno.EINVAL) @@ -82,7 +82,7 @@ def test_dma_unmap_bad_argsz2(): payload = vfio_user_dma_unmap(argsz=SERVER_MAX_DATA_XFER_SIZE + 8, flags=0, addr=PAGE_SIZE, size=PAGE_SIZE) - msg(ctx, sock, VFIO_USER_DMA_UNMAP, payload, + msg(ctx, client.sock, VFIO_USER_DMA_UNMAP, payload, expect=errno.EINVAL) @@ -95,7 +95,7 @@ def test_dma_unmap_dirty_bad_argsz(): bitmap = vfio_user_bitmap(pgsize=PAGE_SIZE, size=(UINT64_MAX - argsz) + 8) payload = bytes(unmap) + bytes(bitmap) - msg(ctx, sock, VFIO_USER_DMA_UNMAP, payload, + msg(ctx, client.sock, VFIO_USER_DMA_UNMAP, payload, expect=errno.EINVAL) @@ -109,7 +109,7 @@ def test_dma_unmap_dirty_not_tracking(): bitmap = vfio_user_bitmap(pgsize=PAGE_SIZE, size=8) payload = bytes(unmap) + bytes(bitmap) + bytes(8) - msg(ctx, sock, VFIO_USER_DMA_UNMAP, payload, + msg(ctx, client.sock, VFIO_USER_DMA_UNMAP, payload, expect=errno.EINVAL) @@ -120,7 +120,7 @@ def test_dma_unmap_dirty_not_mapped(): payload = vfio_user_dirty_pages(argsz=len(vfio_user_dirty_pages()), flags=VFIO_IOMMU_DIRTY_PAGES_FLAG_START) - msg(ctx, sock, VFIO_USER_DIRTY_PAGES, payload) + msg(ctx, client.sock, VFIO_USER_DIRTY_PAGES, payload) argsz = len(vfio_user_dma_unmap()) + len(vfio_user_bitmap()) + 8 unmap = vfio_user_dma_unmap(argsz=argsz, @@ -129,7 +129,7 @@ def test_dma_unmap_dirty_not_mapped(): bitmap = vfio_user_bitmap(pgsize=PAGE_SIZE, size=8) payload = bytes(unmap) + bytes(bitmap) + bytes(8) - msg(ctx, sock, VFIO_USER_DMA_UNMAP, payload, + msg(ctx, client.sock, VFIO_USER_DMA_UNMAP, payload, expect=errno.EINVAL) @@ -138,7 +138,7 @@ def test_dma_unmap_invalid_flags(): setup_dma_regions() payload = vfio_user_dma_unmap(argsz=len(vfio_user_dma_unmap()), flags=0x4, addr=PAGE_SIZE, size=PAGE_SIZE) - msg(ctx, sock, VFIO_USER_DMA_UNMAP, payload, + msg(ctx, client.sock, VFIO_USER_DMA_UNMAP, payload, expect=errno.EINVAL) @@ -147,7 +147,7 @@ def test_dma_unmap(): setup_dma_regions() payload = vfio_user_dma_unmap(argsz=len(vfio_user_dma_unmap()), flags=0, addr=0x0, size=PAGE_SIZE) - msg(ctx, sock, VFIO_USER_DMA_UNMAP, payload) + msg(ctx, client.sock, VFIO_USER_DMA_UNMAP, payload) def test_dma_unmap_invalid_addr(): @@ -156,7 +156,7 @@ def test_dma_unmap_invalid_addr(): payload = vfio_user_dma_unmap(argsz=len(vfio_user_dma_unmap()), addr=0x10 << PAGE_SHIFT, size=PAGE_SIZE) - msg(ctx, sock, VFIO_USER_DMA_UNMAP, payload, + msg(ctx, client.sock, VFIO_USER_DMA_UNMAP, payload, expect=errno.ENOENT) @@ -167,13 +167,13 @@ def test_dma_unmap_async(mock_quiesce): mock_quiesce.side_effect = fail_with_errno(errno.EBUSY) payload = vfio_user_dma_unmap(argsz=len(vfio_user_dma_unmap()), flags=0, addr=0x0, size=PAGE_SIZE) - msg(ctx, sock, VFIO_USER_DMA_UNMAP, payload, rsp=False, + msg(ctx, client.sock, VFIO_USER_DMA_UNMAP, payload, rsp=False, busy=True) ret = vfu_device_quiesced(ctx, 0) assert ret == 0 - get_reply(sock) + get_reply(client.sock) ret = vfu_run_ctx(ctx) assert ret == 0 @@ -185,7 +185,7 @@ def test_dma_unmap_all(): setup_dma_regions(dma_regions) payload = vfio_user_dma_unmap(argsz=len(vfio_user_dma_unmap()), flags=VFIO_DMA_UNMAP_FLAG_ALL, addr=0, size=0) - msg(ctx, sock, VFIO_USER_DMA_UNMAP, payload) + msg(ctx, client.sock, VFIO_USER_DMA_UNMAP, payload) def test_dma_unmap_all_invalid_addr(): @@ -193,7 +193,7 @@ def test_dma_unmap_all_invalid_addr(): payload = vfio_user_dma_unmap(argsz=len(vfio_user_dma_unmap()), flags=VFIO_DMA_UNMAP_FLAG_ALL, addr=0x10 << PAGE_SHIFT, size=PAGE_SIZE) - msg(ctx, sock, VFIO_USER_DMA_UNMAP, payload, + msg(ctx, client.sock, VFIO_USER_DMA_UNMAP, payload, expect=errno.EINVAL) @@ -203,7 +203,7 @@ def test_dma_unmap_all_invalid_flags(): flags=(VFIO_DMA_UNMAP_FLAG_ALL | VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP), addr=0, size=0) - msg(ctx, sock, VFIO_USER_DMA_UNMAP, payload, + msg(ctx, client.sock, VFIO_USER_DMA_UNMAP, payload, expect=errno.EINVAL) # FIXME need to add unit tests that test errors in get_request_header, diff --git a/test/py/test_irq_trigger.py b/test/py/test_irq_trigger.py index 18469a42..b835a58f 100644 --- a/test/py/test_irq_trigger.py +++ b/test/py/test_irq_trigger.py @@ -32,11 +32,11 @@ import errno ctx = None -sock = None +client = None def test_irq_trigger_setup(): - global ctx, sock + global ctx, client ctx = vfu_create_ctx(flags=LIBVFIO_USER_FLAG_ATTACH_NB) assert ctx is not None @@ -50,7 +50,7 @@ def test_irq_trigger_setup(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) def test_irq_trigger_bad_subindex(): @@ -76,7 +76,7 @@ def test_irq_trigger(): fd = eventfd(initval=4) - msg(ctx, sock, VFIO_USER_DEVICE_SET_IRQS, payload, fds=[fd]) + msg(ctx, client.sock, VFIO_USER_DEVICE_SET_IRQS, payload, fds=[fd]) vfu_irq_trigger(ctx, 8) diff --git a/test/py/test_migration.py b/test/py/test_migration.py index 614a6156..a6327d82 100644 --- a/test/py/test_migration.py +++ b/test/py/test_migration.py @@ -33,11 +33,11 @@ from unittest.mock import patch ctx = None -sock = 0 +client = None def setup_function(function): - global ctx, sock + global ctx, client ctx = vfu_create_ctx(flags=LIBVFIO_USER_FLAG_ATTACH_NB) assert ctx is not None @@ -54,7 +54,7 @@ def setup_function(function): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) def teardown_function(function): @@ -73,10 +73,10 @@ def test_migration_bad_access(mock_trans, mock_quiesce): checking for a register-sized access, otherwise we'll change migration state without having quiesced. """ - global ctx, sock + global ctx, client data = VFIO_DEVICE_STATE_V1_SAVING.to_bytes(c.sizeof(c.c_int), 'little') - write_region(ctx, sock, VFU_PCI_DEV_MIGR_REGION_IDX, offset=0, + write_region(ctx, client.sock, VFU_PCI_DEV_MIGR_REGION_IDX, offset=0, count=len(data)-1, data=data, expect=errno.EINVAL) mock_trans.assert_not_called() @@ -89,10 +89,10 @@ def test_migration_trans_sync(mock_trans, mock_quiesce): Tests transitioning to the saving state. """ - global ctx, sock + global ctx, client data = VFIO_DEVICE_STATE_V1_SAVING.to_bytes(c.sizeof(c.c_int), 'little') - write_region(ctx, sock, VFU_PCI_DEV_MIGR_REGION_IDX, offset=0, + write_region(ctx, client.sock, VFU_PCI_DEV_MIGR_REGION_IDX, offset=0, count=len(data), data=data) ret = vfu_run_ctx(ctx) @@ -105,10 +105,10 @@ def test_migration_trans_sync_err(mock_trans): Tests the device returning an error when the migration state is written to. """ - global ctx, sock + global ctx, client data = VFIO_DEVICE_STATE_V1_SAVING.to_bytes(c.sizeof(c.c_int), 'little') - write_region(ctx, sock, VFU_PCI_DEV_MIGR_REGION_IDX, offset=0, + write_region(ctx, client.sock, VFU_PCI_DEV_MIGR_REGION_IDX, offset=0, count=len(data), data=data, expect=errno.EPERM) ret = vfu_run_ctx(ctx) @@ -123,18 +123,18 @@ def test_migration_trans_async(mock_trans, mock_quiesce): quiescing. """ - global ctx, sock + global ctx, client mock_quiesce data = VFIO_DEVICE_STATE_V1_SAVING.to_bytes(c.sizeof(c.c_int), 'little') - write_region(ctx, sock, VFU_PCI_DEV_MIGR_REGION_IDX, offset=0, + write_region(ctx, client.sock, VFU_PCI_DEV_MIGR_REGION_IDX, offset=0, count=len(data), data=data, rsp=False, busy=True) ret = vfu_device_quiesced(ctx, 0) assert ret == 0 - get_reply(sock) + get_reply(client.sock) ret = vfu_run_ctx(ctx) assert ret == 0 @@ -149,10 +149,10 @@ def test_migration_trans_async_err(mock_trans, mock_quiesce): the new migration state. """ - global ctx, sock + global ctx, client data = VFIO_DEVICE_STATE_V1_RUNNING.to_bytes(c.sizeof(c.c_int), 'little') - write_region(ctx, sock, VFU_PCI_DEV_MIGR_REGION_IDX, offset=0, + write_region(ctx, client.sock, VFU_PCI_DEV_MIGR_REGION_IDX, offset=0, count=len(data), data=data, rsp=False, busy=True) @@ -160,7 +160,7 @@ def test_migration_trans_async_err(mock_trans, mock_quiesce): assert ret == 0 print("waiting for reply") - get_reply(sock, errno.ENOTTY) + get_reply(client.sock, errno.ENOTTY) print("received reply") # ex: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab: # diff --git a/test/py/test_negotiate.py b/test/py/test_negotiate.py index b0188400..74049026 100644 --- a/test/py/test_negotiate.py +++ b/test/py/test_negotiate.py @@ -161,15 +161,15 @@ def test_invalid_json_bad_pgsize2(): def test_valid_negotiate_no_json(): - sock = connect_sock() + client = Client(sock=connect_sock()) payload = struct.pack("HH", LIBVFIO_USER_MAJOR, LIBVFIO_USER_MINOR) hdr = vfio_user_header(VFIO_USER_VERSION, size=len(payload)) - sock.send(hdr + payload) + client.sock.send(hdr + payload) vfu_attach_ctx(ctx) - payload = get_reply(sock) + payload = get_reply(client.sock) (major, minor, json_str, _) = struct.unpack("HH%dsc" % (len(payload) - 5), payload) assert major == LIBVFIO_USER_MAJOR @@ -179,7 +179,7 @@ def test_valid_negotiate_no_json(): assert json.capabilities.max_data_xfer_size == SERVER_MAX_DATA_XFER_SIZE # FIXME: migration object checks - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_valid_negotiate_empty_json(): diff --git a/test/py/test_pci_caps.py b/test/py/test_pci_caps.py index b7ad07b8..5a3521b5 100644 --- a/test/py/test_pci_caps.py +++ b/test/py/test_pci_caps.py @@ -153,16 +153,16 @@ def test_add_caps(mock_pci_region_cb): __test_find_caps() - sock = connect_client(ctx) + client = connect_client(ctx) - __test_pci_cap_write_hdr(sock) - __test_pci_cap_readonly(sock) + __test_pci_cap_write_hdr(client.sock) + __test_pci_cap_readonly(client.sock) # FIXME assignment to PCI config space from callback is ignored # Ideally we should ignore this test via pytest command line but this isn't # and individual test, and making it one requires a bit of effort. if not is_32bit(): - __test_pci_cap_callback(sock) - __test_pci_cap_write_pmcs(sock) + __test_pci_cap_callback(client.sock) + __test_pci_cap_write_pmcs(client.sock) def __test_find_caps(): @@ -321,14 +321,14 @@ def test_pci_cap_write_px(mock_quiesce, mock_reset): Tests function level reset. """ setup_pci_dev(realize=True) - sock = connect_client(ctx) + client = connect_client(ctx) setup_flrc(ctx) # iflr offset = PCI_STD_HEADER_SIZEOF + 8 data = b'\x00\x80' - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, count=len(data), data=data) mock_quiesce.assert_called_once_with(ctx) @@ -337,14 +337,14 @@ def test_pci_cap_write_px(mock_quiesce, mock_reset): # bad access for _off in (-1, +1): for _len in (-1, +1): - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset+_off, count=len(data)+_len, data=data, expect=errno.EINVAL) def test_pci_cap_write_msi(): setup_pci_dev(realize=True) - sock = connect_client(ctx) + client = connect_client(ctx) # Set MMC to 100b (16 interrupt vectors) mmc = 0b00001000 @@ -366,20 +366,20 @@ def test_pci_cap_write_msi(): # Test if write fails as expected # as MME is out of bounds, 111b is over the max of 101b (32 vectors) data = b'\xff\xff' - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset + PCI_MSI_FLAGS, count=len(data), data=data, expect=errno.EINVAL) # Test if write fails as expected # as MME is over MMC, 101b (32 vectors) > 100b (16 vectors) data = to_bytes_le(mme_bad, 2) - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset + PCI_MSI_FLAGS, count=len(data), data=data, expect=errno.EINVAL) # Test good write, MSI Enable + good MME data = to_bytes_le(PCI_MSI_FLAGS_ENABLE | mme_good, 2) - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset + PCI_MSI_FLAGS, count=len(data), data=data) @@ -388,21 +388,21 @@ def test_pci_cap_write_msi(): # reset data = size_before_flags * b'\x00' - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset + PCI_MSI_FLAGS, count=len(data), data=data) # Check if MMC is still present after reset (since it is RO) expected = (to_bytes_le(PCI_CAP_ID_MSI) + b'\x00' + to_bytes_le(mmc, 2) + (size_after_flags * b'\x00')) - payload = read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, - count=len(expected)) + payload = read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, + offset=offset, count=len(expected)) assert expected == payload def test_pci_cap_write_msix(): setup_pci_dev(realize=True) - sock = connect_client(ctx) + client = connect_client(ctx) flags = PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE pos = vfu_pci_add_capability(ctx, pos=0, flags=0, @@ -415,51 +415,51 @@ def test_pci_cap_write_msix(): # write exactly to Message Control: mask all vectors and enable MSI-X data = b'\xff\xff' - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset + PCI_MSIX_FLAGS, count=len(data), data=data) data = b'\xff\xff' + to_bytes_le(flags, 2) - payload = read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, - count=len(data)) + payload = read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, + offset=offset, count=len(data)) expected = to_bytes_le(PCI_CAP_ID_MSIX) + b'\x00' + \ to_bytes_le(PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE, 2) assert expected == payload # reset data = b'\x00\x00' - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset + PCI_MSIX_FLAGS, count=len(data), data=data) expected = to_bytes_le(PCI_CAP_ID_MSIX) + b'\x00\x00' - payload = read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, - count=len(expected)) + payload = read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, + offset=offset, count=len(expected)) assert expected == payload # write 2 bytes to Message Control + 1: mask all vectors and enable MSI-X # This looks bizarre, but some versions of QEMU do this. data = to_bytes_le(flags >> 8) + b'\xff' - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset + PCI_MSIX_FLAGS + 1, count=len(data), data=data) # read back entire MSI-X expected = to_bytes_le(PCI_CAP_ID_MSIX) + b'\x00' + \ to_bytes_le(flags, 2) + b'\x00\x00\x00\x00' + b'\x00\x00\x00\x00' - payload = read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, - count=PCI_CAP_MSIX_SIZEOF) + payload = read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, + offset=offset, count=PCI_CAP_MSIX_SIZEOF) assert expected == payload # reset with MSI-X enabled data = to_bytes_le(PCI_MSIX_FLAGS_ENABLE, 2) - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset + PCI_MSIX_FLAGS, count=len(data), data=data) # write 1 byte past Message Control, MSI-X should still be enabled data = b'\x00' - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset + PCI_MSIX_TABLE, count=len(data), data=data) - payload = read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, + payload = read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset + PCI_MSIX_FLAGS, count=2) assert payload == to_bytes_le(PCI_MSIX_FLAGS_ENABLE, 2) @@ -467,34 +467,34 @@ def test_pci_cap_write_msix(): def test_pci_cap_write_pxdc2(): setup_pci_dev(realize=True) - sock = connect_client(ctx) + client = connect_client(ctx) setup_flrc(ctx) offset = (vfu_pci_find_capability(ctx, False, PCI_CAP_ID_EXP) + PCI_EXP_DEVCTL2) data = b'\xde\xad' - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, count=len(data), data=data) - payload = read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, - count=len(data)) + payload = read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, + offset=offset, count=len(data)) assert payload == data def test_pci_cap_write_pxlc2(): setup_pci_dev(realize=True) - sock = connect_client(ctx) + client = connect_client(ctx) setup_flrc(ctx) offset = (vfu_pci_find_capability(ctx, False, PCI_CAP_ID_EXP) + PCI_EXP_LNKCTL2) data = b'\xbe\xef' - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, count=len(data), data=data) - payload = read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, - count=len(data)) + payload = read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, + offset=offset, count=len(data)) assert payload == data diff --git a/test/py/test_pci_ext_caps.py b/test/py/test_pci_ext_caps.py index c425c8b5..53af9df9 100644 --- a/test/py/test_pci_ext_caps.py +++ b/test/py/test_pci_ext_caps.py @@ -224,37 +224,37 @@ def test_find_ext_caps(): def test_pci_ext_cap_write_hdr(): - sock = connect_client(ctx) + client = connect_client(ctx) # struct pcie_ext_cap_hdr offset = cap_offsets[0] data = b'\x01' - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, count=len(data), data=data, expect=errno.EPERM) # struct pcie_ext_cap_vsc_hdr also offset = cap_offsets[1] + 4 - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, count=len(data), data=data, expect=errno.EPERM) - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_pci_ext_cap_readonly(): - sock = connect_client(ctx) + client = connect_client(ctx) # start of vendor payload offset = cap_offsets[1] + 8 data = b'\x01' - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, count=len(data), data=data, expect=errno.EPERM) offset = cap_offsets[1] + 8 - payload = read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, - count=5) + payload = read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, + offset=offset, count=5) assert payload == b'abcde' - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_pci_ext_cap_callback(): @@ -262,42 +262,42 @@ def test_pci_ext_cap_callback(): # FIXME assignment to PCI config space from callback is ignored if is_32bit(): return - sock = connect_client(ctx) + client = connect_client(ctx) # start of vendor payload offset = cap_offsets[2] + 8 data = b"Hello world." - payload = read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, - count=len(data)) + payload = read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, + offset=offset, count=len(data)) assert payload == data data = b"Bye world." - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, count=len(data), data=data) - payload = read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, - count=len(data)) + payload = read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, + offset=offset, count=len(data)) assert payload == data - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_pci_ext_cap_write_dsn(): - sock = connect_client(ctx) + client = connect_client(ctx) data = struct.pack("II", 1, 2) offset = cap_offsets[0] + 4 - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, count=len(data), data=data, expect=errno.EPERM) - payload = read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, - count=len(data)) + payload = read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, + offset=offset, count=len(data)) # unchanged! assert payload == struct.pack("II", 4, 8) - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_pci_ext_cap_write_vendor(): @@ -306,20 +306,20 @@ def test_pci_ext_cap_write_vendor(): if is_32bit(): return - sock = connect_client(ctx) + client = connect_client(ctx) data = struct.pack("II", 0x1, 0x2) # start of vendor payload offset = cap_offsets[2] + 8 - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, count=len(data), data=data) - payload = read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, - count=len(data)) + payload = read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, + offset=offset, count=len(data)) assert payload == data - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_pci_ext_cap_cleanup(): diff --git a/test/py/test_quiesce.py b/test/py/test_quiesce.py index b1fb2fdc..3f728270 100644 --- a/test/py/test_quiesce.py +++ b/test/py/test_quiesce.py @@ -37,10 +37,10 @@ def setup_function(function): - global ctx, sock + global ctx, client ctx = prepare_ctx_for_dma(migration_callbacks=True) assert ctx is not None - sock = connect_client(ctx) + client = connect_client(ctx) def teardown_function(function): @@ -69,14 +69,14 @@ def test_device_quiesce_error(mock_quiesce): that requested it also fails with the same error. """ - global ctx, sock + global ctx, client 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=0x1000) - msg(ctx, sock, VFIO_USER_DMA_MAP, payload, errno.ENOTTY) + msg(ctx, client.sock, VFIO_USER_DMA_MAP, payload, errno.ENOTTY) @patch('libvfio_user.dma_register') @@ -86,14 +86,14 @@ def test_device_quiesce_error_after_busy(mock_quiesce, mock_dma_register): Checks that the device fails to quiesce after it was busy quiescing. """ - global ctx, sock + global ctx, client 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=0x1000) - msg(ctx, sock, VFIO_USER_DMA_MAP, payload, rsp=False, + msg(ctx, client.sock, VFIO_USER_DMA_MAP, payload, rsp=False, busy=True) ret = vfu_device_quiesced(ctx, errno.ENOTTY) @@ -145,10 +145,10 @@ def _unmap_dma_region(ctx, sock, busy=False): def test_allowed_funcs_in_quiesced_dma_register(mock_quiesce, mock_dma_register): - global ctx, sock + global ctx, client # FIXME assert quiesce callback is called - _map_dma_region(ctx, sock) + _map_dma_region(ctx, client.sock) # FIXME it's difficult to check that mock_dma_register has been called with # the expected DMA info because we don't know the vaddr and the mapping # (2nd and 3rd arguments of vfu_dma_info_t) as they're values returned from @@ -163,9 +163,9 @@ def test_allowed_funcs_in_quiesced_dma_register(mock_quiesce, def test_allowed_funcs_in_quiesced_dma_unregister(mock_quiesce, mock_dma_unregister): - global ctx, sock - _map_dma_region(ctx, sock) - _unmap_dma_region(ctx, sock) + global ctx, client + _map_dma_region(ctx, client.sock) + _unmap_dma_region(ctx, client.sock) mock_dma_unregister.assert_called_once_with(ctx, mock.ANY) @@ -174,8 +174,8 @@ def test_allowed_funcs_in_quiesced_dma_unregister(mock_quiesce, def test_allowed_funcs_in_quiesced_dma_register_busy(mock_quiesce, mock_dma_register): - global ctx, sock - _map_dma_region(ctx, sock, errno.EBUSY) + global ctx, client + _map_dma_region(ctx, client.sock, errno.EBUSY) ret = vfu_device_quiesced(ctx, 0) assert ret == 0 mock_dma_register.assert_called_once_with(ctx, mock.ANY) @@ -186,10 +186,10 @@ def test_allowed_funcs_in_quiesced_dma_register_busy(mock_quiesce, def test_allowed_funcs_in_quiesced_dma_unregister_busy(mock_quiesce, mock_dma_unregister): - global ctx, sock - _map_dma_region(ctx, sock) + global ctx, client + _map_dma_region(ctx, client.sock) mock_quiesce.side_effect = fail_with_errno(errno.EBUSY) - _unmap_dma_region(ctx, sock, busy=True) + _unmap_dma_region(ctx, client.sock, busy=True) ret = vfu_device_quiesced(ctx, 0) assert ret == 0 mock_dma_unregister.assert_called_once_with(ctx, mock.ANY) @@ -200,10 +200,10 @@ def test_allowed_funcs_in_quiesced_dma_unregister_busy(mock_quiesce, def test_allowed_funcs_in_quiesed_migration(mock_quiesce, mock_trans): - global ctx, sock - _map_dma_region(ctx, sock) + global ctx, client + _map_dma_region(ctx, client.sock) data = VFIO_DEVICE_STATE_V1_SAVING.to_bytes(c.sizeof(c.c_int), 'little') - write_region(ctx, sock, VFU_PCI_DEV_MIGR_REGION_IDX, offset=0, + write_region(ctx, client.sock, VFU_PCI_DEV_MIGR_REGION_IDX, offset=0, count=len(data), data=data) mock_trans.assert_called_once_with(ctx, VFIO_DEVICE_STATE_V1_SAVING) @@ -213,11 +213,11 @@ def test_allowed_funcs_in_quiesed_migration(mock_quiesce, def test_allowed_funcs_in_quiesed_migration_busy(mock_quiesce, mock_trans): - global ctx, sock - _map_dma_region(ctx, sock) + global ctx, client + _map_dma_region(ctx, client.sock) mock_quiesce.side_effect = fail_with_errno(errno.EBUSY) data = VFIO_DEVICE_STATE_V1_STOP.to_bytes(c.sizeof(c.c_int), 'little') - write_region(ctx, sock, VFU_PCI_DEV_MIGR_REGION_IDX, offset=0, + write_region(ctx, client.sock, VFU_PCI_DEV_MIGR_REGION_IDX, offset=0, count=len(data), data=data, rsp=False, busy=True) ret = vfu_device_quiesced(ctx, 0) @@ -228,19 +228,19 @@ def test_allowed_funcs_in_quiesed_migration_busy(mock_quiesce, @patch('libvfio_user.reset_cb', side_effect=_side_effect) @patch('libvfio_user.quiesce_cb') def test_allowed_funcs_in_quiesced_reset(mock_quiesce, mock_reset): - global ctx, sock - _map_dma_region(ctx, sock) - msg(ctx, sock, VFIO_USER_DEVICE_RESET) + global ctx, client + _map_dma_region(ctx, client.sock) + msg(ctx, client.sock, VFIO_USER_DEVICE_RESET) mock_reset.assert_called_once_with(ctx, VFU_RESET_DEVICE) @patch('libvfio_user.reset_cb', side_effect=_side_effect) @patch('libvfio_user.quiesce_cb') def test_allowed_funcs_in_quiesced_reset_busy(mock_quiesce, mock_reset): - global ctx, sock - _map_dma_region(ctx, sock) + global ctx, client + _map_dma_region(ctx, client.sock) mock_quiesce.side_effect = fail_with_errno(errno.EBUSY) - msg(ctx, sock, VFIO_USER_DEVICE_RESET, rsp=False, + msg(ctx, client.sock, VFIO_USER_DEVICE_RESET, rsp=False, busy=True) ret = vfu_device_quiesced(ctx, 0) assert ret == 0 @@ -253,16 +253,16 @@ def test_flr(mock_quiesce, mock_reset): """Test that an FLR reset callback is still able to call functions not allowed in quiescent state.""" - global ctx, sock + global ctx, client - _map_dma_region(ctx, sock) + _map_dma_region(ctx, client.sock) setup_flrc(ctx) # iflr offset = PCI_STD_HEADER_SIZEOF + 8 data = b'\x00\x80' - write_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, + write_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=offset, count=len(data), data=data) mock_quiesce.assert_called_with(ctx) diff --git a/test/py/test_request_errors.py b/test/py/test_request_errors.py index 79af0f27..c25a7158 100644 --- a/test/py/test_request_errors.py +++ b/test/py/test_request_errors.py @@ -33,12 +33,12 @@ import os ctx = None -sock = None +client = None argsz = len(vfio_irq_set()) def setup_function(function): - global ctx, sock + global ctx, client ctx = vfu_create_ctx(flags=LIBVFIO_USER_FLAG_ATTACH_NB) assert ctx is not None @@ -64,7 +64,7 @@ def setup_function(function): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) def teardown_function(function): @@ -77,9 +77,9 @@ def test_too_small(): hdr = struct.pack("HHIII", 0xbad1, VFIO_USER_DEVICE_SET_IRQS, SIZEOF_VFIO_USER_HEADER - 1, VFIO_USER_F_TYPE_COMMAND, 0) - sock.send(hdr) + client.sock.send(hdr) vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + get_reply(client.sock, expect=errno.EINVAL) def test_too_large(): @@ -87,9 +87,9 @@ def test_too_large(): hdr = struct.pack("HHIII", 0xbad1, VFIO_USER_DEVICE_SET_IRQS, SERVER_MAX_MSG_SIZE + 1, VFIO_USER_F_TYPE_COMMAND, 0) - sock.send(hdr) + client.sock.send(hdr) vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + get_reply(client.sock, expect=errno.EINVAL) def test_unsolicited_reply(): @@ -97,24 +97,24 @@ def test_unsolicited_reply(): hdr = struct.pack("HHIII", 0xbad2, VFIO_USER_DEVICE_SET_IRQS, SIZEOF_VFIO_USER_HEADER, VFIO_USER_F_TYPE_REPLY, 0) - sock.send(hdr) + client.sock.send(hdr) vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + get_reply(client.sock, expect=errno.EINVAL) def test_bad_command(): hdr = vfio_user_header(VFIO_USER_MAX, size=1) - sock.send(hdr + b'\0') + client.sock.send(hdr + b'\0') vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + get_reply(client.sock, expect=errno.EINVAL) def test_no_payload(): hdr = vfio_user_header(VFIO_USER_DEVICE_SET_IRQS, size=0) - sock.send(hdr) + client.sock.send(hdr) vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + get_reply(client.sock, expect=errno.EINVAL) def test_bad_request_closes_fds(): @@ -126,10 +126,10 @@ def test_bad_request_closes_fds(): fd2 = 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("II", fd1, fd2))]) + client.sock.sendmsg([hdr + payload], [(socket.SOL_SOCKET, + socket.SCM_RIGHTS, struct.pack("II", fd1, fd2))]) vfu_run_ctx(ctx) - get_reply(sock, expect=errno.EINVAL) + get_reply(client.sock, expect=errno.EINVAL) # # It's a little cheesy, but this is just ensuring no fd's remain open past @@ -149,8 +149,8 @@ def test_disconnected_socket(mock_quiesce, mock_reset): """Tests that calling vfu_run_ctx on a disconnected socket results in resetting the context and returning ENOTCONN.""" - global ctx, sock - sock.close() + global ctx, client + client.sock.close() vfu_run_ctx(ctx, errno.ENOTCONN) @@ -165,8 +165,8 @@ def test_disconnected_socket_quiesce_busy(mock_quiesce): """Tests that calling vfu_run_ctx on a disconnected socket results in resetting the context which returns EBUSY.""" - global ctx, sock - sock.close() + global ctx, client + client.sock.close() vfu_run_ctx(ctx, errno.EBUSY) @@ -194,16 +194,16 @@ def test_reply_fail_quiesce_busy(mock_get_pending_bytes, mock_quiesce, mock_reset): """Tests failing to reply and the quiesce callback returning EBUSY.""" - global ctx, sock + global ctx, client def get_pending_bytes_side_effect(ctx): - sock.close() + client.sock.close() return 0 mock_get_pending_bytes.side_effect = get_pending_bytes_side_effect # read the get_pending_bytes register, it should close the socket causing # the reply to fail - read_region(ctx, sock, VFU_PCI_DEV_MIGR_REGION_IDX, + read_region(ctx, client.sock, VFU_PCI_DEV_MIGR_REGION_IDX, vfio_user_migration_info.pending_bytes.offset, vfio_user_migration_info.pending_bytes.size, rsp=False, busy=True) @@ -227,7 +227,7 @@ def get_pending_bytes_side_effect(ctx): mock_reset.assert_called_once_with(ctx, VFU_RESET_LOST_CONN) try: - get_reply(sock) + get_reply(client.sock) except OSError as e: assert e.errno == errno.EBADF else: diff --git a/test/py/test_setup_region.py b/test/py/test_setup_region.py index d00de689..05e64574 100644 --- a/test/py/test_setup_region.py +++ b/test/py/test_setup_region.py @@ -165,13 +165,13 @@ def test_setup_region_cfg_always_cb(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) - payload = read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, + payload = read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=0, count=2) assert payload == b'\xcc\xcc' - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_region_offset_overflow(): @@ -185,12 +185,12 @@ def test_region_offset_overflow(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) - read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, + read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=UINT64_MAX, count=256, expect=errno.EINVAL) - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_access_region_zero_count(): @@ -203,40 +203,40 @@ def test_access_region_zero_count(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) - payload = read_region(ctx, sock, VFU_PCI_DEV_BAR0_REGION_IDX, offset=0, - count=0) + payload = read_region(ctx, client.sock, VFU_PCI_DEV_BAR0_REGION_IDX, + offset=0, count=0) assert payload == b'' - write_region(ctx, sock, VFU_PCI_DEV_BAR0_REGION_IDX, offset=0, count=0, - data=payload) + write_region(ctx, client.sock, VFU_PCI_DEV_BAR0_REGION_IDX, offset=0, + count=0, data=payload) - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_access_region_large_count(): global ctx - sock = connect_client(ctx) + client = connect_client(ctx) - read_region(ctx, sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=0, + read_region(ctx, client.sock, VFU_PCI_DEV_CFG_REGION_IDX, offset=0, count=SERVER_MAX_DATA_XFER_SIZE + 8, expect=errno.EINVAL) - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_region_offset_too_short(): global ctx - sock = connect_client(ctx) + client = connect_client(ctx) payload = struct.pack("Q", 0) - msg(ctx, sock, VFIO_USER_REGION_WRITE, payload, + msg(ctx, client.sock, VFIO_USER_REGION_WRITE, payload, expect=errno.EINVAL) - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_setup_region_cleanup(): diff --git a/test/py/test_sgl_get_put.py b/test/py/test_sgl_get_put.py index d44dc6ee..53203fca 100644 --- a/test/py/test_sgl_get_put.py +++ b/test/py/test_sgl_get_put.py @@ -54,13 +54,13 @@ def test_sgl_get_with_invalid_region(): def test_sgl_get_without_fd(): - sock = connect_client(ctx) + client = 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=0x1000, size=4096) - msg(ctx, sock, VFIO_USER_DMA_MAP, payload) + msg(ctx, client.sock, VFIO_USER_DMA_MAP, payload) sg = dma_sg_t() iovec = iovec_t() sg.region = 0 @@ -68,11 +68,11 @@ def test_sgl_get_without_fd(): assert ret == -1 assert ctypes.get_errno() == errno.EFAULT - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_get_multiple_sge(): - sock = connect_client(ctx) + client = connect_client(ctx) regions = 4 f = tempfile.TemporaryFile() f.truncate(0x1000 * regions) @@ -81,7 +81,7 @@ def test_get_multiple_sge(): flags=(VFIO_USER_F_DMA_REGION_READ | VFIO_USER_F_DMA_REGION_WRITE), offset=0, addr=0x1000 * i, size=4096) - msg(ctx, sock, VFIO_USER_DMA_MAP, payload, fds=[f.fileno()]) + msg(ctx, client.sock, VFIO_USER_DMA_MAP, payload, fds=[f.fileno()]) ret, sg = vfu_addr_to_sgl(ctx, dma_addr=0x1000, length=4096 * 3, max_nr_sgs=3, prot=mmap.PROT_READ) @@ -94,11 +94,11 @@ def test_get_multiple_sge(): assert iovec[1].iov_len == 4096 assert iovec[2].iov_len == 4096 - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_sgl_put(): - sock = connect_client(ctx) + client = connect_client(ctx) regions = 4 f = tempfile.TemporaryFile() f.truncate(0x1000 * regions) @@ -107,7 +107,7 @@ def test_sgl_put(): flags=(VFIO_USER_F_DMA_REGION_READ | VFIO_USER_F_DMA_REGION_WRITE), offset=0, addr=0x1000 * i, size=4096) - msg(ctx, sock, VFIO_USER_DMA_MAP, payload, fds=[f.fileno()]) + msg(ctx, client.sock, VFIO_USER_DMA_MAP, payload, fds=[f.fileno()]) ret, sg = vfu_addr_to_sgl(ctx, dma_addr=0x1000, length=4096 * 3, max_nr_sgs=3, prot=mmap.PROT_READ) @@ -118,7 +118,7 @@ def test_sgl_put(): assert ret == 0 vfu_sgl_put(ctx, sg, iovec, cnt=3) - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_sgl_get_put_cleanup(): diff --git a/test/py/test_shadow_ioeventfd.py b/test/py/test_shadow_ioeventfd.py index e64c2538..1375769f 100644 --- a/test/py/test_shadow_ioeventfd.py +++ b/test/py/test_shadow_ioeventfd.py @@ -60,12 +60,12 @@ def test_shadow_ioeventfd(): assert ret == 0 # client queries I/O region FDs - sock = connect_client(ctx) + client = connect_client(ctx) payload = vfio_user_region_io_fds_request( argsz=len(vfio_user_region_io_fds_reply()) + len(vfio_user_sub_region_ioeventfd()), flags=0, index=VFU_PCI_DEV_BAR0_REGION_IDX, count=0) - newfds, ret = msg_fds(ctx, sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, + newfds, ret = msg_fds(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_IO_FDS, payload, expect=0) reply, ret = vfio_user_region_io_fds_reply.pop_from_buffer(ret) assert reply.count == 1 # 1 eventfd From a31b66e2966b5cbe356848a5ebf11d7f439f7031 Mon Sep 17 00:00:00 2001 From: Mattias Nissler Date: Wed, 30 Aug 2023 01:40:34 -0700 Subject: [PATCH 2/3] Address John's review comments. Signed-off-by: Mattias Nissler --- test/py/libvfio_user.py | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/test/py/libvfio_user.py b/test/py/libvfio_user.py index 147aadf5..352e55fa 100644 --- a/test/py/libvfio_user.py +++ b/test/py/libvfio_user.py @@ -32,7 +32,6 @@ # from types import SimpleNamespace -import collections.abc import ctypes as c import array import errno @@ -693,31 +692,13 @@ def __init__(self, sock=None): self.sock = sock self.client_cmd_socket = None - def connect(self, ctx, capabilities={}): + def connect(self, ctx): self.sock = connect_sock() - effective_caps = { - "capabilities": { - "max_data_xfer_size": VFIO_USER_DEFAULT_MAX_DATA_XFER_SIZE, - "max_msg_fds": 8, - "twin_socket": False, - }, - } - - def update(target, overrides): - for k, v in overrides.items(): - if isinstance(v, collections.abc.Mapping): - target[k] = target.get(k, {}) - update(target[k], v) - else: - target[k] = v - - update(effective_caps, capabilities) - caps_json = json.dumps(effective_caps) - + json = b'{ "capabilities": { "max_msg_fds": 8 } }' # struct vfio_user_version - payload = struct.pack("HH%dsc" % len(caps_json), LIBVFIO_USER_MAJOR, - LIBVFIO_USER_MINOR, caps_json.encode(), b'\0') + 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)) self.sock.send(hdr + payload) vfu_attach_ctx(ctx, expect=0) From 3e530dce04bd3874edd8320e47c3d33f83612bf7 Mon Sep 17 00:00:00 2001 From: Mattias Nissler Date: Wed, 30 Aug 2023 23:55:00 -0700 Subject: [PATCH 3/3] Back out twin socket related changed accidentally included here --- test/py/libvfio_user.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/py/libvfio_user.py b/test/py/libvfio_user.py index 352e55fa..fce7b993 100644 --- a/test/py/libvfio_user.py +++ b/test/py/libvfio_user.py @@ -702,16 +702,12 @@ def connect(self, ctx): hdr = vfio_user_header(VFIO_USER_VERSION, size=len(payload)) self.sock.send(hdr + payload) vfu_attach_ctx(ctx, expect=0) - fds, payload = get_reply_fds(self.sock, expect=0) - self.client_cmd_socket = socket.socket(fileno=fds[0]) if fds else None + payload = get_reply(self.sock, expect=0) return self.sock def disconnect(self, ctx): self.sock.close() self.sock = None - if self.client_cmd_socket is not None: - self.client_cmd_socket.close() - self.client_cmd_socket = None # notice client closed connection vfu_run_ctx(ctx, errno.ENOTCONN)