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 len = iov_to_buf(elem->out_sg, elem->out_num, 0, 65 &out, sizeof(out)); 66 if (len != sizeof(out)) { 67 virtio_error(vdev, "The guest sent a malformed VirtFS request: " 68 "header size is %zd, should be 7", len); 69 goto out_free_req; 70 } 71 72 v->elems[pdu->idx] = elem; 73 74 pdu_submit(pdu, &out); 75 } 76 77 return; 78 79 out_free_req: 80 virtqueue_detach_element(vq, elem, 0); 81 g_free(elem); 82 out_free_pdu: 83 pdu_free(pdu); 84 } 85 86 static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features, 87 Error **errp) 88 { 89 virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG); 90 return features; 91 } 92 93 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config) 94 { 95 int len; 96 struct virtio_9p_config *cfg; 97 V9fsVirtioState *v = VIRTIO_9P(vdev); 98 V9fsState *s = &v->state; 99 100 len = strlen(s->tag); 101 cfg = g_malloc0(sizeof(struct virtio_9p_config) + len); 102 virtio_stw_p(vdev, &cfg->tag_len, len); 103 /* We don't copy the terminating null to config space */ 104 memcpy(cfg->tag, s->tag, len); 105 memcpy(config, cfg, v->config_size); 106 g_free(cfg); 107 } 108 109 static void virtio_9p_device_realize(DeviceState *dev, Error **errp) 110 { 111 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 112 V9fsVirtioState *v = VIRTIO_9P(dev); 113 V9fsState *s = &v->state; 114 115 if (v9fs_device_realize_common(s, errp)) { 116 goto out; 117 } 118 119 v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag); 120 virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size); 121 v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output); 122 v9fs_register_transport(s, &virtio_9p_transport); 123 124 out: 125 return; 126 } 127 128 static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp) 129 { 130 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 131 V9fsVirtioState *v = VIRTIO_9P(dev); 132 V9fsState *s = &v->state; 133 134 virtio_cleanup(vdev); 135 v9fs_device_unrealize_common(s, errp); 136 } 137 138 static void virtio_9p_reset(VirtIODevice *vdev) 139 { 140 V9fsVirtioState *v = (V9fsVirtioState *)vdev; 141 142 v9fs_reset(&v->state); 143 } 144 145 static ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset, 146 const char *fmt, va_list ap) 147 { 148 V9fsState *s = pdu->s; 149 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 150 VirtQueueElement *elem = v->elems[pdu->idx]; 151 152 return v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap); 153 } 154 155 static ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset, 156 const char *fmt, va_list ap) 157 { 158 V9fsState *s = pdu->s; 159 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 160 VirtQueueElement *elem = v->elems[pdu->idx]; 161 162 return v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap); 163 } 164 165 /* The size parameter is used by other transports. Do not drop it. */ 166 static void virtio_init_in_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov, 167 unsigned int *pniov, size_t size) 168 { 169 V9fsState *s = pdu->s; 170 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 171 VirtQueueElement *elem = v->elems[pdu->idx]; 172 173 *piov = elem->in_sg; 174 *pniov = elem->in_num; 175 } 176 177 static void virtio_init_out_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov, 178 unsigned int *pniov) 179 { 180 V9fsState *s = pdu->s; 181 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 182 VirtQueueElement *elem = v->elems[pdu->idx]; 183 184 *piov = elem->out_sg; 185 *pniov = elem->out_num; 186 } 187 188 static const struct V9fsTransport virtio_9p_transport = { 189 .pdu_vmarshal = virtio_pdu_vmarshal, 190 .pdu_vunmarshal = virtio_pdu_vunmarshal, 191 .init_in_iov_from_pdu = virtio_init_in_iov_from_pdu, 192 .init_out_iov_from_pdu = virtio_init_out_iov_from_pdu, 193 .push_and_notify = virtio_9p_push_and_notify, 194 }; 195 196 /* virtio-9p device */ 197 198 static const VMStateDescription vmstate_virtio_9p = { 199 .name = "virtio-9p", 200 .minimum_version_id = 1, 201 .version_id = 1, 202 .fields = (VMStateField[]) { 203 VMSTATE_VIRTIO_DEVICE, 204 VMSTATE_END_OF_LIST() 205 }, 206 }; 207 208 static Property virtio_9p_properties[] = { 209 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag), 210 DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id), 211 DEFINE_PROP_END_OF_LIST(), 212 }; 213 214 static void virtio_9p_class_init(ObjectClass *klass, void *data) 215 { 216 DeviceClass *dc = DEVICE_CLASS(klass); 217 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 218 219 dc->props = virtio_9p_properties; 220 dc->vmsd = &vmstate_virtio_9p; 221 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 222 vdc->realize = virtio_9p_device_realize; 223 vdc->unrealize = virtio_9p_device_unrealize; 224 vdc->get_features = virtio_9p_get_features; 225 vdc->get_config = virtio_9p_get_config; 226 vdc->reset = virtio_9p_reset; 227 } 228 229 static const TypeInfo virtio_device_info = { 230 .name = TYPE_VIRTIO_9P, 231 .parent = TYPE_VIRTIO_DEVICE, 232 .instance_size = sizeof(V9fsVirtioState), 233 .class_init = virtio_9p_class_init, 234 }; 235 236 static void virtio_9p_register_types(void) 237 { 238 type_register_static(&virtio_device_info); 239 } 240 241 type_init(virtio_9p_register_types) 242