2 * libqos driver framework
4 * Based on tests/qtest/libqos/virtio-blk.c
6 * Copyright (c) 2020 Coiby Xu <coiby.xu@gmail.com>
8 * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License version 2.1 as published by the Free Software Foundation.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>
23 #include "qemu/osdep.h"
25 #include "qemu/module.h"
26 #include "standard-headers/linux/virtio_blk.h"
27 #include "vhost-user-blk.h"
32 /* virtio-blk-device */
33 static void *qvhost_user_blk_get_driver(QVhostUserBlk
*v_blk
,
34 const char *interface
)
36 if (!g_strcmp0(interface
, "vhost-user-blk")) {
39 if (!g_strcmp0(interface
, "virtio")) {
43 fprintf(stderr
, "%s not present in vhost-user-blk-device\n", interface
);
44 g_assert_not_reached();
47 static void *qvhost_user_blk_device_get_driver(void *object
,
48 const char *interface
)
50 QVhostUserBlkDevice
*v_blk
= object
;
51 return qvhost_user_blk_get_driver(&v_blk
->blk
, interface
);
54 static void *vhost_user_blk_device_create(void *virtio_dev
,
55 QGuestAllocator
*t_alloc
,
58 QVhostUserBlkDevice
*vhost_user_blk
= g_new0(QVhostUserBlkDevice
, 1);
59 QVhostUserBlk
*interface
= &vhost_user_blk
->blk
;
61 interface
->vdev
= virtio_dev
;
63 vhost_user_blk
->obj
.get_driver
= qvhost_user_blk_device_get_driver
;
65 return &vhost_user_blk
->obj
;
69 static void *qvhost_user_blk_pci_get_driver(void *object
, const char *interface
)
71 QVhostUserBlkPCI
*v_blk
= object
;
72 if (!g_strcmp0(interface
, "pci-device")) {
73 return v_blk
->pci_vdev
.pdev
;
75 return qvhost_user_blk_get_driver(&v_blk
->blk
, interface
);
78 static void *vhost_user_blk_pci_create(void *pci_bus
, QGuestAllocator
*t_alloc
,
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
;
85 virtio_pci_init(&vhost_user_blk
->pci_vdev
, pci_bus
, addr
);
86 interface
->vdev
= &vhost_user_blk
->pci_vdev
.vdev
;
88 g_assert_cmphex(interface
->vdev
->device_type
, ==, VIRTIO_ID_BLOCK
);
90 obj
->get_driver
= qvhost_user_blk_pci_get_driver
;
95 static void vhost_user_blk_register_nodes(void)
98 * FIXME: every test using these two nodes needs to setup a
99 * -drive,id=drive0 otherwise QEMU is not going to start.
100 * Therefore, we do not include "produces" edge for virtio
101 * and pci-device yet.
104 char *arg
= g_strdup_printf("id=drv0,chardev=char1,addr=%x.%x",
108 .devfn
= QPCI_DEVFN(PCI_SLOT
, PCI_FN
),
111 QOSGraphEdgeOptions opts
= { };
113 /* virtio-blk-device */
114 /** opts.extra_device_opts = "drive=drive0"; */
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");
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");
130 libqos_init(vhost_user_blk_register_nodes
);