xref: /qemu/hw/9pfs/virtio-9p-device.c (revision bd3be4dbbf0491d6db8bf326706747b4629ace4b)
1f4f61d27SAneesh Kumar K.V /*
2f4f61d27SAneesh Kumar K.V  * Virtio 9p backend
3f4f61d27SAneesh Kumar K.V  *
4f4f61d27SAneesh Kumar K.V  * Copyright IBM, Corp. 2010
5f4f61d27SAneesh Kumar K.V  *
6f4f61d27SAneesh Kumar K.V  * Authors:
7f4f61d27SAneesh Kumar K.V  *  Anthony Liguori   <aliguori@us.ibm.com>
8f4f61d27SAneesh Kumar K.V  *
9f4f61d27SAneesh Kumar K.V  * This work is licensed under the terms of the GNU GPL, version 2.  See
10f4f61d27SAneesh Kumar K.V  * the COPYING file in the top-level directory.
11f4f61d27SAneesh Kumar K.V  *
12f4f61d27SAneesh Kumar K.V  */
13f4f61d27SAneesh Kumar K.V 
149b8bfe21SPeter Maydell #include "qemu/osdep.h"
150d09e41aSPaolo Bonzini #include "hw/virtio/virtio.h"
161de7afc9SPaolo Bonzini #include "qemu/sockets.h"
17f4f61d27SAneesh Kumar K.V #include "virtio-9p.h"
18f4f61d27SAneesh Kumar K.V #include "fsdev/qemu-fsdev.h"
19fe52840cSWei Liu #include "coth.h"
20d64ccb91SGreg Kurz #include "hw/virtio/virtio-access.h"
210192cc5dSWei Liu #include "qemu/iov.h"
22f4f61d27SAneesh Kumar K.V 
23ea83441cSStefano Stabellini static void virtio_9p_push_and_notify(V9fsPDU *pdu)
240d3716b4SWei Liu {
250d3716b4SWei Liu     V9fsState *s = pdu->s;
2600588a0aSWei Liu     V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
2751b19ebeSPaolo Bonzini     VirtQueueElement *elem = v->elems[pdu->idx];
280d3716b4SWei Liu 
290d3716b4SWei Liu     /* push onto queue and notify */
3000588a0aSWei Liu     virtqueue_push(v->vq, elem, pdu->size);
3151b19ebeSPaolo Bonzini     g_free(elem);
3251b19ebeSPaolo Bonzini     v->elems[pdu->idx] = NULL;
330d3716b4SWei Liu 
340d3716b4SWei Liu     /* FIXME: we should batch these completions */
3500588a0aSWei Liu     virtio_notify(VIRTIO_DEVICE(v), v->vq);
360d3716b4SWei Liu }
370d3716b4SWei Liu 
380192cc5dSWei Liu static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
390192cc5dSWei Liu {
4000588a0aSWei Liu     V9fsVirtioState *v = (V9fsVirtioState *)vdev;
4100588a0aSWei Liu     V9fsState *s = &v->state;
420192cc5dSWei Liu     V9fsPDU *pdu;
430192cc5dSWei Liu     ssize_t len;
44d3d74d6fSGreg Kurz     VirtQueueElement *elem;
450192cc5dSWei Liu 
4600588a0aSWei Liu     while ((pdu = pdu_alloc(s))) {
47c9fb47e7SStefano Stabellini         P9MsgHeader out;
480192cc5dSWei Liu 
4951b19ebeSPaolo Bonzini         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
5051b19ebeSPaolo Bonzini         if (!elem) {
51d3d74d6fSGreg Kurz             goto out_free_pdu;
5200588a0aSWei Liu         }
5300588a0aSWei Liu 
54a4d99854SGreg Kurz         if (iov_size(elem->in_sg, elem->in_num) < 7) {
55d3d74d6fSGreg Kurz             virtio_error(vdev,
56d3d74d6fSGreg Kurz                          "The guest sent a VirtFS request without space for "
57d3d74d6fSGreg Kurz                          "the reply");
58d3d74d6fSGreg Kurz             goto out_free_req;
59d3d74d6fSGreg Kurz         }
600192cc5dSWei Liu 
61a4d99854SGreg Kurz         len = iov_to_buf(elem->out_sg, elem->out_num, 0, &out, 7);
62a4d99854SGreg Kurz         if (len != 7) {
63d3d74d6fSGreg Kurz             virtio_error(vdev, "The guest sent a malformed VirtFS request: "
64d3d74d6fSGreg Kurz                          "header size is %zd, should be 7", len);
65d3d74d6fSGreg Kurz             goto out_free_req;
66d3d74d6fSGreg Kurz         }
670192cc5dSWei Liu 
683a21fb2aSGreg Kurz         v->elems[pdu->idx] = elem;
693a21fb2aSGreg Kurz 
70506f3275SGreg Kurz         pdu_submit(pdu, &out);
710192cc5dSWei Liu     }
72d3d74d6fSGreg Kurz 
73d3d74d6fSGreg Kurz     return;
74d3d74d6fSGreg Kurz 
75d3d74d6fSGreg Kurz out_free_req:
76d3d74d6fSGreg Kurz     virtqueue_detach_element(vq, elem, 0);
77d3d74d6fSGreg Kurz     g_free(elem);
78d3d74d6fSGreg Kurz out_free_pdu:
79d3d74d6fSGreg Kurz     pdu_free(pdu);
800192cc5dSWei Liu }
810192cc5dSWei Liu 
829d5b731dSJason Wang static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features,
839d5b731dSJason Wang                                        Error **errp)
84f4f61d27SAneesh Kumar K.V {
850cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG);
86f4f61d27SAneesh Kumar K.V     return features;
87f4f61d27SAneesh Kumar K.V }
88f4f61d27SAneesh Kumar K.V 
89f4f61d27SAneesh Kumar K.V static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
90f4f61d27SAneesh Kumar K.V {
91e9a0152bSAneesh Kumar K.V     int len;
92f4f61d27SAneesh Kumar K.V     struct virtio_9p_config *cfg;
9300588a0aSWei Liu     V9fsVirtioState *v = VIRTIO_9P(vdev);
9400588a0aSWei Liu     V9fsState *s = &v->state;
95f4f61d27SAneesh Kumar K.V 
96e9a0152bSAneesh Kumar K.V     len = strlen(s->tag);
97e9a0152bSAneesh Kumar K.V     cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
98d64ccb91SGreg Kurz     virtio_stw_p(vdev, &cfg->tag_len, len);
99e9a0152bSAneesh Kumar K.V     /* We don't copy the terminating null to config space */
100e9a0152bSAneesh Kumar K.V     memcpy(cfg->tag, s->tag, len);
10100588a0aSWei Liu     memcpy(config, cfg, v->config_size);
1027267c094SAnthony Liguori     g_free(cfg);
103f4f61d27SAneesh Kumar K.V }
104f4f61d27SAneesh Kumar K.V 
1050e44a0fdSGreg Kurz static void virtio_9p_reset(VirtIODevice *vdev)
1060e44a0fdSGreg Kurz {
1070e44a0fdSGreg Kurz     V9fsVirtioState *v = (V9fsVirtioState *)vdev;
1080e44a0fdSGreg Kurz 
1090e44a0fdSGreg Kurz     v9fs_reset(&v->state);
1100e44a0fdSGreg Kurz }
1110e44a0fdSGreg Kurz 
112ea83441cSStefano Stabellini static ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset,
113fe9fa96dSWei Liu                                    const char *fmt, va_list ap)
114fe9fa96dSWei Liu {
11500588a0aSWei Liu     V9fsState *s = pdu->s;
11600588a0aSWei Liu     V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
11751b19ebeSPaolo Bonzini     VirtQueueElement *elem = v->elems[pdu->idx];
1188d37de41SGreg Kurz     ssize_t ret;
11900588a0aSWei Liu 
1208d37de41SGreg Kurz     ret = v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap);
1218d37de41SGreg Kurz     if (ret < 0) {
1228d37de41SGreg Kurz         VirtIODevice *vdev = VIRTIO_DEVICE(v);
1238d37de41SGreg Kurz 
1248d37de41SGreg Kurz         virtio_error(vdev, "Failed to encode VirtFS reply type %d",
1258d37de41SGreg Kurz                      pdu->id + 1);
1268d37de41SGreg Kurz     }
1278d37de41SGreg Kurz     return ret;
128fe9fa96dSWei Liu }
129fe9fa96dSWei Liu 
130ea83441cSStefano Stabellini static ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset,
131fe9fa96dSWei Liu                                      const char *fmt, va_list ap)
132fe9fa96dSWei Liu {
13300588a0aSWei Liu     V9fsState *s = pdu->s;
13400588a0aSWei Liu     V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
13551b19ebeSPaolo Bonzini     VirtQueueElement *elem = v->elems[pdu->idx];
1368d37de41SGreg Kurz     ssize_t ret;
13700588a0aSWei Liu 
1388d37de41SGreg Kurz     ret = v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap);
1398d37de41SGreg Kurz     if (ret < 0) {
1408d37de41SGreg Kurz         VirtIODevice *vdev = VIRTIO_DEVICE(v);
1418d37de41SGreg Kurz 
1428d37de41SGreg Kurz         virtio_error(vdev, "Failed to decode VirtFS request type %d", pdu->id);
1438d37de41SGreg Kurz     }
1448d37de41SGreg Kurz     return ret;
145fe9fa96dSWei Liu }
146fe9fa96dSWei Liu 
14788da0b03SStefano Stabellini static void virtio_init_in_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
14888da0b03SStefano Stabellini                                         unsigned int *pniov, size_t size)
149592707afSWei Liu {
15000588a0aSWei Liu     V9fsState *s = pdu->s;
15100588a0aSWei Liu     V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
15251b19ebeSPaolo Bonzini     VirtQueueElement *elem = v->elems[pdu->idx];
1538d37de41SGreg Kurz     size_t buf_size = iov_size(elem->in_sg, elem->in_num);
1548d37de41SGreg Kurz 
1558d37de41SGreg Kurz     if (buf_size < size) {
1568d37de41SGreg Kurz         VirtIODevice *vdev = VIRTIO_DEVICE(v);
1578d37de41SGreg Kurz 
1588d37de41SGreg Kurz         virtio_error(vdev,
1598d37de41SGreg Kurz                      "VirtFS reply type %d needs %zu bytes, buffer has %zu",
1608d37de41SGreg Kurz                      pdu->id + 1, size, buf_size);
1618d37de41SGreg Kurz     }
16200588a0aSWei Liu 
16300588a0aSWei Liu     *piov = elem->in_sg;
16400588a0aSWei Liu     *pniov = elem->in_num;
165592707afSWei Liu }
16688da0b03SStefano Stabellini 
16788da0b03SStefano Stabellini static void virtio_init_out_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
1688d37de41SGreg Kurz                                          unsigned int *pniov, size_t size)
16988da0b03SStefano Stabellini {
17088da0b03SStefano Stabellini     V9fsState *s = pdu->s;
17188da0b03SStefano Stabellini     V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
17288da0b03SStefano Stabellini     VirtQueueElement *elem = v->elems[pdu->idx];
1738d37de41SGreg Kurz     size_t buf_size = iov_size(elem->out_sg, elem->out_num);
1748d37de41SGreg Kurz 
1758d37de41SGreg Kurz     if (buf_size < size) {
1768d37de41SGreg Kurz         VirtIODevice *vdev = VIRTIO_DEVICE(v);
1778d37de41SGreg Kurz 
1788d37de41SGreg Kurz         virtio_error(vdev,
1798d37de41SGreg Kurz                      "VirtFS request type %d needs %zu bytes, buffer has %zu",
1808d37de41SGreg Kurz                      pdu->id, size, buf_size);
1818d37de41SGreg Kurz     }
18288da0b03SStefano Stabellini 
18388da0b03SStefano Stabellini     *piov = elem->out_sg;
18488da0b03SStefano Stabellini     *pniov = elem->out_num;
185592707afSWei Liu }
186592707afSWei Liu 
187ea83441cSStefano Stabellini static const struct V9fsTransport virtio_9p_transport = {
188ea83441cSStefano Stabellini     .pdu_vmarshal = virtio_pdu_vmarshal,
189ea83441cSStefano Stabellini     .pdu_vunmarshal = virtio_pdu_vunmarshal,
19088da0b03SStefano Stabellini     .init_in_iov_from_pdu = virtio_init_in_iov_from_pdu,
19188da0b03SStefano Stabellini     .init_out_iov_from_pdu = virtio_init_out_iov_from_pdu,
192ea83441cSStefano Stabellini     .push_and_notify = virtio_9p_push_and_notify,
193ea83441cSStefano Stabellini };
194ea83441cSStefano Stabellini 
195*bd3be4dbSGreg Kurz static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
196*bd3be4dbSGreg Kurz {
197*bd3be4dbSGreg Kurz     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
198*bd3be4dbSGreg Kurz     V9fsVirtioState *v = VIRTIO_9P(dev);
199*bd3be4dbSGreg Kurz     V9fsState *s = &v->state;
200*bd3be4dbSGreg Kurz 
201*bd3be4dbSGreg Kurz     if (v9fs_device_realize_common(s, errp)) {
202*bd3be4dbSGreg Kurz         goto out;
203*bd3be4dbSGreg Kurz     }
204*bd3be4dbSGreg Kurz 
205*bd3be4dbSGreg Kurz     v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag);
206*bd3be4dbSGreg Kurz     virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size);
207*bd3be4dbSGreg Kurz     v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
208*bd3be4dbSGreg Kurz     v9fs_register_transport(s, &virtio_9p_transport);
209*bd3be4dbSGreg Kurz 
210*bd3be4dbSGreg Kurz out:
211*bd3be4dbSGreg Kurz     return;
212*bd3be4dbSGreg Kurz }
213*bd3be4dbSGreg Kurz 
214*bd3be4dbSGreg Kurz static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp)
215*bd3be4dbSGreg Kurz {
216*bd3be4dbSGreg Kurz     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
217*bd3be4dbSGreg Kurz     V9fsVirtioState *v = VIRTIO_9P(dev);
218*bd3be4dbSGreg Kurz     V9fsState *s = &v->state;
219*bd3be4dbSGreg Kurz 
220*bd3be4dbSGreg Kurz     virtio_cleanup(vdev);
221*bd3be4dbSGreg Kurz     v9fs_device_unrealize_common(s, errp);
222*bd3be4dbSGreg Kurz }
223*bd3be4dbSGreg Kurz 
224e7303c43SKONRAD Frederic /* virtio-9p device */
225e7303c43SKONRAD Frederic 
226dcaf8ddaSHalil Pasic static const VMStateDescription vmstate_virtio_9p = {
227dcaf8ddaSHalil Pasic     .name = "virtio-9p",
228dcaf8ddaSHalil Pasic     .minimum_version_id = 1,
229dcaf8ddaSHalil Pasic     .version_id = 1,
230dcaf8ddaSHalil Pasic     .fields = (VMStateField[]) {
231dcaf8ddaSHalil Pasic         VMSTATE_VIRTIO_DEVICE,
232dcaf8ddaSHalil Pasic         VMSTATE_END_OF_LIST()
233dcaf8ddaSHalil Pasic     },
234dcaf8ddaSHalil Pasic };
23518e0e5b2SDr. David Alan Gilbert 
236e7303c43SKONRAD Frederic static Property virtio_9p_properties[] = {
23700588a0aSWei Liu     DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag),
23800588a0aSWei Liu     DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id),
239e7303c43SKONRAD Frederic     DEFINE_PROP_END_OF_LIST(),
240e7303c43SKONRAD Frederic };
241e7303c43SKONRAD Frederic 
242e7303c43SKONRAD Frederic static void virtio_9p_class_init(ObjectClass *klass, void *data)
243e7303c43SKONRAD Frederic {
244e7303c43SKONRAD Frederic     DeviceClass *dc = DEVICE_CLASS(klass);
245e7303c43SKONRAD Frederic     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
24659be7522SAndreas Färber 
247e7303c43SKONRAD Frederic     dc->props = virtio_9p_properties;
24818e0e5b2SDr. David Alan Gilbert     dc->vmsd = &vmstate_virtio_9p;
249125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
25059be7522SAndreas Färber     vdc->realize = virtio_9p_device_realize;
2516cecf093SGreg Kurz     vdc->unrealize = virtio_9p_device_unrealize;
252e7303c43SKONRAD Frederic     vdc->get_features = virtio_9p_get_features;
253e7303c43SKONRAD Frederic     vdc->get_config = virtio_9p_get_config;
2540e44a0fdSGreg Kurz     vdc->reset = virtio_9p_reset;
255e7303c43SKONRAD Frederic }
256e7303c43SKONRAD Frederic 
257e7303c43SKONRAD Frederic static const TypeInfo virtio_device_info = {
258e7303c43SKONRAD Frederic     .name = TYPE_VIRTIO_9P,
259e7303c43SKONRAD Frederic     .parent = TYPE_VIRTIO_DEVICE,
26000588a0aSWei Liu     .instance_size = sizeof(V9fsVirtioState),
261e7303c43SKONRAD Frederic     .class_init = virtio_9p_class_init,
262e7303c43SKONRAD Frederic };
263e7303c43SKONRAD Frederic 
264e7303c43SKONRAD Frederic static void virtio_9p_register_types(void)
265e7303c43SKONRAD Frederic {
266e7303c43SKONRAD Frederic     type_register_static(&virtio_device_info);
267e7303c43SKONRAD Frederic }
268e7303c43SKONRAD Frederic 
269e7303c43SKONRAD Frederic type_init(virtio_9p_register_types)
270