]> Git Repo - qemu.git/blame - hw/9pfs/xen-9p-backend.c
virtio-9p: message header is 7-byte long
[qemu.git] / hw / 9pfs / xen-9p-backend.c
CommitLineData
b37eeb02
SS
1/*
2 * Xen 9p backend
3 *
4 * Copyright Aporeto 2017
5 *
6 * Authors:
7 * Stefano Stabellini <[email protected]>
8 *
9 */
10
11#include "qemu/osdep.h"
12
13#include "hw/hw.h"
14#include "hw/9pfs/9p.h"
15#include "hw/xen/xen_backend.h"
16#include "hw/9pfs/xen-9pfs.h"
17#include "qemu/config-file.h"
18#include "fsdev/qemu-fsdev.h"
19
f23ef34a
SS
20#define VERSIONS "1"
21#define MAX_RINGS 8
22#define MAX_RING_ORDER 8
23
24typedef struct Xen9pfsRing {
25 struct Xen9pfsDev *priv;
26
27 int ref;
28 xenevtchn_handle *evtchndev;
29 int evtchn;
30 int local_port;
31 int ring_order;
32 struct xen_9pfs_data_intf *intf;
33 unsigned char *data;
34 struct xen_9pfs_data ring;
35
36 struct iovec *sg;
37 QEMUBH *bh;
38
39 /* local copies, so that we can read/write PDU data directly from
40 * the ring */
41 RING_IDX out_cons, out_size, in_cons;
42 bool inprogress;
43} Xen9pfsRing;
44
b37eeb02
SS
45typedef struct Xen9pfsDev {
46 struct XenDevice xendev; /* must be first */
f23ef34a
SS
47 V9fsState state;
48 char *path;
49 char *security_model;
50 char *tag;
51 char *id;
52
53 int num_rings;
54 Xen9pfsRing *rings;
b37eeb02
SS
55} Xen9pfsDev;
56
40a23892
SS
57static void xen_9pfs_in_sg(Xen9pfsRing *ring,
58 struct iovec *in_sg,
59 int *num,
60 uint32_t idx,
61 uint32_t size)
62{
63 RING_IDX cons, prod, masked_prod, masked_cons;
64
65 cons = ring->intf->in_cons;
66 prod = ring->intf->in_prod;
67 xen_rmb();
68 masked_prod = xen_9pfs_mask(prod, XEN_FLEX_RING_SIZE(ring->ring_order));
69 masked_cons = xen_9pfs_mask(cons, XEN_FLEX_RING_SIZE(ring->ring_order));
70
71 if (masked_prod < masked_cons) {
72 in_sg[0].iov_base = ring->ring.in + masked_prod;
73 in_sg[0].iov_len = masked_cons - masked_prod;
74 *num = 1;
75 } else {
76 in_sg[0].iov_base = ring->ring.in + masked_prod;
77 in_sg[0].iov_len = XEN_FLEX_RING_SIZE(ring->ring_order) - masked_prod;
78 in_sg[1].iov_base = ring->ring.in;
79 in_sg[1].iov_len = masked_cons;
80 *num = 2;
81 }
82}
83
84static void xen_9pfs_out_sg(Xen9pfsRing *ring,
85 struct iovec *out_sg,
86 int *num,
87 uint32_t idx)
88{
89 RING_IDX cons, prod, masked_prod, masked_cons;
90
91 cons = ring->intf->out_cons;
92 prod = ring->intf->out_prod;
93 xen_rmb();
94 masked_prod = xen_9pfs_mask(prod, XEN_FLEX_RING_SIZE(ring->ring_order));
95 masked_cons = xen_9pfs_mask(cons, XEN_FLEX_RING_SIZE(ring->ring_order));
96
97 if (masked_cons < masked_prod) {
98 out_sg[0].iov_base = ring->ring.out + masked_cons;
99 out_sg[0].iov_len = ring->out_size;
100 *num = 1;
101 } else {
102 if (ring->out_size >
103 (XEN_FLEX_RING_SIZE(ring->ring_order) - masked_cons)) {
104 out_sg[0].iov_base = ring->ring.out + masked_cons;
105 out_sg[0].iov_len = XEN_FLEX_RING_SIZE(ring->ring_order) -
106 masked_cons;
107 out_sg[1].iov_base = ring->ring.out;
108 out_sg[1].iov_len = ring->out_size -
109 (XEN_FLEX_RING_SIZE(ring->ring_order) -
110 masked_cons);
111 *num = 2;
112 } else {
113 out_sg[0].iov_base = ring->ring.out + masked_cons;
114 out_sg[0].iov_len = ring->out_size;
115 *num = 1;
116 }
117 }
118}
119
b37eeb02
SS
120static ssize_t xen_9pfs_pdu_vmarshal(V9fsPDU *pdu,
121 size_t offset,
122 const char *fmt,
123 va_list ap)
124{
40a23892
SS
125 Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
126 struct iovec in_sg[2];
127 int num;
128
129 xen_9pfs_in_sg(&xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings],
130 in_sg, &num, pdu->idx, ROUND_UP(offset + 128, 512));
131 return v9fs_iov_vmarshal(in_sg, num, offset, 0, fmt, ap);
b37eeb02
SS
132}
133
134static ssize_t xen_9pfs_pdu_vunmarshal(V9fsPDU *pdu,
135 size_t offset,
136 const char *fmt,
137 va_list ap)
138{
40a23892
SS
139 Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
140 struct iovec out_sg[2];
141 int num;
142
143 xen_9pfs_out_sg(&xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings],
144 out_sg, &num, pdu->idx);
145 return v9fs_iov_vunmarshal(out_sg, num, offset, 0, fmt, ap);
b37eeb02
SS
146}
147
148static void xen_9pfs_init_out_iov_from_pdu(V9fsPDU *pdu,
149 struct iovec **piov,
150 unsigned int *pniov)
151{
40a23892
SS
152 Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
153 Xen9pfsRing *ring = &xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings];
154 int num;
155
156 g_free(ring->sg);
157
158 ring->sg = g_malloc0(sizeof(*ring->sg) * 2);
159 xen_9pfs_out_sg(ring, ring->sg, &num, pdu->idx);
160 *piov = ring->sg;
161 *pniov = num;
b37eeb02
SS
162}
163
164static void xen_9pfs_init_in_iov_from_pdu(V9fsPDU *pdu,
165 struct iovec **piov,
166 unsigned int *pniov,
167 size_t size)
168{
40a23892
SS
169 Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
170 Xen9pfsRing *ring = &xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings];
171 int num;
172
173 g_free(ring->sg);
174
175 ring->sg = g_malloc0(sizeof(*ring->sg) * 2);
176 xen_9pfs_in_sg(ring, ring->sg, &num, pdu->idx, size);
177 *piov = ring->sg;
178 *pniov = num;
b37eeb02
SS
179}
180
181static void xen_9pfs_push_and_notify(V9fsPDU *pdu)
182{
4476e09e
SS
183 RING_IDX prod;
184 Xen9pfsDev *priv = container_of(pdu->s, Xen9pfsDev, state);
185 Xen9pfsRing *ring = &priv->rings[pdu->tag % priv->num_rings];
186
187 g_free(ring->sg);
188 ring->sg = NULL;
189
190 ring->intf->out_cons = ring->out_cons;
191 xen_wmb();
192
193 prod = ring->intf->in_prod;
194 xen_rmb();
195 ring->intf->in_prod = prod + pdu->size;
196 xen_wmb();
197
198 ring->inprogress = false;
199 xenevtchn_notify(ring->evtchndev, ring->local_port);
200
201 qemu_bh_schedule(ring->bh);
b37eeb02
SS
202}
203
204static const struct V9fsTransport xen_9p_transport = {
205 .pdu_vmarshal = xen_9pfs_pdu_vmarshal,
206 .pdu_vunmarshal = xen_9pfs_pdu_vunmarshal,
207 .init_in_iov_from_pdu = xen_9pfs_init_in_iov_from_pdu,
208 .init_out_iov_from_pdu = xen_9pfs_init_out_iov_from_pdu,
209 .push_and_notify = xen_9pfs_push_and_notify,
210};
211
212static int xen_9pfs_init(struct XenDevice *xendev)
213{
214 return 0;
215}
216
47b70fb1
SS
217static int xen_9pfs_receive(Xen9pfsRing *ring)
218{
219 P9MsgHeader h;
220 RING_IDX cons, prod, masked_prod, masked_cons;
221 V9fsPDU *pdu;
222
223 if (ring->inprogress) {
224 return 0;
225 }
226
227 cons = ring->intf->out_cons;
228 prod = ring->intf->out_prod;
229 xen_rmb();
230
231 if (xen_9pfs_queued(prod, cons, XEN_FLEX_RING_SIZE(ring->ring_order)) <
232 sizeof(h)) {
233 return 0;
234 }
235 ring->inprogress = true;
236
237 masked_prod = xen_9pfs_mask(prod, XEN_FLEX_RING_SIZE(ring->ring_order));
238 masked_cons = xen_9pfs_mask(cons, XEN_FLEX_RING_SIZE(ring->ring_order));
239
240 xen_9pfs_read_packet((uint8_t *) &h, ring->ring.out, sizeof(h),
241 masked_prod, &masked_cons,
242 XEN_FLEX_RING_SIZE(ring->ring_order));
243
244 /* cannot fail, because we only handle one request per ring at a time */
245 pdu = pdu_alloc(&ring->priv->state);
47b70fb1
SS
246 ring->out_size = le32_to_cpu(h.size_le);
247 ring->out_cons = cons + le32_to_cpu(h.size_le);
248
506f3275 249 pdu_submit(pdu, &h);
47b70fb1
SS
250
251 return 0;
252}
253
f23ef34a
SS
254static void xen_9pfs_bh(void *opaque)
255{
47b70fb1
SS
256 Xen9pfsRing *ring = opaque;
257 xen_9pfs_receive(ring);
f23ef34a
SS
258}
259
260static void xen_9pfs_evtchn_event(void *opaque)
261{
47b70fb1
SS
262 Xen9pfsRing *ring = opaque;
263 evtchn_port_t port;
264
265 port = xenevtchn_pending(ring->evtchndev);
266 xenevtchn_unmask(ring->evtchndev, port);
267
268 qemu_bh_schedule(ring->bh);
f23ef34a
SS
269}
270
b37eeb02
SS
271static int xen_9pfs_free(struct XenDevice *xendev)
272{
f23ef34a
SS
273 int i;
274 Xen9pfsDev *xen_9pdev = container_of(xendev, Xen9pfsDev, xendev);
275
276 g_free(xen_9pdev->id);
277 g_free(xen_9pdev->tag);
278 g_free(xen_9pdev->path);
279 g_free(xen_9pdev->security_model);
280
281 for (i = 0; i < xen_9pdev->num_rings; i++) {
282 if (xen_9pdev->rings[i].data != NULL) {
283 xengnttab_unmap(xen_9pdev->xendev.gnttabdev,
284 xen_9pdev->rings[i].data,
285 (1 << xen_9pdev->rings[i].ring_order));
286 }
287 if (xen_9pdev->rings[i].intf != NULL) {
288 xengnttab_unmap(xen_9pdev->xendev.gnttabdev,
289 xen_9pdev->rings[i].intf,
290 1);
291 }
292 if (xen_9pdev->rings[i].evtchndev > 0) {
293 qemu_set_fd_handler(xenevtchn_fd(xen_9pdev->rings[i].evtchndev),
294 NULL, NULL, NULL);
295 xenevtchn_unbind(xen_9pdev->rings[i].evtchndev,
296 xen_9pdev->rings[i].local_port);
297 }
298 if (xen_9pdev->rings[i].bh != NULL) {
299 qemu_bh_delete(xen_9pdev->rings[i].bh);
300 }
301 }
302 g_free(xen_9pdev->rings);
303 return 0;
b37eeb02
SS
304}
305
306static int xen_9pfs_connect(struct XenDevice *xendev)
307{
f23ef34a
SS
308 int i;
309 Xen9pfsDev *xen_9pdev = container_of(xendev, Xen9pfsDev, xendev);
310 V9fsState *s = &xen_9pdev->state;
311 QemuOpts *fsdev;
312
313 if (xenstore_read_fe_int(&xen_9pdev->xendev, "num-rings",
314 &xen_9pdev->num_rings) == -1 ||
315 xen_9pdev->num_rings > MAX_RINGS || xen_9pdev->num_rings < 1) {
316 return -1;
317 }
318
319 xen_9pdev->rings = g_malloc0(xen_9pdev->num_rings * sizeof(Xen9pfsRing));
320 for (i = 0; i < xen_9pdev->num_rings; i++) {
321 char *str;
322 int ring_order;
323
324 xen_9pdev->rings[i].priv = xen_9pdev;
325 xen_9pdev->rings[i].evtchn = -1;
326 xen_9pdev->rings[i].local_port = -1;
327
328 str = g_strdup_printf("ring-ref%u", i);
329 if (xenstore_read_fe_int(&xen_9pdev->xendev, str,
330 &xen_9pdev->rings[i].ref) == -1) {
c0c24b95 331 g_free(str);
f23ef34a
SS
332 goto out;
333 }
334 g_free(str);
335 str = g_strdup_printf("event-channel-%u", i);
336 if (xenstore_read_fe_int(&xen_9pdev->xendev, str,
337 &xen_9pdev->rings[i].evtchn) == -1) {
c0c24b95 338 g_free(str);
f23ef34a
SS
339 goto out;
340 }
341 g_free(str);
342
343 xen_9pdev->rings[i].intf = xengnttab_map_grant_ref(
344 xen_9pdev->xendev.gnttabdev,
345 xen_9pdev->xendev.dom,
346 xen_9pdev->rings[i].ref,
347 PROT_READ | PROT_WRITE);
348 if (!xen_9pdev->rings[i].intf) {
349 goto out;
350 }
351 ring_order = xen_9pdev->rings[i].intf->ring_order;
352 if (ring_order > MAX_RING_ORDER) {
353 goto out;
354 }
355 xen_9pdev->rings[i].ring_order = ring_order;
356 xen_9pdev->rings[i].data = xengnttab_map_domain_grant_refs(
357 xen_9pdev->xendev.gnttabdev,
358 (1 << ring_order),
359 xen_9pdev->xendev.dom,
360 xen_9pdev->rings[i].intf->ref,
361 PROT_READ | PROT_WRITE);
362 if (!xen_9pdev->rings[i].data) {
363 goto out;
364 }
365 xen_9pdev->rings[i].ring.in = xen_9pdev->rings[i].data;
366 xen_9pdev->rings[i].ring.out = xen_9pdev->rings[i].data +
367 XEN_FLEX_RING_SIZE(ring_order);
368
369 xen_9pdev->rings[i].bh = qemu_bh_new(xen_9pfs_bh, &xen_9pdev->rings[i]);
370 xen_9pdev->rings[i].out_cons = 0;
371 xen_9pdev->rings[i].out_size = 0;
372 xen_9pdev->rings[i].inprogress = false;
373
374
375 xen_9pdev->rings[i].evtchndev = xenevtchn_open(NULL, 0);
376 if (xen_9pdev->rings[i].evtchndev == NULL) {
377 goto out;
378 }
01cd90b6 379 qemu_set_cloexec(xenevtchn_fd(xen_9pdev->rings[i].evtchndev));
f23ef34a
SS
380 xen_9pdev->rings[i].local_port = xenevtchn_bind_interdomain
381 (xen_9pdev->rings[i].evtchndev,
382 xendev->dom,
383 xen_9pdev->rings[i].evtchn);
384 if (xen_9pdev->rings[i].local_port == -1) {
385 xen_pv_printf(xendev, 0,
386 "xenevtchn_bind_interdomain failed port=%d\n",
387 xen_9pdev->rings[i].evtchn);
388 goto out;
389 }
390 xen_pv_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
391 qemu_set_fd_handler(xenevtchn_fd(xen_9pdev->rings[i].evtchndev),
392 xen_9pfs_evtchn_event, NULL, &xen_9pdev->rings[i]);
393 }
394
395 xen_9pdev->security_model = xenstore_read_be_str(xendev, "security_model");
396 xen_9pdev->path = xenstore_read_be_str(xendev, "path");
397 xen_9pdev->id = s->fsconf.fsdev_id =
398 g_strdup_printf("xen9p%d", xendev->dev);
399 xen_9pdev->tag = s->fsconf.tag = xenstore_read_fe_str(xendev, "tag");
400 v9fs_register_transport(s, &xen_9p_transport);
401 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
402 s->fsconf.tag,
403 1, NULL);
404 qemu_opt_set(fsdev, "fsdriver", "local", NULL);
405 qemu_opt_set(fsdev, "path", xen_9pdev->path, NULL);
406 qemu_opt_set(fsdev, "security_model", xen_9pdev->security_model, NULL);
407 qemu_opts_set_id(fsdev, s->fsconf.fsdev_id);
408 qemu_fsdev_add(fsdev);
409 v9fs_device_realize_common(s, NULL);
410
b37eeb02 411 return 0;
f23ef34a
SS
412
413out:
414 xen_9pfs_free(xendev);
415 return -1;
b37eeb02
SS
416}
417
418static void xen_9pfs_alloc(struct XenDevice *xendev)
419{
f23ef34a
SS
420 xenstore_write_be_str(xendev, "versions", VERSIONS);
421 xenstore_write_be_int(xendev, "max-rings", MAX_RINGS);
422 xenstore_write_be_int(xendev, "max-ring-page-order", MAX_RING_ORDER);
b37eeb02
SS
423}
424
425static void xen_9pfs_disconnect(struct XenDevice *xendev)
426{
f23ef34a 427 /* Dynamic hotplug of PV filesystems at runtime is not supported. */
b37eeb02
SS
428}
429
430struct XenDevOps xen_9pfs_ops = {
431 .size = sizeof(Xen9pfsDev),
432 .flags = DEVOPS_FLAG_NEED_GNTDEV,
433 .alloc = xen_9pfs_alloc,
434 .init = xen_9pfs_init,
435 .initialise = xen_9pfs_connect,
436 .disconnect = xen_9pfs_disconnect,
437 .free = xen_9pfs_free,
438};
This page took 0.0767409999999999 seconds and 4 git commands to generate.