]>
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 | |
0d3716b4 WL |
23 | void virtio_9p_push_and_notify(V9fsPDU *pdu) |
24 | { | |
25 | V9fsState *s = pdu->s; | |
00588a0a | 26 | V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); |
51b19ebe | 27 | VirtQueueElement *elem = v->elems[pdu->idx]; |
0d3716b4 WL |
28 | |
29 | /* push onto queue and notify */ | |
00588a0a | 30 | virtqueue_push(v->vq, elem, pdu->size); |
51b19ebe PB |
31 | g_free(elem); |
32 | v->elems[pdu->idx] = NULL; | |
0d3716b4 WL |
33 | |
34 | /* FIXME: we should batch these completions */ | |
00588a0a | 35 | virtio_notify(VIRTIO_DEVICE(v), v->vq); |
0d3716b4 WL |
36 | } |
37 | ||
0192cc5d WL |
38 | static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq) |
39 | { | |
00588a0a WL |
40 | V9fsVirtioState *v = (V9fsVirtioState *)vdev; |
41 | V9fsState *s = &v->state; | |
0192cc5d WL |
42 | V9fsPDU *pdu; |
43 | ssize_t len; | |
d3d74d6f | 44 | VirtQueueElement *elem; |
0192cc5d | 45 | |
00588a0a | 46 | while ((pdu = pdu_alloc(s))) { |
0192cc5d WL |
47 | struct { |
48 | uint32_t size_le; | |
49 | uint8_t id; | |
50 | uint16_t tag_le; | |
51 | } QEMU_PACKED out; | |
0192cc5d | 52 | |
51b19ebe PB |
53 | elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); |
54 | if (!elem) { | |
d3d74d6f | 55 | goto out_free_pdu; |
00588a0a WL |
56 | } |
57 | ||
d3d74d6f GK |
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 | } | |
e8582891 | 64 | QEMU_BUILD_BUG_ON(sizeof(out) != 7); |
0192cc5d | 65 | |
51b19ebe | 66 | v->elems[pdu->idx] = elem; |
00588a0a | 67 | len = iov_to_buf(elem->out_sg, elem->out_num, 0, |
e8582891 | 68 | &out, sizeof(out)); |
d3d74d6f GK |
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 | } | |
0192cc5d WL |
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 | } | |
d3d74d6f GK |
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); | |
0192cc5d WL |
91 | } |
92 | ||
9d5b731d JW |
93 | static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features, |
94 | Error **errp) | |
f4f61d27 | 95 | { |
0cd09c3a | 96 | virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG); |
f4f61d27 AK |
97 | return features; |
98 | } | |
99 | ||
f4f61d27 AK |
100 | static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config) |
101 | { | |
e9a0152b | 102 | int len; |
f4f61d27 | 103 | struct virtio_9p_config *cfg; |
00588a0a WL |
104 | V9fsVirtioState *v = VIRTIO_9P(vdev); |
105 | V9fsState *s = &v->state; | |
f4f61d27 | 106 | |
e9a0152b AK |
107 | len = strlen(s->tag); |
108 | cfg = g_malloc0(sizeof(struct virtio_9p_config) + len); | |
d64ccb91 | 109 | virtio_stw_p(vdev, &cfg->tag_len, len); |
e9a0152b AK |
110 | /* We don't copy the terminating null to config space */ |
111 | memcpy(cfg->tag, s->tag, len); | |
00588a0a | 112 | memcpy(config, cfg, v->config_size); |
7267c094 | 113 | g_free(cfg); |
f4f61d27 AK |
114 | } |
115 | ||
18e0e5b2 | 116 | static int virtio_9p_load(QEMUFile *f, void *opaque, size_t size) |
4652f164 | 117 | { |
18e0e5b2 | 118 | return virtio_load(VIRTIO_DEVICE(opaque), f, 1); |
4652f164 GK |
119 | } |
120 | ||
59be7522 | 121 | static void virtio_9p_device_realize(DeviceState *dev, Error **errp) |
0174fe73 | 122 | { |
59be7522 | 123 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
00588a0a WL |
124 | V9fsVirtioState *v = VIRTIO_9P(dev); |
125 | V9fsState *s = &v->state; | |
f4f61d27 | 126 | |
2a0c56aa | 127 | if (v9fs_device_realize_common(s, errp)) { |
92304bf3 | 128 | goto out; |
f4f61d27 | 129 | } |
e9a0152b | 130 | |
00588a0a WL |
131 | v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag); |
132 | virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size); | |
133 | v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output); | |
2a0c56aa | 134 | |
92304bf3 | 135 | out: |
2a0c56aa | 136 | return; |
e7303c43 FK |
137 | } |
138 | ||
6cecf093 GK |
139 | static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp) |
140 | { | |
141 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
00588a0a WL |
142 | V9fsVirtioState *v = VIRTIO_9P(dev); |
143 | V9fsState *s = &v->state; | |
6cecf093 GK |
144 | |
145 | virtio_cleanup(vdev); | |
2a0c56aa | 146 | v9fs_device_unrealize_common(s, errp); |
6cecf093 GK |
147 | } |
148 | ||
fe9fa96d WL |
149 | ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset, |
150 | const char *fmt, va_list ap) | |
151 | { | |
00588a0a WL |
152 | V9fsState *s = pdu->s; |
153 | V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); | |
51b19ebe | 154 | VirtQueueElement *elem = v->elems[pdu->idx]; |
00588a0a WL |
155 | |
156 | return v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap); | |
fe9fa96d WL |
157 | } |
158 | ||
159 | ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset, | |
160 | const char *fmt, va_list ap) | |
161 | { | |
00588a0a WL |
162 | V9fsState *s = pdu->s; |
163 | V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); | |
51b19ebe | 164 | VirtQueueElement *elem = v->elems[pdu->idx]; |
00588a0a WL |
165 | |
166 | return v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap); | |
fe9fa96d WL |
167 | } |
168 | ||
592707af WL |
169 | void virtio_init_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov, |
170 | unsigned int *pniov, bool is_write) | |
171 | { | |
00588a0a WL |
172 | V9fsState *s = pdu->s; |
173 | V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); | |
51b19ebe | 174 | VirtQueueElement *elem = v->elems[pdu->idx]; |
00588a0a | 175 | |
592707af | 176 | if (is_write) { |
00588a0a WL |
177 | *piov = elem->out_sg; |
178 | *pniov = elem->out_num; | |
592707af | 179 | } else { |
00588a0a WL |
180 | *piov = elem->in_sg; |
181 | *pniov = elem->in_num; | |
592707af WL |
182 | } |
183 | } | |
184 | ||
e7303c43 FK |
185 | /* virtio-9p device */ |
186 | ||
18e0e5b2 DDAG |
187 | VMSTATE_VIRTIO_DEVICE(9p, 1, virtio_9p_load, virtio_vmstate_save); |
188 | ||
e7303c43 | 189 | static Property virtio_9p_properties[] = { |
00588a0a WL |
190 | DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag), |
191 | DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id), | |
e7303c43 FK |
192 | DEFINE_PROP_END_OF_LIST(), |
193 | }; | |
194 | ||
195 | static void virtio_9p_class_init(ObjectClass *klass, void *data) | |
196 | { | |
197 | DeviceClass *dc = DEVICE_CLASS(klass); | |
198 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | |
59be7522 | 199 | |
e7303c43 | 200 | dc->props = virtio_9p_properties; |
18e0e5b2 | 201 | dc->vmsd = &vmstate_virtio_9p; |
125ee0ed | 202 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
59be7522 | 203 | vdc->realize = virtio_9p_device_realize; |
6cecf093 | 204 | vdc->unrealize = virtio_9p_device_unrealize; |
e7303c43 FK |
205 | vdc->get_features = virtio_9p_get_features; |
206 | vdc->get_config = virtio_9p_get_config; | |
207 | } | |
208 | ||
209 | static const TypeInfo virtio_device_info = { | |
210 | .name = TYPE_VIRTIO_9P, | |
211 | .parent = TYPE_VIRTIO_DEVICE, | |
00588a0a | 212 | .instance_size = sizeof(V9fsVirtioState), |
e7303c43 FK |
213 | .class_init = virtio_9p_class_init, |
214 | }; | |
215 | ||
216 | static void virtio_9p_register_types(void) | |
217 | { | |
218 | type_register_static(&virtio_device_info); | |
219 | } | |
220 | ||
221 | type_init(virtio_9p_register_types) |