xref: /openbmc/qemu/hw/9pfs/virtio-9p-device.c (revision 56e2cd24)
1 /*
2  * Virtio 9p backend
3  *
4  * Copyright IBM, Corp. 2010
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 
14 #include "qemu/osdep.h"
15 #include "hw/virtio/virtio.h"
16 #include "qemu/sockets.h"
17 #include "virtio-9p.h"
18 #include "fsdev/qemu-fsdev.h"
19 #include "coth.h"
20 #include "hw/virtio/virtio-access.h"
21 #include "qemu/iov.h"
22 
23 static const struct V9fsTransport virtio_9p_transport;
24 
25 static void virtio_9p_push_and_notify(V9fsPDU *pdu)
26 {
27     V9fsState *s = pdu->s;
28     V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
29     VirtQueueElement *elem = v->elems[pdu->idx];
30 
31     /* push onto queue and notify */
32     virtqueue_push(v->vq, elem, pdu->size);
33     g_free(elem);
34     v->elems[pdu->idx] = NULL;
35 
36     /* FIXME: we should batch these completions */
37     virtio_notify(VIRTIO_DEVICE(v), v->vq);
38 }
39 
40 static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
41 {
42     V9fsVirtioState *v = (V9fsVirtioState *)vdev;
43     V9fsState *s = &v->state;
44     V9fsPDU *pdu;
45     ssize_t len;
46     VirtQueueElement *elem;
47 
48     while ((pdu = pdu_alloc(s))) {
49         P9MsgHeader out;
50 
51         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
52         if (!elem) {
53             goto out_free_pdu;
54         }
55 
56         if (elem->in_num == 0) {
57             virtio_error(vdev,
58                          "The guest sent a VirtFS request without space for "
59                          "the reply");
60             goto out_free_req;
61         }
62         QEMU_BUILD_BUG_ON(sizeof(out) != 7);
63 
64         v->elems[pdu->idx] = elem;
65         len = iov_to_buf(elem->out_sg, elem->out_num, 0,
66                          &out, sizeof(out));
67         if (len != sizeof(out)) {
68             virtio_error(vdev, "The guest sent a malformed VirtFS request: "
69                          "header size is %zd, should be 7", len);
70             goto out_free_req;
71         }
72 
73         pdu->size = le32_to_cpu(out.size_le);
74 
75         pdu->id = out.id;
76         pdu->tag = le16_to_cpu(out.tag_le);
77 
78         qemu_co_queue_init(&pdu->complete);
79         pdu_submit(pdu);
80     }
81 
82     return;
83 
84 out_free_req:
85     virtqueue_detach_element(vq, elem, 0);
86     g_free(elem);
87 out_free_pdu:
88     pdu_free(pdu);
89 }
90 
91 static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features,
92                                        Error **errp)
93 {
94     virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG);
95     return features;
96 }
97 
98 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
99 {
100     int len;
101     struct virtio_9p_config *cfg;
102     V9fsVirtioState *v = VIRTIO_9P(vdev);
103     V9fsState *s = &v->state;
104 
105     len = strlen(s->tag);
106     cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
107     virtio_stw_p(vdev, &cfg->tag_len, len);
108     /* We don't copy the terminating null to config space */
109     memcpy(cfg->tag, s->tag, len);
110     memcpy(config, cfg, v->config_size);
111     g_free(cfg);
112 }
113 
114 static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
115 {
116     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
117     V9fsVirtioState *v = VIRTIO_9P(dev);
118     V9fsState *s = &v->state;
119 
120     if (v9fs_device_realize_common(s, errp)) {
121         goto out;
122     }
123 
124     v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag);
125     virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size);
126     v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
127     v9fs_register_transport(s, &virtio_9p_transport);
128 
129 out:
130     return;
131 }
132 
133 static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp)
134 {
135     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
136     V9fsVirtioState *v = VIRTIO_9P(dev);
137     V9fsState *s = &v->state;
138 
139     virtio_cleanup(vdev);
140     v9fs_device_unrealize_common(s, errp);
141 }
142 
143 static void virtio_9p_reset(VirtIODevice *vdev)
144 {
145     V9fsVirtioState *v = (V9fsVirtioState *)vdev;
146 
147     v9fs_reset(&v->state);
148 }
149 
150 static ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset,
151                                    const char *fmt, va_list ap)
152 {
153     V9fsState *s = pdu->s;
154     V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
155     VirtQueueElement *elem = v->elems[pdu->idx];
156 
157     return v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap);
158 }
159 
160 static ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset,
161                                      const char *fmt, va_list ap)
162 {
163     V9fsState *s = pdu->s;
164     V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
165     VirtQueueElement *elem = v->elems[pdu->idx];
166 
167     return v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap);
168 }
169 
170 /* The size parameter is used by other transports. Do not drop it. */
171 static void virtio_init_in_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
172                                         unsigned int *pniov, size_t size)
173 {
174     V9fsState *s = pdu->s;
175     V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
176     VirtQueueElement *elem = v->elems[pdu->idx];
177 
178     *piov = elem->in_sg;
179     *pniov = elem->in_num;
180 }
181 
182 static void virtio_init_out_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
183                                          unsigned int *pniov)
184 {
185     V9fsState *s = pdu->s;
186     V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
187     VirtQueueElement *elem = v->elems[pdu->idx];
188 
189     *piov = elem->out_sg;
190     *pniov = elem->out_num;
191 }
192 
193 static const struct V9fsTransport virtio_9p_transport = {
194     .pdu_vmarshal = virtio_pdu_vmarshal,
195     .pdu_vunmarshal = virtio_pdu_vunmarshal,
196     .init_in_iov_from_pdu = virtio_init_in_iov_from_pdu,
197     .init_out_iov_from_pdu = virtio_init_out_iov_from_pdu,
198     .push_and_notify = virtio_9p_push_and_notify,
199 };
200 
201 /* virtio-9p device */
202 
203 static const VMStateDescription vmstate_virtio_9p = {
204     .name = "virtio-9p",
205     .minimum_version_id = 1,
206     .version_id = 1,
207     .fields = (VMStateField[]) {
208         VMSTATE_VIRTIO_DEVICE,
209         VMSTATE_END_OF_LIST()
210     },
211 };
212 
213 static Property virtio_9p_properties[] = {
214     DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag),
215     DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id),
216     DEFINE_PROP_END_OF_LIST(),
217 };
218 
219 static void virtio_9p_class_init(ObjectClass *klass, void *data)
220 {
221     DeviceClass *dc = DEVICE_CLASS(klass);
222     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
223 
224     dc->props = virtio_9p_properties;
225     dc->vmsd = &vmstate_virtio_9p;
226     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
227     vdc->realize = virtio_9p_device_realize;
228     vdc->unrealize = virtio_9p_device_unrealize;
229     vdc->get_features = virtio_9p_get_features;
230     vdc->get_config = virtio_9p_get_config;
231     vdc->reset = virtio_9p_reset;
232 }
233 
234 static const TypeInfo virtio_device_info = {
235     .name = TYPE_VIRTIO_9P,
236     .parent = TYPE_VIRTIO_DEVICE,
237     .instance_size = sizeof(V9fsVirtioState),
238     .class_init = virtio_9p_class_init,
239 };
240 
241 static void virtio_9p_register_types(void)
242 {
243     type_register_static(&virtio_device_info);
244 }
245 
246 type_init(virtio_9p_register_types)
247