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