1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include "qemu/osdep.h"
24#include "../libqtest.h"
25#include "qemu/module.h"
26#include "standard-headers/linux/virtio_blk.h"
27#include "vhost-user-blk.h"
28
29#define PCI_SLOT 0x04
30#define PCI_FN 0x00
31
32
33static void *qvhost_user_blk_get_driver(QVhostUserBlk *v_blk,
34 const char *interface)
35{
36 if (!g_strcmp0(interface, "vhost-user-blk")) {
37 return v_blk;
38 }
39 if (!g_strcmp0(interface, "virtio")) {
40 return v_blk->vdev;
41 }
42
43 fprintf(stderr, "%s not present in vhost-user-blk-device\n", interface);
44 g_assert_not_reached();
45}
46
47static void *qvhost_user_blk_device_get_driver(void *object,
48 const char *interface)
49{
50 QVhostUserBlkDevice *v_blk = object;
51 return qvhost_user_blk_get_driver(&v_blk->blk, interface);
52}
53
54static void *vhost_user_blk_device_create(void *virtio_dev,
55 QGuestAllocator *t_alloc,
56 void *addr)
57{
58 QVhostUserBlkDevice *vhost_user_blk = g_new0(QVhostUserBlkDevice, 1);
59 QVhostUserBlk *interface = &vhost_user_blk->blk;
60
61 interface->vdev = virtio_dev;
62
63 vhost_user_blk->obj.get_driver = qvhost_user_blk_device_get_driver;
64
65 return &vhost_user_blk->obj;
66}
67
68
69static void *qvhost_user_blk_pci_get_driver(void *object, const char *interface)
70{
71 QVhostUserBlkPCI *v_blk = object;
72 if (!g_strcmp0(interface, "pci-device")) {
73 return v_blk->pci_vdev.pdev;
74 }
75 return qvhost_user_blk_get_driver(&v_blk->blk, interface);
76}
77
78static void *vhost_user_blk_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
79 void *addr)
80{
81 QVhostUserBlkPCI *vhost_user_blk = g_new0(QVhostUserBlkPCI, 1);
82 QVhostUserBlk *interface = &vhost_user_blk->blk;
83 QOSGraphObject *obj = &vhost_user_blk->pci_vdev.obj;
84
85 virtio_pci_init(&vhost_user_blk->pci_vdev, pci_bus, addr);
86 interface->vdev = &vhost_user_blk->pci_vdev.vdev;
87
88 g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_BLOCK);
89
90 obj->get_driver = qvhost_user_blk_pci_get_driver;
91
92 return obj;
93}
94
95static void vhost_user_blk_register_nodes(void)
96{
97
98
99
100
101
102
103
104 char *arg = g_strdup_printf("id=drv0,chardev=char1,addr=%x.%x",
105 PCI_SLOT, PCI_FN);
106
107 QPCIAddress addr = {
108 .devfn = QPCI_DEVFN(PCI_SLOT, PCI_FN),
109 };
110
111 QOSGraphEdgeOptions opts = { };
112
113
114
115 qos_node_create_driver("vhost-user-blk-device",
116 vhost_user_blk_device_create);
117 qos_node_consumes("vhost-user-blk-device", "virtio-bus", &opts);
118 qos_node_produces("vhost-user-blk-device", "vhost-user-blk");
119
120
121 opts.extra_device_opts = arg;
122 add_qpci_address(&opts, &addr);
123 qos_node_create_driver("vhost-user-blk-pci", vhost_user_blk_pci_create);
124 qos_node_consumes("vhost-user-blk-pci", "pci-bus", &opts);
125 qos_node_produces("vhost-user-blk-pci", "vhost-user-blk");
126
127 g_free(arg);
128}
129
130libqos_init(vhost_user_blk_register_nodes);
131