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