4 * Copyright Aporeto 2017
7 * Stefano Stabellini <stefano@aporeto.com>
11 #include "qemu/osdep.h"
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"
24 #define MAX_RING_ORDER 9
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
;
42 /* local copies, so that we can read/write PDU data directly from
44 RING_IDX out_cons
, out_size
, in_cons
;
48 typedef struct Xen9pfsDev
{
49 struct XenLegacyDevice xendev
; /* must be first */
60 static void xen_9pfs_disconnect(struct XenLegacyDevice
*xendev
);
62 static void xen_9pfs_in_sg(Xen9pfsRing
*ring
,
68 RING_IDX cons
, prod
, masked_prod
, masked_cons
;
70 cons
= ring
->intf
->in_cons
;
71 prod
= ring
->intf
->in_prod
;
73 masked_prod
= xen_9pfs_mask(prod
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
74 masked_cons
= xen_9pfs_mask(cons
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
76 if (masked_prod
< masked_cons
) {
77 in_sg
[0].iov_base
= ring
->ring
.in
+ masked_prod
;
78 in_sg
[0].iov_len
= masked_cons
- masked_prod
;
81 in_sg
[0].iov_base
= ring
->ring
.in
+ masked_prod
;
82 in_sg
[0].iov_len
= XEN_FLEX_RING_SIZE(ring
->ring_order
) - masked_prod
;
83 in_sg
[1].iov_base
= ring
->ring
.in
;
84 in_sg
[1].iov_len
= masked_cons
;
89 static void xen_9pfs_out_sg(Xen9pfsRing
*ring
,
94 RING_IDX cons
, prod
, masked_prod
, masked_cons
;
96 cons
= ring
->intf
->out_cons
;
97 prod
= ring
->intf
->out_prod
;
99 masked_prod
= xen_9pfs_mask(prod
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
100 masked_cons
= xen_9pfs_mask(cons
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
102 if (masked_cons
< masked_prod
) {
103 out_sg
[0].iov_base
= ring
->ring
.out
+ masked_cons
;
104 out_sg
[0].iov_len
= ring
->out_size
;
108 (XEN_FLEX_RING_SIZE(ring
->ring_order
) - masked_cons
)) {
109 out_sg
[0].iov_base
= ring
->ring
.out
+ masked_cons
;
110 out_sg
[0].iov_len
= XEN_FLEX_RING_SIZE(ring
->ring_order
) -
112 out_sg
[1].iov_base
= ring
->ring
.out
;
113 out_sg
[1].iov_len
= ring
->out_size
-
114 (XEN_FLEX_RING_SIZE(ring
->ring_order
) -
118 out_sg
[0].iov_base
= ring
->ring
.out
+ masked_cons
;
119 out_sg
[0].iov_len
= ring
->out_size
;
125 static ssize_t
xen_9pfs_pdu_vmarshal(V9fsPDU
*pdu
,
130 Xen9pfsDev
*xen_9pfs
= container_of(pdu
->s
, Xen9pfsDev
, state
);
131 struct iovec in_sg
[2];
135 xen_9pfs_in_sg(&xen_9pfs
->rings
[pdu
->tag
% xen_9pfs
->num_rings
],
136 in_sg
, &num
, pdu
->idx
, ROUND_UP(offset
+ 128, 512));
138 ret
= v9fs_iov_vmarshal(in_sg
, num
, offset
, 0, fmt
, ap
);
140 xen_pv_printf(&xen_9pfs
->xendev
, 0,
141 "Failed to encode VirtFS reply type %d\n",
143 xen_be_set_state(&xen_9pfs
->xendev
, XenbusStateClosing
);
144 xen_9pfs_disconnect(&xen_9pfs
->xendev
);
149 static ssize_t
xen_9pfs_pdu_vunmarshal(V9fsPDU
*pdu
,
154 Xen9pfsDev
*xen_9pfs
= container_of(pdu
->s
, Xen9pfsDev
, state
);
155 struct iovec out_sg
[2];
159 xen_9pfs_out_sg(&xen_9pfs
->rings
[pdu
->tag
% xen_9pfs
->num_rings
],
160 out_sg
, &num
, pdu
->idx
);
162 ret
= v9fs_iov_vunmarshal(out_sg
, num
, offset
, 0, fmt
, ap
);
164 xen_pv_printf(&xen_9pfs
->xendev
, 0,
165 "Failed to decode VirtFS request type %d\n", pdu
->id
);
166 xen_be_set_state(&xen_9pfs
->xendev
, XenbusStateClosing
);
167 xen_9pfs_disconnect(&xen_9pfs
->xendev
);
172 static void xen_9pfs_init_out_iov_from_pdu(V9fsPDU
*pdu
,
177 Xen9pfsDev
*xen_9pfs
= container_of(pdu
->s
, Xen9pfsDev
, state
);
178 Xen9pfsRing
*ring
= &xen_9pfs
->rings
[pdu
->tag
% xen_9pfs
->num_rings
];
183 ring
->sg
= g_new0(struct iovec
, 2);
184 xen_9pfs_out_sg(ring
, ring
->sg
, &num
, pdu
->idx
);
189 static void xen_9pfs_init_in_iov_from_pdu(V9fsPDU
*pdu
,
194 Xen9pfsDev
*xen_9pfs
= container_of(pdu
->s
, Xen9pfsDev
, state
);
195 Xen9pfsRing
*ring
= &xen_9pfs
->rings
[pdu
->tag
% xen_9pfs
->num_rings
];
201 ring
->sg
= g_new0(struct iovec
, 2);
202 ring
->co
= qemu_coroutine_self();
203 /* make sure other threads see ring->co changes before continuing */
207 xen_9pfs_in_sg(ring
, ring
->sg
, &num
, pdu
->idx
, size
);
208 buf_size
= iov_size(ring
->sg
, num
);
209 if (buf_size
< size
) {
210 qemu_coroutine_yield();
214 /* make sure other threads see ring->co changes before continuing */
221 static void xen_9pfs_push_and_notify(V9fsPDU
*pdu
)
224 Xen9pfsDev
*priv
= container_of(pdu
->s
, Xen9pfsDev
, state
);
225 Xen9pfsRing
*ring
= &priv
->rings
[pdu
->tag
% priv
->num_rings
];
230 ring
->intf
->out_cons
= ring
->out_cons
;
233 prod
= ring
->intf
->in_prod
;
235 ring
->intf
->in_prod
= prod
+ pdu
->size
;
238 ring
->inprogress
= false;
239 xenevtchn_notify(ring
->evtchndev
, ring
->local_port
);
241 qemu_bh_schedule(ring
->bh
);
244 static const V9fsTransport xen_9p_transport
= {
245 .pdu_vmarshal
= xen_9pfs_pdu_vmarshal
,
246 .pdu_vunmarshal
= xen_9pfs_pdu_vunmarshal
,
247 .init_in_iov_from_pdu
= xen_9pfs_init_in_iov_from_pdu
,
248 .init_out_iov_from_pdu
= xen_9pfs_init_out_iov_from_pdu
,
249 .push_and_notify
= xen_9pfs_push_and_notify
,
252 static int xen_9pfs_init(struct XenLegacyDevice
*xendev
)
257 static int xen_9pfs_receive(Xen9pfsRing
*ring
)
260 RING_IDX cons
, prod
, masked_prod
, masked_cons
, queued
;
263 if (ring
->inprogress
) {
267 cons
= ring
->intf
->out_cons
;
268 prod
= ring
->intf
->out_prod
;
271 queued
= xen_9pfs_queued(prod
, cons
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
272 if (queued
< sizeof(h
)) {
275 ring
->inprogress
= true;
277 masked_prod
= xen_9pfs_mask(prod
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
278 masked_cons
= xen_9pfs_mask(cons
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
280 xen_9pfs_read_packet((uint8_t *) &h
, ring
->ring
.out
, sizeof(h
),
281 masked_prod
, &masked_cons
,
282 XEN_FLEX_RING_SIZE(ring
->ring_order
));
283 if (queued
< le32_to_cpu(h
.size_le
)) {
287 /* cannot fail, because we only handle one request per ring at a time */
288 pdu
= pdu_alloc(&ring
->priv
->state
);
289 ring
->out_size
= le32_to_cpu(h
.size_le
);
290 ring
->out_cons
= cons
+ le32_to_cpu(h
.size_le
);
297 static void xen_9pfs_bh(void *opaque
)
299 Xen9pfsRing
*ring
= opaque
;
303 wait
= ring
->co
!= NULL
&& qemu_coroutine_entered(ring
->co
);
304 /* paired with the smb_wmb barriers in xen_9pfs_init_in_iov_from_pdu */
311 if (ring
->co
!= NULL
) {
312 qemu_coroutine_enter_if_inactive(ring
->co
);
314 xen_9pfs_receive(ring
);
317 static void xen_9pfs_evtchn_event(void *opaque
)
319 Xen9pfsRing
*ring
= opaque
;
322 port
= xenevtchn_pending(ring
->evtchndev
);
323 xenevtchn_unmask(ring
->evtchndev
, port
);
325 qemu_bh_schedule(ring
->bh
);
328 static void xen_9pfs_disconnect(struct XenLegacyDevice
*xendev
)
330 Xen9pfsDev
*xen_9pdev
= container_of(xendev
, Xen9pfsDev
, xendev
);
333 for (i
= 0; i
< xen_9pdev
->num_rings
; i
++) {
334 if (xen_9pdev
->rings
[i
].evtchndev
!= NULL
) {
335 qemu_set_fd_handler(xenevtchn_fd(xen_9pdev
->rings
[i
].evtchndev
),
337 xenevtchn_unbind(xen_9pdev
->rings
[i
].evtchndev
,
338 xen_9pdev
->rings
[i
].local_port
);
339 xen_9pdev
->rings
[i
].evtchndev
= NULL
;
344 static int xen_9pfs_free(struct XenLegacyDevice
*xendev
)
346 Xen9pfsDev
*xen_9pdev
= container_of(xendev
, Xen9pfsDev
, xendev
);
349 if (xen_9pdev
->rings
[0].evtchndev
!= NULL
) {
350 xen_9pfs_disconnect(xendev
);
353 for (i
= 0; i
< xen_9pdev
->num_rings
; i
++) {
354 if (xen_9pdev
->rings
[i
].data
!= NULL
) {
355 xen_be_unmap_grant_refs(&xen_9pdev
->xendev
,
356 xen_9pdev
->rings
[i
].data
,
357 (1 << xen_9pdev
->rings
[i
].ring_order
));
359 if (xen_9pdev
->rings
[i
].intf
!= NULL
) {
360 xen_be_unmap_grant_refs(&xen_9pdev
->xendev
,
361 xen_9pdev
->rings
[i
].intf
,
364 if (xen_9pdev
->rings
[i
].bh
!= NULL
) {
365 qemu_bh_delete(xen_9pdev
->rings
[i
].bh
);
369 g_free(xen_9pdev
->id
);
370 g_free(xen_9pdev
->tag
);
371 g_free(xen_9pdev
->path
);
372 g_free(xen_9pdev
->security_model
);
373 g_free(xen_9pdev
->rings
);
377 static int xen_9pfs_connect(struct XenLegacyDevice
*xendev
)
381 Xen9pfsDev
*xen_9pdev
= container_of(xendev
, Xen9pfsDev
, xendev
);
382 V9fsState
*s
= &xen_9pdev
->state
;
385 if (xenstore_read_fe_int(&xen_9pdev
->xendev
, "num-rings",
386 &xen_9pdev
->num_rings
) == -1 ||
387 xen_9pdev
->num_rings
> MAX_RINGS
|| xen_9pdev
->num_rings
< 1) {
391 xen_9pdev
->rings
= g_new0(Xen9pfsRing
, xen_9pdev
->num_rings
);
392 for (i
= 0; i
< xen_9pdev
->num_rings
; i
++) {
396 xen_9pdev
->rings
[i
].priv
= xen_9pdev
;
397 xen_9pdev
->rings
[i
].evtchn
= -1;
398 xen_9pdev
->rings
[i
].local_port
= -1;
400 str
= g_strdup_printf("ring-ref%u", i
);
401 if (xenstore_read_fe_int(&xen_9pdev
->xendev
, str
,
402 &xen_9pdev
->rings
[i
].ref
) == -1) {
407 str
= g_strdup_printf("event-channel-%u", i
);
408 if (xenstore_read_fe_int(&xen_9pdev
->xendev
, str
,
409 &xen_9pdev
->rings
[i
].evtchn
) == -1) {
415 xen_9pdev
->rings
[i
].intf
=
416 xen_be_map_grant_ref(&xen_9pdev
->xendev
,
417 xen_9pdev
->rings
[i
].ref
,
418 PROT_READ
| PROT_WRITE
);
419 if (!xen_9pdev
->rings
[i
].intf
) {
422 ring_order
= xen_9pdev
->rings
[i
].intf
->ring_order
;
423 if (ring_order
> MAX_RING_ORDER
) {
426 xen_9pdev
->rings
[i
].ring_order
= ring_order
;
427 xen_9pdev
->rings
[i
].data
=
428 xen_be_map_grant_refs(&xen_9pdev
->xendev
,
429 xen_9pdev
->rings
[i
].intf
->ref
,
431 PROT_READ
| PROT_WRITE
);
432 if (!xen_9pdev
->rings
[i
].data
) {
435 xen_9pdev
->rings
[i
].ring
.in
= xen_9pdev
->rings
[i
].data
;
436 xen_9pdev
->rings
[i
].ring
.out
= xen_9pdev
->rings
[i
].data
+
437 XEN_FLEX_RING_SIZE(ring_order
);
439 xen_9pdev
->rings
[i
].bh
= qemu_bh_new(xen_9pfs_bh
, &xen_9pdev
->rings
[i
]);
440 xen_9pdev
->rings
[i
].out_cons
= 0;
441 xen_9pdev
->rings
[i
].out_size
= 0;
442 xen_9pdev
->rings
[i
].inprogress
= false;
445 xen_9pdev
->rings
[i
].evtchndev
= xenevtchn_open(NULL
, 0);
446 if (xen_9pdev
->rings
[i
].evtchndev
== NULL
) {
449 qemu_set_cloexec(xenevtchn_fd(xen_9pdev
->rings
[i
].evtchndev
));
450 xen_9pdev
->rings
[i
].local_port
= xenevtchn_bind_interdomain
451 (xen_9pdev
->rings
[i
].evtchndev
,
453 xen_9pdev
->rings
[i
].evtchn
);
454 if (xen_9pdev
->rings
[i
].local_port
== -1) {
455 xen_pv_printf(xendev
, 0,
456 "xenevtchn_bind_interdomain failed port=%d\n",
457 xen_9pdev
->rings
[i
].evtchn
);
460 xen_pv_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
461 qemu_set_fd_handler(xenevtchn_fd(xen_9pdev
->rings
[i
].evtchndev
),
462 xen_9pfs_evtchn_event
, NULL
, &xen_9pdev
->rings
[i
]);
465 xen_9pdev
->security_model
= xenstore_read_be_str(xendev
, "security_model");
466 xen_9pdev
->path
= xenstore_read_be_str(xendev
, "path");
467 xen_9pdev
->id
= s
->fsconf
.fsdev_id
=
468 g_strdup_printf("xen9p%d", xendev
->dev
);
469 xen_9pdev
->tag
= s
->fsconf
.tag
= xenstore_read_fe_str(xendev
, "tag");
470 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
473 qemu_opt_set(fsdev
, "fsdriver", "local", NULL
);
474 qemu_opt_set(fsdev
, "path", xen_9pdev
->path
, NULL
);
475 qemu_opt_set(fsdev
, "security_model", xen_9pdev
->security_model
, NULL
);
476 qemu_opts_set_id(fsdev
, s
->fsconf
.fsdev_id
);
477 qemu_fsdev_add(fsdev
, &err
);
479 error_report_err(err
);
481 v9fs_device_realize_common(s
, &xen_9p_transport
, NULL
);
486 xen_9pfs_free(xendev
);
490 static void xen_9pfs_alloc(struct XenLegacyDevice
*xendev
)
492 xenstore_write_be_str(xendev
, "versions", VERSIONS
);
493 xenstore_write_be_int(xendev
, "max-rings", MAX_RINGS
);
494 xenstore_write_be_int(xendev
, "max-ring-page-order", MAX_RING_ORDER
);
497 struct XenDevOps xen_9pfs_ops
= {
498 .size
= sizeof(Xen9pfsDev
),
499 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
500 .alloc
= xen_9pfs_alloc
,
501 .init
= xen_9pfs_init
,
502 .initialise
= xen_9pfs_connect
,
503 .disconnect
= xen_9pfs_disconnect
,
504 .free
= xen_9pfs_free
,