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_backend.h"
16 #include "hw/9pfs/xen-9pfs.h"
17 #include "qemu/config-file.h"
18 #include "qemu/option.h"
19 #include "fsdev/qemu-fsdev.h"
23 #define MAX_RING_ORDER 8
25 typedef struct Xen9pfsRing
{
26 struct Xen9pfsDev
*priv
;
29 xenevtchn_handle
*evtchndev
;
33 struct xen_9pfs_data_intf
*intf
;
35 struct xen_9pfs_data ring
;
40 /* local copies, so that we can read/write PDU data directly from
42 RING_IDX out_cons
, out_size
, in_cons
;
46 typedef struct Xen9pfsDev
{
47 struct XenDevice xendev
; /* must be first */
58 static void xen_9pfs_disconnect(struct XenDevice
*xendev
);
60 static void xen_9pfs_in_sg(Xen9pfsRing
*ring
,
66 RING_IDX cons
, prod
, masked_prod
, masked_cons
;
68 cons
= ring
->intf
->in_cons
;
69 prod
= ring
->intf
->in_prod
;
71 masked_prod
= xen_9pfs_mask(prod
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
72 masked_cons
= xen_9pfs_mask(cons
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
74 if (masked_prod
< masked_cons
) {
75 in_sg
[0].iov_base
= ring
->ring
.in
+ masked_prod
;
76 in_sg
[0].iov_len
= masked_cons
- masked_prod
;
79 in_sg
[0].iov_base
= ring
->ring
.in
+ masked_prod
;
80 in_sg
[0].iov_len
= XEN_FLEX_RING_SIZE(ring
->ring_order
) - masked_prod
;
81 in_sg
[1].iov_base
= ring
->ring
.in
;
82 in_sg
[1].iov_len
= masked_cons
;
87 static void xen_9pfs_out_sg(Xen9pfsRing
*ring
,
92 RING_IDX cons
, prod
, masked_prod
, masked_cons
;
94 cons
= ring
->intf
->out_cons
;
95 prod
= ring
->intf
->out_prod
;
97 masked_prod
= xen_9pfs_mask(prod
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
98 masked_cons
= xen_9pfs_mask(cons
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
100 if (masked_cons
< masked_prod
) {
101 out_sg
[0].iov_base
= ring
->ring
.out
+ masked_cons
;
102 out_sg
[0].iov_len
= ring
->out_size
;
106 (XEN_FLEX_RING_SIZE(ring
->ring_order
) - masked_cons
)) {
107 out_sg
[0].iov_base
= ring
->ring
.out
+ masked_cons
;
108 out_sg
[0].iov_len
= XEN_FLEX_RING_SIZE(ring
->ring_order
) -
110 out_sg
[1].iov_base
= ring
->ring
.out
;
111 out_sg
[1].iov_len
= ring
->out_size
-
112 (XEN_FLEX_RING_SIZE(ring
->ring_order
) -
116 out_sg
[0].iov_base
= ring
->ring
.out
+ masked_cons
;
117 out_sg
[0].iov_len
= ring
->out_size
;
123 static ssize_t
xen_9pfs_pdu_vmarshal(V9fsPDU
*pdu
,
128 Xen9pfsDev
*xen_9pfs
= container_of(pdu
->s
, Xen9pfsDev
, state
);
129 struct iovec in_sg
[2];
133 xen_9pfs_in_sg(&xen_9pfs
->rings
[pdu
->tag
% xen_9pfs
->num_rings
],
134 in_sg
, &num
, pdu
->idx
, ROUND_UP(offset
+ 128, 512));
136 ret
= v9fs_iov_vmarshal(in_sg
, num
, offset
, 0, fmt
, ap
);
138 xen_pv_printf(&xen_9pfs
->xendev
, 0,
139 "Failed to encode VirtFS request type %d\n", pdu
->id
+ 1);
140 xen_be_set_state(&xen_9pfs
->xendev
, XenbusStateClosing
);
141 xen_9pfs_disconnect(&xen_9pfs
->xendev
);
146 static ssize_t
xen_9pfs_pdu_vunmarshal(V9fsPDU
*pdu
,
151 Xen9pfsDev
*xen_9pfs
= container_of(pdu
->s
, Xen9pfsDev
, state
);
152 struct iovec out_sg
[2];
156 xen_9pfs_out_sg(&xen_9pfs
->rings
[pdu
->tag
% xen_9pfs
->num_rings
],
157 out_sg
, &num
, pdu
->idx
);
159 ret
= v9fs_iov_vunmarshal(out_sg
, num
, offset
, 0, fmt
, ap
);
161 xen_pv_printf(&xen_9pfs
->xendev
, 0,
162 "Failed to decode VirtFS request type %d\n", pdu
->id
);
163 xen_be_set_state(&xen_9pfs
->xendev
, XenbusStateClosing
);
164 xen_9pfs_disconnect(&xen_9pfs
->xendev
);
169 static void xen_9pfs_init_out_iov_from_pdu(V9fsPDU
*pdu
,
174 Xen9pfsDev
*xen_9pfs
= container_of(pdu
->s
, Xen9pfsDev
, state
);
175 Xen9pfsRing
*ring
= &xen_9pfs
->rings
[pdu
->tag
% xen_9pfs
->num_rings
];
180 ring
->sg
= g_malloc0(sizeof(*ring
->sg
) * 2);
181 xen_9pfs_out_sg(ring
, ring
->sg
, &num
, pdu
->idx
);
186 static void xen_9pfs_init_in_iov_from_pdu(V9fsPDU
*pdu
,
191 Xen9pfsDev
*xen_9pfs
= container_of(pdu
->s
, Xen9pfsDev
, state
);
192 Xen9pfsRing
*ring
= &xen_9pfs
->rings
[pdu
->tag
% xen_9pfs
->num_rings
];
198 ring
->sg
= g_malloc0(sizeof(*ring
->sg
) * 2);
199 xen_9pfs_in_sg(ring
, ring
->sg
, &num
, pdu
->idx
, size
);
201 buf_size
= iov_size(ring
->sg
, num
);
202 if (buf_size
< size
) {
203 xen_pv_printf(&xen_9pfs
->xendev
, 0, "Xen 9pfs request type %d"
204 "needs %zu bytes, buffer has %zu\n", pdu
->id
, size
,
206 xen_be_set_state(&xen_9pfs
->xendev
, XenbusStateClosing
);
207 xen_9pfs_disconnect(&xen_9pfs
->xendev
);
214 static void xen_9pfs_push_and_notify(V9fsPDU
*pdu
)
217 Xen9pfsDev
*priv
= container_of(pdu
->s
, Xen9pfsDev
, state
);
218 Xen9pfsRing
*ring
= &priv
->rings
[pdu
->tag
% priv
->num_rings
];
223 ring
->intf
->out_cons
= ring
->out_cons
;
226 prod
= ring
->intf
->in_prod
;
228 ring
->intf
->in_prod
= prod
+ pdu
->size
;
231 ring
->inprogress
= false;
232 xenevtchn_notify(ring
->evtchndev
, ring
->local_port
);
234 qemu_bh_schedule(ring
->bh
);
237 static const V9fsTransport xen_9p_transport
= {
238 .pdu_vmarshal
= xen_9pfs_pdu_vmarshal
,
239 .pdu_vunmarshal
= xen_9pfs_pdu_vunmarshal
,
240 .init_in_iov_from_pdu
= xen_9pfs_init_in_iov_from_pdu
,
241 .init_out_iov_from_pdu
= xen_9pfs_init_out_iov_from_pdu
,
242 .push_and_notify
= xen_9pfs_push_and_notify
,
245 static int xen_9pfs_init(struct XenDevice
*xendev
)
250 static int xen_9pfs_receive(Xen9pfsRing
*ring
)
253 RING_IDX cons
, prod
, masked_prod
, masked_cons
, queued
;
256 if (ring
->inprogress
) {
260 cons
= ring
->intf
->out_cons
;
261 prod
= ring
->intf
->out_prod
;
264 queued
= xen_9pfs_queued(prod
, cons
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
265 if (queued
< sizeof(h
)) {
268 ring
->inprogress
= true;
270 masked_prod
= xen_9pfs_mask(prod
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
271 masked_cons
= xen_9pfs_mask(cons
, XEN_FLEX_RING_SIZE(ring
->ring_order
));
273 xen_9pfs_read_packet((uint8_t *) &h
, ring
->ring
.out
, sizeof(h
),
274 masked_prod
, &masked_cons
,
275 XEN_FLEX_RING_SIZE(ring
->ring_order
));
276 if (queued
< le32_to_cpu(h
.size_le
)) {
280 /* cannot fail, because we only handle one request per ring at a time */
281 pdu
= pdu_alloc(&ring
->priv
->state
);
282 ring
->out_size
= le32_to_cpu(h
.size_le
);
283 ring
->out_cons
= cons
+ le32_to_cpu(h
.size_le
);
290 static void xen_9pfs_bh(void *opaque
)
292 Xen9pfsRing
*ring
= opaque
;
293 xen_9pfs_receive(ring
);
296 static void xen_9pfs_evtchn_event(void *opaque
)
298 Xen9pfsRing
*ring
= opaque
;
301 port
= xenevtchn_pending(ring
->evtchndev
);
302 xenevtchn_unmask(ring
->evtchndev
, port
);
304 qemu_bh_schedule(ring
->bh
);
307 static void xen_9pfs_disconnect(struct XenDevice
*xendev
)
309 Xen9pfsDev
*xen_9pdev
= container_of(xendev
, Xen9pfsDev
, xendev
);
312 for (i
= 0; i
< xen_9pdev
->num_rings
; i
++) {
313 if (xen_9pdev
->rings
[i
].evtchndev
!= NULL
) {
314 qemu_set_fd_handler(xenevtchn_fd(xen_9pdev
->rings
[i
].evtchndev
),
316 xenevtchn_unbind(xen_9pdev
->rings
[i
].evtchndev
,
317 xen_9pdev
->rings
[i
].local_port
);
318 xen_9pdev
->rings
[i
].evtchndev
= NULL
;
323 static int xen_9pfs_free(struct XenDevice
*xendev
)
325 Xen9pfsDev
*xen_9pdev
= container_of(xendev
, Xen9pfsDev
, xendev
);
328 if (xen_9pdev
->rings
[0].evtchndev
!= NULL
) {
329 xen_9pfs_disconnect(xendev
);
332 for (i
= 0; i
< xen_9pdev
->num_rings
; i
++) {
333 if (xen_9pdev
->rings
[i
].data
!= NULL
) {
334 xen_be_unmap_grant_refs(&xen_9pdev
->xendev
,
335 xen_9pdev
->rings
[i
].data
,
336 (1 << xen_9pdev
->rings
[i
].ring_order
));
338 if (xen_9pdev
->rings
[i
].intf
!= NULL
) {
339 xen_be_unmap_grant_refs(&xen_9pdev
->xendev
,
340 xen_9pdev
->rings
[i
].intf
,
343 if (xen_9pdev
->rings
[i
].bh
!= NULL
) {
344 qemu_bh_delete(xen_9pdev
->rings
[i
].bh
);
348 g_free(xen_9pdev
->id
);
349 g_free(xen_9pdev
->tag
);
350 g_free(xen_9pdev
->path
);
351 g_free(xen_9pdev
->security_model
);
352 g_free(xen_9pdev
->rings
);
356 static int xen_9pfs_connect(struct XenDevice
*xendev
)
359 Xen9pfsDev
*xen_9pdev
= container_of(xendev
, Xen9pfsDev
, xendev
);
360 V9fsState
*s
= &xen_9pdev
->state
;
363 if (xenstore_read_fe_int(&xen_9pdev
->xendev
, "num-rings",
364 &xen_9pdev
->num_rings
) == -1 ||
365 xen_9pdev
->num_rings
> MAX_RINGS
|| xen_9pdev
->num_rings
< 1) {
369 xen_9pdev
->rings
= g_malloc0(xen_9pdev
->num_rings
* sizeof(Xen9pfsRing
));
370 for (i
= 0; i
< xen_9pdev
->num_rings
; i
++) {
374 xen_9pdev
->rings
[i
].priv
= xen_9pdev
;
375 xen_9pdev
->rings
[i
].evtchn
= -1;
376 xen_9pdev
->rings
[i
].local_port
= -1;
378 str
= g_strdup_printf("ring-ref%u", i
);
379 if (xenstore_read_fe_int(&xen_9pdev
->xendev
, str
,
380 &xen_9pdev
->rings
[i
].ref
) == -1) {
385 str
= g_strdup_printf("event-channel-%u", i
);
386 if (xenstore_read_fe_int(&xen_9pdev
->xendev
, str
,
387 &xen_9pdev
->rings
[i
].evtchn
) == -1) {
393 xen_9pdev
->rings
[i
].intf
=
394 xen_be_map_grant_ref(&xen_9pdev
->xendev
,
395 xen_9pdev
->rings
[i
].ref
,
396 PROT_READ
| PROT_WRITE
);
397 if (!xen_9pdev
->rings
[i
].intf
) {
400 ring_order
= xen_9pdev
->rings
[i
].intf
->ring_order
;
401 if (ring_order
> MAX_RING_ORDER
) {
404 xen_9pdev
->rings
[i
].ring_order
= ring_order
;
405 xen_9pdev
->rings
[i
].data
=
406 xen_be_map_grant_refs(&xen_9pdev
->xendev
,
407 xen_9pdev
->rings
[i
].intf
->ref
,
409 PROT_READ
| PROT_WRITE
);
410 if (!xen_9pdev
->rings
[i
].data
) {
413 xen_9pdev
->rings
[i
].ring
.in
= xen_9pdev
->rings
[i
].data
;
414 xen_9pdev
->rings
[i
].ring
.out
= xen_9pdev
->rings
[i
].data
+
415 XEN_FLEX_RING_SIZE(ring_order
);
417 xen_9pdev
->rings
[i
].bh
= qemu_bh_new(xen_9pfs_bh
, &xen_9pdev
->rings
[i
]);
418 xen_9pdev
->rings
[i
].out_cons
= 0;
419 xen_9pdev
->rings
[i
].out_size
= 0;
420 xen_9pdev
->rings
[i
].inprogress
= false;
423 xen_9pdev
->rings
[i
].evtchndev
= xenevtchn_open(NULL
, 0);
424 if (xen_9pdev
->rings
[i
].evtchndev
== NULL
) {
427 qemu_set_cloexec(xenevtchn_fd(xen_9pdev
->rings
[i
].evtchndev
));
428 xen_9pdev
->rings
[i
].local_port
= xenevtchn_bind_interdomain
429 (xen_9pdev
->rings
[i
].evtchndev
,
431 xen_9pdev
->rings
[i
].evtchn
);
432 if (xen_9pdev
->rings
[i
].local_port
== -1) {
433 xen_pv_printf(xendev
, 0,
434 "xenevtchn_bind_interdomain failed port=%d\n",
435 xen_9pdev
->rings
[i
].evtchn
);
438 xen_pv_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
439 qemu_set_fd_handler(xenevtchn_fd(xen_9pdev
->rings
[i
].evtchndev
),
440 xen_9pfs_evtchn_event
, NULL
, &xen_9pdev
->rings
[i
]);
443 xen_9pdev
->security_model
= xenstore_read_be_str(xendev
, "security_model");
444 xen_9pdev
->path
= xenstore_read_be_str(xendev
, "path");
445 xen_9pdev
->id
= s
->fsconf
.fsdev_id
=
446 g_strdup_printf("xen9p%d", xendev
->dev
);
447 xen_9pdev
->tag
= s
->fsconf
.tag
= xenstore_read_fe_str(xendev
, "tag");
448 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
451 qemu_opt_set(fsdev
, "fsdriver", "local", NULL
);
452 qemu_opt_set(fsdev
, "path", xen_9pdev
->path
, NULL
);
453 qemu_opt_set(fsdev
, "security_model", xen_9pdev
->security_model
, NULL
);
454 qemu_opts_set_id(fsdev
, s
->fsconf
.fsdev_id
);
455 qemu_fsdev_add(fsdev
);
456 v9fs_device_realize_common(s
, &xen_9p_transport
, NULL
);
461 xen_9pfs_free(xendev
);
465 static void xen_9pfs_alloc(struct XenDevice
*xendev
)
467 xenstore_write_be_str(xendev
, "versions", VERSIONS
);
468 xenstore_write_be_int(xendev
, "max-rings", MAX_RINGS
);
469 xenstore_write_be_int(xendev
, "max-ring-page-order", MAX_RING_ORDER
);
472 struct XenDevOps xen_9pfs_ops
= {
473 .size
= sizeof(Xen9pfsDev
),
474 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
475 .alloc
= xen_9pfs_alloc
,
476 .init
= xen_9pfs_init
,
477 .initialise
= xen_9pfs_connect
,
478 .disconnect
= xen_9pfs_disconnect
,
479 .free
= xen_9pfs_free
,