xref: /qemu/hw/9pfs/xen-9p-backend.c (revision ab930e80)
1 /*
2  * Xen 9p backend
3  *
4  * Copyright Aporeto 2017
5  *
6  * Authors:
7  *  Stefano Stabellini <stefano@aporeto.com>
8  *
9  */
10 
11 /*
12  * Not so fast! You might want to read the 9p developer docs first:
13  * https://wiki.qemu.org/Documentation/9p
14  */
15 
16 #include "qemu/osdep.h"
17 
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 "qemu/iov.h"
26 #include "fsdev/qemu-fsdev.h"
27 
28 #define VERSIONS "1"
29 #define MAX_RINGS 8
30 #define MAX_RING_ORDER 9
31 
32 typedef struct Xen9pfsRing {
33     struct Xen9pfsDev *priv;
34 
35     int ref;
36     xenevtchn_handle   *evtchndev;
37     int evtchn;
38     int local_port;
39     int ring_order;
40     struct xen_9pfs_data_intf *intf;
41     unsigned char *data;
42     struct xen_9pfs_data ring;
43 
44     struct iovec *sg;
45     QEMUBH *bh;
46     Coroutine *co;
47 
48     /* local copies, so that we can read/write PDU data directly from
49      * the ring */
50     RING_IDX out_cons, out_size, in_cons;
51     bool inprogress;
52 } Xen9pfsRing;
53 
54 typedef struct Xen9pfsDev {
55     struct XenLegacyDevice xendev;  /* must be first */
56     V9fsState state;
57     char *path;
58     char *security_model;
59     char *tag;
60     char *id;
61 
62     int num_rings;
63     Xen9pfsRing *rings;
64     MemReentrancyGuard mem_reentrancy_guard;
65 } Xen9pfsDev;
66 
67 static void xen_9pfs_disconnect(struct XenLegacyDevice *xendev);
68 
69 static void xen_9pfs_in_sg(Xen9pfsRing *ring,
70                            struct iovec *in_sg,
71                            int *num,
72                            uint32_t idx,
73                            uint32_t size)
74 {
75     RING_IDX cons, prod, masked_prod, masked_cons;
76 
77     cons = ring->intf->in_cons;
78     prod = ring->intf->in_prod;
79     xen_rmb();
80     masked_prod = xen_9pfs_mask(prod, XEN_FLEX_RING_SIZE(ring->ring_order));
81     masked_cons = xen_9pfs_mask(cons, XEN_FLEX_RING_SIZE(ring->ring_order));
82 
83     if (masked_prod < masked_cons) {
84         in_sg[0].iov_base = ring->ring.in + masked_prod;
85         in_sg[0].iov_len = masked_cons - masked_prod;
86         *num = 1;
87     } else {
88         in_sg[0].iov_base = ring->ring.in + masked_prod;
89         in_sg[0].iov_len = XEN_FLEX_RING_SIZE(ring->ring_order) - masked_prod;
90         in_sg[1].iov_base = ring->ring.in;
91         in_sg[1].iov_len = masked_cons;
92         *num = 2;
93     }
94 }
95 
96 static void xen_9pfs_out_sg(Xen9pfsRing *ring,
97                             struct iovec *out_sg,
98                             int *num,
99                             uint32_t idx)
100 {
101     RING_IDX cons, prod, masked_prod, masked_cons;
102 
103     cons = ring->intf->out_cons;
104     prod = ring->intf->out_prod;
105     xen_rmb();
106     masked_prod = xen_9pfs_mask(prod, XEN_FLEX_RING_SIZE(ring->ring_order));
107     masked_cons = xen_9pfs_mask(cons, XEN_FLEX_RING_SIZE(ring->ring_order));
108 
109     if (masked_cons < masked_prod) {
110         out_sg[0].iov_base = ring->ring.out + masked_cons;
111         out_sg[0].iov_len = ring->out_size;
112         *num = 1;
113     } else {
114         if (ring->out_size >
115             (XEN_FLEX_RING_SIZE(ring->ring_order) - masked_cons)) {
116             out_sg[0].iov_base = ring->ring.out + masked_cons;
117             out_sg[0].iov_len = XEN_FLEX_RING_SIZE(ring->ring_order) -
118                                 masked_cons;
119             out_sg[1].iov_base = ring->ring.out;
120             out_sg[1].iov_len = ring->out_size -
121                                 (XEN_FLEX_RING_SIZE(ring->ring_order) -
122                                  masked_cons);
123             *num = 2;
124         } else {
125             out_sg[0].iov_base = ring->ring.out + masked_cons;
126             out_sg[0].iov_len = ring->out_size;
127             *num = 1;
128         }
129     }
130 }
131 
132 static ssize_t xen_9pfs_pdu_vmarshal(V9fsPDU *pdu,
133                                      size_t offset,
134                                      const char *fmt,
135                                      va_list ap)
136 {
137     Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
138     struct iovec in_sg[2];
139     int num;
140     ssize_t ret;
141 
142     xen_9pfs_in_sg(&xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings],
143                    in_sg, &num, pdu->idx, ROUND_UP(offset + 128, 512));
144 
145     ret = v9fs_iov_vmarshal(in_sg, num, offset, 0, fmt, ap);
146     if (ret < 0) {
147         xen_pv_printf(&xen_9pfs->xendev, 0,
148                       "Failed to encode VirtFS reply type %d\n",
149                       pdu->id + 1);
150         xen_be_set_state(&xen_9pfs->xendev, XenbusStateClosing);
151         xen_9pfs_disconnect(&xen_9pfs->xendev);
152     }
153     return ret;
154 }
155 
156 static ssize_t xen_9pfs_pdu_vunmarshal(V9fsPDU *pdu,
157                                        size_t offset,
158                                        const char *fmt,
159                                        va_list ap)
160 {
161     Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
162     struct iovec out_sg[2];
163     int num;
164     ssize_t ret;
165 
166     xen_9pfs_out_sg(&xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings],
167                     out_sg, &num, pdu->idx);
168 
169     ret = v9fs_iov_vunmarshal(out_sg, num, offset, 0, fmt, ap);
170     if (ret < 0) {
171         xen_pv_printf(&xen_9pfs->xendev, 0,
172                       "Failed to decode VirtFS request type %d\n", pdu->id);
173         xen_be_set_state(&xen_9pfs->xendev, XenbusStateClosing);
174         xen_9pfs_disconnect(&xen_9pfs->xendev);
175     }
176     return ret;
177 }
178 
179 static void xen_9pfs_init_out_iov_from_pdu(V9fsPDU *pdu,
180                                            struct iovec **piov,
181                                            unsigned int *pniov,
182                                            size_t size)
183 {
184     Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
185     Xen9pfsRing *ring = &xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings];
186     int num;
187 
188     g_free(ring->sg);
189 
190     ring->sg = g_new0(struct iovec, 2);
191     xen_9pfs_out_sg(ring, ring->sg, &num, pdu->idx);
192     *piov = ring->sg;
193     *pniov = num;
194 }
195 
196 static void xen_9pfs_init_in_iov_from_pdu(V9fsPDU *pdu,
197                                           struct iovec **piov,
198                                           unsigned int *pniov,
199                                           size_t size)
200 {
201     Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
202     Xen9pfsRing *ring = &xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings];
203     int num;
204     size_t buf_size;
205 
206     g_free(ring->sg);
207 
208     ring->sg = g_new0(struct iovec, 2);
209     ring->co = qemu_coroutine_self();
210     /* make sure other threads see ring->co changes before continuing */
211     smp_wmb();
212 
213 again:
214     xen_9pfs_in_sg(ring, ring->sg, &num, pdu->idx, size);
215     buf_size = iov_size(ring->sg, num);
216     if (buf_size  < size) {
217         qemu_coroutine_yield();
218         goto again;
219     }
220     ring->co = NULL;
221     /* make sure other threads see ring->co changes before continuing */
222     smp_wmb();
223 
224     *piov = ring->sg;
225     *pniov = num;
226 }
227 
228 static void xen_9pfs_push_and_notify(V9fsPDU *pdu)
229 {
230     RING_IDX prod;
231     Xen9pfsDev *priv = container_of(pdu->s, Xen9pfsDev, state);
232     Xen9pfsRing *ring = &priv->rings[pdu->tag % priv->num_rings];
233 
234     g_free(ring->sg);
235     ring->sg = NULL;
236 
237     ring->intf->out_cons = ring->out_cons;
238     xen_wmb();
239 
240     prod = ring->intf->in_prod;
241     xen_rmb();
242     ring->intf->in_prod = prod + pdu->size;
243     xen_wmb();
244 
245     ring->inprogress = false;
246     qemu_xen_evtchn_notify(ring->evtchndev, ring->local_port);
247 
248     qemu_bh_schedule(ring->bh);
249 }
250 
251 static const V9fsTransport xen_9p_transport = {
252     .pdu_vmarshal = xen_9pfs_pdu_vmarshal,
253     .pdu_vunmarshal = xen_9pfs_pdu_vunmarshal,
254     .init_in_iov_from_pdu = xen_9pfs_init_in_iov_from_pdu,
255     .init_out_iov_from_pdu = xen_9pfs_init_out_iov_from_pdu,
256     .push_and_notify = xen_9pfs_push_and_notify,
257 };
258 
259 static int xen_9pfs_init(struct XenLegacyDevice *xendev)
260 {
261     return 0;
262 }
263 
264 static int xen_9pfs_receive(Xen9pfsRing *ring)
265 {
266     P9MsgHeader h;
267     RING_IDX cons, prod, masked_prod, masked_cons, queued;
268     V9fsPDU *pdu;
269 
270     if (ring->inprogress) {
271         return 0;
272     }
273 
274     cons = ring->intf->out_cons;
275     prod = ring->intf->out_prod;
276     xen_rmb();
277 
278     queued = xen_9pfs_queued(prod, cons, XEN_FLEX_RING_SIZE(ring->ring_order));
279     if (queued < sizeof(h)) {
280         return 0;
281     }
282     ring->inprogress = true;
283 
284     masked_prod = xen_9pfs_mask(prod, XEN_FLEX_RING_SIZE(ring->ring_order));
285     masked_cons = xen_9pfs_mask(cons, XEN_FLEX_RING_SIZE(ring->ring_order));
286 
287     xen_9pfs_read_packet((uint8_t *) &h, ring->ring.out, sizeof(h),
288                          masked_prod, &masked_cons,
289                          XEN_FLEX_RING_SIZE(ring->ring_order));
290     if (queued < le32_to_cpu(h.size_le)) {
291         return 0;
292     }
293 
294     /* cannot fail, because we only handle one request per ring at a time */
295     pdu = pdu_alloc(&ring->priv->state);
296     ring->out_size = le32_to_cpu(h.size_le);
297     ring->out_cons = cons + le32_to_cpu(h.size_le);
298 
299     pdu_submit(pdu, &h);
300 
301     return 0;
302 }
303 
304 static void xen_9pfs_bh(void *opaque)
305 {
306     Xen9pfsRing *ring = opaque;
307     bool wait;
308 
309 again:
310     wait = ring->co != NULL && qemu_coroutine_entered(ring->co);
311     /* paired with the smb_wmb barriers in xen_9pfs_init_in_iov_from_pdu */
312     smp_rmb();
313     if (wait) {
314         cpu_relax();
315         goto again;
316     }
317 
318     if (ring->co != NULL) {
319         qemu_coroutine_enter_if_inactive(ring->co);
320     }
321     xen_9pfs_receive(ring);
322 }
323 
324 static void xen_9pfs_evtchn_event(void *opaque)
325 {
326     Xen9pfsRing *ring = opaque;
327     evtchn_port_t port;
328 
329     port = qemu_xen_evtchn_pending(ring->evtchndev);
330     qemu_xen_evtchn_unmask(ring->evtchndev, port);
331 
332     qemu_bh_schedule(ring->bh);
333 }
334 
335 static void xen_9pfs_disconnect(struct XenLegacyDevice *xendev)
336 {
337     Xen9pfsDev *xen_9pdev = container_of(xendev, Xen9pfsDev, xendev);
338     int i;
339 
340     for (i = 0; i < xen_9pdev->num_rings; i++) {
341         if (xen_9pdev->rings[i].evtchndev != NULL) {
342             qemu_set_fd_handler(qemu_xen_evtchn_fd(xen_9pdev->rings[i].evtchndev),
343                                 NULL, NULL, NULL);
344             qemu_xen_evtchn_unbind(xen_9pdev->rings[i].evtchndev,
345                                    xen_9pdev->rings[i].local_port);
346             xen_9pdev->rings[i].evtchndev = NULL;
347         }
348     }
349 }
350 
351 static int xen_9pfs_free(struct XenLegacyDevice *xendev)
352 {
353     Xen9pfsDev *xen_9pdev = container_of(xendev, Xen9pfsDev, xendev);
354     int i;
355 
356     if (xen_9pdev->rings[0].evtchndev != NULL) {
357         xen_9pfs_disconnect(xendev);
358     }
359 
360     for (i = 0; i < xen_9pdev->num_rings; i++) {
361         if (xen_9pdev->rings[i].data != NULL) {
362             xen_be_unmap_grant_refs(&xen_9pdev->xendev,
363                                     xen_9pdev->rings[i].data,
364                                     xen_9pdev->rings[i].intf->ref,
365                                     (1 << xen_9pdev->rings[i].ring_order));
366         }
367         if (xen_9pdev->rings[i].intf != NULL) {
368             xen_be_unmap_grant_ref(&xen_9pdev->xendev,
369                                    xen_9pdev->rings[i].intf,
370                                    xen_9pdev->rings[i].ref);
371         }
372         if (xen_9pdev->rings[i].bh != NULL) {
373             qemu_bh_delete(xen_9pdev->rings[i].bh);
374         }
375     }
376 
377     g_free(xen_9pdev->id);
378     g_free(xen_9pdev->tag);
379     g_free(xen_9pdev->path);
380     g_free(xen_9pdev->security_model);
381     g_free(xen_9pdev->rings);
382     return 0;
383 }
384 
385 static int xen_9pfs_connect(struct XenLegacyDevice *xendev)
386 {
387     Error *err = NULL;
388     int i;
389     Xen9pfsDev *xen_9pdev = container_of(xendev, Xen9pfsDev, xendev);
390     V9fsState *s = &xen_9pdev->state;
391     QemuOpts *fsdev;
392 
393     if (xenstore_read_fe_int(&xen_9pdev->xendev, "num-rings",
394                              &xen_9pdev->num_rings) == -1 ||
395         xen_9pdev->num_rings > MAX_RINGS || xen_9pdev->num_rings < 1) {
396         return -1;
397     }
398 
399     xen_9pdev->rings = g_new0(Xen9pfsRing, xen_9pdev->num_rings);
400     for (i = 0; i < xen_9pdev->num_rings; i++) {
401         char *str;
402         int ring_order;
403 
404         xen_9pdev->rings[i].priv = xen_9pdev;
405         xen_9pdev->rings[i].evtchn = -1;
406         xen_9pdev->rings[i].local_port = -1;
407 
408         str = g_strdup_printf("ring-ref%u", i);
409         if (xenstore_read_fe_int(&xen_9pdev->xendev, str,
410                                  &xen_9pdev->rings[i].ref) == -1) {
411             g_free(str);
412             goto out;
413         }
414         g_free(str);
415         str = g_strdup_printf("event-channel-%u", i);
416         if (xenstore_read_fe_int(&xen_9pdev->xendev, str,
417                                  &xen_9pdev->rings[i].evtchn) == -1) {
418             g_free(str);
419             goto out;
420         }
421         g_free(str);
422 
423         xen_9pdev->rings[i].intf =
424             xen_be_map_grant_ref(&xen_9pdev->xendev,
425                                  xen_9pdev->rings[i].ref,
426                                  PROT_READ | PROT_WRITE);
427         if (!xen_9pdev->rings[i].intf) {
428             goto out;
429         }
430         ring_order = xen_9pdev->rings[i].intf->ring_order;
431         if (ring_order > MAX_RING_ORDER) {
432             goto out;
433         }
434         xen_9pdev->rings[i].ring_order = ring_order;
435         xen_9pdev->rings[i].data =
436             xen_be_map_grant_refs(&xen_9pdev->xendev,
437                                   xen_9pdev->rings[i].intf->ref,
438                                   (1 << ring_order),
439                                   PROT_READ | PROT_WRITE);
440         if (!xen_9pdev->rings[i].data) {
441             goto out;
442         }
443         xen_9pdev->rings[i].ring.in = xen_9pdev->rings[i].data;
444         xen_9pdev->rings[i].ring.out = xen_9pdev->rings[i].data +
445                                        XEN_FLEX_RING_SIZE(ring_order);
446 
447         xen_9pdev->rings[i].bh = qemu_bh_new_guarded(xen_9pfs_bh,
448                                                      &xen_9pdev->rings[i],
449                                                      &xen_9pdev->mem_reentrancy_guard);
450         xen_9pdev->rings[i].out_cons = 0;
451         xen_9pdev->rings[i].out_size = 0;
452         xen_9pdev->rings[i].inprogress = false;
453 
454 
455         xen_9pdev->rings[i].evtchndev = qemu_xen_evtchn_open();
456         if (xen_9pdev->rings[i].evtchndev == NULL) {
457             goto out;
458         }
459         qemu_set_cloexec(qemu_xen_evtchn_fd(xen_9pdev->rings[i].evtchndev));
460         xen_9pdev->rings[i].local_port = qemu_xen_evtchn_bind_interdomain
461                                             (xen_9pdev->rings[i].evtchndev,
462                                              xendev->dom,
463                                              xen_9pdev->rings[i].evtchn);
464         if (xen_9pdev->rings[i].local_port == -1) {
465             xen_pv_printf(xendev, 0,
466                           "xenevtchn_bind_interdomain failed port=%d\n",
467                           xen_9pdev->rings[i].evtchn);
468             goto out;
469         }
470         xen_pv_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
471         qemu_set_fd_handler(qemu_xen_evtchn_fd(xen_9pdev->rings[i].evtchndev),
472                             xen_9pfs_evtchn_event, NULL, &xen_9pdev->rings[i]);
473     }
474 
475     xen_9pdev->security_model = xenstore_read_be_str(xendev, "security_model");
476     xen_9pdev->path = xenstore_read_be_str(xendev, "path");
477     xen_9pdev->id = s->fsconf.fsdev_id =
478         g_strdup_printf("xen9p%d", xendev->dev);
479     xen_9pdev->tag = s->fsconf.tag = xenstore_read_fe_str(xendev, "tag");
480     fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
481             s->fsconf.tag,
482             1, NULL);
483     qemu_opt_set(fsdev, "fsdriver", "local", NULL);
484     qemu_opt_set(fsdev, "path", xen_9pdev->path, NULL);
485     qemu_opt_set(fsdev, "security_model", xen_9pdev->security_model, NULL);
486     qemu_opts_set_id(fsdev, s->fsconf.fsdev_id);
487     qemu_fsdev_add(fsdev, &err);
488     if (err) {
489         error_report_err(err);
490     }
491     v9fs_device_realize_common(s, &xen_9p_transport, NULL);
492 
493     return 0;
494 
495 out:
496     xen_9pfs_free(xendev);
497     return -1;
498 }
499 
500 static void xen_9pfs_alloc(struct XenLegacyDevice *xendev)
501 {
502     xenstore_write_be_str(xendev, "versions", VERSIONS);
503     xenstore_write_be_int(xendev, "max-rings", MAX_RINGS);
504     xenstore_write_be_int(xendev, "max-ring-page-order", MAX_RING_ORDER);
505 }
506 
507 struct XenDevOps xen_9pfs_ops = {
508     .size       = sizeof(Xen9pfsDev),
509     .flags      = DEVOPS_FLAG_NEED_GNTDEV,
510     .alloc      = xen_9pfs_alloc,
511     .init       = xen_9pfs_init,
512     .initialise = xen_9pfs_connect,
513     .disconnect = xen_9pfs_disconnect,
514     .free       = xen_9pfs_free,
515 };
516