diff options
author | John Levon <john.levon@nutanix.com> | 2021-05-10 15:01:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-10 15:01:50 +0100 |
commit | 702a4cad49c40406ff498e0da4a80c6d07b4fc83 (patch) | |
tree | 13dab2b60f6f5882e896174a4480654f7e6b3520 /test/py/libvfio_user.py | |
parent | e92b1f6b57d0131ec4aa581e57e10ab7d628a0cd (diff) | |
download | libvfio-user-702a4cad49c40406ff498e0da4a80c6d07b4fc83.zip libvfio-user-702a4cad49c40406ff498e0da4a80c6d07b4fc83.tar.gz libvfio-user-702a4cad49c40406ff498e0da4a80c6d07b4fc83.tar.bz2 |
python tests: add vfu_create_ctx(), vfu_realize_ctx() (#448)
Signed-off-by: John Levon <john.levon@nutanix.com>
Reviewed-by: Thanos Makatos <thanos.makatos@nutanix.com>
Diffstat (limited to 'test/py/libvfio_user.py')
-rw-r--r-- | test/py/libvfio_user.py | 120 |
1 files changed, 116 insertions, 4 deletions
diff --git a/test/py/libvfio_user.py b/test/py/libvfio_user.py index 8dee414..2d64269 100644 --- a/test/py/libvfio_user.py +++ b/test/py/libvfio_user.py @@ -77,6 +77,47 @@ VFIO_USER_F_TYPE_REPLY = 1 SIZEOF_VFIO_USER_HEADER = 16 +VFU_PCI_DEV_BAR0_REGION_IDX = 0 +VFU_PCI_DEV_BAR1_REGION_IDX = 1 +VFU_PCI_DEV_BAR2_REGION_IDX = 2 +VFU_PCI_DEV_BAR3_REGION_IDX = 3 +VFU_PCI_DEV_BAR4_REGION_IDX = 4 +VFU_PCI_DEV_BAR5_REGION_IDX = 5 +VFU_PCI_DEV_ROM_REGION_IDX = 6 +VFU_PCI_DEV_CFG_REGION_IDX = 7 +VFU_PCI_DEV_VGA_REGION_IDX = 8 +VFU_PCI_DEV_MIGR_REGION_IDX = 9 +VFU_PCI_DEV_NUM_REGIONS = 10 + +VFU_REGION_FLAG_READ = 1 +VFU_REGION_FLAG_WRITE = 2 +VFU_REGION_FLAG_RW = (VFU_REGION_FLAG_READ | VFU_REGION_FLAG_WRITE) +VFU_REGION_FLAG_MEM = 4 + +PCI_BARS_NR = 6 + +# enum vfu_dev_irq_type +VFU_DEV_INTX_IRQ = 0 +VFU_DEV_MSI_IRQ = 1 +VFU_DEV_MSIX_IRQ = 2 +VFU_DEV_ERR_IRQ = 3 +VFU_DEV_REQ_IRQ = 4 +VFU_DEV_NUM_IRQS = 5 + +# vfu_pci_type_t +VFU_PCI_TYPE_CONVENTIONAL = 0 +VFU_PCI_TYPE_PCI_X_1 = 1 +VFU_PCI_TYPE_PCI_X_2 = 2 +VFU_PCI_TYPE_EXPRESS = 3 + +PCI_HEADER_TYPE_NORMAL = 0 + +PCI_STD_HEADER_SIZEOF = 64 + +PCI_CFG_SPACE_SIZE = 256 +PCI_CAP_ID_PM = b'\1' + + topdir = os.path.realpath(os.path.dirname(__file__) + "/../..") build_type = os.getenv("BUILD_TYPE", default="dbg") libname = "%s/build/%s/lib/libvfio-user.so" % (topdir, build_type) @@ -89,10 +130,60 @@ lib.vfu_realize_ctx.argtypes = (c.c_void_p,) lib.vfu_attach_ctx.argtypes = (c.c_void_p,) lib.vfu_run_ctx.argtypes = (c.c_void_p,) lib.vfu_destroy_ctx.argtypes = (c.c_void_p,) +lib.vfu_setup_region.argtypes = (c.c_void_p, c.c_int, c.c_long, c.c_void_p, + c.c_int, c.c_void_p, c.c_int, c.c_int) +lib.vfu_pci_get_config_space.argtypes = (c.c_void_p,) +lib.vfu_pci_get_config_space.restype = (c.c_void_p) +lib.vfu_setup_device_nr_irqs.argtypes = (c.c_void_p, c.c_int, c.c_int) +lib.vfu_pci_init.argtypes = (c.c_void_p, c.c_int, c.c_int, c.c_int) +lib.vfu_pci_add_capability.argtypes = (c.c_void_p, c.c_long, c.c_int, + c.POINTER(c.c_byte)) msg_id = 1 # +# Structures +# +class vfu_bar_t(c.Union): + _pack_ = 1 + _fields_ = [ + ("mem", c.c_int), + ("io", c.c_int) + ] + +class vfu_pci_hdr_intr_t(c.Structure): + _pack_ = 1 + _fields_ = [ + ("iline", c.c_byte), + ("ipin", c.c_byte) + ] + +class vfu_pci_hdr_t(c.Structure): + _pack_ = 1 + _fields_ = [ + ("id", c.c_int), + ("cmd", c.c_short), + ("sts", c.c_short), + ("rid", c.c_byte), + ("cc_pi", c.c_byte), + ("cc_scc", c.c_byte), + ("cc_bcc", c.c_byte), + ("cls", c.c_byte), + ("mlt", c.c_byte), + ("htype", c.c_byte), + ("bist", c.c_byte), + ("bars", vfu_bar_t * PCI_BARS_NR), + ("ccptr", c.c_int), + ("ss", c.c_int), + ("erom", c.c_int), + ("cap", c.c_byte), + ("res1", c.c_byte * 7), + ("intr", vfu_pci_hdr_intr_t), + ("mgnt", c.c_byte), + ("mlat", c.c_byte) + ] + +# # Util functions # @@ -107,12 +198,13 @@ def get_reply(sock, expect=0): assert errno == expect return buf[16:] -# -# Parse JSON into an object with attributes (instead of using a dict) -# def parse_json(json_str): + """Parse JSON into an object with attributes (instead of using a dict).""" return json.loads(json_str, object_hook=lambda d: SimpleNamespace(**d)) +def get_pci_header(ctx): + ptr = lib.vfu_pci_get_config_space(ctx) + return c.cast(ptr, c.POINTER(vfu_pci_hdr_t)).contents # # Library wrappers @@ -157,10 +249,30 @@ def vfu_attach_ctx(ctx, expect=0): return ret def vfu_run_ctx(ctx): - lib.vfu_run_ctx(ctx) + return lib.vfu_run_ctx(ctx) def vfu_destroy_ctx(ctx): lib.vfu_destroy_ctx(ctx) ctx = None if os.path.exists(SOCK_PATH): os.remove(SOCK_PATH) + +def vfu_setup_region(ctx, index, size, flags=0): + assert ctx != None + ret = lib.vfu_setup_region(ctx, index, size, None, flags, None, 0, -1) + return ret + +def vfu_setup_device_nr_irqs(ctx, irqtype, count): + assert ctx != None + return lib.vfu_setup_device_nr_irqs(ctx, irqtype, count) + +def vfu_pci_init(ctx, pci_type=VFU_PCI_TYPE_EXPRESS, + hdr_type=PCI_HEADER_TYPE_NORMAL): + assert ctx != None + return lib.vfu_pci_init(ctx, pci_type, hdr_type, 0) + +def vfu_pci_add_capability(ctx, pos, flags, data): + assert ctx != None + + databuf = (c.c_byte * len(data)).from_buffer(bytearray(data)) + return lib.vfu_pci_add_capability(ctx, pos, flags, databuf) |