]> git.proxmox.com Git - mirror_qemu.git/blame - hw/9pfs/virtio-9p-device.c
virtio: drop name parameter for virtio_init()
[mirror_qemu.git] / hw / 9pfs / virtio-9p-device.c
CommitLineData
f4f61d27
AK
1/*
2 * Virtio 9p backend
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
6f569084
CS
14/*
15 * Not so fast! You might want to read the 9p developer docs first:
16 * https://wiki.qemu.org/Documentation/9p
17 */
18
9b8bfe21 19#include "qemu/osdep.h"
0d09e41a 20#include "hw/virtio/virtio.h"
1de7afc9 21#include "qemu/sockets.h"
f4f61d27
AK
22#include "virtio-9p.h"
23#include "fsdev/qemu-fsdev.h"
fe52840c 24#include "coth.h"
a27bd6c7 25#include "hw/qdev-properties.h"
d64ccb91 26#include "hw/virtio/virtio-access.h"
0192cc5d 27#include "qemu/iov.h"
0b8fa32f 28#include "qemu/module.h"
b036d9ac 29#include "sysemu/qtest.h"
f4f61d27 30
ea83441c 31static void virtio_9p_push_and_notify(V9fsPDU *pdu)
0d3716b4
WL
32{
33 V9fsState *s = pdu->s;
00588a0a 34 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 35 VirtQueueElement *elem = v->elems[pdu->idx];
0d3716b4
WL
36
37 /* push onto queue and notify */
00588a0a 38 virtqueue_push(v->vq, elem, pdu->size);
51b19ebe
PB
39 g_free(elem);
40 v->elems[pdu->idx] = NULL;
0d3716b4
WL
41
42 /* FIXME: we should batch these completions */
00588a0a 43 virtio_notify(VIRTIO_DEVICE(v), v->vq);
0d3716b4
WL
44}
45
0192cc5d
WL
46static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
47{
00588a0a
WL
48 V9fsVirtioState *v = (V9fsVirtioState *)vdev;
49 V9fsState *s = &v->state;
0192cc5d
WL
50 V9fsPDU *pdu;
51 ssize_t len;
d3d74d6f 52 VirtQueueElement *elem;
0192cc5d 53
00588a0a 54 while ((pdu = pdu_alloc(s))) {
c9fb47e7 55 P9MsgHeader out;
0192cc5d 56
51b19ebe
PB
57 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
58 if (!elem) {
d3d74d6f 59 goto out_free_pdu;
00588a0a
WL
60 }
61
a4d99854 62 if (iov_size(elem->in_sg, elem->in_num) < 7) {
d3d74d6f
GK
63 virtio_error(vdev,
64 "The guest sent a VirtFS request without space for "
65 "the reply");
66 goto out_free_req;
67 }
0192cc5d 68
a4d99854
GK
69 len = iov_to_buf(elem->out_sg, elem->out_num, 0, &out, 7);
70 if (len != 7) {
d3d74d6f
GK
71 virtio_error(vdev, "The guest sent a malformed VirtFS request: "
72 "header size is %zd, should be 7", len);
73 goto out_free_req;
74 }
0192cc5d 75
3a21fb2a
GK
76 v->elems[pdu->idx] = elem;
77
506f3275 78 pdu_submit(pdu, &out);
0192cc5d 79 }
d3d74d6f
GK
80
81 return;
82
83out_free_req:
84 virtqueue_detach_element(vq, elem, 0);
85 g_free(elem);
86out_free_pdu:
87 pdu_free(pdu);
0192cc5d
WL
88}
89
9d5b731d
JW
90static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features,
91 Error **errp)
f4f61d27 92{
0cd09c3a 93 virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG);
f4f61d27
AK
94 return features;
95}
96
f4f61d27
AK
97static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
98{
e9a0152b 99 int len;
f4f61d27 100 struct virtio_9p_config *cfg;
00588a0a
WL
101 V9fsVirtioState *v = VIRTIO_9P(vdev);
102 V9fsState *s = &v->state;
f4f61d27 103
e9a0152b
AK
104 len = strlen(s->tag);
105 cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
d64ccb91 106 virtio_stw_p(vdev, &cfg->tag_len, len);
e9a0152b
AK
107 /* We don't copy the terminating null to config space */
108 memcpy(cfg->tag, s->tag, len);
00588a0a 109 memcpy(config, cfg, v->config_size);
7267c094 110 g_free(cfg);
f4f61d27
AK
111}
112
0e44a0fd
GK
113static void virtio_9p_reset(VirtIODevice *vdev)
114{
115 V9fsVirtioState *v = (V9fsVirtioState *)vdev;
116
117 v9fs_reset(&v->state);
118}
119
ea83441c
SS
120static ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset,
121 const char *fmt, va_list ap)
fe9fa96d 122{
00588a0a
WL
123 V9fsState *s = pdu->s;
124 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 125 VirtQueueElement *elem = v->elems[pdu->idx];
8d37de41 126 ssize_t ret;
00588a0a 127
8d37de41
GK
128 ret = v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap);
129 if (ret < 0) {
130 VirtIODevice *vdev = VIRTIO_DEVICE(v);
131
132 virtio_error(vdev, "Failed to encode VirtFS reply type %d",
133 pdu->id + 1);
134 }
135 return ret;
fe9fa96d
WL
136}
137
ea83441c
SS
138static ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset,
139 const char *fmt, va_list ap)
fe9fa96d 140{
00588a0a
WL
141 V9fsState *s = pdu->s;
142 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 143 VirtQueueElement *elem = v->elems[pdu->idx];
8d37de41
GK
144 ssize_t ret;
145
146 ret = v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap);
147 if (ret < 0) {
148 VirtIODevice *vdev = VIRTIO_DEVICE(v);
00588a0a 149
8d37de41
GK
150 virtio_error(vdev, "Failed to decode VirtFS request type %d", pdu->id);
151 }
152 return ret;
fe9fa96d
WL
153}
154
88da0b03 155static void virtio_init_in_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
cf45183b 156 unsigned int *pniov, size_t size)
592707af 157{
00588a0a
WL
158 V9fsState *s = pdu->s;
159 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
51b19ebe 160 VirtQueueElement *elem = v->elems[pdu->idx];
8d37de41
GK
161 size_t buf_size = iov_size(elem->in_sg, elem->in_num);
162
cf45183b 163 if (buf_size < size) {
8d37de41
GK
164 VirtIODevice *vdev = VIRTIO_DEVICE(v);
165
166 virtio_error(vdev,
cf45183b
SS
167 "VirtFS reply type %d needs %zu bytes, buffer has %zu",
168 pdu->id + 1, size, buf_size);
8d37de41 169 }
00588a0a 170
88da0b03
SS
171 *piov = elem->in_sg;
172 *pniov = elem->in_num;
173}
174
175static void virtio_init_out_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
8d37de41 176 unsigned int *pniov, size_t size)
88da0b03
SS
177{
178 V9fsState *s = pdu->s;
179 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
180 VirtQueueElement *elem = v->elems[pdu->idx];
8d37de41
GK
181 size_t buf_size = iov_size(elem->out_sg, elem->out_num);
182
183 if (buf_size < size) {
184 VirtIODevice *vdev = VIRTIO_DEVICE(v);
185
186 virtio_error(vdev,
187 "VirtFS request type %d needs %zu bytes, buffer has %zu",
188 pdu->id, size, buf_size);
189 }
88da0b03
SS
190
191 *piov = elem->out_sg;
192 *pniov = elem->out_num;
592707af
WL
193}
194
8e71b96c 195static const V9fsTransport virtio_9p_transport = {
ea83441c
SS
196 .pdu_vmarshal = virtio_pdu_vmarshal,
197 .pdu_vunmarshal = virtio_pdu_vunmarshal,
88da0b03
SS
198 .init_in_iov_from_pdu = virtio_init_in_iov_from_pdu,
199 .init_out_iov_from_pdu = virtio_init_out_iov_from_pdu,
ea83441c
SS
200 .push_and_notify = virtio_9p_push_and_notify,
201};
202
bd3be4db
GK
203static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
204{
205 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
206 V9fsVirtioState *v = VIRTIO_9P(dev);
207 V9fsState *s = &v->state;
b036d9ac
CS
208 FsDriverEntry *fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
209
210 if (qtest_enabled() && fse) {
211 fse->export_flags |= V9FS_NO_PERF_WARN;
212 }
bd3be4db 213
066eb006
GK
214 if (v9fs_device_realize_common(s, &virtio_9p_transport, errp)) {
215 return;
bd3be4db
GK
216 }
217
218 v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag);
3857cd5c 219 virtio_init(vdev, VIRTIO_ID_9P, v->config_size);
bd3be4db 220 v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
bd3be4db
GK
221}
222
b69c3c21 223static void virtio_9p_device_unrealize(DeviceState *dev)
bd3be4db
GK
224{
225 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
226 V9fsVirtioState *v = VIRTIO_9P(dev);
227 V9fsState *s = &v->state;
228
ad30a9e9 229 virtio_delete_queue(v->vq);
bd3be4db 230 virtio_cleanup(vdev);
b69c3c21 231 v9fs_device_unrealize_common(s);
bd3be4db
GK
232}
233
e7303c43
FK
234/* virtio-9p device */
235
dcaf8dda
HP
236static const VMStateDescription vmstate_virtio_9p = {
237 .name = "virtio-9p",
238 .minimum_version_id = 1,
239 .version_id = 1,
240 .fields = (VMStateField[]) {
241 VMSTATE_VIRTIO_DEVICE,
242 VMSTATE_END_OF_LIST()
243 },
244};
18e0e5b2 245
e7303c43 246static Property virtio_9p_properties[] = {
00588a0a
WL
247 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag),
248 DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id),
e7303c43
FK
249 DEFINE_PROP_END_OF_LIST(),
250};
251
252static void virtio_9p_class_init(ObjectClass *klass, void *data)
253{
254 DeviceClass *dc = DEVICE_CLASS(klass);
255 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
59be7522 256
4f67d30b 257 device_class_set_props(dc, virtio_9p_properties);
18e0e5b2 258 dc->vmsd = &vmstate_virtio_9p;
125ee0ed 259 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
59be7522 260 vdc->realize = virtio_9p_device_realize;
6cecf093 261 vdc->unrealize = virtio_9p_device_unrealize;
e7303c43
FK
262 vdc->get_features = virtio_9p_get_features;
263 vdc->get_config = virtio_9p_get_config;
0e44a0fd 264 vdc->reset = virtio_9p_reset;
e7303c43
FK
265}
266
267static const TypeInfo virtio_device_info = {
268 .name = TYPE_VIRTIO_9P,
269 .parent = TYPE_VIRTIO_DEVICE,
00588a0a 270 .instance_size = sizeof(V9fsVirtioState),
e7303c43
FK
271 .class_init = virtio_9p_class_init,
272};
273
274static void virtio_9p_register_types(void)
275{
276 type_register_static(&virtio_device_info);
277}
278
279type_init(virtio_9p_register_types)