]>
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" |
4d43a603 | 16 | #include "chardev/char-fe.h" |
5f6f6664 NN |
17 | #include "sysemu/kvm.h" |
18 | #include "qemu/error-report.h" | |
19 | #include "qemu/sockets.h" | |
efbfeb81 | 20 | #include "sysemu/cryptodev.h" |
9ccbfe14 DDAG |
21 | #include "migration/migration.h" |
22 | #include "migration/postcopy-ram.h" | |
6864a7b5 | 23 | #include "trace.h" |
5f6f6664 | 24 | |
5f6f6664 NN |
25 | #include <sys/ioctl.h> |
26 | #include <sys/socket.h> | |
27 | #include <sys/un.h> | |
28 | #include <linux/vhost.h> | |
375318d0 | 29 | #include <linux/userfaultfd.h> |
5f6f6664 NN |
30 | |
31 | #define VHOST_MEMORY_MAX_NREGIONS 8 | |
dcb10c00 | 32 | #define VHOST_USER_F_PROTOCOL_FEATURES 30 |
e2051e9e | 33 | |
4c3e257b CL |
34 | /* |
35 | * Maximum size of virtio device config space | |
36 | */ | |
37 | #define VHOST_USER_MAX_CONFIG_SIZE 256 | |
38 | ||
de1372d4 TC |
39 | enum VhostUserProtocolFeature { |
40 | VHOST_USER_PROTOCOL_F_MQ = 0, | |
41 | VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1, | |
42 | VHOST_USER_PROTOCOL_F_RARP = 2, | |
ca525ce5 | 43 | VHOST_USER_PROTOCOL_F_REPLY_ACK = 3, |
c5f048d8 | 44 | VHOST_USER_PROTOCOL_F_NET_MTU = 4, |
4bbeeba0 | 45 | VHOST_USER_PROTOCOL_F_SLAVE_REQ = 5, |
5df04f17 | 46 | VHOST_USER_PROTOCOL_F_CROSS_ENDIAN = 6, |
efbfeb81 | 47 | VHOST_USER_PROTOCOL_F_CRYPTO_SESSION = 7, |
9ccbfe14 | 48 | VHOST_USER_PROTOCOL_F_PAGEFAULT = 8, |
de1372d4 TC |
49 | VHOST_USER_PROTOCOL_F_MAX |
50 | }; | |
51 | ||
52 | #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1) | |
5f6f6664 NN |
53 | |
54 | typedef enum VhostUserRequest { | |
55 | VHOST_USER_NONE = 0, | |
56 | VHOST_USER_GET_FEATURES = 1, | |
57 | VHOST_USER_SET_FEATURES = 2, | |
58 | VHOST_USER_SET_OWNER = 3, | |
60915dc4 | 59 | VHOST_USER_RESET_OWNER = 4, |
5f6f6664 NN |
60 | VHOST_USER_SET_MEM_TABLE = 5, |
61 | VHOST_USER_SET_LOG_BASE = 6, | |
62 | VHOST_USER_SET_LOG_FD = 7, | |
63 | VHOST_USER_SET_VRING_NUM = 8, | |
64 | VHOST_USER_SET_VRING_ADDR = 9, | |
65 | VHOST_USER_SET_VRING_BASE = 10, | |
66 | VHOST_USER_GET_VRING_BASE = 11, | |
67 | VHOST_USER_SET_VRING_KICK = 12, | |
68 | VHOST_USER_SET_VRING_CALL = 13, | |
69 | VHOST_USER_SET_VRING_ERR = 14, | |
dcb10c00 MT |
70 | VHOST_USER_GET_PROTOCOL_FEATURES = 15, |
71 | VHOST_USER_SET_PROTOCOL_FEATURES = 16, | |
e2051e9e | 72 | VHOST_USER_GET_QUEUE_NUM = 17, |
7263a0ad | 73 | VHOST_USER_SET_VRING_ENABLE = 18, |
3e866365 | 74 | VHOST_USER_SEND_RARP = 19, |
c5f048d8 | 75 | VHOST_USER_NET_SET_MTU = 20, |
4bbeeba0 | 76 | VHOST_USER_SET_SLAVE_REQ_FD = 21, |
6dcdd06e | 77 | VHOST_USER_IOTLB_MSG = 22, |
5df04f17 | 78 | VHOST_USER_SET_VRING_ENDIAN = 23, |
4c3e257b CL |
79 | VHOST_USER_GET_CONFIG = 24, |
80 | VHOST_USER_SET_CONFIG = 25, | |
efbfeb81 GA |
81 | VHOST_USER_CREATE_CRYPTO_SESSION = 26, |
82 | VHOST_USER_CLOSE_CRYPTO_SESSION = 27, | |
d3dff7a5 | 83 | VHOST_USER_POSTCOPY_ADVISE = 28, |
6864a7b5 | 84 | VHOST_USER_POSTCOPY_LISTEN = 29, |
c639187e | 85 | VHOST_USER_POSTCOPY_END = 30, |
5f6f6664 NN |
86 | VHOST_USER_MAX |
87 | } VhostUserRequest; | |
88 | ||
4bbeeba0 MAL |
89 | typedef enum VhostUserSlaveRequest { |
90 | VHOST_USER_SLAVE_NONE = 0, | |
6dcdd06e | 91 | VHOST_USER_SLAVE_IOTLB_MSG = 1, |
4c3e257b | 92 | VHOST_USER_SLAVE_CONFIG_CHANGE_MSG = 2, |
4bbeeba0 MAL |
93 | VHOST_USER_SLAVE_MAX |
94 | } VhostUserSlaveRequest; | |
95 | ||
5f6f6664 NN |
96 | typedef struct VhostUserMemoryRegion { |
97 | uint64_t guest_phys_addr; | |
98 | uint64_t memory_size; | |
99 | uint64_t userspace_addr; | |
3fd74b84 | 100 | uint64_t mmap_offset; |
5f6f6664 NN |
101 | } VhostUserMemoryRegion; |
102 | ||
103 | typedef struct VhostUserMemory { | |
104 | uint32_t nregions; | |
105 | uint32_t padding; | |
106 | VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS]; | |
107 | } VhostUserMemory; | |
108 | ||
2b8819c6 VK |
109 | typedef struct VhostUserLog { |
110 | uint64_t mmap_size; | |
111 | uint64_t mmap_offset; | |
112 | } VhostUserLog; | |
113 | ||
4c3e257b CL |
114 | typedef struct VhostUserConfig { |
115 | uint32_t offset; | |
116 | uint32_t size; | |
117 | uint32_t flags; | |
118 | uint8_t region[VHOST_USER_MAX_CONFIG_SIZE]; | |
119 | } VhostUserConfig; | |
120 | ||
efbfeb81 GA |
121 | #define VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN 512 |
122 | #define VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN 64 | |
123 | ||
124 | typedef struct VhostUserCryptoSession { | |
125 | /* session id for success, -1 on errors */ | |
126 | int64_t session_id; | |
127 | CryptoDevBackendSymSessionInfo session_setup_data; | |
128 | uint8_t key[VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN]; | |
129 | uint8_t auth_key[VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN]; | |
130 | } VhostUserCryptoSession; | |
131 | ||
4c3e257b CL |
132 | static VhostUserConfig c __attribute__ ((unused)); |
133 | #define VHOST_USER_CONFIG_HDR_SIZE (sizeof(c.offset) \ | |
134 | + sizeof(c.size) \ | |
135 | + sizeof(c.flags)) | |
136 | ||
24e34754 | 137 | typedef struct { |
5f6f6664 NN |
138 | VhostUserRequest request; |
139 | ||
140 | #define VHOST_USER_VERSION_MASK (0x3) | |
141 | #define VHOST_USER_REPLY_MASK (0x1<<2) | |
ca525ce5 | 142 | #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3) |
5f6f6664 NN |
143 | uint32_t flags; |
144 | uint32_t size; /* the following payload size */ | |
24e34754 MT |
145 | } QEMU_PACKED VhostUserHeader; |
146 | ||
147 | typedef union { | |
5f6f6664 NN |
148 | #define VHOST_USER_VRING_IDX_MASK (0xff) |
149 | #define VHOST_USER_VRING_NOFD_MASK (0x1<<8) | |
150 | uint64_t u64; | |
151 | struct vhost_vring_state state; | |
152 | struct vhost_vring_addr addr; | |
153 | VhostUserMemory memory; | |
2b8819c6 | 154 | VhostUserLog log; |
6dcdd06e | 155 | struct vhost_iotlb_msg iotlb; |
4c3e257b | 156 | VhostUserConfig config; |
efbfeb81 | 157 | VhostUserCryptoSession session; |
24e34754 MT |
158 | } VhostUserPayload; |
159 | ||
160 | typedef struct VhostUserMsg { | |
161 | VhostUserHeader hdr; | |
162 | VhostUserPayload payload; | |
5f6f6664 NN |
163 | } QEMU_PACKED VhostUserMsg; |
164 | ||
165 | static VhostUserMsg m __attribute__ ((unused)); | |
24e34754 | 166 | #define VHOST_USER_HDR_SIZE (sizeof(VhostUserHeader)) |
5f6f6664 | 167 | |
24e34754 | 168 | #define VHOST_USER_PAYLOAD_SIZE (sizeof(VhostUserPayload)) |
5f6f6664 NN |
169 | |
170 | /* The version of the protocol we support */ | |
171 | #define VHOST_USER_VERSION (0x1) | |
172 | ||
2152f3fe | 173 | struct vhost_user { |
9ccbfe14 | 174 | struct vhost_dev *dev; |
2152f3fe | 175 | CharBackend *chr; |
4bbeeba0 | 176 | int slave_fd; |
9ccbfe14 | 177 | NotifierWithReturn postcopy_notifier; |
f82c1116 | 178 | struct PostCopyFD postcopy_fd; |
9bb38019 | 179 | uint64_t postcopy_client_bases[VHOST_MEMORY_MAX_NREGIONS]; |
905125d0 DDAG |
180 | /* Length of the region_rb and region_rb_offset arrays */ |
181 | size_t region_rb_len; | |
182 | /* RAMBlock associated with a given region */ | |
183 | RAMBlock **region_rb; | |
184 | /* The offset from the start of the RAMBlock to the start of the | |
185 | * vhost region. | |
186 | */ | |
187 | ram_addr_t *region_rb_offset; | |
188 | ||
6864a7b5 DDAG |
189 | /* True once we've entered postcopy_listen */ |
190 | bool postcopy_listen; | |
2152f3fe MAL |
191 | }; |
192 | ||
5f6f6664 NN |
193 | static bool ioeventfd_enabled(void) |
194 | { | |
195 | return kvm_enabled() && kvm_eventfds_enabled(); | |
196 | } | |
197 | ||
5f6f6664 NN |
198 | static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg) |
199 | { | |
2152f3fe MAL |
200 | struct vhost_user *u = dev->opaque; |
201 | CharBackend *chr = u->chr; | |
5f6f6664 NN |
202 | uint8_t *p = (uint8_t *) msg; |
203 | int r, size = VHOST_USER_HDR_SIZE; | |
204 | ||
205 | r = qemu_chr_fe_read_all(chr, p, size); | |
206 | if (r != size) { | |
5421f318 | 207 | error_report("Failed to read msg header. Read %d instead of %d." |
24e34754 | 208 | " Original request %d.", r, size, msg->hdr.request); |
5f6f6664 NN |
209 | goto fail; |
210 | } | |
211 | ||
212 | /* validate received flags */ | |
24e34754 | 213 | if (msg->hdr.flags != (VHOST_USER_REPLY_MASK | VHOST_USER_VERSION)) { |
5f6f6664 | 214 | error_report("Failed to read msg header." |
24e34754 | 215 | " Flags 0x%x instead of 0x%x.", msg->hdr.flags, |
5f6f6664 NN |
216 | VHOST_USER_REPLY_MASK | VHOST_USER_VERSION); |
217 | goto fail; | |
218 | } | |
219 | ||
220 | /* validate message size is sane */ | |
24e34754 | 221 | if (msg->hdr.size > VHOST_USER_PAYLOAD_SIZE) { |
5f6f6664 | 222 | error_report("Failed to read msg header." |
24e34754 | 223 | " Size %d exceeds the maximum %zu.", msg->hdr.size, |
5f6f6664 NN |
224 | VHOST_USER_PAYLOAD_SIZE); |
225 | goto fail; | |
226 | } | |
227 | ||
24e34754 | 228 | if (msg->hdr.size) { |
5f6f6664 | 229 | p += VHOST_USER_HDR_SIZE; |
24e34754 | 230 | size = msg->hdr.size; |
5f6f6664 NN |
231 | r = qemu_chr_fe_read_all(chr, p, size); |
232 | if (r != size) { | |
233 | error_report("Failed to read msg payload." | |
24e34754 | 234 | " Read %d instead of %d.", r, msg->hdr.size); |
5f6f6664 NN |
235 | goto fail; |
236 | } | |
237 | } | |
238 | ||
239 | return 0; | |
240 | ||
241 | fail: | |
242 | return -1; | |
243 | } | |
244 | ||
ca525ce5 | 245 | static int process_message_reply(struct vhost_dev *dev, |
3cf7daf8 | 246 | const VhostUserMsg *msg) |
ca525ce5 | 247 | { |
60cd1102 | 248 | VhostUserMsg msg_reply; |
ca525ce5 | 249 | |
24e34754 | 250 | if ((msg->hdr.flags & VHOST_USER_NEED_REPLY_MASK) == 0) { |
60cd1102 ZY |
251 | return 0; |
252 | } | |
253 | ||
254 | if (vhost_user_read(dev, &msg_reply) < 0) { | |
ca525ce5 PS |
255 | return -1; |
256 | } | |
257 | ||
24e34754 | 258 | if (msg_reply.hdr.request != msg->hdr.request) { |
ca525ce5 PS |
259 | error_report("Received unexpected msg type." |
260 | "Expected %d received %d", | |
24e34754 | 261 | msg->hdr.request, msg_reply.hdr.request); |
ca525ce5 PS |
262 | return -1; |
263 | } | |
264 | ||
60cd1102 | 265 | return msg_reply.payload.u64 ? -1 : 0; |
ca525ce5 PS |
266 | } |
267 | ||
21e70425 MAL |
268 | static bool vhost_user_one_time_request(VhostUserRequest request) |
269 | { | |
270 | switch (request) { | |
271 | case VHOST_USER_SET_OWNER: | |
60915dc4 | 272 | case VHOST_USER_RESET_OWNER: |
21e70425 MAL |
273 | case VHOST_USER_SET_MEM_TABLE: |
274 | case VHOST_USER_GET_QUEUE_NUM: | |
c5f048d8 | 275 | case VHOST_USER_NET_SET_MTU: |
21e70425 MAL |
276 | return true; |
277 | default: | |
278 | return false; | |
279 | } | |
280 | } | |
281 | ||
282 | /* most non-init callers ignore the error */ | |
5f6f6664 NN |
283 | static int vhost_user_write(struct vhost_dev *dev, VhostUserMsg *msg, |
284 | int *fds, int fd_num) | |
285 | { | |
2152f3fe MAL |
286 | struct vhost_user *u = dev->opaque; |
287 | CharBackend *chr = u->chr; | |
24e34754 | 288 | int ret, size = VHOST_USER_HDR_SIZE + msg->hdr.size; |
5f6f6664 | 289 | |
21e70425 MAL |
290 | /* |
291 | * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE, | |
292 | * we just need send it once in the first time. For later such | |
293 | * request, we just ignore it. | |
294 | */ | |
24e34754 MT |
295 | if (vhost_user_one_time_request(msg->hdr.request) && dev->vq_index != 0) { |
296 | msg->hdr.flags &= ~VHOST_USER_NEED_REPLY_MASK; | |
21e70425 MAL |
297 | return 0; |
298 | } | |
299 | ||
6fab2f3f | 300 | if (qemu_chr_fe_set_msgfds(chr, fds, fd_num) < 0) { |
f6b85710 | 301 | error_report("Failed to set msg fds."); |
6fab2f3f MAL |
302 | return -1; |
303 | } | |
5f6f6664 | 304 | |
f6b85710 MAL |
305 | ret = qemu_chr_fe_write_all(chr, (const uint8_t *) msg, size); |
306 | if (ret != size) { | |
307 | error_report("Failed to write msg." | |
308 | " Wrote %d instead of %d.", ret, size); | |
309 | return -1; | |
310 | } | |
311 | ||
312 | return 0; | |
5f6f6664 NN |
313 | } |
314 | ||
21e70425 MAL |
315 | static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base, |
316 | struct vhost_log *log) | |
b931bfbf | 317 | { |
21e70425 MAL |
318 | int fds[VHOST_MEMORY_MAX_NREGIONS]; |
319 | size_t fd_num = 0; | |
320 | bool shmfd = virtio_has_feature(dev->protocol_features, | |
321 | VHOST_USER_PROTOCOL_F_LOG_SHMFD); | |
322 | VhostUserMsg msg = { | |
24e34754 MT |
323 | .hdr.request = VHOST_USER_SET_LOG_BASE, |
324 | .hdr.flags = VHOST_USER_VERSION, | |
48854f57 | 325 | .payload.log.mmap_size = log->size * sizeof(*(log->log)), |
2b8819c6 | 326 | .payload.log.mmap_offset = 0, |
24e34754 | 327 | .hdr.size = sizeof(msg.payload.log), |
21e70425 MAL |
328 | }; |
329 | ||
330 | if (shmfd && log->fd != -1) { | |
331 | fds[fd_num++] = log->fd; | |
332 | } | |
333 | ||
c4843a45 MAL |
334 | if (vhost_user_write(dev, &msg, fds, fd_num) < 0) { |
335 | return -1; | |
336 | } | |
21e70425 MAL |
337 | |
338 | if (shmfd) { | |
24e34754 | 339 | msg.hdr.size = 0; |
21e70425 | 340 | if (vhost_user_read(dev, &msg) < 0) { |
c4843a45 | 341 | return -1; |
21e70425 MAL |
342 | } |
343 | ||
24e34754 | 344 | if (msg.hdr.request != VHOST_USER_SET_LOG_BASE) { |
21e70425 MAL |
345 | error_report("Received unexpected msg type. " |
346 | "Expected %d received %d", | |
24e34754 | 347 | VHOST_USER_SET_LOG_BASE, msg.hdr.request); |
21e70425 MAL |
348 | return -1; |
349 | } | |
b931bfbf | 350 | } |
21e70425 MAL |
351 | |
352 | return 0; | |
b931bfbf CO |
353 | } |
354 | ||
55d754b3 DDAG |
355 | static int vhost_user_set_mem_table_postcopy(struct vhost_dev *dev, |
356 | struct vhost_memory *mem) | |
357 | { | |
9bb38019 | 358 | struct vhost_user *u = dev->opaque; |
55d754b3 DDAG |
359 | int fds[VHOST_MEMORY_MAX_NREGIONS]; |
360 | int i, fd; | |
361 | size_t fd_num = 0; | |
362 | bool reply_supported = virtio_has_feature(dev->protocol_features, | |
363 | VHOST_USER_PROTOCOL_F_REPLY_ACK); | |
9bb38019 DDAG |
364 | VhostUserMsg msg_reply; |
365 | int region_i, msg_i; | |
366 | ||
55d754b3 DDAG |
367 | VhostUserMsg msg = { |
368 | .hdr.request = VHOST_USER_SET_MEM_TABLE, | |
369 | .hdr.flags = VHOST_USER_VERSION, | |
370 | }; | |
371 | ||
372 | if (reply_supported) { | |
373 | msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK; | |
374 | } | |
375 | ||
905125d0 DDAG |
376 | if (u->region_rb_len < dev->mem->nregions) { |
377 | u->region_rb = g_renew(RAMBlock*, u->region_rb, dev->mem->nregions); | |
378 | u->region_rb_offset = g_renew(ram_addr_t, u->region_rb_offset, | |
379 | dev->mem->nregions); | |
380 | memset(&(u->region_rb[u->region_rb_len]), '\0', | |
381 | sizeof(RAMBlock *) * (dev->mem->nregions - u->region_rb_len)); | |
382 | memset(&(u->region_rb_offset[u->region_rb_len]), '\0', | |
383 | sizeof(ram_addr_t) * (dev->mem->nregions - u->region_rb_len)); | |
384 | u->region_rb_len = dev->mem->nregions; | |
385 | } | |
386 | ||
55d754b3 DDAG |
387 | for (i = 0; i < dev->mem->nregions; ++i) { |
388 | struct vhost_memory_region *reg = dev->mem->regions + i; | |
389 | ram_addr_t offset; | |
390 | MemoryRegion *mr; | |
391 | ||
392 | assert((uintptr_t)reg->userspace_addr == reg->userspace_addr); | |
393 | mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr, | |
394 | &offset); | |
395 | fd = memory_region_get_fd(mr); | |
396 | if (fd > 0) { | |
905125d0 DDAG |
397 | trace_vhost_user_set_mem_table_withfd(fd_num, mr->name, |
398 | reg->memory_size, | |
399 | reg->guest_phys_addr, | |
400 | reg->userspace_addr, offset); | |
401 | u->region_rb_offset[i] = offset; | |
402 | u->region_rb[i] = mr->ram_block; | |
55d754b3 DDAG |
403 | msg.payload.memory.regions[fd_num].userspace_addr = |
404 | reg->userspace_addr; | |
405 | msg.payload.memory.regions[fd_num].memory_size = reg->memory_size; | |
406 | msg.payload.memory.regions[fd_num].guest_phys_addr = | |
407 | reg->guest_phys_addr; | |
408 | msg.payload.memory.regions[fd_num].mmap_offset = offset; | |
409 | assert(fd_num < VHOST_MEMORY_MAX_NREGIONS); | |
410 | fds[fd_num++] = fd; | |
905125d0 DDAG |
411 | } else { |
412 | u->region_rb_offset[i] = 0; | |
413 | u->region_rb[i] = NULL; | |
55d754b3 DDAG |
414 | } |
415 | } | |
416 | ||
417 | msg.payload.memory.nregions = fd_num; | |
418 | ||
419 | if (!fd_num) { | |
420 | error_report("Failed initializing vhost-user memory map, " | |
421 | "consider using -object memory-backend-file share=on"); | |
422 | return -1; | |
423 | } | |
424 | ||
425 | msg.hdr.size = sizeof(msg.payload.memory.nregions); | |
426 | msg.hdr.size += sizeof(msg.payload.memory.padding); | |
427 | msg.hdr.size += fd_num * sizeof(VhostUserMemoryRegion); | |
428 | ||
429 | if (vhost_user_write(dev, &msg, fds, fd_num) < 0) { | |
430 | return -1; | |
431 | } | |
432 | ||
9bb38019 DDAG |
433 | if (vhost_user_read(dev, &msg_reply) < 0) { |
434 | return -1; | |
435 | } | |
436 | ||
437 | if (msg_reply.hdr.request != VHOST_USER_SET_MEM_TABLE) { | |
438 | error_report("%s: Received unexpected msg type." | |
439 | "Expected %d received %d", __func__, | |
440 | VHOST_USER_SET_MEM_TABLE, msg_reply.hdr.request); | |
441 | return -1; | |
442 | } | |
443 | /* We're using the same structure, just reusing one of the | |
444 | * fields, so it should be the same size. | |
445 | */ | |
446 | if (msg_reply.hdr.size != msg.hdr.size) { | |
447 | error_report("%s: Unexpected size for postcopy reply " | |
448 | "%d vs %d", __func__, msg_reply.hdr.size, msg.hdr.size); | |
449 | return -1; | |
450 | } | |
451 | ||
452 | memset(u->postcopy_client_bases, 0, | |
453 | sizeof(uint64_t) * VHOST_MEMORY_MAX_NREGIONS); | |
454 | ||
455 | /* They're in the same order as the regions that were sent | |
456 | * but some of the regions were skipped (above) if they | |
457 | * didn't have fd's | |
458 | */ | |
459 | for (msg_i = 0, region_i = 0; | |
460 | region_i < dev->mem->nregions; | |
461 | region_i++) { | |
462 | if (msg_i < fd_num && | |
463 | msg_reply.payload.memory.regions[msg_i].guest_phys_addr == | |
464 | dev->mem->regions[region_i].guest_phys_addr) { | |
465 | u->postcopy_client_bases[region_i] = | |
466 | msg_reply.payload.memory.regions[msg_i].userspace_addr; | |
467 | trace_vhost_user_set_mem_table_postcopy( | |
468 | msg_reply.payload.memory.regions[msg_i].userspace_addr, | |
469 | msg.payload.memory.regions[msg_i].userspace_addr, | |
470 | msg_i, region_i); | |
471 | msg_i++; | |
472 | } | |
473 | } | |
474 | if (msg_i != fd_num) { | |
475 | error_report("%s: postcopy reply not fully consumed " | |
476 | "%d vs %zd", | |
477 | __func__, msg_i, fd_num); | |
478 | return -1; | |
479 | } | |
480 | /* Now we've registered this with the postcopy code, we ack to the client, | |
481 | * because now we're in the position to be able to deal with any faults | |
482 | * it generates. | |
483 | */ | |
484 | /* TODO: Use this for failure cases as well with a bad value */ | |
485 | msg.hdr.size = sizeof(msg.payload.u64); | |
486 | msg.payload.u64 = 0; /* OK */ | |
487 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { | |
488 | return -1; | |
489 | } | |
490 | ||
55d754b3 DDAG |
491 | if (reply_supported) { |
492 | return process_message_reply(dev, &msg); | |
493 | } | |
494 | ||
495 | return 0; | |
496 | } | |
497 | ||
94c9cb31 MT |
498 | static int vhost_user_set_mem_table(struct vhost_dev *dev, |
499 | struct vhost_memory *mem) | |
500 | { | |
55d754b3 | 501 | struct vhost_user *u = dev->opaque; |
94c9cb31 MT |
502 | int fds[VHOST_MEMORY_MAX_NREGIONS]; |
503 | int i, fd; | |
504 | size_t fd_num = 0; | |
55d754b3 | 505 | bool do_postcopy = u->postcopy_listen && u->postcopy_fd.handler; |
94c9cb31 | 506 | bool reply_supported = virtio_has_feature(dev->protocol_features, |
9bb38019 DDAG |
507 | VHOST_USER_PROTOCOL_F_REPLY_ACK) && |
508 | !do_postcopy; | |
94c9cb31 | 509 | |
55d754b3 DDAG |
510 | if (do_postcopy) { |
511 | /* Postcopy has enough differences that it's best done in it's own | |
512 | * version | |
513 | */ | |
514 | return vhost_user_set_mem_table_postcopy(dev, mem); | |
515 | } | |
516 | ||
94c9cb31 | 517 | VhostUserMsg msg = { |
24e34754 MT |
518 | .hdr.request = VHOST_USER_SET_MEM_TABLE, |
519 | .hdr.flags = VHOST_USER_VERSION, | |
94c9cb31 MT |
520 | }; |
521 | ||
522 | if (reply_supported) { | |
24e34754 | 523 | msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK; |
94c9cb31 MT |
524 | } |
525 | ||
526 | for (i = 0; i < dev->mem->nregions; ++i) { | |
527 | struct vhost_memory_region *reg = dev->mem->regions + i; | |
528 | ram_addr_t offset; | |
529 | MemoryRegion *mr; | |
530 | ||
531 | assert((uintptr_t)reg->userspace_addr == reg->userspace_addr); | |
532 | mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr, | |
533 | &offset); | |
534 | fd = memory_region_get_fd(mr); | |
535 | if (fd > 0) { | |
f4bf56fb JZ |
536 | if (fd_num == VHOST_MEMORY_MAX_NREGIONS) { |
537 | error_report("Failed preparing vhost-user memory table msg"); | |
538 | return -1; | |
539 | } | |
55d754b3 DDAG |
540 | msg.payload.memory.regions[fd_num].userspace_addr = |
541 | reg->userspace_addr; | |
94c9cb31 | 542 | msg.payload.memory.regions[fd_num].memory_size = reg->memory_size; |
55d754b3 DDAG |
543 | msg.payload.memory.regions[fd_num].guest_phys_addr = |
544 | reg->guest_phys_addr; | |
94c9cb31 | 545 | msg.payload.memory.regions[fd_num].mmap_offset = offset; |
94c9cb31 MT |
546 | fds[fd_num++] = fd; |
547 | } | |
548 | } | |
549 | ||
550 | msg.payload.memory.nregions = fd_num; | |
551 | ||
552 | if (!fd_num) { | |
553 | error_report("Failed initializing vhost-user memory map, " | |
554 | "consider using -object memory-backend-file share=on"); | |
555 | return -1; | |
556 | } | |
557 | ||
24e34754 MT |
558 | msg.hdr.size = sizeof(msg.payload.memory.nregions); |
559 | msg.hdr.size += sizeof(msg.payload.memory.padding); | |
560 | msg.hdr.size += fd_num * sizeof(VhostUserMemoryRegion); | |
94c9cb31 MT |
561 | |
562 | if (vhost_user_write(dev, &msg, fds, fd_num) < 0) { | |
563 | return -1; | |
564 | } | |
565 | ||
566 | if (reply_supported) { | |
3cf7daf8 | 567 | return process_message_reply(dev, &msg); |
94c9cb31 MT |
568 | } |
569 | ||
570 | return 0; | |
571 | } | |
572 | ||
21e70425 MAL |
573 | static int vhost_user_set_vring_addr(struct vhost_dev *dev, |
574 | struct vhost_vring_addr *addr) | |
575 | { | |
576 | VhostUserMsg msg = { | |
24e34754 MT |
577 | .hdr.request = VHOST_USER_SET_VRING_ADDR, |
578 | .hdr.flags = VHOST_USER_VERSION, | |
7f4a930e | 579 | .payload.addr = *addr, |
24e34754 | 580 | .hdr.size = sizeof(msg.payload.addr), |
21e70425 | 581 | }; |
5f6f6664 | 582 | |
c4843a45 MAL |
583 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { |
584 | return -1; | |
585 | } | |
5f6f6664 | 586 | |
21e70425 MAL |
587 | return 0; |
588 | } | |
5f6f6664 | 589 | |
21e70425 MAL |
590 | static int vhost_user_set_vring_endian(struct vhost_dev *dev, |
591 | struct vhost_vring_state *ring) | |
592 | { | |
5df04f17 FF |
593 | bool cross_endian = virtio_has_feature(dev->protocol_features, |
594 | VHOST_USER_PROTOCOL_F_CROSS_ENDIAN); | |
595 | VhostUserMsg msg = { | |
24e34754 MT |
596 | .hdr.request = VHOST_USER_SET_VRING_ENDIAN, |
597 | .hdr.flags = VHOST_USER_VERSION, | |
5df04f17 | 598 | .payload.state = *ring, |
24e34754 | 599 | .hdr.size = sizeof(msg.payload.state), |
5df04f17 FF |
600 | }; |
601 | ||
602 | if (!cross_endian) { | |
603 | error_report("vhost-user trying to send unhandled ioctl"); | |
604 | return -1; | |
605 | } | |
606 | ||
607 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { | |
608 | return -1; | |
609 | } | |
610 | ||
611 | return 0; | |
21e70425 | 612 | } |
5f6f6664 | 613 | |
21e70425 MAL |
614 | static int vhost_set_vring(struct vhost_dev *dev, |
615 | unsigned long int request, | |
616 | struct vhost_vring_state *ring) | |
617 | { | |
618 | VhostUserMsg msg = { | |
24e34754 MT |
619 | .hdr.request = request, |
620 | .hdr.flags = VHOST_USER_VERSION, | |
7f4a930e | 621 | .payload.state = *ring, |
24e34754 | 622 | .hdr.size = sizeof(msg.payload.state), |
21e70425 MAL |
623 | }; |
624 | ||
c4843a45 MAL |
625 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { |
626 | return -1; | |
627 | } | |
21e70425 MAL |
628 | |
629 | return 0; | |
630 | } | |
631 | ||
632 | static int vhost_user_set_vring_num(struct vhost_dev *dev, | |
633 | struct vhost_vring_state *ring) | |
634 | { | |
635 | return vhost_set_vring(dev, VHOST_USER_SET_VRING_NUM, ring); | |
636 | } | |
637 | ||
638 | static int vhost_user_set_vring_base(struct vhost_dev *dev, | |
639 | struct vhost_vring_state *ring) | |
640 | { | |
641 | return vhost_set_vring(dev, VHOST_USER_SET_VRING_BASE, ring); | |
642 | } | |
643 | ||
644 | static int vhost_user_set_vring_enable(struct vhost_dev *dev, int enable) | |
645 | { | |
dc3db6ad | 646 | int i; |
21e70425 | 647 | |
923e2d98 | 648 | if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) { |
5f6f6664 | 649 | return -1; |
5f6f6664 NN |
650 | } |
651 | ||
dc3db6ad MT |
652 | for (i = 0; i < dev->nvqs; ++i) { |
653 | struct vhost_vring_state state = { | |
654 | .index = dev->vq_index + i, | |
655 | .num = enable, | |
656 | }; | |
657 | ||
658 | vhost_set_vring(dev, VHOST_USER_SET_VRING_ENABLE, &state); | |
659 | } | |
21e70425 | 660 | |
dc3db6ad MT |
661 | return 0; |
662 | } | |
21e70425 MAL |
663 | |
664 | static int vhost_user_get_vring_base(struct vhost_dev *dev, | |
665 | struct vhost_vring_state *ring) | |
666 | { | |
667 | VhostUserMsg msg = { | |
24e34754 MT |
668 | .hdr.request = VHOST_USER_GET_VRING_BASE, |
669 | .hdr.flags = VHOST_USER_VERSION, | |
7f4a930e | 670 | .payload.state = *ring, |
24e34754 | 671 | .hdr.size = sizeof(msg.payload.state), |
21e70425 MAL |
672 | }; |
673 | ||
c4843a45 MAL |
674 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { |
675 | return -1; | |
676 | } | |
21e70425 MAL |
677 | |
678 | if (vhost_user_read(dev, &msg) < 0) { | |
c4843a45 | 679 | return -1; |
5f6f6664 NN |
680 | } |
681 | ||
24e34754 | 682 | if (msg.hdr.request != VHOST_USER_GET_VRING_BASE) { |
21e70425 | 683 | error_report("Received unexpected msg type. Expected %d received %d", |
24e34754 | 684 | VHOST_USER_GET_VRING_BASE, msg.hdr.request); |
21e70425 MAL |
685 | return -1; |
686 | } | |
5f6f6664 | 687 | |
24e34754 | 688 | if (msg.hdr.size != sizeof(msg.payload.state)) { |
21e70425 MAL |
689 | error_report("Received bad msg size."); |
690 | return -1; | |
5f6f6664 NN |
691 | } |
692 | ||
7f4a930e | 693 | *ring = msg.payload.state; |
21e70425 | 694 | |
5f6f6664 NN |
695 | return 0; |
696 | } | |
697 | ||
21e70425 MAL |
698 | static int vhost_set_vring_file(struct vhost_dev *dev, |
699 | VhostUserRequest request, | |
700 | struct vhost_vring_file *file) | |
c2bea314 | 701 | { |
9a78a5dd MAL |
702 | int fds[VHOST_MEMORY_MAX_NREGIONS]; |
703 | size_t fd_num = 0; | |
c2bea314 | 704 | VhostUserMsg msg = { |
24e34754 MT |
705 | .hdr.request = request, |
706 | .hdr.flags = VHOST_USER_VERSION, | |
7f4a930e | 707 | .payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK, |
24e34754 | 708 | .hdr.size = sizeof(msg.payload.u64), |
c2bea314 MAL |
709 | }; |
710 | ||
21e70425 MAL |
711 | if (ioeventfd_enabled() && file->fd > 0) { |
712 | fds[fd_num++] = file->fd; | |
713 | } else { | |
7f4a930e | 714 | msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK; |
9a78a5dd MAL |
715 | } |
716 | ||
c4843a45 MAL |
717 | if (vhost_user_write(dev, &msg, fds, fd_num) < 0) { |
718 | return -1; | |
719 | } | |
9a78a5dd | 720 | |
21e70425 MAL |
721 | return 0; |
722 | } | |
9a78a5dd | 723 | |
21e70425 MAL |
724 | static int vhost_user_set_vring_kick(struct vhost_dev *dev, |
725 | struct vhost_vring_file *file) | |
726 | { | |
727 | return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_KICK, file); | |
728 | } | |
729 | ||
730 | static int vhost_user_set_vring_call(struct vhost_dev *dev, | |
731 | struct vhost_vring_file *file) | |
732 | { | |
733 | return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_CALL, file); | |
734 | } | |
735 | ||
736 | static int vhost_user_set_u64(struct vhost_dev *dev, int request, uint64_t u64) | |
737 | { | |
738 | VhostUserMsg msg = { | |
24e34754 MT |
739 | .hdr.request = request, |
740 | .hdr.flags = VHOST_USER_VERSION, | |
7f4a930e | 741 | .payload.u64 = u64, |
24e34754 | 742 | .hdr.size = sizeof(msg.payload.u64), |
21e70425 MAL |
743 | }; |
744 | ||
c4843a45 MAL |
745 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { |
746 | return -1; | |
747 | } | |
21e70425 MAL |
748 | |
749 | return 0; | |
750 | } | |
751 | ||
752 | static int vhost_user_set_features(struct vhost_dev *dev, | |
753 | uint64_t features) | |
754 | { | |
755 | return vhost_user_set_u64(dev, VHOST_USER_SET_FEATURES, features); | |
756 | } | |
757 | ||
758 | static int vhost_user_set_protocol_features(struct vhost_dev *dev, | |
759 | uint64_t features) | |
760 | { | |
761 | return vhost_user_set_u64(dev, VHOST_USER_SET_PROTOCOL_FEATURES, features); | |
762 | } | |
763 | ||
764 | static int vhost_user_get_u64(struct vhost_dev *dev, int request, uint64_t *u64) | |
765 | { | |
766 | VhostUserMsg msg = { | |
24e34754 MT |
767 | .hdr.request = request, |
768 | .hdr.flags = VHOST_USER_VERSION, | |
21e70425 MAL |
769 | }; |
770 | ||
771 | if (vhost_user_one_time_request(request) && dev->vq_index != 0) { | |
772 | return 0; | |
9a78a5dd | 773 | } |
c2bea314 | 774 | |
c4843a45 MAL |
775 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { |
776 | return -1; | |
777 | } | |
21e70425 MAL |
778 | |
779 | if (vhost_user_read(dev, &msg) < 0) { | |
c4843a45 | 780 | return -1; |
21e70425 MAL |
781 | } |
782 | ||
24e34754 | 783 | if (msg.hdr.request != request) { |
21e70425 | 784 | error_report("Received unexpected msg type. Expected %d received %d", |
24e34754 | 785 | request, msg.hdr.request); |
21e70425 MAL |
786 | return -1; |
787 | } | |
788 | ||
24e34754 | 789 | if (msg.hdr.size != sizeof(msg.payload.u64)) { |
21e70425 MAL |
790 | error_report("Received bad msg size."); |
791 | return -1; | |
792 | } | |
793 | ||
7f4a930e | 794 | *u64 = msg.payload.u64; |
21e70425 MAL |
795 | |
796 | return 0; | |
797 | } | |
798 | ||
799 | static int vhost_user_get_features(struct vhost_dev *dev, uint64_t *features) | |
800 | { | |
801 | return vhost_user_get_u64(dev, VHOST_USER_GET_FEATURES, features); | |
802 | } | |
803 | ||
804 | static int vhost_user_set_owner(struct vhost_dev *dev) | |
805 | { | |
806 | VhostUserMsg msg = { | |
24e34754 MT |
807 | .hdr.request = VHOST_USER_SET_OWNER, |
808 | .hdr.flags = VHOST_USER_VERSION, | |
21e70425 MAL |
809 | }; |
810 | ||
c4843a45 MAL |
811 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { |
812 | return -1; | |
813 | } | |
21e70425 MAL |
814 | |
815 | return 0; | |
816 | } | |
817 | ||
818 | static int vhost_user_reset_device(struct vhost_dev *dev) | |
819 | { | |
820 | VhostUserMsg msg = { | |
24e34754 MT |
821 | .hdr.request = VHOST_USER_RESET_OWNER, |
822 | .hdr.flags = VHOST_USER_VERSION, | |
21e70425 MAL |
823 | }; |
824 | ||
c4843a45 MAL |
825 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { |
826 | return -1; | |
827 | } | |
21e70425 | 828 | |
c2bea314 MAL |
829 | return 0; |
830 | } | |
831 | ||
4c3e257b CL |
832 | static int vhost_user_slave_handle_config_change(struct vhost_dev *dev) |
833 | { | |
834 | int ret = -1; | |
835 | ||
836 | if (!dev->config_ops) { | |
837 | return -1; | |
838 | } | |
839 | ||
840 | if (dev->config_ops->vhost_dev_config_notifier) { | |
841 | ret = dev->config_ops->vhost_dev_config_notifier(dev); | |
842 | } | |
843 | ||
844 | return ret; | |
845 | } | |
846 | ||
4bbeeba0 MAL |
847 | static void slave_read(void *opaque) |
848 | { | |
849 | struct vhost_dev *dev = opaque; | |
850 | struct vhost_user *u = dev->opaque; | |
69aff030 MT |
851 | VhostUserHeader hdr = { 0, }; |
852 | VhostUserPayload payload = { 0, }; | |
4bbeeba0 MAL |
853 | int size, ret = 0; |
854 | ||
855 | /* Read header */ | |
69aff030 | 856 | size = read(u->slave_fd, &hdr, VHOST_USER_HDR_SIZE); |
4bbeeba0 MAL |
857 | if (size != VHOST_USER_HDR_SIZE) { |
858 | error_report("Failed to read from slave."); | |
859 | goto err; | |
860 | } | |
861 | ||
69aff030 | 862 | if (hdr.size > VHOST_USER_PAYLOAD_SIZE) { |
4bbeeba0 | 863 | error_report("Failed to read msg header." |
69aff030 | 864 | " Size %d exceeds the maximum %zu.", hdr.size, |
4bbeeba0 MAL |
865 | VHOST_USER_PAYLOAD_SIZE); |
866 | goto err; | |
867 | } | |
868 | ||
869 | /* Read payload */ | |
69aff030 MT |
870 | size = read(u->slave_fd, &payload, hdr.size); |
871 | if (size != hdr.size) { | |
4bbeeba0 MAL |
872 | error_report("Failed to read payload from slave."); |
873 | goto err; | |
874 | } | |
875 | ||
69aff030 | 876 | switch (hdr.request) { |
6dcdd06e | 877 | case VHOST_USER_SLAVE_IOTLB_MSG: |
69aff030 | 878 | ret = vhost_backend_handle_iotlb_msg(dev, &payload.iotlb); |
6dcdd06e | 879 | break; |
4c3e257b CL |
880 | case VHOST_USER_SLAVE_CONFIG_CHANGE_MSG : |
881 | ret = vhost_user_slave_handle_config_change(dev); | |
882 | break; | |
4bbeeba0 MAL |
883 | default: |
884 | error_report("Received unexpected msg type."); | |
885 | ret = -EINVAL; | |
886 | } | |
887 | ||
888 | /* | |
889 | * REPLY_ACK feature handling. Other reply types has to be managed | |
890 | * directly in their request handlers. | |
891 | */ | |
69aff030 MT |
892 | if (hdr.flags & VHOST_USER_NEED_REPLY_MASK) { |
893 | struct iovec iovec[2]; | |
4bbeeba0 | 894 | |
4bbeeba0 | 895 | |
69aff030 MT |
896 | hdr.flags &= ~VHOST_USER_NEED_REPLY_MASK; |
897 | hdr.flags |= VHOST_USER_REPLY_MASK; | |
898 | ||
899 | payload.u64 = !!ret; | |
900 | hdr.size = sizeof(payload.u64); | |
901 | ||
902 | iovec[0].iov_base = &hdr; | |
903 | iovec[0].iov_len = VHOST_USER_HDR_SIZE; | |
904 | iovec[1].iov_base = &payload; | |
905 | iovec[1].iov_len = hdr.size; | |
906 | ||
907 | size = writev(u->slave_fd, iovec, ARRAY_SIZE(iovec)); | |
908 | if (size != VHOST_USER_HDR_SIZE + hdr.size) { | |
4bbeeba0 MAL |
909 | error_report("Failed to send msg reply to slave."); |
910 | goto err; | |
911 | } | |
912 | } | |
913 | ||
914 | return; | |
915 | ||
916 | err: | |
917 | qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL); | |
918 | close(u->slave_fd); | |
919 | u->slave_fd = -1; | |
920 | return; | |
921 | } | |
922 | ||
923 | static int vhost_setup_slave_channel(struct vhost_dev *dev) | |
924 | { | |
925 | VhostUserMsg msg = { | |
24e34754 MT |
926 | .hdr.request = VHOST_USER_SET_SLAVE_REQ_FD, |
927 | .hdr.flags = VHOST_USER_VERSION, | |
4bbeeba0 MAL |
928 | }; |
929 | struct vhost_user *u = dev->opaque; | |
930 | int sv[2], ret = 0; | |
931 | bool reply_supported = virtio_has_feature(dev->protocol_features, | |
932 | VHOST_USER_PROTOCOL_F_REPLY_ACK); | |
933 | ||
934 | if (!virtio_has_feature(dev->protocol_features, | |
935 | VHOST_USER_PROTOCOL_F_SLAVE_REQ)) { | |
936 | return 0; | |
937 | } | |
938 | ||
939 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { | |
940 | error_report("socketpair() failed"); | |
941 | return -1; | |
942 | } | |
943 | ||
944 | u->slave_fd = sv[0]; | |
945 | qemu_set_fd_handler(u->slave_fd, slave_read, NULL, dev); | |
946 | ||
947 | if (reply_supported) { | |
24e34754 | 948 | msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK; |
4bbeeba0 MAL |
949 | } |
950 | ||
951 | ret = vhost_user_write(dev, &msg, &sv[1], 1); | |
952 | if (ret) { | |
953 | goto out; | |
954 | } | |
955 | ||
956 | if (reply_supported) { | |
957 | ret = process_message_reply(dev, &msg); | |
958 | } | |
959 | ||
960 | out: | |
961 | close(sv[1]); | |
962 | if (ret) { | |
963 | qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL); | |
964 | close(u->slave_fd); | |
965 | u->slave_fd = -1; | |
966 | } | |
967 | ||
968 | return ret; | |
969 | } | |
970 | ||
f82c1116 DDAG |
971 | /* |
972 | * Called back from the postcopy fault thread when a fault is received on our | |
973 | * ufd. | |
974 | * TODO: This is Linux specific | |
975 | */ | |
976 | static int vhost_user_postcopy_fault_handler(struct PostCopyFD *pcfd, | |
977 | void *ufd) | |
978 | { | |
375318d0 DDAG |
979 | struct vhost_dev *dev = pcfd->data; |
980 | struct vhost_user *u = dev->opaque; | |
981 | struct uffd_msg *msg = ufd; | |
982 | uint64_t faultaddr = msg->arg.pagefault.address; | |
983 | RAMBlock *rb = NULL; | |
984 | uint64_t rb_offset; | |
985 | int i; | |
986 | ||
987 | trace_vhost_user_postcopy_fault_handler(pcfd->idstr, faultaddr, | |
988 | dev->mem->nregions); | |
989 | for (i = 0; i < MIN(dev->mem->nregions, u->region_rb_len); i++) { | |
990 | trace_vhost_user_postcopy_fault_handler_loop(i, | |
991 | u->postcopy_client_bases[i], dev->mem->regions[i].memory_size); | |
992 | if (faultaddr >= u->postcopy_client_bases[i]) { | |
993 | /* Ofset of the fault address in the vhost region */ | |
994 | uint64_t region_offset = faultaddr - u->postcopy_client_bases[i]; | |
995 | if (region_offset < dev->mem->regions[i].memory_size) { | |
996 | rb_offset = region_offset + u->region_rb_offset[i]; | |
997 | trace_vhost_user_postcopy_fault_handler_found(i, | |
998 | region_offset, rb_offset); | |
999 | rb = u->region_rb[i]; | |
1000 | return postcopy_request_shared_page(pcfd, rb, faultaddr, | |
1001 | rb_offset); | |
1002 | } | |
1003 | } | |
1004 | } | |
1005 | error_report("%s: Failed to find region for fault %" PRIx64, | |
1006 | __func__, faultaddr); | |
1007 | return -1; | |
f82c1116 DDAG |
1008 | } |
1009 | ||
c07e3615 DDAG |
1010 | static int vhost_user_postcopy_waker(struct PostCopyFD *pcfd, RAMBlock *rb, |
1011 | uint64_t offset) | |
1012 | { | |
1013 | struct vhost_dev *dev = pcfd->data; | |
1014 | struct vhost_user *u = dev->opaque; | |
1015 | int i; | |
1016 | ||
1017 | trace_vhost_user_postcopy_waker(qemu_ram_get_idstr(rb), offset); | |
1018 | ||
1019 | if (!u) { | |
1020 | return 0; | |
1021 | } | |
1022 | /* Translate the offset into an address in the clients address space */ | |
1023 | for (i = 0; i < MIN(dev->mem->nregions, u->region_rb_len); i++) { | |
1024 | if (u->region_rb[i] == rb && | |
1025 | offset >= u->region_rb_offset[i] && | |
1026 | offset < (u->region_rb_offset[i] + | |
1027 | dev->mem->regions[i].memory_size)) { | |
1028 | uint64_t client_addr = (offset - u->region_rb_offset[i]) + | |
1029 | u->postcopy_client_bases[i]; | |
1030 | trace_vhost_user_postcopy_waker_found(client_addr); | |
1031 | return postcopy_wake_shared(pcfd, client_addr, rb); | |
1032 | } | |
1033 | } | |
1034 | ||
1035 | trace_vhost_user_postcopy_waker_nomatch(qemu_ram_get_idstr(rb), offset); | |
1036 | return 0; | |
1037 | } | |
1038 | ||
d3dff7a5 DDAG |
1039 | /* |
1040 | * Called at the start of an inbound postcopy on reception of the | |
1041 | * 'advise' command. | |
1042 | */ | |
1043 | static int vhost_user_postcopy_advise(struct vhost_dev *dev, Error **errp) | |
1044 | { | |
1045 | struct vhost_user *u = dev->opaque; | |
1046 | CharBackend *chr = u->chr; | |
1047 | int ufd; | |
1048 | VhostUserMsg msg = { | |
1049 | .hdr.request = VHOST_USER_POSTCOPY_ADVISE, | |
1050 | .hdr.flags = VHOST_USER_VERSION, | |
1051 | }; | |
1052 | ||
1053 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { | |
1054 | error_setg(errp, "Failed to send postcopy_advise to vhost"); | |
1055 | return -1; | |
1056 | } | |
1057 | ||
1058 | if (vhost_user_read(dev, &msg) < 0) { | |
1059 | error_setg(errp, "Failed to get postcopy_advise reply from vhost"); | |
1060 | return -1; | |
1061 | } | |
1062 | ||
1063 | if (msg.hdr.request != VHOST_USER_POSTCOPY_ADVISE) { | |
1064 | error_setg(errp, "Unexpected msg type. Expected %d received %d", | |
1065 | VHOST_USER_POSTCOPY_ADVISE, msg.hdr.request); | |
1066 | return -1; | |
1067 | } | |
1068 | ||
1069 | if (msg.hdr.size) { | |
1070 | error_setg(errp, "Received bad msg size."); | |
1071 | return -1; | |
1072 | } | |
1073 | ufd = qemu_chr_fe_get_msgfd(chr); | |
1074 | if (ufd < 0) { | |
1075 | error_setg(errp, "%s: Failed to get ufd", __func__); | |
1076 | return -1; | |
1077 | } | |
f82c1116 | 1078 | fcntl(ufd, F_SETFL, O_NONBLOCK); |
d3dff7a5 | 1079 | |
f82c1116 DDAG |
1080 | /* register ufd with userfault thread */ |
1081 | u->postcopy_fd.fd = ufd; | |
1082 | u->postcopy_fd.data = dev; | |
1083 | u->postcopy_fd.handler = vhost_user_postcopy_fault_handler; | |
c07e3615 | 1084 | u->postcopy_fd.waker = vhost_user_postcopy_waker; |
f82c1116 DDAG |
1085 | u->postcopy_fd.idstr = "vhost-user"; /* Need to find unique name */ |
1086 | postcopy_register_shared_ufd(&u->postcopy_fd); | |
d3dff7a5 DDAG |
1087 | return 0; |
1088 | } | |
1089 | ||
6864a7b5 DDAG |
1090 | /* |
1091 | * Called at the switch to postcopy on reception of the 'listen' command. | |
1092 | */ | |
1093 | static int vhost_user_postcopy_listen(struct vhost_dev *dev, Error **errp) | |
1094 | { | |
1095 | struct vhost_user *u = dev->opaque; | |
1096 | int ret; | |
1097 | VhostUserMsg msg = { | |
1098 | .hdr.request = VHOST_USER_POSTCOPY_LISTEN, | |
1099 | .hdr.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK, | |
1100 | }; | |
1101 | u->postcopy_listen = true; | |
1102 | trace_vhost_user_postcopy_listen(); | |
1103 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { | |
1104 | error_setg(errp, "Failed to send postcopy_listen to vhost"); | |
1105 | return -1; | |
1106 | } | |
1107 | ||
1108 | ret = process_message_reply(dev, &msg); | |
1109 | if (ret) { | |
1110 | error_setg(errp, "Failed to receive reply to postcopy_listen"); | |
1111 | return ret; | |
1112 | } | |
1113 | ||
1114 | return 0; | |
1115 | } | |
1116 | ||
9ccbfe14 DDAG |
1117 | static int vhost_user_postcopy_notifier(NotifierWithReturn *notifier, |
1118 | void *opaque) | |
1119 | { | |
1120 | struct PostcopyNotifyData *pnd = opaque; | |
1121 | struct vhost_user *u = container_of(notifier, struct vhost_user, | |
1122 | postcopy_notifier); | |
1123 | struct vhost_dev *dev = u->dev; | |
1124 | ||
1125 | switch (pnd->reason) { | |
1126 | case POSTCOPY_NOTIFY_PROBE: | |
1127 | if (!virtio_has_feature(dev->protocol_features, | |
1128 | VHOST_USER_PROTOCOL_F_PAGEFAULT)) { | |
1129 | /* TODO: Get the device name into this error somehow */ | |
1130 | error_setg(pnd->errp, | |
1131 | "vhost-user backend not capable of postcopy"); | |
1132 | return -ENOENT; | |
1133 | } | |
1134 | break; | |
1135 | ||
d3dff7a5 DDAG |
1136 | case POSTCOPY_NOTIFY_INBOUND_ADVISE: |
1137 | return vhost_user_postcopy_advise(dev, pnd->errp); | |
1138 | ||
6864a7b5 DDAG |
1139 | case POSTCOPY_NOTIFY_INBOUND_LISTEN: |
1140 | return vhost_user_postcopy_listen(dev, pnd->errp); | |
1141 | ||
9ccbfe14 DDAG |
1142 | default: |
1143 | /* We ignore notifications we don't know */ | |
1144 | break; | |
1145 | } | |
1146 | ||
1147 | return 0; | |
1148 | } | |
1149 | ||
5f6f6664 NN |
1150 | static int vhost_user_init(struct vhost_dev *dev, void *opaque) |
1151 | { | |
6dcdd06e | 1152 | uint64_t features, protocol_features; |
2152f3fe | 1153 | struct vhost_user *u; |
dcb10c00 MT |
1154 | int err; |
1155 | ||
5f6f6664 NN |
1156 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); |
1157 | ||
2152f3fe MAL |
1158 | u = g_new0(struct vhost_user, 1); |
1159 | u->chr = opaque; | |
4bbeeba0 | 1160 | u->slave_fd = -1; |
9ccbfe14 | 1161 | u->dev = dev; |
2152f3fe | 1162 | dev->opaque = u; |
5f6f6664 | 1163 | |
21e70425 | 1164 | err = vhost_user_get_features(dev, &features); |
dcb10c00 MT |
1165 | if (err < 0) { |
1166 | return err; | |
1167 | } | |
1168 | ||
1169 | if (virtio_has_feature(features, VHOST_USER_F_PROTOCOL_FEATURES)) { | |
1170 | dev->backend_features |= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES; | |
1171 | ||
21e70425 | 1172 | err = vhost_user_get_u64(dev, VHOST_USER_GET_PROTOCOL_FEATURES, |
6dcdd06e | 1173 | &protocol_features); |
dcb10c00 MT |
1174 | if (err < 0) { |
1175 | return err; | |
1176 | } | |
1177 | ||
6dcdd06e MC |
1178 | dev->protocol_features = |
1179 | protocol_features & VHOST_USER_PROTOCOL_FEATURE_MASK; | |
21e70425 | 1180 | err = vhost_user_set_protocol_features(dev, dev->protocol_features); |
dcb10c00 MT |
1181 | if (err < 0) { |
1182 | return err; | |
1183 | } | |
e2051e9e YL |
1184 | |
1185 | /* query the max queues we support if backend supports Multiple Queue */ | |
1186 | if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)) { | |
21e70425 MAL |
1187 | err = vhost_user_get_u64(dev, VHOST_USER_GET_QUEUE_NUM, |
1188 | &dev->max_queues); | |
e2051e9e YL |
1189 | if (err < 0) { |
1190 | return err; | |
1191 | } | |
1192 | } | |
6dcdd06e MC |
1193 | |
1194 | if (virtio_has_feature(features, VIRTIO_F_IOMMU_PLATFORM) && | |
1195 | !(virtio_has_feature(dev->protocol_features, | |
1196 | VHOST_USER_PROTOCOL_F_SLAVE_REQ) && | |
1197 | virtio_has_feature(dev->protocol_features, | |
1198 | VHOST_USER_PROTOCOL_F_REPLY_ACK))) { | |
1199 | error_report("IOMMU support requires reply-ack and " | |
1200 | "slave-req protocol features."); | |
1201 | return -1; | |
1202 | } | |
dcb10c00 MT |
1203 | } |
1204 | ||
d2fc4402 MAL |
1205 | if (dev->migration_blocker == NULL && |
1206 | !virtio_has_feature(dev->protocol_features, | |
1207 | VHOST_USER_PROTOCOL_F_LOG_SHMFD)) { | |
1208 | error_setg(&dev->migration_blocker, | |
1209 | "Migration disabled: vhost-user backend lacks " | |
1210 | "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature."); | |
1211 | } | |
1212 | ||
4bbeeba0 MAL |
1213 | err = vhost_setup_slave_channel(dev); |
1214 | if (err < 0) { | |
1215 | return err; | |
1216 | } | |
1217 | ||
9ccbfe14 DDAG |
1218 | u->postcopy_notifier.notify = vhost_user_postcopy_notifier; |
1219 | postcopy_add_notifier(&u->postcopy_notifier); | |
1220 | ||
5f6f6664 NN |
1221 | return 0; |
1222 | } | |
1223 | ||
1224 | static int vhost_user_cleanup(struct vhost_dev *dev) | |
1225 | { | |
2152f3fe MAL |
1226 | struct vhost_user *u; |
1227 | ||
5f6f6664 NN |
1228 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); |
1229 | ||
2152f3fe | 1230 | u = dev->opaque; |
9ccbfe14 DDAG |
1231 | if (u->postcopy_notifier.notify) { |
1232 | postcopy_remove_notifier(&u->postcopy_notifier); | |
1233 | u->postcopy_notifier.notify = NULL; | |
1234 | } | |
4bbeeba0 | 1235 | if (u->slave_fd >= 0) { |
b9ec9bd4 | 1236 | qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL); |
4bbeeba0 MAL |
1237 | close(u->slave_fd); |
1238 | u->slave_fd = -1; | |
1239 | } | |
905125d0 DDAG |
1240 | g_free(u->region_rb); |
1241 | u->region_rb = NULL; | |
1242 | g_free(u->region_rb_offset); | |
1243 | u->region_rb_offset = NULL; | |
1244 | u->region_rb_len = 0; | |
2152f3fe | 1245 | g_free(u); |
5f6f6664 NN |
1246 | dev->opaque = 0; |
1247 | ||
1248 | return 0; | |
1249 | } | |
1250 | ||
fc57fd99 YL |
1251 | static int vhost_user_get_vq_index(struct vhost_dev *dev, int idx) |
1252 | { | |
1253 | assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs); | |
1254 | ||
1255 | return idx; | |
1256 | } | |
1257 | ||
2ce68e4c IM |
1258 | static int vhost_user_memslots_limit(struct vhost_dev *dev) |
1259 | { | |
1260 | return VHOST_MEMORY_MAX_NREGIONS; | |
1261 | } | |
1262 | ||
1be0ac21 MAL |
1263 | static bool vhost_user_requires_shm_log(struct vhost_dev *dev) |
1264 | { | |
1265 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); | |
1266 | ||
1267 | return virtio_has_feature(dev->protocol_features, | |
1268 | VHOST_USER_PROTOCOL_F_LOG_SHMFD); | |
1269 | } | |
1270 | ||
3e866365 TC |
1271 | static int vhost_user_migration_done(struct vhost_dev *dev, char* mac_addr) |
1272 | { | |
1273 | VhostUserMsg msg = { 0 }; | |
3e866365 TC |
1274 | |
1275 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); | |
1276 | ||
1277 | /* If guest supports GUEST_ANNOUNCE do nothing */ | |
1278 | if (virtio_has_feature(dev->acked_features, VIRTIO_NET_F_GUEST_ANNOUNCE)) { | |
1279 | return 0; | |
1280 | } | |
1281 | ||
1282 | /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */ | |
1283 | if (virtio_has_feature(dev->protocol_features, | |
1284 | VHOST_USER_PROTOCOL_F_RARP)) { | |
24e34754 MT |
1285 | msg.hdr.request = VHOST_USER_SEND_RARP; |
1286 | msg.hdr.flags = VHOST_USER_VERSION; | |
7f4a930e | 1287 | memcpy((char *)&msg.payload.u64, mac_addr, 6); |
24e34754 | 1288 | msg.hdr.size = sizeof(msg.payload.u64); |
3e866365 | 1289 | |
c4843a45 | 1290 | return vhost_user_write(dev, &msg, NULL, 0); |
3e866365 TC |
1291 | } |
1292 | return -1; | |
1293 | } | |
1294 | ||
ffe42cc1 MT |
1295 | static bool vhost_user_can_merge(struct vhost_dev *dev, |
1296 | uint64_t start1, uint64_t size1, | |
1297 | uint64_t start2, uint64_t size2) | |
1298 | { | |
07bdaa41 | 1299 | ram_addr_t offset; |
ffe42cc1 MT |
1300 | int mfd, rfd; |
1301 | MemoryRegion *mr; | |
1302 | ||
07bdaa41 | 1303 | mr = memory_region_from_host((void *)(uintptr_t)start1, &offset); |
4ff87573 | 1304 | mfd = memory_region_get_fd(mr); |
ffe42cc1 | 1305 | |
07bdaa41 | 1306 | mr = memory_region_from_host((void *)(uintptr_t)start2, &offset); |
4ff87573 | 1307 | rfd = memory_region_get_fd(mr); |
ffe42cc1 MT |
1308 | |
1309 | return mfd == rfd; | |
1310 | } | |
1311 | ||
c5f048d8 MC |
1312 | static int vhost_user_net_set_mtu(struct vhost_dev *dev, uint16_t mtu) |
1313 | { | |
1314 | VhostUserMsg msg; | |
1315 | bool reply_supported = virtio_has_feature(dev->protocol_features, | |
1316 | VHOST_USER_PROTOCOL_F_REPLY_ACK); | |
1317 | ||
1318 | if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU))) { | |
1319 | return 0; | |
1320 | } | |
1321 | ||
24e34754 | 1322 | msg.hdr.request = VHOST_USER_NET_SET_MTU; |
c5f048d8 | 1323 | msg.payload.u64 = mtu; |
24e34754 MT |
1324 | msg.hdr.size = sizeof(msg.payload.u64); |
1325 | msg.hdr.flags = VHOST_USER_VERSION; | |
c5f048d8 | 1326 | if (reply_supported) { |
24e34754 | 1327 | msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK; |
c5f048d8 MC |
1328 | } |
1329 | ||
1330 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { | |
1331 | return -1; | |
1332 | } | |
1333 | ||
1334 | /* If reply_ack supported, slave has to ack specified MTU is valid */ | |
1335 | if (reply_supported) { | |
3cf7daf8 | 1336 | return process_message_reply(dev, &msg); |
c5f048d8 MC |
1337 | } |
1338 | ||
1339 | return 0; | |
1340 | } | |
1341 | ||
6dcdd06e MC |
1342 | static int vhost_user_send_device_iotlb_msg(struct vhost_dev *dev, |
1343 | struct vhost_iotlb_msg *imsg) | |
1344 | { | |
1345 | VhostUserMsg msg = { | |
24e34754 MT |
1346 | .hdr.request = VHOST_USER_IOTLB_MSG, |
1347 | .hdr.size = sizeof(msg.payload.iotlb), | |
1348 | .hdr.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK, | |
6dcdd06e MC |
1349 | .payload.iotlb = *imsg, |
1350 | }; | |
1351 | ||
1352 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { | |
1353 | return -EFAULT; | |
1354 | } | |
1355 | ||
1356 | return process_message_reply(dev, &msg); | |
1357 | } | |
1358 | ||
1359 | ||
1360 | static void vhost_user_set_iotlb_callback(struct vhost_dev *dev, int enabled) | |
1361 | { | |
1362 | /* No-op as the receive channel is not dedicated to IOTLB messages. */ | |
1363 | } | |
1364 | ||
4c3e257b CL |
1365 | static int vhost_user_get_config(struct vhost_dev *dev, uint8_t *config, |
1366 | uint32_t config_len) | |
1367 | { | |
1368 | VhostUserMsg msg = { | |
24e34754 MT |
1369 | .hdr.request = VHOST_USER_GET_CONFIG, |
1370 | .hdr.flags = VHOST_USER_VERSION, | |
1371 | .hdr.size = VHOST_USER_CONFIG_HDR_SIZE + config_len, | |
4c3e257b CL |
1372 | }; |
1373 | ||
1374 | if (config_len > VHOST_USER_MAX_CONFIG_SIZE) { | |
1375 | return -1; | |
1376 | } | |
1377 | ||
1378 | msg.payload.config.offset = 0; | |
1379 | msg.payload.config.size = config_len; | |
1380 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { | |
1381 | return -1; | |
1382 | } | |
1383 | ||
1384 | if (vhost_user_read(dev, &msg) < 0) { | |
1385 | return -1; | |
1386 | } | |
1387 | ||
24e34754 | 1388 | if (msg.hdr.request != VHOST_USER_GET_CONFIG) { |
4c3e257b | 1389 | error_report("Received unexpected msg type. Expected %d received %d", |
24e34754 | 1390 | VHOST_USER_GET_CONFIG, msg.hdr.request); |
4c3e257b CL |
1391 | return -1; |
1392 | } | |
1393 | ||
24e34754 | 1394 | if (msg.hdr.size != VHOST_USER_CONFIG_HDR_SIZE + config_len) { |
4c3e257b CL |
1395 | error_report("Received bad msg size."); |
1396 | return -1; | |
1397 | } | |
1398 | ||
1399 | memcpy(config, msg.payload.config.region, config_len); | |
1400 | ||
1401 | return 0; | |
1402 | } | |
1403 | ||
1404 | static int vhost_user_set_config(struct vhost_dev *dev, const uint8_t *data, | |
1405 | uint32_t offset, uint32_t size, uint32_t flags) | |
1406 | { | |
1407 | uint8_t *p; | |
1408 | bool reply_supported = virtio_has_feature(dev->protocol_features, | |
1409 | VHOST_USER_PROTOCOL_F_REPLY_ACK); | |
1410 | ||
1411 | VhostUserMsg msg = { | |
24e34754 MT |
1412 | .hdr.request = VHOST_USER_SET_CONFIG, |
1413 | .hdr.flags = VHOST_USER_VERSION, | |
1414 | .hdr.size = VHOST_USER_CONFIG_HDR_SIZE + size, | |
4c3e257b CL |
1415 | }; |
1416 | ||
1417 | if (reply_supported) { | |
24e34754 | 1418 | msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK; |
4c3e257b CL |
1419 | } |
1420 | ||
1421 | if (size > VHOST_USER_MAX_CONFIG_SIZE) { | |
1422 | return -1; | |
1423 | } | |
1424 | ||
1425 | msg.payload.config.offset = offset, | |
1426 | msg.payload.config.size = size, | |
1427 | msg.payload.config.flags = flags, | |
1428 | p = msg.payload.config.region; | |
1429 | memcpy(p, data, size); | |
1430 | ||
1431 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { | |
1432 | return -1; | |
1433 | } | |
1434 | ||
1435 | if (reply_supported) { | |
1436 | return process_message_reply(dev, &msg); | |
1437 | } | |
1438 | ||
1439 | return 0; | |
1440 | } | |
1441 | ||
efbfeb81 GA |
1442 | static int vhost_user_crypto_create_session(struct vhost_dev *dev, |
1443 | void *session_info, | |
1444 | uint64_t *session_id) | |
1445 | { | |
1446 | bool crypto_session = virtio_has_feature(dev->protocol_features, | |
1447 | VHOST_USER_PROTOCOL_F_CRYPTO_SESSION); | |
1448 | CryptoDevBackendSymSessionInfo *sess_info = session_info; | |
1449 | VhostUserMsg msg = { | |
1450 | .hdr.request = VHOST_USER_CREATE_CRYPTO_SESSION, | |
1451 | .hdr.flags = VHOST_USER_VERSION, | |
1452 | .hdr.size = sizeof(msg.payload.session), | |
1453 | }; | |
1454 | ||
1455 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); | |
1456 | ||
1457 | if (!crypto_session) { | |
1458 | error_report("vhost-user trying to send unhandled ioctl"); | |
1459 | return -1; | |
1460 | } | |
1461 | ||
1462 | memcpy(&msg.payload.session.session_setup_data, sess_info, | |
1463 | sizeof(CryptoDevBackendSymSessionInfo)); | |
1464 | if (sess_info->key_len) { | |
1465 | memcpy(&msg.payload.session.key, sess_info->cipher_key, | |
1466 | sess_info->key_len); | |
1467 | } | |
1468 | if (sess_info->auth_key_len > 0) { | |
1469 | memcpy(&msg.payload.session.auth_key, sess_info->auth_key, | |
1470 | sess_info->auth_key_len); | |
1471 | } | |
1472 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { | |
1473 | error_report("vhost_user_write() return -1, create session failed"); | |
1474 | return -1; | |
1475 | } | |
1476 | ||
1477 | if (vhost_user_read(dev, &msg) < 0) { | |
1478 | error_report("vhost_user_read() return -1, create session failed"); | |
1479 | return -1; | |
1480 | } | |
1481 | ||
1482 | if (msg.hdr.request != VHOST_USER_CREATE_CRYPTO_SESSION) { | |
1483 | error_report("Received unexpected msg type. Expected %d received %d", | |
1484 | VHOST_USER_CREATE_CRYPTO_SESSION, msg.hdr.request); | |
1485 | return -1; | |
1486 | } | |
1487 | ||
1488 | if (msg.hdr.size != sizeof(msg.payload.session)) { | |
1489 | error_report("Received bad msg size."); | |
1490 | return -1; | |
1491 | } | |
1492 | ||
1493 | if (msg.payload.session.session_id < 0) { | |
1494 | error_report("Bad session id: %" PRId64 "", | |
1495 | msg.payload.session.session_id); | |
1496 | return -1; | |
1497 | } | |
1498 | *session_id = msg.payload.session.session_id; | |
1499 | ||
1500 | return 0; | |
1501 | } | |
1502 | ||
1503 | static int | |
1504 | vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id) | |
1505 | { | |
1506 | bool crypto_session = virtio_has_feature(dev->protocol_features, | |
1507 | VHOST_USER_PROTOCOL_F_CRYPTO_SESSION); | |
1508 | VhostUserMsg msg = { | |
1509 | .hdr.request = VHOST_USER_CLOSE_CRYPTO_SESSION, | |
1510 | .hdr.flags = VHOST_USER_VERSION, | |
1511 | .hdr.size = sizeof(msg.payload.u64), | |
1512 | }; | |
1513 | msg.payload.u64 = session_id; | |
1514 | ||
1515 | if (!crypto_session) { | |
1516 | error_report("vhost-user trying to send unhandled ioctl"); | |
1517 | return -1; | |
1518 | } | |
1519 | ||
1520 | if (vhost_user_write(dev, &msg, NULL, 0) < 0) { | |
1521 | error_report("vhost_user_write() return -1, close session failed"); | |
1522 | return -1; | |
1523 | } | |
1524 | ||
1525 | return 0; | |
1526 | } | |
1527 | ||
5f6f6664 NN |
1528 | const VhostOps user_ops = { |
1529 | .backend_type = VHOST_BACKEND_TYPE_USER, | |
5f6f6664 | 1530 | .vhost_backend_init = vhost_user_init, |
fc57fd99 | 1531 | .vhost_backend_cleanup = vhost_user_cleanup, |
2ce68e4c | 1532 | .vhost_backend_memslots_limit = vhost_user_memslots_limit, |
21e70425 MAL |
1533 | .vhost_set_log_base = vhost_user_set_log_base, |
1534 | .vhost_set_mem_table = vhost_user_set_mem_table, | |
1535 | .vhost_set_vring_addr = vhost_user_set_vring_addr, | |
1536 | .vhost_set_vring_endian = vhost_user_set_vring_endian, | |
1537 | .vhost_set_vring_num = vhost_user_set_vring_num, | |
1538 | .vhost_set_vring_base = vhost_user_set_vring_base, | |
1539 | .vhost_get_vring_base = vhost_user_get_vring_base, | |
1540 | .vhost_set_vring_kick = vhost_user_set_vring_kick, | |
1541 | .vhost_set_vring_call = vhost_user_set_vring_call, | |
1542 | .vhost_set_features = vhost_user_set_features, | |
1543 | .vhost_get_features = vhost_user_get_features, | |
1544 | .vhost_set_owner = vhost_user_set_owner, | |
1545 | .vhost_reset_device = vhost_user_reset_device, | |
1546 | .vhost_get_vq_index = vhost_user_get_vq_index, | |
1547 | .vhost_set_vring_enable = vhost_user_set_vring_enable, | |
1be0ac21 | 1548 | .vhost_requires_shm_log = vhost_user_requires_shm_log, |
3e866365 | 1549 | .vhost_migration_done = vhost_user_migration_done, |
ffe42cc1 | 1550 | .vhost_backend_can_merge = vhost_user_can_merge, |
c5f048d8 | 1551 | .vhost_net_set_mtu = vhost_user_net_set_mtu, |
6dcdd06e MC |
1552 | .vhost_set_iotlb_callback = vhost_user_set_iotlb_callback, |
1553 | .vhost_send_device_iotlb_msg = vhost_user_send_device_iotlb_msg, | |
4c3e257b CL |
1554 | .vhost_get_config = vhost_user_get_config, |
1555 | .vhost_set_config = vhost_user_set_config, | |
efbfeb81 GA |
1556 | .vhost_crypto_create_session = vhost_user_crypto_create_session, |
1557 | .vhost_crypto_close_session = vhost_user_crypto_close_session, | |
fc57fd99 | 1558 | }; |