4 * Copyright Aporeto 2017
7 * Stefano Stabellini <stefano@aporeto.com>
11 #include "qemu/osdep.h"
14 #include "hw/9pfs/9p.h"
15 #include "hw/xen/xen-legacy-backend.h"
16 #include "hw/9pfs/xen-9pfs.h"
17 #include "qapi/error.h"
18 #include "qemu/config-file.h"
19 #include "qemu/option.h"
20 #include "fsdev/qemu-fsdev.h"
24 #define MAX_RING_ORDER 8
26 typedef struct Xen9pfsRing
{
27 struct Xen9pfsDev
*priv
;
30 xenevtchn_handle
*evtchndev
;
34 struct xen_9pfs_data_intf
*intf
;
36 struct xen_9pfs_data ring
;
41 /* local copies, so that we can read/write PDU data directly from
43 RING_IDX out_cons
, out_size
, in_cons
;
47 typedef struct Xen9pfsDev
{
48 struct XenLegacyDevice xendev
; /* must be first */
59 static void xen_9pfs_disconnect(struct XenLegacyDevice
*xendev
);
61 static void xen_9pfs_in_sg(Xen9pfsRing
*ring
,
67 RING_IDX cons
, prod
, masked_prod
, masked_cons
;
69 cons
= ring
->intf
->in_cons
;
70 prod
= ring
->intf
->in_prod
;
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
));
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
;
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
;
88 static void xen_9pfs_out_sg(Xen9pfsRing
*ring
,
93 RING_IDX cons
, prod
, masked_prod
, masked_cons
;
95 cons
= ring
->intf
->out_cons
;
96 prod
= ring
->intf
->out_prod
;
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
));
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
;
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
) -
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
) -
117 out_sg
[0].iov_base
= ring
->ring
.out
+ masked_cons
;
118 out_sg
[0].iov_len
= ring
->out_size
;
124 static ssize_t
xen_9pfs_pdu_vmarshal(V9fsPDU
*pdu
,
129 Xen9pfsDev
*xen_9pfs
= container_of(pdu
->s
, Xen9pfsDev
, state
);
130 struct iovec in_sg
[2];
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));
137 ret
= v9fs_iov_vmarshal(in_sg
, num
, offset
, 0, fmt
, ap
);
139 xen_pv_printf(&xen_9pfs
->xendev
, 0,
140 "Failed to encode VirtFS request type %d\n", pdu
->id
+ 1);
141 xen_be_set_state(&xen_9pfs
->xendev
, XenbusStateClosing
);
142 xen_9pfs_disconnect(&xen_9pfs
->xendev
);
147 static ssize_t
xen_9pfs_pdu_vunmarshal(V9fsPDU
*pdu
,
152 Xen9pfsDev
*xen_9pfs
= container_of(pdu
->s
, Xen9pfsDev
, state
);
153 struct iovec out_sg
[2];
157 xen_9pfs_out_sg(&xen_9pfs
->rings
[pdu
->tag
% xen_9pfs
->num_rings
],
158 out_sg
, &num
, pdu
->idx
);
160 ret
= v9fs_iov_vunmarshal(out_sg
, num
, offset
, 0, fmt
, ap
);
162 xen_pv_printf(&xen_9pfs
->xendev
, 0,
163 "Failed to decode VirtFS request type %d\n", pdu
->id
);
164 xen_be_set_state(&xen_9pfs
->xendev
, XenbusStateClosing
);
165 xen_9pfs_disconnect(&xen_9pfs
->xendev
);
170 static void xen_9pfs_init_out_iov_from_pdu(V9fsPDU
*pdu
,
175 Xen9pfsDev
*xen_9pfs
= container_of(pdu
->s
, Xen9pfsDev
, state
);
176 Xen9pfsRing
*ring
= &xen_9pfs
->rings
[pdu
->tag
% xen_9pfs
->num_rings
];
181 ring
->sg
= g_new0(struct iovec
, 2);
182 xen_9pfs_out_sg(ring
, ring
->sg
, &num
, pdu
->idx
);
187 static void xen_9pfs_init_in_iov_from_pdu(V9fsPDU
*pdu
,
192 Xen9pfsDev
*xen_9pfs
= container_of(pdu
->s
, Xen9pfsDev
, state
);
193 Xen9pfsRing
*ring
= &xen_9pfs
->rings
[pdu
->tag
% xen_9pfs
->num_rings
];
199 ring
->sg
= g_new0(struct iovec
, 2);
200 xen_9pfs_in_sg(ring
, ring
->sg
, &num
, pdu
->idx
, size
);
202 buf_size
= iov_size(ring
->sg
, num
);
203 if (buf_size
< size
) {
204 xen_pv_printf(&xen_9pfs
->xendev
, 0, "Xen 9pfs request type %d"
205 "needs %zu bytes, buffer has %zu\n", pdu
->id
, size
,
207 xen_be_set_state(&xen_9pfs
->xendev
, XenbusStateClosing
);
208 xen_9pfs_disconnect(&xen_9pfs
->xendev
);
215 static void xen_9pfs_push_and_notify(V9fsPDU
*pdu
)
218 Xen9pfsDev
*priv
= container_of(pdu
->s
, Xen9pfsDev
, state
);
219 Xen9pfsRing
*ring
= &priv
->rings
[pdu
->tag
% priv
->num_rings
];
224 ring
->intf
->out_cons
= ring
->out_cons
;
227 prod
= ring
->intf
->in_prod
;
229 ring
->intf
->in_prod
= prod
+ pdu
->size
;
232 ring
->inprogress
= false;
233 xenevtchn_notify(ring
->evtchndev
, ring
->local_port
);
235 qemu_bh_schedule(ring
->bh
);
238 static const V9fsTransport xen_9p_transport
= {
239 .pdu_vmarshal
= xen_9pfs_pdu_vmarshal
,
240 .pdu_vunmarshal
= xen_9pfs_pdu_vunmarshal
,
241 .init_in_iov_from_pdu
= xen_9pfs_init_in_iov_from_pdu
,
242 .init_out_iov_from_pdu
= xen_9pfs_init_out_iov_from_pdu
,
243 .push_and_notify
= xen_9pfs_push_and_notify
,
246 static int xen_9pfs_init(struct XenLegacyDevice
*xendev
)
251 static int xen_9pfs_receive(Xen9pfsRing
*ring
)
254 RING_IDX cons
, prod
, masked_prod
, masked_cons
, queued
;
257 if (ring
->inprogress
) {
261 cons
= ring
->intf
->out_cons
;
262 prod
= ring
->intf
->out_prod
;
265 queued
= xen_9pfs_queued(prod
, cons
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
266 if (queued
< sizeof(h
)) {
269 ring
->inprogress
= true;
271 masked_prod
= xen_9pfs_mask(prod
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
272 masked_cons
= xen_9pfs_mask(cons
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
274 xen_9pfs_read_packet((uint8_t *) &h
, ring
->ring
.out
, sizeof(h
),
275 masked_prod
, &masked_cons
,
276 XEN_FLEX_RING_SIZE(ring
->ring_order
));
277 if (queued
< le32_to_cpu(h
.size_le
)) {
281 /* cannot fail, because we only handle one request per ring at a time */
282 pdu
= pdu_alloc(&ring
->priv
->state
);
283 ring
->out_size
= le32_to_cpu(h
.size_le
);
284 ring
->out_cons
= cons
+ le32_to_cpu(h
.size_le
);
291 static void xen_9pfs_bh(void *opaque
)
293 Xen9pfsRing
*ring
= opaque
;
294 xen_9pfs_receive(ring
);
297 static void xen_9pfs_evtchn_event(void *opaque
)
299 Xen9pfsRing
*ring
= opaque
;
302 port
= xenevtchn_pending(ring
->evtchndev
);
303 xenevtchn_unmask(ring
->evtchndev
, port
);
305 qemu_bh_schedule(ring
->bh
);
308 static void xen_9pfs_disconnect(struct XenLegacyDevice
*xendev
)
310 Xen9pfsDev
*xen_9pdev
= container_of(xendev
, Xen9pfsDev
, xendev
);
313 for (i
= 0; i
< xen_9pdev
->num_rings
; i
++) {
314 if (xen_9pdev
->rings
[i
].evtchndev
!= NULL
) {
315 qemu_set_fd_handler(xenevtchn_fd(xen_9pdev
->rings
[i
].evtchndev
),
317 xenevtchn_unbind(xen_9pdev
->rings
[i
].evtchndev
,
318 xen_9pdev
->rings
[i
].local_port
);
319 xen_9pdev
->rings
[i
].evtchndev
= NULL
;
324 static int xen_9pfs_free(struct XenLegacyDevice
*xendev
)
326 Xen9pfsDev
*xen_9pdev
= container_of(xendev
, Xen9pfsDev
, xendev
);
329 if (xen_9pdev
->rings
[0].evtchndev
!= NULL
) {
330 xen_9pfs_disconnect(xendev
);
333 for (i
= 0; i
< xen_9pdev
->num_rings
; i
++) {
334 if (xen_9pdev
->rings
[i
].data
!= NULL
) {
335 xen_be_unmap_grant_refs(&xen_9pdev
->xendev
,
336 xen_9pdev
->rings
[i
].data
,
337 (1 << xen_9pdev
->rings
[i
].ring_order
));
339 if (xen_9pdev
->rings
[i
].intf
!= NULL
) {
340 xen_be_unmap_grant_refs(&xen_9pdev
->xendev
,
341 xen_9pdev
->rings
[i
].intf
,
344 if (xen_9pdev
->rings
[i
].bh
!= NULL
) {
345 qemu_bh_delete(xen_9pdev
->rings
[i
].bh
);
349 g_free(xen_9pdev
->id
);
350 g_free(xen_9pdev
->tag
);
351 g_free(xen_9pdev
->path
);
352 g_free(xen_9pdev
->security_model
);
353 g_free(xen_9pdev
->rings
);
357 static int xen_9pfs_connect(struct XenLegacyDevice
*xendev
)
361 Xen9pfsDev
*xen_9pdev
= container_of(xendev
, Xen9pfsDev
, xendev
);
362 V9fsState
*s
= &xen_9pdev
->state
;
365 if (xenstore_read_fe_int(&xen_9pdev
->xendev
, "num-rings",
366 &xen_9pdev
->num_rings
) == -1 ||
367 xen_9pdev
->num_rings
> MAX_RINGS
|| xen_9pdev
->num_rings
< 1) {
371 xen_9pdev
->rings
= g_new0(Xen9pfsRing
, xen_9pdev
->num_rings
);
372 for (i
= 0; i
< xen_9pdev
->num_rings
; i
++) {
376 xen_9pdev
->rings
[i
].priv
= xen_9pdev
;
377 xen_9pdev
->rings
[i
].evtchn
= -1;
378 xen_9pdev
->rings
[i
].local_port
= -1;
380 str
= g_strdup_printf("ring-ref%u", i
);
381 if (xenstore_read_fe_int(&xen_9pdev
->xendev
, str
,
382 &xen_9pdev
->rings
[i
].ref
) == -1) {
387 str
= g_strdup_printf("event-channel-%u", i
);
388 if (xenstore_read_fe_int(&xen_9pdev
->xendev
, str
,
389 &xen_9pdev
->rings
[i
].evtchn
) == -1) {
395 xen_9pdev
->rings
[i
].intf
=
396 xen_be_map_grant_ref(&xen_9pdev
->xendev
,
397 xen_9pdev
->rings
[i
].ref
,
398 PROT_READ
| PROT_WRITE
);
399 if (!xen_9pdev
->rings
[i
].intf
) {
402 ring_order
= xen_9pdev
->rings
[i
].intf
->ring_order
;
403 if (ring_order
> MAX_RING_ORDER
) {
406 xen_9pdev
->rings
[i
].ring_order
= ring_order
;
407 xen_9pdev
->rings
[i
].data
=
408 xen_be_map_grant_refs(&xen_9pdev
->xendev
,
409 xen_9pdev
->rings
[i
].intf
->ref
,
411 PROT_READ
| PROT_WRITE
);
412 if (!xen_9pdev
->rings
[i
].data
) {
415 xen_9pdev
->rings
[i
].ring
.in
= xen_9pdev
->rings
[i
].data
;
416 xen_9pdev
->rings
[i
].ring
.out
= xen_9pdev
->rings
[i
].data
+
417 XEN_FLEX_RING_SIZE(ring_order
);
419 xen_9pdev
->rings
[i
].bh
= qemu_bh_new(xen_9pfs_bh
, &xen_9pdev
->rings
[i
]);
420 xen_9pdev
->rings
[i
].out_cons
= 0;
421 xen_9pdev
->rings
[i
].out_size
= 0;
422 xen_9pdev
->rings
[i
].inprogress
= false;
425 xen_9pdev
->rings
[i
].evtchndev
= xenevtchn_open(NULL
, 0);
426 if (xen_9pdev
->rings
[i
].evtchndev
== NULL
) {
429 qemu_set_cloexec(xenevtchn_fd(xen_9pdev
->rings
[i
].evtchndev
));
430 xen_9pdev
->rings
[i
].local_port
= xenevtchn_bind_interdomain
431 (xen_9pdev
->rings
[i
].evtchndev
,
433 xen_9pdev
->rings
[i
].evtchn
);
434 if (xen_9pdev
->rings
[i
].local_port
== -1) {
435 xen_pv_printf(xendev
, 0,
436 "xenevtchn_bind_interdomain failed port=%d\n",
437 xen_9pdev
->rings
[i
].evtchn
);
440 xen_pv_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
441 qemu_set_fd_handler(xenevtchn_fd(xen_9pdev
->rings
[i
].evtchndev
),
442 xen_9pfs_evtchn_event
, NULL
, &xen_9pdev
->rings
[i
]);
445 xen_9pdev
->security_model
= xenstore_read_be_str(xendev
, "security_model");
446 xen_9pdev
->path
= xenstore_read_be_str(xendev
, "path");
447 xen_9pdev
->id
= s
->fsconf
.fsdev_id
=
448 g_strdup_printf("xen9p%d", xendev
->dev
);
449 xen_9pdev
->tag
= s
->fsconf
.tag
= xenstore_read_fe_str(xendev
, "tag");
450 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
453 qemu_opt_set(fsdev
, "fsdriver", "local", NULL
);
454 qemu_opt_set(fsdev
, "path", xen_9pdev
->path
, NULL
);
455 qemu_opt_set(fsdev
, "security_model", xen_9pdev
->security_model
, NULL
);
456 qemu_opts_set_id(fsdev
, s
->fsconf
.fsdev_id
);
457 qemu_fsdev_add(fsdev
, &err
);
459 error_report_err(err
);
461 v9fs_device_realize_common(s
, &xen_9p_transport
, NULL
);
466 xen_9pfs_free(xendev
);
470 static void xen_9pfs_alloc(struct XenLegacyDevice
*xendev
)
472 xenstore_write_be_str(xendev
, "versions", VERSIONS
);
473 xenstore_write_be_int(xendev
, "max-rings", MAX_RINGS
);
474 xenstore_write_be_int(xendev
, "max-ring-page-order", MAX_RING_ORDER
);
477 struct XenDevOps xen_9pfs_ops
= {
478 .size
= sizeof(Xen9pfsDev
),
479 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
480 .alloc
= xen_9pfs_alloc
,
481 .init
= xen_9pfs_init
,
482 .initialise
= xen_9pfs_connect
,
483 .disconnect
= xen_9pfs_disconnect
,
484 .free
= xen_9pfs_free
,