xref: /f-stack/dpdk/lib/librte_vhost/vhost.c (revision 2d9fd380)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4 
5 #include <linux/vhost.h>
6 #include <linux/virtio_net.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #ifdef RTE_LIBRTE_VHOST_NUMA
11 #include <numa.h>
12 #include <numaif.h>
13 #endif
14 
15 #include <rte_errno.h>
16 #include <rte_ethdev.h>
17 #include <rte_log.h>
18 #include <rte_string_fns.h>
19 #include <rte_memory.h>
20 #include <rte_malloc.h>
21 #include <rte_vhost.h>
22 #include <rte_rwlock.h>
23 
24 #include "iotlb.h"
25 #include "vhost.h"
26 #include "vhost_user.h"
27 
28 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
29 
30 /* Called with iotlb_lock read-locked */
31 uint64_t
__vhost_iova_to_vva(struct virtio_net * dev,struct vhost_virtqueue * vq,uint64_t iova,uint64_t * size,uint8_t perm)32 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
33 		    uint64_t iova, uint64_t *size, uint8_t perm)
34 {
35 	uint64_t vva, tmp_size;
36 
37 	if (unlikely(!*size))
38 		return 0;
39 
40 	tmp_size = *size;
41 
42 	vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
43 	if (tmp_size == *size)
44 		return vva;
45 
46 	iova += tmp_size;
47 
48 	if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
49 		/*
50 		 * iotlb_lock is read-locked for a full burst,
51 		 * but it only protects the iotlb cache.
52 		 * In case of IOTLB miss, we might block on the socket,
53 		 * which could cause a deadlock with QEMU if an IOTLB update
54 		 * is being handled. We can safely unlock here to avoid it.
55 		 */
56 		vhost_user_iotlb_rd_unlock(vq);
57 
58 		vhost_user_iotlb_pending_insert(vq, iova, perm);
59 		if (vhost_user_iotlb_miss(dev, iova, perm)) {
60 			VHOST_LOG_CONFIG(ERR,
61 				"IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
62 				iova);
63 			vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
64 		}
65 
66 		vhost_user_iotlb_rd_lock(vq);
67 	}
68 
69 	return 0;
70 }
71 
72 #define VHOST_LOG_PAGE	4096
73 
74 /*
75  * Atomically set a bit in memory.
76  */
77 static __rte_always_inline void
vhost_set_bit(unsigned int nr,volatile uint8_t * addr)78 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
79 {
80 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
81 	/*
82 	 * __sync_ built-ins are deprecated, but __atomic_ ones
83 	 * are sub-optimized in older GCC versions.
84 	 */
85 	__sync_fetch_and_or_1(addr, (1U << nr));
86 #else
87 	__atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
88 #endif
89 }
90 
91 static __rte_always_inline void
vhost_log_page(uint8_t * log_base,uint64_t page)92 vhost_log_page(uint8_t *log_base, uint64_t page)
93 {
94 	vhost_set_bit(page % 8, &log_base[page / 8]);
95 }
96 
97 void
__vhost_log_write(struct virtio_net * dev,uint64_t addr,uint64_t len)98 __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
99 {
100 	uint64_t page;
101 
102 	if (unlikely(!dev->log_base || !len))
103 		return;
104 
105 	if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
106 		return;
107 
108 	/* To make sure guest memory updates are committed before logging */
109 	rte_smp_wmb();
110 
111 	page = addr / VHOST_LOG_PAGE;
112 	while (page * VHOST_LOG_PAGE < addr + len) {
113 		vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
114 		page += 1;
115 	}
116 }
117 
118 void
__vhost_log_write_iova(struct virtio_net * dev,struct vhost_virtqueue * vq,uint64_t iova,uint64_t len)119 __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
120 			     uint64_t iova, uint64_t len)
121 {
122 	uint64_t hva, gpa, map_len;
123 	map_len = len;
124 
125 	hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
126 	if (map_len != len) {
127 		VHOST_LOG_DATA(ERR,
128 			"Failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
129 			iova);
130 		return;
131 	}
132 
133 	gpa = hva_to_gpa(dev, hva, len);
134 	if (gpa)
135 		__vhost_log_write(dev, gpa, len);
136 }
137 
138 void
__vhost_log_cache_sync(struct virtio_net * dev,struct vhost_virtqueue * vq)139 __vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
140 {
141 	unsigned long *log_base;
142 	int i;
143 
144 	if (unlikely(!dev->log_base))
145 		return;
146 
147 	rte_smp_wmb();
148 
149 	log_base = (unsigned long *)(uintptr_t)dev->log_base;
150 
151 	for (i = 0; i < vq->log_cache_nb_elem; i++) {
152 		struct log_cache_entry *elem = vq->log_cache + i;
153 
154 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
155 		/*
156 		 * '__sync' builtins are deprecated, but '__atomic' ones
157 		 * are sub-optimized in older GCC versions.
158 		 */
159 		__sync_fetch_and_or(log_base + elem->offset, elem->val);
160 #else
161 		__atomic_fetch_or(log_base + elem->offset, elem->val,
162 				__ATOMIC_RELAXED);
163 #endif
164 	}
165 
166 	rte_smp_wmb();
167 
168 	vq->log_cache_nb_elem = 0;
169 }
170 
171 static __rte_always_inline void
vhost_log_cache_page(struct virtio_net * dev,struct vhost_virtqueue * vq,uint64_t page)172 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
173 			uint64_t page)
174 {
175 	uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
176 	uint32_t offset = page / (sizeof(unsigned long) << 3);
177 	int i;
178 
179 	for (i = 0; i < vq->log_cache_nb_elem; i++) {
180 		struct log_cache_entry *elem = vq->log_cache + i;
181 
182 		if (elem->offset == offset) {
183 			elem->val |= (1UL << bit_nr);
184 			return;
185 		}
186 	}
187 
188 	if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
189 		/*
190 		 * No more room for a new log cache entry,
191 		 * so write the dirty log map directly.
192 		 */
193 		rte_smp_wmb();
194 		vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
195 
196 		return;
197 	}
198 
199 	vq->log_cache[i].offset = offset;
200 	vq->log_cache[i].val = (1UL << bit_nr);
201 	vq->log_cache_nb_elem++;
202 }
203 
204 void
__vhost_log_cache_write(struct virtio_net * dev,struct vhost_virtqueue * vq,uint64_t addr,uint64_t len)205 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
206 			uint64_t addr, uint64_t len)
207 {
208 	uint64_t page;
209 
210 	if (unlikely(!dev->log_base || !len))
211 		return;
212 
213 	if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
214 		return;
215 
216 	page = addr / VHOST_LOG_PAGE;
217 	while (page * VHOST_LOG_PAGE < addr + len) {
218 		vhost_log_cache_page(dev, vq, page);
219 		page += 1;
220 	}
221 }
222 
223 void
__vhost_log_cache_write_iova(struct virtio_net * dev,struct vhost_virtqueue * vq,uint64_t iova,uint64_t len)224 __vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
225 			     uint64_t iova, uint64_t len)
226 {
227 	uint64_t hva, gpa, map_len;
228 	map_len = len;
229 
230 	hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
231 	if (map_len != len) {
232 		VHOST_LOG_DATA(ERR,
233 			"Failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
234 			iova);
235 		return;
236 	}
237 
238 	gpa = hva_to_gpa(dev, hva, len);
239 	if (gpa)
240 		__vhost_log_cache_write(dev, vq, gpa, len);
241 }
242 
243 void *
vhost_alloc_copy_ind_table(struct virtio_net * dev,struct vhost_virtqueue * vq,uint64_t desc_addr,uint64_t desc_len)244 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
245 		uint64_t desc_addr, uint64_t desc_len)
246 {
247 	void *idesc;
248 	uint64_t src, dst;
249 	uint64_t len, remain = desc_len;
250 
251 	idesc = rte_malloc(__func__, desc_len, 0);
252 	if (unlikely(!idesc))
253 		return NULL;
254 
255 	dst = (uint64_t)(uintptr_t)idesc;
256 
257 	while (remain) {
258 		len = remain;
259 		src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
260 				VHOST_ACCESS_RO);
261 		if (unlikely(!src || !len)) {
262 			rte_free(idesc);
263 			return NULL;
264 		}
265 
266 		rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
267 
268 		remain -= len;
269 		dst += len;
270 		desc_addr += len;
271 	}
272 
273 	return idesc;
274 }
275 
276 void
cleanup_vq(struct vhost_virtqueue * vq,int destroy)277 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
278 {
279 	if ((vq->callfd >= 0) && (destroy != 0))
280 		close(vq->callfd);
281 	if (vq->kickfd >= 0)
282 		close(vq->kickfd);
283 }
284 
285 void
cleanup_vq_inflight(struct virtio_net * dev,struct vhost_virtqueue * vq)286 cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq)
287 {
288 	if (!(dev->protocol_features &
289 	    (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
290 		return;
291 
292 	if (vq_is_packed(dev)) {
293 		if (vq->inflight_packed)
294 			vq->inflight_packed = NULL;
295 	} else {
296 		if (vq->inflight_split)
297 			vq->inflight_split = NULL;
298 	}
299 
300 	if (vq->resubmit_inflight) {
301 		if (vq->resubmit_inflight->resubmit_list) {
302 			free(vq->resubmit_inflight->resubmit_list);
303 			vq->resubmit_inflight->resubmit_list = NULL;
304 		}
305 		free(vq->resubmit_inflight);
306 		vq->resubmit_inflight = NULL;
307 	}
308 }
309 
310 /*
311  * Unmap any memory, close any file descriptors and
312  * free any memory owned by a device.
313  */
314 void
cleanup_device(struct virtio_net * dev,int destroy)315 cleanup_device(struct virtio_net *dev, int destroy)
316 {
317 	uint32_t i;
318 
319 	vhost_backend_cleanup(dev);
320 
321 	for (i = 0; i < dev->nr_vring; i++) {
322 		cleanup_vq(dev->virtqueue[i], destroy);
323 		cleanup_vq_inflight(dev, dev->virtqueue[i]);
324 	}
325 }
326 
327 static void
vhost_free_async_mem(struct vhost_virtqueue * vq)328 vhost_free_async_mem(struct vhost_virtqueue *vq)
329 {
330 	if (vq->async_pkts_pending)
331 		rte_free(vq->async_pkts_pending);
332 	if (vq->async_pkts_info)
333 		rte_free(vq->async_pkts_info);
334 	if (vq->it_pool)
335 		rte_free(vq->it_pool);
336 	if (vq->vec_pool)
337 		rte_free(vq->vec_pool);
338 
339 	vq->async_pkts_pending = NULL;
340 	vq->async_pkts_info = NULL;
341 	vq->it_pool = NULL;
342 	vq->vec_pool = NULL;
343 }
344 
345 void
free_vq(struct virtio_net * dev,struct vhost_virtqueue * vq)346 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
347 {
348 	if (vq_is_packed(dev))
349 		rte_free(vq->shadow_used_packed);
350 	else {
351 		rte_free(vq->shadow_used_split);
352 		vhost_free_async_mem(vq);
353 	}
354 	rte_free(vq->batch_copy_elems);
355 	rte_mempool_free(vq->iotlb_pool);
356 	rte_free(vq);
357 }
358 
359 /*
360  * Release virtqueues and device memory.
361  */
362 static void
free_device(struct virtio_net * dev)363 free_device(struct virtio_net *dev)
364 {
365 	uint32_t i;
366 
367 	for (i = 0; i < dev->nr_vring; i++)
368 		free_vq(dev, dev->virtqueue[i]);
369 
370 	rte_free(dev);
371 }
372 
373 static __rte_always_inline int
log_translate(struct virtio_net * dev,struct vhost_virtqueue * vq)374 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
375 {
376 	if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
377 		return 0;
378 
379 	vq->log_guest_addr = translate_log_addr(dev, vq,
380 						vq->ring_addrs.log_guest_addr);
381 	if (vq->log_guest_addr == 0)
382 		return -1;
383 
384 	return 0;
385 }
386 
387 /*
388  * Converts vring log address to GPA
389  * If IOMMU is enabled, the log address is IOVA
390  * If IOMMU not enabled, the log address is already GPA
391  *
392  * Caller should have iotlb_lock read-locked
393  */
394 uint64_t
translate_log_addr(struct virtio_net * dev,struct vhost_virtqueue * vq,uint64_t log_addr)395 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
396 		uint64_t log_addr)
397 {
398 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
399 		const uint64_t exp_size = sizeof(uint64_t);
400 		uint64_t hva, gpa;
401 		uint64_t size = exp_size;
402 
403 		hva = vhost_iova_to_vva(dev, vq, log_addr,
404 					&size, VHOST_ACCESS_RW);
405 
406 		if (size != exp_size)
407 			return 0;
408 
409 		gpa = hva_to_gpa(dev, hva, exp_size);
410 		if (!gpa) {
411 			VHOST_LOG_CONFIG(ERR,
412 				"VQ: Failed to find GPA for log_addr: 0x%"
413 				PRIx64 " hva: 0x%" PRIx64 "\n",
414 				log_addr, hva);
415 			return 0;
416 		}
417 		return gpa;
418 
419 	} else
420 		return log_addr;
421 }
422 
423 /* Caller should have iotlb_lock read-locked */
424 static int
vring_translate_split(struct virtio_net * dev,struct vhost_virtqueue * vq)425 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
426 {
427 	uint64_t req_size, size;
428 
429 	req_size = sizeof(struct vring_desc) * vq->size;
430 	size = req_size;
431 	vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
432 						vq->ring_addrs.desc_user_addr,
433 						&size, VHOST_ACCESS_RW);
434 	if (!vq->desc || size != req_size)
435 		return -1;
436 
437 	req_size = sizeof(struct vring_avail);
438 	req_size += sizeof(uint16_t) * vq->size;
439 	if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
440 		req_size += sizeof(uint16_t);
441 	size = req_size;
442 	vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
443 						vq->ring_addrs.avail_user_addr,
444 						&size, VHOST_ACCESS_RW);
445 	if (!vq->avail || size != req_size)
446 		return -1;
447 
448 	req_size = sizeof(struct vring_used);
449 	req_size += sizeof(struct vring_used_elem) * vq->size;
450 	if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
451 		req_size += sizeof(uint16_t);
452 	size = req_size;
453 	vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
454 						vq->ring_addrs.used_user_addr,
455 						&size, VHOST_ACCESS_RW);
456 	if (!vq->used || size != req_size)
457 		return -1;
458 
459 	return 0;
460 }
461 
462 /* Caller should have iotlb_lock read-locked */
463 static int
vring_translate_packed(struct virtio_net * dev,struct vhost_virtqueue * vq)464 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
465 {
466 	uint64_t req_size, size;
467 
468 	req_size = sizeof(struct vring_packed_desc) * vq->size;
469 	size = req_size;
470 	vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
471 		vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
472 				&size, VHOST_ACCESS_RW);
473 	if (!vq->desc_packed || size != req_size)
474 		return -1;
475 
476 	req_size = sizeof(struct vring_packed_desc_event);
477 	size = req_size;
478 	vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
479 		vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
480 				&size, VHOST_ACCESS_RW);
481 	if (!vq->driver_event || size != req_size)
482 		return -1;
483 
484 	req_size = sizeof(struct vring_packed_desc_event);
485 	size = req_size;
486 	vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
487 		vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
488 				&size, VHOST_ACCESS_RW);
489 	if (!vq->device_event || size != req_size)
490 		return -1;
491 
492 	return 0;
493 }
494 
495 int
vring_translate(struct virtio_net * dev,struct vhost_virtqueue * vq)496 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
497 {
498 
499 	if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
500 		return -1;
501 
502 	if (vq_is_packed(dev)) {
503 		if (vring_translate_packed(dev, vq) < 0)
504 			return -1;
505 	} else {
506 		if (vring_translate_split(dev, vq) < 0)
507 			return -1;
508 	}
509 
510 	if (log_translate(dev, vq) < 0)
511 		return -1;
512 
513 	vq->access_ok = 1;
514 
515 	return 0;
516 }
517 
518 void
vring_invalidate(struct virtio_net * dev,struct vhost_virtqueue * vq)519 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
520 {
521 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
522 		vhost_user_iotlb_wr_lock(vq);
523 
524 	vq->access_ok = 0;
525 	vq->desc = NULL;
526 	vq->avail = NULL;
527 	vq->used = NULL;
528 	vq->log_guest_addr = 0;
529 
530 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
531 		vhost_user_iotlb_wr_unlock(vq);
532 }
533 
534 static void
init_vring_queue(struct virtio_net * dev,uint32_t vring_idx)535 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
536 {
537 	struct vhost_virtqueue *vq;
538 
539 	if (vring_idx >= VHOST_MAX_VRING) {
540 		VHOST_LOG_CONFIG(ERR,
541 				"Failed not init vring, out of bound (%d)\n",
542 				vring_idx);
543 		return;
544 	}
545 
546 	vq = dev->virtqueue[vring_idx];
547 	if (!vq) {
548 		VHOST_LOG_CONFIG(ERR, "Virtqueue not allocated (%d)\n",
549 				vring_idx);
550 		return;
551 	}
552 
553 	memset(vq, 0, sizeof(struct vhost_virtqueue));
554 
555 	vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
556 	vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
557 	vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
558 
559 	vhost_user_iotlb_init(dev, vring_idx);
560 	/* Backends are set to -1 indicating an inactive device. */
561 	vq->backend = -1;
562 }
563 
564 static void
reset_vring_queue(struct virtio_net * dev,uint32_t vring_idx)565 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
566 {
567 	struct vhost_virtqueue *vq;
568 	int callfd;
569 
570 	if (vring_idx >= VHOST_MAX_VRING) {
571 		VHOST_LOG_CONFIG(ERR,
572 				"Failed not init vring, out of bound (%d)\n",
573 				vring_idx);
574 		return;
575 	}
576 
577 	vq = dev->virtqueue[vring_idx];
578 	if (!vq) {
579 		VHOST_LOG_CONFIG(ERR, "Virtqueue not allocated (%d)\n",
580 				vring_idx);
581 		return;
582 	}
583 
584 	callfd = vq->callfd;
585 	init_vring_queue(dev, vring_idx);
586 	vq->callfd = callfd;
587 }
588 
589 int
alloc_vring_queue(struct virtio_net * dev,uint32_t vring_idx)590 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
591 {
592 	struct vhost_virtqueue *vq;
593 	uint32_t i;
594 
595 	/* Also allocate holes, if any, up to requested vring index. */
596 	for (i = 0; i <= vring_idx; i++) {
597 		if (dev->virtqueue[i])
598 			continue;
599 
600 		vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
601 		if (vq == NULL) {
602 			VHOST_LOG_CONFIG(ERR,
603 				"Failed to allocate memory for vring:%u.\n", i);
604 			return -1;
605 		}
606 
607 		dev->virtqueue[i] = vq;
608 		init_vring_queue(dev, i);
609 		rte_spinlock_init(&vq->access_lock);
610 		vq->avail_wrap_counter = 1;
611 		vq->used_wrap_counter = 1;
612 		vq->signalled_used_valid = false;
613 	}
614 
615 	dev->nr_vring = RTE_MAX(dev->nr_vring, vring_idx + 1);
616 
617 	return 0;
618 }
619 
620 /*
621  * Reset some variables in device structure, while keeping few
622  * others untouched, such as vid, ifname, nr_vring: they
623  * should be same unless the device is removed.
624  */
625 void
reset_device(struct virtio_net * dev)626 reset_device(struct virtio_net *dev)
627 {
628 	uint32_t i;
629 
630 	dev->features = 0;
631 	dev->protocol_features = 0;
632 	dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
633 
634 	for (i = 0; i < dev->nr_vring; i++)
635 		reset_vring_queue(dev, i);
636 }
637 
638 /*
639  * Invoked when there is a new vhost-user connection established (when
640  * there is a new virtio device being attached).
641  */
642 int
vhost_new_device(void)643 vhost_new_device(void)
644 {
645 	struct virtio_net *dev;
646 	int i;
647 
648 	for (i = 0; i < MAX_VHOST_DEVICE; i++) {
649 		if (vhost_devices[i] == NULL)
650 			break;
651 	}
652 
653 	if (i == MAX_VHOST_DEVICE) {
654 		VHOST_LOG_CONFIG(ERR,
655 			"Failed to find a free slot for new device.\n");
656 		return -1;
657 	}
658 
659 	dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
660 	if (dev == NULL) {
661 		VHOST_LOG_CONFIG(ERR,
662 			"Failed to allocate memory for new dev.\n");
663 		return -1;
664 	}
665 
666 	vhost_devices[i] = dev;
667 	dev->vid = i;
668 	dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
669 	dev->slave_req_fd = -1;
670 	dev->postcopy_ufd = -1;
671 	rte_spinlock_init(&dev->slave_req_lock);
672 
673 	return i;
674 }
675 
676 void
vhost_destroy_device_notify(struct virtio_net * dev)677 vhost_destroy_device_notify(struct virtio_net *dev)
678 {
679 	struct rte_vdpa_device *vdpa_dev;
680 
681 	if (dev->flags & VIRTIO_DEV_RUNNING) {
682 		vdpa_dev = dev->vdpa_dev;
683 		if (vdpa_dev)
684 			vdpa_dev->ops->dev_close(dev->vid);
685 		dev->flags &= ~VIRTIO_DEV_RUNNING;
686 		dev->notify_ops->destroy_device(dev->vid);
687 	}
688 }
689 
690 /*
691  * Invoked when there is the vhost-user connection is broken (when
692  * the virtio device is being detached).
693  */
694 void
vhost_destroy_device(int vid)695 vhost_destroy_device(int vid)
696 {
697 	struct virtio_net *dev = get_device(vid);
698 
699 	if (dev == NULL)
700 		return;
701 
702 	vhost_destroy_device_notify(dev);
703 
704 	cleanup_device(dev, 1);
705 	free_device(dev);
706 
707 	vhost_devices[vid] = NULL;
708 }
709 
710 void
vhost_attach_vdpa_device(int vid,struct rte_vdpa_device * vdpa_dev)711 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
712 {
713 	struct virtio_net *dev = get_device(vid);
714 
715 	if (dev == NULL)
716 		return;
717 
718 	dev->vdpa_dev = vdpa_dev;
719 }
720 
721 void
vhost_set_ifname(int vid,const char * if_name,unsigned int if_len)722 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
723 {
724 	struct virtio_net *dev;
725 	unsigned int len;
726 
727 	dev = get_device(vid);
728 	if (dev == NULL)
729 		return;
730 
731 	len = if_len > sizeof(dev->ifname) ?
732 		sizeof(dev->ifname) : if_len;
733 
734 	strncpy(dev->ifname, if_name, len);
735 	dev->ifname[sizeof(dev->ifname) - 1] = '\0';
736 }
737 
738 void
vhost_set_builtin_virtio_net(int vid,bool enable)739 vhost_set_builtin_virtio_net(int vid, bool enable)
740 {
741 	struct virtio_net *dev = get_device(vid);
742 
743 	if (dev == NULL)
744 		return;
745 
746 	if (enable)
747 		dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
748 	else
749 		dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
750 }
751 
752 void
vhost_enable_extbuf(int vid)753 vhost_enable_extbuf(int vid)
754 {
755 	struct virtio_net *dev = get_device(vid);
756 
757 	if (dev == NULL)
758 		return;
759 
760 	dev->extbuf = 1;
761 }
762 
763 void
vhost_enable_linearbuf(int vid)764 vhost_enable_linearbuf(int vid)
765 {
766 	struct virtio_net *dev = get_device(vid);
767 
768 	if (dev == NULL)
769 		return;
770 
771 	dev->linearbuf = 1;
772 }
773 
774 int
rte_vhost_get_mtu(int vid,uint16_t * mtu)775 rte_vhost_get_mtu(int vid, uint16_t *mtu)
776 {
777 	struct virtio_net *dev = get_device(vid);
778 
779 	if (dev == NULL || mtu == NULL)
780 		return -ENODEV;
781 
782 	if (!(dev->flags & VIRTIO_DEV_READY))
783 		return -EAGAIN;
784 
785 	if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
786 		return -ENOTSUP;
787 
788 	*mtu = dev->mtu;
789 
790 	return 0;
791 }
792 
793 int
rte_vhost_get_numa_node(int vid)794 rte_vhost_get_numa_node(int vid)
795 {
796 #ifdef RTE_LIBRTE_VHOST_NUMA
797 	struct virtio_net *dev = get_device(vid);
798 	int numa_node;
799 	int ret;
800 
801 	if (dev == NULL || numa_available() != 0)
802 		return -1;
803 
804 	ret = get_mempolicy(&numa_node, NULL, 0, dev,
805 			    MPOL_F_NODE | MPOL_F_ADDR);
806 	if (ret < 0) {
807 		VHOST_LOG_CONFIG(ERR,
808 			"(%d) failed to query numa node: %s\n",
809 			vid, rte_strerror(errno));
810 		return -1;
811 	}
812 
813 	return numa_node;
814 #else
815 	RTE_SET_USED(vid);
816 	return -1;
817 #endif
818 }
819 
820 uint32_t
rte_vhost_get_queue_num(int vid)821 rte_vhost_get_queue_num(int vid)
822 {
823 	struct virtio_net *dev = get_device(vid);
824 
825 	if (dev == NULL)
826 		return 0;
827 
828 	return dev->nr_vring / 2;
829 }
830 
831 uint16_t
rte_vhost_get_vring_num(int vid)832 rte_vhost_get_vring_num(int vid)
833 {
834 	struct virtio_net *dev = get_device(vid);
835 
836 	if (dev == NULL)
837 		return 0;
838 
839 	return dev->nr_vring;
840 }
841 
842 int
rte_vhost_get_ifname(int vid,char * buf,size_t len)843 rte_vhost_get_ifname(int vid, char *buf, size_t len)
844 {
845 	struct virtio_net *dev = get_device(vid);
846 
847 	if (dev == NULL || buf == NULL)
848 		return -1;
849 
850 	len = RTE_MIN(len, sizeof(dev->ifname));
851 
852 	strncpy(buf, dev->ifname, len);
853 	buf[len - 1] = '\0';
854 
855 	return 0;
856 }
857 
858 int
rte_vhost_get_negotiated_features(int vid,uint64_t * features)859 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
860 {
861 	struct virtio_net *dev;
862 
863 	dev = get_device(vid);
864 	if (dev == NULL || features == NULL)
865 		return -1;
866 
867 	*features = dev->features;
868 	return 0;
869 }
870 
871 int
rte_vhost_get_mem_table(int vid,struct rte_vhost_memory ** mem)872 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
873 {
874 	struct virtio_net *dev;
875 	struct rte_vhost_memory *m;
876 	size_t size;
877 
878 	dev = get_device(vid);
879 	if (dev == NULL || mem == NULL)
880 		return -1;
881 
882 	size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
883 	m = malloc(sizeof(struct rte_vhost_memory) + size);
884 	if (!m)
885 		return -1;
886 
887 	m->nregions = dev->mem->nregions;
888 	memcpy(m->regions, dev->mem->regions, size);
889 	*mem = m;
890 
891 	return 0;
892 }
893 
894 int
rte_vhost_get_vhost_vring(int vid,uint16_t vring_idx,struct rte_vhost_vring * vring)895 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
896 			  struct rte_vhost_vring *vring)
897 {
898 	struct virtio_net *dev;
899 	struct vhost_virtqueue *vq;
900 
901 	dev = get_device(vid);
902 	if (dev == NULL || vring == NULL)
903 		return -1;
904 
905 	if (vring_idx >= VHOST_MAX_VRING)
906 		return -1;
907 
908 	vq = dev->virtqueue[vring_idx];
909 	if (!vq)
910 		return -1;
911 
912 	if (vq_is_packed(dev)) {
913 		vring->desc_packed = vq->desc_packed;
914 		vring->driver_event = vq->driver_event;
915 		vring->device_event = vq->device_event;
916 	} else {
917 		vring->desc = vq->desc;
918 		vring->avail = vq->avail;
919 		vring->used = vq->used;
920 	}
921 	vring->log_guest_addr  = vq->log_guest_addr;
922 
923 	vring->callfd  = vq->callfd;
924 	vring->kickfd  = vq->kickfd;
925 	vring->size    = vq->size;
926 
927 	return 0;
928 }
929 
930 int
rte_vhost_get_vhost_ring_inflight(int vid,uint16_t vring_idx,struct rte_vhost_ring_inflight * vring)931 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
932 				  struct rte_vhost_ring_inflight *vring)
933 {
934 	struct virtio_net *dev;
935 	struct vhost_virtqueue *vq;
936 
937 	dev = get_device(vid);
938 	if (unlikely(!dev))
939 		return -1;
940 
941 	if (vring_idx >= VHOST_MAX_VRING)
942 		return -1;
943 
944 	vq = dev->virtqueue[vring_idx];
945 	if (unlikely(!vq))
946 		return -1;
947 
948 	if (vq_is_packed(dev)) {
949 		if (unlikely(!vq->inflight_packed))
950 			return -1;
951 
952 		vring->inflight_packed = vq->inflight_packed;
953 	} else {
954 		if (unlikely(!vq->inflight_split))
955 			return -1;
956 
957 		vring->inflight_split = vq->inflight_split;
958 	}
959 
960 	vring->resubmit_inflight = vq->resubmit_inflight;
961 
962 	return 0;
963 }
964 
965 int
rte_vhost_set_inflight_desc_split(int vid,uint16_t vring_idx,uint16_t idx)966 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
967 				  uint16_t idx)
968 {
969 	struct vhost_virtqueue *vq;
970 	struct virtio_net *dev;
971 
972 	dev = get_device(vid);
973 	if (unlikely(!dev))
974 		return -1;
975 
976 	if (unlikely(!(dev->protocol_features &
977 	    (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
978 		return 0;
979 
980 	if (unlikely(vq_is_packed(dev)))
981 		return -1;
982 
983 	if (unlikely(vring_idx >= VHOST_MAX_VRING))
984 		return -1;
985 
986 	vq = dev->virtqueue[vring_idx];
987 	if (unlikely(!vq))
988 		return -1;
989 
990 	if (unlikely(!vq->inflight_split))
991 		return -1;
992 
993 	if (unlikely(idx >= vq->size))
994 		return -1;
995 
996 	vq->inflight_split->desc[idx].counter = vq->global_counter++;
997 	vq->inflight_split->desc[idx].inflight = 1;
998 	return 0;
999 }
1000 
1001 int
rte_vhost_set_inflight_desc_packed(int vid,uint16_t vring_idx,uint16_t head,uint16_t last,uint16_t * inflight_entry)1002 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
1003 				   uint16_t head, uint16_t last,
1004 				   uint16_t *inflight_entry)
1005 {
1006 	struct rte_vhost_inflight_info_packed *inflight_info;
1007 	struct virtio_net *dev;
1008 	struct vhost_virtqueue *vq;
1009 	struct vring_packed_desc *desc;
1010 	uint16_t old_free_head, free_head;
1011 
1012 	dev = get_device(vid);
1013 	if (unlikely(!dev))
1014 		return -1;
1015 
1016 	if (unlikely(!(dev->protocol_features &
1017 	    (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1018 		return 0;
1019 
1020 	if (unlikely(!vq_is_packed(dev)))
1021 		return -1;
1022 
1023 	if (unlikely(vring_idx >= VHOST_MAX_VRING))
1024 		return -1;
1025 
1026 	vq = dev->virtqueue[vring_idx];
1027 	if (unlikely(!vq))
1028 		return -1;
1029 
1030 	inflight_info = vq->inflight_packed;
1031 	if (unlikely(!inflight_info))
1032 		return -1;
1033 
1034 	if (unlikely(head >= vq->size))
1035 		return -1;
1036 
1037 	desc = vq->desc_packed;
1038 	old_free_head = inflight_info->old_free_head;
1039 	if (unlikely(old_free_head >= vq->size))
1040 		return -1;
1041 
1042 	free_head = old_free_head;
1043 
1044 	/* init header descriptor */
1045 	inflight_info->desc[old_free_head].num = 0;
1046 	inflight_info->desc[old_free_head].counter = vq->global_counter++;
1047 	inflight_info->desc[old_free_head].inflight = 1;
1048 
1049 	/* save desc entry in flight entry */
1050 	while (head != ((last + 1) % vq->size)) {
1051 		inflight_info->desc[old_free_head].num++;
1052 		inflight_info->desc[free_head].addr = desc[head].addr;
1053 		inflight_info->desc[free_head].len = desc[head].len;
1054 		inflight_info->desc[free_head].flags = desc[head].flags;
1055 		inflight_info->desc[free_head].id = desc[head].id;
1056 
1057 		inflight_info->desc[old_free_head].last = free_head;
1058 		free_head = inflight_info->desc[free_head].next;
1059 		inflight_info->free_head = free_head;
1060 		head = (head + 1) % vq->size;
1061 	}
1062 
1063 	inflight_info->old_free_head = free_head;
1064 	*inflight_entry = old_free_head;
1065 
1066 	return 0;
1067 }
1068 
1069 int
rte_vhost_clr_inflight_desc_split(int vid,uint16_t vring_idx,uint16_t last_used_idx,uint16_t idx)1070 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1071 				  uint16_t last_used_idx, uint16_t idx)
1072 {
1073 	struct virtio_net *dev;
1074 	struct vhost_virtqueue *vq;
1075 
1076 	dev = get_device(vid);
1077 	if (unlikely(!dev))
1078 		return -1;
1079 
1080 	if (unlikely(!(dev->protocol_features &
1081 	    (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1082 		return 0;
1083 
1084 	if (unlikely(vq_is_packed(dev)))
1085 		return -1;
1086 
1087 	if (unlikely(vring_idx >= VHOST_MAX_VRING))
1088 		return -1;
1089 
1090 	vq = dev->virtqueue[vring_idx];
1091 	if (unlikely(!vq))
1092 		return -1;
1093 
1094 	if (unlikely(!vq->inflight_split))
1095 		return -1;
1096 
1097 	if (unlikely(idx >= vq->size))
1098 		return -1;
1099 
1100 	rte_smp_mb();
1101 
1102 	vq->inflight_split->desc[idx].inflight = 0;
1103 
1104 	rte_smp_mb();
1105 
1106 	vq->inflight_split->used_idx = last_used_idx;
1107 	return 0;
1108 }
1109 
1110 int
rte_vhost_clr_inflight_desc_packed(int vid,uint16_t vring_idx,uint16_t head)1111 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1112 				   uint16_t head)
1113 {
1114 	struct rte_vhost_inflight_info_packed *inflight_info;
1115 	struct virtio_net *dev;
1116 	struct vhost_virtqueue *vq;
1117 
1118 	dev = get_device(vid);
1119 	if (unlikely(!dev))
1120 		return -1;
1121 
1122 	if (unlikely(!(dev->protocol_features &
1123 	    (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1124 		return 0;
1125 
1126 	if (unlikely(!vq_is_packed(dev)))
1127 		return -1;
1128 
1129 	if (unlikely(vring_idx >= VHOST_MAX_VRING))
1130 		return -1;
1131 
1132 	vq = dev->virtqueue[vring_idx];
1133 	if (unlikely(!vq))
1134 		return -1;
1135 
1136 	inflight_info = vq->inflight_packed;
1137 	if (unlikely(!inflight_info))
1138 		return -1;
1139 
1140 	if (unlikely(head >= vq->size))
1141 		return -1;
1142 
1143 	rte_smp_mb();
1144 
1145 	inflight_info->desc[head].inflight = 0;
1146 
1147 	rte_smp_mb();
1148 
1149 	inflight_info->old_free_head = inflight_info->free_head;
1150 	inflight_info->old_used_idx = inflight_info->used_idx;
1151 	inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1152 
1153 	return 0;
1154 }
1155 
1156 int
rte_vhost_set_last_inflight_io_split(int vid,uint16_t vring_idx,uint16_t idx)1157 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1158 				     uint16_t idx)
1159 {
1160 	struct virtio_net *dev;
1161 	struct vhost_virtqueue *vq;
1162 
1163 	dev = get_device(vid);
1164 	if (unlikely(!dev))
1165 		return -1;
1166 
1167 	if (unlikely(!(dev->protocol_features &
1168 	    (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1169 		return 0;
1170 
1171 	if (unlikely(vq_is_packed(dev)))
1172 		return -1;
1173 
1174 	if (unlikely(vring_idx >= VHOST_MAX_VRING))
1175 		return -1;
1176 
1177 	vq = dev->virtqueue[vring_idx];
1178 	if (unlikely(!vq))
1179 		return -1;
1180 
1181 	if (unlikely(!vq->inflight_split))
1182 		return -1;
1183 
1184 	vq->inflight_split->last_inflight_io = idx;
1185 	return 0;
1186 }
1187 
1188 int
rte_vhost_set_last_inflight_io_packed(int vid,uint16_t vring_idx,uint16_t head)1189 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1190 				      uint16_t head)
1191 {
1192 	struct rte_vhost_inflight_info_packed *inflight_info;
1193 	struct virtio_net *dev;
1194 	struct vhost_virtqueue *vq;
1195 	uint16_t last;
1196 
1197 	dev = get_device(vid);
1198 	if (unlikely(!dev))
1199 		return -1;
1200 
1201 	if (unlikely(!(dev->protocol_features &
1202 	    (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1203 		return 0;
1204 
1205 	if (unlikely(!vq_is_packed(dev)))
1206 		return -1;
1207 
1208 	if (unlikely(vring_idx >= VHOST_MAX_VRING))
1209 		return -1;
1210 
1211 	vq = dev->virtqueue[vring_idx];
1212 	if (unlikely(!vq))
1213 		return -1;
1214 
1215 	inflight_info = vq->inflight_packed;
1216 	if (unlikely(!inflight_info))
1217 		return -1;
1218 
1219 	if (unlikely(head >= vq->size))
1220 		return -1;
1221 
1222 	last = inflight_info->desc[head].last;
1223 	if (unlikely(last >= vq->size))
1224 		return -1;
1225 
1226 	inflight_info->desc[last].next = inflight_info->free_head;
1227 	inflight_info->free_head = head;
1228 	inflight_info->used_idx += inflight_info->desc[head].num;
1229 	if (inflight_info->used_idx >= inflight_info->desc_num) {
1230 		inflight_info->used_idx -= inflight_info->desc_num;
1231 		inflight_info->used_wrap_counter =
1232 			!inflight_info->used_wrap_counter;
1233 	}
1234 
1235 	return 0;
1236 }
1237 
1238 int
rte_vhost_vring_call(int vid,uint16_t vring_idx)1239 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1240 {
1241 	struct virtio_net *dev;
1242 	struct vhost_virtqueue *vq;
1243 
1244 	dev = get_device(vid);
1245 	if (!dev)
1246 		return -1;
1247 
1248 	if (vring_idx >= VHOST_MAX_VRING)
1249 		return -1;
1250 
1251 	vq = dev->virtqueue[vring_idx];
1252 	if (!vq)
1253 		return -1;
1254 
1255 	if (vq_is_packed(dev))
1256 		vhost_vring_call_packed(dev, vq);
1257 	else
1258 		vhost_vring_call_split(dev, vq);
1259 
1260 	return 0;
1261 }
1262 
1263 uint16_t
rte_vhost_avail_entries(int vid,uint16_t queue_id)1264 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1265 {
1266 	struct virtio_net *dev;
1267 	struct vhost_virtqueue *vq;
1268 	uint16_t ret = 0;
1269 
1270 	dev = get_device(vid);
1271 	if (!dev)
1272 		return 0;
1273 
1274 	if (queue_id >= VHOST_MAX_VRING)
1275 		return 0;
1276 
1277 	vq = dev->virtqueue[queue_id];
1278 	if (!vq)
1279 		return 0;
1280 
1281 	rte_spinlock_lock(&vq->access_lock);
1282 
1283 	if (unlikely(!vq->enabled || vq->avail == NULL))
1284 		goto out;
1285 
1286 	ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1287 
1288 out:
1289 	rte_spinlock_unlock(&vq->access_lock);
1290 	return ret;
1291 }
1292 
1293 static inline int
vhost_enable_notify_split(struct virtio_net * dev,struct vhost_virtqueue * vq,int enable)1294 vhost_enable_notify_split(struct virtio_net *dev,
1295 		struct vhost_virtqueue *vq, int enable)
1296 {
1297 	if (vq->used == NULL)
1298 		return -1;
1299 
1300 	if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1301 		if (enable)
1302 			vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1303 		else
1304 			vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1305 	} else {
1306 		if (enable)
1307 			vhost_avail_event(vq) = vq->last_avail_idx;
1308 	}
1309 	return 0;
1310 }
1311 
1312 static inline int
vhost_enable_notify_packed(struct virtio_net * dev,struct vhost_virtqueue * vq,int enable)1313 vhost_enable_notify_packed(struct virtio_net *dev,
1314 		struct vhost_virtqueue *vq, int enable)
1315 {
1316 	uint16_t flags;
1317 
1318 	if (vq->device_event == NULL)
1319 		return -1;
1320 
1321 	if (!enable) {
1322 		vq->device_event->flags = VRING_EVENT_F_DISABLE;
1323 		return 0;
1324 	}
1325 
1326 	flags = VRING_EVENT_F_ENABLE;
1327 	if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1328 		flags = VRING_EVENT_F_DESC;
1329 		vq->device_event->off_wrap = vq->last_avail_idx |
1330 			vq->avail_wrap_counter << 15;
1331 	}
1332 
1333 	rte_smp_wmb();
1334 
1335 	vq->device_event->flags = flags;
1336 	return 0;
1337 }
1338 
1339 int
vhost_enable_guest_notification(struct virtio_net * dev,struct vhost_virtqueue * vq,int enable)1340 vhost_enable_guest_notification(struct virtio_net *dev,
1341 		struct vhost_virtqueue *vq, int enable)
1342 {
1343 	/*
1344 	 * If the virtqueue is not ready yet, it will be applied
1345 	 * when it will become ready.
1346 	 */
1347 	if (!vq->ready)
1348 		return 0;
1349 
1350 	if (vq_is_packed(dev))
1351 		return vhost_enable_notify_packed(dev, vq, enable);
1352 	else
1353 		return vhost_enable_notify_split(dev, vq, enable);
1354 }
1355 
1356 int
rte_vhost_enable_guest_notification(int vid,uint16_t queue_id,int enable)1357 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1358 {
1359 	struct virtio_net *dev = get_device(vid);
1360 	struct vhost_virtqueue *vq;
1361 	int ret;
1362 
1363 	if (!dev)
1364 		return -1;
1365 
1366 	if (queue_id >= VHOST_MAX_VRING)
1367 		return -1;
1368 
1369 	vq = dev->virtqueue[queue_id];
1370 	if (!vq)
1371 		return -1;
1372 
1373 	rte_spinlock_lock(&vq->access_lock);
1374 
1375 	vq->notif_enable = enable;
1376 	ret = vhost_enable_guest_notification(dev, vq, enable);
1377 
1378 	rte_spinlock_unlock(&vq->access_lock);
1379 
1380 	return ret;
1381 }
1382 
1383 void
rte_vhost_log_write(int vid,uint64_t addr,uint64_t len)1384 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1385 {
1386 	struct virtio_net *dev = get_device(vid);
1387 
1388 	if (dev == NULL)
1389 		return;
1390 
1391 	vhost_log_write(dev, addr, len);
1392 }
1393 
1394 void
rte_vhost_log_used_vring(int vid,uint16_t vring_idx,uint64_t offset,uint64_t len)1395 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1396 			 uint64_t offset, uint64_t len)
1397 {
1398 	struct virtio_net *dev;
1399 	struct vhost_virtqueue *vq;
1400 
1401 	dev = get_device(vid);
1402 	if (dev == NULL)
1403 		return;
1404 
1405 	if (vring_idx >= VHOST_MAX_VRING)
1406 		return;
1407 	vq = dev->virtqueue[vring_idx];
1408 	if (!vq)
1409 		return;
1410 
1411 	vhost_log_used_vring(dev, vq, offset, len);
1412 }
1413 
1414 uint32_t
rte_vhost_rx_queue_count(int vid,uint16_t qid)1415 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1416 {
1417 	struct virtio_net *dev;
1418 	struct vhost_virtqueue *vq;
1419 	uint32_t ret = 0;
1420 
1421 	dev = get_device(vid);
1422 	if (dev == NULL)
1423 		return 0;
1424 
1425 	if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1426 		VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1427 			dev->vid, __func__, qid);
1428 		return 0;
1429 	}
1430 
1431 	vq = dev->virtqueue[qid];
1432 	if (vq == NULL)
1433 		return 0;
1434 
1435 	rte_spinlock_lock(&vq->access_lock);
1436 
1437 	if (unlikely(vq->enabled == 0 || vq->avail == NULL))
1438 		goto out;
1439 
1440 	ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1441 
1442 out:
1443 	rte_spinlock_unlock(&vq->access_lock);
1444 	return ret;
1445 }
1446 
1447 struct rte_vdpa_device *
rte_vhost_get_vdpa_device(int vid)1448 rte_vhost_get_vdpa_device(int vid)
1449 {
1450 	struct virtio_net *dev = get_device(vid);
1451 
1452 	if (dev == NULL)
1453 		return NULL;
1454 
1455 	return dev->vdpa_dev;
1456 }
1457 
rte_vhost_get_log_base(int vid,uint64_t * log_base,uint64_t * log_size)1458 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1459 		uint64_t *log_size)
1460 {
1461 	struct virtio_net *dev = get_device(vid);
1462 
1463 	if (dev == NULL || log_base == NULL || log_size == NULL)
1464 		return -1;
1465 
1466 	*log_base = dev->log_base;
1467 	*log_size = dev->log_size;
1468 
1469 	return 0;
1470 }
1471 
rte_vhost_get_vring_base(int vid,uint16_t queue_id,uint16_t * last_avail_idx,uint16_t * last_used_idx)1472 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1473 		uint16_t *last_avail_idx, uint16_t *last_used_idx)
1474 {
1475 	struct vhost_virtqueue *vq;
1476 	struct virtio_net *dev = get_device(vid);
1477 
1478 	if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1479 		return -1;
1480 
1481 	if (queue_id >= VHOST_MAX_VRING)
1482 		return -1;
1483 
1484 	vq = dev->virtqueue[queue_id];
1485 	if (!vq)
1486 		return -1;
1487 
1488 	if (vq_is_packed(dev)) {
1489 		*last_avail_idx = (vq->avail_wrap_counter << 15) |
1490 				  vq->last_avail_idx;
1491 		*last_used_idx = (vq->used_wrap_counter << 15) |
1492 				 vq->last_used_idx;
1493 	} else {
1494 		*last_avail_idx = vq->last_avail_idx;
1495 		*last_used_idx = vq->last_used_idx;
1496 	}
1497 
1498 	return 0;
1499 }
1500 
rte_vhost_set_vring_base(int vid,uint16_t queue_id,uint16_t last_avail_idx,uint16_t last_used_idx)1501 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1502 		uint16_t last_avail_idx, uint16_t last_used_idx)
1503 {
1504 	struct vhost_virtqueue *vq;
1505 	struct virtio_net *dev = get_device(vid);
1506 
1507 	if (!dev)
1508 		return -1;
1509 
1510 	if (queue_id >= VHOST_MAX_VRING)
1511 		return -1;
1512 
1513 	vq = dev->virtqueue[queue_id];
1514 	if (!vq)
1515 		return -1;
1516 
1517 	if (vq_is_packed(dev)) {
1518 		vq->last_avail_idx = last_avail_idx & 0x7fff;
1519 		vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1520 		vq->last_used_idx = last_used_idx & 0x7fff;
1521 		vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1522 	} else {
1523 		vq->last_avail_idx = last_avail_idx;
1524 		vq->last_used_idx = last_used_idx;
1525 	}
1526 
1527 	return 0;
1528 }
1529 
1530 int
rte_vhost_get_vring_base_from_inflight(int vid,uint16_t queue_id,uint16_t * last_avail_idx,uint16_t * last_used_idx)1531 rte_vhost_get_vring_base_from_inflight(int vid,
1532 				       uint16_t queue_id,
1533 				       uint16_t *last_avail_idx,
1534 				       uint16_t *last_used_idx)
1535 {
1536 	struct rte_vhost_inflight_info_packed *inflight_info;
1537 	struct vhost_virtqueue *vq;
1538 	struct virtio_net *dev = get_device(vid);
1539 
1540 	if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1541 		return -1;
1542 
1543 	if (queue_id >= VHOST_MAX_VRING)
1544 		return -1;
1545 
1546 	vq = dev->virtqueue[queue_id];
1547 	if (!vq)
1548 		return -1;
1549 
1550 	if (!vq_is_packed(dev))
1551 		return -1;
1552 
1553 	inflight_info = vq->inflight_packed;
1554 	if (!inflight_info)
1555 		return -1;
1556 
1557 	*last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1558 			  inflight_info->old_used_idx;
1559 	*last_used_idx = *last_avail_idx;
1560 
1561 	return 0;
1562 }
1563 
rte_vhost_extern_callback_register(int vid,struct rte_vhost_user_extern_ops const * const ops,void * ctx)1564 int rte_vhost_extern_callback_register(int vid,
1565 		struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1566 {
1567 	struct virtio_net *dev = get_device(vid);
1568 
1569 	if (dev == NULL || ops == NULL)
1570 		return -1;
1571 
1572 	dev->extern_ops = *ops;
1573 	dev->extern_data = ctx;
1574 	return 0;
1575 }
1576 
rte_vhost_async_channel_register(int vid,uint16_t queue_id,uint32_t features,struct rte_vhost_async_channel_ops * ops)1577 int rte_vhost_async_channel_register(int vid, uint16_t queue_id,
1578 					uint32_t features,
1579 					struct rte_vhost_async_channel_ops *ops)
1580 {
1581 	struct vhost_virtqueue *vq;
1582 	struct virtio_net *dev = get_device(vid);
1583 	struct rte_vhost_async_features f;
1584 	int node;
1585 
1586 	if (dev == NULL || ops == NULL)
1587 		return -1;
1588 
1589 	f.intval = features;
1590 
1591 	if (queue_id >= VHOST_MAX_VRING)
1592 		return -1;
1593 
1594 	vq = dev->virtqueue[queue_id];
1595 
1596 	if (unlikely(vq == NULL || !dev->async_copy))
1597 		return -1;
1598 
1599 	/* packed queue is not supported */
1600 	if (unlikely(vq_is_packed(dev) || !f.async_inorder)) {
1601 		VHOST_LOG_CONFIG(ERR,
1602 			"async copy is not supported on packed queue or non-inorder mode "
1603 			"(vid %d, qid: %d)\n", vid, queue_id);
1604 		return -1;
1605 	}
1606 
1607 	if (unlikely(ops->check_completed_copies == NULL ||
1608 		ops->transfer_data == NULL))
1609 		return -1;
1610 
1611 	rte_spinlock_lock(&vq->access_lock);
1612 
1613 	if (unlikely(vq->async_registered)) {
1614 		VHOST_LOG_CONFIG(ERR,
1615 			"async register failed: channel already registered "
1616 			"(vid %d, qid: %d)\n", vid, queue_id);
1617 		goto reg_out;
1618 	}
1619 
1620 #ifdef RTE_LIBRTE_VHOST_NUMA
1621 	if (get_mempolicy(&node, NULL, 0, vq, MPOL_F_NODE | MPOL_F_ADDR)) {
1622 		VHOST_LOG_CONFIG(ERR,
1623 			"unable to get numa information in async register. "
1624 			"allocating async buffer memory on the caller thread node\n");
1625 		node = SOCKET_ID_ANY;
1626 	}
1627 #else
1628 	node = SOCKET_ID_ANY;
1629 #endif
1630 
1631 	vq->async_pkts_pending = rte_malloc_socket(NULL,
1632 			vq->size * sizeof(uintptr_t),
1633 			RTE_CACHE_LINE_SIZE, node);
1634 	vq->async_pkts_info = rte_malloc_socket(NULL,
1635 			vq->size * sizeof(struct async_inflight_info),
1636 			RTE_CACHE_LINE_SIZE, node);
1637 	vq->it_pool = rte_malloc_socket(NULL,
1638 			VHOST_MAX_ASYNC_IT * sizeof(struct rte_vhost_iov_iter),
1639 			RTE_CACHE_LINE_SIZE, node);
1640 	vq->vec_pool = rte_malloc_socket(NULL,
1641 			VHOST_MAX_ASYNC_VEC * sizeof(struct iovec),
1642 			RTE_CACHE_LINE_SIZE, node);
1643 	if (!vq->async_pkts_pending || !vq->async_pkts_info ||
1644 		!vq->it_pool || !vq->vec_pool) {
1645 		vhost_free_async_mem(vq);
1646 		VHOST_LOG_CONFIG(ERR,
1647 				"async register failed: cannot allocate memory for vq data "
1648 				"(vid %d, qid: %d)\n", vid, queue_id);
1649 		goto reg_out;
1650 	}
1651 
1652 	vq->async_ops.check_completed_copies = ops->check_completed_copies;
1653 	vq->async_ops.transfer_data = ops->transfer_data;
1654 
1655 	vq->async_inorder = f.async_inorder;
1656 	vq->async_threshold = f.async_threshold;
1657 
1658 	vq->async_registered = true;
1659 
1660 reg_out:
1661 	rte_spinlock_unlock(&vq->access_lock);
1662 
1663 	return 0;
1664 }
1665 
rte_vhost_async_channel_unregister(int vid,uint16_t queue_id)1666 int rte_vhost_async_channel_unregister(int vid, uint16_t queue_id)
1667 {
1668 	struct vhost_virtqueue *vq;
1669 	struct virtio_net *dev = get_device(vid);
1670 	int ret = -1;
1671 
1672 	if (dev == NULL)
1673 		return ret;
1674 
1675 	if (queue_id >= VHOST_MAX_VRING)
1676 		return ret;
1677 
1678 	vq = dev->virtqueue[queue_id];
1679 
1680 	if (vq == NULL)
1681 		return ret;
1682 
1683 	ret = 0;
1684 
1685 	if (!vq->async_registered)
1686 		return ret;
1687 
1688 	if (!rte_spinlock_trylock(&vq->access_lock)) {
1689 		VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. "
1690 			"virt queue busy.\n");
1691 		return -1;
1692 	}
1693 
1694 	if (vq->async_pkts_inflight_n) {
1695 		VHOST_LOG_CONFIG(ERR, "Failed to unregister async channel. "
1696 			"async inflight packets must be completed before unregistration.\n");
1697 		ret = -1;
1698 		goto out;
1699 	}
1700 
1701 	vhost_free_async_mem(vq);
1702 
1703 	vq->async_ops.transfer_data = NULL;
1704 	vq->async_ops.check_completed_copies = NULL;
1705 	vq->async_registered = false;
1706 
1707 out:
1708 	rte_spinlock_unlock(&vq->access_lock);
1709 
1710 	return ret;
1711 }
1712 
1713 RTE_LOG_REGISTER(vhost_config_log_level, lib.vhost.config, INFO);
1714 RTE_LOG_REGISTER(vhost_data_log_level, lib.vhost.data, WARNING);
1715