Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2018 Intel Corporation
3 : : */
4 : :
5 : : /* Security model
6 : : * --------------
7 : : * The vhost-user protocol connection is an external interface, so it must be
8 : : * robust against invalid inputs.
9 : : *
10 : : * This is important because the vhost-user frontend is only one step removed
11 : : * from the guest. Malicious guests that have escaped will then launch further
12 : : * attacks from the vhost-user frontend.
13 : : *
14 : : * Even in deployments where guests are trusted, a bug in the vhost-user frontend
15 : : * can still cause invalid messages to be sent. Such messages must not
16 : : * compromise the stability of the DPDK application by causing crashes, memory
17 : : * corruption, or other problematic behavior.
18 : : *
19 : : * Do not assume received VhostUserMsg fields contain sensible values!
20 : : */
21 : :
22 : : #include <assert.h>
23 : : #include <stdint.h>
24 : : #include <stdio.h>
25 : : #include <stdlib.h>
26 : : #include <string.h>
27 : : #include <unistd.h>
28 : : #include <fcntl.h>
29 : : #include <sys/ioctl.h>
30 : : #include <sys/mman.h>
31 : : #include <sys/stat.h>
32 : : #include <sys/syscall.h>
33 : : #ifdef RTE_LIBRTE_VHOST_NUMA
34 : : #include <numaif.h>
35 : : #endif
36 : : #ifdef RTE_LIBRTE_VHOST_POSTCOPY
37 : : #include <linux/userfaultfd.h>
38 : : #endif
39 : : #ifdef F_ADD_SEALS /* if file sealing is supported, so is memfd */
40 : : #include <linux/memfd.h>
41 : : #define MEMFD_SUPPORTED
42 : : #endif
43 : :
44 : : #include <rte_common.h>
45 : : #include <rte_malloc.h>
46 : : #include <rte_log.h>
47 : : #include <rte_vfio.h>
48 : : #include <rte_errno.h>
49 : :
50 : : #include "iotlb.h"
51 : : #include "vhost.h"
52 : : #include "vhost_user.h"
53 : :
54 : : #define VIRTIO_MIN_MTU 68
55 : : #define VIRTIO_MAX_MTU 65535
56 : :
57 : : #define INFLIGHT_ALIGNMENT 64
58 : : #define INFLIGHT_VERSION 0x1
59 : :
60 : : typedef const struct vhost_message_handler {
61 : : const char *description;
62 : : int (*callback)(struct virtio_net **pdev, struct vhu_msg_context *ctx,
63 : : int main_fd);
64 : : bool accepts_fd;
65 : : bool lock_all_qps;
66 : : } vhost_message_handler_t;
67 : : static vhost_message_handler_t vhost_message_handlers[];
68 : :
69 : : #define VHOST_MESSAGE_HANDLERS \
70 : : VHOST_MESSAGE_HANDLER(VHOST_USER_NONE, NULL, false, false) \
71 : : VHOST_MESSAGE_HANDLER(VHOST_USER_GET_FEATURES, vhost_user_get_features, false, false) \
72 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_FEATURES, vhost_user_set_features, false, true) \
73 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_OWNER, vhost_user_set_owner, false, true) \
74 : : VHOST_MESSAGE_HANDLER(VHOST_USER_RESET_OWNER, vhost_user_reset_owner, false, false) \
75 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_MEM_TABLE, vhost_user_set_mem_table, true, true) \
76 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_BASE, vhost_user_set_log_base, true, true) \
77 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_FD, vhost_user_set_log_fd, true, true) \
78 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_NUM, vhost_user_set_vring_num, false, true) \
79 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ADDR, vhost_user_set_vring_addr, false, true) \
80 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_BASE, vhost_user_set_vring_base, false, true) \
81 : : VHOST_MESSAGE_HANDLER(VHOST_USER_GET_VRING_BASE, vhost_user_get_vring_base, false, false) \
82 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_KICK, vhost_user_set_vring_kick, true, true) \
83 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_CALL, vhost_user_set_vring_call, true, true) \
84 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ERR, vhost_user_set_vring_err, true, true) \
85 : : VHOST_MESSAGE_HANDLER(VHOST_USER_GET_PROTOCOL_FEATURES, vhost_user_get_protocol_features, \
86 : : false, false) \
87 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_PROTOCOL_FEATURES, vhost_user_set_protocol_features, \
88 : : false, true) \
89 : : VHOST_MESSAGE_HANDLER(VHOST_USER_GET_QUEUE_NUM, vhost_user_get_queue_num, false, false) \
90 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ENABLE, vhost_user_set_vring_enable, false, true) \
91 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SEND_RARP, vhost_user_send_rarp, false, true) \
92 : : VHOST_MESSAGE_HANDLER(VHOST_USER_NET_SET_MTU, vhost_user_net_set_mtu, false, true) \
93 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_BACKEND_REQ_FD, vhost_user_set_req_fd, true, true) \
94 : : VHOST_MESSAGE_HANDLER(VHOST_USER_IOTLB_MSG, vhost_user_iotlb_msg, false, false) \
95 : : VHOST_MESSAGE_HANDLER(VHOST_USER_GET_CONFIG, vhost_user_get_config, false, false) \
96 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_CONFIG, vhost_user_set_config, false, false) \
97 : : VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_ADVISE, vhost_user_set_postcopy_advise, false, false) \
98 : : VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_LISTEN, vhost_user_set_postcopy_listen, false, false) \
99 : : VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_END, vhost_user_postcopy_end, false, false) \
100 : : VHOST_MESSAGE_HANDLER(VHOST_USER_GET_INFLIGHT_FD, vhost_user_get_inflight_fd, false, false) \
101 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_INFLIGHT_FD, vhost_user_set_inflight_fd, true, false) \
102 : : VHOST_MESSAGE_HANDLER(VHOST_USER_SET_STATUS, vhost_user_set_status, false, false) \
103 : : VHOST_MESSAGE_HANDLER(VHOST_USER_GET_STATUS, vhost_user_get_status, false, false)
104 : :
105 : : #define VHOST_MESSAGE_HANDLER(id, handler, accepts_fd, lock_all_qps) \
106 : : id ## _LOCK_ALL_QPS = lock_all_qps,
107 : : enum {
108 : : VHOST_MESSAGE_HANDLERS
109 : : };
110 : : #undef VHOST_MESSAGE_HANDLER
111 : :
112 : : /* vhost_user_msg_handler() locks all qps based on a handler's lock_all_qps.
113 : : * Later, a handler may need to ensure the vq has been locked (for example,
114 : : * when calling lock annotated helpers).
115 : : *
116 : : * Note: unfortunately, static_assert() does not see an array content as a
117 : : * constant expression. Because of this, we can't simply check for
118 : : * vhost_user_msg_handler[].lock_all_qps.
119 : : * Instead, define an enum for each handler.
120 : : */
121 : : #define VHOST_USER_ASSERT_LOCK(dev, vq, id) do { \
122 : : static_assert(id ## _LOCK_ALL_QPS == true, \
123 : : #id " handler is not declared as locking all queue pairs"); \
124 : : vq_assert_lock(dev, vq); \
125 : : } while (0)
126 : :
127 : : static int send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx);
128 : : static int read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx);
129 : :
130 : : static void
131 : 0 : close_msg_fds(struct vhu_msg_context *ctx)
132 : : {
133 : : int i;
134 : :
135 [ # # ]: 0 : for (i = 0; i < ctx->fd_num; i++) {
136 : 0 : int fd = ctx->fds[i];
137 : :
138 [ # # ]: 0 : if (fd == -1)
139 : 0 : continue;
140 : :
141 : 0 : ctx->fds[i] = -1;
142 : 0 : close(fd);
143 : : }
144 : 0 : }
145 : :
146 : : /*
147 : : * Ensure the expected number of FDs is received,
148 : : * close all FDs and return an error if this is not the case.
149 : : */
150 : : static int
151 : 0 : validate_msg_fds(struct virtio_net *dev, struct vhu_msg_context *ctx, int expected_fds)
152 : : {
153 [ # # ]: 0 : if (ctx->fd_num == expected_fds)
154 : : return 0;
155 : :
156 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
157 : : "expect %d FDs for request %s, received %d",
158 : : expected_fds, vhost_message_handlers[ctx->msg.request.frontend].description,
159 : : ctx->fd_num);
160 : :
161 : 0 : close_msg_fds(ctx);
162 : :
163 : 0 : return -1;
164 : : }
165 : :
166 : : static uint64_t
167 : : get_blk_size(int fd)
168 : : {
169 : : struct stat stat;
170 : : int ret;
171 : :
172 : 0 : ret = fstat(fd, &stat);
173 [ # # # # : 0 : return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
# # # # #
# ]
174 : : }
175 : :
176 : : static void
177 : 0 : async_dma_map(struct virtio_net *dev, bool do_map)
178 : : {
179 : : int ret = 0;
180 : : uint32_t i;
181 : : struct guest_page *page;
182 : :
183 [ # # ]: 0 : if (do_map) {
184 [ # # ]: 0 : for (i = 0; i < dev->nr_guest_pages; i++) {
185 : 0 : page = &dev->guest_pages[i];
186 : 0 : ret = rte_vfio_container_dma_map(RTE_VFIO_DEFAULT_CONTAINER_FD,
187 : : page->host_user_addr,
188 : : page->host_iova,
189 : : page->size);
190 [ # # ]: 0 : if (ret) {
191 : : /*
192 : : * DMA device may bind with kernel driver, in this case,
193 : : * we don't need to program IOMMU manually. However, if no
194 : : * device is bound with vfio/uio in DPDK, and vfio kernel
195 : : * module is loaded, the API will still be called and return
196 : : * with ENODEV.
197 : : *
198 : : * DPDK vfio only returns ENODEV in very similar situations
199 : : * (vfio either unsupported, or supported but no devices found).
200 : : * Either way, no mappings could be performed. We treat it as
201 : : * normal case in async path. This is a workaround.
202 : : */
203 [ # # ]: 0 : if (rte_errno == ENODEV)
204 : : return;
205 : :
206 : : /* DMA mapping errors won't stop VHOST_USER_SET_MEM_TABLE. */
207 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "DMA engine map failed");
208 : : }
209 : : }
210 : :
211 : : } else {
212 [ # # ]: 0 : for (i = 0; i < dev->nr_guest_pages; i++) {
213 : 0 : page = &dev->guest_pages[i];
214 : 0 : ret = rte_vfio_container_dma_unmap(RTE_VFIO_DEFAULT_CONTAINER_FD,
215 : : page->host_user_addr,
216 : : page->host_iova,
217 : : page->size);
218 [ # # ]: 0 : if (ret) {
219 : : /* like DMA map, ignore the kernel driver case when unmap. */
220 [ # # ]: 0 : if (rte_errno == EINVAL)
221 : : return;
222 : :
223 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "DMA engine unmap failed");
224 : : }
225 : : }
226 : : }
227 : : }
228 : :
229 : : static void
230 : 0 : free_mem_region(struct virtio_net *dev)
231 : : {
232 : : uint32_t i;
233 : : struct rte_vhost_mem_region *reg;
234 : :
235 [ # # # # ]: 0 : if (!dev || !dev->mem)
236 : : return;
237 : :
238 [ # # # # ]: 0 : if (dev->async_copy && rte_vfio_is_enabled("vfio"))
239 : 0 : async_dma_map(dev, false);
240 : :
241 [ # # ]: 0 : for (i = 0; i < dev->mem->nregions; i++) {
242 : : reg = &dev->mem->regions[i];
243 [ # # ]: 0 : if (reg->host_user_addr) {
244 : 0 : munmap(reg->mmap_addr, reg->mmap_size);
245 : 0 : close(reg->fd);
246 : : }
247 : : }
248 : : }
249 : :
250 : : void
251 : 0 : vhost_backend_cleanup(struct virtio_net *dev)
252 : : {
253 : : struct rte_vdpa_device *vdpa_dev;
254 : :
255 : 0 : vdpa_dev = dev->vdpa_dev;
256 [ # # # # ]: 0 : if (vdpa_dev && vdpa_dev->ops->dev_cleanup != NULL)
257 : 0 : vdpa_dev->ops->dev_cleanup(dev->vid);
258 : :
259 [ # # ]: 0 : if (dev->mem) {
260 : 0 : free_mem_region(dev);
261 : 0 : rte_free(dev->mem);
262 : 0 : dev->mem = NULL;
263 : : }
264 : :
265 : 0 : rte_free(dev->guest_pages);
266 : 0 : dev->guest_pages = NULL;
267 : :
268 [ # # ]: 0 : if (dev->log_addr) {
269 : 0 : munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
270 : 0 : dev->log_addr = 0;
271 : : }
272 : :
273 [ # # ]: 0 : if (dev->inflight_info) {
274 [ # # ]: 0 : if (dev->inflight_info->addr) {
275 : 0 : munmap(dev->inflight_info->addr,
276 : : dev->inflight_info->size);
277 : 0 : dev->inflight_info->addr = NULL;
278 : : }
279 : :
280 [ # # ]: 0 : if (dev->inflight_info->fd >= 0) {
281 : 0 : close(dev->inflight_info->fd);
282 : 0 : dev->inflight_info->fd = -1;
283 : : }
284 : :
285 : 0 : rte_free(dev->inflight_info);
286 : 0 : dev->inflight_info = NULL;
287 : : }
288 : :
289 [ # # ]: 0 : if (dev->backend_req_fd >= 0) {
290 : 0 : close(dev->backend_req_fd);
291 : 0 : dev->backend_req_fd = -1;
292 : : }
293 : :
294 [ # # ]: 0 : if (dev->postcopy_ufd >= 0) {
295 : 0 : close(dev->postcopy_ufd);
296 : 0 : dev->postcopy_ufd = -1;
297 : : }
298 : :
299 : 0 : dev->postcopy_listening = 0;
300 : :
301 : 0 : vhost_user_iotlb_destroy(dev);
302 : 0 : }
303 : :
304 : : static void
305 : 0 : vhost_user_notify_queue_state(struct virtio_net *dev, struct vhost_virtqueue *vq,
306 : : int enable)
307 : : {
308 : 0 : struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
309 : :
310 : : /* Configure guest notifications on enable */
311 [ # # # # ]: 0 : if (enable && vq->notif_enable != VIRTIO_UNINITIALIZED_NOTIF)
312 : 0 : vhost_enable_guest_notification(dev, vq, vq->notif_enable);
313 : :
314 [ # # # # ]: 0 : if (vdpa_dev && vdpa_dev->ops->set_vring_state)
315 : 0 : vdpa_dev->ops->set_vring_state(dev->vid, vq->index, enable);
316 : :
317 [ # # ]: 0 : if (dev->notify_ops->vring_state_changed)
318 : 0 : dev->notify_ops->vring_state_changed(dev->vid, vq->index, enable);
319 : 0 : }
320 : :
321 : : /*
322 : : * This function just returns success at the moment unless
323 : : * the device hasn't been initialised.
324 : : */
325 : : static int
326 : 0 : vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
327 : : struct vhu_msg_context *ctx __rte_unused,
328 : : int main_fd __rte_unused)
329 : : {
330 : 0 : return RTE_VHOST_MSG_RESULT_OK;
331 : : }
332 : :
333 : : static int
334 : 0 : vhost_user_reset_owner(struct virtio_net **pdev,
335 : : struct vhu_msg_context *ctx __rte_unused,
336 : : int main_fd __rte_unused)
337 : : {
338 : 0 : struct virtio_net *dev = *pdev;
339 : :
340 : 0 : vhost_destroy_device_notify(dev);
341 : :
342 : 0 : cleanup_device(dev, 0);
343 : 0 : reset_device(dev);
344 : 0 : return RTE_VHOST_MSG_RESULT_OK;
345 : : }
346 : :
347 : : /*
348 : : * The features that we support are requested.
349 : : */
350 : : static int
351 : 0 : vhost_user_get_features(struct virtio_net **pdev,
352 : : struct vhu_msg_context *ctx,
353 : : int main_fd __rte_unused)
354 : : {
355 : 0 : struct virtio_net *dev = *pdev;
356 : 0 : uint64_t features = 0;
357 : :
358 : 0 : rte_vhost_driver_get_features(dev->ifname, &features);
359 : :
360 : 0 : ctx->msg.payload.u64 = features;
361 : 0 : ctx->msg.size = sizeof(ctx->msg.payload.u64);
362 : 0 : ctx->fd_num = 0;
363 : :
364 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
365 : : }
366 : :
367 : : /*
368 : : * The queue number that we support are requested.
369 : : */
370 : : static int
371 : 0 : vhost_user_get_queue_num(struct virtio_net **pdev,
372 : : struct vhu_msg_context *ctx,
373 : : int main_fd __rte_unused)
374 : : {
375 : 0 : struct virtio_net *dev = *pdev;
376 : 0 : uint32_t queue_num = 0;
377 : :
378 : 0 : rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
379 : :
380 : 0 : ctx->msg.payload.u64 = (uint64_t)queue_num;
381 : 0 : ctx->msg.size = sizeof(ctx->msg.payload.u64);
382 : 0 : ctx->fd_num = 0;
383 : :
384 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
385 : : }
386 : :
387 : : /*
388 : : * We receive the negotiated features supported by us and the virtio device.
389 : : */
390 : : static int
391 : 0 : vhost_user_set_features(struct virtio_net **pdev,
392 : : struct vhu_msg_context *ctx,
393 : : int main_fd __rte_unused)
394 : : {
395 : 0 : struct virtio_net *dev = *pdev;
396 : 0 : uint64_t features = ctx->msg.payload.u64;
397 : 0 : uint64_t vhost_features = 0;
398 : : struct rte_vdpa_device *vdpa_dev;
399 : :
400 : 0 : rte_vhost_driver_get_features(dev->ifname, &vhost_features);
401 [ # # ]: 0 : if (features & ~vhost_features) {
402 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "received invalid negotiated features.");
403 : 0 : dev->flags |= VIRTIO_DEV_FEATURES_FAILED;
404 : 0 : dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
405 : :
406 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
407 : : }
408 : :
409 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_RUNNING) {
410 [ # # ]: 0 : if (dev->features == features)
411 : : return RTE_VHOST_MSG_RESULT_OK;
412 : :
413 : : /*
414 : : * Error out if frontend tries to change features while device is
415 : : * in running state. The exception being VHOST_F_LOG_ALL, which
416 : : * is enabled when the live-migration starts.
417 : : */
418 [ # # ]: 0 : if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) {
419 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
420 : : "features changed while device is running.");
421 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
422 : : }
423 : :
424 [ # # ]: 0 : if (dev->notify_ops->features_changed)
425 : 0 : dev->notify_ops->features_changed(dev->vid, features);
426 : : }
427 : :
428 : 0 : dev->features = features;
429 [ # # ]: 0 : if (dev->features &
430 : : ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
431 : : (1ULL << VIRTIO_F_VERSION_1) |
432 : : (1ULL << VIRTIO_F_RING_PACKED))) {
433 : 0 : dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
434 : : } else {
435 : 0 : dev->vhost_hlen = sizeof(struct virtio_net_hdr);
436 : : }
437 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
438 : : "negotiated Virtio features: 0x%" PRIx64,
439 : : dev->features);
440 [ # # # # ]: 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG,
441 : : "mergeable RX buffers %s, virtio 1 %s",
442 : : (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
443 : : (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
444 : :
445 [ # # ]: 0 : if ((dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) &&
446 [ # # ]: 0 : !(dev->features & (1ULL << VIRTIO_NET_F_MQ))) {
447 : : /*
448 : : * Remove all but first queue pair if MQ hasn't been
449 : : * negotiated. This is safe because the device is not
450 : : * running at this stage.
451 : : */
452 [ # # ]: 0 : while (dev->nr_vring > 2) {
453 : : struct vhost_virtqueue *vq;
454 : :
455 : 0 : vq = dev->virtqueue[--dev->nr_vring];
456 [ # # ]: 0 : if (!vq)
457 : 0 : continue;
458 : :
459 : 0 : dev->virtqueue[dev->nr_vring] = NULL;
460 : 0 : cleanup_vq(vq, 1);
461 : 0 : cleanup_vq_inflight(dev, vq);
462 : : /* vhost_user_lock_all_queue_pairs locked all qps */
463 : 0 : VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_FEATURES);
464 : : rte_rwlock_write_unlock(&vq->access_lock);
465 : 0 : free_vq(dev, vq);
466 : : }
467 : : }
468 : :
469 : 0 : vdpa_dev = dev->vdpa_dev;
470 [ # # ]: 0 : if (vdpa_dev)
471 : 0 : vdpa_dev->ops->set_features(dev->vid);
472 : :
473 : 0 : dev->flags &= ~VIRTIO_DEV_FEATURES_FAILED;
474 : 0 : return RTE_VHOST_MSG_RESULT_OK;
475 : : }
476 : :
477 : : /*
478 : : * The virtio device sends us the size of the descriptor ring.
479 : : */
480 : : static int
481 : 0 : vhost_user_set_vring_num(struct virtio_net **pdev,
482 : : struct vhu_msg_context *ctx,
483 : : int main_fd __rte_unused)
484 : : {
485 : 0 : struct virtio_net *dev = *pdev;
486 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
487 : :
488 [ # # ]: 0 : if (ctx->msg.payload.state.num > 32768) {
489 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
490 : : "invalid virtqueue size %u",
491 : : ctx->msg.payload.state.num);
492 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
493 : : }
494 : :
495 [ # # ]: 0 : vq->size = ctx->msg.payload.state.num;
496 : :
497 : : /* VIRTIO 1.0, 2.4 Virtqueues says:
498 : : *
499 : : * Queue Size value is always a power of 2. The maximum Queue Size
500 : : * value is 32768.
501 : : *
502 : : * VIRTIO 1.1 2.7 Virtqueues says:
503 : : *
504 : : * Packed virtqueues support up to 2^15 entries each.
505 : : */
506 [ # # ]: 0 : if (!vq_is_packed(dev)) {
507 [ # # ]: 0 : if (vq->size & (vq->size - 1)) {
508 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
509 : : "invalid virtqueue size %u",
510 : : vq->size);
511 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
512 : : }
513 : : }
514 : :
515 [ # # ]: 0 : if (vq_is_packed(dev)) {
516 : 0 : rte_free(vq->shadow_used_packed);
517 : 0 : vq->shadow_used_packed = rte_malloc_socket(NULL,
518 : 0 : vq->size *
519 : : sizeof(struct vring_used_elem_packed),
520 : : RTE_CACHE_LINE_SIZE, vq->numa_node);
521 [ # # ]: 0 : if (!vq->shadow_used_packed) {
522 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
523 : : "failed to allocate memory for shadow used ring.");
524 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
525 : : }
526 : :
527 : : } else {
528 : 0 : rte_free(vq->shadow_used_split);
529 : :
530 : 0 : vq->shadow_used_split = rte_malloc_socket(NULL,
531 : 0 : vq->size * sizeof(struct vring_used_elem),
532 : : RTE_CACHE_LINE_SIZE, vq->numa_node);
533 : :
534 [ # # ]: 0 : if (!vq->shadow_used_split) {
535 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
536 : : "failed to allocate memory for vq internal data.");
537 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
538 : : }
539 : : }
540 : :
541 : 0 : rte_free(vq->batch_copy_elems);
542 : 0 : vq->batch_copy_elems = rte_malloc_socket(NULL,
543 : 0 : vq->size * sizeof(struct batch_copy_elem),
544 : : RTE_CACHE_LINE_SIZE, vq->numa_node);
545 [ # # ]: 0 : if (!vq->batch_copy_elems) {
546 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
547 : : "failed to allocate memory for batching copy.");
548 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
549 : : }
550 : :
551 : : return RTE_VHOST_MSG_RESULT_OK;
552 : : }
553 : :
554 : : /*
555 : : * Reallocate virtio_dev, vhost_virtqueue and related data structures to
556 : : * make them on the same numa node as the memory of vring descriptor.
557 : : */
558 : : #ifdef RTE_LIBRTE_VHOST_NUMA
559 : : static void
560 : 0 : numa_realloc(struct virtio_net **pdev, struct vhost_virtqueue **pvq)
561 : : {
562 : : int node, dev_node;
563 : : struct virtio_net *dev;
564 : : struct vhost_virtqueue *vq;
565 : : struct batch_copy_elem *bce;
566 : : struct guest_page *gp;
567 : : struct rte_vhost_memory *mem;
568 : : size_t mem_size;
569 : : int ret;
570 : :
571 : 0 : dev = *pdev;
572 : 0 : vq = *pvq;
573 : :
574 : : /*
575 : : * If VQ is ready, it is too late to reallocate, it certainly already
576 : : * happened anyway on VHOST_USER_SET_VRING_ADRR.
577 : : */
578 [ # # ]: 0 : if (vq->ready)
579 : 0 : return;
580 : :
581 : 0 : ret = get_mempolicy(&node, NULL, 0, vq->desc, MPOL_F_NODE | MPOL_F_ADDR);
582 [ # # ]: 0 : if (ret) {
583 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
584 : : "unable to get virtqueue %d numa information.",
585 : : vq->index);
586 : 0 : return;
587 : : }
588 : :
589 [ # # ]: 0 : if (node == vq->numa_node)
590 : 0 : goto out_dev_realloc;
591 : :
592 : 0 : vq = rte_realloc_socket(*pvq, sizeof(**pvq), 0, node);
593 [ # # ]: 0 : if (!vq) {
594 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
595 : : "failed to realloc virtqueue %d on node %d",
596 : : (*pvq)->index, node);
597 : 0 : return;
598 : : }
599 : 0 : *pvq = vq;
600 : :
601 [ # # ]: 0 : if (vq != dev->virtqueue[vq->index]) {
602 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "reallocated virtqueue on node %d", node);
603 : 0 : dev->virtqueue[vq->index] = vq;
604 : : }
605 : :
606 [ # # ]: 0 : if (vq_is_packed(dev)) {
607 : : struct vring_used_elem_packed *sup;
608 : :
609 : 0 : sup = rte_realloc_socket(vq->shadow_used_packed, vq->size * sizeof(*sup),
610 : : RTE_CACHE_LINE_SIZE, node);
611 [ # # ]: 0 : if (!sup) {
612 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
613 : : "failed to realloc shadow packed on node %d",
614 : : node);
615 : 0 : return;
616 : : }
617 : 0 : vq->shadow_used_packed = sup;
618 : : } else {
619 : : struct vring_used_elem *sus;
620 : :
621 : 0 : sus = rte_realloc_socket(vq->shadow_used_split, vq->size * sizeof(*sus),
622 : : RTE_CACHE_LINE_SIZE, node);
623 [ # # ]: 0 : if (!sus) {
624 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
625 : : "failed to realloc shadow split on node %d",
626 : : node);
627 : 0 : return;
628 : : }
629 : 0 : vq->shadow_used_split = sus;
630 : : }
631 : :
632 : 0 : bce = rte_realloc_socket(vq->batch_copy_elems, vq->size * sizeof(*bce),
633 : : RTE_CACHE_LINE_SIZE, node);
634 [ # # ]: 0 : if (!bce) {
635 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
636 : : "failed to realloc batch copy elem on node %d",
637 : : node);
638 : 0 : return;
639 : : }
640 : 0 : vq->batch_copy_elems = bce;
641 : :
642 [ # # ]: 0 : if (vq->log_cache) {
643 : : struct log_cache_entry *lc;
644 : :
645 : 0 : lc = rte_realloc_socket(vq->log_cache, sizeof(*lc) * VHOST_LOG_CACHE_NR, 0, node);
646 [ # # ]: 0 : if (!lc) {
647 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
648 : : "failed to realloc log cache on node %d",
649 : : node);
650 : 0 : return;
651 : : }
652 : 0 : vq->log_cache = lc;
653 : : }
654 : :
655 [ # # ]: 0 : if (vq->resubmit_inflight) {
656 : : struct rte_vhost_resubmit_info *ri;
657 : :
658 : 0 : ri = rte_realloc_socket(vq->resubmit_inflight, sizeof(*ri), 0, node);
659 [ # # ]: 0 : if (!ri) {
660 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
661 : : "failed to realloc resubmit inflight on node %d",
662 : : node);
663 : 0 : return;
664 : : }
665 : 0 : vq->resubmit_inflight = ri;
666 : :
667 [ # # ]: 0 : if (ri->resubmit_list) {
668 : : struct rte_vhost_resubmit_desc *rd;
669 : :
670 : 0 : rd = rte_realloc_socket(ri->resubmit_list, sizeof(*rd) * ri->resubmit_num,
671 : : 0, node);
672 [ # # ]: 0 : if (!rd) {
673 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
674 : : "failed to realloc resubmit list on node %d",
675 : : node);
676 : 0 : return;
677 : : }
678 : 0 : ri->resubmit_list = rd;
679 : : }
680 : : }
681 : :
682 : 0 : vq->numa_node = node;
683 : :
684 : 0 : out_dev_realloc:
685 : :
686 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_RUNNING)
687 : : return;
688 : :
689 : 0 : ret = get_mempolicy(&dev_node, NULL, 0, dev, MPOL_F_NODE | MPOL_F_ADDR);
690 [ # # ]: 0 : if (ret) {
691 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "unable to get numa information.");
692 : 0 : return;
693 : : }
694 : :
695 [ # # ]: 0 : if (dev_node == node)
696 : : return;
697 : :
698 : 0 : dev = rte_realloc_socket(*pdev, sizeof(**pdev), 0, node);
699 [ # # ]: 0 : if (!dev) {
700 : 0 : VHOST_CONFIG_LOG((*pdev)->ifname, ERR, "failed to realloc dev on node %d", node);
701 : 0 : return;
702 : : }
703 : 0 : *pdev = dev;
704 : :
705 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "reallocated device on node %d", node);
706 : 0 : vhost_devices[dev->vid] = dev;
707 : :
708 : 0 : mem_size = sizeof(struct rte_vhost_memory) +
709 : 0 : sizeof(struct rte_vhost_mem_region) * dev->mem->nregions;
710 : 0 : mem = rte_realloc_socket(dev->mem, mem_size, 0, node);
711 [ # # ]: 0 : if (!mem) {
712 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
713 : : "failed to realloc mem table on node %d",
714 : : node);
715 : 0 : return;
716 : : }
717 : 0 : dev->mem = mem;
718 : :
719 : 0 : gp = rte_realloc_socket(dev->guest_pages, dev->max_guest_pages * sizeof(*gp),
720 : : RTE_CACHE_LINE_SIZE, node);
721 [ # # ]: 0 : if (!gp) {
722 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
723 : : "failed to realloc guest pages on node %d",
724 : : node);
725 : 0 : return;
726 : : }
727 : 0 : dev->guest_pages = gp;
728 : :
729 : 0 : vhost_user_iotlb_init(dev);
730 : : }
731 : : #else
732 : : static void
733 : : numa_realloc(struct virtio_net **pdev, struct vhost_virtqueue **pvq)
734 : : {
735 : : RTE_SET_USED(pdev);
736 : : RTE_SET_USED(pvq);
737 : : }
738 : : #endif
739 : :
740 : : /* Converts QEMU virtual address to Vhost virtual address. */
741 : : static uint64_t
742 : 0 : qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
743 : : {
744 : : struct rte_vhost_mem_region *r;
745 : : uint32_t i;
746 : :
747 [ # # # # ]: 0 : if (unlikely(!dev || !dev->mem))
748 : 0 : goto out_error;
749 : :
750 : : /* Find the region where the address lives. */
751 [ # # ]: 0 : for (i = 0; i < dev->mem->nregions; i++) {
752 : : r = &dev->mem->regions[i];
753 : :
754 [ # # ]: 0 : if (qva >= r->guest_user_addr &&
755 [ # # ]: 0 : qva < r->guest_user_addr + r->size) {
756 : :
757 [ # # ]: 0 : if (unlikely(*len > r->guest_user_addr + r->size - qva))
758 : 0 : *len = r->guest_user_addr + r->size - qva;
759 : :
760 : 0 : return qva - r->guest_user_addr +
761 : 0 : r->host_user_addr;
762 : : }
763 : : }
764 : 0 : out_error:
765 : 0 : *len = 0;
766 : :
767 : 0 : return 0;
768 : : }
769 : :
770 : :
771 : : /*
772 : : * Converts ring address to Vhost virtual address.
773 : : * If IOMMU is enabled, the ring address is a guest IO virtual address,
774 : : * else it is a QEMU virtual address.
775 : : */
776 : : static uint64_t
777 : 0 : ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
778 : : uint64_t ra, uint64_t *size)
779 : : {
780 [ # # ]: 0 : if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
781 : : uint64_t vva;
782 : :
783 : : vhost_user_iotlb_rd_lock(vq);
784 : : vva = vhost_iova_to_vva(dev, vq, ra,
785 : : size, VHOST_ACCESS_RW);
786 : : vhost_user_iotlb_rd_unlock(vq);
787 : :
788 : 0 : return vva;
789 : : }
790 : :
791 : 0 : return qva_to_vva(dev, ra, size);
792 : : }
793 : :
794 : : static uint64_t
795 : 0 : log_addr_to_gpa(struct virtio_net *dev, struct vhost_virtqueue *vq)
796 : : {
797 : : uint64_t log_gpa;
798 : :
799 : : vhost_user_iotlb_rd_lock(vq);
800 : 0 : log_gpa = translate_log_addr(dev, vq, vq->ring_addrs.log_guest_addr);
801 : : vhost_user_iotlb_rd_unlock(vq);
802 : :
803 : 0 : return log_gpa;
804 : : }
805 : :
806 : : static uint64_t
807 : 0 : hua_to_alignment(struct rte_vhost_memory *mem, void *ptr)
808 : : {
809 : : struct rte_vhost_mem_region *r;
810 : : uint32_t i;
811 : 0 : uintptr_t hua = (uintptr_t)ptr;
812 : :
813 [ # # ]: 0 : for (i = 0; i < mem->nregions; i++) {
814 : : r = &mem->regions[i];
815 [ # # ]: 0 : if (hua >= r->host_user_addr &&
816 [ # # ]: 0 : hua < r->host_user_addr + r->size) {
817 : 0 : return get_blk_size(r->fd);
818 : : }
819 : : }
820 : :
821 : : /* If region isn't found, don't align at all */
822 : : return 1;
823 : : }
824 : :
825 : : void
826 : 0 : mem_set_dump(struct virtio_net *dev, void *ptr, size_t size, bool enable, uint64_t pagesz)
827 : : {
828 : : #ifdef MADV_DONTDUMP
829 : 0 : void *start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
830 : 0 : uintptr_t end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
831 : 0 : size_t len = end - (uintptr_t)start;
832 : :
833 [ # # # # ]: 0 : if (madvise(start, len, enable ? MADV_DODUMP : MADV_DONTDUMP) == -1) {
834 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
835 : : "could not set coredump preference (%s).", strerror(errno));
836 : : }
837 : : #endif
838 : 0 : }
839 : :
840 : : static void
841 : 0 : translate_ring_addresses(struct virtio_net **pdev, struct vhost_virtqueue **pvq)
842 : : {
843 : : struct vhost_virtqueue *vq;
844 : : struct virtio_net *dev;
845 : : uint64_t len, expected_len;
846 : :
847 : 0 : dev = *pdev;
848 : 0 : vq = *pvq;
849 : :
850 : 0 : vq_assert_lock(dev, vq);
851 : :
852 [ # # ]: 0 : if (vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG)) {
853 : 0 : vq->log_guest_addr =
854 : 0 : log_addr_to_gpa(dev, vq);
855 [ # # ]: 0 : if (vq->log_guest_addr == 0) {
856 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map log_guest_addr.");
857 : 0 : return;
858 : : }
859 : : }
860 : :
861 [ # # ]: 0 : if (vq_is_packed(dev)) {
862 : 0 : len = sizeof(struct vring_packed_desc) * vq->size;
863 : 0 : vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
864 : 0 : ring_addr_to_vva(dev, vq, vq->ring_addrs.desc_user_addr, &len);
865 [ # # ]: 0 : if (vq->desc_packed == NULL ||
866 : 0 : len != sizeof(struct vring_packed_desc) *
867 [ # # ]: 0 : vq->size) {
868 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map desc_packed ring.");
869 : 0 : return;
870 : : }
871 : :
872 : 0 : mem_set_dump(dev, vq->desc_packed, len, true,
873 : : hua_to_alignment(dev->mem, vq->desc_packed));
874 : 0 : numa_realloc(&dev, &vq);
875 : 0 : *pdev = dev;
876 : 0 : *pvq = vq;
877 : :
878 : 0 : len = sizeof(struct vring_packed_desc_event);
879 : 0 : vq->driver_event = (struct vring_packed_desc_event *)
880 : 0 : (uintptr_t)ring_addr_to_vva(dev,
881 : 0 : vq, vq->ring_addrs.avail_user_addr, &len);
882 [ # # ]: 0 : if (vq->driver_event == NULL ||
883 [ # # ]: 0 : len != sizeof(struct vring_packed_desc_event)) {
884 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG,
885 : : "failed to find driver area address.");
886 : 0 : return;
887 : : }
888 : :
889 : 0 : mem_set_dump(dev, vq->driver_event, len, true,
890 : : hua_to_alignment(dev->mem, vq->driver_event));
891 : 0 : len = sizeof(struct vring_packed_desc_event);
892 : 0 : vq->device_event = (struct vring_packed_desc_event *)
893 : 0 : (uintptr_t)ring_addr_to_vva(dev,
894 : 0 : vq, vq->ring_addrs.used_user_addr, &len);
895 [ # # ]: 0 : if (vq->device_event == NULL ||
896 [ # # ]: 0 : len != sizeof(struct vring_packed_desc_event)) {
897 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG,
898 : : "failed to find device area address.");
899 : 0 : return;
900 : : }
901 : :
902 : 0 : mem_set_dump(dev, vq->device_event, len, true,
903 : : hua_to_alignment(dev->mem, vq->device_event));
904 : 0 : vq->access_ok = true;
905 : 0 : return;
906 : : }
907 : :
908 : : /* The addresses are converted from QEMU virtual to Vhost virtual. */
909 [ # # # # : 0 : if (vq->desc && vq->avail && vq->used)
# # ]
910 : : return;
911 : :
912 : 0 : len = sizeof(struct vring_desc) * vq->size;
913 : 0 : vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
914 : 0 : vq, vq->ring_addrs.desc_user_addr, &len);
915 [ # # # # ]: 0 : if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) {
916 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map desc ring.");
917 : 0 : return;
918 : : }
919 : :
920 : 0 : mem_set_dump(dev, vq->desc, len, true, hua_to_alignment(dev->mem, vq->desc));
921 : 0 : numa_realloc(&dev, &vq);
922 : 0 : *pdev = dev;
923 : 0 : *pvq = vq;
924 : :
925 : 0 : len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
926 [ # # ]: 0 : if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
927 : 0 : len += sizeof(uint16_t);
928 : 0 : expected_len = len;
929 : 0 : vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
930 : 0 : vq, vq->ring_addrs.avail_user_addr, &len);
931 [ # # # # ]: 0 : if (vq->avail == 0 || len != expected_len) {
932 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map avail ring.");
933 : 0 : return;
934 : : }
935 : :
936 : 0 : mem_set_dump(dev, vq->avail, len, true, hua_to_alignment(dev->mem, vq->avail));
937 : 0 : len = sizeof(struct vring_used) +
938 : 0 : sizeof(struct vring_used_elem) * vq->size;
939 [ # # ]: 0 : if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
940 : 0 : len += sizeof(uint16_t);
941 : 0 : expected_len = len;
942 : 0 : vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
943 : 0 : vq, vq->ring_addrs.used_user_addr, &len);
944 [ # # # # ]: 0 : if (vq->used == 0 || len != expected_len) {
945 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map used ring.");
946 : 0 : return;
947 : : }
948 : :
949 : 0 : mem_set_dump(dev, vq->used, len, true, hua_to_alignment(dev->mem, vq->used));
950 : :
951 [ # # ]: 0 : if (vq->last_used_idx != vq->used->idx) {
952 : 0 : VHOST_CONFIG_LOG(dev->ifname, WARNING,
953 : : "last_used_idx (%u) and vq->used->idx (%u) mismatches;",
954 : : vq->last_used_idx, vq->used->idx);
955 : 0 : vq->last_used_idx = vq->used->idx;
956 : 0 : vq->last_avail_idx = vq->used->idx;
957 : 0 : VHOST_CONFIG_LOG(dev->ifname, WARNING,
958 : : "some packets maybe resent for Tx and dropped for Rx");
959 : : }
960 : :
961 : 0 : vq->access_ok = true;
962 : :
963 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "mapped address desc: %p", vq->desc);
964 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "mapped address avail: %p", vq->avail);
965 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "mapped address used: %p", vq->used);
966 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "log_guest_addr: %" PRIx64, vq->log_guest_addr);
967 : : }
968 : :
969 : : /*
970 : : * The virtio device sends us the desc, used and avail ring addresses.
971 : : * This function then converts these to our address space.
972 : : */
973 : : static int
974 : 0 : vhost_user_set_vring_addr(struct virtio_net **pdev,
975 : : struct vhu_msg_context *ctx,
976 : : int main_fd __rte_unused)
977 : : {
978 : 0 : struct virtio_net *dev = *pdev;
979 : : struct vhost_virtqueue *vq;
980 : 0 : struct vhost_vring_addr *addr = &ctx->msg.payload.addr;
981 : : bool access_ok;
982 : :
983 [ # # ]: 0 : if (dev->mem == NULL)
984 : : return RTE_VHOST_MSG_RESULT_ERR;
985 : :
986 : : /* addr->index refers to the queue index. The txq 1, rxq is 0. */
987 : 0 : vq = dev->virtqueue[ctx->msg.payload.addr.index];
988 : :
989 : : /*
990 : : * Rings addresses should not be interpreted as long as the ring is not
991 : : * started and enabled
992 : : */
993 [ # # ]: 0 : memcpy(&vq->ring_addrs, addr, sizeof(*addr));
994 : :
995 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)
996 : 0 : goto out;
997 : :
998 : : /* vhost_user_lock_all_queue_pairs locked all qps */
999 : 0 : VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_VRING_ADDR);
1000 : :
1001 : 0 : access_ok = vq->access_ok;
1002 : :
1003 : 0 : vring_invalidate(dev, vq);
1004 : :
1005 [ # # # # ]: 0 : if ((vq->enabled && (dev->features &
1006 [ # # ]: 0 : (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) ||
1007 : : access_ok) {
1008 : 0 : translate_ring_addresses(&dev, &vq);
1009 : 0 : *pdev = dev;
1010 : : }
1011 : :
1012 : 0 : out:
1013 : : return RTE_VHOST_MSG_RESULT_OK;
1014 : : }
1015 : :
1016 : : /*
1017 : : * The virtio device sends us the available ring last used index.
1018 : : */
1019 : : static int
1020 : 0 : vhost_user_set_vring_base(struct virtio_net **pdev,
1021 : : struct vhu_msg_context *ctx,
1022 : : int main_fd __rte_unused)
1023 : : {
1024 : 0 : struct virtio_net *dev = *pdev;
1025 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
1026 [ # # ]: 0 : uint64_t val = ctx->msg.payload.state.num;
1027 : :
1028 [ # # ]: 0 : if (vq_is_packed(dev)) {
1029 : : /*
1030 : : * Bit[0:14]: avail index
1031 : : * Bit[15]: avail wrap counter
1032 : : */
1033 : 0 : vq->last_avail_idx = val & 0x7fff;
1034 : 0 : vq->avail_wrap_counter = !!(val & (0x1 << 15));
1035 : : /*
1036 : : * Set used index to same value as available one, as
1037 : : * their values should be the same since ring processing
1038 : : * was stopped at get time.
1039 : : */
1040 : 0 : vq->last_used_idx = vq->last_avail_idx;
1041 : 0 : vq->used_wrap_counter = vq->avail_wrap_counter;
1042 : : } else {
1043 : 0 : vq->last_used_idx = ctx->msg.payload.state.num;
1044 : 0 : vq->last_avail_idx = ctx->msg.payload.state.num;
1045 : : }
1046 : :
1047 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1048 : : "vring base idx:%u last_used_idx:%u last_avail_idx:%u.",
1049 : : ctx->msg.payload.state.index, vq->last_used_idx, vq->last_avail_idx);
1050 : :
1051 : 0 : return RTE_VHOST_MSG_RESULT_OK;
1052 : : }
1053 : :
1054 : : static int
1055 : 0 : add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
1056 : : uint64_t host_iova, uint64_t host_user_addr, uint64_t size)
1057 : : {
1058 : : struct guest_page *page, *last_page;
1059 : : struct guest_page *old_pages;
1060 : :
1061 [ # # ]: 0 : if (dev->nr_guest_pages == dev->max_guest_pages) {
1062 : 0 : dev->max_guest_pages *= 2;
1063 : 0 : old_pages = dev->guest_pages;
1064 : 0 : dev->guest_pages = rte_realloc(dev->guest_pages,
1065 : 0 : dev->max_guest_pages * sizeof(*page),
1066 : : RTE_CACHE_LINE_SIZE);
1067 [ # # ]: 0 : if (dev->guest_pages == NULL) {
1068 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "cannot realloc guest_pages");
1069 : 0 : rte_free(old_pages);
1070 : 0 : return -1;
1071 : : }
1072 : : }
1073 : :
1074 [ # # ]: 0 : if (dev->nr_guest_pages > 0) {
1075 : 0 : last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
1076 : : /* merge if the two pages are continuous */
1077 [ # # ]: 0 : if (host_iova == last_page->host_iova + last_page->size &&
1078 [ # # ]: 0 : guest_phys_addr == last_page->guest_phys_addr + last_page->size &&
1079 [ # # ]: 0 : host_user_addr == last_page->host_user_addr + last_page->size) {
1080 : 0 : last_page->size += size;
1081 : 0 : return 0;
1082 : : }
1083 : : }
1084 : :
1085 : 0 : page = &dev->guest_pages[dev->nr_guest_pages++];
1086 : 0 : page->guest_phys_addr = guest_phys_addr;
1087 : 0 : page->host_iova = host_iova;
1088 : 0 : page->host_user_addr = host_user_addr;
1089 : 0 : page->size = size;
1090 : :
1091 : 0 : return 0;
1092 : : }
1093 : :
1094 : : static int
1095 : 0 : add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
1096 : : uint64_t page_size)
1097 : : {
1098 : 0 : uint64_t reg_size = reg->size;
1099 : 0 : uint64_t host_user_addr = reg->host_user_addr;
1100 : 0 : uint64_t guest_phys_addr = reg->guest_phys_addr;
1101 : : uint64_t host_iova;
1102 : : uint64_t size;
1103 : :
1104 : 0 : host_iova = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
1105 : 0 : size = page_size - (guest_phys_addr & (page_size - 1));
1106 : 0 : size = RTE_MIN(size, reg_size);
1107 : :
1108 [ # # ]: 0 : if (add_one_guest_page(dev, guest_phys_addr, host_iova,
1109 : : host_user_addr, size) < 0)
1110 : : return -1;
1111 : :
1112 : 0 : host_user_addr += size;
1113 : 0 : guest_phys_addr += size;
1114 : 0 : reg_size -= size;
1115 : :
1116 [ # # ]: 0 : while (reg_size > 0) {
1117 : 0 : size = RTE_MIN(reg_size, page_size);
1118 : 0 : host_iova = rte_mem_virt2iova((void *)(uintptr_t)
1119 : : host_user_addr);
1120 [ # # ]: 0 : if (add_one_guest_page(dev, guest_phys_addr, host_iova,
1121 : : host_user_addr, size) < 0)
1122 : : return -1;
1123 : :
1124 : 0 : host_user_addr += size;
1125 : 0 : guest_phys_addr += size;
1126 : 0 : reg_size -= size;
1127 : : }
1128 : :
1129 : : /* sort guest page array if over binary search threshold */
1130 [ # # ]: 0 : if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
1131 : 0 : qsort((void *)dev->guest_pages, dev->nr_guest_pages,
1132 : : sizeof(struct guest_page), guest_page_addrcmp);
1133 : : }
1134 : :
1135 : : return 0;
1136 : : }
1137 : :
1138 : : #ifdef RTE_LIBRTE_VHOST_DEBUG
1139 : : /* TODO: enable it only in debug mode? */
1140 : : static void
1141 : : dump_guest_pages(struct virtio_net *dev)
1142 : : {
1143 : : uint32_t i;
1144 : : struct guest_page *page;
1145 : :
1146 : : for (i = 0; i < dev->nr_guest_pages; i++) {
1147 : : page = &dev->guest_pages[i];
1148 : :
1149 : : VHOST_CONFIG_LOG(dev->ifname, INFO, "guest physical page region %u", i);
1150 : : VHOST_CONFIG_LOG(dev->ifname, INFO, "\tguest_phys_addr: %" PRIx64,
1151 : : page->guest_phys_addr);
1152 : : VHOST_CONFIG_LOG(dev->ifname, INFO, "\thost_iova : %" PRIx64,
1153 : : page->host_iova);
1154 : : VHOST_CONFIG_LOG(dev->ifname, INFO, "\tsize : %" PRIx64,
1155 : : page->size);
1156 : : }
1157 : : }
1158 : : #else
1159 : : #define dump_guest_pages(dev)
1160 : : #endif
1161 : :
1162 : : static bool
1163 : 0 : vhost_memory_changed(struct VhostUserMemory *new,
1164 : : struct rte_vhost_memory *old)
1165 : : {
1166 : : uint32_t i;
1167 : :
1168 [ # # ]: 0 : if (new->nregions != old->nregions)
1169 : : return true;
1170 : :
1171 [ # # ]: 0 : for (i = 0; i < new->nregions; ++i) {
1172 : : VhostUserMemoryRegion *new_r = &new->regions[i];
1173 : : struct rte_vhost_mem_region *old_r = &old->regions[i];
1174 : :
1175 [ # # ]: 0 : if (new_r->guest_phys_addr != old_r->guest_phys_addr)
1176 : : return true;
1177 [ # # ]: 0 : if (new_r->memory_size != old_r->size)
1178 : : return true;
1179 [ # # ]: 0 : if (new_r->userspace_addr != old_r->guest_user_addr)
1180 : : return true;
1181 : : }
1182 : :
1183 : : return false;
1184 : : }
1185 : :
1186 : : #ifdef RTE_LIBRTE_VHOST_POSTCOPY
1187 : : static int
1188 : 0 : vhost_user_postcopy_region_register(struct virtio_net *dev,
1189 : : struct rte_vhost_mem_region *reg)
1190 : : {
1191 : : struct uffdio_register reg_struct;
1192 : :
1193 : : /*
1194 : : * Let's register all the mmapped area to ensure
1195 : : * alignment on page boundary.
1196 : : */
1197 : 0 : reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr;
1198 : 0 : reg_struct.range.len = reg->mmap_size;
1199 : 0 : reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
1200 : :
1201 [ # # ]: 0 : if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
1202 : : ®_struct)) {
1203 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1204 : : "failed to register ufd for region "
1205 : : "%" PRIx64 " - %" PRIx64 " (ufd = %d) %s",
1206 : : (uint64_t)reg_struct.range.start,
1207 : : (uint64_t)reg_struct.range.start +
1208 : : (uint64_t)reg_struct.range.len - 1,
1209 : : dev->postcopy_ufd,
1210 : : strerror(errno));
1211 : 0 : return -1;
1212 : : }
1213 : :
1214 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1215 : : "\t userfaultfd registered for range : %" PRIx64 " - %" PRIx64,
1216 : : (uint64_t)reg_struct.range.start,
1217 : : (uint64_t)reg_struct.range.start +
1218 : : (uint64_t)reg_struct.range.len - 1);
1219 : :
1220 : 0 : return 0;
1221 : : }
1222 : : #else
1223 : : static int
1224 : : vhost_user_postcopy_region_register(struct virtio_net *dev __rte_unused,
1225 : : struct rte_vhost_mem_region *reg __rte_unused)
1226 : : {
1227 : : return -1;
1228 : : }
1229 : : #endif
1230 : :
1231 : : static int
1232 : 0 : vhost_user_postcopy_register(struct virtio_net *dev, int main_fd,
1233 : : struct vhu_msg_context *ctx)
1234 : : {
1235 : : struct VhostUserMemory *memory;
1236 : : struct rte_vhost_mem_region *reg;
1237 : : struct vhu_msg_context ack_ctx;
1238 : : uint32_t i;
1239 : :
1240 [ # # ]: 0 : if (!dev->postcopy_listening)
1241 : : return 0;
1242 : :
1243 : : /*
1244 : : * We haven't a better way right now than sharing
1245 : : * DPDK's virtual address with Qemu, so that Qemu can
1246 : : * retrieve the region offset when handling userfaults.
1247 : : */
1248 : : memory = &ctx->msg.payload.memory;
1249 [ # # ]: 0 : for (i = 0; i < memory->nregions; i++) {
1250 : 0 : reg = &dev->mem->regions[i];
1251 : 0 : memory->regions[i].userspace_addr = reg->host_user_addr;
1252 : : }
1253 : :
1254 : : /* Send the addresses back to qemu */
1255 : 0 : ctx->fd_num = 0;
1256 : 0 : send_vhost_reply(dev, main_fd, ctx);
1257 : :
1258 : : /* Wait for qemu to acknowledge it got the addresses
1259 : : * we've got to wait before we're allowed to generate faults.
1260 : : */
1261 [ # # ]: 0 : if (read_vhost_message(dev, main_fd, &ack_ctx) <= 0) {
1262 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1263 : : "failed to read qemu ack on postcopy set-mem-table");
1264 : 0 : return -1;
1265 : : }
1266 : :
1267 [ # # ]: 0 : if (validate_msg_fds(dev, &ack_ctx, 0) != 0)
1268 : : return -1;
1269 : :
1270 [ # # ]: 0 : if (ack_ctx.msg.request.frontend != VHOST_USER_SET_MEM_TABLE) {
1271 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1272 : : "bad qemu ack on postcopy set-mem-table (%d)",
1273 : : ack_ctx.msg.request.frontend);
1274 : 0 : return -1;
1275 : : }
1276 : :
1277 : : /* Now userfault register and we can use the memory */
1278 [ # # ]: 0 : for (i = 0; i < memory->nregions; i++) {
1279 : 0 : reg = &dev->mem->regions[i];
1280 [ # # ]: 0 : if (vhost_user_postcopy_region_register(dev, reg) < 0)
1281 : : return -1;
1282 : : }
1283 : :
1284 : : return 0;
1285 : : }
1286 : :
1287 : : static int
1288 : 0 : vhost_user_mmap_region(struct virtio_net *dev,
1289 : : struct rte_vhost_mem_region *region,
1290 : : uint64_t mmap_offset)
1291 : : {
1292 : : void *mmap_addr;
1293 : : uint64_t mmap_size;
1294 : : uint64_t alignment;
1295 : : int populate;
1296 : :
1297 : : /* Check for memory_size + mmap_offset overflow */
1298 [ # # ]: 0 : if (mmap_offset >= -region->size) {
1299 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1300 : : "mmap_offset (%#"PRIx64") and memory_size (%#"PRIx64") overflow",
1301 : : mmap_offset, region->size);
1302 : 0 : return -1;
1303 : : }
1304 : :
1305 : 0 : mmap_size = region->size + mmap_offset;
1306 : :
1307 : : /* mmap() without flag of MAP_ANONYMOUS, should be called with length
1308 : : * argument aligned with hugepagesz at older longterm version Linux,
1309 : : * like 2.6.32 and 3.2.72, or mmap() will fail with EINVAL.
1310 : : *
1311 : : * To avoid failure, make sure in caller to keep length aligned.
1312 : : */
1313 : 0 : alignment = get_blk_size(region->fd);
1314 [ # # ]: 0 : if (alignment == (uint64_t)-1) {
1315 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "couldn't get hugepage size through fstat");
1316 : 0 : return -1;
1317 : : }
1318 : 0 : mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
1319 [ # # ]: 0 : if (mmap_size == 0) {
1320 : : /*
1321 : : * It could happen if initial mmap_size + alignment overflows
1322 : : * the sizeof uint64, which could happen if either mmap_size or
1323 : : * alignment value is wrong.
1324 : : *
1325 : : * mmap() kernel implementation would return an error, but
1326 : : * better catch it before and provide useful info in the logs.
1327 : : */
1328 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1329 : : "mmap size (0x%" PRIx64 ") or alignment (0x%" PRIx64 ") is invalid",
1330 : : region->size + mmap_offset, alignment);
1331 : 0 : return -1;
1332 : : }
1333 : :
1334 [ # # ]: 0 : populate = dev->async_copy ? MAP_POPULATE : 0;
1335 : 0 : mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
1336 : : MAP_SHARED | populate, region->fd, 0);
1337 : :
1338 [ # # ]: 0 : if (mmap_addr == MAP_FAILED) {
1339 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "mmap failed (%s).", strerror(errno));
1340 : 0 : return -1;
1341 : : }
1342 : :
1343 : 0 : region->mmap_addr = mmap_addr;
1344 : 0 : region->mmap_size = mmap_size;
1345 : 0 : region->host_user_addr = (uint64_t)(uintptr_t)mmap_addr + mmap_offset;
1346 : 0 : mem_set_dump(dev, mmap_addr, mmap_size, false, alignment);
1347 : :
1348 [ # # ]: 0 : if (dev->async_copy) {
1349 [ # # ]: 0 : if (add_guest_pages(dev, region, alignment) < 0) {
1350 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1351 : : "adding guest pages to region failed.");
1352 : 0 : return -1;
1353 : : }
1354 : : }
1355 : :
1356 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1357 : : "guest memory region size: 0x%" PRIx64,
1358 : : region->size);
1359 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1360 : : "\t guest physical addr: 0x%" PRIx64,
1361 : : region->guest_phys_addr);
1362 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1363 : : "\t guest virtual addr: 0x%" PRIx64,
1364 : : region->guest_user_addr);
1365 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1366 : : "\t host virtual addr: 0x%" PRIx64,
1367 : : region->host_user_addr);
1368 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1369 : : "\t mmap addr : 0x%" PRIx64,
1370 : : (uint64_t)(uintptr_t)mmap_addr);
1371 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1372 : : "\t mmap size : 0x%" PRIx64,
1373 : : mmap_size);
1374 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1375 : : "\t mmap align: 0x%" PRIx64,
1376 : : alignment);
1377 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1378 : : "\t mmap off : 0x%" PRIx64,
1379 : : mmap_offset);
1380 : :
1381 : 0 : return 0;
1382 : : }
1383 : :
1384 : : static int
1385 : 0 : vhost_user_set_mem_table(struct virtio_net **pdev,
1386 : : struct vhu_msg_context *ctx,
1387 : : int main_fd)
1388 : : {
1389 : 0 : struct virtio_net *dev = *pdev;
1390 : 0 : struct VhostUserMemory *memory = &ctx->msg.payload.memory;
1391 : : struct rte_vhost_mem_region *reg;
1392 : : int numa_node = SOCKET_ID_ANY;
1393 : : uint64_t mmap_offset;
1394 : : uint32_t i;
1395 : : bool async_notify = false;
1396 : :
1397 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, memory->nregions) != 0)
1398 : : return RTE_VHOST_MSG_RESULT_ERR;
1399 : :
1400 [ # # ]: 0 : if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) {
1401 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1402 : : "too many memory regions (%u)",
1403 : : memory->nregions);
1404 : 0 : goto close_msg_fds;
1405 : : }
1406 : :
1407 [ # # # # ]: 0 : if (dev->mem && !vhost_memory_changed(memory, dev->mem)) {
1408 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "memory regions not changed");
1409 : :
1410 : 0 : close_msg_fds(ctx);
1411 : :
1412 : 0 : return RTE_VHOST_MSG_RESULT_OK;
1413 : : }
1414 : :
1415 [ # # ]: 0 : if (dev->mem) {
1416 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) {
1417 : 0 : struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
1418 : :
1419 [ # # # # ]: 0 : if (vdpa_dev && vdpa_dev->ops->dev_close)
1420 : 0 : vdpa_dev->ops->dev_close(dev->vid);
1421 : 0 : dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
1422 : : }
1423 : :
1424 : : /* notify the vhost application to stop DMA transfers */
1425 [ # # # # ]: 0 : if (dev->async_copy && dev->notify_ops->vring_state_changed) {
1426 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
1427 : 0 : dev->notify_ops->vring_state_changed(dev->vid,
1428 : : i, 0);
1429 : : }
1430 : : async_notify = true;
1431 : : }
1432 : :
1433 : : /* Flush IOTLB cache as previous HVAs are now invalid */
1434 [ # # ]: 0 : if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1435 : 0 : vhost_user_iotlb_flush_all(dev);
1436 : :
1437 : 0 : free_mem_region(dev);
1438 : 0 : rte_free(dev->mem);
1439 : 0 : dev->mem = NULL;
1440 : : }
1441 : :
1442 : : /*
1443 : : * If VQ 0 has already been allocated, try to allocate on the same
1444 : : * NUMA node. It can be reallocated later in numa_realloc().
1445 : : */
1446 [ # # ]: 0 : if (dev->nr_vring > 0)
1447 : 0 : numa_node = dev->virtqueue[0]->numa_node;
1448 : :
1449 : 0 : dev->nr_guest_pages = 0;
1450 [ # # ]: 0 : if (dev->guest_pages == NULL) {
1451 : 0 : dev->max_guest_pages = 8;
1452 : 0 : dev->guest_pages = rte_zmalloc_socket(NULL,
1453 : : dev->max_guest_pages *
1454 : : sizeof(struct guest_page),
1455 : : RTE_CACHE_LINE_SIZE,
1456 : : numa_node);
1457 [ # # ]: 0 : if (dev->guest_pages == NULL) {
1458 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1459 : : "failed to allocate memory for dev->guest_pages");
1460 : 0 : goto close_msg_fds;
1461 : : }
1462 : : }
1463 : :
1464 : 0 : dev->mem = rte_zmalloc_socket("vhost-mem-table", sizeof(struct rte_vhost_memory) +
1465 : 0 : sizeof(struct rte_vhost_mem_region) * memory->nregions, 0, numa_node);
1466 [ # # ]: 0 : if (dev->mem == NULL) {
1467 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to allocate memory for dev->mem");
1468 : 0 : goto free_guest_pages;
1469 : : }
1470 : :
1471 [ # # ]: 0 : for (i = 0; i < memory->nregions; i++) {
1472 : 0 : reg = &dev->mem->regions[i];
1473 : :
1474 : 0 : reg->guest_phys_addr = memory->regions[i].guest_phys_addr;
1475 : 0 : reg->guest_user_addr = memory->regions[i].userspace_addr;
1476 : 0 : reg->size = memory->regions[i].memory_size;
1477 : 0 : reg->fd = ctx->fds[i];
1478 : :
1479 : : /*
1480 : : * Assign invalid file descriptor value to avoid double
1481 : : * closing on error path.
1482 : : */
1483 : 0 : ctx->fds[i] = -1;
1484 : :
1485 : 0 : mmap_offset = memory->regions[i].mmap_offset;
1486 : :
1487 [ # # ]: 0 : if (vhost_user_mmap_region(dev, reg, mmap_offset) < 0) {
1488 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap region %u", i);
1489 : 0 : goto free_mem_table;
1490 : : }
1491 : :
1492 : 0 : dev->mem->nregions++;
1493 : : }
1494 : :
1495 [ # # # # ]: 0 : if (dev->async_copy && rte_vfio_is_enabled("vfio"))
1496 : 0 : async_dma_map(dev, true);
1497 : :
1498 [ # # ]: 0 : if (vhost_user_postcopy_register(dev, main_fd, ctx) < 0)
1499 : 0 : goto free_mem_table;
1500 : :
1501 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
1502 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
1503 : :
1504 [ # # ]: 0 : if (!vq)
1505 : 0 : continue;
1506 : :
1507 [ # # # # : 0 : if (vq->desc || vq->avail || vq->used) {
# # ]
1508 : : /* vhost_user_lock_all_queue_pairs locked all qps */
1509 : 0 : VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_MEM_TABLE);
1510 : :
1511 : : /*
1512 : : * If the memory table got updated, the ring addresses
1513 : : * need to be translated again as virtual addresses have
1514 : : * changed.
1515 : : */
1516 : 0 : vring_invalidate(dev, vq);
1517 : :
1518 : 0 : translate_ring_addresses(&dev, &vq);
1519 : 0 : *pdev = dev;
1520 : : }
1521 : : }
1522 : :
1523 : : dump_guest_pages(dev);
1524 : :
1525 [ # # ]: 0 : if (async_notify) {
1526 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++)
1527 : 0 : dev->notify_ops->vring_state_changed(dev->vid, i, 1);
1528 : : }
1529 : :
1530 : : return RTE_VHOST_MSG_RESULT_OK;
1531 : :
1532 : 0 : free_mem_table:
1533 : 0 : free_mem_region(dev);
1534 : 0 : rte_free(dev->mem);
1535 : 0 : dev->mem = NULL;
1536 : :
1537 : 0 : free_guest_pages:
1538 : 0 : rte_free(dev->guest_pages);
1539 : 0 : dev->guest_pages = NULL;
1540 : 0 : close_msg_fds:
1541 : 0 : close_msg_fds(ctx);
1542 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
1543 : : }
1544 : :
1545 : : static bool
1546 : 0 : vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq)
1547 : : {
1548 : : bool rings_ok;
1549 : :
1550 [ # # ]: 0 : if (!vq)
1551 : : return false;
1552 : :
1553 [ # # ]: 0 : if (vq_is_packed(dev))
1554 [ # # # # ]: 0 : rings_ok = vq->desc_packed && vq->driver_event &&
1555 [ # # ]: 0 : vq->device_event;
1556 : : else
1557 [ # # # # : 0 : rings_ok = vq->desc && vq->avail && vq->used;
# # ]
1558 : :
1559 : 0 : return rings_ok &&
1560 [ # # ]: 0 : vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1561 [ # # # # ]: 0 : vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1562 [ # # ]: 0 : vq->enabled;
1563 : : }
1564 : :
1565 : : #define VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY 2u
1566 : : #define VIRTIO_BLK_NUM_VQS_TO_BE_READY 1u
1567 : :
1568 : : static int
1569 : 0 : virtio_is_ready(struct virtio_net *dev)
1570 : : {
1571 : : struct rte_vdpa_device *vdpa_dev;
1572 : : struct vhost_virtqueue *vq;
1573 : : uint32_t vdpa_type;
1574 : 0 : uint32_t i, nr_vring = dev->nr_vring;
1575 : :
1576 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_READY)
1577 : : return 1;
1578 : :
1579 [ # # ]: 0 : if (!dev->nr_vring)
1580 : : return 0;
1581 : :
1582 : 0 : vdpa_dev = dev->vdpa_dev;
1583 [ # # ]: 0 : if (vdpa_dev)
1584 : 0 : vdpa_type = vdpa_dev->type;
1585 : : else
1586 : : vdpa_type = -1;
1587 : :
1588 [ # # ]: 0 : if (vdpa_type == RTE_VHOST_VDPA_DEVICE_TYPE_BLK) {
1589 : : nr_vring = VIRTIO_BLK_NUM_VQS_TO_BE_READY;
1590 : : } else {
1591 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET)
1592 : : nr_vring = VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY;
1593 : : }
1594 : :
1595 [ # # ]: 0 : if (dev->nr_vring < nr_vring)
1596 : : return 0;
1597 : :
1598 [ # # ]: 0 : for (i = 0; i < nr_vring; i++) {
1599 : 0 : vq = dev->virtqueue[i];
1600 : :
1601 [ # # ]: 0 : if (!vq_is_ready(dev, vq))
1602 : : return 0;
1603 : : }
1604 : :
1605 : : /* If supported, ensure the frontend is really done with config */
1606 [ # # ]: 0 : if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS))
1607 [ # # ]: 0 : if (!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK))
1608 : : return 0;
1609 : :
1610 : 0 : dev->flags |= VIRTIO_DEV_READY;
1611 : :
1612 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_RUNNING))
1613 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "virtio is now ready for processing.");
1614 : : return 1;
1615 : : }
1616 : :
1617 : : static void *
1618 : 0 : inflight_mem_alloc(struct virtio_net *dev, const char *name, size_t size, int *fd)
1619 : : {
1620 : : void *ptr;
1621 : : int mfd = -1;
1622 : : uint64_t alignment;
1623 : 0 : char fname[20] = "/tmp/memfd-XXXXXX";
1624 : :
1625 : 0 : *fd = -1;
1626 : : #ifdef MEMFD_SUPPORTED
1627 : 0 : mfd = memfd_create(name, MFD_CLOEXEC);
1628 : : #else
1629 : : RTE_SET_USED(name);
1630 : : #endif
1631 [ # # ]: 0 : if (mfd == -1) {
1632 : 0 : mfd = mkstemp(fname);
1633 [ # # ]: 0 : if (mfd == -1) {
1634 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to get inflight buffer fd");
1635 : 0 : return NULL;
1636 : : }
1637 : :
1638 : 0 : unlink(fname);
1639 : : }
1640 : :
1641 [ # # ]: 0 : if (ftruncate(mfd, size) == -1) {
1642 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc inflight buffer");
1643 : 0 : close(mfd);
1644 : 0 : return NULL;
1645 : : }
1646 : :
1647 : 0 : ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
1648 [ # # ]: 0 : if (ptr == MAP_FAILED) {
1649 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap inflight buffer");
1650 : 0 : close(mfd);
1651 : 0 : return NULL;
1652 : : }
1653 : :
1654 : : alignment = get_blk_size(mfd);
1655 : 0 : mem_set_dump(dev, ptr, size, false, alignment);
1656 : 0 : *fd = mfd;
1657 : 0 : return ptr;
1658 : : }
1659 : :
1660 : : static uint32_t
1661 : : get_pervq_shm_size_split(uint16_t queue_size)
1662 : : {
1663 : 0 : return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_split) *
1664 : : queue_size + sizeof(uint64_t) +
1665 : : sizeof(uint16_t) * 4, INFLIGHT_ALIGNMENT);
1666 : : }
1667 : :
1668 : : static uint32_t
1669 : : get_pervq_shm_size_packed(uint16_t queue_size)
1670 : : {
1671 : 0 : return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_packed)
1672 : : * queue_size + sizeof(uint64_t) +
1673 : : sizeof(uint16_t) * 6 + sizeof(uint8_t) * 9,
1674 : : INFLIGHT_ALIGNMENT);
1675 : : }
1676 : :
1677 : : static int
1678 : 0 : vhost_user_get_inflight_fd(struct virtio_net **pdev,
1679 : : struct vhu_msg_context *ctx,
1680 : : int main_fd __rte_unused)
1681 : : {
1682 : : struct rte_vhost_inflight_info_packed *inflight_packed;
1683 : : uint64_t pervq_inflight_size, mmap_size;
1684 : : uint16_t num_queues, queue_size;
1685 : 0 : struct virtio_net *dev = *pdev;
1686 : : int fd, i, j;
1687 : : int numa_node = SOCKET_ID_ANY;
1688 : : void *addr;
1689 : :
1690 [ # # ]: 0 : if (ctx->msg.size != sizeof(ctx->msg.payload.inflight)) {
1691 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1692 : : "invalid get_inflight_fd message size is %d",
1693 : : ctx->msg.size);
1694 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
1695 : : }
1696 : :
1697 : : /*
1698 : : * If VQ 0 has already been allocated, try to allocate on the same
1699 : : * NUMA node. It can be reallocated later in numa_realloc().
1700 : : */
1701 [ # # ]: 0 : if (dev->nr_vring > 0)
1702 : 0 : numa_node = dev->virtqueue[0]->numa_node;
1703 : :
1704 [ # # ]: 0 : if (dev->inflight_info == NULL) {
1705 : 0 : dev->inflight_info = rte_zmalloc_socket("inflight_info",
1706 : : sizeof(struct inflight_mem_info), 0, numa_node);
1707 [ # # ]: 0 : if (!dev->inflight_info) {
1708 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc dev inflight area");
1709 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
1710 : : }
1711 : 0 : dev->inflight_info->fd = -1;
1712 : : }
1713 : :
1714 : 0 : num_queues = ctx->msg.payload.inflight.num_queues;
1715 : 0 : queue_size = ctx->msg.payload.inflight.queue_size;
1716 : :
1717 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1718 : : "get_inflight_fd num_queues: %u",
1719 : : ctx->msg.payload.inflight.num_queues);
1720 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1721 : : "get_inflight_fd queue_size: %u",
1722 : : ctx->msg.payload.inflight.queue_size);
1723 : :
1724 [ # # ]: 0 : if (vq_is_packed(dev))
1725 : 0 : pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1726 : : else
1727 : 0 : pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1728 : :
1729 : 0 : mmap_size = num_queues * pervq_inflight_size;
1730 : 0 : addr = inflight_mem_alloc(dev, "vhost-inflight", mmap_size, &fd);
1731 [ # # ]: 0 : if (!addr) {
1732 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc vhost inflight area");
1733 : 0 : ctx->msg.payload.inflight.mmap_size = 0;
1734 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
1735 : : }
1736 : : memset(addr, 0, mmap_size);
1737 : :
1738 [ # # ]: 0 : if (dev->inflight_info->addr) {
1739 : 0 : munmap(dev->inflight_info->addr, dev->inflight_info->size);
1740 : 0 : dev->inflight_info->addr = NULL;
1741 : : }
1742 : :
1743 [ # # ]: 0 : if (dev->inflight_info->fd >= 0) {
1744 : 0 : close(dev->inflight_info->fd);
1745 : 0 : dev->inflight_info->fd = -1;
1746 : : }
1747 : :
1748 : 0 : dev->inflight_info->addr = addr;
1749 : 0 : dev->inflight_info->size = ctx->msg.payload.inflight.mmap_size = mmap_size;
1750 : 0 : dev->inflight_info->fd = ctx->fds[0] = fd;
1751 : 0 : ctx->msg.payload.inflight.mmap_offset = 0;
1752 [ # # ]: 0 : ctx->fd_num = 1;
1753 : :
1754 [ # # ]: 0 : if (vq_is_packed(dev)) {
1755 [ # # ]: 0 : for (i = 0; i < num_queues; i++) {
1756 : : inflight_packed =
1757 : : (struct rte_vhost_inflight_info_packed *)addr;
1758 : 0 : inflight_packed->used_wrap_counter = 1;
1759 : 0 : inflight_packed->old_used_wrap_counter = 1;
1760 [ # # ]: 0 : for (j = 0; j < queue_size; j++)
1761 : 0 : inflight_packed->desc[j].next = j + 1;
1762 : 0 : addr = (void *)((char *)addr + pervq_inflight_size);
1763 : : }
1764 : : }
1765 : :
1766 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1767 : : "send inflight mmap_size: %"PRIu64,
1768 : : ctx->msg.payload.inflight.mmap_size);
1769 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1770 : : "send inflight mmap_offset: %"PRIu64,
1771 : : ctx->msg.payload.inflight.mmap_offset);
1772 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1773 : : "send inflight fd: %d", ctx->fds[0]);
1774 : :
1775 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
1776 : : }
1777 : :
1778 : : static int
1779 : 0 : vhost_user_set_inflight_fd(struct virtio_net **pdev,
1780 : : struct vhu_msg_context *ctx,
1781 : : int main_fd __rte_unused)
1782 : : {
1783 : : uint64_t mmap_size, mmap_offset;
1784 : : uint16_t num_queues, queue_size;
1785 : 0 : struct virtio_net *dev = *pdev;
1786 : : uint32_t pervq_inflight_size;
1787 : : struct vhost_virtqueue *vq;
1788 : : void *addr;
1789 : : int fd, i;
1790 : : int numa_node = SOCKET_ID_ANY;
1791 : :
1792 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, 1) != 0)
1793 : : return RTE_VHOST_MSG_RESULT_ERR;
1794 : :
1795 : 0 : fd = ctx->fds[0];
1796 [ # # # # ]: 0 : if (ctx->msg.size != sizeof(ctx->msg.payload.inflight) || fd < 0) {
1797 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1798 : : "invalid set_inflight_fd message size is %d,fd is %d",
1799 : : ctx->msg.size, fd);
1800 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
1801 : : }
1802 : :
1803 : 0 : mmap_size = ctx->msg.payload.inflight.mmap_size;
1804 : 0 : mmap_offset = ctx->msg.payload.inflight.mmap_offset;
1805 : 0 : num_queues = ctx->msg.payload.inflight.num_queues;
1806 [ # # ]: 0 : queue_size = ctx->msg.payload.inflight.queue_size;
1807 : :
1808 [ # # ]: 0 : if (vq_is_packed(dev))
1809 : : pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1810 : : else
1811 : : pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1812 : :
1813 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "set_inflight_fd mmap_size: %"PRIu64, mmap_size);
1814 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1815 : : "set_inflight_fd mmap_offset: %"PRIu64,
1816 : : mmap_offset);
1817 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1818 : : "set_inflight_fd num_queues: %u",
1819 : : num_queues);
1820 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1821 : : "set_inflight_fd queue_size: %u",
1822 : : queue_size);
1823 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1824 : : "set_inflight_fd fd: %d",
1825 : : fd);
1826 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1827 : : "set_inflight_fd pervq_inflight_size: %d",
1828 : : pervq_inflight_size);
1829 : :
1830 : : /*
1831 : : * If VQ 0 has already been allocated, try to allocate on the same
1832 : : * NUMA node. It can be reallocated later in numa_realloc().
1833 : : */
1834 [ # # ]: 0 : if (dev->nr_vring > 0)
1835 : 0 : numa_node = dev->virtqueue[0]->numa_node;
1836 : :
1837 [ # # ]: 0 : if (!dev->inflight_info) {
1838 : 0 : dev->inflight_info = rte_zmalloc_socket("inflight_info",
1839 : : sizeof(struct inflight_mem_info), 0, numa_node);
1840 [ # # ]: 0 : if (dev->inflight_info == NULL) {
1841 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc dev inflight area");
1842 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
1843 : : }
1844 : 0 : dev->inflight_info->fd = -1;
1845 : : }
1846 : :
1847 [ # # ]: 0 : if (dev->inflight_info->addr) {
1848 : 0 : munmap(dev->inflight_info->addr, dev->inflight_info->size);
1849 : 0 : dev->inflight_info->addr = NULL;
1850 : : }
1851 : :
1852 : 0 : addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
1853 : : fd, mmap_offset);
1854 [ # # ]: 0 : if (addr == MAP_FAILED) {
1855 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap share memory.");
1856 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
1857 : : }
1858 : :
1859 [ # # ]: 0 : if (dev->inflight_info->fd >= 0) {
1860 : 0 : close(dev->inflight_info->fd);
1861 : 0 : dev->inflight_info->fd = -1;
1862 : : }
1863 : :
1864 : 0 : mem_set_dump(dev, addr, mmap_size, false, get_blk_size(fd));
1865 : 0 : dev->inflight_info->fd = fd;
1866 : 0 : dev->inflight_info->addr = addr;
1867 : 0 : dev->inflight_info->size = mmap_size;
1868 : :
1869 [ # # ]: 0 : for (i = 0; i < num_queues; i++) {
1870 : 0 : vq = dev->virtqueue[i];
1871 [ # # ]: 0 : if (!vq)
1872 : 0 : continue;
1873 : :
1874 [ # # ]: 0 : if (vq_is_packed(dev)) {
1875 : 0 : vq->inflight_packed = addr;
1876 : 0 : vq->inflight_packed->desc_num = queue_size;
1877 : : } else {
1878 : 0 : vq->inflight_split = addr;
1879 : 0 : vq->inflight_split->desc_num = queue_size;
1880 : : }
1881 : 0 : addr = (void *)((char *)addr + pervq_inflight_size);
1882 : : }
1883 : :
1884 : : return RTE_VHOST_MSG_RESULT_OK;
1885 : : }
1886 : :
1887 : : static int
1888 : 0 : vhost_user_set_vring_call(struct virtio_net **pdev,
1889 : : struct vhu_msg_context *ctx,
1890 : : int main_fd __rte_unused)
1891 : : {
1892 : 0 : struct virtio_net *dev = *pdev;
1893 : : struct vhost_vring_file file;
1894 : : struct vhost_virtqueue *vq;
1895 : : int expected_fds;
1896 : :
1897 : 0 : expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1898 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, expected_fds) != 0)
1899 : : return RTE_VHOST_MSG_RESULT_ERR;
1900 : :
1901 : 0 : file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
1902 [ # # ]: 0 : if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1903 : : file.fd = VIRTIO_INVALID_EVENTFD;
1904 : : else
1905 : 0 : file.fd = ctx->fds[0];
1906 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1907 : : "vring call idx:%d file:%d",
1908 : : file.index, file.fd);
1909 : :
1910 : 0 : vq = dev->virtqueue[file.index];
1911 : :
1912 [ # # ]: 0 : if (vq->ready) {
1913 : 0 : vq->ready = false;
1914 : 0 : vhost_user_notify_queue_state(dev, vq, 0);
1915 : : }
1916 : :
1917 [ # # ]: 0 : if (vq->callfd >= 0)
1918 : 0 : close(vq->callfd);
1919 : :
1920 : 0 : vq->callfd = file.fd;
1921 : :
1922 : 0 : return RTE_VHOST_MSG_RESULT_OK;
1923 : : }
1924 : :
1925 : 0 : static int vhost_user_set_vring_err(struct virtio_net **pdev,
1926 : : struct vhu_msg_context *ctx,
1927 : : int main_fd __rte_unused)
1928 : : {
1929 : 0 : struct virtio_net *dev = *pdev;
1930 : : int expected_fds;
1931 : :
1932 : 0 : expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1933 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, expected_fds) != 0)
1934 : : return RTE_VHOST_MSG_RESULT_ERR;
1935 : :
1936 [ # # ]: 0 : if (!(ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1937 : 0 : close(ctx->fds[0]);
1938 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "not implemented");
1939 : :
1940 : 0 : return RTE_VHOST_MSG_RESULT_OK;
1941 : : }
1942 : :
1943 : : static int
1944 : 0 : resubmit_desc_compare(const void *a, const void *b)
1945 : : {
1946 : : const struct rte_vhost_resubmit_desc *desc0 = a;
1947 : : const struct rte_vhost_resubmit_desc *desc1 = b;
1948 : :
1949 [ # # ]: 0 : if (desc1->counter > desc0->counter)
1950 : 0 : return 1;
1951 : :
1952 : : return -1;
1953 : : }
1954 : :
1955 : : static int
1956 : 0 : vhost_check_queue_inflights_split(struct virtio_net *dev,
1957 : : struct vhost_virtqueue *vq)
1958 : : {
1959 : : uint16_t i;
1960 : : uint16_t resubmit_num = 0, last_io, num;
1961 : 0 : struct vring_used *used = vq->used;
1962 : : struct rte_vhost_resubmit_info *resubmit;
1963 : : struct rte_vhost_inflight_info_split *inflight_split;
1964 : :
1965 [ # # ]: 0 : if (!(dev->protocol_features &
1966 : : (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1967 : : return RTE_VHOST_MSG_RESULT_OK;
1968 : :
1969 : : /* The frontend may still not support the inflight feature
1970 : : * although we negotiate the protocol feature.
1971 : : */
1972 [ # # ]: 0 : if ((!vq->inflight_split))
1973 : : return RTE_VHOST_MSG_RESULT_OK;
1974 : :
1975 [ # # ]: 0 : if (!vq->inflight_split->version) {
1976 : 0 : vq->inflight_split->version = INFLIGHT_VERSION;
1977 : 0 : return RTE_VHOST_MSG_RESULT_OK;
1978 : : }
1979 : :
1980 [ # # ]: 0 : if (vq->resubmit_inflight)
1981 : : return RTE_VHOST_MSG_RESULT_OK;
1982 : :
1983 : : inflight_split = vq->inflight_split;
1984 : 0 : vq->global_counter = 0;
1985 : 0 : last_io = inflight_split->last_inflight_io;
1986 : :
1987 [ # # ]: 0 : if (inflight_split->used_idx != used->idx) {
1988 : 0 : inflight_split->desc[last_io].inflight = 0;
1989 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
1990 : 0 : inflight_split->used_idx = used->idx;
1991 : : }
1992 : :
1993 [ # # ]: 0 : for (i = 0; i < inflight_split->desc_num; i++) {
1994 [ # # ]: 0 : if (inflight_split->desc[i].inflight == 1)
1995 : 0 : resubmit_num++;
1996 : : }
1997 : :
1998 : 0 : vq->last_avail_idx += resubmit_num;
1999 : :
2000 [ # # ]: 0 : if (resubmit_num) {
2001 : 0 : resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
2002 : : 0, vq->numa_node);
2003 [ # # ]: 0 : if (!resubmit) {
2004 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2005 : : "failed to allocate memory for resubmit info.");
2006 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2007 : : }
2008 : :
2009 : 0 : resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
2010 : : resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
2011 : : 0, vq->numa_node);
2012 [ # # ]: 0 : if (!resubmit->resubmit_list) {
2013 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2014 : : "failed to allocate memory for inflight desc.");
2015 : 0 : rte_free(resubmit);
2016 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2017 : : }
2018 : :
2019 : : num = 0;
2020 [ # # ]: 0 : for (i = 0; i < vq->inflight_split->desc_num; i++) {
2021 [ # # ]: 0 : if (vq->inflight_split->desc[i].inflight == 1) {
2022 : 0 : resubmit->resubmit_list[num].index = i;
2023 : 0 : resubmit->resubmit_list[num].counter =
2024 : 0 : inflight_split->desc[i].counter;
2025 : 0 : num++;
2026 : : }
2027 : : }
2028 : 0 : resubmit->resubmit_num = num;
2029 : :
2030 [ # # ]: 0 : if (resubmit->resubmit_num > 1)
2031 : 0 : qsort(resubmit->resubmit_list, resubmit->resubmit_num,
2032 : : sizeof(struct rte_vhost_resubmit_desc),
2033 : : resubmit_desc_compare);
2034 : :
2035 : 0 : vq->global_counter = resubmit->resubmit_list[0].counter + 1;
2036 : 0 : vq->resubmit_inflight = resubmit;
2037 : : }
2038 : :
2039 : : return RTE_VHOST_MSG_RESULT_OK;
2040 : : }
2041 : :
2042 : : static int
2043 : 0 : vhost_check_queue_inflights_packed(struct virtio_net *dev,
2044 : : struct vhost_virtqueue *vq)
2045 : : {
2046 : : uint16_t i;
2047 : : uint16_t resubmit_num = 0, old_used_idx, num;
2048 : : struct rte_vhost_resubmit_info *resubmit;
2049 : : struct rte_vhost_inflight_info_packed *inflight_packed;
2050 : :
2051 [ # # ]: 0 : if (!(dev->protocol_features &
2052 : : (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
2053 : : return RTE_VHOST_MSG_RESULT_OK;
2054 : :
2055 : : /* The frontend may still not support the inflight feature
2056 : : * although we negotiate the protocol feature.
2057 : : */
2058 [ # # ]: 0 : if ((!vq->inflight_packed))
2059 : : return RTE_VHOST_MSG_RESULT_OK;
2060 : :
2061 [ # # ]: 0 : if (!vq->inflight_packed->version) {
2062 : 0 : vq->inflight_packed->version = INFLIGHT_VERSION;
2063 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2064 : : }
2065 : :
2066 [ # # ]: 0 : if (vq->resubmit_inflight)
2067 : : return RTE_VHOST_MSG_RESULT_OK;
2068 : :
2069 : : inflight_packed = vq->inflight_packed;
2070 : 0 : vq->global_counter = 0;
2071 : 0 : old_used_idx = inflight_packed->old_used_idx;
2072 : :
2073 [ # # ]: 0 : if (inflight_packed->used_idx != old_used_idx) {
2074 [ # # ]: 0 : if (inflight_packed->desc[old_used_idx].inflight == 0) {
2075 : 0 : inflight_packed->old_used_idx =
2076 : : inflight_packed->used_idx;
2077 : 0 : inflight_packed->old_used_wrap_counter =
2078 : 0 : inflight_packed->used_wrap_counter;
2079 : 0 : inflight_packed->old_free_head =
2080 : 0 : inflight_packed->free_head;
2081 : : } else {
2082 : 0 : inflight_packed->used_idx =
2083 : : inflight_packed->old_used_idx;
2084 : 0 : inflight_packed->used_wrap_counter =
2085 : 0 : inflight_packed->old_used_wrap_counter;
2086 : 0 : inflight_packed->free_head =
2087 : 0 : inflight_packed->old_free_head;
2088 : : }
2089 : : }
2090 : :
2091 [ # # ]: 0 : for (i = 0; i < inflight_packed->desc_num; i++) {
2092 [ # # ]: 0 : if (inflight_packed->desc[i].inflight == 1)
2093 : 0 : resubmit_num++;
2094 : : }
2095 : :
2096 [ # # ]: 0 : if (resubmit_num) {
2097 : 0 : resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
2098 : : 0, vq->numa_node);
2099 [ # # ]: 0 : if (resubmit == NULL) {
2100 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2101 : : "failed to allocate memory for resubmit info.");
2102 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2103 : : }
2104 : :
2105 : 0 : resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
2106 : : resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
2107 : : 0, vq->numa_node);
2108 [ # # ]: 0 : if (resubmit->resubmit_list == NULL) {
2109 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2110 : : "failed to allocate memory for resubmit desc.");
2111 : 0 : rte_free(resubmit);
2112 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2113 : : }
2114 : :
2115 : : num = 0;
2116 [ # # ]: 0 : for (i = 0; i < inflight_packed->desc_num; i++) {
2117 [ # # ]: 0 : if (vq->inflight_packed->desc[i].inflight == 1) {
2118 : 0 : resubmit->resubmit_list[num].index = i;
2119 : 0 : resubmit->resubmit_list[num].counter =
2120 : 0 : inflight_packed->desc[i].counter;
2121 : 0 : num++;
2122 : : }
2123 : : }
2124 : 0 : resubmit->resubmit_num = num;
2125 : :
2126 [ # # ]: 0 : if (resubmit->resubmit_num > 1)
2127 : 0 : qsort(resubmit->resubmit_list, resubmit->resubmit_num,
2128 : : sizeof(struct rte_vhost_resubmit_desc),
2129 : : resubmit_desc_compare);
2130 : :
2131 : 0 : vq->global_counter = resubmit->resubmit_list[0].counter + 1;
2132 : 0 : vq->resubmit_inflight = resubmit;
2133 : : }
2134 : :
2135 : : return RTE_VHOST_MSG_RESULT_OK;
2136 : : }
2137 : :
2138 : : static int
2139 : 0 : vhost_user_set_vring_kick(struct virtio_net **pdev,
2140 : : struct vhu_msg_context *ctx,
2141 : : int main_fd __rte_unused)
2142 : : {
2143 : 0 : struct virtio_net *dev = *pdev;
2144 : : struct vhost_vring_file file;
2145 : : struct vhost_virtqueue *vq;
2146 : : int expected_fds;
2147 : :
2148 : 0 : expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
2149 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, expected_fds) != 0)
2150 : : return RTE_VHOST_MSG_RESULT_ERR;
2151 : :
2152 : 0 : file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
2153 [ # # ]: 0 : if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
2154 : : file.fd = VIRTIO_INVALID_EVENTFD;
2155 : : else
2156 : 0 : file.fd = ctx->fds[0];
2157 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2158 : : "vring kick idx:%d file:%d",
2159 : : file.index, file.fd);
2160 : :
2161 : : /* Interpret ring addresses only when ring is started. */
2162 : 0 : vq = dev->virtqueue[file.index];
2163 : 0 : translate_ring_addresses(&dev, &vq);
2164 : 0 : *pdev = dev;
2165 : :
2166 : : /*
2167 : : * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
2168 : : * the ring starts already enabled. Otherwise, it is enabled via
2169 : : * the SET_VRING_ENABLE message.
2170 : : */
2171 [ # # ]: 0 : if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
2172 : 0 : vq->enabled = true;
2173 : : }
2174 : :
2175 [ # # ]: 0 : if (vq->ready) {
2176 : 0 : vq->ready = false;
2177 : 0 : vhost_user_notify_queue_state(dev, vq, 0);
2178 : : }
2179 : :
2180 [ # # ]: 0 : if (vq->kickfd >= 0)
2181 : 0 : close(vq->kickfd);
2182 [ # # ]: 0 : vq->kickfd = file.fd;
2183 : :
2184 [ # # ]: 0 : if (vq_is_packed(dev)) {
2185 [ # # ]: 0 : if (vhost_check_queue_inflights_packed(dev, vq)) {
2186 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2187 : : "failed to inflights for vq: %d",
2188 : : file.index);
2189 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2190 : : }
2191 : : } else {
2192 [ # # ]: 0 : if (vhost_check_queue_inflights_split(dev, vq)) {
2193 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2194 : : "failed to inflights for vq: %d",
2195 : : file.index);
2196 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2197 : : }
2198 : : }
2199 : :
2200 : : return RTE_VHOST_MSG_RESULT_OK;
2201 : : }
2202 : :
2203 : : /*
2204 : : * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
2205 : : */
2206 : : static int
2207 : 0 : vhost_user_get_vring_base(struct virtio_net **pdev,
2208 : : struct vhu_msg_context *ctx,
2209 : : int main_fd __rte_unused)
2210 : : {
2211 : 0 : struct virtio_net *dev = *pdev;
2212 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
2213 : : uint64_t val;
2214 : :
2215 : : /* We have to stop the queue (virtio) if it is running. */
2216 : 0 : vhost_destroy_device_notify(dev);
2217 : :
2218 : 0 : dev->flags &= ~VIRTIO_DEV_READY;
2219 [ # # ]: 0 : dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
2220 : :
2221 : : /* Here we are safe to get the indexes */
2222 [ # # ]: 0 : if (vq_is_packed(dev)) {
2223 : : /*
2224 : : * Bit[0:14]: avail index
2225 : : * Bit[15]: avail wrap counter
2226 : : */
2227 : 0 : val = vq->last_avail_idx & 0x7fff;
2228 : 0 : val |= vq->avail_wrap_counter << 15;
2229 : 0 : ctx->msg.payload.state.num = val;
2230 : : } else {
2231 : 0 : ctx->msg.payload.state.num = vq->last_avail_idx;
2232 : : }
2233 : :
2234 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2235 : : "vring base idx:%d file:%d",
2236 : : ctx->msg.payload.state.index, ctx->msg.payload.state.num);
2237 : : /*
2238 : : * Based on current qemu vhost-user implementation, this message is
2239 : : * sent and only sent in vhost_vring_stop.
2240 : : * TODO: cleanup the vring, it isn't usable since here.
2241 : : */
2242 [ # # ]: 0 : if (vq->kickfd >= 0)
2243 : 0 : close(vq->kickfd);
2244 : :
2245 : 0 : vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
2246 : :
2247 [ # # ]: 0 : if (vq->callfd >= 0)
2248 : 0 : close(vq->callfd);
2249 : :
2250 : 0 : vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
2251 : :
2252 [ # # ]: 0 : vq->signalled_used_valid = false;
2253 : :
2254 [ # # ]: 0 : if (vq_is_packed(dev)) {
2255 : 0 : rte_free(vq->shadow_used_packed);
2256 : 0 : vq->shadow_used_packed = NULL;
2257 : : } else {
2258 : 0 : rte_free(vq->shadow_used_split);
2259 : 0 : vq->shadow_used_split = NULL;
2260 : : }
2261 : :
2262 : 0 : rte_free(vq->batch_copy_elems);
2263 : 0 : vq->batch_copy_elems = NULL;
2264 : :
2265 : 0 : rte_free(vq->log_cache);
2266 : 0 : vq->log_cache = NULL;
2267 : :
2268 : 0 : ctx->msg.size = sizeof(ctx->msg.payload.state);
2269 : 0 : ctx->fd_num = 0;
2270 : :
2271 : 0 : vhost_user_iotlb_flush_all(dev);
2272 : :
2273 : 0 : rte_rwlock_write_lock(&vq->access_lock);
2274 : 0 : vring_invalidate(dev, vq);
2275 : : rte_rwlock_write_unlock(&vq->access_lock);
2276 : :
2277 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
2278 : : }
2279 : :
2280 : : /*
2281 : : * when virtio queues are ready to work, qemu will send us to
2282 : : * enable the virtio queue pair.
2283 : : */
2284 : : static int
2285 : 0 : vhost_user_set_vring_enable(struct virtio_net **pdev,
2286 : : struct vhu_msg_context *ctx,
2287 : : int main_fd __rte_unused)
2288 : : {
2289 : 0 : struct virtio_net *dev = *pdev;
2290 : : struct vhost_virtqueue *vq;
2291 : 0 : bool enable = !!ctx->msg.payload.state.num;
2292 : 0 : int index = (int)ctx->msg.payload.state.index;
2293 : :
2294 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2295 : : "set queue enable: %d to qp idx: %d",
2296 : : enable, index);
2297 : :
2298 : 0 : vq = dev->virtqueue[index];
2299 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
2300 : : /* vhost_user_lock_all_queue_pairs locked all qps */
2301 : 0 : VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_VRING_ENABLE);
2302 [ # # # # : 0 : if (enable && vq->async && vq->async->pkts_inflight_n) {
# # ]
2303 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2304 : : "failed to enable vring. Inflight packets must be completed first");
2305 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2306 : : }
2307 : : }
2308 : :
2309 : 0 : vq->enabled = enable;
2310 : :
2311 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2312 : : }
2313 : :
2314 : : static int
2315 : 0 : vhost_user_get_protocol_features(struct virtio_net **pdev,
2316 : : struct vhu_msg_context *ctx,
2317 : : int main_fd __rte_unused)
2318 : : {
2319 : 0 : struct virtio_net *dev = *pdev;
2320 : : uint64_t features, protocol_features;
2321 : :
2322 : 0 : rte_vhost_driver_get_features(dev->ifname, &features);
2323 : 0 : rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
2324 : :
2325 : 0 : ctx->msg.payload.u64 = protocol_features;
2326 : 0 : ctx->msg.size = sizeof(ctx->msg.payload.u64);
2327 : 0 : ctx->fd_num = 0;
2328 : :
2329 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
2330 : : }
2331 : :
2332 : : static int
2333 : 0 : vhost_user_set_protocol_features(struct virtio_net **pdev,
2334 : : struct vhu_msg_context *ctx,
2335 : : int main_fd __rte_unused)
2336 : : {
2337 : 0 : struct virtio_net *dev = *pdev;
2338 : 0 : uint64_t protocol_features = ctx->msg.payload.u64;
2339 : 0 : uint64_t backend_protocol_features = 0;
2340 : :
2341 : 0 : rte_vhost_driver_get_protocol_features(dev->ifname,
2342 : : &backend_protocol_features);
2343 [ # # ]: 0 : if (protocol_features & ~backend_protocol_features) {
2344 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "received invalid protocol features.");
2345 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2346 : : }
2347 : :
2348 : 0 : dev->protocol_features = protocol_features;
2349 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2350 : : "negotiated Vhost-user protocol features: 0x%" PRIx64,
2351 : : dev->protocol_features);
2352 : :
2353 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2354 : : }
2355 : :
2356 : : static int
2357 : 0 : vhost_user_set_log_base(struct virtio_net **pdev,
2358 : : struct vhu_msg_context *ctx,
2359 : : int main_fd __rte_unused)
2360 : : {
2361 : 0 : struct virtio_net *dev = *pdev;
2362 : 0 : int fd = ctx->fds[0];
2363 : : uint64_t size, off;
2364 : : uint64_t alignment;
2365 : : void *addr;
2366 : : uint32_t i;
2367 : :
2368 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, 1) != 0)
2369 : : return RTE_VHOST_MSG_RESULT_ERR;
2370 : :
2371 [ # # ]: 0 : if (fd < 0) {
2372 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid log fd: %d", fd);
2373 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2374 : : }
2375 : :
2376 [ # # ]: 0 : if (ctx->msg.size != sizeof(VhostUserLog)) {
2377 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2378 : : "invalid log base msg size: %"PRId32" != %d",
2379 : : ctx->msg.size, (int)sizeof(VhostUserLog));
2380 : 0 : goto close_msg_fds;
2381 : : }
2382 : :
2383 : 0 : size = ctx->msg.payload.log.mmap_size;
2384 : 0 : off = ctx->msg.payload.log.mmap_offset;
2385 : :
2386 : : /* Check for mmap size and offset overflow. */
2387 [ # # ]: 0 : if (off >= -size) {
2388 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2389 : : "log offset %#"PRIx64" and log size %#"PRIx64" overflow",
2390 : : off, size);
2391 : 0 : goto close_msg_fds;
2392 : : }
2393 : :
2394 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2395 : : "log mmap size: %"PRId64", offset: %"PRId64,
2396 : : size, off);
2397 : :
2398 : : /*
2399 : : * mmap from 0 to workaround a hugepage mmap bug: mmap will
2400 : : * fail when offset is not page size aligned.
2401 : : */
2402 : 0 : addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
2403 : : alignment = get_blk_size(fd);
2404 : 0 : close(fd);
2405 [ # # ]: 0 : if (addr == MAP_FAILED) {
2406 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "mmap log base failed!");
2407 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2408 : : }
2409 : :
2410 : : /*
2411 : : * Free previously mapped log memory on occasionally
2412 : : * multiple VHOST_USER_SET_LOG_BASE.
2413 : : */
2414 [ # # ]: 0 : if (dev->log_addr) {
2415 : 0 : munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
2416 : : }
2417 : 0 : dev->log_addr = (uint64_t)(uintptr_t)addr;
2418 : 0 : dev->log_base = dev->log_addr + off;
2419 : 0 : dev->log_size = size;
2420 : 0 : mem_set_dump(dev, addr, size + off, false, alignment);
2421 : :
2422 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
2423 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
2424 : :
2425 : 0 : rte_free(vq->log_cache);
2426 : 0 : vq->log_cache = NULL;
2427 : 0 : vq->log_cache_nb_elem = 0;
2428 : 0 : vq->log_cache = rte_malloc_socket("vq log cache",
2429 : : sizeof(struct log_cache_entry) * VHOST_LOG_CACHE_NR,
2430 : : 0, vq->numa_node);
2431 : : /*
2432 : : * If log cache alloc fail, don't fail migration, but no
2433 : : * caching will be done, which will impact performance
2434 : : */
2435 [ # # ]: 0 : if (!vq->log_cache)
2436 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2437 : : "failed to allocate VQ logging cache");
2438 : : }
2439 : :
2440 : : /*
2441 : : * The spec is not clear about it (yet), but QEMU doesn't expect
2442 : : * any payload in the reply.
2443 : : */
2444 : 0 : ctx->msg.size = 0;
2445 : 0 : ctx->fd_num = 0;
2446 : :
2447 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
2448 : :
2449 : 0 : close_msg_fds:
2450 : 0 : close_msg_fds(ctx);
2451 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2452 : : }
2453 : :
2454 : 0 : static int vhost_user_set_log_fd(struct virtio_net **pdev,
2455 : : struct vhu_msg_context *ctx,
2456 : : int main_fd __rte_unused)
2457 : : {
2458 : 0 : struct virtio_net *dev = *pdev;
2459 : :
2460 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, 1) != 0)
2461 : : return RTE_VHOST_MSG_RESULT_ERR;
2462 : :
2463 : 0 : close(ctx->fds[0]);
2464 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "not implemented.");
2465 : :
2466 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2467 : : }
2468 : :
2469 : : /*
2470 : : * An rarp packet is constructed and broadcasted to notify switches about
2471 : : * the new location of the migrated VM, so that packets from outside will
2472 : : * not be lost after migration.
2473 : : *
2474 : : * However, we don't actually "send" a rarp packet here, instead, we set
2475 : : * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
2476 : : */
2477 : : static int
2478 : 0 : vhost_user_send_rarp(struct virtio_net **pdev,
2479 : : struct vhu_msg_context *ctx,
2480 : : int main_fd __rte_unused)
2481 : : {
2482 : 0 : struct virtio_net *dev = *pdev;
2483 : 0 : uint8_t *mac = (uint8_t *)&ctx->msg.payload.u64;
2484 : : struct rte_vdpa_device *vdpa_dev;
2485 : :
2486 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG,
2487 : : "MAC: " RTE_ETHER_ADDR_PRT_FMT,
2488 : : mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
2489 [ # # ]: 0 : memcpy(dev->mac.addr_bytes, mac, 6);
2490 : :
2491 : : /*
2492 : : * Set the flag to inject a RARP broadcast packet at
2493 : : * rte_vhost_dequeue_burst().
2494 : : *
2495 : : * rte_memory_order_release ordering is for making sure the mac is
2496 : : * copied before the flag is set.
2497 : : */
2498 : 0 : rte_atomic_store_explicit(&dev->broadcast_rarp, 1, rte_memory_order_release);
2499 : 0 : vdpa_dev = dev->vdpa_dev;
2500 [ # # # # ]: 0 : if (vdpa_dev && vdpa_dev->ops->migration_done)
2501 : 0 : vdpa_dev->ops->migration_done(dev->vid);
2502 : :
2503 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2504 : : }
2505 : :
2506 : : static int
2507 : 0 : vhost_user_net_set_mtu(struct virtio_net **pdev,
2508 : : struct vhu_msg_context *ctx,
2509 : : int main_fd __rte_unused)
2510 : : {
2511 : 0 : struct virtio_net *dev = *pdev;
2512 : :
2513 [ # # ]: 0 : if (ctx->msg.payload.u64 < VIRTIO_MIN_MTU ||
2514 : : ctx->msg.payload.u64 > VIRTIO_MAX_MTU) {
2515 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2516 : : "invalid MTU size (%"PRIu64")",
2517 : : ctx->msg.payload.u64);
2518 : :
2519 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2520 : : }
2521 : :
2522 : 0 : dev->mtu = ctx->msg.payload.u64;
2523 : :
2524 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2525 : : }
2526 : :
2527 : : static int
2528 : 0 : vhost_user_set_req_fd(struct virtio_net **pdev,
2529 : : struct vhu_msg_context *ctx,
2530 : : int main_fd __rte_unused)
2531 : : {
2532 : 0 : struct virtio_net *dev = *pdev;
2533 : 0 : int fd = ctx->fds[0];
2534 : :
2535 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, 1) != 0)
2536 : : return RTE_VHOST_MSG_RESULT_ERR;
2537 : :
2538 [ # # ]: 0 : if (fd < 0) {
2539 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2540 : : "invalid file descriptor for backend channel (%d)", fd);
2541 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2542 : : }
2543 : :
2544 [ # # ]: 0 : if (dev->backend_req_fd >= 0)
2545 : 0 : close(dev->backend_req_fd);
2546 : :
2547 : 0 : dev->backend_req_fd = fd;
2548 : :
2549 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2550 : : }
2551 : :
2552 : : static int
2553 : 0 : is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2554 : : {
2555 : : struct vhost_vring_addr *ra;
2556 : : uint64_t start, end, len;
2557 : :
2558 : 0 : start = imsg->iova;
2559 : 0 : end = start + imsg->size;
2560 : :
2561 : : ra = &vq->ring_addrs;
2562 : 0 : len = sizeof(struct vring_desc) * vq->size;
2563 [ # # # # ]: 0 : if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2564 : : return 1;
2565 : :
2566 : 0 : len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
2567 [ # # # # ]: 0 : if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2568 : : return 1;
2569 : :
2570 : 0 : len = sizeof(struct vring_used) +
2571 : 0 : sizeof(struct vring_used_elem) * vq->size;
2572 [ # # # # ]: 0 : if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2573 : : return 1;
2574 : :
2575 [ # # ]: 0 : if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2576 : : len = sizeof(uint64_t);
2577 [ # # ]: 0 : if (ra->log_guest_addr < end &&
2578 [ # # ]: 0 : (ra->log_guest_addr + len) > start)
2579 : 0 : return 1;
2580 : : }
2581 : :
2582 : : return 0;
2583 : : }
2584 : :
2585 : : static int
2586 : 0 : is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2587 : : {
2588 : : struct vhost_vring_addr *ra;
2589 : : uint64_t start, end, len;
2590 : :
2591 : 0 : start = imsg->iova;
2592 : 0 : end = start + imsg->size;
2593 : :
2594 : : ra = &vq->ring_addrs;
2595 : 0 : len = sizeof(struct vring_packed_desc) * vq->size;
2596 [ # # # # ]: 0 : if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2597 : : return 1;
2598 : :
2599 : : len = sizeof(struct vring_packed_desc_event);
2600 [ # # # # ]: 0 : if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2601 : : return 1;
2602 : :
2603 : : len = sizeof(struct vring_packed_desc_event);
2604 [ # # # # ]: 0 : if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2605 : : return 1;
2606 : :
2607 [ # # ]: 0 : if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2608 : : len = sizeof(uint64_t);
2609 [ # # ]: 0 : if (ra->log_guest_addr < end &&
2610 [ # # ]: 0 : (ra->log_guest_addr + len) > start)
2611 : 0 : return 1;
2612 : : }
2613 : :
2614 : : return 0;
2615 : : }
2616 : :
2617 [ # # ]: 0 : static int is_vring_iotlb(struct virtio_net *dev,
2618 : : struct vhost_virtqueue *vq,
2619 : : struct vhost_iotlb_msg *imsg)
2620 : : {
2621 [ # # ]: 0 : if (vq_is_packed(dev))
2622 : 0 : return is_vring_iotlb_packed(vq, imsg);
2623 : : else
2624 : 0 : return is_vring_iotlb_split(vq, imsg);
2625 : : }
2626 : :
2627 : : static int
2628 : 0 : vhost_user_get_config(struct virtio_net **pdev,
2629 : : struct vhu_msg_context *ctx,
2630 : : int main_fd __rte_unused)
2631 : : {
2632 : 0 : struct virtio_net *dev = *pdev;
2633 : 0 : struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
2634 : : int ret = 0;
2635 : :
2636 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, 0) != 0)
2637 : : return RTE_VHOST_MSG_RESULT_ERR;
2638 : :
2639 [ # # ]: 0 : if (!vdpa_dev) {
2640 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "is not vDPA device!");
2641 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2642 : : }
2643 : :
2644 [ # # ]: 0 : if (vdpa_dev->ops->get_config) {
2645 : 0 : ret = vdpa_dev->ops->get_config(dev->vid,
2646 : 0 : ctx->msg.payload.cfg.region,
2647 : : ctx->msg.payload.cfg.size);
2648 [ # # ]: 0 : if (ret != 0) {
2649 : 0 : ctx->msg.size = 0;
2650 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "get_config() return error!");
2651 : : }
2652 : : } else {
2653 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "get_config() not supported!");
2654 : : }
2655 : :
2656 : : return RTE_VHOST_MSG_RESULT_REPLY;
2657 : : }
2658 : :
2659 : : static int
2660 : 0 : vhost_user_set_config(struct virtio_net **pdev,
2661 : : struct vhu_msg_context *ctx,
2662 : : int main_fd __rte_unused)
2663 : : {
2664 : 0 : struct virtio_net *dev = *pdev;
2665 : 0 : struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
2666 : : int ret = 0;
2667 : :
2668 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, 0) != 0)
2669 : : return RTE_VHOST_MSG_RESULT_ERR;
2670 : :
2671 [ # # ]: 0 : if (ctx->msg.payload.cfg.size > VHOST_USER_MAX_CONFIG_SIZE) {
2672 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2673 : : "vhost_user_config size: %"PRIu32", should not be larger than %d",
2674 : : ctx->msg.payload.cfg.size, VHOST_USER_MAX_CONFIG_SIZE);
2675 : 0 : goto out;
2676 : : }
2677 : :
2678 [ # # ]: 0 : if (!vdpa_dev) {
2679 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "is not vDPA device!");
2680 : 0 : goto out;
2681 : : }
2682 : :
2683 [ # # ]: 0 : if (vdpa_dev->ops->set_config) {
2684 : 0 : ret = vdpa_dev->ops->set_config(dev->vid,
2685 : 0 : ctx->msg.payload.cfg.region,
2686 : : ctx->msg.payload.cfg.offset,
2687 : : ctx->msg.payload.cfg.size,
2688 : : ctx->msg.payload.cfg.flags);
2689 [ # # ]: 0 : if (ret)
2690 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "set_config() return error!");
2691 : : } else {
2692 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "set_config() not supported!");
2693 : : }
2694 : :
2695 : : return RTE_VHOST_MSG_RESULT_OK;
2696 : :
2697 : : out:
2698 : : return RTE_VHOST_MSG_RESULT_ERR;
2699 : : }
2700 : :
2701 : : static int
2702 : 0 : vhost_user_iotlb_msg(struct virtio_net **pdev,
2703 : : struct vhu_msg_context *ctx,
2704 : : int main_fd __rte_unused)
2705 : : {
2706 : 0 : struct virtio_net *dev = *pdev;
2707 : 0 : struct vhost_iotlb_msg *imsg = &ctx->msg.payload.iotlb;
2708 : : uint16_t i;
2709 : : uint64_t vva, len, pg_sz;
2710 : :
2711 [ # # # ]: 0 : switch (imsg->type) {
2712 : 0 : case VHOST_IOTLB_UPDATE:
2713 : 0 : len = imsg->size;
2714 : 0 : vva = qva_to_vva(dev, imsg->uaddr, &len);
2715 [ # # ]: 0 : if (!vva)
2716 : : return RTE_VHOST_MSG_RESULT_ERR;
2717 : :
2718 : 0 : pg_sz = hua_to_alignment(dev->mem, (void *)(uintptr_t)vva);
2719 : :
2720 : 0 : vhost_user_iotlb_cache_insert(dev, imsg->iova, vva, 0, len, pg_sz, imsg->perm);
2721 : :
2722 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
2723 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
2724 : :
2725 [ # # ]: 0 : if (!vq)
2726 : 0 : continue;
2727 : :
2728 [ # # ]: 0 : if (is_vring_iotlb(dev, vq, imsg)) {
2729 : 0 : rte_rwlock_write_lock(&vq->access_lock);
2730 : 0 : translate_ring_addresses(&dev, &vq);
2731 : 0 : *pdev = dev;
2732 : 0 : rte_rwlock_write_unlock(&vq->access_lock);
2733 : : }
2734 : : }
2735 : : break;
2736 : 0 : case VHOST_IOTLB_INVALIDATE:
2737 : 0 : vhost_user_iotlb_cache_remove(dev, imsg->iova, imsg->size);
2738 : :
2739 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
2740 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
2741 : :
2742 [ # # ]: 0 : if (!vq)
2743 : 0 : continue;
2744 : :
2745 [ # # ]: 0 : if (is_vring_iotlb(dev, vq, imsg)) {
2746 : 0 : rte_rwlock_write_lock(&vq->access_lock);
2747 : 0 : vring_invalidate(dev, vq);
2748 : : rte_rwlock_write_unlock(&vq->access_lock);
2749 : : }
2750 : : }
2751 : : break;
2752 : 0 : default:
2753 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid IOTLB message type (%d)",
2754 : : imsg->type);
2755 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2756 : : }
2757 : :
2758 : : return RTE_VHOST_MSG_RESULT_OK;
2759 : : }
2760 : :
2761 : : static int
2762 : 0 : vhost_user_set_postcopy_advise(struct virtio_net **pdev,
2763 : : struct vhu_msg_context *ctx,
2764 : : int main_fd __rte_unused)
2765 : : {
2766 : 0 : struct virtio_net *dev = *pdev;
2767 : : #ifdef RTE_LIBRTE_VHOST_POSTCOPY
2768 : : struct uffdio_api api_struct;
2769 : :
2770 : 0 : dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
2771 : :
2772 [ # # ]: 0 : if (dev->postcopy_ufd == -1) {
2773 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2774 : : "userfaultfd not available: %s",
2775 : : strerror(errno));
2776 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2777 : : }
2778 : 0 : api_struct.api = UFFD_API;
2779 : 0 : api_struct.features = 0;
2780 [ # # ]: 0 : if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
2781 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2782 : : "UFFDIO_API ioctl failure: %s",
2783 : : strerror(errno));
2784 : 0 : close(dev->postcopy_ufd);
2785 : 0 : dev->postcopy_ufd = -1;
2786 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2787 : : }
2788 : 0 : ctx->fds[0] = dev->postcopy_ufd;
2789 : 0 : ctx->fd_num = 1;
2790 : :
2791 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
2792 : : #else
2793 : : dev->postcopy_ufd = -1;
2794 : : ctx->fd_num = 0;
2795 : :
2796 : : return RTE_VHOST_MSG_RESULT_ERR;
2797 : : #endif
2798 : : }
2799 : :
2800 : : static int
2801 : 0 : vhost_user_set_postcopy_listen(struct virtio_net **pdev,
2802 : : struct vhu_msg_context *ctx __rte_unused,
2803 : : int main_fd __rte_unused)
2804 : : {
2805 : 0 : struct virtio_net *dev = *pdev;
2806 : :
2807 [ # # # # ]: 0 : if (dev->mem && dev->mem->nregions) {
2808 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2809 : : "regions already registered at postcopy-listen");
2810 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2811 : : }
2812 : 0 : dev->postcopy_listening = 1;
2813 : :
2814 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2815 : : }
2816 : :
2817 : : static int
2818 : 0 : vhost_user_postcopy_end(struct virtio_net **pdev,
2819 : : struct vhu_msg_context *ctx,
2820 : : int main_fd __rte_unused)
2821 : : {
2822 : 0 : struct virtio_net *dev = *pdev;
2823 : :
2824 : 0 : dev->postcopy_listening = 0;
2825 [ # # ]: 0 : if (dev->postcopy_ufd >= 0) {
2826 : 0 : close(dev->postcopy_ufd);
2827 : 0 : dev->postcopy_ufd = -1;
2828 : : }
2829 : :
2830 : 0 : ctx->msg.payload.u64 = 0;
2831 : 0 : ctx->msg.size = sizeof(ctx->msg.payload.u64);
2832 : 0 : ctx->fd_num = 0;
2833 : :
2834 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
2835 : : }
2836 : :
2837 : : static int
2838 : 0 : vhost_user_get_status(struct virtio_net **pdev,
2839 : : struct vhu_msg_context *ctx,
2840 : : int main_fd __rte_unused)
2841 : : {
2842 : 0 : struct virtio_net *dev = *pdev;
2843 : :
2844 : 0 : ctx->msg.payload.u64 = dev->status;
2845 : 0 : ctx->msg.size = sizeof(ctx->msg.payload.u64);
2846 : 0 : ctx->fd_num = 0;
2847 : :
2848 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
2849 : : }
2850 : :
2851 : : static int
2852 : 0 : vhost_user_set_status(struct virtio_net **pdev,
2853 : : struct vhu_msg_context *ctx,
2854 : : int main_fd __rte_unused)
2855 : : {
2856 : 0 : struct virtio_net *dev = *pdev;
2857 : :
2858 : : /* As per Virtio specification, the device status is 8bits long */
2859 [ # # ]: 0 : if (ctx->msg.payload.u64 > UINT8_MAX) {
2860 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2861 : : "invalid VHOST_USER_SET_STATUS payload 0x%" PRIx64,
2862 : : ctx->msg.payload.u64);
2863 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2864 : : }
2865 : :
2866 : 0 : dev->status = ctx->msg.payload.u64;
2867 : :
2868 [ # # ]: 0 : if ((dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK) &&
2869 [ # # ]: 0 : (dev->flags & VIRTIO_DEV_FEATURES_FAILED)) {
2870 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2871 : : "FEATURES_OK bit is set but feature negotiation failed");
2872 : : /*
2873 : : * Clear the bit to let the driver know about the feature
2874 : : * negotiation failure
2875 : : */
2876 : 0 : dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
2877 : : }
2878 : :
2879 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "new device status(0x%08x):", dev->status);
2880 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2881 : : "\t-RESET: %u",
2882 : : (dev->status == VIRTIO_DEVICE_STATUS_RESET));
2883 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2884 : : "\t-ACKNOWLEDGE: %u",
2885 : : !!(dev->status & VIRTIO_DEVICE_STATUS_ACK));
2886 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2887 : : "\t-DRIVER: %u",
2888 : : !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER));
2889 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2890 : : "\t-FEATURES_OK: %u",
2891 : : !!(dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK));
2892 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2893 : : "\t-DRIVER_OK: %u",
2894 : : !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK));
2895 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2896 : : "\t-DEVICE_NEED_RESET: %u",
2897 : : !!(dev->status & VIRTIO_DEVICE_STATUS_DEV_NEED_RESET));
2898 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2899 : : "\t-FAILED: %u",
2900 : : !!(dev->status & VIRTIO_DEVICE_STATUS_FAILED));
2901 : :
2902 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2903 : : }
2904 : :
2905 : : #define VHOST_MESSAGE_HANDLER(id, handler, accepts_fd, lock_all_qps) \
2906 : : [id] = { #id, handler, accepts_fd, id ## _LOCK_ALL_QPS },
2907 : : static vhost_message_handler_t vhost_message_handlers[] = {
2908 : : VHOST_MESSAGE_HANDLERS
2909 : : };
2910 : : #undef VHOST_MESSAGE_HANDLER
2911 : :
2912 : : /* return bytes# of read on success or negative val on failure. */
2913 : : static int
2914 : 0 : read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2915 : : {
2916 : : int ret;
2917 : :
2918 : 0 : ret = read_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, VHOST_USER_HDR_SIZE,
2919 : 0 : ctx->fds, VHOST_MEMORY_MAX_NREGIONS, &ctx->fd_num);
2920 [ # # ]: 0 : if (ret <= 0)
2921 : 0 : goto out;
2922 : :
2923 [ # # ]: 0 : if (ret != VHOST_USER_HDR_SIZE) {
2924 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Unexpected header size read");
2925 : : ret = -1;
2926 : 0 : goto out;
2927 : : }
2928 : :
2929 [ # # ]: 0 : if (ctx->msg.size) {
2930 [ # # ]: 0 : if (ctx->msg.size > sizeof(ctx->msg.payload)) {
2931 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid msg size: %d",
2932 : : ctx->msg.size);
2933 : : ret = -1;
2934 : 0 : goto out;
2935 : : }
2936 [ # # ]: 0 : ret = read(sockfd, &ctx->msg.payload, ctx->msg.size);
2937 [ # # ]: 0 : if (ret <= 0)
2938 : 0 : goto out;
2939 [ # # ]: 0 : if (ret != (int)ctx->msg.size) {
2940 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "read control message failed");
2941 : : ret = -1;
2942 : 0 : goto out;
2943 : : }
2944 : : }
2945 : :
2946 : 0 : out:
2947 [ # # ]: 0 : if (ret <= 0)
2948 : 0 : close_msg_fds(ctx);
2949 : :
2950 : 0 : return ret;
2951 : : }
2952 : :
2953 : : static int
2954 : : send_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2955 : : {
2956 : 0 : if (!ctx)
2957 : : return 0;
2958 : :
2959 : 0 : return send_fd_message(dev->ifname, sockfd, (char *)&ctx->msg,
2960 : 0 : VHOST_USER_HDR_SIZE + ctx->msg.size, ctx->fds, ctx->fd_num);
2961 : : }
2962 : :
2963 : : static int
2964 : 0 : send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2965 : : {
2966 [ # # ]: 0 : if (!ctx)
2967 : : return 0;
2968 : :
2969 : 0 : ctx->msg.flags &= ~VHOST_USER_VERSION_MASK;
2970 : 0 : ctx->msg.flags &= ~VHOST_USER_NEED_REPLY;
2971 : 0 : ctx->msg.flags |= VHOST_USER_VERSION;
2972 : 0 : ctx->msg.flags |= VHOST_USER_REPLY_MASK;
2973 : :
2974 : 0 : return send_vhost_message(dev, sockfd, ctx);
2975 : : }
2976 : :
2977 : : static int
2978 : 0 : send_vhost_backend_message(struct virtio_net *dev, struct vhu_msg_context *ctx)
2979 : : {
2980 [ # # ]: 0 : return send_vhost_message(dev, dev->backend_req_fd, ctx);
2981 : : }
2982 : :
2983 : : static int
2984 : 0 : send_vhost_backend_message_process_reply(struct virtio_net *dev, struct vhu_msg_context *ctx)
2985 : : {
2986 : : struct vhu_msg_context msg_reply;
2987 : : int ret;
2988 : :
2989 : 0 : rte_spinlock_lock(&dev->backend_req_lock);
2990 : 0 : ret = send_vhost_backend_message(dev, ctx);
2991 [ # # ]: 0 : if (ret < 0) {
2992 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to send config change (%d)", ret);
2993 : 0 : goto out;
2994 : : }
2995 : :
2996 : 0 : ret = read_vhost_message(dev, dev->backend_req_fd, &msg_reply);
2997 [ # # ]: 0 : if (ret <= 0) {
2998 [ # # ]: 0 : if (ret < 0)
2999 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
3000 : : "vhost read backend message reply failed");
3001 : : else
3002 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "vhost peer closed");
3003 : : ret = -1;
3004 : 0 : goto out;
3005 : : }
3006 : :
3007 [ # # ]: 0 : if (msg_reply.msg.request.backend != ctx->msg.request.backend) {
3008 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
3009 : : "received unexpected msg type (%u), expected %u",
3010 : : msg_reply.msg.request.backend, ctx->msg.request.backend);
3011 : : ret = -1;
3012 : 0 : goto out;
3013 : : }
3014 : :
3015 [ # # ]: 0 : ret = msg_reply.msg.payload.u64 ? -1 : 0;
3016 : 0 : out:
3017 : : rte_spinlock_unlock(&dev->backend_req_lock);
3018 : 0 : return ret;
3019 : : }
3020 : :
3021 : : /*
3022 : : * Allocate a queue pair if it hasn't been allocated yet
3023 : : */
3024 : : static int
3025 : 0 : vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev,
3026 : : struct vhu_msg_context *ctx)
3027 : : {
3028 : : uint32_t vring_idx;
3029 : :
3030 [ # # # # : 0 : switch (ctx->msg.request.frontend) {
# ]
3031 : 0 : case VHOST_USER_SET_VRING_KICK:
3032 : : case VHOST_USER_SET_VRING_CALL:
3033 : : case VHOST_USER_SET_VRING_ERR:
3034 : 0 : vring_idx = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
3035 : 0 : break;
3036 : 0 : case VHOST_USER_SET_VRING_NUM:
3037 : : case VHOST_USER_SET_VRING_BASE:
3038 : : case VHOST_USER_GET_VRING_BASE:
3039 : : case VHOST_USER_SET_VRING_ENABLE:
3040 : 0 : vring_idx = ctx->msg.payload.state.index;
3041 : 0 : break;
3042 : 0 : case VHOST_USER_SET_VRING_ADDR:
3043 : 0 : vring_idx = ctx->msg.payload.addr.index;
3044 : 0 : break;
3045 : 0 : case VHOST_USER_SET_INFLIGHT_FD:
3046 : 0 : vring_idx = ctx->msg.payload.inflight.num_queues - 1;
3047 : 0 : break;
3048 : : default:
3049 : : return 0;
3050 : : }
3051 : :
3052 [ # # ]: 0 : if (vring_idx >= VHOST_MAX_VRING) {
3053 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid vring index: %u", vring_idx);
3054 : 0 : return -1;
3055 : : }
3056 : :
3057 [ # # ]: 0 : if (dev->virtqueue[vring_idx])
3058 : : return 0;
3059 : :
3060 : 0 : return alloc_vring_queue(dev, vring_idx);
3061 : : }
3062 : :
3063 : : static void
3064 : 0 : vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
3065 : : __rte_no_thread_safety_analysis
3066 : : {
3067 : : unsigned int i = 0;
3068 : : unsigned int vq_num = 0;
3069 : :
3070 [ # # ]: 0 : while (vq_num < dev->nr_vring) {
3071 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
3072 : :
3073 [ # # ]: 0 : if (vq) {
3074 : 0 : rte_rwlock_write_lock(&vq->access_lock);
3075 : 0 : vq_num++;
3076 : : }
3077 : 0 : i++;
3078 : : }
3079 : 0 : }
3080 : :
3081 : : static void
3082 : 0 : vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
3083 : : __rte_no_thread_safety_analysis
3084 : : {
3085 : : unsigned int i = 0;
3086 : : unsigned int vq_num = 0;
3087 : :
3088 [ # # ]: 0 : while (vq_num < dev->nr_vring) {
3089 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
3090 : :
3091 [ # # ]: 0 : if (vq) {
3092 : : rte_rwlock_write_unlock(&vq->access_lock);
3093 : 0 : vq_num++;
3094 : : }
3095 : 0 : i++;
3096 : : }
3097 : 0 : }
3098 : :
3099 : : int
3100 [ # # ]: 0 : vhost_user_msg_handler(int vid, int fd)
3101 : : {
3102 : : struct virtio_net *dev;
3103 : : struct vhu_msg_context ctx;
3104 : : vhost_message_handler_t *msg_handler;
3105 : : struct rte_vdpa_device *vdpa_dev;
3106 : : int msg_result = RTE_VHOST_MSG_RESULT_OK;
3107 : : int ret;
3108 : : int unlock_required = 0;
3109 : : bool handled;
3110 : : uint32_t request;
3111 : : uint32_t i;
3112 : : uint16_t blk_call_fd;
3113 : :
3114 : 0 : dev = get_device(vid);
3115 [ # # ]: 0 : if (dev == NULL)
3116 : : return -1;
3117 : :
3118 [ # # ]: 0 : if (!dev->notify_ops) {
3119 : 0 : dev->notify_ops = vhost_driver_callback_get(dev->ifname);
3120 [ # # ]: 0 : if (!dev->notify_ops) {
3121 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
3122 : : "failed to get callback ops for driver");
3123 : 0 : return -1;
3124 : : }
3125 : : }
3126 : :
3127 : 0 : ctx.msg.request.frontend = VHOST_USER_NONE;
3128 : 0 : ret = read_vhost_message(dev, fd, &ctx);
3129 [ # # ]: 0 : if (ret == 0) {
3130 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "vhost peer closed");
3131 : 0 : return -1;
3132 : : }
3133 : :
3134 : 0 : request = ctx.msg.request.frontend;
3135 [ # # ]: 0 : if (request > VHOST_USER_NONE && request < RTE_DIM(vhost_message_handlers))
3136 : 0 : msg_handler = &vhost_message_handlers[request];
3137 : : else
3138 : : msg_handler = NULL;
3139 : :
3140 [ # # ]: 0 : if (ret < 0) {
3141 [ # # # # : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "vhost read message %s%s%sfailed",
# # ]
3142 : : msg_handler != NULL ? "for " : "",
3143 : : msg_handler != NULL ? msg_handler->description : "",
3144 : : msg_handler != NULL ? " " : "");
3145 : 0 : return -1;
3146 : : }
3147 : :
3148 [ # # # # ]: 0 : if (msg_handler != NULL && msg_handler->description != NULL) {
3149 [ # # ]: 0 : if (request != VHOST_USER_IOTLB_MSG)
3150 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
3151 : : "read message %s",
3152 : : msg_handler->description);
3153 : : else
3154 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG,
3155 : : "read message %s",
3156 : : msg_handler->description);
3157 : : } else {
3158 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "external request %d", request);
3159 : : }
3160 : :
3161 : 0 : ret = vhost_user_check_and_alloc_queue_pair(dev, &ctx);
3162 [ # # ]: 0 : if (ret < 0) {
3163 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc queue");
3164 : 0 : return -1;
3165 : : }
3166 : :
3167 : : /*
3168 : : * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
3169 : : * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
3170 : : * and device is destroyed. destroy_device waits for queues to be
3171 : : * inactive, so it is safe. Otherwise taking the access_lock
3172 : : * would cause a dead lock.
3173 : : */
3174 [ # # # # ]: 0 : if (msg_handler != NULL && msg_handler->lock_all_qps) {
3175 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3176 : 0 : vhost_user_lock_all_queue_pairs(dev);
3177 : : unlock_required = 1;
3178 : : }
3179 : : }
3180 : :
3181 : : handled = false;
3182 [ # # ]: 0 : if (dev->extern_ops.pre_msg_handle) {
3183 : : RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
3184 : 0 : msg_result = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx);
3185 [ # # # ]: 0 : switch (msg_result) {
3186 : 0 : case RTE_VHOST_MSG_RESULT_REPLY:
3187 : 0 : send_vhost_reply(dev, fd, &ctx);
3188 : : /* Fall-through */
3189 : 0 : case RTE_VHOST_MSG_RESULT_ERR:
3190 : : case RTE_VHOST_MSG_RESULT_OK:
3191 : : handled = true;
3192 : 0 : goto skip_to_post_handle;
3193 : : case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3194 : : default:
3195 : : break;
3196 : : }
3197 : : }
3198 : :
3199 [ # # # # ]: 0 : if (msg_handler == NULL || msg_handler->callback == NULL)
3200 : 0 : goto skip_to_post_handle;
3201 : :
3202 [ # # # # ]: 0 : if (!msg_handler->accepts_fd && validate_msg_fds(dev, &ctx, 0) != 0) {
3203 : : msg_result = RTE_VHOST_MSG_RESULT_ERR;
3204 : : } else {
3205 : 0 : msg_result = msg_handler->callback(&dev, &ctx, fd);
3206 : : }
3207 : :
3208 [ # # # # ]: 0 : switch (msg_result) {
3209 : 0 : case RTE_VHOST_MSG_RESULT_ERR:
3210 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
3211 : : "processing %s failed.",
3212 : : msg_handler->description);
3213 : : handled = true;
3214 : 0 : break;
3215 : 0 : case RTE_VHOST_MSG_RESULT_OK:
3216 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG,
3217 : : "processing %s succeeded.",
3218 : : msg_handler->description);
3219 : : handled = true;
3220 : 0 : break;
3221 : 0 : case RTE_VHOST_MSG_RESULT_REPLY:
3222 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG,
3223 : : "processing %s succeeded and needs reply.",
3224 : : msg_handler->description);
3225 : 0 : send_vhost_reply(dev, fd, &ctx);
3226 : : handled = true;
3227 : 0 : break;
3228 : : default:
3229 : : break;
3230 : : }
3231 : :
3232 : 0 : skip_to_post_handle:
3233 [ # # ]: 0 : if (msg_result != RTE_VHOST_MSG_RESULT_ERR &&
3234 [ # # ]: 0 : dev->extern_ops.post_msg_handle) {
3235 : : RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
3236 : 0 : msg_result = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx);
3237 [ # # # ]: 0 : switch (msg_result) {
3238 : 0 : case RTE_VHOST_MSG_RESULT_REPLY:
3239 : 0 : send_vhost_reply(dev, fd, &ctx);
3240 : : /* Fall-through */
3241 : : case RTE_VHOST_MSG_RESULT_ERR:
3242 : : case RTE_VHOST_MSG_RESULT_OK:
3243 : : handled = true;
3244 : : case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3245 : : default:
3246 : : break;
3247 : : }
3248 : : }
3249 : :
3250 : : /* If message was not handled at this stage, treat it as an error */
3251 [ # # ]: 0 : if (!handled) {
3252 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
3253 : : "vhost message (req: %d) was not handled.",
3254 : : request);
3255 : 0 : close_msg_fds(&ctx);
3256 : : msg_result = RTE_VHOST_MSG_RESULT_ERR;
3257 : : }
3258 : :
3259 : : /*
3260 : : * If the request required a reply that was already sent,
3261 : : * this optional reply-ack won't be sent as the
3262 : : * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply().
3263 : : */
3264 [ # # ]: 0 : if (ctx.msg.flags & VHOST_USER_NEED_REPLY) {
3265 : 0 : ctx.msg.payload.u64 = msg_result == RTE_VHOST_MSG_RESULT_ERR;
3266 : 0 : ctx.msg.size = sizeof(ctx.msg.payload.u64);
3267 : 0 : ctx.fd_num = 0;
3268 : 0 : send_vhost_reply(dev, fd, &ctx);
3269 [ # # ]: 0 : } else if (msg_result == RTE_VHOST_MSG_RESULT_ERR) {
3270 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "vhost message handling failed.");
3271 : : ret = -1;
3272 : 0 : goto unlock;
3273 : : }
3274 : :
3275 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
3276 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
3277 : 0 : bool cur_ready = vq_is_ready(dev, vq);
3278 : :
3279 [ # # # # : 0 : if (cur_ready != (vq && vq->ready)) {
# # ]
3280 : 0 : vq->ready = cur_ready;
3281 : 0 : vhost_user_notify_queue_state(dev, vq, cur_ready);
3282 : : }
3283 : : }
3284 : :
3285 : 0 : unlock:
3286 [ # # ]: 0 : if (unlock_required)
3287 : 0 : vhost_user_unlock_all_queue_pairs(dev);
3288 : :
3289 [ # # # # ]: 0 : if (ret != 0 || !virtio_is_ready(dev))
3290 : 0 : goto out;
3291 : :
3292 : : /*
3293 : : * Virtio is now ready. If not done already, it is time
3294 : : * to notify the application it can process the rings and
3295 : : * configure the vDPA device if present.
3296 : : */
3297 : :
3298 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
3299 [ # # ]: 0 : if (dev->notify_ops->new_device(dev->vid) == 0)
3300 : 0 : dev->flags |= VIRTIO_DEV_RUNNING;
3301 : : }
3302 : :
3303 : 0 : vdpa_dev = dev->vdpa_dev;
3304 [ # # ]: 0 : if (!vdpa_dev)
3305 : 0 : goto out;
3306 : :
3307 [ # # ]: 0 : if (vdpa_dev->type == RTE_VHOST_VDPA_DEVICE_TYPE_BLK) {
3308 [ # # ]: 0 : if (request == VHOST_USER_SET_VRING_CALL) {
3309 : 0 : blk_call_fd = ctx.msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
3310 [ # # ]: 0 : if (blk_call_fd != dev->nr_vring - 1)
3311 : 0 : goto out;
3312 : : } else {
3313 : 0 : goto out;
3314 : : }
3315 : : }
3316 : :
3317 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3318 [ # # ]: 0 : if (vdpa_dev->ops->dev_conf(dev->vid))
3319 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to configure vDPA device");
3320 : : else
3321 : 0 : dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
3322 : : }
3323 : :
3324 : 0 : out:
3325 : : return ret;
3326 : : }
3327 : :
3328 : : static int
3329 : 0 : vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
3330 : : {
3331 : : int ret;
3332 : 0 : struct vhu_msg_context ctx = {
3333 : : .msg = {
3334 : : .request.backend = VHOST_USER_BACKEND_IOTLB_MSG,
3335 : : .flags = VHOST_USER_VERSION,
3336 : : .size = sizeof(ctx.msg.payload.iotlb),
3337 : : .payload.iotlb = {
3338 : : .iova = iova,
3339 : : .perm = perm,
3340 : : .type = VHOST_IOTLB_MISS,
3341 : : },
3342 : : },
3343 : : };
3344 : :
3345 : 0 : ret = send_vhost_message(dev, dev->backend_req_fd, &ctx);
3346 [ # # ]: 0 : if (ret < 0) {
3347 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
3348 : : "failed to send IOTLB miss message (%d)",
3349 : : ret);
3350 : 0 : return ret;
3351 : : }
3352 : :
3353 : : return 0;
3354 : : }
3355 : :
3356 : : int
3357 : 0 : rte_vhost_backend_config_change(int vid, bool need_reply)
3358 : : {
3359 [ # # ]: 0 : struct vhu_msg_context ctx = {
3360 : : .msg = {
3361 : : .request.backend = VHOST_USER_BACKEND_CONFIG_CHANGE_MSG,
3362 : : .flags = VHOST_USER_VERSION,
3363 : : .size = 0,
3364 : : }
3365 : : };
3366 : : struct virtio_net *dev;
3367 : : int ret;
3368 : :
3369 : : dev = get_device(vid);
3370 [ # # ]: 0 : if (!dev)
3371 : : return -ENODEV;
3372 : :
3373 [ # # ]: 0 : if (!need_reply) {
3374 : : ret = send_vhost_backend_message(dev, &ctx);
3375 : : } else {
3376 : 0 : ctx.msg.flags |= VHOST_USER_NEED_REPLY;
3377 : 0 : ret = send_vhost_backend_message_process_reply(dev, &ctx);
3378 : : }
3379 : :
3380 [ # # ]: 0 : if (ret < 0)
3381 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to send config change (%d)", ret);
3382 : : return ret;
3383 : : }
3384 : :
3385 : 0 : static int vhost_user_backend_set_vring_host_notifier(struct virtio_net *dev,
3386 : : int index, int fd,
3387 : : uint64_t offset,
3388 : : uint64_t size)
3389 : : {
3390 : : int ret;
3391 : 0 : struct vhu_msg_context ctx = {
3392 : : .msg = {
3393 : : .request.backend = VHOST_USER_BACKEND_VRING_HOST_NOTIFIER_MSG,
3394 : : .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
3395 : : .size = sizeof(ctx.msg.payload.area),
3396 : : .payload.area = {
3397 : 0 : .u64 = index & VHOST_USER_VRING_IDX_MASK,
3398 : : .size = size,
3399 : : .offset = offset,
3400 : : },
3401 : : },
3402 : : };
3403 : :
3404 [ # # ]: 0 : if (fd < 0)
3405 : 0 : ctx.msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
3406 : : else {
3407 : 0 : ctx.fds[0] = fd;
3408 : 0 : ctx.fd_num = 1;
3409 : : }
3410 : :
3411 : 0 : ret = send_vhost_backend_message_process_reply(dev, &ctx);
3412 [ # # ]: 0 : if (ret < 0)
3413 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to set host notifier (%d)", ret);
3414 : :
3415 : 0 : return ret;
3416 : : }
3417 : :
3418 [ # # ]: 0 : int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable)
3419 : : {
3420 : : struct virtio_net *dev;
3421 : : struct rte_vdpa_device *vdpa_dev;
3422 : : int vfio_device_fd, ret = 0;
3423 : : uint64_t offset, size;
3424 : : unsigned int i, q_start, q_last;
3425 : :
3426 : : dev = get_device(vid);
3427 [ # # ]: 0 : if (!dev)
3428 : : return -ENODEV;
3429 : :
3430 : 0 : vdpa_dev = dev->vdpa_dev;
3431 [ # # ]: 0 : if (vdpa_dev == NULL)
3432 : : return -ENODEV;
3433 : :
3434 [ # # ]: 0 : if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
3435 : : !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
3436 : : !(dev->protocol_features &
3437 : : (1ULL << VHOST_USER_PROTOCOL_F_BACKEND_REQ)) ||
3438 : : !(dev->protocol_features &
3439 [ # # ]: 0 : (1ULL << VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD)) ||
3440 : : !(dev->protocol_features &
3441 : : (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
3442 : : return -ENOTSUP;
3443 : :
3444 [ # # ]: 0 : if (qid == RTE_VHOST_QUEUE_ALL) {
3445 : : q_start = 0;
3446 : 0 : q_last = dev->nr_vring - 1;
3447 : : } else {
3448 [ # # ]: 0 : if (qid >= dev->nr_vring)
3449 : : return -EINVAL;
3450 : : q_start = qid;
3451 : : q_last = qid;
3452 : : }
3453 : :
3454 [ # # ]: 0 : if (vdpa_dev->ops->get_vfio_device_fd == NULL)
3455 : : return -ENOTSUP;
3456 [ # # ]: 0 : if (vdpa_dev->ops->get_notify_area == NULL)
3457 : : return -ENOTSUP;
3458 : :
3459 : 0 : vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
3460 [ # # ]: 0 : if (vfio_device_fd < 0)
3461 : : return -ENOTSUP;
3462 : :
3463 [ # # ]: 0 : if (enable) {
3464 [ # # ]: 0 : for (i = q_start; i <= q_last; i++) {
3465 [ # # ]: 0 : if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
3466 : : &size) < 0) {
3467 : : ret = -ENOTSUP;
3468 : 0 : goto disable;
3469 : : }
3470 : :
3471 [ # # ]: 0 : if (vhost_user_backend_set_vring_host_notifier(dev, i,
3472 : : vfio_device_fd, offset, size) < 0) {
3473 : : ret = -EFAULT;
3474 : 0 : goto disable;
3475 : : }
3476 : : }
3477 : : } else {
3478 : 0 : disable:
3479 [ # # ]: 0 : for (i = q_start; i <= q_last; i++) {
3480 : 0 : vhost_user_backend_set_vring_host_notifier(dev, i, -1,
3481 : : 0, 0);
3482 : : }
3483 : : }
3484 : :
3485 : : return ret;
3486 : : }
3487 : :
3488 : : static int
3489 : 0 : vhost_user_inject_irq(struct virtio_net *dev __rte_unused, struct vhost_virtqueue *vq)
3490 : : {
3491 [ # # ]: 0 : if (vq->callfd < 0)
3492 : : return -1;
3493 : :
3494 : 0 : return eventfd_write(vq->callfd, (eventfd_t)1);
3495 : : }
3496 : :
3497 : : static struct vhost_backend_ops vhost_user_backend_ops = {
3498 : : .iotlb_miss = vhost_user_iotlb_miss,
3499 : : .inject_irq = vhost_user_inject_irq,
3500 : : };
3501 : :
3502 : : int
3503 : 0 : vhost_user_new_device(void)
3504 : : {
3505 : 0 : return vhost_new_device(&vhost_user_backend_ops);
3506 : : }
|