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