xen/9pfs: implement in/out_iov_from_pdu and vmarshal/vunmarshal
[qemu.git] / hw / 9pfs / xen-9p-backend.c
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/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
20 #define VERSIONS "1"
21 #define MAX_RINGS 8
22 #define MAX_RING_ORDER 8
23
24 typedef 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
45 typedef struct Xen9pfsDev {
46 struct XenDevice xendev; /* must be first */
47 V9fsState state;
48 char *path;
49 char *security_model;
50 char *tag;
51 char *id;
52
53 int num_rings;
54 Xen9pfsRing *rings;
55 } Xen9pfsDev;
56
57 static 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
84 static 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
120 static ssize_t xen_9pfs_pdu_vmarshal(V9fsPDU *pdu,
121 size_t offset,
122 const char *fmt,
123 va_list ap)
124 {
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);
132 }
133
134 static ssize_t xen_9pfs_pdu_vunmarshal(V9fsPDU *pdu,
135 size_t offset,
136 const char *fmt,
137 va_list ap)
138 {
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);
146 }
147
148 static void xen_9pfs_init_out_iov_from_pdu(V9fsPDU *pdu,
149 struct iovec **piov,
150 unsigned int *pniov)
151 {
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;
162 }
163
164 static void xen_9pfs_init_in_iov_from_pdu(V9fsPDU *pdu,
165 struct iovec **piov,
166 unsigned int *pniov,
167 size_t size)
168 {
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;
179 }
180
181 static void xen_9pfs_push_and_notify(V9fsPDU *pdu)
182 {
183 }
184
185 static const struct V9fsTransport xen_9p_transport = {
186 .pdu_vmarshal = xen_9pfs_pdu_vmarshal,
187 .pdu_vunmarshal = xen_9pfs_pdu_vunmarshal,
188 .init_in_iov_from_pdu = xen_9pfs_init_in_iov_from_pdu,
189 .init_out_iov_from_pdu = xen_9pfs_init_out_iov_from_pdu,
190 .push_and_notify = xen_9pfs_push_and_notify,
191 };
192
193 static int xen_9pfs_init(struct XenDevice *xendev)
194 {
195 return 0;
196 }
197
198 static int xen_9pfs_receive(Xen9pfsRing *ring)
199 {
200 P9MsgHeader h;
201 RING_IDX cons, prod, masked_prod, masked_cons;
202 V9fsPDU *pdu;
203
204 if (ring->inprogress) {
205 return 0;
206 }
207
208 cons = ring->intf->out_cons;
209 prod = ring->intf->out_prod;
210 xen_rmb();
211
212 if (xen_9pfs_queued(prod, cons, XEN_FLEX_RING_SIZE(ring->ring_order)) <
213 sizeof(h)) {
214 return 0;
215 }
216 ring->inprogress = true;
217
218 masked_prod = xen_9pfs_mask(prod, XEN_FLEX_RING_SIZE(ring->ring_order));
219 masked_cons = xen_9pfs_mask(cons, XEN_FLEX_RING_SIZE(ring->ring_order));
220
221 xen_9pfs_read_packet((uint8_t *) &h, ring->ring.out, sizeof(h),
222 masked_prod, &masked_cons,
223 XEN_FLEX_RING_SIZE(ring->ring_order));
224
225 /* cannot fail, because we only handle one request per ring at a time */
226 pdu = pdu_alloc(&ring->priv->state);
227 pdu->size = le32_to_cpu(h.size_le);
228 pdu->id = h.id;
229 pdu->tag = le32_to_cpu(h.tag_le);
230 ring->out_size = le32_to_cpu(h.size_le);
231 ring->out_cons = cons + le32_to_cpu(h.size_le);
232
233 qemu_co_queue_init(&pdu->complete);
234 pdu_submit(pdu);
235
236 return 0;
237 }
238
239 static void xen_9pfs_bh(void *opaque)
240 {
241 Xen9pfsRing *ring = opaque;
242 xen_9pfs_receive(ring);
243 }
244
245 static void xen_9pfs_evtchn_event(void *opaque)
246 {
247 Xen9pfsRing *ring = opaque;
248 evtchn_port_t port;
249
250 port = xenevtchn_pending(ring->evtchndev);
251 xenevtchn_unmask(ring->evtchndev, port);
252
253 qemu_bh_schedule(ring->bh);
254 }
255
256 static int xen_9pfs_free(struct XenDevice *xendev)
257 {
258 int i;
259 Xen9pfsDev *xen_9pdev = container_of(xendev, Xen9pfsDev, xendev);
260
261 g_free(xen_9pdev->id);
262 g_free(xen_9pdev->tag);
263 g_free(xen_9pdev->path);
264 g_free(xen_9pdev->security_model);
265
266 for (i = 0; i < xen_9pdev->num_rings; i++) {
267 if (xen_9pdev->rings[i].data != NULL) {
268 xengnttab_unmap(xen_9pdev->xendev.gnttabdev,
269 xen_9pdev->rings[i].data,
270 (1 << xen_9pdev->rings[i].ring_order));
271 }
272 if (xen_9pdev->rings[i].intf != NULL) {
273 xengnttab_unmap(xen_9pdev->xendev.gnttabdev,
274 xen_9pdev->rings[i].intf,
275 1);
276 }
277 if (xen_9pdev->rings[i].evtchndev > 0) {
278 qemu_set_fd_handler(xenevtchn_fd(xen_9pdev->rings[i].evtchndev),
279 NULL, NULL, NULL);
280 xenevtchn_unbind(xen_9pdev->rings[i].evtchndev,
281 xen_9pdev->rings[i].local_port);
282 }
283 if (xen_9pdev->rings[i].bh != NULL) {
284 qemu_bh_delete(xen_9pdev->rings[i].bh);
285 }
286 }
287 g_free(xen_9pdev->rings);
288 return 0;
289 }
290
291 static int xen_9pfs_connect(struct XenDevice *xendev)
292 {
293 int i;
294 Xen9pfsDev *xen_9pdev = container_of(xendev, Xen9pfsDev, xendev);
295 V9fsState *s = &xen_9pdev->state;
296 QemuOpts *fsdev;
297
298 if (xenstore_read_fe_int(&xen_9pdev->xendev, "num-rings",
299 &xen_9pdev->num_rings) == -1 ||
300 xen_9pdev->num_rings > MAX_RINGS || xen_9pdev->num_rings < 1) {
301 return -1;
302 }
303
304 xen_9pdev->rings = g_malloc0(xen_9pdev->num_rings * sizeof(Xen9pfsRing));
305 for (i = 0; i < xen_9pdev->num_rings; i++) {
306 char *str;
307 int ring_order;
308
309 xen_9pdev->rings[i].priv = xen_9pdev;
310 xen_9pdev->rings[i].evtchn = -1;
311 xen_9pdev->rings[i].local_port = -1;
312
313 str = g_strdup_printf("ring-ref%u", i);
314 if (xenstore_read_fe_int(&xen_9pdev->xendev, str,
315 &xen_9pdev->rings[i].ref) == -1) {
316 goto out;
317 }
318 g_free(str);
319 str = g_strdup_printf("event-channel-%u", i);
320 if (xenstore_read_fe_int(&xen_9pdev->xendev, str,
321 &xen_9pdev->rings[i].evtchn) == -1) {
322 goto out;
323 }
324 g_free(str);
325
326 xen_9pdev->rings[i].intf = xengnttab_map_grant_ref(
327 xen_9pdev->xendev.gnttabdev,
328 xen_9pdev->xendev.dom,
329 xen_9pdev->rings[i].ref,
330 PROT_READ | PROT_WRITE);
331 if (!xen_9pdev->rings[i].intf) {
332 goto out;
333 }
334 ring_order = xen_9pdev->rings[i].intf->ring_order;
335 if (ring_order > MAX_RING_ORDER) {
336 goto out;
337 }
338 xen_9pdev->rings[i].ring_order = ring_order;
339 xen_9pdev->rings[i].data = xengnttab_map_domain_grant_refs(
340 xen_9pdev->xendev.gnttabdev,
341 (1 << ring_order),
342 xen_9pdev->xendev.dom,
343 xen_9pdev->rings[i].intf->ref,
344 PROT_READ | PROT_WRITE);
345 if (!xen_9pdev->rings[i].data) {
346 goto out;
347 }
348 xen_9pdev->rings[i].ring.in = xen_9pdev->rings[i].data;
349 xen_9pdev->rings[i].ring.out = xen_9pdev->rings[i].data +
350 XEN_FLEX_RING_SIZE(ring_order);
351
352 xen_9pdev->rings[i].bh = qemu_bh_new(xen_9pfs_bh, &xen_9pdev->rings[i]);
353 xen_9pdev->rings[i].out_cons = 0;
354 xen_9pdev->rings[i].out_size = 0;
355 xen_9pdev->rings[i].inprogress = false;
356
357
358 xen_9pdev->rings[i].evtchndev = xenevtchn_open(NULL, 0);
359 if (xen_9pdev->rings[i].evtchndev == NULL) {
360 goto out;
361 }
362 fcntl(xenevtchn_fd(xen_9pdev->rings[i].evtchndev), F_SETFD, FD_CLOEXEC);
363 xen_9pdev->rings[i].local_port = xenevtchn_bind_interdomain
364 (xen_9pdev->rings[i].evtchndev,
365 xendev->dom,
366 xen_9pdev->rings[i].evtchn);
367 if (xen_9pdev->rings[i].local_port == -1) {
368 xen_pv_printf(xendev, 0,
369 "xenevtchn_bind_interdomain failed port=%d\n",
370 xen_9pdev->rings[i].evtchn);
371 goto out;
372 }
373 xen_pv_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
374 qemu_set_fd_handler(xenevtchn_fd(xen_9pdev->rings[i].evtchndev),
375 xen_9pfs_evtchn_event, NULL, &xen_9pdev->rings[i]);
376 }
377
378 xen_9pdev->security_model = xenstore_read_be_str(xendev, "security_model");
379 xen_9pdev->path = xenstore_read_be_str(xendev, "path");
380 xen_9pdev->id = s->fsconf.fsdev_id =
381 g_strdup_printf("xen9p%d", xendev->dev);
382 xen_9pdev->tag = s->fsconf.tag = xenstore_read_fe_str(xendev, "tag");
383 v9fs_register_transport(s, &xen_9p_transport);
384 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
385 s->fsconf.tag,
386 1, NULL);
387 qemu_opt_set(fsdev, "fsdriver", "local", NULL);
388 qemu_opt_set(fsdev, "path", xen_9pdev->path, NULL);
389 qemu_opt_set(fsdev, "security_model", xen_9pdev->security_model, NULL);
390 qemu_opts_set_id(fsdev, s->fsconf.fsdev_id);
391 qemu_fsdev_add(fsdev);
392 v9fs_device_realize_common(s, NULL);
393
394 return 0;
395
396 out:
397 xen_9pfs_free(xendev);
398 return -1;
399 }
400
401 static void xen_9pfs_alloc(struct XenDevice *xendev)
402 {
403 xenstore_write_be_str(xendev, "versions", VERSIONS);
404 xenstore_write_be_int(xendev, "max-rings", MAX_RINGS);
405 xenstore_write_be_int(xendev, "max-ring-page-order", MAX_RING_ORDER);
406 }
407
408 static void xen_9pfs_disconnect(struct XenDevice *xendev)
409 {
410 /* Dynamic hotplug of PV filesystems at runtime is not supported. */
411 }
412
413 struct XenDevOps xen_9pfs_ops = {
414 .size = sizeof(Xen9pfsDev),
415 .flags = DEVOPS_FLAG_NEED_GNTDEV,
416 .alloc = xen_9pfs_alloc,
417 .init = xen_9pfs_init,
418 .initialise = xen_9pfs_connect,
419 .disconnect = xen_9pfs_disconnect,
420 .free = xen_9pfs_free,
421 };