]>
Commit | Line | Data |
---|---|---|
5f6f6664 NN |
1 | /* |
2 | * vhost-user | |
3 | * | |
4 | * Copyright (c) 2013 Virtual Open Systems Sarl. | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
7 | * See the COPYING file in the top-level directory. | |
8 | * | |
9 | */ | |
10 | ||
9b8bfe21 | 11 | #include "qemu/osdep.h" |
da34e65c | 12 | #include "qapi/error.h" |
5f6f6664 NN |
13 | #include "hw/virtio/vhost.h" |
14 | #include "hw/virtio/vhost-backend.h" | |
3e866365 | 15 | #include "hw/virtio/virtio-net.h" |
5f6f6664 NN |
16 | #include "sysemu/char.h" |
17 | #include "sysemu/kvm.h" | |
18 | #include "qemu/error-report.h" | |
19 | #include "qemu/sockets.h" | |
20 | ||
5f6f6664 NN |
21 | #include <sys/ioctl.h> |
22 | #include <sys/socket.h> | |
23 | #include <sys/un.h> | |
24 | #include <linux/vhost.h> | |
25 | ||
26 | #define VHOST_MEMORY_MAX_NREGIONS 8 | |
dcb10c00 | 27 | #define VHOST_USER_F_PROTOCOL_FEATURES 30 |
e2051e9e | 28 | |
de1372d4 TC |
29 | enum VhostUserProtocolFeature { |
30 | VHOST_USER_PROTOCOL_F_MQ = 0, | |
31 | VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1, | |
32 | VHOST_USER_PROTOCOL_F_RARP = 2, | |
ca525ce5 | 33 | VHOST_USER_PROTOCOL_F_REPLY_ACK = 3, |
c5f048d8 | 34 | VHOST_USER_PROTOCOL_F_NET_MTU = 4, |
de1372d4 TC |
35 | |
36 | VHOST_USER_PROTOCOL_F_MAX | |
37 | }; | |
38 | ||
39 | #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1) | |
5f6f6664 NN |
40 | |
41 | typedef enum VhostUserRequest { | |
42 | VHOST_USER_NONE = 0, | |
43 | VHOST_USER_GET_FEATURES = 1, | |
44 | VHOST_USER_SET_FEATURES = 2, | |
45 | VHOST_USER_SET_OWNER = 3, | |
60915dc4 | 46 | VHOST_USER_RESET_OWNER = 4, |
5f6f6664 NN |
47 | VHOST_USER_SET_MEM_TABLE = 5, |
48 | VHOST_USER_SET_LOG_BASE = 6, | |
49 | VHOST_USER_SET_LOG_FD = 7, | |
50 | VHOST_USER_SET_VRING_NUM = 8, | |
51 | VHOST_USER_SET_VRING_ADDR = 9, | |
52 | VHOST_USER_SET_VRING_BASE = 10, | |
53 | VHOST_USER_GET_VRING_BASE = 11, | |
54 | VHOST_USER_SET_VRING_KICK = 12, | |
55 | VHOST_USER_SET_VRING_CALL = 13, | |
56 | VHOST_USER_SET_VRING_ERR = 14, | |
dcb10c00 MT |
57 | VHOST_USER_GET_PROTOCOL_FEATURES = 15, |
58 | VHOST_USER_SET_PROTOCOL_FEATURES = 16, | |
e2051e9e | 59 | VHOST_USER_GET_QUEUE_NUM = 17, |
7263a0ad | 60 | VHOST_USER_SET_VRING_ENABLE = 18, |
3e866365 | 61 | VHOST_USER_SEND_RARP = 19, |
c5f048d8 | 62 | VHOST_USER_NET_SET_MTU = 20, |
5f6f6664 NN |
63 | VHOST_USER_MAX |
64 | } VhostUserRequest; | |
65 | ||
66 | typedef struct VhostUserMemoryRegion { | |
67 | uint64_t guest_phys_addr; | |
68 | uint64_t memory_size; | |
69 | uint64_t userspace_addr; | |
3fd74b84 | 70 | uint64_t mmap_offset; |
5f6f6664 NN |
71 | } VhostUserMemoryRegion; |
72 | ||
73 | typedef struct VhostUserMemory { | |
74 | uint32_t nregions; | |
75 | uint32_t padding; | |
76 | VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS]; | |
77 | } VhostUserMemory; | |
78 | ||
2b8819c6 VK |
79 | typedef struct VhostUserLog { |
80 | uint64_t mmap_size; | |
81 | uint64_t mmap_offset; | |
82 | } VhostUserLog; | |
83 | ||
5f6f6664 NN |
84 | typedef struct VhostUserMsg { |
85 | VhostUserRequest request; | |
86 | ||
87 | #define VHOST_USER_VERSION_MASK (0x3) | |
88 | #define VHOST_USER_REPLY_MASK (0x1<<2) | |
ca525ce5 | 89 | #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3) |
5f6f6664 NN |
90 | uint32_t flags; |
91 | uint32_t size; /* the following payload size */ | |
92 | union { | |
93 | #define VHOST_USER_VRING_IDX_MASK (0xff) | |
94 | #define VHOST_USER_VRING_NOFD_MASK (0x1<<8) | |
95 | uint64_t u64; | |
96 | struct vhost_vring_state state; | |
97 | struct vhost_vring_addr addr; | |
98 | VhostUserMemory memory; | |
2b8819c6 | 99 | VhostUserLog log; |
7f4a930e | 100 | } payload; |
5f6f6664 NN |
101 | } QEMU_PACKED VhostUserMsg; |
102 | ||
103 | static VhostUserMsg m __attribute__ ((unused)); | |
104 | #define VHOST_USER_HDR_SIZE (sizeof(m.request) \ | |
105 | + sizeof(m.flags) \ | |
106 | + sizeof(m.size)) | |
107 | ||
108 | #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE) | |
109 | ||
110 | /* The version of the protocol we support */ | |
111 | #define VHOST_USER_VERSION (0x1) | |
112 | ||
2152f3fe MAL |
113 | struct vhost_user { |
114 | CharBackend *chr; | |
115 | }; | |
116 | ||
5f6f6664 NN |
117 | static bool ioeventfd_enabled(void) |
118 | { | |
119 | return kvm_enabled() && kvm_eventfds_enabled(); | |
120 | } | |
121 | ||
5f6f6664 NN |
122 | static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg) |
123 | { | |
2152f3fe MAL |
124 | struct vhost_user *u = dev->opaque; |
125 | CharBackend *chr = u->chr; | |
5f6f6664 NN |
126 | uint8_t *p = (uint8_t *) msg; |
127 | int r, size = VHOST_USER_HDR_SIZE; | |
128 | ||
129 | r = qemu_chr_fe_read_all(chr, p, size); | |
130 | if (r != size) { | |
5421f318 MT |
131 | error_report("Failed to read msg header. Read %d instead of %d." |
132 | " Original request %d.", r, size, msg->request); | |
5f6f6664 NN |
133 | goto fail; |
134 | } | |
135 | ||
136 | /* validate received flags */ | |
137 | if (msg->flags != (VHOST_USER_REPLY_MASK | VHOST_USER_VERSION)) { | |
138 | error_report("Failed to read msg header." | |
ab7c5aaf | 139 | " Flags 0x%x instead of 0x%x.", msg->flags, |
5f6f6664 NN |
140 | VHOST_USER_REPLY_MASK | VHOST_USER_VERSION); |
141 | goto fail; | |
142 | } | |
143 | ||
144 | /* validate message size is sane */ | |
145 | if (msg->size > VHOST_USER_PAYLOAD_SIZE) { | |
146 | error_report("Failed to read msg header." | |
ab7c5aaf | 147 | " Size %d exceeds the maximum %zu.", msg->size, |
5f6f6664 NN |
148 | VHOST_USER_PAYLOAD_SIZE); |
149 | goto fail; | |
150 | } | |
151 | ||
152 | if (msg->size) { | |
153 | p += VHOST_USER_HDR_SIZE; | |
154 | size = msg->size; | |
155 | r = qemu_chr_fe_read_all(chr, p, size); | |
156 | if (r != size) { | |
157 | error_report("Failed to read msg payload." | |
ab7c5aaf | 158 | " Read %d instead of %d.", r, msg->size); |
5f6f6664 NN |
159 | goto fail; |
160 | } | |
161 | } | |
162 | ||
163 | return 0; | |
164 | ||
165 | fail: | |
166 | return -1; | |
167 | } | |
168 | ||
ca525ce5 | 169 | static int process_message_reply(struct vhost_dev *dev, |
3cf7daf8 | 170 | const VhostUserMsg *msg) |
ca525ce5 | 171 | { |
60cd1102 | 172 | VhostUserMsg msg_reply; |
ca525ce5 | 173 | |
3cf7daf8 | 174 | if ((msg->flags & VHOST_USER_NEED_REPLY_MASK) == 0) { |
60cd1102 ZY |
175 | return 0; |
176 | } | |
177 | ||
178 | if (vhost_user_read(dev, &msg_reply) < 0) { | |
ca525ce5 PS |
179 | return -1; |
180 | } | |
181 | ||
3cf7daf8 | 182 | if (msg_reply.request != msg->request) { |
ca525ce5 PS |
183 | error_report("Received unexpected msg type." |
184 | "Expected %d received %d", | |
3cf7daf8 | 185 | msg->request, msg_reply.request); |
ca525ce5 PS |
186 | return -1; |
187 | } | |
188 | ||
60cd1102 | 189 | return msg_reply.payload.u64 ? -1 : 0; |
ca525ce5 PS |
190 | } |
191 | ||
21e70425 MAL |
192 | static bool vhost_user_one_time_request(VhostUserRequest request) |
193 | { | |
194 | switch (request) { | |
195 | case VHOST_USER_SET_OWNER: | |
60915dc4 | 196 | case VHOST_USER_RESET_OWNER: |
21e70425 MAL |
197 | case VHOST_USER_SET_MEM_TABLE: |
198 | case VHOST_USER_GET_QUEUE_NUM: | |
c5f048d8 | 199 | case VHOST_USER_NET_SET_MTU: |
21e70425 MAL |
200 | return true; |
201 | default: | |
202 | return false; | |
203 | } | |
204 | } | |
205 | ||
206 | /* most non-init callers ignore the error */ | |
5f6f6664 NN |
207 | static int vhost_user_write(struct vhost_dev *dev, VhostUserMsg *msg, |
208 | int *fds, int fd_num) | |
209 | { | |
2152f3fe MAL |
210 | struct vhost_user *u = dev->opaque; |
211 | CharBackend *chr = u->chr; | |
f6b85710 | 212 | int ret, size = VHOST_USER_HDR_SIZE + msg->size; |
5f6f6664 | 213 | |
21e70425 MAL |
214 | /* |
215 | * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE, | |
216 | * we just need send it once in the first time. For later such | |
217 | * request, we just ignore it. | |
218 | */ | |
219 | if (vhost_user_one_time_request(msg->request) && dev->vq_index != 0) { | |
60cd1102 | 220 | msg->flags &= ~VHOST_USER_NEED_REPLY_MASK; |
21e70425 MAL |
221 | return 0; |
222 | } | |
223 | ||
6fab2f3f | 224 | if (qemu_chr_fe_set_msgfds(chr, fds, fd_num) < 0) { |
f6b85710 | 225 | error_report("Failed to set msg fds."); |
6fab2f3f MAL |
226 | return -1; |
227 | } | |
5f6f6664 | 228 | |
f6b85710 MAL |
229 | ret = qemu_chr_fe_write_all(chr, (const uint8_t *) msg, size); |
230 | if (ret != size) { | |
231 | error_report("Failed to write msg." | |
232 | " Wrote %d instead of %d.", ret, size); | |
233 | return -1; | |
234 | } | |
235 | ||
236 | return 0; | |
5f6f6664 NN |
237 | } |
238 | ||
21e70425 MAL |
239 | static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base, |
240 | struct vhost_log *log) | |
b931bfbf | 241 | { |
21e70425 MAL |
242 | int fds[VHOST_MEMORY_MAX_NREGIONS]; |
243 | size_t fd_num = 0; | |
244 | bool shmfd = virtio_has_feature(dev->protocol_features, | |
245 | VHOST_USER_PROTOCOL_F_LOG_SHMFD); | |
246 | VhostUserMsg msg = { | |
247 | .request = VHOST_USER_SET_LOG_BASE, | |
248 | .flags = VHOST_USER_VERSION, | |
48854f57 | 249 | .payload.log.mmap_size = log->size * sizeof(*(log->log)), |
2b8819c6 VK |
250 | .payload.log.mmap_offset = 0, |
251 | .size = sizeof(msg.payload.log), | |
21e70425 MAL |
252 | }; |
253 | ||
254 | if (shmfd && log->fd != -1) { | |
255 | fds[fd_num++] = log->fd; | |
256 | } | |
257 | ||
c4843a45 MAL |
258 | if (vhost_user_write(dev, &msg, fds, fd_num) < 0) { |
259 | return -1; | |
260 | } | |
21e70425 MAL |
261 | |
262 | if (shmfd) { | |
263 | msg.size = 0; | |
264 | if (vhost_user_read(dev, &msg) < 0) { | |
c4843a45 | 265 | return -1; |
21e70425 MAL |
266 | } |
267 | ||
268 | if (msg.request != VHOST_USER_SET_LOG_BASE) { | |
269 | error_report("Received unexpected msg type. " | |
270 | "Expected %d received %d", | |
271 | VHOST_USER_SET_LOG_BASE, msg.request); | |
272 | return -1; | |
273 | } | |
b931bfbf | 274 | } |
21e70425 MAL |
275 | |
276 | return 0; | |
b931bfbf CO |
277 | } |
278 | ||
94c9cb31 MT |
279 | static int vhost_user_set_mem_table(struct vhost_dev *dev, |
280 | struct vhost_memory *mem) | |
281 | { | |
282 | int fds[VHOST_MEMORY_MAX_NREGIONS]; | |
283 | int i, fd; | |
284 | size_t fd_num = 0; | |
285 | bool reply_supported = virtio_has_feature(dev->protocol_features, | |
286 | VHOST_USER_PROTOCOL_F_REPLY_ACK); | |
287 | ||
288 | VhostUserMsg msg = { | |
289 | .request = VHOST_USER_SET_MEM_TABLE, | |
290 | .flags = VHOST_USER_VERSION, | |
291 | }; | |
292 | ||
293 | if (reply_supported) { | |
294 | msg.flags |= VHOST_USER_NEED_REPLY_MASK; | |
295 | } | |
296 | ||
297 | for (i = 0; i < dev->mem->nregions; ++i) { | |
298 | struct vhost_memory_region *reg = dev->mem->regions + i; | |
299 | ram_addr_t offset; | |
300 | MemoryRegion *mr; | |
301 | ||
302 | assert((uintptr_t)reg->userspace_addr == reg->userspace_addr); | |
303 | mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr, | |
304 | &offset); | |
305 | fd = memory_region_get_fd(mr); | |
306 | if (fd > 0) { | |
307 | msg.payload.memory.regions[fd_num].userspace_addr = reg->userspace_addr; | |
308 | msg.payload.memory.regions[fd_num].memory_size = reg->memory_size; | |
309 | msg.payload.memory.regions[fd_num].guest_phys_addr = reg->guest_phys_addr; | |
310 | msg.payload.memory.regions[fd_num].mmap_offset = offset; | |
311 | assert(fd_num < VHOST_MEMORY_MAX_NREGIONS); | |
312 | fds[fd_num++] = fd; | |
313 | } | |
314 | } | |
315 | ||
316 | msg.payload.memory.nregions = fd_num; | |
317 | ||
318 | if (!fd_num) { | |
319 | error_report("Failed initializing vhost-user memory map, " | |
320 | "consider using -object memory-backend-file share=on"); | |
321 | return -1; | |
322 | } | |
323 | ||
324 | msg.size = sizeof(msg.payload.memory.nregions); | |
325 | msg.size += sizeof(msg.payload.memory.padding); | |
326 | msg.size += fd_num * sizeof(VhostUserMemoryRegion); | |
327 | ||
328 | if (vhost_user_write(dev, &msg, fds, fd_num) < 0) { | |
329 | return -1; | |
330 | } | |
331 | ||
332 | if (reply_supported) { | |
3cf7daf8 | 333 | return process_message_reply(dev, &msg); |
94c9cb31 MT |
334 | } |
335 | ||
336 | return 0; | |
337 | } | |
338 | ||
21e70425 MAL |
339 | static int vhost_user_set_vring_addr(struct vhost_dev *dev, |
340 | struct vhost_vring_addr *addr) | |
341 | { | |
342 | VhostUserMsg msg = { | |
343 | .request = VHOST_USER_SET_VRING_ADDR, | |
344 | .flags = VHOST_USER_VERSION, | |
7f4a930e | 345 | .payload.addr = *addr, |
7fc0246c | 346 | .size = sizeof(msg.payload.addr), |
21e70425 | 347 | }; |
5f6f6664 | 348 | |
c4843a45 MAL |
349 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { |
350 | return -1; | |
351 | } | |
5f6f6664 | 352 | |
21e70425 MAL |
353 | return 0; |
354 | } | |
5f6f6664 | 355 | |
21e70425 MAL |
356 | static int vhost_user_set_vring_endian(struct vhost_dev *dev, |
357 | struct vhost_vring_state *ring) | |
358 | { | |
359 | error_report("vhost-user trying to send unhandled ioctl"); | |
360 | return -1; | |
361 | } | |
5f6f6664 | 362 | |
21e70425 MAL |
363 | static int vhost_set_vring(struct vhost_dev *dev, |
364 | unsigned long int request, | |
365 | struct vhost_vring_state *ring) | |
366 | { | |
367 | VhostUserMsg msg = { | |
368 | .request = request, | |
369 | .flags = VHOST_USER_VERSION, | |
7f4a930e | 370 | .payload.state = *ring, |
7fc0246c | 371 | .size = sizeof(msg.payload.state), |
21e70425 MAL |
372 | }; |
373 | ||
c4843a45 MAL |
374 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { |
375 | return -1; | |
376 | } | |
21e70425 MAL |
377 | |
378 | return 0; | |
379 | } | |
380 | ||
381 | static int vhost_user_set_vring_num(struct vhost_dev *dev, | |
382 | struct vhost_vring_state *ring) | |
383 | { | |
384 | return vhost_set_vring(dev, VHOST_USER_SET_VRING_NUM, ring); | |
385 | } | |
386 | ||
387 | static int vhost_user_set_vring_base(struct vhost_dev *dev, | |
388 | struct vhost_vring_state *ring) | |
389 | { | |
390 | return vhost_set_vring(dev, VHOST_USER_SET_VRING_BASE, ring); | |
391 | } | |
392 | ||
393 | static int vhost_user_set_vring_enable(struct vhost_dev *dev, int enable) | |
394 | { | |
dc3db6ad | 395 | int i; |
21e70425 | 396 | |
923e2d98 | 397 | if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) { |
5f6f6664 | 398 | return -1; |
5f6f6664 NN |
399 | } |
400 | ||
dc3db6ad MT |
401 | for (i = 0; i < dev->nvqs; ++i) { |
402 | struct vhost_vring_state state = { | |
403 | .index = dev->vq_index + i, | |
404 | .num = enable, | |
405 | }; | |
406 | ||
407 | vhost_set_vring(dev, VHOST_USER_SET_VRING_ENABLE, &state); | |
408 | } | |
21e70425 | 409 | |
dc3db6ad MT |
410 | return 0; |
411 | } | |
21e70425 MAL |
412 | |
413 | static int vhost_user_get_vring_base(struct vhost_dev *dev, | |
414 | struct vhost_vring_state *ring) | |
415 | { | |
416 | VhostUserMsg msg = { | |
417 | .request = VHOST_USER_GET_VRING_BASE, | |
418 | .flags = VHOST_USER_VERSION, | |
7f4a930e | 419 | .payload.state = *ring, |
7fc0246c | 420 | .size = sizeof(msg.payload.state), |
21e70425 MAL |
421 | }; |
422 | ||
c4843a45 MAL |
423 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { |
424 | return -1; | |
425 | } | |
21e70425 MAL |
426 | |
427 | if (vhost_user_read(dev, &msg) < 0) { | |
c4843a45 | 428 | return -1; |
5f6f6664 NN |
429 | } |
430 | ||
21e70425 MAL |
431 | if (msg.request != VHOST_USER_GET_VRING_BASE) { |
432 | error_report("Received unexpected msg type. Expected %d received %d", | |
433 | VHOST_USER_GET_VRING_BASE, msg.request); | |
434 | return -1; | |
435 | } | |
5f6f6664 | 436 | |
86abad0f | 437 | if (msg.size != sizeof(msg.payload.state)) { |
21e70425 MAL |
438 | error_report("Received bad msg size."); |
439 | return -1; | |
5f6f6664 NN |
440 | } |
441 | ||
7f4a930e | 442 | *ring = msg.payload.state; |
21e70425 | 443 | |
5f6f6664 NN |
444 | return 0; |
445 | } | |
446 | ||
21e70425 MAL |
447 | static int vhost_set_vring_file(struct vhost_dev *dev, |
448 | VhostUserRequest request, | |
449 | struct vhost_vring_file *file) | |
c2bea314 | 450 | { |
9a78a5dd MAL |
451 | int fds[VHOST_MEMORY_MAX_NREGIONS]; |
452 | size_t fd_num = 0; | |
c2bea314 | 453 | VhostUserMsg msg = { |
21e70425 | 454 | .request = request, |
c2bea314 | 455 | .flags = VHOST_USER_VERSION, |
7f4a930e | 456 | .payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK, |
86abad0f | 457 | .size = sizeof(msg.payload.u64), |
c2bea314 MAL |
458 | }; |
459 | ||
21e70425 MAL |
460 | if (ioeventfd_enabled() && file->fd > 0) { |
461 | fds[fd_num++] = file->fd; | |
462 | } else { | |
7f4a930e | 463 | msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK; |
9a78a5dd MAL |
464 | } |
465 | ||
c4843a45 MAL |
466 | if (vhost_user_write(dev, &msg, fds, fd_num) < 0) { |
467 | return -1; | |
468 | } | |
9a78a5dd | 469 | |
21e70425 MAL |
470 | return 0; |
471 | } | |
9a78a5dd | 472 | |
21e70425 MAL |
473 | static int vhost_user_set_vring_kick(struct vhost_dev *dev, |
474 | struct vhost_vring_file *file) | |
475 | { | |
476 | return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_KICK, file); | |
477 | } | |
478 | ||
479 | static int vhost_user_set_vring_call(struct vhost_dev *dev, | |
480 | struct vhost_vring_file *file) | |
481 | { | |
482 | return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_CALL, file); | |
483 | } | |
484 | ||
485 | static int vhost_user_set_u64(struct vhost_dev *dev, int request, uint64_t u64) | |
486 | { | |
487 | VhostUserMsg msg = { | |
488 | .request = request, | |
489 | .flags = VHOST_USER_VERSION, | |
7f4a930e | 490 | .payload.u64 = u64, |
86abad0f | 491 | .size = sizeof(msg.payload.u64), |
21e70425 MAL |
492 | }; |
493 | ||
c4843a45 MAL |
494 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { |
495 | return -1; | |
496 | } | |
21e70425 MAL |
497 | |
498 | return 0; | |
499 | } | |
500 | ||
501 | static int vhost_user_set_features(struct vhost_dev *dev, | |
502 | uint64_t features) | |
503 | { | |
504 | return vhost_user_set_u64(dev, VHOST_USER_SET_FEATURES, features); | |
505 | } | |
506 | ||
507 | static int vhost_user_set_protocol_features(struct vhost_dev *dev, | |
508 | uint64_t features) | |
509 | { | |
510 | return vhost_user_set_u64(dev, VHOST_USER_SET_PROTOCOL_FEATURES, features); | |
511 | } | |
512 | ||
513 | static int vhost_user_get_u64(struct vhost_dev *dev, int request, uint64_t *u64) | |
514 | { | |
515 | VhostUserMsg msg = { | |
516 | .request = request, | |
517 | .flags = VHOST_USER_VERSION, | |
518 | }; | |
519 | ||
520 | if (vhost_user_one_time_request(request) && dev->vq_index != 0) { | |
521 | return 0; | |
9a78a5dd | 522 | } |
c2bea314 | 523 | |
c4843a45 MAL |
524 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { |
525 | return -1; | |
526 | } | |
21e70425 MAL |
527 | |
528 | if (vhost_user_read(dev, &msg) < 0) { | |
c4843a45 | 529 | return -1; |
21e70425 MAL |
530 | } |
531 | ||
532 | if (msg.request != request) { | |
533 | error_report("Received unexpected msg type. Expected %d received %d", | |
534 | request, msg.request); | |
535 | return -1; | |
536 | } | |
537 | ||
86abad0f | 538 | if (msg.size != sizeof(msg.payload.u64)) { |
21e70425 MAL |
539 | error_report("Received bad msg size."); |
540 | return -1; | |
541 | } | |
542 | ||
7f4a930e | 543 | *u64 = msg.payload.u64; |
21e70425 MAL |
544 | |
545 | return 0; | |
546 | } | |
547 | ||
548 | static int vhost_user_get_features(struct vhost_dev *dev, uint64_t *features) | |
549 | { | |
550 | return vhost_user_get_u64(dev, VHOST_USER_GET_FEATURES, features); | |
551 | } | |
552 | ||
553 | static int vhost_user_set_owner(struct vhost_dev *dev) | |
554 | { | |
555 | VhostUserMsg msg = { | |
556 | .request = VHOST_USER_SET_OWNER, | |
557 | .flags = VHOST_USER_VERSION, | |
558 | }; | |
559 | ||
c4843a45 MAL |
560 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { |
561 | return -1; | |
562 | } | |
21e70425 MAL |
563 | |
564 | return 0; | |
565 | } | |
566 | ||
567 | static int vhost_user_reset_device(struct vhost_dev *dev) | |
568 | { | |
569 | VhostUserMsg msg = { | |
60915dc4 | 570 | .request = VHOST_USER_RESET_OWNER, |
21e70425 MAL |
571 | .flags = VHOST_USER_VERSION, |
572 | }; | |
573 | ||
c4843a45 MAL |
574 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { |
575 | return -1; | |
576 | } | |
21e70425 | 577 | |
c2bea314 MAL |
578 | return 0; |
579 | } | |
580 | ||
5f6f6664 NN |
581 | static int vhost_user_init(struct vhost_dev *dev, void *opaque) |
582 | { | |
21e70425 | 583 | uint64_t features; |
2152f3fe | 584 | struct vhost_user *u; |
dcb10c00 MT |
585 | int err; |
586 | ||
5f6f6664 NN |
587 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); |
588 | ||
2152f3fe MAL |
589 | u = g_new0(struct vhost_user, 1); |
590 | u->chr = opaque; | |
591 | dev->opaque = u; | |
5f6f6664 | 592 | |
21e70425 | 593 | err = vhost_user_get_features(dev, &features); |
dcb10c00 MT |
594 | if (err < 0) { |
595 | return err; | |
596 | } | |
597 | ||
598 | if (virtio_has_feature(features, VHOST_USER_F_PROTOCOL_FEATURES)) { | |
599 | dev->backend_features |= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES; | |
600 | ||
21e70425 MAL |
601 | err = vhost_user_get_u64(dev, VHOST_USER_GET_PROTOCOL_FEATURES, |
602 | &features); | |
dcb10c00 MT |
603 | if (err < 0) { |
604 | return err; | |
605 | } | |
606 | ||
607 | dev->protocol_features = features & VHOST_USER_PROTOCOL_FEATURE_MASK; | |
21e70425 | 608 | err = vhost_user_set_protocol_features(dev, dev->protocol_features); |
dcb10c00 MT |
609 | if (err < 0) { |
610 | return err; | |
611 | } | |
e2051e9e YL |
612 | |
613 | /* query the max queues we support if backend supports Multiple Queue */ | |
614 | if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)) { | |
21e70425 MAL |
615 | err = vhost_user_get_u64(dev, VHOST_USER_GET_QUEUE_NUM, |
616 | &dev->max_queues); | |
e2051e9e YL |
617 | if (err < 0) { |
618 | return err; | |
619 | } | |
620 | } | |
dcb10c00 MT |
621 | } |
622 | ||
d2fc4402 MAL |
623 | if (dev->migration_blocker == NULL && |
624 | !virtio_has_feature(dev->protocol_features, | |
625 | VHOST_USER_PROTOCOL_F_LOG_SHMFD)) { | |
626 | error_setg(&dev->migration_blocker, | |
627 | "Migration disabled: vhost-user backend lacks " | |
628 | "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature."); | |
629 | } | |
630 | ||
5f6f6664 NN |
631 | return 0; |
632 | } | |
633 | ||
634 | static int vhost_user_cleanup(struct vhost_dev *dev) | |
635 | { | |
2152f3fe MAL |
636 | struct vhost_user *u; |
637 | ||
5f6f6664 NN |
638 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); |
639 | ||
2152f3fe MAL |
640 | u = dev->opaque; |
641 | g_free(u); | |
5f6f6664 NN |
642 | dev->opaque = 0; |
643 | ||
644 | return 0; | |
645 | } | |
646 | ||
fc57fd99 YL |
647 | static int vhost_user_get_vq_index(struct vhost_dev *dev, int idx) |
648 | { | |
649 | assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs); | |
650 | ||
651 | return idx; | |
652 | } | |
653 | ||
2ce68e4c IM |
654 | static int vhost_user_memslots_limit(struct vhost_dev *dev) |
655 | { | |
656 | return VHOST_MEMORY_MAX_NREGIONS; | |
657 | } | |
658 | ||
1be0ac21 MAL |
659 | static bool vhost_user_requires_shm_log(struct vhost_dev *dev) |
660 | { | |
661 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); | |
662 | ||
663 | return virtio_has_feature(dev->protocol_features, | |
664 | VHOST_USER_PROTOCOL_F_LOG_SHMFD); | |
665 | } | |
666 | ||
3e866365 TC |
667 | static int vhost_user_migration_done(struct vhost_dev *dev, char* mac_addr) |
668 | { | |
669 | VhostUserMsg msg = { 0 }; | |
3e866365 TC |
670 | |
671 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); | |
672 | ||
673 | /* If guest supports GUEST_ANNOUNCE do nothing */ | |
674 | if (virtio_has_feature(dev->acked_features, VIRTIO_NET_F_GUEST_ANNOUNCE)) { | |
675 | return 0; | |
676 | } | |
677 | ||
678 | /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */ | |
679 | if (virtio_has_feature(dev->protocol_features, | |
680 | VHOST_USER_PROTOCOL_F_RARP)) { | |
681 | msg.request = VHOST_USER_SEND_RARP; | |
682 | msg.flags = VHOST_USER_VERSION; | |
7f4a930e | 683 | memcpy((char *)&msg.payload.u64, mac_addr, 6); |
86abad0f | 684 | msg.size = sizeof(msg.payload.u64); |
3e866365 | 685 | |
c4843a45 | 686 | return vhost_user_write(dev, &msg, NULL, 0); |
3e866365 TC |
687 | } |
688 | return -1; | |
689 | } | |
690 | ||
ffe42cc1 MT |
691 | static bool vhost_user_can_merge(struct vhost_dev *dev, |
692 | uint64_t start1, uint64_t size1, | |
693 | uint64_t start2, uint64_t size2) | |
694 | { | |
07bdaa41 | 695 | ram_addr_t offset; |
ffe42cc1 MT |
696 | int mfd, rfd; |
697 | MemoryRegion *mr; | |
698 | ||
07bdaa41 | 699 | mr = memory_region_from_host((void *)(uintptr_t)start1, &offset); |
4ff87573 | 700 | mfd = memory_region_get_fd(mr); |
ffe42cc1 | 701 | |
07bdaa41 | 702 | mr = memory_region_from_host((void *)(uintptr_t)start2, &offset); |
4ff87573 | 703 | rfd = memory_region_get_fd(mr); |
ffe42cc1 MT |
704 | |
705 | return mfd == rfd; | |
706 | } | |
707 | ||
c5f048d8 MC |
708 | static int vhost_user_net_set_mtu(struct vhost_dev *dev, uint16_t mtu) |
709 | { | |
710 | VhostUserMsg msg; | |
711 | bool reply_supported = virtio_has_feature(dev->protocol_features, | |
712 | VHOST_USER_PROTOCOL_F_REPLY_ACK); | |
713 | ||
714 | if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU))) { | |
715 | return 0; | |
716 | } | |
717 | ||
718 | msg.request = VHOST_USER_NET_SET_MTU; | |
719 | msg.payload.u64 = mtu; | |
720 | msg.size = sizeof(msg.payload.u64); | |
721 | msg.flags = VHOST_USER_VERSION; | |
722 | if (reply_supported) { | |
723 | msg.flags |= VHOST_USER_NEED_REPLY_MASK; | |
724 | } | |
725 | ||
726 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { | |
727 | return -1; | |
728 | } | |
729 | ||
730 | /* If reply_ack supported, slave has to ack specified MTU is valid */ | |
731 | if (reply_supported) { | |
3cf7daf8 | 732 | return process_message_reply(dev, &msg); |
c5f048d8 MC |
733 | } |
734 | ||
735 | return 0; | |
736 | } | |
737 | ||
5f6f6664 NN |
738 | const VhostOps user_ops = { |
739 | .backend_type = VHOST_BACKEND_TYPE_USER, | |
5f6f6664 | 740 | .vhost_backend_init = vhost_user_init, |
fc57fd99 | 741 | .vhost_backend_cleanup = vhost_user_cleanup, |
2ce68e4c | 742 | .vhost_backend_memslots_limit = vhost_user_memslots_limit, |
21e70425 MAL |
743 | .vhost_set_log_base = vhost_user_set_log_base, |
744 | .vhost_set_mem_table = vhost_user_set_mem_table, | |
745 | .vhost_set_vring_addr = vhost_user_set_vring_addr, | |
746 | .vhost_set_vring_endian = vhost_user_set_vring_endian, | |
747 | .vhost_set_vring_num = vhost_user_set_vring_num, | |
748 | .vhost_set_vring_base = vhost_user_set_vring_base, | |
749 | .vhost_get_vring_base = vhost_user_get_vring_base, | |
750 | .vhost_set_vring_kick = vhost_user_set_vring_kick, | |
751 | .vhost_set_vring_call = vhost_user_set_vring_call, | |
752 | .vhost_set_features = vhost_user_set_features, | |
753 | .vhost_get_features = vhost_user_get_features, | |
754 | .vhost_set_owner = vhost_user_set_owner, | |
755 | .vhost_reset_device = vhost_user_reset_device, | |
756 | .vhost_get_vq_index = vhost_user_get_vq_index, | |
757 | .vhost_set_vring_enable = vhost_user_set_vring_enable, | |
1be0ac21 | 758 | .vhost_requires_shm_log = vhost_user_requires_shm_log, |
3e866365 | 759 | .vhost_migration_done = vhost_user_migration_done, |
ffe42cc1 | 760 | .vhost_backend_can_merge = vhost_user_can_merge, |
c5f048d8 | 761 | .vhost_net_set_mtu = vhost_user_net_set_mtu, |
fc57fd99 | 762 | }; |