4 * Copyright Aporeto 2017
12 * Not so fast! You might want to read the 9p developer docs first:
13 * https://wiki.qemu.org/Documentation/9p
16 #include "qemu/osdep.h"
18 #include "hw/9pfs/9p.h"
19 #include "hw/xen/xen-legacy-backend.h"
20 #include "hw/9pfs/xen-9pfs.h"
21 #include "qapi/error.h"
22 #include "qemu/config-file.h"
23 #include "qemu/main-loop.h"
24 #include "qemu/option.h"
25 #include "fsdev/qemu-fsdev.h"
29 #define MAX_RING_ORDER 9
31 typedef struct Xen9pfsRing {
32 struct Xen9pfsDev *priv;
35 xenevtchn_handle *evtchndev;
39 struct xen_9pfs_data_intf *intf;
41 struct xen_9pfs_data ring;
47 /* local copies, so that we can read/write PDU data directly from
49 RING_IDX out_cons, out_size, in_cons;
53 typedef struct Xen9pfsDev {
54 struct XenLegacyDevice xendev; /* must be first */
65 static void xen_9pfs_disconnect(struct XenLegacyDevice *xendev);
67 static void xen_9pfs_in_sg(Xen9pfsRing *ring,
73 RING_IDX cons, prod, masked_prod, masked_cons;
75 cons = ring->intf->in_cons;
76 prod = ring->intf->in_prod;
78 masked_prod = xen_9pfs_mask(prod, XEN_FLEX_RING_SIZE(ring->ring_order));
79 masked_cons = xen_9pfs_mask(cons, XEN_FLEX_RING_SIZE(ring->ring_order));
81 if (masked_prod < masked_cons) {
82 in_sg[0].iov_base = ring->ring.in + masked_prod;
83 in_sg[0].iov_len = masked_cons - masked_prod;
86 in_sg[0].iov_base = ring->ring.in + masked_prod;
87 in_sg[0].iov_len = XEN_FLEX_RING_SIZE(ring->ring_order) - masked_prod;
88 in_sg[1].iov_base = ring->ring.in;
89 in_sg[1].iov_len = masked_cons;
94 static void xen_9pfs_out_sg(Xen9pfsRing *ring,
99 RING_IDX cons, prod, masked_prod, masked_cons;
101 cons = ring->intf->out_cons;
102 prod = ring->intf->out_prod;
104 masked_prod = xen_9pfs_mask(prod, XEN_FLEX_RING_SIZE(ring->ring_order));
105 masked_cons = xen_9pfs_mask(cons, XEN_FLEX_RING_SIZE(ring->ring_order));
107 if (masked_cons < masked_prod) {
108 out_sg[0].iov_base = ring->ring.out + masked_cons;
109 out_sg[0].iov_len = ring->out_size;
113 (XEN_FLEX_RING_SIZE(ring->ring_order) - masked_cons)) {
114 out_sg[0].iov_base = ring->ring.out + masked_cons;
115 out_sg[0].iov_len = XEN_FLEX_RING_SIZE(ring->ring_order) -
117 out_sg[1].iov_base = ring->ring.out;
118 out_sg[1].iov_len = ring->out_size -
119 (XEN_FLEX_RING_SIZE(ring->ring_order) -
123 out_sg[0].iov_base = ring->ring.out + masked_cons;
124 out_sg[0].iov_len = ring->out_size;
130 static ssize_t xen_9pfs_pdu_vmarshal(V9fsPDU *pdu,
135 Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
136 struct iovec in_sg[2];
140 xen_9pfs_in_sg(&xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings],
141 in_sg, &num, pdu->idx, ROUND_UP(offset + 128, 512));
143 ret = v9fs_iov_vmarshal(in_sg, num, offset, 0, fmt, ap);
145 xen_pv_printf(&xen_9pfs->xendev, 0,
146 "Failed to encode VirtFS reply type %d\n",
148 xen_be_set_state(&xen_9pfs->xendev, XenbusStateClosing);
149 xen_9pfs_disconnect(&xen_9pfs->xendev);
154 static ssize_t xen_9pfs_pdu_vunmarshal(V9fsPDU *pdu,
159 Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
160 struct iovec out_sg[2];
164 xen_9pfs_out_sg(&xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings],
165 out_sg, &num, pdu->idx);
167 ret = v9fs_iov_vunmarshal(out_sg, num, offset, 0, fmt, ap);
169 xen_pv_printf(&xen_9pfs->xendev, 0,
170 "Failed to decode VirtFS request type %d\n", pdu->id);
171 xen_be_set_state(&xen_9pfs->xendev, XenbusStateClosing);
172 xen_9pfs_disconnect(&xen_9pfs->xendev);
177 static void xen_9pfs_init_out_iov_from_pdu(V9fsPDU *pdu,
182 Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
183 Xen9pfsRing *ring = &xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings];
188 ring->sg = g_new0(struct iovec, 2);
189 xen_9pfs_out_sg(ring, ring->sg, &num, pdu->idx);
194 static void xen_9pfs_init_in_iov_from_pdu(V9fsPDU *pdu,
199 Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
200 Xen9pfsRing *ring = &xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings];
206 ring->sg = g_new0(struct iovec, 2);
207 ring->co = qemu_coroutine_self();
208 /* make sure other threads see ring->co changes before continuing */
212 xen_9pfs_in_sg(ring, ring->sg, &num, pdu->idx, size);
213 buf_size = iov_size(ring->sg, num);
214 if (buf_size < size) {
215 qemu_coroutine_yield();
219 /* make sure other threads see ring->co changes before continuing */
226 static void xen_9pfs_push_and_notify(V9fsPDU *pdu)
229 Xen9pfsDev *priv = container_of(pdu->s, Xen9pfsDev, state);
230 Xen9pfsRing *ring = &priv->rings[pdu->tag % priv->num_rings];
235 ring->intf->out_cons = ring->out_cons;
238 prod = ring->intf->in_prod;
240 ring->intf->in_prod = prod + pdu->size;
243 ring->inprogress = false;
244 xenevtchn_notify(ring->evtchndev, ring->local_port);
246 qemu_bh_schedule(ring->bh);
249 static const V9fsTransport xen_9p_transport = {
250 .pdu_vmarshal = xen_9pfs_pdu_vmarshal,
251 .pdu_vunmarshal = xen_9pfs_pdu_vunmarshal,
252 .init_in_iov_from_pdu = xen_9pfs_init_in_iov_from_pdu,
253 .init_out_iov_from_pdu = xen_9pfs_init_out_iov_from_pdu,
254 .push_and_notify = xen_9pfs_push_and_notify,
257 static int xen_9pfs_init(struct XenLegacyDevice *xendev)
262 static int xen_9pfs_receive(Xen9pfsRing *ring)
265 RING_IDX cons, prod, masked_prod, masked_cons, queued;
268 if (ring->inprogress) {
272 cons = ring->intf->out_cons;
273 prod = ring->intf->out_prod;
276 queued = xen_9pfs_queued(prod, cons, XEN_FLEX_RING_SIZE(ring->ring_order));
277 if (queued < sizeof(h)) {
280 ring->inprogress = true;
282 masked_prod = xen_9pfs_mask(prod, XEN_FLEX_RING_SIZE(ring->ring_order));
283 masked_cons = xen_9pfs_mask(cons, XEN_FLEX_RING_SIZE(ring->ring_order));
285 xen_9pfs_read_packet((uint8_t *) &h, ring->ring.out, sizeof(h),
286 masked_prod, &masked_cons,
287 XEN_FLEX_RING_SIZE(ring->ring_order));
288 if (queued < le32_to_cpu(h.size_le)) {
292 /* cannot fail, because we only handle one request per ring at a time */
293 pdu = pdu_alloc(&ring->priv->state);
294 ring->out_size = le32_to_cpu(h.size_le);
295 ring->out_cons = cons + le32_to_cpu(h.size_le);
302 static void xen_9pfs_bh(void *opaque)
304 Xen9pfsRing *ring = opaque;
308 wait = ring->co != NULL && qemu_coroutine_entered(ring->co);
309 /* paired with the smb_wmb barriers in xen_9pfs_init_in_iov_from_pdu */
316 if (ring->co != NULL) {
317 qemu_coroutine_enter_if_inactive(ring->co);
319 xen_9pfs_receive(ring);
322 static void xen_9pfs_evtchn_event(void *opaque)
324 Xen9pfsRing *ring = opaque;
327 port = xenevtchn_pending(ring->evtchndev);
328 xenevtchn_unmask(ring->evtchndev, port);
330 qemu_bh_schedule(ring->bh);
333 static void xen_9pfs_disconnect(struct XenLegacyDevice *xendev)
335 Xen9pfsDev *xen_9pdev = container_of(xendev, Xen9pfsDev, xendev);
338 for (i = 0; i < xen_9pdev->num_rings; i++) {
339 if (xen_9pdev->rings[i].evtchndev != NULL) {
340 qemu_set_fd_handler(xenevtchn_fd(xen_9pdev->rings[i].evtchndev),
342 xenevtchn_unbind(xen_9pdev->rings[i].evtchndev,
343 xen_9pdev->rings[i].local_port);
344 xen_9pdev->rings[i].evtchndev = NULL;
349 static int xen_9pfs_free(struct XenLegacyDevice *xendev)
351 Xen9pfsDev *xen_9pdev = container_of(xendev, Xen9pfsDev, xendev);
354 if (xen_9pdev->rings[0].evtchndev != NULL) {
355 xen_9pfs_disconnect(xendev);
358 for (i = 0; i < xen_9pdev->num_rings; i++) {
359 if (xen_9pdev->rings[i].data != NULL) {
360 xen_be_unmap_grant_refs(&xen_9pdev->xendev,
361 xen_9pdev->rings[i].data,
362 (1 << xen_9pdev->rings[i].ring_order));
364 if (xen_9pdev->rings[i].intf != NULL) {
365 xen_be_unmap_grant_refs(&xen_9pdev->xendev,
366 xen_9pdev->rings[i].intf,
369 if (xen_9pdev->rings[i].bh != NULL) {
370 qemu_bh_delete(xen_9pdev->rings[i].bh);
374 g_free(xen_9pdev->id);
375 g_free(xen_9pdev->tag);
376 g_free(xen_9pdev->path);
377 g_free(xen_9pdev->security_model);
378 g_free(xen_9pdev->rings);
382 static int xen_9pfs_connect(struct XenLegacyDevice *xendev)
386 Xen9pfsDev *xen_9pdev = container_of(xendev, Xen9pfsDev, xendev);
387 V9fsState *s = &xen_9pdev->state;
390 if (xenstore_read_fe_int(&xen_9pdev->xendev, "num-rings",
391 &xen_9pdev->num_rings) == -1 ||
392 xen_9pdev->num_rings > MAX_RINGS || xen_9pdev->num_rings < 1) {
396 xen_9pdev->rings = g_new0(Xen9pfsRing, xen_9pdev->num_rings);
397 for (i = 0; i < xen_9pdev->num_rings; i++) {
401 xen_9pdev->rings[i].priv = xen_9pdev;
402 xen_9pdev->rings[i].evtchn = -1;
403 xen_9pdev->rings[i].local_port = -1;
405 str = g_strdup_printf("ring-ref%u", i);
406 if (xenstore_read_fe_int(&xen_9pdev->xendev, str,
407 &xen_9pdev->rings[i].ref) == -1) {
412 str = g_strdup_printf("event-channel-%u", i);
413 if (xenstore_read_fe_int(&xen_9pdev->xendev, str,
414 &xen_9pdev->rings[i].evtchn) == -1) {
420 xen_9pdev->rings[i].intf =
421 xen_be_map_grant_ref(&xen_9pdev->xendev,
422 xen_9pdev->rings[i].ref,
423 PROT_READ | PROT_WRITE);
424 if (!xen_9pdev->rings[i].intf) {
427 ring_order = xen_9pdev->rings[i].intf->ring_order;
428 if (ring_order > MAX_RING_ORDER) {
431 xen_9pdev->rings[i].ring_order = ring_order;
432 xen_9pdev->rings[i].data =
433 xen_be_map_grant_refs(&xen_9pdev->xendev,
434 xen_9pdev->rings[i].intf->ref,
436 PROT_READ | PROT_WRITE);
437 if (!xen_9pdev->rings[i].data) {
440 xen_9pdev->rings[i].ring.in = xen_9pdev->rings[i].data;
441 xen_9pdev->rings[i].ring.out = xen_9pdev->rings[i].data +
442 XEN_FLEX_RING_SIZE(ring_order);
444 xen_9pdev->rings[i].bh = qemu_bh_new(xen_9pfs_bh, &xen_9pdev->rings[i]);
445 xen_9pdev->rings[i].out_cons = 0;
446 xen_9pdev->rings[i].out_size = 0;
447 xen_9pdev->rings[i].inprogress = false;
450 xen_9pdev->rings[i].evtchndev = xenevtchn_open(NULL, 0);
451 if (xen_9pdev->rings[i].evtchndev == NULL) {
454 qemu_set_cloexec(xenevtchn_fd(xen_9pdev->rings[i].evtchndev));
455 xen_9pdev->rings[i].local_port = xenevtchn_bind_interdomain
456 (xen_9pdev->rings[i].evtchndev,
458 xen_9pdev->rings[i].evtchn);
459 if (xen_9pdev->rings[i].local_port == -1) {
460 xen_pv_printf(xendev, 0,
461 "xenevtchn_bind_interdomain failed port=%d\n",
462 xen_9pdev->rings[i].evtchn);
465 xen_pv_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
466 qemu_set_fd_handler(xenevtchn_fd(xen_9pdev->rings[i].evtchndev),
467 xen_9pfs_evtchn_event, NULL, &xen_9pdev->rings[i]);
470 xen_9pdev->security_model = xenstore_read_be_str(xendev, "security_model");
471 xen_9pdev->path = xenstore_read_be_str(xendev, "path");
472 xen_9pdev->id = s->fsconf.fsdev_id =
473 g_strdup_printf("xen9p%d", xendev->dev);
474 xen_9pdev->tag = s->fsconf.tag = xenstore_read_fe_str(xendev, "tag");
475 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
478 qemu_opt_set(fsdev, "fsdriver", "local", NULL);
479 qemu_opt_set(fsdev, "path", xen_9pdev->path, NULL);
480 qemu_opt_set(fsdev, "security_model", xen_9pdev->security_model, NULL);
481 qemu_opts_set_id(fsdev, s->fsconf.fsdev_id);
482 qemu_fsdev_add(fsdev, &err);
484 error_report_err(err);
486 v9fs_device_realize_common(s, &xen_9p_transport, NULL);
491 xen_9pfs_free(xendev);
495 static void xen_9pfs_alloc(struct XenLegacyDevice *xendev)
497 xenstore_write_be_str(xendev, "versions", VERSIONS);
498 xenstore_write_be_int(xendev, "max-rings", MAX_RINGS);
499 xenstore_write_be_int(xendev, "max-ring-page-order", MAX_RING_ORDER);
502 struct XenDevOps xen_9pfs_ops = {
503 .size = sizeof(Xen9pfsDev),
504 .flags = DEVOPS_FLAG_NEED_GNTDEV,
505 .alloc = xen_9pfs_alloc,
506 .init = xen_9pfs_init,
507 .initialise = xen_9pfs_connect,
508 .disconnect = xen_9pfs_disconnect,
509 .free = xen_9pfs_free,