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 : : vhost_virtqueue_reconnect_log_split(vq);
958 : 0 : VHOST_CONFIG_LOG(dev->ifname, WARNING,
959 : : "some packets maybe resent for Tx and dropped for Rx");
960 : : }
961 : :
962 : 0 : vq->access_ok = true;
963 : :
964 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "mapped address desc: %p", vq->desc);
965 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "mapped address avail: %p", vq->avail);
966 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "mapped address used: %p", vq->used);
967 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "log_guest_addr: %" PRIx64, vq->log_guest_addr);
968 : : }
969 : :
970 : : /*
971 : : * The virtio device sends us the desc, used and avail ring addresses.
972 : : * This function then converts these to our address space.
973 : : */
974 : : static int
975 : 0 : vhost_user_set_vring_addr(struct virtio_net **pdev,
976 : : struct vhu_msg_context *ctx,
977 : : int main_fd __rte_unused)
978 : : {
979 : 0 : struct virtio_net *dev = *pdev;
980 : : struct vhost_virtqueue *vq;
981 : 0 : struct vhost_vring_addr *addr = &ctx->msg.payload.addr;
982 : : bool access_ok;
983 : :
984 [ # # ]: 0 : if (dev->mem == NULL)
985 : : return RTE_VHOST_MSG_RESULT_ERR;
986 : :
987 : : /* addr->index refers to the queue index. The txq 1, rxq is 0. */
988 : 0 : vq = dev->virtqueue[ctx->msg.payload.addr.index];
989 : :
990 : : /*
991 : : * Rings addresses should not be interpreted as long as the ring is not
992 : : * started and enabled
993 : : */
994 [ # # ]: 0 : memcpy(&vq->ring_addrs, addr, sizeof(*addr));
995 : :
996 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)
997 : 0 : goto out;
998 : :
999 : : /* vhost_user_lock_all_queue_pairs locked all qps */
1000 : 0 : VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_VRING_ADDR);
1001 : :
1002 : 0 : access_ok = vq->access_ok;
1003 : :
1004 : 0 : vring_invalidate(dev, vq);
1005 : :
1006 [ # # # # ]: 0 : if ((vq->enabled && (dev->features &
1007 [ # # ]: 0 : (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) ||
1008 : : access_ok) {
1009 : 0 : translate_ring_addresses(&dev, &vq);
1010 : 0 : *pdev = dev;
1011 : : }
1012 : :
1013 : 0 : out:
1014 : : return RTE_VHOST_MSG_RESULT_OK;
1015 : : }
1016 : :
1017 : : /*
1018 : : * The virtio device sends us the available ring last used index.
1019 : : */
1020 : : static int
1021 : 0 : vhost_user_set_vring_base(struct virtio_net **pdev,
1022 : : struct vhu_msg_context *ctx,
1023 : : int main_fd __rte_unused)
1024 : : {
1025 : 0 : struct virtio_net *dev = *pdev;
1026 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
1027 [ # # ]: 0 : uint64_t val = ctx->msg.payload.state.num;
1028 : :
1029 [ # # ]: 0 : if (vq_is_packed(dev)) {
1030 : : /*
1031 : : * Bit[0:14]: avail index
1032 : : * Bit[15]: avail wrap counter
1033 : : */
1034 : 0 : vq->last_avail_idx = val & 0x7fff;
1035 : 0 : vq->avail_wrap_counter = !!(val & (0x1 << 15));
1036 : : /*
1037 : : * Set used index to same value as available one, as
1038 : : * their values should be the same since ring processing
1039 : : * was stopped at get time.
1040 : : */
1041 : 0 : vq->last_used_idx = vq->last_avail_idx;
1042 [ # # ]: 0 : vq->used_wrap_counter = vq->avail_wrap_counter;
1043 : : vhost_virtqueue_reconnect_log_packed(vq);
1044 : : } else {
1045 : 0 : vq->last_used_idx = ctx->msg.payload.state.num;
1046 [ # # ]: 0 : vq->last_avail_idx = ctx->msg.payload.state.num;
1047 : : vhost_virtqueue_reconnect_log_split(vq);
1048 : : }
1049 : :
1050 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1051 : : "vring base idx:%u last_used_idx:%u last_avail_idx:%u.",
1052 : : ctx->msg.payload.state.index, vq->last_used_idx, vq->last_avail_idx);
1053 : :
1054 : 0 : return RTE_VHOST_MSG_RESULT_OK;
1055 : : }
1056 : :
1057 : : static int
1058 : 0 : add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
1059 : : uint64_t host_iova, uint64_t host_user_addr, uint64_t size)
1060 : : {
1061 : : struct guest_page *page, *last_page;
1062 : : struct guest_page *old_pages;
1063 : :
1064 [ # # ]: 0 : if (dev->nr_guest_pages == dev->max_guest_pages) {
1065 : 0 : dev->max_guest_pages *= 2;
1066 : 0 : old_pages = dev->guest_pages;
1067 : 0 : dev->guest_pages = rte_realloc(dev->guest_pages,
1068 : 0 : dev->max_guest_pages * sizeof(*page),
1069 : : RTE_CACHE_LINE_SIZE);
1070 [ # # ]: 0 : if (dev->guest_pages == NULL) {
1071 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "cannot realloc guest_pages");
1072 : 0 : rte_free(old_pages);
1073 : 0 : return -1;
1074 : : }
1075 : : }
1076 : :
1077 [ # # ]: 0 : if (dev->nr_guest_pages > 0) {
1078 : 0 : last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
1079 : : /* merge if the two pages are continuous */
1080 [ # # ]: 0 : if (host_iova == last_page->host_iova + last_page->size &&
1081 [ # # ]: 0 : guest_phys_addr == last_page->guest_phys_addr + last_page->size &&
1082 [ # # ]: 0 : host_user_addr == last_page->host_user_addr + last_page->size) {
1083 : 0 : last_page->size += size;
1084 : 0 : return 0;
1085 : : }
1086 : : }
1087 : :
1088 : 0 : page = &dev->guest_pages[dev->nr_guest_pages++];
1089 : 0 : page->guest_phys_addr = guest_phys_addr;
1090 : 0 : page->host_iova = host_iova;
1091 : 0 : page->host_user_addr = host_user_addr;
1092 : 0 : page->size = size;
1093 : :
1094 : 0 : return 0;
1095 : : }
1096 : :
1097 : : static int
1098 : 0 : add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
1099 : : uint64_t page_size)
1100 : : {
1101 : 0 : uint64_t reg_size = reg->size;
1102 : 0 : uint64_t host_user_addr = reg->host_user_addr;
1103 : 0 : uint64_t guest_phys_addr = reg->guest_phys_addr;
1104 : : uint64_t host_iova;
1105 : : uint64_t size;
1106 : :
1107 : 0 : host_iova = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
1108 : 0 : size = page_size - (guest_phys_addr & (page_size - 1));
1109 : 0 : size = RTE_MIN(size, reg_size);
1110 : :
1111 [ # # ]: 0 : if (add_one_guest_page(dev, guest_phys_addr, host_iova,
1112 : : host_user_addr, size) < 0)
1113 : : return -1;
1114 : :
1115 : 0 : host_user_addr += size;
1116 : 0 : guest_phys_addr += size;
1117 : 0 : reg_size -= size;
1118 : :
1119 [ # # ]: 0 : while (reg_size > 0) {
1120 : 0 : size = RTE_MIN(reg_size, page_size);
1121 : 0 : host_iova = rte_mem_virt2iova((void *)(uintptr_t)
1122 : : host_user_addr);
1123 [ # # ]: 0 : if (add_one_guest_page(dev, guest_phys_addr, host_iova,
1124 : : host_user_addr, size) < 0)
1125 : : return -1;
1126 : :
1127 : 0 : host_user_addr += size;
1128 : 0 : guest_phys_addr += size;
1129 : 0 : reg_size -= size;
1130 : : }
1131 : :
1132 : : /* sort guest page array if over binary search threshold */
1133 [ # # ]: 0 : if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
1134 : 0 : qsort((void *)dev->guest_pages, dev->nr_guest_pages,
1135 : : sizeof(struct guest_page), guest_page_addrcmp);
1136 : : }
1137 : :
1138 : : return 0;
1139 : : }
1140 : :
1141 : : #ifdef RTE_LIBRTE_VHOST_DEBUG
1142 : : /* TODO: enable it only in debug mode? */
1143 : : static void
1144 : : dump_guest_pages(struct virtio_net *dev)
1145 : : {
1146 : : uint32_t i;
1147 : : struct guest_page *page;
1148 : :
1149 : : for (i = 0; i < dev->nr_guest_pages; i++) {
1150 : : page = &dev->guest_pages[i];
1151 : :
1152 : : VHOST_CONFIG_LOG(dev->ifname, INFO, "guest physical page region %u", i);
1153 : : VHOST_CONFIG_LOG(dev->ifname, INFO, "\tguest_phys_addr: %" PRIx64,
1154 : : page->guest_phys_addr);
1155 : : VHOST_CONFIG_LOG(dev->ifname, INFO, "\thost_iova : %" PRIx64,
1156 : : page->host_iova);
1157 : : VHOST_CONFIG_LOG(dev->ifname, INFO, "\tsize : %" PRIx64,
1158 : : page->size);
1159 : : }
1160 : : }
1161 : : #else
1162 : : #define dump_guest_pages(dev)
1163 : : #endif
1164 : :
1165 : : static bool
1166 : 0 : vhost_memory_changed(struct VhostUserMemory *new,
1167 : : struct rte_vhost_memory *old)
1168 : : {
1169 : : uint32_t i;
1170 : :
1171 [ # # ]: 0 : if (new->nregions != old->nregions)
1172 : : return true;
1173 : :
1174 [ # # ]: 0 : for (i = 0; i < new->nregions; ++i) {
1175 : : VhostUserMemoryRegion *new_r = &new->regions[i];
1176 : : struct rte_vhost_mem_region *old_r = &old->regions[i];
1177 : :
1178 [ # # ]: 0 : if (new_r->guest_phys_addr != old_r->guest_phys_addr)
1179 : : return true;
1180 [ # # ]: 0 : if (new_r->memory_size != old_r->size)
1181 : : return true;
1182 [ # # ]: 0 : if (new_r->userspace_addr != old_r->guest_user_addr)
1183 : : return true;
1184 : : }
1185 : :
1186 : : return false;
1187 : : }
1188 : :
1189 : : #ifdef RTE_LIBRTE_VHOST_POSTCOPY
1190 : : static int
1191 : 0 : vhost_user_postcopy_region_register(struct virtio_net *dev,
1192 : : struct rte_vhost_mem_region *reg)
1193 : : {
1194 : : struct uffdio_register reg_struct;
1195 : :
1196 : : /*
1197 : : * Let's register all the mmapped area to ensure
1198 : : * alignment on page boundary.
1199 : : */
1200 : 0 : reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr;
1201 : 0 : reg_struct.range.len = reg->mmap_size;
1202 : 0 : reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
1203 : :
1204 [ # # ]: 0 : if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
1205 : : ®_struct)) {
1206 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1207 : : "failed to register ufd for region "
1208 : : "%" PRIx64 " - %" PRIx64 " (ufd = %d) %s",
1209 : : (uint64_t)reg_struct.range.start,
1210 : : (uint64_t)reg_struct.range.start +
1211 : : (uint64_t)reg_struct.range.len - 1,
1212 : : dev->postcopy_ufd,
1213 : : strerror(errno));
1214 : 0 : return -1;
1215 : : }
1216 : :
1217 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1218 : : "\t userfaultfd registered for range : %" PRIx64 " - %" PRIx64,
1219 : : (uint64_t)reg_struct.range.start,
1220 : : (uint64_t)reg_struct.range.start +
1221 : : (uint64_t)reg_struct.range.len - 1);
1222 : :
1223 : 0 : return 0;
1224 : : }
1225 : : #else
1226 : : static int
1227 : : vhost_user_postcopy_region_register(struct virtio_net *dev __rte_unused,
1228 : : struct rte_vhost_mem_region *reg __rte_unused)
1229 : : {
1230 : : return -1;
1231 : : }
1232 : : #endif
1233 : :
1234 : : static int
1235 : 0 : vhost_user_postcopy_register(struct virtio_net *dev, int main_fd,
1236 : : struct vhu_msg_context *ctx)
1237 : : {
1238 : : struct VhostUserMemory *memory;
1239 : : struct rte_vhost_mem_region *reg;
1240 : : struct vhu_msg_context ack_ctx;
1241 : : uint32_t i;
1242 : :
1243 [ # # ]: 0 : if (!dev->postcopy_listening)
1244 : : return 0;
1245 : :
1246 : : /*
1247 : : * We haven't a better way right now than sharing
1248 : : * DPDK's virtual address with Qemu, so that Qemu can
1249 : : * retrieve the region offset when handling userfaults.
1250 : : */
1251 : : memory = &ctx->msg.payload.memory;
1252 [ # # ]: 0 : for (i = 0; i < memory->nregions; i++) {
1253 : 0 : reg = &dev->mem->regions[i];
1254 : 0 : memory->regions[i].userspace_addr = reg->host_user_addr;
1255 : : }
1256 : :
1257 : : /* Send the addresses back to qemu */
1258 : 0 : ctx->fd_num = 0;
1259 : 0 : send_vhost_reply(dev, main_fd, ctx);
1260 : :
1261 : : /* Wait for qemu to acknowledge it got the addresses
1262 : : * we've got to wait before we're allowed to generate faults.
1263 : : */
1264 [ # # ]: 0 : if (read_vhost_message(dev, main_fd, &ack_ctx) <= 0) {
1265 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1266 : : "failed to read qemu ack on postcopy set-mem-table");
1267 : 0 : return -1;
1268 : : }
1269 : :
1270 [ # # ]: 0 : if (validate_msg_fds(dev, &ack_ctx, 0) != 0)
1271 : : return -1;
1272 : :
1273 [ # # ]: 0 : if (ack_ctx.msg.request.frontend != VHOST_USER_SET_MEM_TABLE) {
1274 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1275 : : "bad qemu ack on postcopy set-mem-table (%d)",
1276 : : ack_ctx.msg.request.frontend);
1277 : 0 : return -1;
1278 : : }
1279 : :
1280 : : /* Now userfault register and we can use the memory */
1281 [ # # ]: 0 : for (i = 0; i < memory->nregions; i++) {
1282 : 0 : reg = &dev->mem->regions[i];
1283 [ # # ]: 0 : if (vhost_user_postcopy_region_register(dev, reg) < 0)
1284 : : return -1;
1285 : : }
1286 : :
1287 : : return 0;
1288 : : }
1289 : :
1290 : : static int
1291 : 0 : vhost_user_mmap_region(struct virtio_net *dev,
1292 : : struct rte_vhost_mem_region *region,
1293 : : uint64_t mmap_offset)
1294 : : {
1295 : : void *mmap_addr;
1296 : : uint64_t mmap_size;
1297 : : uint64_t alignment;
1298 : : int populate;
1299 : :
1300 : : /* Check for memory_size + mmap_offset overflow */
1301 [ # # ]: 0 : if (mmap_offset >= -region->size) {
1302 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1303 : : "mmap_offset (%#"PRIx64") and memory_size (%#"PRIx64") overflow",
1304 : : mmap_offset, region->size);
1305 : 0 : return -1;
1306 : : }
1307 : :
1308 : 0 : mmap_size = region->size + mmap_offset;
1309 : :
1310 : : /* mmap() without flag of MAP_ANONYMOUS, should be called with length
1311 : : * argument aligned with hugepagesz at older longterm version Linux,
1312 : : * like 2.6.32 and 3.2.72, or mmap() will fail with EINVAL.
1313 : : *
1314 : : * To avoid failure, make sure in caller to keep length aligned.
1315 : : */
1316 : 0 : alignment = get_blk_size(region->fd);
1317 [ # # ]: 0 : if (alignment == (uint64_t)-1) {
1318 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "couldn't get hugepage size through fstat");
1319 : 0 : return -1;
1320 : : }
1321 : 0 : mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
1322 [ # # ]: 0 : if (mmap_size == 0) {
1323 : : /*
1324 : : * It could happen if initial mmap_size + alignment overflows
1325 : : * the sizeof uint64, which could happen if either mmap_size or
1326 : : * alignment value is wrong.
1327 : : *
1328 : : * mmap() kernel implementation would return an error, but
1329 : : * better catch it before and provide useful info in the logs.
1330 : : */
1331 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1332 : : "mmap size (0x%" PRIx64 ") or alignment (0x%" PRIx64 ") is invalid",
1333 : : region->size + mmap_offset, alignment);
1334 : 0 : return -1;
1335 : : }
1336 : :
1337 [ # # ]: 0 : populate = dev->async_copy ? MAP_POPULATE : 0;
1338 : 0 : mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
1339 : : MAP_SHARED | populate, region->fd, 0);
1340 : :
1341 [ # # ]: 0 : if (mmap_addr == MAP_FAILED) {
1342 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "mmap failed (%s).", strerror(errno));
1343 : 0 : return -1;
1344 : : }
1345 : :
1346 : 0 : region->mmap_addr = mmap_addr;
1347 : 0 : region->mmap_size = mmap_size;
1348 : 0 : region->host_user_addr = (uint64_t)(uintptr_t)mmap_addr + mmap_offset;
1349 : 0 : mem_set_dump(dev, mmap_addr, mmap_size, false, alignment);
1350 : :
1351 [ # # ]: 0 : if (dev->async_copy) {
1352 [ # # ]: 0 : if (add_guest_pages(dev, region, alignment) < 0) {
1353 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1354 : : "adding guest pages to region failed.");
1355 : 0 : return -1;
1356 : : }
1357 : : }
1358 : :
1359 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1360 : : "guest memory region size: 0x%" PRIx64,
1361 : : region->size);
1362 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1363 : : "\t guest physical addr: 0x%" PRIx64,
1364 : : region->guest_phys_addr);
1365 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1366 : : "\t guest virtual addr: 0x%" PRIx64,
1367 : : region->guest_user_addr);
1368 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1369 : : "\t host virtual addr: 0x%" PRIx64,
1370 : : region->host_user_addr);
1371 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1372 : : "\t mmap addr : 0x%" PRIx64,
1373 : : (uint64_t)(uintptr_t)mmap_addr);
1374 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1375 : : "\t mmap size : 0x%" PRIx64,
1376 : : mmap_size);
1377 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1378 : : "\t mmap align: 0x%" PRIx64,
1379 : : alignment);
1380 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1381 : : "\t mmap off : 0x%" PRIx64,
1382 : : mmap_offset);
1383 : :
1384 : 0 : return 0;
1385 : : }
1386 : :
1387 : : static int
1388 : 0 : vhost_user_set_mem_table(struct virtio_net **pdev,
1389 : : struct vhu_msg_context *ctx,
1390 : : int main_fd)
1391 : : {
1392 : 0 : struct virtio_net *dev = *pdev;
1393 : 0 : struct VhostUserMemory *memory = &ctx->msg.payload.memory;
1394 : : struct rte_vhost_mem_region *reg;
1395 : : int numa_node = SOCKET_ID_ANY;
1396 : : uint64_t mmap_offset;
1397 : : uint32_t i;
1398 : : bool async_notify = false;
1399 : :
1400 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, memory->nregions) != 0)
1401 : : return RTE_VHOST_MSG_RESULT_ERR;
1402 : :
1403 [ # # ]: 0 : if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) {
1404 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1405 : : "too many memory regions (%u)",
1406 : : memory->nregions);
1407 : 0 : goto close_msg_fds;
1408 : : }
1409 : :
1410 [ # # # # ]: 0 : if (dev->mem && !vhost_memory_changed(memory, dev->mem)) {
1411 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "memory regions not changed");
1412 : :
1413 : 0 : close_msg_fds(ctx);
1414 : :
1415 : 0 : return RTE_VHOST_MSG_RESULT_OK;
1416 : : }
1417 : :
1418 [ # # ]: 0 : if (dev->mem) {
1419 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) {
1420 : 0 : struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
1421 : :
1422 [ # # # # ]: 0 : if (vdpa_dev && vdpa_dev->ops->dev_close)
1423 : 0 : vdpa_dev->ops->dev_close(dev->vid);
1424 : 0 : dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
1425 : : }
1426 : :
1427 : : /* notify the vhost application to stop DMA transfers */
1428 [ # # # # ]: 0 : if (dev->async_copy && dev->notify_ops->vring_state_changed) {
1429 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
1430 : 0 : dev->notify_ops->vring_state_changed(dev->vid,
1431 : : i, 0);
1432 : : }
1433 : : async_notify = true;
1434 : : }
1435 : :
1436 : : /* Flush IOTLB cache as previous HVAs are now invalid */
1437 [ # # ]: 0 : if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1438 : 0 : vhost_user_iotlb_flush_all(dev);
1439 : :
1440 : 0 : free_mem_region(dev);
1441 : 0 : rte_free(dev->mem);
1442 : 0 : dev->mem = NULL;
1443 : : }
1444 : :
1445 : : /*
1446 : : * If VQ 0 has already been allocated, try to allocate on the same
1447 : : * NUMA node. It can be reallocated later in numa_realloc().
1448 : : */
1449 [ # # ]: 0 : if (dev->nr_vring > 0)
1450 : 0 : numa_node = dev->virtqueue[0]->numa_node;
1451 : :
1452 : 0 : dev->nr_guest_pages = 0;
1453 [ # # ]: 0 : if (dev->guest_pages == NULL) {
1454 : 0 : dev->max_guest_pages = 8;
1455 : 0 : dev->guest_pages = rte_zmalloc_socket(NULL,
1456 : : dev->max_guest_pages *
1457 : : sizeof(struct guest_page),
1458 : : RTE_CACHE_LINE_SIZE,
1459 : : numa_node);
1460 [ # # ]: 0 : if (dev->guest_pages == NULL) {
1461 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1462 : : "failed to allocate memory for dev->guest_pages");
1463 : 0 : goto close_msg_fds;
1464 : : }
1465 : : }
1466 : :
1467 : 0 : dev->mem = rte_zmalloc_socket("vhost-mem-table", sizeof(struct rte_vhost_memory) +
1468 : 0 : sizeof(struct rte_vhost_mem_region) * memory->nregions, 0, numa_node);
1469 [ # # ]: 0 : if (dev->mem == NULL) {
1470 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to allocate memory for dev->mem");
1471 : 0 : goto free_guest_pages;
1472 : : }
1473 : :
1474 [ # # ]: 0 : for (i = 0; i < memory->nregions; i++) {
1475 : 0 : reg = &dev->mem->regions[i];
1476 : :
1477 : 0 : reg->guest_phys_addr = memory->regions[i].guest_phys_addr;
1478 : 0 : reg->guest_user_addr = memory->regions[i].userspace_addr;
1479 : 0 : reg->size = memory->regions[i].memory_size;
1480 : 0 : reg->fd = ctx->fds[i];
1481 : :
1482 : : /*
1483 : : * Assign invalid file descriptor value to avoid double
1484 : : * closing on error path.
1485 : : */
1486 : 0 : ctx->fds[i] = -1;
1487 : :
1488 : 0 : mmap_offset = memory->regions[i].mmap_offset;
1489 : :
1490 [ # # ]: 0 : if (vhost_user_mmap_region(dev, reg, mmap_offset) < 0) {
1491 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap region %u", i);
1492 : 0 : goto free_mem_table;
1493 : : }
1494 : :
1495 : 0 : dev->mem->nregions++;
1496 : : }
1497 : :
1498 [ # # # # ]: 0 : if (dev->async_copy && rte_vfio_is_enabled("vfio"))
1499 : 0 : async_dma_map(dev, true);
1500 : :
1501 [ # # ]: 0 : if (vhost_user_postcopy_register(dev, main_fd, ctx) < 0)
1502 : 0 : goto free_mem_table;
1503 : :
1504 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
1505 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
1506 : :
1507 [ # # ]: 0 : if (!vq)
1508 : 0 : continue;
1509 : :
1510 [ # # # # : 0 : if (vq->desc || vq->avail || vq->used) {
# # ]
1511 : : /* vhost_user_lock_all_queue_pairs locked all qps */
1512 : 0 : VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_MEM_TABLE);
1513 : :
1514 : : /*
1515 : : * If the memory table got updated, the ring addresses
1516 : : * need to be translated again as virtual addresses have
1517 : : * changed.
1518 : : */
1519 : 0 : vring_invalidate(dev, vq);
1520 : :
1521 : 0 : translate_ring_addresses(&dev, &vq);
1522 : 0 : *pdev = dev;
1523 : : }
1524 : : }
1525 : :
1526 : : dump_guest_pages(dev);
1527 : :
1528 [ # # ]: 0 : if (async_notify) {
1529 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++)
1530 : 0 : dev->notify_ops->vring_state_changed(dev->vid, i, 1);
1531 : : }
1532 : :
1533 : : return RTE_VHOST_MSG_RESULT_OK;
1534 : :
1535 : 0 : free_mem_table:
1536 : 0 : free_mem_region(dev);
1537 : 0 : rte_free(dev->mem);
1538 : 0 : dev->mem = NULL;
1539 : :
1540 : 0 : free_guest_pages:
1541 : 0 : rte_free(dev->guest_pages);
1542 : 0 : dev->guest_pages = NULL;
1543 : 0 : close_msg_fds:
1544 : 0 : close_msg_fds(ctx);
1545 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
1546 : : }
1547 : :
1548 : : static bool
1549 : 0 : vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq)
1550 : : {
1551 : : bool rings_ok;
1552 : :
1553 [ # # ]: 0 : if (!vq)
1554 : : return false;
1555 : :
1556 [ # # ]: 0 : if (vq_is_packed(dev))
1557 [ # # # # ]: 0 : rings_ok = vq->desc_packed && vq->driver_event &&
1558 [ # # ]: 0 : vq->device_event;
1559 : : else
1560 [ # # # # : 0 : rings_ok = vq->desc && vq->avail && vq->used;
# # ]
1561 : :
1562 : 0 : return rings_ok &&
1563 [ # # ]: 0 : vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1564 [ # # # # ]: 0 : vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1565 [ # # ]: 0 : vq->enabled;
1566 : : }
1567 : :
1568 : : #define VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY 2u
1569 : : #define VIRTIO_BLK_NUM_VQS_TO_BE_READY 1u
1570 : :
1571 : : static int
1572 : 0 : virtio_is_ready(struct virtio_net *dev)
1573 : : {
1574 : : struct rte_vdpa_device *vdpa_dev;
1575 : : struct vhost_virtqueue *vq;
1576 : : uint32_t vdpa_type;
1577 : 0 : uint32_t i, nr_vring = dev->nr_vring;
1578 : :
1579 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_READY)
1580 : : return 1;
1581 : :
1582 [ # # ]: 0 : if (!dev->nr_vring)
1583 : : return 0;
1584 : :
1585 : 0 : vdpa_dev = dev->vdpa_dev;
1586 [ # # ]: 0 : if (vdpa_dev)
1587 : 0 : vdpa_type = vdpa_dev->type;
1588 : : else
1589 : : vdpa_type = -1;
1590 : :
1591 [ # # ]: 0 : if (vdpa_type == RTE_VHOST_VDPA_DEVICE_TYPE_BLK) {
1592 : : nr_vring = VIRTIO_BLK_NUM_VQS_TO_BE_READY;
1593 : : } else {
1594 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET)
1595 : : nr_vring = VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY;
1596 : : }
1597 : :
1598 [ # # ]: 0 : if (dev->nr_vring < nr_vring)
1599 : : return 0;
1600 : :
1601 [ # # ]: 0 : for (i = 0; i < nr_vring; i++) {
1602 : 0 : vq = dev->virtqueue[i];
1603 : :
1604 [ # # ]: 0 : if (!vq_is_ready(dev, vq))
1605 : : return 0;
1606 : : }
1607 : :
1608 : : /* If supported, ensure the frontend is really done with config */
1609 [ # # ]: 0 : if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS))
1610 [ # # ]: 0 : if (!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK))
1611 : : return 0;
1612 : :
1613 : 0 : dev->flags |= VIRTIO_DEV_READY;
1614 : :
1615 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_RUNNING))
1616 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "virtio is now ready for processing.");
1617 : : return 1;
1618 : : }
1619 : :
1620 : : static void *
1621 : 0 : inflight_mem_alloc(struct virtio_net *dev, const char *name, size_t size, int *fd)
1622 : : {
1623 : : void *ptr;
1624 : : int mfd = -1;
1625 : : uint64_t alignment;
1626 : 0 : char fname[20] = "/tmp/memfd-XXXXXX";
1627 : :
1628 : 0 : *fd = -1;
1629 : : #ifdef MEMFD_SUPPORTED
1630 : 0 : mfd = memfd_create(name, MFD_CLOEXEC);
1631 : : #else
1632 : : RTE_SET_USED(name);
1633 : : #endif
1634 [ # # ]: 0 : if (mfd == -1) {
1635 : 0 : mfd = mkstemp(fname);
1636 [ # # ]: 0 : if (mfd == -1) {
1637 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to get inflight buffer fd");
1638 : 0 : return NULL;
1639 : : }
1640 : :
1641 : 0 : unlink(fname);
1642 : : }
1643 : :
1644 [ # # ]: 0 : if (ftruncate(mfd, size) == -1) {
1645 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc inflight buffer");
1646 : 0 : close(mfd);
1647 : 0 : return NULL;
1648 : : }
1649 : :
1650 : 0 : ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
1651 [ # # ]: 0 : if (ptr == MAP_FAILED) {
1652 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap inflight buffer");
1653 : 0 : close(mfd);
1654 : 0 : return NULL;
1655 : : }
1656 : :
1657 : : alignment = get_blk_size(mfd);
1658 : 0 : mem_set_dump(dev, ptr, size, false, alignment);
1659 : 0 : *fd = mfd;
1660 : 0 : return ptr;
1661 : : }
1662 : :
1663 : : static uint32_t
1664 : : get_pervq_shm_size_split(uint16_t queue_size)
1665 : : {
1666 : 0 : return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_split) *
1667 : : queue_size + sizeof(uint64_t) +
1668 : : sizeof(uint16_t) * 4, INFLIGHT_ALIGNMENT);
1669 : : }
1670 : :
1671 : : static uint32_t
1672 : : get_pervq_shm_size_packed(uint16_t queue_size)
1673 : : {
1674 : 0 : return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_packed)
1675 : : * queue_size + sizeof(uint64_t) +
1676 : : sizeof(uint16_t) * 6 + sizeof(uint8_t) * 9,
1677 : : INFLIGHT_ALIGNMENT);
1678 : : }
1679 : :
1680 : : static int
1681 : 0 : vhost_user_get_inflight_fd(struct virtio_net **pdev,
1682 : : struct vhu_msg_context *ctx,
1683 : : int main_fd __rte_unused)
1684 : : {
1685 : : struct rte_vhost_inflight_info_packed *inflight_packed;
1686 : : uint64_t pervq_inflight_size, mmap_size;
1687 : : uint16_t num_queues, queue_size;
1688 : 0 : struct virtio_net *dev = *pdev;
1689 : : int fd, i, j;
1690 : : int numa_node = SOCKET_ID_ANY;
1691 : : void *addr;
1692 : :
1693 [ # # ]: 0 : if (ctx->msg.size != sizeof(ctx->msg.payload.inflight)) {
1694 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1695 : : "invalid get_inflight_fd message size is %d",
1696 : : ctx->msg.size);
1697 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
1698 : : }
1699 : :
1700 : : /*
1701 : : * If VQ 0 has already been allocated, try to allocate on the same
1702 : : * NUMA node. It can be reallocated later in numa_realloc().
1703 : : */
1704 [ # # ]: 0 : if (dev->nr_vring > 0)
1705 : 0 : numa_node = dev->virtqueue[0]->numa_node;
1706 : :
1707 [ # # ]: 0 : if (dev->inflight_info == NULL) {
1708 : 0 : dev->inflight_info = rte_zmalloc_socket("inflight_info",
1709 : : sizeof(struct inflight_mem_info), 0, numa_node);
1710 [ # # ]: 0 : if (!dev->inflight_info) {
1711 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc dev inflight area");
1712 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
1713 : : }
1714 : 0 : dev->inflight_info->fd = -1;
1715 : : }
1716 : :
1717 : 0 : num_queues = ctx->msg.payload.inflight.num_queues;
1718 : 0 : queue_size = ctx->msg.payload.inflight.queue_size;
1719 : :
1720 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1721 : : "get_inflight_fd num_queues: %u",
1722 : : ctx->msg.payload.inflight.num_queues);
1723 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1724 : : "get_inflight_fd queue_size: %u",
1725 : : ctx->msg.payload.inflight.queue_size);
1726 : :
1727 [ # # ]: 0 : if (vq_is_packed(dev))
1728 : 0 : pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1729 : : else
1730 : 0 : pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1731 : :
1732 : 0 : mmap_size = num_queues * pervq_inflight_size;
1733 : 0 : addr = inflight_mem_alloc(dev, "vhost-inflight", mmap_size, &fd);
1734 [ # # ]: 0 : if (!addr) {
1735 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc vhost inflight area");
1736 : 0 : ctx->msg.payload.inflight.mmap_size = 0;
1737 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
1738 : : }
1739 : : memset(addr, 0, mmap_size);
1740 : :
1741 [ # # ]: 0 : if (dev->inflight_info->addr) {
1742 : 0 : munmap(dev->inflight_info->addr, dev->inflight_info->size);
1743 : 0 : dev->inflight_info->addr = NULL;
1744 : : }
1745 : :
1746 [ # # ]: 0 : if (dev->inflight_info->fd >= 0) {
1747 : 0 : close(dev->inflight_info->fd);
1748 : 0 : dev->inflight_info->fd = -1;
1749 : : }
1750 : :
1751 : 0 : dev->inflight_info->addr = addr;
1752 : 0 : dev->inflight_info->size = ctx->msg.payload.inflight.mmap_size = mmap_size;
1753 : 0 : dev->inflight_info->fd = ctx->fds[0] = fd;
1754 : 0 : ctx->msg.payload.inflight.mmap_offset = 0;
1755 [ # # ]: 0 : ctx->fd_num = 1;
1756 : :
1757 [ # # ]: 0 : if (vq_is_packed(dev)) {
1758 [ # # ]: 0 : for (i = 0; i < num_queues; i++) {
1759 : : inflight_packed =
1760 : : (struct rte_vhost_inflight_info_packed *)addr;
1761 : 0 : inflight_packed->used_wrap_counter = 1;
1762 : 0 : inflight_packed->old_used_wrap_counter = 1;
1763 [ # # ]: 0 : for (j = 0; j < queue_size; j++)
1764 : 0 : inflight_packed->desc[j].next = j + 1;
1765 : 0 : addr = (void *)((char *)addr + pervq_inflight_size);
1766 : : }
1767 : : }
1768 : :
1769 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1770 : : "send inflight mmap_size: %"PRIu64,
1771 : : ctx->msg.payload.inflight.mmap_size);
1772 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1773 : : "send inflight mmap_offset: %"PRIu64,
1774 : : ctx->msg.payload.inflight.mmap_offset);
1775 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1776 : : "send inflight fd: %d", ctx->fds[0]);
1777 : :
1778 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
1779 : : }
1780 : :
1781 : : static int
1782 : 0 : vhost_user_set_inflight_fd(struct virtio_net **pdev,
1783 : : struct vhu_msg_context *ctx,
1784 : : int main_fd __rte_unused)
1785 : : {
1786 : : uint64_t mmap_size, mmap_offset;
1787 : : uint16_t num_queues, queue_size;
1788 : 0 : struct virtio_net *dev = *pdev;
1789 : : uint32_t pervq_inflight_size;
1790 : : struct vhost_virtqueue *vq;
1791 : : void *addr;
1792 : : int fd, i;
1793 : : int numa_node = SOCKET_ID_ANY;
1794 : :
1795 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, 1) != 0)
1796 : : return RTE_VHOST_MSG_RESULT_ERR;
1797 : :
1798 : 0 : fd = ctx->fds[0];
1799 [ # # # # ]: 0 : if (ctx->msg.size != sizeof(ctx->msg.payload.inflight) || fd < 0) {
1800 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1801 : : "invalid set_inflight_fd message size is %d,fd is %d",
1802 : : ctx->msg.size, fd);
1803 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
1804 : : }
1805 : :
1806 : 0 : mmap_size = ctx->msg.payload.inflight.mmap_size;
1807 : 0 : mmap_offset = ctx->msg.payload.inflight.mmap_offset;
1808 : 0 : num_queues = ctx->msg.payload.inflight.num_queues;
1809 [ # # ]: 0 : queue_size = ctx->msg.payload.inflight.queue_size;
1810 : :
1811 [ # # ]: 0 : if (vq_is_packed(dev))
1812 : : pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1813 : : else
1814 : : pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1815 : :
1816 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "set_inflight_fd mmap_size: %"PRIu64, mmap_size);
1817 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1818 : : "set_inflight_fd mmap_offset: %"PRIu64,
1819 : : mmap_offset);
1820 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1821 : : "set_inflight_fd num_queues: %u",
1822 : : num_queues);
1823 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1824 : : "set_inflight_fd queue_size: %u",
1825 : : queue_size);
1826 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1827 : : "set_inflight_fd fd: %d",
1828 : : fd);
1829 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1830 : : "set_inflight_fd pervq_inflight_size: %d",
1831 : : pervq_inflight_size);
1832 : :
1833 : : /*
1834 : : * If VQ 0 has already been allocated, try to allocate on the same
1835 : : * NUMA node. It can be reallocated later in numa_realloc().
1836 : : */
1837 [ # # ]: 0 : if (dev->nr_vring > 0)
1838 : 0 : numa_node = dev->virtqueue[0]->numa_node;
1839 : :
1840 [ # # ]: 0 : if (!dev->inflight_info) {
1841 : 0 : dev->inflight_info = rte_zmalloc_socket("inflight_info",
1842 : : sizeof(struct inflight_mem_info), 0, numa_node);
1843 [ # # ]: 0 : if (dev->inflight_info == NULL) {
1844 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc dev inflight area");
1845 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
1846 : : }
1847 : 0 : dev->inflight_info->fd = -1;
1848 : : }
1849 : :
1850 [ # # ]: 0 : if (dev->inflight_info->addr) {
1851 : 0 : munmap(dev->inflight_info->addr, dev->inflight_info->size);
1852 : 0 : dev->inflight_info->addr = NULL;
1853 : : }
1854 : :
1855 : 0 : addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
1856 : : fd, mmap_offset);
1857 [ # # ]: 0 : if (addr == MAP_FAILED) {
1858 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap share memory.");
1859 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
1860 : : }
1861 : :
1862 [ # # ]: 0 : if (dev->inflight_info->fd >= 0) {
1863 : 0 : close(dev->inflight_info->fd);
1864 : 0 : dev->inflight_info->fd = -1;
1865 : : }
1866 : :
1867 : 0 : mem_set_dump(dev, addr, mmap_size, false, get_blk_size(fd));
1868 : 0 : dev->inflight_info->fd = fd;
1869 : 0 : dev->inflight_info->addr = addr;
1870 : 0 : dev->inflight_info->size = mmap_size;
1871 : :
1872 [ # # ]: 0 : for (i = 0; i < num_queues; i++) {
1873 : 0 : vq = dev->virtqueue[i];
1874 [ # # ]: 0 : if (!vq)
1875 : 0 : continue;
1876 : :
1877 : 0 : cleanup_vq_inflight(dev, vq);
1878 [ # # ]: 0 : if (vq_is_packed(dev)) {
1879 : 0 : vq->inflight_packed = addr;
1880 : 0 : vq->inflight_packed->desc_num = queue_size;
1881 : : } else {
1882 : 0 : vq->inflight_split = addr;
1883 : 0 : vq->inflight_split->desc_num = queue_size;
1884 : : }
1885 : 0 : addr = (void *)((char *)addr + pervq_inflight_size);
1886 : : }
1887 : :
1888 : : return RTE_VHOST_MSG_RESULT_OK;
1889 : : }
1890 : :
1891 : : static int
1892 : 0 : vhost_user_set_vring_call(struct virtio_net **pdev,
1893 : : struct vhu_msg_context *ctx,
1894 : : int main_fd __rte_unused)
1895 : : {
1896 : 0 : struct virtio_net *dev = *pdev;
1897 : : struct vhost_vring_file file;
1898 : : struct vhost_virtqueue *vq;
1899 : : int expected_fds;
1900 : :
1901 : 0 : expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1902 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, expected_fds) != 0)
1903 : : return RTE_VHOST_MSG_RESULT_ERR;
1904 : :
1905 : 0 : file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
1906 [ # # ]: 0 : if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1907 : : file.fd = VIRTIO_INVALID_EVENTFD;
1908 : : else
1909 : 0 : file.fd = ctx->fds[0];
1910 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
1911 : : "vring call idx:%d file:%d",
1912 : : file.index, file.fd);
1913 : :
1914 : 0 : vq = dev->virtqueue[file.index];
1915 : :
1916 [ # # ]: 0 : if (vq->ready) {
1917 : 0 : vq->ready = false;
1918 : 0 : vhost_user_notify_queue_state(dev, vq, 0);
1919 : : }
1920 : :
1921 [ # # ]: 0 : if (vq->callfd >= 0)
1922 : 0 : close(vq->callfd);
1923 : :
1924 : 0 : vq->callfd = file.fd;
1925 : :
1926 : 0 : return RTE_VHOST_MSG_RESULT_OK;
1927 : : }
1928 : :
1929 : 0 : static int vhost_user_set_vring_err(struct virtio_net **pdev,
1930 : : struct vhu_msg_context *ctx,
1931 : : int main_fd __rte_unused)
1932 : : {
1933 : 0 : struct virtio_net *dev = *pdev;
1934 : : int expected_fds;
1935 : :
1936 : 0 : expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1937 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, expected_fds) != 0)
1938 : : return RTE_VHOST_MSG_RESULT_ERR;
1939 : :
1940 [ # # ]: 0 : if (!(ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1941 : 0 : close(ctx->fds[0]);
1942 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "not implemented");
1943 : :
1944 : 0 : return RTE_VHOST_MSG_RESULT_OK;
1945 : : }
1946 : :
1947 : : static int
1948 : 0 : resubmit_desc_compare(const void *a, const void *b)
1949 : : {
1950 : : const struct rte_vhost_resubmit_desc *desc0 = a;
1951 : : const struct rte_vhost_resubmit_desc *desc1 = b;
1952 : :
1953 [ # # ]: 0 : if (desc1->counter > desc0->counter)
1954 : 0 : return 1;
1955 : :
1956 : : return -1;
1957 : : }
1958 : :
1959 : : static int
1960 : 0 : vhost_check_queue_inflights_split(struct virtio_net *dev,
1961 : : struct vhost_virtqueue *vq)
1962 : : {
1963 : : uint16_t i;
1964 : : uint16_t resubmit_num = 0, last_io, num;
1965 : 0 : struct vring_used *used = vq->used;
1966 : : struct rte_vhost_resubmit_info *resubmit;
1967 : : struct rte_vhost_inflight_info_split *inflight_split;
1968 : :
1969 [ # # ]: 0 : if (!(dev->protocol_features &
1970 : : (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1971 : : return RTE_VHOST_MSG_RESULT_OK;
1972 : :
1973 : : /* The frontend may still not support the inflight feature
1974 : : * although we negotiate the protocol feature.
1975 : : */
1976 [ # # ]: 0 : if ((!vq->inflight_split))
1977 : : return RTE_VHOST_MSG_RESULT_OK;
1978 : :
1979 [ # # ]: 0 : if (!vq->inflight_split->version) {
1980 : 0 : vq->inflight_split->version = INFLIGHT_VERSION;
1981 : 0 : return RTE_VHOST_MSG_RESULT_OK;
1982 : : }
1983 : :
1984 [ # # ]: 0 : if (vq->resubmit_inflight)
1985 : : return RTE_VHOST_MSG_RESULT_OK;
1986 : :
1987 : : inflight_split = vq->inflight_split;
1988 : 0 : vq->global_counter = 0;
1989 : 0 : last_io = inflight_split->last_inflight_io;
1990 : :
1991 [ # # ]: 0 : if (inflight_split->used_idx != used->idx) {
1992 : 0 : inflight_split->desc[last_io].inflight = 0;
1993 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
1994 : 0 : inflight_split->used_idx = used->idx;
1995 : : }
1996 : :
1997 [ # # ]: 0 : for (i = 0; i < inflight_split->desc_num; i++) {
1998 [ # # ]: 0 : if (inflight_split->desc[i].inflight == 1)
1999 : 0 : resubmit_num++;
2000 : : }
2001 : :
2002 [ # # ]: 0 : vq->last_avail_idx += resubmit_num;
2003 : : vhost_virtqueue_reconnect_log_split(vq);
2004 : :
2005 [ # # ]: 0 : if (resubmit_num) {
2006 : 0 : resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
2007 : : 0, vq->numa_node);
2008 [ # # ]: 0 : if (!resubmit) {
2009 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2010 : : "failed to allocate memory for resubmit info.");
2011 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2012 : : }
2013 : :
2014 : 0 : resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
2015 : : resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
2016 : : 0, vq->numa_node);
2017 [ # # ]: 0 : if (!resubmit->resubmit_list) {
2018 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2019 : : "failed to allocate memory for inflight desc.");
2020 : 0 : rte_free(resubmit);
2021 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2022 : : }
2023 : :
2024 : : num = 0;
2025 [ # # ]: 0 : for (i = 0; i < vq->inflight_split->desc_num; i++) {
2026 [ # # ]: 0 : if (vq->inflight_split->desc[i].inflight == 1) {
2027 : 0 : resubmit->resubmit_list[num].index = i;
2028 : 0 : resubmit->resubmit_list[num].counter =
2029 : 0 : inflight_split->desc[i].counter;
2030 : 0 : num++;
2031 : : }
2032 : : }
2033 : 0 : resubmit->resubmit_num = num;
2034 : :
2035 [ # # ]: 0 : if (resubmit->resubmit_num > 1)
2036 : 0 : qsort(resubmit->resubmit_list, resubmit->resubmit_num,
2037 : : sizeof(struct rte_vhost_resubmit_desc),
2038 : : resubmit_desc_compare);
2039 : :
2040 : 0 : vq->global_counter = resubmit->resubmit_list[0].counter + 1;
2041 : 0 : vq->resubmit_inflight = resubmit;
2042 : : }
2043 : :
2044 : : return RTE_VHOST_MSG_RESULT_OK;
2045 : : }
2046 : :
2047 : : static int
2048 : 0 : vhost_check_queue_inflights_packed(struct virtio_net *dev,
2049 : : struct vhost_virtqueue *vq)
2050 : : {
2051 : : uint16_t i;
2052 : : uint16_t resubmit_num = 0, old_used_idx, num;
2053 : : struct rte_vhost_resubmit_info *resubmit;
2054 : : struct rte_vhost_inflight_info_packed *inflight_packed;
2055 : :
2056 [ # # ]: 0 : if (!(dev->protocol_features &
2057 : : (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
2058 : : return RTE_VHOST_MSG_RESULT_OK;
2059 : :
2060 : : /* The frontend may still not support the inflight feature
2061 : : * although we negotiate the protocol feature.
2062 : : */
2063 [ # # ]: 0 : if ((!vq->inflight_packed))
2064 : : return RTE_VHOST_MSG_RESULT_OK;
2065 : :
2066 [ # # ]: 0 : if (!vq->inflight_packed->version) {
2067 : 0 : vq->inflight_packed->version = INFLIGHT_VERSION;
2068 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2069 : : }
2070 : :
2071 [ # # ]: 0 : if (vq->resubmit_inflight)
2072 : : return RTE_VHOST_MSG_RESULT_OK;
2073 : :
2074 : : inflight_packed = vq->inflight_packed;
2075 : 0 : vq->global_counter = 0;
2076 : 0 : old_used_idx = inflight_packed->old_used_idx;
2077 : :
2078 [ # # ]: 0 : if (inflight_packed->used_idx != old_used_idx) {
2079 [ # # ]: 0 : if (inflight_packed->desc[old_used_idx].inflight == 0) {
2080 : 0 : inflight_packed->old_used_idx =
2081 : : inflight_packed->used_idx;
2082 : 0 : inflight_packed->old_used_wrap_counter =
2083 : 0 : inflight_packed->used_wrap_counter;
2084 : 0 : inflight_packed->old_free_head =
2085 : 0 : inflight_packed->free_head;
2086 : : } else {
2087 : 0 : inflight_packed->used_idx =
2088 : : inflight_packed->old_used_idx;
2089 : 0 : inflight_packed->used_wrap_counter =
2090 : 0 : inflight_packed->old_used_wrap_counter;
2091 : 0 : inflight_packed->free_head =
2092 : 0 : inflight_packed->old_free_head;
2093 : : }
2094 : : }
2095 : :
2096 [ # # ]: 0 : for (i = 0; i < inflight_packed->desc_num; i++) {
2097 [ # # ]: 0 : if (inflight_packed->desc[i].inflight == 1)
2098 : 0 : resubmit_num++;
2099 : : }
2100 : :
2101 [ # # ]: 0 : if (resubmit_num) {
2102 : 0 : resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
2103 : : 0, vq->numa_node);
2104 [ # # ]: 0 : if (resubmit == NULL) {
2105 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2106 : : "failed to allocate memory for resubmit info.");
2107 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2108 : : }
2109 : :
2110 : 0 : resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
2111 : : resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
2112 : : 0, vq->numa_node);
2113 [ # # ]: 0 : if (resubmit->resubmit_list == NULL) {
2114 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2115 : : "failed to allocate memory for resubmit desc.");
2116 : 0 : rte_free(resubmit);
2117 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2118 : : }
2119 : :
2120 : : num = 0;
2121 [ # # ]: 0 : for (i = 0; i < inflight_packed->desc_num; i++) {
2122 [ # # ]: 0 : if (vq->inflight_packed->desc[i].inflight == 1) {
2123 : 0 : resubmit->resubmit_list[num].index = i;
2124 : 0 : resubmit->resubmit_list[num].counter =
2125 : 0 : inflight_packed->desc[i].counter;
2126 : 0 : num++;
2127 : : }
2128 : : }
2129 : 0 : resubmit->resubmit_num = num;
2130 : :
2131 [ # # ]: 0 : if (resubmit->resubmit_num > 1)
2132 : 0 : qsort(resubmit->resubmit_list, resubmit->resubmit_num,
2133 : : sizeof(struct rte_vhost_resubmit_desc),
2134 : : resubmit_desc_compare);
2135 : :
2136 : 0 : vq->global_counter = resubmit->resubmit_list[0].counter + 1;
2137 : 0 : vq->resubmit_inflight = resubmit;
2138 : : }
2139 : :
2140 : : return RTE_VHOST_MSG_RESULT_OK;
2141 : : }
2142 : :
2143 : : static int
2144 : 0 : vhost_user_set_vring_kick(struct virtio_net **pdev,
2145 : : struct vhu_msg_context *ctx,
2146 : : int main_fd __rte_unused)
2147 : : {
2148 : 0 : struct virtio_net *dev = *pdev;
2149 : : struct vhost_vring_file file;
2150 : : struct vhost_virtqueue *vq;
2151 : : int expected_fds;
2152 : :
2153 : 0 : expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
2154 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, expected_fds) != 0)
2155 : : return RTE_VHOST_MSG_RESULT_ERR;
2156 : :
2157 : 0 : file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
2158 [ # # ]: 0 : if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
2159 : : file.fd = VIRTIO_INVALID_EVENTFD;
2160 : : else
2161 : 0 : file.fd = ctx->fds[0];
2162 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2163 : : "vring kick idx:%d file:%d",
2164 : : file.index, file.fd);
2165 : :
2166 : : /* Interpret ring addresses only when ring is started. */
2167 : 0 : vq = dev->virtqueue[file.index];
2168 : 0 : translate_ring_addresses(&dev, &vq);
2169 : 0 : *pdev = dev;
2170 : :
2171 : : /*
2172 : : * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
2173 : : * the ring starts already enabled. Otherwise, it is enabled via
2174 : : * the SET_VRING_ENABLE message.
2175 : : */
2176 [ # # ]: 0 : if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
2177 : 0 : vq->enabled = true;
2178 : : }
2179 : :
2180 [ # # ]: 0 : if (vq->ready) {
2181 : 0 : vq->ready = false;
2182 : 0 : vhost_user_notify_queue_state(dev, vq, 0);
2183 : : }
2184 : :
2185 [ # # ]: 0 : if (vq->kickfd >= 0)
2186 : 0 : close(vq->kickfd);
2187 [ # # ]: 0 : vq->kickfd = file.fd;
2188 : :
2189 [ # # ]: 0 : if (vq_is_packed(dev)) {
2190 [ # # ]: 0 : if (vhost_check_queue_inflights_packed(dev, vq)) {
2191 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2192 : : "failed to inflights for vq: %d",
2193 : : file.index);
2194 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2195 : : }
2196 : : } else {
2197 [ # # ]: 0 : if (vhost_check_queue_inflights_split(dev, vq)) {
2198 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2199 : : "failed to inflights for vq: %d",
2200 : : file.index);
2201 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2202 : : }
2203 : : }
2204 : :
2205 : : return RTE_VHOST_MSG_RESULT_OK;
2206 : : }
2207 : :
2208 : : /*
2209 : : * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
2210 : : */
2211 : : static int
2212 : 0 : vhost_user_get_vring_base(struct virtio_net **pdev,
2213 : : struct vhu_msg_context *ctx,
2214 : : int main_fd __rte_unused)
2215 : : {
2216 : 0 : struct virtio_net *dev = *pdev;
2217 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
2218 : : uint64_t val;
2219 : :
2220 : : /* We have to stop the queue (virtio) if it is running. */
2221 : 0 : vhost_destroy_device_notify(dev);
2222 : :
2223 : 0 : dev->flags &= ~VIRTIO_DEV_READY;
2224 [ # # ]: 0 : dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
2225 : :
2226 : : /* Here we are safe to get the indexes */
2227 [ # # ]: 0 : if (vq_is_packed(dev)) {
2228 : : /*
2229 : : * Bit[0:14]: avail index
2230 : : * Bit[15]: avail wrap counter
2231 : : */
2232 : 0 : val = vq->last_avail_idx & 0x7fff;
2233 : 0 : val |= vq->avail_wrap_counter << 15;
2234 : 0 : ctx->msg.payload.state.num = val;
2235 : : } else {
2236 : 0 : ctx->msg.payload.state.num = vq->last_avail_idx;
2237 : : }
2238 : :
2239 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2240 : : "vring base idx:%d file:%d",
2241 : : ctx->msg.payload.state.index, ctx->msg.payload.state.num);
2242 : : /*
2243 : : * Based on current qemu vhost-user implementation, this message is
2244 : : * sent and only sent in vhost_vring_stop.
2245 : : * TODO: cleanup the vring, it isn't usable since here.
2246 : : */
2247 [ # # ]: 0 : if (vq->kickfd >= 0)
2248 : 0 : close(vq->kickfd);
2249 : :
2250 : 0 : vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
2251 : :
2252 [ # # ]: 0 : if (vq->callfd >= 0)
2253 : 0 : close(vq->callfd);
2254 : :
2255 : 0 : vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
2256 : :
2257 [ # # ]: 0 : vq->signalled_used_valid = false;
2258 : :
2259 [ # # ]: 0 : if (vq_is_packed(dev)) {
2260 : 0 : rte_free(vq->shadow_used_packed);
2261 : 0 : vq->shadow_used_packed = NULL;
2262 : : } else {
2263 : 0 : rte_free(vq->shadow_used_split);
2264 : 0 : vq->shadow_used_split = NULL;
2265 : : }
2266 : :
2267 : 0 : rte_free(vq->batch_copy_elems);
2268 : 0 : vq->batch_copy_elems = NULL;
2269 : :
2270 : 0 : rte_free(vq->log_cache);
2271 : 0 : vq->log_cache = NULL;
2272 : :
2273 : 0 : ctx->msg.size = sizeof(ctx->msg.payload.state);
2274 : 0 : ctx->fd_num = 0;
2275 : :
2276 : 0 : vhost_user_iotlb_flush_all(dev);
2277 : :
2278 : 0 : rte_rwlock_write_lock(&vq->access_lock);
2279 : 0 : vring_invalidate(dev, vq);
2280 : 0 : memset(&vq->ring_addrs, 0, sizeof(struct vhost_vring_addr));
2281 : : rte_rwlock_write_unlock(&vq->access_lock);
2282 : :
2283 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
2284 : : }
2285 : :
2286 : : /*
2287 : : * when virtio queues are ready to work, qemu will send us to
2288 : : * enable the virtio queue pair.
2289 : : */
2290 : : static int
2291 : 0 : vhost_user_set_vring_enable(struct virtio_net **pdev,
2292 : : struct vhu_msg_context *ctx,
2293 : : int main_fd __rte_unused)
2294 : : {
2295 : 0 : struct virtio_net *dev = *pdev;
2296 : : struct vhost_virtqueue *vq;
2297 : 0 : bool enable = !!ctx->msg.payload.state.num;
2298 : 0 : int index = (int)ctx->msg.payload.state.index;
2299 : :
2300 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2301 : : "set queue enable: %d to qp idx: %d",
2302 : : enable, index);
2303 : :
2304 : 0 : vq = dev->virtqueue[index];
2305 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
2306 : : /* vhost_user_lock_all_queue_pairs locked all qps */
2307 : 0 : VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_VRING_ENABLE);
2308 [ # # # # : 0 : if (enable && vq->async && vq->async->pkts_inflight_n) {
# # ]
2309 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2310 : : "failed to enable vring. Inflight packets must be completed first");
2311 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2312 : : }
2313 : : }
2314 : :
2315 : 0 : vq->enabled = enable;
2316 : :
2317 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2318 : : }
2319 : :
2320 : : static int
2321 : 0 : vhost_user_get_protocol_features(struct virtio_net **pdev,
2322 : : struct vhu_msg_context *ctx,
2323 : : int main_fd __rte_unused)
2324 : : {
2325 : 0 : struct virtio_net *dev = *pdev;
2326 : : uint64_t protocol_features;
2327 : :
2328 : 0 : rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
2329 : :
2330 : 0 : ctx->msg.payload.u64 = protocol_features;
2331 : 0 : ctx->msg.size = sizeof(ctx->msg.payload.u64);
2332 : 0 : ctx->fd_num = 0;
2333 : :
2334 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
2335 : : }
2336 : :
2337 : : static int
2338 : 0 : vhost_user_set_protocol_features(struct virtio_net **pdev,
2339 : : struct vhu_msg_context *ctx,
2340 : : int main_fd __rte_unused)
2341 : : {
2342 : 0 : struct virtio_net *dev = *pdev;
2343 : 0 : uint64_t protocol_features = ctx->msg.payload.u64;
2344 : 0 : uint64_t backend_protocol_features = 0;
2345 : :
2346 : 0 : rte_vhost_driver_get_protocol_features(dev->ifname,
2347 : : &backend_protocol_features);
2348 [ # # ]: 0 : if (protocol_features & ~backend_protocol_features) {
2349 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "received invalid protocol features.");
2350 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2351 : : }
2352 : :
2353 : 0 : dev->protocol_features = protocol_features;
2354 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2355 : : "negotiated Vhost-user protocol features: 0x%" PRIx64,
2356 : : dev->protocol_features);
2357 : :
2358 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2359 : : }
2360 : :
2361 : : static int
2362 : 0 : vhost_user_set_log_base(struct virtio_net **pdev,
2363 : : struct vhu_msg_context *ctx,
2364 : : int main_fd __rte_unused)
2365 : : {
2366 : 0 : struct virtio_net *dev = *pdev;
2367 : 0 : int fd = ctx->fds[0];
2368 : : uint64_t size, off;
2369 : : uint64_t alignment;
2370 : : void *addr;
2371 : : uint32_t i;
2372 : :
2373 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, 1) != 0)
2374 : : return RTE_VHOST_MSG_RESULT_ERR;
2375 : :
2376 [ # # ]: 0 : if (fd < 0) {
2377 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid log fd: %d", fd);
2378 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2379 : : }
2380 : :
2381 [ # # ]: 0 : if (ctx->msg.size != sizeof(VhostUserLog)) {
2382 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2383 : : "invalid log base msg size: %"PRId32" != %d",
2384 : : ctx->msg.size, (int)sizeof(VhostUserLog));
2385 : 0 : goto close_msg_fds;
2386 : : }
2387 : :
2388 : 0 : size = ctx->msg.payload.log.mmap_size;
2389 : 0 : off = ctx->msg.payload.log.mmap_offset;
2390 : :
2391 : : /* Check for mmap size and offset overflow. */
2392 [ # # ]: 0 : if (off >= -size) {
2393 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2394 : : "log offset %#"PRIx64" and log size %#"PRIx64" overflow",
2395 : : off, size);
2396 : 0 : goto close_msg_fds;
2397 : : }
2398 : :
2399 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2400 : : "log mmap size: %"PRId64", offset: %"PRId64,
2401 : : size, off);
2402 : :
2403 : : /*
2404 : : * mmap from 0 to workaround a hugepage mmap bug: mmap will
2405 : : * fail when offset is not page size aligned.
2406 : : */
2407 : 0 : addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, off);
2408 : : alignment = get_blk_size(fd);
2409 : 0 : close(fd);
2410 [ # # ]: 0 : if (addr == MAP_FAILED) {
2411 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "mmap log base failed!");
2412 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2413 : : }
2414 : :
2415 : : /*
2416 : : * Free previously mapped log memory on occasionally
2417 : : * multiple VHOST_USER_SET_LOG_BASE.
2418 : : */
2419 [ # # ]: 0 : if (dev->log_addr) {
2420 : 0 : munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
2421 : : }
2422 : 0 : dev->log_addr = (uint64_t)(uintptr_t)addr;
2423 : 0 : dev->log_base = dev->log_addr + off;
2424 : 0 : dev->log_size = size;
2425 : 0 : mem_set_dump(dev, addr, size + off, false, alignment);
2426 : :
2427 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
2428 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
2429 : :
2430 : 0 : rte_free(vq->log_cache);
2431 : 0 : vq->log_cache = NULL;
2432 : 0 : vq->log_cache_nb_elem = 0;
2433 : 0 : vq->log_cache = rte_malloc_socket("vq log cache",
2434 : : sizeof(struct log_cache_entry) * VHOST_LOG_CACHE_NR,
2435 : : 0, vq->numa_node);
2436 : : /*
2437 : : * If log cache alloc fail, don't fail migration, but no
2438 : : * caching will be done, which will impact performance
2439 : : */
2440 [ # # ]: 0 : if (!vq->log_cache)
2441 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2442 : : "failed to allocate VQ logging cache");
2443 : : }
2444 : :
2445 : : /*
2446 : : * The spec is not clear about it (yet), but QEMU doesn't expect
2447 : : * any payload in the reply.
2448 : : */
2449 : 0 : ctx->msg.size = 0;
2450 : 0 : ctx->fd_num = 0;
2451 : :
2452 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
2453 : :
2454 : 0 : close_msg_fds:
2455 : 0 : close_msg_fds(ctx);
2456 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2457 : : }
2458 : :
2459 : 0 : static int vhost_user_set_log_fd(struct virtio_net **pdev,
2460 : : struct vhu_msg_context *ctx,
2461 : : int main_fd __rte_unused)
2462 : : {
2463 : 0 : struct virtio_net *dev = *pdev;
2464 : :
2465 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, 1) != 0)
2466 : : return RTE_VHOST_MSG_RESULT_ERR;
2467 : :
2468 : 0 : close(ctx->fds[0]);
2469 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "not implemented.");
2470 : :
2471 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2472 : : }
2473 : :
2474 : : /*
2475 : : * An rarp packet is constructed and broadcasted to notify switches about
2476 : : * the new location of the migrated VM, so that packets from outside will
2477 : : * not be lost after migration.
2478 : : *
2479 : : * However, we don't actually "send" a rarp packet here, instead, we set
2480 : : * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
2481 : : */
2482 : : static int
2483 : 0 : vhost_user_send_rarp(struct virtio_net **pdev,
2484 : : struct vhu_msg_context *ctx,
2485 : : int main_fd __rte_unused)
2486 : : {
2487 : 0 : struct virtio_net *dev = *pdev;
2488 : 0 : uint8_t *mac = (uint8_t *)&ctx->msg.payload.u64;
2489 : : struct rte_vdpa_device *vdpa_dev;
2490 : :
2491 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG,
2492 : : "MAC: " RTE_ETHER_ADDR_PRT_FMT,
2493 : : mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
2494 [ # # ]: 0 : memcpy(dev->mac.addr_bytes, mac, 6);
2495 : :
2496 : : /*
2497 : : * Set the flag to inject a RARP broadcast packet at
2498 : : * rte_vhost_dequeue_burst().
2499 : : *
2500 : : * rte_memory_order_release ordering is for making sure the mac is
2501 : : * copied before the flag is set.
2502 : : */
2503 : 0 : rte_atomic_store_explicit(&dev->broadcast_rarp, 1, rte_memory_order_release);
2504 : 0 : vdpa_dev = dev->vdpa_dev;
2505 [ # # # # ]: 0 : if (vdpa_dev && vdpa_dev->ops->migration_done)
2506 : 0 : vdpa_dev->ops->migration_done(dev->vid);
2507 : :
2508 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2509 : : }
2510 : :
2511 : : static int
2512 : 0 : vhost_user_net_set_mtu(struct virtio_net **pdev,
2513 : : struct vhu_msg_context *ctx,
2514 : : int main_fd __rte_unused)
2515 : : {
2516 : 0 : struct virtio_net *dev = *pdev;
2517 : :
2518 [ # # ]: 0 : if (ctx->msg.payload.u64 < VIRTIO_MIN_MTU ||
2519 : : ctx->msg.payload.u64 > VIRTIO_MAX_MTU) {
2520 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2521 : : "invalid MTU size (%"PRIu64")",
2522 : : ctx->msg.payload.u64);
2523 : :
2524 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2525 : : }
2526 : :
2527 : 0 : dev->mtu = ctx->msg.payload.u64;
2528 : :
2529 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2530 : : }
2531 : :
2532 : : static int
2533 : 0 : vhost_user_set_req_fd(struct virtio_net **pdev,
2534 : : struct vhu_msg_context *ctx,
2535 : : int main_fd __rte_unused)
2536 : : {
2537 : 0 : struct virtio_net *dev = *pdev;
2538 : 0 : int fd = ctx->fds[0];
2539 : :
2540 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, 1) != 0)
2541 : : return RTE_VHOST_MSG_RESULT_ERR;
2542 : :
2543 [ # # ]: 0 : if (fd < 0) {
2544 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2545 : : "invalid file descriptor for backend channel (%d)", fd);
2546 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2547 : : }
2548 : :
2549 [ # # ]: 0 : if (dev->backend_req_fd >= 0)
2550 : 0 : close(dev->backend_req_fd);
2551 : :
2552 : 0 : dev->backend_req_fd = fd;
2553 : :
2554 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2555 : : }
2556 : :
2557 : : static int
2558 : 0 : is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2559 : : {
2560 : : struct vhost_vring_addr *ra;
2561 : : uint64_t start, end, len;
2562 : :
2563 : 0 : start = imsg->iova;
2564 : 0 : end = start + imsg->size;
2565 : :
2566 : : ra = &vq->ring_addrs;
2567 : 0 : len = sizeof(struct vring_desc) * vq->size;
2568 [ # # # # ]: 0 : if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2569 : : return 1;
2570 : :
2571 : 0 : len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
2572 [ # # # # ]: 0 : if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2573 : : return 1;
2574 : :
2575 : 0 : len = sizeof(struct vring_used) +
2576 : 0 : sizeof(struct vring_used_elem) * vq->size;
2577 [ # # # # ]: 0 : if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2578 : : return 1;
2579 : :
2580 [ # # ]: 0 : if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2581 : : len = sizeof(uint64_t);
2582 [ # # ]: 0 : if (ra->log_guest_addr < end &&
2583 [ # # ]: 0 : (ra->log_guest_addr + len) > start)
2584 : 0 : return 1;
2585 : : }
2586 : :
2587 : : return 0;
2588 : : }
2589 : :
2590 : : static int
2591 : 0 : is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2592 : : {
2593 : : struct vhost_vring_addr *ra;
2594 : : uint64_t start, end, len;
2595 : :
2596 : 0 : start = imsg->iova;
2597 : 0 : end = start + imsg->size;
2598 : :
2599 : : ra = &vq->ring_addrs;
2600 : 0 : len = sizeof(struct vring_packed_desc) * vq->size;
2601 [ # # # # ]: 0 : if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2602 : : return 1;
2603 : :
2604 : : len = sizeof(struct vring_packed_desc_event);
2605 [ # # # # ]: 0 : if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2606 : : return 1;
2607 : :
2608 : : len = sizeof(struct vring_packed_desc_event);
2609 [ # # # # ]: 0 : if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2610 : : return 1;
2611 : :
2612 [ # # ]: 0 : if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2613 : : len = sizeof(uint64_t);
2614 [ # # ]: 0 : if (ra->log_guest_addr < end &&
2615 [ # # ]: 0 : (ra->log_guest_addr + len) > start)
2616 : 0 : return 1;
2617 : : }
2618 : :
2619 : : return 0;
2620 : : }
2621 : :
2622 [ # # ]: 0 : static int is_vring_iotlb(struct virtio_net *dev,
2623 : : struct vhost_virtqueue *vq,
2624 : : struct vhost_iotlb_msg *imsg)
2625 : : {
2626 [ # # ]: 0 : if (vq_is_packed(dev))
2627 : 0 : return is_vring_iotlb_packed(vq, imsg);
2628 : : else
2629 : 0 : return is_vring_iotlb_split(vq, imsg);
2630 : : }
2631 : :
2632 : : static int
2633 : 0 : vhost_user_get_config(struct virtio_net **pdev,
2634 : : struct vhu_msg_context *ctx,
2635 : : int main_fd __rte_unused)
2636 : : {
2637 : 0 : struct virtio_net *dev = *pdev;
2638 : 0 : struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
2639 : : int ret = 0;
2640 : :
2641 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, 0) != 0)
2642 : : return RTE_VHOST_MSG_RESULT_ERR;
2643 : :
2644 [ # # ]: 0 : if (!vdpa_dev) {
2645 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "is not vDPA device!");
2646 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2647 : : }
2648 : :
2649 [ # # ]: 0 : if (vdpa_dev->ops->get_config) {
2650 : 0 : ret = vdpa_dev->ops->get_config(dev->vid,
2651 : 0 : ctx->msg.payload.cfg.region,
2652 : : ctx->msg.payload.cfg.size);
2653 [ # # ]: 0 : if (ret != 0) {
2654 : 0 : ctx->msg.size = 0;
2655 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "get_config() return error!");
2656 : : }
2657 : : } else {
2658 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "get_config() not supported!");
2659 : : }
2660 : :
2661 : : return RTE_VHOST_MSG_RESULT_REPLY;
2662 : : }
2663 : :
2664 : : static int
2665 : 0 : vhost_user_set_config(struct virtio_net **pdev,
2666 : : struct vhu_msg_context *ctx,
2667 : : int main_fd __rte_unused)
2668 : : {
2669 : 0 : struct virtio_net *dev = *pdev;
2670 : 0 : struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
2671 : : int ret = 0;
2672 : :
2673 [ # # ]: 0 : if (validate_msg_fds(dev, ctx, 0) != 0)
2674 : : return RTE_VHOST_MSG_RESULT_ERR;
2675 : :
2676 [ # # ]: 0 : if (ctx->msg.payload.cfg.size > VHOST_USER_MAX_CONFIG_SIZE) {
2677 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2678 : : "vhost_user_config size: %"PRIu32", should not be larger than %d",
2679 : : ctx->msg.payload.cfg.size, VHOST_USER_MAX_CONFIG_SIZE);
2680 : 0 : goto out;
2681 : : }
2682 : :
2683 [ # # ]: 0 : if (!vdpa_dev) {
2684 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "is not vDPA device!");
2685 : 0 : goto out;
2686 : : }
2687 : :
2688 [ # # ]: 0 : if (vdpa_dev->ops->set_config) {
2689 : 0 : ret = vdpa_dev->ops->set_config(dev->vid,
2690 : 0 : ctx->msg.payload.cfg.region,
2691 : : ctx->msg.payload.cfg.offset,
2692 : : ctx->msg.payload.cfg.size,
2693 : : ctx->msg.payload.cfg.flags);
2694 [ # # ]: 0 : if (ret)
2695 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "set_config() return error!");
2696 : : } else {
2697 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "set_config() not supported!");
2698 : : }
2699 : :
2700 : : return RTE_VHOST_MSG_RESULT_OK;
2701 : :
2702 : : out:
2703 : : return RTE_VHOST_MSG_RESULT_ERR;
2704 : : }
2705 : :
2706 : : static int
2707 : 0 : vhost_user_iotlb_msg(struct virtio_net **pdev,
2708 : : struct vhu_msg_context *ctx,
2709 : : int main_fd __rte_unused)
2710 : : {
2711 : 0 : struct virtio_net *dev = *pdev;
2712 : 0 : struct vhost_iotlb_msg *imsg = &ctx->msg.payload.iotlb;
2713 : : uint16_t i;
2714 : : uint64_t vva, len, pg_sz;
2715 : :
2716 [ # # # ]: 0 : switch (imsg->type) {
2717 : 0 : case VHOST_IOTLB_UPDATE:
2718 : 0 : len = imsg->size;
2719 : 0 : vva = qva_to_vva(dev, imsg->uaddr, &len);
2720 [ # # ]: 0 : if (!vva)
2721 : : return RTE_VHOST_MSG_RESULT_ERR;
2722 : :
2723 : 0 : pg_sz = hua_to_alignment(dev->mem, (void *)(uintptr_t)vva);
2724 : :
2725 : 0 : vhost_user_iotlb_cache_insert(dev, imsg->iova, vva, 0, len, pg_sz, imsg->perm);
2726 : :
2727 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
2728 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
2729 : :
2730 [ # # ]: 0 : if (!vq)
2731 : 0 : continue;
2732 : :
2733 [ # # ]: 0 : if (is_vring_iotlb(dev, vq, imsg)) {
2734 : 0 : rte_rwlock_write_lock(&vq->access_lock);
2735 : 0 : translate_ring_addresses(&dev, &vq);
2736 : 0 : *pdev = dev;
2737 : 0 : rte_rwlock_write_unlock(&vq->access_lock);
2738 : : }
2739 : : }
2740 : : break;
2741 : 0 : case VHOST_IOTLB_INVALIDATE:
2742 : 0 : vhost_user_iotlb_cache_remove(dev, imsg->iova, imsg->size);
2743 : :
2744 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
2745 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
2746 : :
2747 [ # # ]: 0 : if (!vq)
2748 : 0 : continue;
2749 : :
2750 [ # # ]: 0 : if (is_vring_iotlb(dev, vq, imsg)) {
2751 : 0 : rte_rwlock_write_lock(&vq->access_lock);
2752 : 0 : vring_invalidate(dev, vq);
2753 : : rte_rwlock_write_unlock(&vq->access_lock);
2754 : : }
2755 : : }
2756 : : break;
2757 : 0 : default:
2758 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid IOTLB message type (%d)",
2759 : : imsg->type);
2760 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2761 : : }
2762 : :
2763 : : return RTE_VHOST_MSG_RESULT_OK;
2764 : : }
2765 : :
2766 : : static int
2767 : 0 : vhost_user_set_postcopy_advise(struct virtio_net **pdev,
2768 : : struct vhu_msg_context *ctx,
2769 : : int main_fd __rte_unused)
2770 : : {
2771 : 0 : struct virtio_net *dev = *pdev;
2772 : : #ifdef RTE_LIBRTE_VHOST_POSTCOPY
2773 : : struct uffdio_api api_struct;
2774 : :
2775 : 0 : dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
2776 : :
2777 [ # # ]: 0 : if (dev->postcopy_ufd == -1) {
2778 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2779 : : "userfaultfd not available: %s",
2780 : : strerror(errno));
2781 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2782 : : }
2783 : 0 : api_struct.api = UFFD_API;
2784 : 0 : api_struct.features = 0;
2785 [ # # ]: 0 : if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
2786 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2787 : : "UFFDIO_API ioctl failure: %s",
2788 : : strerror(errno));
2789 : 0 : close(dev->postcopy_ufd);
2790 : 0 : dev->postcopy_ufd = -1;
2791 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2792 : : }
2793 : 0 : ctx->fds[0] = dev->postcopy_ufd;
2794 : 0 : ctx->fd_num = 1;
2795 : :
2796 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
2797 : : #else
2798 : : dev->postcopy_ufd = -1;
2799 : : ctx->fd_num = 0;
2800 : :
2801 : : return RTE_VHOST_MSG_RESULT_ERR;
2802 : : #endif
2803 : : }
2804 : :
2805 : : static int
2806 : 0 : vhost_user_set_postcopy_listen(struct virtio_net **pdev,
2807 : : struct vhu_msg_context *ctx __rte_unused,
2808 : : int main_fd __rte_unused)
2809 : : {
2810 : 0 : struct virtio_net *dev = *pdev;
2811 : :
2812 [ # # # # ]: 0 : if (dev->mem && dev->mem->nregions) {
2813 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2814 : : "regions already registered at postcopy-listen");
2815 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2816 : : }
2817 : 0 : dev->postcopy_listening = 1;
2818 : :
2819 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2820 : : }
2821 : :
2822 : : static int
2823 : 0 : vhost_user_postcopy_end(struct virtio_net **pdev,
2824 : : struct vhu_msg_context *ctx,
2825 : : int main_fd __rte_unused)
2826 : : {
2827 : 0 : struct virtio_net *dev = *pdev;
2828 : :
2829 : 0 : dev->postcopy_listening = 0;
2830 [ # # ]: 0 : if (dev->postcopy_ufd >= 0) {
2831 : 0 : close(dev->postcopy_ufd);
2832 : 0 : dev->postcopy_ufd = -1;
2833 : : }
2834 : :
2835 : 0 : ctx->msg.payload.u64 = 0;
2836 : 0 : ctx->msg.size = sizeof(ctx->msg.payload.u64);
2837 : 0 : ctx->fd_num = 0;
2838 : :
2839 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
2840 : : }
2841 : :
2842 : : static int
2843 : 0 : vhost_user_get_status(struct virtio_net **pdev,
2844 : : struct vhu_msg_context *ctx,
2845 : : int main_fd __rte_unused)
2846 : : {
2847 : 0 : struct virtio_net *dev = *pdev;
2848 : :
2849 : 0 : ctx->msg.payload.u64 = dev->status;
2850 : 0 : ctx->msg.size = sizeof(ctx->msg.payload.u64);
2851 : 0 : ctx->fd_num = 0;
2852 : :
2853 : 0 : return RTE_VHOST_MSG_RESULT_REPLY;
2854 : : }
2855 : :
2856 : : static int
2857 : 0 : vhost_user_set_status(struct virtio_net **pdev,
2858 : : struct vhu_msg_context *ctx,
2859 : : int main_fd __rte_unused)
2860 : : {
2861 : 0 : struct virtio_net *dev = *pdev;
2862 : :
2863 : : /* As per Virtio specification, the device status is 8bits long */
2864 [ # # ]: 0 : if (ctx->msg.payload.u64 > UINT8_MAX) {
2865 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2866 : : "invalid VHOST_USER_SET_STATUS payload 0x%" PRIx64,
2867 : : ctx->msg.payload.u64);
2868 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
2869 : : }
2870 : :
2871 : 0 : dev->status = ctx->msg.payload.u64;
2872 : :
2873 [ # # ]: 0 : if ((dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK) &&
2874 [ # # ]: 0 : (dev->flags & VIRTIO_DEV_FEATURES_FAILED)) {
2875 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
2876 : : "FEATURES_OK bit is set but feature negotiation failed");
2877 : : /*
2878 : : * Clear the bit to let the driver know about the feature
2879 : : * negotiation failure
2880 : : */
2881 : 0 : dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
2882 : : }
2883 : :
2884 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "new device status(0x%08x):", dev->status);
2885 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2886 : : "\t-RESET: %u",
2887 : : (dev->status == VIRTIO_DEVICE_STATUS_RESET));
2888 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2889 : : "\t-ACKNOWLEDGE: %u",
2890 : : !!(dev->status & VIRTIO_DEVICE_STATUS_ACK));
2891 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2892 : : "\t-DRIVER: %u",
2893 : : !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER));
2894 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2895 : : "\t-FEATURES_OK: %u",
2896 : : !!(dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK));
2897 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2898 : : "\t-DRIVER_OK: %u",
2899 : : !!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK));
2900 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2901 : : "\t-DEVICE_NEED_RESET: %u",
2902 : : !!(dev->status & VIRTIO_DEVICE_STATUS_DEV_NEED_RESET));
2903 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
2904 : : "\t-FAILED: %u",
2905 : : !!(dev->status & VIRTIO_DEVICE_STATUS_FAILED));
2906 : :
2907 : 0 : return RTE_VHOST_MSG_RESULT_OK;
2908 : : }
2909 : :
2910 : : #define VHOST_MESSAGE_HANDLER(id, handler, accepts_fd, lock_all_qps) \
2911 : : [id] = { #id, handler, accepts_fd, id ## _LOCK_ALL_QPS },
2912 : : static vhost_message_handler_t vhost_message_handlers[] = {
2913 : : VHOST_MESSAGE_HANDLERS
2914 : : };
2915 : : #undef VHOST_MESSAGE_HANDLER
2916 : :
2917 : : /* return bytes# of read on success or negative val on failure. */
2918 : : static int
2919 : 0 : read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2920 : : {
2921 : : int ret;
2922 : :
2923 : 0 : ret = read_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, VHOST_USER_HDR_SIZE,
2924 : 0 : ctx->fds, VHOST_MEMORY_MAX_NREGIONS, &ctx->fd_num);
2925 [ # # ]: 0 : if (ret <= 0)
2926 : 0 : goto out;
2927 : :
2928 [ # # ]: 0 : if (ret != VHOST_USER_HDR_SIZE) {
2929 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Unexpected header size read");
2930 : : ret = -1;
2931 : 0 : goto out;
2932 : : }
2933 : :
2934 [ # # ]: 0 : if (ctx->msg.size) {
2935 [ # # ]: 0 : if (ctx->msg.size > sizeof(ctx->msg.payload)) {
2936 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid msg size: %d",
2937 : : ctx->msg.size);
2938 : : ret = -1;
2939 : 0 : goto out;
2940 : : }
2941 [ # # ]: 0 : ret = read(sockfd, &ctx->msg.payload, ctx->msg.size);
2942 [ # # ]: 0 : if (ret <= 0)
2943 : 0 : goto out;
2944 [ # # ]: 0 : if (ret != (int)ctx->msg.size) {
2945 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "read control message failed");
2946 : : ret = -1;
2947 : 0 : goto out;
2948 : : }
2949 : : }
2950 : :
2951 : 0 : out:
2952 [ # # ]: 0 : if (ret <= 0)
2953 : 0 : close_msg_fds(ctx);
2954 : :
2955 : 0 : return ret;
2956 : : }
2957 : :
2958 : : static int
2959 : : send_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2960 : : {
2961 : 0 : if (!ctx)
2962 : : return 0;
2963 : :
2964 : 0 : return send_fd_message(dev->ifname, sockfd, (char *)&ctx->msg,
2965 : 0 : VHOST_USER_HDR_SIZE + ctx->msg.size, ctx->fds, ctx->fd_num);
2966 : : }
2967 : :
2968 : : static int
2969 : 0 : send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2970 : : {
2971 [ # # ]: 0 : if (!ctx)
2972 : : return 0;
2973 : :
2974 : 0 : ctx->msg.flags &= ~VHOST_USER_VERSION_MASK;
2975 : 0 : ctx->msg.flags &= ~VHOST_USER_NEED_REPLY;
2976 : 0 : ctx->msg.flags |= VHOST_USER_VERSION;
2977 : 0 : ctx->msg.flags |= VHOST_USER_REPLY_MASK;
2978 : :
2979 : 0 : return send_vhost_message(dev, sockfd, ctx);
2980 : : }
2981 : :
2982 : : static int
2983 : 0 : send_vhost_backend_message(struct virtio_net *dev, struct vhu_msg_context *ctx)
2984 : : {
2985 [ # # ]: 0 : return send_vhost_message(dev, dev->backend_req_fd, ctx);
2986 : : }
2987 : :
2988 : : static int
2989 : 0 : send_vhost_backend_message_process_reply(struct virtio_net *dev, struct vhu_msg_context *ctx)
2990 : : {
2991 : : struct vhu_msg_context msg_reply;
2992 : : int ret;
2993 : :
2994 : 0 : rte_spinlock_lock(&dev->backend_req_lock);
2995 : 0 : ret = send_vhost_backend_message(dev, ctx);
2996 [ # # ]: 0 : if (ret < 0) {
2997 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to send config change (%d)", ret);
2998 : 0 : goto out;
2999 : : }
3000 : :
3001 : 0 : ret = read_vhost_message(dev, dev->backend_req_fd, &msg_reply);
3002 [ # # ]: 0 : if (ret <= 0) {
3003 [ # # ]: 0 : if (ret < 0)
3004 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
3005 : : "vhost read backend message reply failed");
3006 : : else
3007 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "vhost peer closed");
3008 : : ret = -1;
3009 : 0 : goto out;
3010 : : }
3011 : :
3012 [ # # ]: 0 : if (msg_reply.msg.request.backend != ctx->msg.request.backend) {
3013 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
3014 : : "received unexpected msg type (%u), expected %u",
3015 : : msg_reply.msg.request.backend, ctx->msg.request.backend);
3016 : : ret = -1;
3017 : 0 : goto out;
3018 : : }
3019 : :
3020 [ # # ]: 0 : ret = msg_reply.msg.payload.u64 ? -1 : 0;
3021 : 0 : out:
3022 : : rte_spinlock_unlock(&dev->backend_req_lock);
3023 : 0 : return ret;
3024 : : }
3025 : :
3026 : : /*
3027 : : * Allocate a queue pair if it hasn't been allocated yet
3028 : : */
3029 : : static int
3030 : 0 : vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev,
3031 : : struct vhu_msg_context *ctx)
3032 : : {
3033 : : uint32_t vring_idx;
3034 : :
3035 [ # # # # : 0 : switch (ctx->msg.request.frontend) {
# ]
3036 : 0 : case VHOST_USER_SET_VRING_KICK:
3037 : : case VHOST_USER_SET_VRING_CALL:
3038 : : case VHOST_USER_SET_VRING_ERR:
3039 : 0 : vring_idx = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
3040 : 0 : break;
3041 : 0 : case VHOST_USER_SET_VRING_NUM:
3042 : : case VHOST_USER_SET_VRING_BASE:
3043 : : case VHOST_USER_GET_VRING_BASE:
3044 : : case VHOST_USER_SET_VRING_ENABLE:
3045 : 0 : vring_idx = ctx->msg.payload.state.index;
3046 : 0 : break;
3047 : 0 : case VHOST_USER_SET_VRING_ADDR:
3048 : 0 : vring_idx = ctx->msg.payload.addr.index;
3049 : 0 : break;
3050 : 0 : case VHOST_USER_SET_INFLIGHT_FD:
3051 : 0 : vring_idx = ctx->msg.payload.inflight.num_queues - 1;
3052 : 0 : break;
3053 : : default:
3054 : : return 0;
3055 : : }
3056 : :
3057 [ # # ]: 0 : if (vring_idx >= VHOST_MAX_VRING) {
3058 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid vring index: %u", vring_idx);
3059 : 0 : return -1;
3060 : : }
3061 : :
3062 [ # # ]: 0 : if (dev->virtqueue[vring_idx])
3063 : : return 0;
3064 : :
3065 : 0 : return alloc_vring_queue(dev, vring_idx);
3066 : : }
3067 : :
3068 : : static void
3069 : 0 : vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
3070 : : __rte_no_thread_safety_analysis
3071 : : {
3072 : : unsigned int i = 0;
3073 : : unsigned int vq_num = 0;
3074 : :
3075 [ # # ]: 0 : while (vq_num < dev->nr_vring) {
3076 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
3077 : :
3078 [ # # ]: 0 : if (vq) {
3079 : 0 : rte_rwlock_write_lock(&vq->access_lock);
3080 : 0 : vq_num++;
3081 : : }
3082 : 0 : i++;
3083 : : }
3084 : 0 : }
3085 : :
3086 : : static void
3087 : 0 : vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
3088 : : __rte_no_thread_safety_analysis
3089 : : {
3090 : : unsigned int i = 0;
3091 : : unsigned int vq_num = 0;
3092 : :
3093 [ # # ]: 0 : while (vq_num < dev->nr_vring) {
3094 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
3095 : :
3096 [ # # ]: 0 : if (vq) {
3097 : : rte_rwlock_write_unlock(&vq->access_lock);
3098 : 0 : vq_num++;
3099 : : }
3100 : 0 : i++;
3101 : : }
3102 : 0 : }
3103 : :
3104 : : int
3105 [ # # ]: 0 : vhost_user_msg_handler(int vid, int fd)
3106 : : {
3107 : : struct virtio_net *dev;
3108 : : struct vhu_msg_context ctx;
3109 : : vhost_message_handler_t *msg_handler;
3110 : : struct rte_vdpa_device *vdpa_dev;
3111 : : int msg_result = RTE_VHOST_MSG_RESULT_OK;
3112 : : int ret;
3113 : : int unlock_required = 0;
3114 : : bool handled;
3115 : : uint32_t request;
3116 : : uint32_t i;
3117 : : uint16_t blk_call_fd;
3118 : :
3119 : 0 : dev = get_device(vid);
3120 [ # # ]: 0 : if (dev == NULL)
3121 : : return -1;
3122 : :
3123 [ # # ]: 0 : if (!dev->notify_ops) {
3124 : 0 : dev->notify_ops = vhost_driver_callback_get(dev->ifname);
3125 [ # # ]: 0 : if (!dev->notify_ops) {
3126 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
3127 : : "failed to get callback ops for driver");
3128 : 0 : return -1;
3129 : : }
3130 : : }
3131 : :
3132 : 0 : ctx.msg.request.frontend = VHOST_USER_NONE;
3133 : 0 : ret = read_vhost_message(dev, fd, &ctx);
3134 [ # # ]: 0 : if (ret == 0) {
3135 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "vhost peer closed");
3136 : 0 : return -1;
3137 : : }
3138 : :
3139 : 0 : request = ctx.msg.request.frontend;
3140 [ # # ]: 0 : if (request > VHOST_USER_NONE && request < RTE_DIM(vhost_message_handlers))
3141 : 0 : msg_handler = &vhost_message_handlers[request];
3142 : : else
3143 : : msg_handler = NULL;
3144 : :
3145 [ # # ]: 0 : if (ret < 0) {
3146 [ # # # # : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "vhost read message %s%s%sfailed",
# # ]
3147 : : msg_handler != NULL ? "for " : "",
3148 : : msg_handler != NULL ? msg_handler->description : "",
3149 : : msg_handler != NULL ? " " : "");
3150 : 0 : return -1;
3151 : : }
3152 : :
3153 [ # # # # ]: 0 : if (msg_handler != NULL && msg_handler->description != NULL) {
3154 [ # # ]: 0 : if (request != VHOST_USER_IOTLB_MSG)
3155 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO,
3156 : : "read message %s",
3157 : : msg_handler->description);
3158 : : else
3159 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG,
3160 : : "read message %s",
3161 : : msg_handler->description);
3162 : : } else {
3163 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "external request %d", request);
3164 : : }
3165 : :
3166 : 0 : ret = vhost_user_check_and_alloc_queue_pair(dev, &ctx);
3167 [ # # ]: 0 : if (ret < 0) {
3168 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc queue");
3169 : 0 : return -1;
3170 : : }
3171 : :
3172 : : /*
3173 : : * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
3174 : : * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
3175 : : * and device is destroyed. destroy_device waits for queues to be
3176 : : * inactive, so it is safe. Otherwise taking the access_lock
3177 : : * would cause a dead lock.
3178 : : */
3179 [ # # # # ]: 0 : if (msg_handler != NULL && msg_handler->lock_all_qps) {
3180 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3181 : 0 : vhost_user_lock_all_queue_pairs(dev);
3182 : : unlock_required = 1;
3183 : : }
3184 : : }
3185 : :
3186 : : handled = false;
3187 [ # # ]: 0 : if (dev->extern_ops.pre_msg_handle) {
3188 : : RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
3189 : 0 : msg_result = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx);
3190 [ # # # ]: 0 : switch (msg_result) {
3191 : 0 : case RTE_VHOST_MSG_RESULT_REPLY:
3192 : 0 : send_vhost_reply(dev, fd, &ctx);
3193 : : /* Fall-through */
3194 : 0 : case RTE_VHOST_MSG_RESULT_ERR:
3195 : : case RTE_VHOST_MSG_RESULT_OK:
3196 : : handled = true;
3197 : 0 : goto skip_to_post_handle;
3198 : : case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3199 : : default:
3200 : : break;
3201 : : }
3202 : : }
3203 : :
3204 [ # # # # ]: 0 : if (msg_handler == NULL || msg_handler->callback == NULL)
3205 : 0 : goto skip_to_post_handle;
3206 : :
3207 [ # # # # ]: 0 : if (!msg_handler->accepts_fd && validate_msg_fds(dev, &ctx, 0) != 0) {
3208 : : msg_result = RTE_VHOST_MSG_RESULT_ERR;
3209 : : } else {
3210 : 0 : msg_result = msg_handler->callback(&dev, &ctx, fd);
3211 : : }
3212 : :
3213 [ # # # # ]: 0 : switch (msg_result) {
3214 : 0 : case RTE_VHOST_MSG_RESULT_ERR:
3215 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
3216 : : "processing %s failed.",
3217 : : msg_handler->description);
3218 : : handled = true;
3219 : 0 : break;
3220 : 0 : case RTE_VHOST_MSG_RESULT_OK:
3221 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG,
3222 : : "processing %s succeeded.",
3223 : : msg_handler->description);
3224 : : handled = true;
3225 : 0 : break;
3226 : 0 : case RTE_VHOST_MSG_RESULT_REPLY:
3227 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG,
3228 : : "processing %s succeeded and needs reply.",
3229 : : msg_handler->description);
3230 : 0 : send_vhost_reply(dev, fd, &ctx);
3231 : : handled = true;
3232 : 0 : break;
3233 : : default:
3234 : : break;
3235 : : }
3236 : :
3237 : 0 : skip_to_post_handle:
3238 [ # # ]: 0 : if (msg_result != RTE_VHOST_MSG_RESULT_ERR &&
3239 [ # # ]: 0 : dev->extern_ops.post_msg_handle) {
3240 : : RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
3241 : 0 : msg_result = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx);
3242 [ # # # ]: 0 : switch (msg_result) {
3243 : 0 : case RTE_VHOST_MSG_RESULT_REPLY:
3244 : 0 : send_vhost_reply(dev, fd, &ctx);
3245 : : /* Fall-through */
3246 : : case RTE_VHOST_MSG_RESULT_ERR:
3247 : : case RTE_VHOST_MSG_RESULT_OK:
3248 : : handled = true;
3249 : : case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3250 : : default:
3251 : : break;
3252 : : }
3253 : : }
3254 : :
3255 : : /* If message was not handled at this stage, treat it as an error */
3256 [ # # ]: 0 : if (!handled) {
3257 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
3258 : : "vhost message (req: %d) was not handled.",
3259 : : request);
3260 : 0 : close_msg_fds(&ctx);
3261 : : msg_result = RTE_VHOST_MSG_RESULT_ERR;
3262 : : }
3263 : :
3264 : : /*
3265 : : * If the request required a reply that was already sent,
3266 : : * this optional reply-ack won't be sent as the
3267 : : * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply().
3268 : : */
3269 [ # # ]: 0 : if (ctx.msg.flags & VHOST_USER_NEED_REPLY) {
3270 : 0 : ctx.msg.payload.u64 = msg_result == RTE_VHOST_MSG_RESULT_ERR;
3271 : 0 : ctx.msg.size = sizeof(ctx.msg.payload.u64);
3272 : 0 : ctx.fd_num = 0;
3273 : 0 : send_vhost_reply(dev, fd, &ctx);
3274 [ # # ]: 0 : } else if (msg_result == RTE_VHOST_MSG_RESULT_ERR) {
3275 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "vhost message handling failed.");
3276 : : ret = -1;
3277 : 0 : goto unlock;
3278 : : }
3279 : :
3280 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
3281 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
3282 : 0 : bool cur_ready = vq_is_ready(dev, vq);
3283 : :
3284 [ # # # # : 0 : if (cur_ready != (vq && vq->ready)) {
# # ]
3285 : 0 : vq->ready = cur_ready;
3286 : 0 : vhost_user_notify_queue_state(dev, vq, cur_ready);
3287 : : }
3288 : : }
3289 : :
3290 : 0 : unlock:
3291 [ # # ]: 0 : if (unlock_required)
3292 : 0 : vhost_user_unlock_all_queue_pairs(dev);
3293 : :
3294 [ # # # # ]: 0 : if (ret != 0 || !virtio_is_ready(dev))
3295 : 0 : goto out;
3296 : :
3297 : : /*
3298 : : * Virtio is now ready. If not done already, it is time
3299 : : * to notify the application it can process the rings and
3300 : : * configure the vDPA device if present.
3301 : : */
3302 : :
3303 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
3304 [ # # ]: 0 : if (dev->notify_ops->new_device(dev->vid) == 0)
3305 : 0 : dev->flags |= VIRTIO_DEV_RUNNING;
3306 : : }
3307 : :
3308 : 0 : vdpa_dev = dev->vdpa_dev;
3309 [ # # ]: 0 : if (!vdpa_dev)
3310 : 0 : goto out;
3311 : :
3312 [ # # ]: 0 : if (vdpa_dev->type == RTE_VHOST_VDPA_DEVICE_TYPE_BLK) {
3313 [ # # ]: 0 : if (request == VHOST_USER_SET_VRING_CALL) {
3314 : 0 : blk_call_fd = ctx.msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
3315 [ # # ]: 0 : if (blk_call_fd != dev->nr_vring - 1)
3316 : 0 : goto out;
3317 : : } else {
3318 : 0 : goto out;
3319 : : }
3320 : : }
3321 : :
3322 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3323 [ # # ]: 0 : if (vdpa_dev->ops->dev_conf(dev->vid))
3324 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to configure vDPA device");
3325 : : else
3326 : 0 : dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
3327 : : }
3328 : :
3329 : 0 : out:
3330 : : return ret;
3331 : : }
3332 : :
3333 : : static int
3334 : 0 : vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
3335 : : {
3336 : : int ret;
3337 : 0 : struct vhu_msg_context ctx = {
3338 : : .msg = {
3339 : : .request.backend = VHOST_USER_BACKEND_IOTLB_MSG,
3340 : : .flags = VHOST_USER_VERSION,
3341 : : .size = sizeof(ctx.msg.payload.iotlb),
3342 : : .payload.iotlb = {
3343 : : .iova = iova,
3344 : : .perm = perm,
3345 : : .type = VHOST_IOTLB_MISS,
3346 : : },
3347 : : },
3348 : : };
3349 : :
3350 : 0 : ret = send_vhost_message(dev, dev->backend_req_fd, &ctx);
3351 [ # # ]: 0 : if (ret < 0) {
3352 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
3353 : : "failed to send IOTLB miss message (%d)",
3354 : : ret);
3355 : 0 : return ret;
3356 : : }
3357 : :
3358 : : return 0;
3359 : : }
3360 : :
3361 : : int
3362 : 0 : rte_vhost_backend_config_change(int vid, bool need_reply)
3363 : : {
3364 [ # # ]: 0 : struct vhu_msg_context ctx = {
3365 : : .msg = {
3366 : : .request.backend = VHOST_USER_BACKEND_CONFIG_CHANGE_MSG,
3367 : : .flags = VHOST_USER_VERSION,
3368 : : .size = 0,
3369 : : }
3370 : : };
3371 : : struct virtio_net *dev;
3372 : : int ret;
3373 : :
3374 : : dev = get_device(vid);
3375 [ # # ]: 0 : if (!dev)
3376 : : return -ENODEV;
3377 : :
3378 [ # # ]: 0 : if (!need_reply) {
3379 : : ret = send_vhost_backend_message(dev, &ctx);
3380 : : } else {
3381 : 0 : ctx.msg.flags |= VHOST_USER_NEED_REPLY;
3382 : 0 : ret = send_vhost_backend_message_process_reply(dev, &ctx);
3383 : : }
3384 : :
3385 [ # # ]: 0 : if (ret < 0)
3386 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to send config change (%d)", ret);
3387 : : return ret;
3388 : : }
3389 : :
3390 : 0 : static int vhost_user_backend_set_vring_host_notifier(struct virtio_net *dev,
3391 : : int index, int fd,
3392 : : uint64_t offset,
3393 : : uint64_t size)
3394 : : {
3395 : : int ret;
3396 : 0 : struct vhu_msg_context ctx = {
3397 : : .msg = {
3398 : : .request.backend = VHOST_USER_BACKEND_VRING_HOST_NOTIFIER_MSG,
3399 : : .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
3400 : : .size = sizeof(ctx.msg.payload.area),
3401 : : .payload.area = {
3402 : 0 : .u64 = index & VHOST_USER_VRING_IDX_MASK,
3403 : : .size = size,
3404 : : .offset = offset,
3405 : : },
3406 : : },
3407 : : };
3408 : :
3409 [ # # ]: 0 : if (fd < 0)
3410 : 0 : ctx.msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
3411 : : else {
3412 : 0 : ctx.fds[0] = fd;
3413 : 0 : ctx.fd_num = 1;
3414 : : }
3415 : :
3416 : 0 : ret = send_vhost_backend_message_process_reply(dev, &ctx);
3417 [ # # ]: 0 : if (ret < 0)
3418 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to set host notifier (%d)", ret);
3419 : :
3420 : 0 : return ret;
3421 : : }
3422 : :
3423 [ # # ]: 0 : int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable)
3424 : : {
3425 : : struct virtio_net *dev;
3426 : : struct rte_vdpa_device *vdpa_dev;
3427 : : int vfio_device_fd, ret = 0;
3428 : : uint64_t offset, size;
3429 : : unsigned int i, q_start, q_last;
3430 : :
3431 : : dev = get_device(vid);
3432 [ # # ]: 0 : if (!dev)
3433 : : return -ENODEV;
3434 : :
3435 : 0 : vdpa_dev = dev->vdpa_dev;
3436 [ # # ]: 0 : if (vdpa_dev == NULL)
3437 : : return -ENODEV;
3438 : :
3439 [ # # ]: 0 : if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
3440 : : !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
3441 : : !(dev->protocol_features &
3442 : : (1ULL << VHOST_USER_PROTOCOL_F_BACKEND_REQ)) ||
3443 : : !(dev->protocol_features &
3444 [ # # ]: 0 : (1ULL << VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD)) ||
3445 : : !(dev->protocol_features &
3446 : : (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
3447 : : return -ENOTSUP;
3448 : :
3449 [ # # ]: 0 : if (qid == RTE_VHOST_QUEUE_ALL) {
3450 : : q_start = 0;
3451 : 0 : q_last = dev->nr_vring - 1;
3452 : : } else {
3453 [ # # ]: 0 : if (qid >= dev->nr_vring)
3454 : : return -EINVAL;
3455 : : q_start = qid;
3456 : : q_last = qid;
3457 : : }
3458 : :
3459 [ # # ]: 0 : if (vdpa_dev->ops->get_vfio_device_fd == NULL)
3460 : : return -ENOTSUP;
3461 [ # # ]: 0 : if (vdpa_dev->ops->get_notify_area == NULL)
3462 : : return -ENOTSUP;
3463 : :
3464 : 0 : vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
3465 [ # # ]: 0 : if (vfio_device_fd < 0)
3466 : : return -ENOTSUP;
3467 : :
3468 [ # # ]: 0 : if (enable) {
3469 [ # # ]: 0 : for (i = q_start; i <= q_last; i++) {
3470 [ # # ]: 0 : if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
3471 : : &size) < 0) {
3472 : : ret = -ENOTSUP;
3473 : 0 : goto disable;
3474 : : }
3475 : :
3476 [ # # ]: 0 : if (vhost_user_backend_set_vring_host_notifier(dev, i,
3477 : : vfio_device_fd, offset, size) < 0) {
3478 : : ret = -EFAULT;
3479 : 0 : goto disable;
3480 : : }
3481 : : }
3482 : : } else {
3483 : 0 : disable:
3484 [ # # ]: 0 : for (i = q_start; i <= q_last; i++) {
3485 : 0 : vhost_user_backend_set_vring_host_notifier(dev, i, -1,
3486 : : 0, 0);
3487 : : }
3488 : : }
3489 : :
3490 : : return ret;
3491 : : }
3492 : :
3493 : : static int
3494 : 0 : vhost_user_inject_irq(struct virtio_net *dev __rte_unused, struct vhost_virtqueue *vq)
3495 : : {
3496 [ # # ]: 0 : if (vq->callfd < 0)
3497 : : return -1;
3498 : :
3499 : 0 : return eventfd_write(vq->callfd, (eventfd_t)1);
3500 : : }
3501 : :
3502 : : static struct vhost_backend_ops vhost_user_backend_ops = {
3503 : : .iotlb_miss = vhost_user_iotlb_miss,
3504 : : .inject_irq = vhost_user_inject_irq,
3505 : : };
3506 : :
3507 : : int
3508 : 0 : vhost_user_new_device(void)
3509 : : {
3510 : 0 : return vhost_new_device(&vhost_user_backend_ops);
3511 : : }
|