Branch data Line data Source code
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 <stdint.h>
8 : : #include <stdlib.h>
9 : : #include <pthread.h>
10 : : #ifdef RTE_LIBRTE_VHOST_NUMA
11 : : #include <numa.h>
12 : : #include <numaif.h>
13 : : #endif
14 : :
15 : : #include <eal_export.h>
16 : : #include <rte_errno.h>
17 : : #include <rte_log.h>
18 : : #include <rte_memory.h>
19 : : #include <rte_malloc.h>
20 : : #include <rte_vhost.h>
21 : :
22 : : #include "iotlb.h"
23 : : #include "vhost.h"
24 : : #include "vhost_user.h"
25 : :
26 : : struct virtio_net *vhost_devices[RTE_MAX_VHOST_DEVICE];
27 : : pthread_mutex_t vhost_dev_lock = PTHREAD_MUTEX_INITIALIZER;
28 : : pthread_mutex_t vhost_dma_lock = PTHREAD_MUTEX_INITIALIZER;
29 : :
30 : : struct vhost_vq_stats_name_off {
31 : : char name[RTE_VHOST_STATS_NAME_SIZE];
32 : : unsigned int offset;
33 : : };
34 : :
35 : : static const struct vhost_vq_stats_name_off vhost_vq_stat_strings[] = {
36 : : {"good_packets", offsetof(struct vhost_virtqueue, stats.packets)},
37 : : {"good_bytes", offsetof(struct vhost_virtqueue, stats.bytes)},
38 : : {"multicast_packets", offsetof(struct vhost_virtqueue, stats.multicast)},
39 : : {"broadcast_packets", offsetof(struct vhost_virtqueue, stats.broadcast)},
40 : : {"undersize_packets", offsetof(struct vhost_virtqueue, stats.size_bins[0])},
41 : : {"size_64_packets", offsetof(struct vhost_virtqueue, stats.size_bins[1])},
42 : : {"size_65_127_packets", offsetof(struct vhost_virtqueue, stats.size_bins[2])},
43 : : {"size_128_255_packets", offsetof(struct vhost_virtqueue, stats.size_bins[3])},
44 : : {"size_256_511_packets", offsetof(struct vhost_virtqueue, stats.size_bins[4])},
45 : : {"size_512_1023_packets", offsetof(struct vhost_virtqueue, stats.size_bins[5])},
46 : : {"size_1024_1518_packets", offsetof(struct vhost_virtqueue, stats.size_bins[6])},
47 : : {"size_1519_max_packets", offsetof(struct vhost_virtqueue, stats.size_bins[7])},
48 : : {"guest_notifications", offsetof(struct vhost_virtqueue, stats.guest_notifications)},
49 : : {"guest_notifications_offloaded", offsetof(struct vhost_virtqueue,
50 : : stats.guest_notifications_offloaded)},
51 : : {"guest_notifications_error", offsetof(struct vhost_virtqueue,
52 : : stats.guest_notifications_error)},
53 : : {"guest_notifications_suppressed", offsetof(struct vhost_virtqueue,
54 : : stats.guest_notifications_suppressed)},
55 : : {"iotlb_hits", offsetof(struct vhost_virtqueue, stats.iotlb_hits)},
56 : : {"iotlb_misses", offsetof(struct vhost_virtqueue, stats.iotlb_misses)},
57 : : {"inflight_submitted", offsetof(struct vhost_virtqueue, stats.inflight_submitted)},
58 : : {"inflight_completed", offsetof(struct vhost_virtqueue, stats.inflight_completed)},
59 : : {"mbuf_alloc_failed", offsetof(struct vhost_virtqueue, stats.mbuf_alloc_failed)},
60 : : };
61 : :
62 : : #define VHOST_NB_VQ_STATS RTE_DIM(vhost_vq_stat_strings)
63 : :
64 : : static int
65 : : vhost_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
66 : : {
67 : 0 : return dev->backend_ops->iotlb_miss(dev, iova, perm);
68 : : }
69 : :
70 : : uint64_t
71 : 0 : __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
72 : : uint64_t iova, uint64_t *size, uint8_t perm)
73 : : {
74 : : uint64_t vva, tmp_size;
75 : :
76 [ # # ]: 0 : if (unlikely(!*size))
77 : : return 0;
78 : :
79 : 0 : tmp_size = *size;
80 : :
81 : 0 : vva = vhost_user_iotlb_cache_find(dev, iova, &tmp_size, perm);
82 [ # # ]: 0 : if (tmp_size == *size) {
83 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
84 : 0 : vq->stats.iotlb_hits++;
85 : 0 : return vva;
86 : : }
87 : :
88 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
89 : 0 : vq->stats.iotlb_misses++;
90 : :
91 : 0 : iova += tmp_size;
92 : :
93 [ # # ]: 0 : if (!vhost_user_iotlb_pending_miss(dev, iova, perm)) {
94 : : /*
95 : : * iotlb_lock is read-locked for a full burst,
96 : : * but it only protects the iotlb cache.
97 : : * In case of IOTLB miss, we might block on the socket,
98 : : * which could cause a deadlock with QEMU if an IOTLB update
99 : : * is being handled. We can safely unlock here to avoid it.
100 : : */
101 : : vhost_user_iotlb_rd_unlock(vq);
102 : :
103 : 0 : vhost_user_iotlb_pending_insert(dev, iova, perm);
104 [ # # ]: 0 : if (vhost_iotlb_miss(dev, iova, perm)) {
105 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
106 : : "IOTLB miss req failed for IOVA 0x%" PRIx64,
107 : : iova);
108 : 0 : vhost_user_iotlb_pending_remove(dev, iova, 1, perm);
109 : : }
110 : :
111 : : vhost_user_iotlb_rd_lock(vq);
112 : : }
113 : :
114 : 0 : tmp_size = *size;
115 : : /* Retry in case of VDUSE, as it is synchronous */
116 : 0 : vva = vhost_user_iotlb_cache_find(dev, iova, &tmp_size, perm);
117 [ # # ]: 0 : if (tmp_size == *size)
118 : 0 : return vva;
119 : :
120 : : return 0;
121 : : }
122 : :
123 : : #define VHOST_LOG_PAGE 4096
124 : :
125 : : /*
126 : : * Atomically set a bit in memory.
127 : : */
128 : : static __rte_always_inline void
129 : : vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
130 : : {
131 : 0 : rte_atomic_fetch_or_explicit((volatile uint8_t __rte_atomic *)addr, (1U << nr),
132 : : rte_memory_order_relaxed);
133 : : }
134 : :
135 : : static __rte_always_inline void
136 : : vhost_log_page(uint8_t *log_base, uint64_t page)
137 : : {
138 : 0 : vhost_set_bit(page % 8, &log_base[page / 8]);
139 : : }
140 : :
141 : : void
142 : 0 : __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
143 : : {
144 : : uint64_t page;
145 : :
146 [ # # # # ]: 0 : if (unlikely(!dev->log_base || !len))
147 : : return;
148 : :
149 [ # # ]: 0 : if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
150 : : return;
151 : :
152 : : /* To make sure guest memory updates are committed before logging */
153 : : rte_atomic_thread_fence(rte_memory_order_release);
154 : :
155 : 0 : page = addr / VHOST_LOG_PAGE;
156 [ # # ]: 0 : while (page * VHOST_LOG_PAGE < addr + len) {
157 : 0 : vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
158 : 0 : page += 1;
159 : : }
160 : : }
161 : :
162 : : void
163 : 0 : __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
164 : : uint64_t iova, uint64_t len)
165 : : {
166 : : uint64_t hva, gpa, map_len;
167 : 0 : map_len = len;
168 : :
169 : 0 : hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
170 [ # # ]: 0 : if (map_len != len) {
171 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
172 : : "failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found",
173 : : iova);
174 : 0 : return;
175 : : }
176 : :
177 : : gpa = hva_to_gpa(dev, hva, len);
178 [ # # ]: 0 : if (gpa)
179 : 0 : __vhost_log_write(dev, gpa, len);
180 : : }
181 : :
182 : : void
183 : 0 : __vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
184 : : {
185 : : unsigned long *log_base;
186 : : int i;
187 : :
188 [ # # ]: 0 : if (unlikely(!dev->log_base))
189 : : return;
190 : :
191 : : /* No cache, nothing to sync */
192 [ # # ]: 0 : if (unlikely(!vq->log_cache))
193 : : return;
194 : :
195 : : rte_atomic_thread_fence(rte_memory_order_release);
196 : :
197 : 0 : log_base = (unsigned long *)(uintptr_t)dev->log_base;
198 : :
199 [ # # ]: 0 : for (i = 0; i < vq->log_cache_nb_elem; i++) {
200 : 0 : struct log_cache_entry *elem = vq->log_cache + i;
201 : :
202 : 0 : rte_atomic_fetch_or_explicit(
203 : : (unsigned long __rte_atomic *)(log_base + elem->offset),
204 : : elem->val, rte_memory_order_relaxed);
205 : : }
206 : :
207 : : rte_atomic_thread_fence(rte_memory_order_release);
208 : :
209 : 0 : vq->log_cache_nb_elem = 0;
210 : : }
211 : :
212 : : static __rte_always_inline void
213 : : vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
214 : : uint64_t page)
215 : : {
216 : 0 : uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
217 : 0 : uint32_t offset = page / (sizeof(unsigned long) << 3);
218 : : int i;
219 : :
220 [ # # ]: 0 : if (unlikely(!vq->log_cache)) {
221 : : /* No logging cache allocated, write dirty log map directly */
222 : : rte_atomic_thread_fence(rte_memory_order_release);
223 : 0 : vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
224 : :
225 : 0 : return;
226 : : }
227 : :
228 [ # # ]: 0 : for (i = 0; i < vq->log_cache_nb_elem; i++) {
229 : 0 : struct log_cache_entry *elem = vq->log_cache + i;
230 : :
231 [ # # ]: 0 : if (elem->offset == offset) {
232 : 0 : elem->val |= (1UL << bit_nr);
233 : 0 : return;
234 : : }
235 : : }
236 : :
237 [ # # ]: 0 : if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
238 : : /*
239 : : * No more room for a new log cache entry,
240 : : * so write the dirty log map directly.
241 : : */
242 : : rte_atomic_thread_fence(rte_memory_order_release);
243 : 0 : vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
244 : :
245 : 0 : return;
246 : : }
247 : :
248 : 0 : vq->log_cache[i].offset = offset;
249 : 0 : vq->log_cache[i].val = (1UL << bit_nr);
250 : 0 : vq->log_cache_nb_elem++;
251 : : }
252 : :
253 : : void
254 : 0 : __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
255 : : uint64_t addr, uint64_t len)
256 : : {
257 : : uint64_t page;
258 : :
259 [ # # # # ]: 0 : if (unlikely(!dev->log_base || !len))
260 : : return;
261 : :
262 [ # # ]: 0 : if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
263 : : return;
264 : :
265 : 0 : page = addr / VHOST_LOG_PAGE;
266 [ # # ]: 0 : while (page * VHOST_LOG_PAGE < addr + len) {
267 : : vhost_log_cache_page(dev, vq, page);
268 : 0 : page += 1;
269 : : }
270 : : }
271 : :
272 : : void
273 : 0 : __vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
274 : : uint64_t iova, uint64_t len)
275 : : {
276 : : uint64_t hva, gpa, map_len;
277 : 0 : map_len = len;
278 : :
279 : 0 : hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
280 [ # # ]: 0 : if (map_len != len) {
281 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
282 : : "failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found",
283 : : iova);
284 : 0 : return;
285 : : }
286 : :
287 : : gpa = hva_to_gpa(dev, hva, len);
288 [ # # ]: 0 : if (gpa)
289 : 0 : __vhost_log_cache_write(dev, vq, gpa, len);
290 : : }
291 : :
292 : : void *
293 : 0 : vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
294 : : uint64_t desc_addr, uint64_t desc_len)
295 : : {
296 : : void *idesc;
297 : : uint64_t src, dst;
298 : : uint64_t len, remain = desc_len;
299 : :
300 : 0 : idesc = rte_malloc_socket(__func__, desc_len, 0, vq->numa_node);
301 [ # # ]: 0 : if (unlikely(!idesc))
302 : : return NULL;
303 : :
304 : 0 : dst = (uint64_t)(uintptr_t)idesc;
305 : :
306 [ # # ]: 0 : while (remain) {
307 [ # # ]: 0 : len = remain;
308 : : src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
309 : : VHOST_ACCESS_RO);
310 [ # # # # ]: 0 : if (unlikely(!src || !len)) {
311 : 0 : rte_free(idesc);
312 : 0 : return NULL;
313 : : }
314 : :
315 [ # # ]: 0 : rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
316 : :
317 : 0 : remain -= len;
318 : 0 : dst += len;
319 : 0 : desc_addr += len;
320 : : }
321 : :
322 : : return idesc;
323 : : }
324 : :
325 : : void
326 : 0 : cleanup_vq(struct vhost_virtqueue *vq, int destroy)
327 : : {
328 [ # # # # ]: 0 : if ((vq->callfd >= 0) && (destroy != 0))
329 : 0 : close(vq->callfd);
330 [ # # ]: 0 : if (vq->kickfd >= 0)
331 : 0 : close(vq->kickfd);
332 : 0 : }
333 : :
334 : : void
335 : 0 : cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq)
336 : : {
337 [ # # ]: 0 : if (!(dev->protocol_features &
338 : : (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
339 : : return;
340 : :
341 [ # # ]: 0 : if (vq_is_packed(dev)) {
342 [ # # ]: 0 : if (vq->inflight_packed)
343 : 0 : vq->inflight_packed = NULL;
344 : : } else {
345 [ # # ]: 0 : if (vq->inflight_split)
346 : 0 : vq->inflight_split = NULL;
347 : : }
348 : :
349 [ # # ]: 0 : if (vq->resubmit_inflight) {
350 [ # # ]: 0 : if (vq->resubmit_inflight->resubmit_list) {
351 : 0 : rte_free(vq->resubmit_inflight->resubmit_list);
352 : 0 : vq->resubmit_inflight->resubmit_list = NULL;
353 : : }
354 : 0 : rte_free(vq->resubmit_inflight);
355 : 0 : vq->resubmit_inflight = NULL;
356 : : }
357 : : }
358 : :
359 : : /*
360 : : * Unmap any memory, close any file descriptors and
361 : : * free any memory owned by a device.
362 : : */
363 : : void
364 : 0 : cleanup_device(struct virtio_net *dev, int destroy)
365 : : {
366 : : uint32_t i;
367 : :
368 : 0 : vhost_backend_cleanup(dev);
369 : :
370 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
371 : 0 : cleanup_vq(dev->virtqueue[i], destroy);
372 : 0 : cleanup_vq_inflight(dev, dev->virtqueue[i]);
373 : : }
374 : 0 : }
375 : :
376 : : static void
377 : 0 : vhost_free_async_mem(struct vhost_virtqueue *vq)
378 : : __rte_requires_capability(&vq->access_lock)
379 : : {
380 [ # # ]: 0 : if (!vq->async)
381 : : return;
382 : :
383 : 0 : rte_free(vq->async->pkts_info);
384 : 0 : rte_free(vq->async->pkts_cmpl_flag);
385 : :
386 : 0 : rte_free(vq->async->buffers_packed);
387 : 0 : vq->async->buffers_packed = NULL;
388 : 0 : rte_free(vq->async->descs_split);
389 : 0 : vq->async->descs_split = NULL;
390 : :
391 : 0 : rte_free(vq->async);
392 : 0 : vq->async = NULL;
393 : : }
394 : :
395 : : void
396 [ # # ]: 0 : free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
397 : : {
398 [ # # ]: 0 : if (vq_is_packed(dev))
399 : 0 : rte_free(vq->shadow_used_packed);
400 : : else
401 : 0 : rte_free(vq->shadow_used_split);
402 : :
403 : 0 : rte_rwlock_write_lock(&vq->access_lock);
404 : 0 : vhost_free_async_mem(vq);
405 : : rte_rwlock_write_unlock(&vq->access_lock);
406 : 0 : rte_free(vq->batch_copy_elems);
407 : 0 : rte_free(vq->log_cache);
408 : 0 : rte_free(vq);
409 : 0 : }
410 : :
411 : : /*
412 : : * Release virtqueues and device memory.
413 : : */
414 : : static void
415 : 0 : free_device(struct virtio_net *dev)
416 : : {
417 : : uint32_t i;
418 : :
419 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++)
420 : 0 : free_vq(dev, dev->virtqueue[i]);
421 : :
422 : 0 : rte_free(dev);
423 : 0 : }
424 : :
425 : : static __rte_always_inline int
426 : : log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
427 : : __rte_requires_shared_capability(&vq->iotlb_lock)
428 : : {
429 [ # # ]: 0 : if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
430 : : return 0;
431 : :
432 : 0 : vq->log_guest_addr = translate_log_addr(dev, vq,
433 : 0 : vq->ring_addrs.log_guest_addr);
434 [ # # ]: 0 : if (vq->log_guest_addr == 0)
435 : : return -1;
436 : :
437 : : return 0;
438 : : }
439 : :
440 : : /*
441 : : * Converts vring log address to GPA
442 : : * If IOMMU is enabled, the log address is IOVA
443 : : * If IOMMU not enabled, the log address is already GPA
444 : : */
445 : : uint64_t
446 : 0 : translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
447 : : uint64_t log_addr)
448 : : {
449 [ # # ]: 0 : if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
450 : : const uint64_t exp_size = sizeof(uint64_t);
451 : : uint64_t hva, gpa;
452 : 0 : uint64_t size = exp_size;
453 : :
454 : : hva = vhost_iova_to_vva(dev, vq, log_addr,
455 : : &size, VHOST_ACCESS_RW);
456 : :
457 [ # # ]: 0 : if (size != exp_size)
458 : : return 0;
459 : :
460 : : gpa = hva_to_gpa(dev, hva, exp_size);
461 [ # # ]: 0 : if (!gpa) {
462 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
463 : : "failed to find GPA for log_addr: 0x%"
464 : : PRIx64 " hva: 0x%" PRIx64,
465 : : log_addr, hva);
466 : 0 : return 0;
467 : : }
468 : : return gpa;
469 : :
470 : : } else
471 : : return log_addr;
472 : : }
473 : :
474 : : static int
475 : 0 : vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
476 : : __rte_requires_shared_capability(&vq->iotlb_lock)
477 : : {
478 : : uint64_t req_size, size;
479 : :
480 : 0 : req_size = sizeof(struct vring_desc) * vq->size;
481 : 0 : size = req_size;
482 : 0 : vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
483 [ # # ]: 0 : vq->ring_addrs.desc_user_addr,
484 : : &size, VHOST_ACCESS_RO);
485 [ # # # # ]: 0 : if (!vq->desc || size != req_size)
486 : : return -1;
487 : :
488 : : req_size = sizeof(struct vring_avail);
489 : 0 : req_size += sizeof(uint16_t) * vq->size;
490 [ # # ]: 0 : if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
491 : 0 : req_size += sizeof(uint16_t);
492 : 0 : size = req_size;
493 : 0 : vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
494 [ # # ]: 0 : vq->ring_addrs.avail_user_addr,
495 : : &size, VHOST_ACCESS_RO);
496 [ # # # # ]: 0 : if (!vq->avail || size != req_size)
497 : : return -1;
498 : :
499 : : req_size = sizeof(struct vring_used);
500 : 0 : req_size += sizeof(struct vring_used_elem) * vq->size;
501 [ # # ]: 0 : if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
502 : 0 : req_size += sizeof(uint16_t);
503 : 0 : size = req_size;
504 : 0 : vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
505 [ # # ]: 0 : vq->ring_addrs.used_user_addr,
506 : : &size, VHOST_ACCESS_RW);
507 [ # # # # ]: 0 : if (!vq->used || size != req_size)
508 : 0 : return -1;
509 : :
510 : : return 0;
511 : : }
512 : :
513 : : static int
514 : 0 : vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
515 : : __rte_requires_shared_capability(&vq->iotlb_lock)
516 : : {
517 : : uint64_t req_size, size;
518 : :
519 : 0 : req_size = sizeof(struct vring_packed_desc) * vq->size;
520 : 0 : size = req_size;
521 : 0 : vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
522 [ # # ]: 0 : vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
523 : : &size, VHOST_ACCESS_RW);
524 [ # # # # ]: 0 : if (!vq->desc_packed || size != req_size)
525 : : return -1;
526 : :
527 : : req_size = sizeof(struct vring_packed_desc_event);
528 : 0 : size = req_size;
529 : 0 : vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
530 [ # # ]: 0 : vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
531 : : &size, VHOST_ACCESS_RO);
532 [ # # # # ]: 0 : if (!vq->driver_event || size != req_size)
533 : : return -1;
534 : :
535 : : req_size = sizeof(struct vring_packed_desc_event);
536 : 0 : size = req_size;
537 : 0 : vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
538 [ # # ]: 0 : vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
539 : : &size, VHOST_ACCESS_RW);
540 [ # # # # ]: 0 : if (!vq->device_event || size != req_size)
541 : 0 : return -1;
542 : :
543 : : return 0;
544 : : }
545 : :
546 : : int
547 : 0 : vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
548 : : {
549 : :
550 [ # # ]: 0 : if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
551 : : return -1;
552 : :
553 [ # # ]: 0 : if (vq_is_packed(dev)) {
554 [ # # ]: 0 : if (vring_translate_packed(dev, vq) < 0)
555 : : return -1;
556 : : } else {
557 [ # # ]: 0 : if (vring_translate_split(dev, vq) < 0)
558 : : return -1;
559 : : }
560 : :
561 : : if (log_translate(dev, vq) < 0)
562 : : return -1;
563 : :
564 : 0 : vq->access_ok = true;
565 : :
566 : 0 : return 0;
567 : : }
568 : :
569 : : void
570 : 0 : vring_invalidate(struct virtio_net *dev __rte_unused, struct vhost_virtqueue *vq)
571 : : {
572 : : vhost_user_iotlb_wr_lock(vq);
573 : :
574 : 0 : vq->access_ok = false;
575 : 0 : vq->desc = NULL;
576 : 0 : vq->avail = NULL;
577 : 0 : vq->used = NULL;
578 : 0 : vq->log_guest_addr = 0;
579 : :
580 : : vhost_user_iotlb_wr_unlock(vq);
581 : 0 : }
582 : :
583 : : static void
584 : 0 : init_vring_queue(struct virtio_net *dev __rte_unused, struct vhost_virtqueue *vq,
585 : : uint32_t vring_idx)
586 : : {
587 : 0 : int numa_node = SOCKET_ID_ANY;
588 : :
589 : : memset(vq, 0, sizeof(struct vhost_virtqueue));
590 : :
591 : 0 : vq->index = vring_idx;
592 : 0 : vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
593 : 0 : vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
594 : 0 : vq->notif_enable = VIRTIO_UNINITIALIZED_NOTIF;
595 : :
596 : : #ifdef RTE_LIBRTE_VHOST_NUMA
597 [ # # ]: 0 : if (get_mempolicy(&numa_node, NULL, 0, vq, MPOL_F_NODE | MPOL_F_ADDR)) {
598 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to query numa node: %s",
599 : : rte_strerror(errno));
600 : 0 : numa_node = SOCKET_ID_ANY;
601 : : }
602 : : #endif
603 : 0 : vq->numa_node = numa_node;
604 : 0 : }
605 : :
606 : : static void
607 : : reset_vring_queue(struct virtio_net *dev, struct vhost_virtqueue *vq)
608 : : {
609 : : int callfd;
610 : :
611 : 0 : callfd = vq->callfd;
612 : 0 : init_vring_queue(dev, vq, vq->index);
613 : 0 : vq->callfd = callfd;
614 : 0 : }
615 : :
616 : : int
617 : 0 : alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
618 : : {
619 : : struct vhost_virtqueue *vq;
620 : : uint32_t i;
621 : :
622 : : /* Also allocate holes, if any, up to requested vring index. */
623 [ # # ]: 0 : for (i = 0; i <= vring_idx; i++) {
624 [ # # ]: 0 : if (dev->virtqueue[i])
625 : 0 : continue;
626 : :
627 : 0 : vq = rte_zmalloc(NULL, sizeof(struct vhost_virtqueue), 0);
628 [ # # ]: 0 : if (vq == NULL) {
629 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
630 : : "failed to allocate memory for vring %u.",
631 : : i);
632 : 0 : return -1;
633 : : }
634 : :
635 : 0 : dev->virtqueue[i] = vq;
636 : 0 : init_vring_queue(dev, vq, i);
637 : : rte_rwlock_init(&vq->access_lock);
638 : : rte_rwlock_init(&vq->iotlb_lock);
639 : 0 : vq->avail_wrap_counter = 1;
640 : 0 : vq->used_wrap_counter = 1;
641 : 0 : vq->signalled_used_valid = false;
642 : : }
643 : :
644 : 0 : dev->nr_vring = RTE_MAX(dev->nr_vring, vring_idx + 1);
645 : :
646 : 0 : return 0;
647 : : }
648 : :
649 : : /*
650 : : * Reset some variables in device structure, while keeping few
651 : : * others untouched, such as vid, ifname, nr_vring: they
652 : : * should be same unless the device is removed.
653 : : */
654 : : void
655 : 0 : reset_device(struct virtio_net *dev)
656 : : {
657 : : uint32_t i;
658 : :
659 : 0 : dev->features = 0;
660 : 0 : dev->protocol_features = 0;
661 : 0 : dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
662 : :
663 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
664 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
665 : :
666 [ # # ]: 0 : if (!vq) {
667 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
668 : : "failed to reset vring, virtqueue not allocated (%d)", i);
669 : 0 : continue;
670 : : }
671 : : reset_vring_queue(dev, vq);
672 : : }
673 : 0 : }
674 : :
675 : : /*
676 : : * Invoked when there is a new vhost-user connection established (when
677 : : * there is a new virtio device being attached).
678 : : */
679 : : int
680 : 0 : vhost_new_device(struct vhost_backend_ops *ops)
681 : : {
682 : : struct virtio_net *dev;
683 : : int i;
684 : :
685 [ # # ]: 0 : if (ops == NULL) {
686 : 0 : VHOST_CONFIG_LOG("device", ERR, "missing backend ops.");
687 : 0 : return -1;
688 : : }
689 : :
690 [ # # ]: 0 : if (ops->iotlb_miss == NULL) {
691 : 0 : VHOST_CONFIG_LOG("device", ERR, "missing IOTLB miss backend op.");
692 : 0 : return -1;
693 : : }
694 : :
695 [ # # ]: 0 : if (ops->inject_irq == NULL) {
696 : 0 : VHOST_CONFIG_LOG("device", ERR, "missing IRQ injection backend op.");
697 : 0 : return -1;
698 : : }
699 : :
700 : 0 : pthread_mutex_lock(&vhost_dev_lock);
701 [ # # ]: 0 : for (i = 0; i < RTE_MAX_VHOST_DEVICE; i++) {
702 [ # # ]: 0 : if (vhost_devices[i] == NULL)
703 : : break;
704 : : }
705 : :
706 [ # # ]: 0 : if (i == RTE_MAX_VHOST_DEVICE) {
707 : 0 : VHOST_CONFIG_LOG("device", ERR, "failed to find a free slot for new device.");
708 : 0 : pthread_mutex_unlock(&vhost_dev_lock);
709 : 0 : return -1;
710 : : }
711 : :
712 : 0 : dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
713 [ # # ]: 0 : if (dev == NULL) {
714 : 0 : VHOST_CONFIG_LOG("device", ERR, "failed to allocate memory for new device.");
715 : 0 : pthread_mutex_unlock(&vhost_dev_lock);
716 : 0 : return -1;
717 : : }
718 : :
719 : 0 : vhost_devices[i] = dev;
720 : 0 : pthread_mutex_unlock(&vhost_dev_lock);
721 : :
722 : 0 : dev->vid = i;
723 : 0 : dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
724 : 0 : dev->backend_req_fd = -1;
725 : 0 : dev->postcopy_ufd = -1;
726 : : rte_spinlock_init(&dev->backend_req_lock);
727 : 0 : dev->backend_ops = ops;
728 : :
729 : 0 : return i;
730 : : }
731 : :
732 : : void
733 : 0 : vhost_destroy_device_notify(struct virtio_net *dev)
734 : : {
735 : : struct rte_vdpa_device *vdpa_dev;
736 : :
737 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_RUNNING) {
738 : 0 : vdpa_dev = dev->vdpa_dev;
739 [ # # # # ]: 0 : if (vdpa_dev && vdpa_dev->ops->dev_close)
740 : 0 : vdpa_dev->ops->dev_close(dev->vid);
741 : 0 : dev->flags &= ~VIRTIO_DEV_RUNNING;
742 [ # # ]: 0 : if (dev->notify_ops->destroy_device)
743 : 0 : dev->notify_ops->destroy_device(dev->vid);
744 : : }
745 : 0 : }
746 : :
747 : : /*
748 : : * Invoked when there is the vhost-user connection is broken (when
749 : : * the virtio device is being detached).
750 : : */
751 : : void
752 [ # # ]: 0 : vhost_destroy_device(int vid)
753 : : {
754 : : struct virtio_net *dev = get_device(vid);
755 : :
756 [ # # ]: 0 : if (dev == NULL)
757 : : return;
758 : :
759 : 0 : vhost_destroy_device_notify(dev);
760 : :
761 : 0 : cleanup_device(dev, 1);
762 : 0 : free_device(dev);
763 : :
764 : 0 : vhost_devices[vid] = NULL;
765 : : }
766 : :
767 : : void
768 [ # # ]: 0 : vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
769 : : {
770 : : struct virtio_net *dev = get_device(vid);
771 : :
772 [ # # ]: 0 : if (dev == NULL)
773 : : return;
774 : :
775 : 0 : dev->vdpa_dev = vdpa_dev;
776 : : }
777 : :
778 : : void
779 [ # # ]: 0 : vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
780 : : {
781 : : struct virtio_net *dev;
782 : : unsigned int len;
783 : :
784 : : dev = get_device(vid);
785 [ # # ]: 0 : if (dev == NULL)
786 : : return;
787 : :
788 : 0 : len = if_len > sizeof(dev->ifname) ?
789 : : sizeof(dev->ifname) : if_len;
790 : :
791 : 0 : strncpy(dev->ifname, if_name, len);
792 : 0 : dev->ifname[sizeof(dev->ifname) - 1] = '\0';
793 : : }
794 : :
795 : : void
796 [ # # ]: 0 : vhost_setup_virtio_net(int vid, bool enable, bool compliant_ol_flags, bool stats_enabled,
797 : : bool support_iommu)
798 : : {
799 : : struct virtio_net *dev = get_device(vid);
800 : :
801 [ # # ]: 0 : if (dev == NULL)
802 : : return;
803 : :
804 [ # # ]: 0 : if (enable)
805 : 0 : dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
806 : : else
807 : 0 : dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
808 [ # # ]: 0 : if (!compliant_ol_flags)
809 : 0 : dev->flags |= VIRTIO_DEV_LEGACY_OL_FLAGS;
810 : : else
811 : 0 : dev->flags &= ~VIRTIO_DEV_LEGACY_OL_FLAGS;
812 [ # # ]: 0 : if (stats_enabled)
813 : 0 : dev->flags |= VIRTIO_DEV_STATS_ENABLED;
814 : : else
815 : 0 : dev->flags &= ~VIRTIO_DEV_STATS_ENABLED;
816 [ # # ]: 0 : if (support_iommu)
817 : 0 : dev->flags |= VIRTIO_DEV_SUPPORT_IOMMU;
818 : : else
819 : 0 : dev->flags &= ~VIRTIO_DEV_SUPPORT_IOMMU;
820 : :
821 [ # # ]: 0 : if (vhost_user_iotlb_init(dev) < 0)
822 : 0 : VHOST_CONFIG_LOG("device", ERR, "failed to init IOTLB");
823 : :
824 : : }
825 : :
826 : : void
827 [ # # ]: 0 : vhost_enable_extbuf(int vid)
828 : : {
829 : : struct virtio_net *dev = get_device(vid);
830 : :
831 [ # # ]: 0 : if (dev == NULL)
832 : : return;
833 : :
834 : 0 : dev->extbuf = 1;
835 : : }
836 : :
837 : : void
838 [ # # ]: 0 : vhost_enable_linearbuf(int vid)
839 : : {
840 : : struct virtio_net *dev = get_device(vid);
841 : :
842 [ # # ]: 0 : if (dev == NULL)
843 : : return;
844 : :
845 : 0 : dev->linearbuf = 1;
846 : : }
847 : :
848 : : RTE_EXPORT_SYMBOL(rte_vhost_get_mtu)
849 : : int
850 [ # # ]: 0 : rte_vhost_get_mtu(int vid, uint16_t *mtu)
851 : : {
852 : : struct virtio_net *dev = get_device(vid);
853 : :
854 [ # # ]: 0 : if (dev == NULL || mtu == NULL)
855 : : return -ENODEV;
856 : :
857 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_READY))
858 : : return -EAGAIN;
859 : :
860 [ # # ]: 0 : if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
861 : : return -ENOTSUP;
862 : :
863 : 0 : *mtu = dev->mtu;
864 : :
865 : 0 : return 0;
866 : : }
867 : :
868 : : RTE_EXPORT_SYMBOL(rte_vhost_get_numa_node)
869 : : int
870 [ # # ]: 0 : rte_vhost_get_numa_node(int vid)
871 : : {
872 : : #ifdef RTE_LIBRTE_VHOST_NUMA
873 : : struct virtio_net *dev = get_device(vid);
874 : : int numa_node;
875 : : int ret;
876 : :
877 [ # # # # ]: 0 : if (dev == NULL || numa_available() != 0)
878 : 0 : return -1;
879 : :
880 : 0 : ret = get_mempolicy(&numa_node, NULL, 0, dev,
881 : : MPOL_F_NODE | MPOL_F_ADDR);
882 [ # # ]: 0 : if (ret < 0) {
883 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to query numa node: %s",
884 : : rte_strerror(errno));
885 : 0 : return -1;
886 : : }
887 : :
888 : 0 : return numa_node;
889 : : #else
890 : : RTE_SET_USED(vid);
891 : : return -1;
892 : : #endif
893 : : }
894 : :
895 : : RTE_EXPORT_SYMBOL(rte_vhost_get_vring_num)
896 : : uint16_t
897 [ # # ]: 0 : rte_vhost_get_vring_num(int vid)
898 : : {
899 : : struct virtio_net *dev = get_device(vid);
900 : :
901 [ # # ]: 0 : if (dev == NULL)
902 : : return 0;
903 : :
904 : 0 : return dev->nr_vring;
905 : : }
906 : :
907 : : RTE_EXPORT_SYMBOL(rte_vhost_get_ifname)
908 : : int
909 [ # # ]: 0 : rte_vhost_get_ifname(int vid, char *buf, size_t len)
910 : : {
911 : : struct virtio_net *dev = get_device(vid);
912 : :
913 [ # # ]: 0 : if (dev == NULL || buf == NULL)
914 : : return -1;
915 : :
916 : 0 : len = RTE_MIN(len, sizeof(dev->ifname));
917 : :
918 : 0 : strncpy(buf, dev->ifname, len);
919 : 0 : buf[len - 1] = '\0';
920 : :
921 : 0 : return 0;
922 : : }
923 : :
924 : : RTE_EXPORT_SYMBOL(rte_vhost_get_negotiated_features)
925 : : int
926 [ # # ]: 0 : rte_vhost_get_negotiated_features(int vid, uint64_t *features)
927 : : {
928 : : struct virtio_net *dev;
929 : :
930 : : dev = get_device(vid);
931 [ # # ]: 0 : if (dev == NULL || features == NULL)
932 : : return -1;
933 : :
934 : 0 : *features = dev->features;
935 : 0 : return 0;
936 : : }
937 : :
938 : : RTE_EXPORT_SYMBOL(rte_vhost_get_negotiated_protocol_features)
939 : : int
940 [ # # ]: 0 : rte_vhost_get_negotiated_protocol_features(int vid,
941 : : uint64_t *protocol_features)
942 : : {
943 : : struct virtio_net *dev;
944 : :
945 : : dev = get_device(vid);
946 [ # # ]: 0 : if (dev == NULL || protocol_features == NULL)
947 : : return -1;
948 : :
949 : 0 : *protocol_features = dev->protocol_features;
950 : 0 : return 0;
951 : : }
952 : :
953 : : RTE_EXPORT_SYMBOL(rte_vhost_get_mem_table)
954 : : int
955 [ # # ]: 0 : rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
956 : : {
957 : : struct virtio_net *dev;
958 : : struct rte_vhost_memory *m;
959 : : size_t size;
960 : :
961 : : dev = get_device(vid);
962 [ # # ]: 0 : if (dev == NULL || mem == NULL)
963 : : return -1;
964 : :
965 : 0 : size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
966 : 0 : m = malloc(sizeof(struct rte_vhost_memory) + size);
967 [ # # ]: 0 : if (!m)
968 : : return -1;
969 : :
970 : 0 : m->nregions = dev->mem->nregions;
971 : 0 : memcpy(m->regions, dev->mem->regions, size);
972 : 0 : *mem = m;
973 : :
974 : 0 : return 0;
975 : : }
976 : :
977 : : RTE_EXPORT_SYMBOL(rte_vhost_get_vhost_vring)
978 : : int
979 [ # # ]: 0 : rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
980 : : struct rte_vhost_vring *vring)
981 : : {
982 : : struct virtio_net *dev;
983 : : struct vhost_virtqueue *vq;
984 : :
985 : : dev = get_device(vid);
986 [ # # ]: 0 : if (dev == NULL || vring == NULL)
987 : : return -1;
988 : :
989 [ # # ]: 0 : if (vring_idx >= VHOST_MAX_VRING)
990 : : return -1;
991 : :
992 : 0 : vq = dev->virtqueue[vring_idx];
993 [ # # ]: 0 : if (!vq)
994 : : return -1;
995 : :
996 [ # # ]: 0 : if (vq_is_packed(dev)) {
997 : 0 : vring->desc_packed = vq->desc_packed;
998 : 0 : vring->driver_event = vq->driver_event;
999 : 0 : vring->device_event = vq->device_event;
1000 : : } else {
1001 : 0 : vring->desc = vq->desc;
1002 : 0 : vring->avail = vq->avail;
1003 : 0 : vring->used = vq->used;
1004 : : }
1005 : 0 : vring->log_guest_addr = vq->log_guest_addr;
1006 : :
1007 : 0 : vring->callfd = vq->callfd;
1008 : 0 : vring->kickfd = vq->kickfd;
1009 : 0 : vring->size = vq->size;
1010 : :
1011 : 0 : return 0;
1012 : : }
1013 : :
1014 : : RTE_EXPORT_SYMBOL(rte_vhost_get_vhost_ring_inflight)
1015 : : int
1016 [ # # ]: 0 : rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
1017 : : struct rte_vhost_ring_inflight *vring)
1018 : : {
1019 : : struct virtio_net *dev;
1020 : : struct vhost_virtqueue *vq;
1021 : :
1022 : : dev = get_device(vid);
1023 [ # # ]: 0 : if (unlikely(!dev))
1024 : : return -1;
1025 : :
1026 [ # # ]: 0 : if (vring_idx >= VHOST_MAX_VRING)
1027 : : return -1;
1028 : :
1029 : 0 : vq = dev->virtqueue[vring_idx];
1030 [ # # ]: 0 : if (unlikely(!vq))
1031 : : return -1;
1032 : :
1033 [ # # ]: 0 : if (vq_is_packed(dev)) {
1034 [ # # ]: 0 : if (unlikely(!vq->inflight_packed))
1035 : : return -1;
1036 : :
1037 : 0 : vring->inflight_packed = vq->inflight_packed;
1038 : : } else {
1039 [ # # ]: 0 : if (unlikely(!vq->inflight_split))
1040 : : return -1;
1041 : :
1042 : 0 : vring->inflight_split = vq->inflight_split;
1043 : : }
1044 : :
1045 : 0 : vring->resubmit_inflight = vq->resubmit_inflight;
1046 : :
1047 : 0 : return 0;
1048 : : }
1049 : :
1050 : : RTE_EXPORT_SYMBOL(rte_vhost_set_inflight_desc_split)
1051 : : int
1052 [ # # ]: 0 : rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
1053 : : uint16_t idx)
1054 : : {
1055 : : struct vhost_virtqueue *vq;
1056 : : struct virtio_net *dev;
1057 : :
1058 : : dev = get_device(vid);
1059 [ # # ]: 0 : if (unlikely(!dev))
1060 : : return -1;
1061 : :
1062 [ # # ]: 0 : if (unlikely(!(dev->protocol_features &
1063 : : (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1064 : : return 0;
1065 : :
1066 [ # # ]: 0 : if (unlikely(vq_is_packed(dev)))
1067 : : return -1;
1068 : :
1069 [ # # ]: 0 : if (unlikely(vring_idx >= VHOST_MAX_VRING))
1070 : : return -1;
1071 : :
1072 : 0 : vq = dev->virtqueue[vring_idx];
1073 [ # # ]: 0 : if (unlikely(!vq))
1074 : : return -1;
1075 : :
1076 [ # # ]: 0 : if (unlikely(!vq->inflight_split))
1077 : : return -1;
1078 : :
1079 [ # # ]: 0 : if (unlikely(idx >= vq->size))
1080 : : return -1;
1081 : :
1082 : 0 : vq->inflight_split->desc[idx].counter = vq->global_counter++;
1083 : 0 : vq->inflight_split->desc[idx].inflight = 1;
1084 : 0 : return 0;
1085 : : }
1086 : :
1087 : : RTE_EXPORT_SYMBOL(rte_vhost_set_inflight_desc_packed)
1088 : : int
1089 [ # # ]: 0 : rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
1090 : : uint16_t head, uint16_t last,
1091 : : uint16_t *inflight_entry)
1092 : : {
1093 : : struct rte_vhost_inflight_info_packed *inflight_info;
1094 : : struct virtio_net *dev;
1095 : : struct vhost_virtqueue *vq;
1096 : : struct vring_packed_desc *desc;
1097 : : uint16_t old_free_head, free_head;
1098 : :
1099 : : dev = get_device(vid);
1100 [ # # ]: 0 : if (unlikely(!dev))
1101 : : return -1;
1102 : :
1103 [ # # ]: 0 : if (unlikely(!(dev->protocol_features &
1104 : : (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1105 : : return 0;
1106 : :
1107 [ # # ]: 0 : if (unlikely(!vq_is_packed(dev)))
1108 : : return -1;
1109 : :
1110 [ # # ]: 0 : if (unlikely(vring_idx >= VHOST_MAX_VRING))
1111 : : return -1;
1112 : :
1113 : 0 : vq = dev->virtqueue[vring_idx];
1114 [ # # ]: 0 : if (unlikely(!vq))
1115 : : return -1;
1116 : :
1117 : 0 : inflight_info = vq->inflight_packed;
1118 [ # # ]: 0 : if (unlikely(!inflight_info))
1119 : : return -1;
1120 : :
1121 [ # # ]: 0 : if (unlikely(head >= vq->size))
1122 : : return -1;
1123 : :
1124 : 0 : desc = vq->desc_packed;
1125 : 0 : old_free_head = inflight_info->old_free_head;
1126 [ # # ]: 0 : if (unlikely(old_free_head >= vq->size))
1127 : : return -1;
1128 : :
1129 : : free_head = old_free_head;
1130 : :
1131 : : /* init header descriptor */
1132 : 0 : inflight_info->desc[old_free_head].num = 0;
1133 : 0 : inflight_info->desc[old_free_head].counter = vq->global_counter++;
1134 : 0 : inflight_info->desc[old_free_head].inflight = 1;
1135 : :
1136 : : /* save desc entry in flight entry */
1137 [ # # ]: 0 : while (head != ((last + 1) % vq->size)) {
1138 : 0 : inflight_info->desc[old_free_head].num++;
1139 : 0 : inflight_info->desc[free_head].addr = desc[head].addr;
1140 : 0 : inflight_info->desc[free_head].len = desc[head].len;
1141 : 0 : inflight_info->desc[free_head].flags = desc[head].flags;
1142 : 0 : inflight_info->desc[free_head].id = desc[head].id;
1143 : :
1144 : 0 : inflight_info->desc[old_free_head].last = free_head;
1145 : 0 : free_head = inflight_info->desc[free_head].next;
1146 : 0 : inflight_info->free_head = free_head;
1147 : 0 : head = (head + 1) % vq->size;
1148 : : }
1149 : :
1150 : 0 : inflight_info->old_free_head = free_head;
1151 : 0 : *inflight_entry = old_free_head;
1152 : :
1153 : 0 : return 0;
1154 : : }
1155 : :
1156 : : RTE_EXPORT_SYMBOL(rte_vhost_clr_inflight_desc_split)
1157 : : int
1158 [ # # ]: 0 : rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1159 : : uint16_t last_used_idx, uint16_t idx)
1160 : : {
1161 : : struct virtio_net *dev;
1162 : : struct vhost_virtqueue *vq;
1163 : :
1164 : : dev = get_device(vid);
1165 [ # # ]: 0 : if (unlikely(!dev))
1166 : : return -1;
1167 : :
1168 [ # # ]: 0 : if (unlikely(!(dev->protocol_features &
1169 : : (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1170 : : return 0;
1171 : :
1172 [ # # ]: 0 : if (unlikely(vq_is_packed(dev)))
1173 : : return -1;
1174 : :
1175 [ # # ]: 0 : if (unlikely(vring_idx >= VHOST_MAX_VRING))
1176 : : return -1;
1177 : :
1178 : 0 : vq = dev->virtqueue[vring_idx];
1179 [ # # ]: 0 : if (unlikely(!vq))
1180 : : return -1;
1181 : :
1182 [ # # ]: 0 : if (unlikely(!vq->inflight_split))
1183 : : return -1;
1184 : :
1185 [ # # ]: 0 : if (unlikely(idx >= vq->size))
1186 : : return -1;
1187 : :
1188 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
1189 : :
1190 : 0 : vq->inflight_split->desc[idx].inflight = 0;
1191 : :
1192 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
1193 : :
1194 : 0 : vq->inflight_split->used_idx = last_used_idx;
1195 : 0 : return 0;
1196 : : }
1197 : :
1198 : : RTE_EXPORT_SYMBOL(rte_vhost_clr_inflight_desc_packed)
1199 : : int
1200 [ # # ]: 0 : rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1201 : : uint16_t head)
1202 : : {
1203 : : struct rte_vhost_inflight_info_packed *inflight_info;
1204 : : struct virtio_net *dev;
1205 : : struct vhost_virtqueue *vq;
1206 : :
1207 : : dev = get_device(vid);
1208 [ # # ]: 0 : if (unlikely(!dev))
1209 : : return -1;
1210 : :
1211 [ # # ]: 0 : if (unlikely(!(dev->protocol_features &
1212 : : (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1213 : : return 0;
1214 : :
1215 [ # # ]: 0 : if (unlikely(!vq_is_packed(dev)))
1216 : : return -1;
1217 : :
1218 [ # # ]: 0 : if (unlikely(vring_idx >= VHOST_MAX_VRING))
1219 : : return -1;
1220 : :
1221 : 0 : vq = dev->virtqueue[vring_idx];
1222 [ # # ]: 0 : if (unlikely(!vq))
1223 : : return -1;
1224 : :
1225 : 0 : inflight_info = vq->inflight_packed;
1226 [ # # ]: 0 : if (unlikely(!inflight_info))
1227 : : return -1;
1228 : :
1229 [ # # ]: 0 : if (unlikely(head >= vq->size))
1230 : : return -1;
1231 : :
1232 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
1233 : :
1234 : 0 : inflight_info->desc[head].inflight = 0;
1235 : :
1236 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
1237 : :
1238 : 0 : inflight_info->old_free_head = inflight_info->free_head;
1239 : 0 : inflight_info->old_used_idx = inflight_info->used_idx;
1240 : 0 : inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1241 : :
1242 : 0 : return 0;
1243 : : }
1244 : :
1245 : : RTE_EXPORT_SYMBOL(rte_vhost_set_last_inflight_io_split)
1246 : : int
1247 [ # # ]: 0 : rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1248 : : uint16_t idx)
1249 : : {
1250 : : struct virtio_net *dev;
1251 : : struct vhost_virtqueue *vq;
1252 : :
1253 : : dev = get_device(vid);
1254 [ # # ]: 0 : if (unlikely(!dev))
1255 : : return -1;
1256 : :
1257 [ # # ]: 0 : if (unlikely(!(dev->protocol_features &
1258 : : (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1259 : : return 0;
1260 : :
1261 [ # # ]: 0 : if (unlikely(vq_is_packed(dev)))
1262 : : return -1;
1263 : :
1264 [ # # ]: 0 : if (unlikely(vring_idx >= VHOST_MAX_VRING))
1265 : : return -1;
1266 : :
1267 : 0 : vq = dev->virtqueue[vring_idx];
1268 [ # # ]: 0 : if (unlikely(!vq))
1269 : : return -1;
1270 : :
1271 [ # # ]: 0 : if (unlikely(!vq->inflight_split))
1272 : : return -1;
1273 : :
1274 [ # # ]: 0 : if (unlikely(idx >= vq->size))
1275 : : return -1;
1276 : :
1277 : 0 : vq->inflight_split->last_inflight_io = idx;
1278 : 0 : return 0;
1279 : : }
1280 : :
1281 : : RTE_EXPORT_SYMBOL(rte_vhost_set_last_inflight_io_packed)
1282 : : int
1283 [ # # ]: 0 : rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1284 : : uint16_t head)
1285 : : {
1286 : : struct rte_vhost_inflight_info_packed *inflight_info;
1287 : : struct virtio_net *dev;
1288 : : struct vhost_virtqueue *vq;
1289 : : uint16_t last;
1290 : :
1291 : : dev = get_device(vid);
1292 [ # # ]: 0 : if (unlikely(!dev))
1293 : : return -1;
1294 : :
1295 [ # # ]: 0 : if (unlikely(!(dev->protocol_features &
1296 : : (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1297 : : return 0;
1298 : :
1299 [ # # ]: 0 : if (unlikely(!vq_is_packed(dev)))
1300 : : return -1;
1301 : :
1302 [ # # ]: 0 : if (unlikely(vring_idx >= VHOST_MAX_VRING))
1303 : : return -1;
1304 : :
1305 : 0 : vq = dev->virtqueue[vring_idx];
1306 [ # # ]: 0 : if (unlikely(!vq))
1307 : : return -1;
1308 : :
1309 : 0 : inflight_info = vq->inflight_packed;
1310 [ # # ]: 0 : if (unlikely(!inflight_info))
1311 : : return -1;
1312 : :
1313 [ # # ]: 0 : if (unlikely(head >= vq->size))
1314 : : return -1;
1315 : :
1316 : 0 : last = inflight_info->desc[head].last;
1317 [ # # ]: 0 : if (unlikely(last >= vq->size))
1318 : : return -1;
1319 : :
1320 : 0 : inflight_info->desc[last].next = inflight_info->free_head;
1321 : 0 : inflight_info->free_head = head;
1322 : 0 : inflight_info->used_idx += inflight_info->desc[head].num;
1323 [ # # ]: 0 : if (inflight_info->used_idx >= inflight_info->desc_num) {
1324 : 0 : inflight_info->used_idx -= inflight_info->desc_num;
1325 : 0 : inflight_info->used_wrap_counter =
1326 : 0 : !inflight_info->used_wrap_counter;
1327 : : }
1328 : :
1329 : : return 0;
1330 : : }
1331 : :
1332 : : RTE_EXPORT_SYMBOL(rte_vhost_vring_call)
1333 : : int
1334 [ # # ]: 0 : rte_vhost_vring_call(int vid, uint16_t vring_idx)
1335 : : {
1336 : : struct virtio_net *dev;
1337 : : struct vhost_virtqueue *vq;
1338 : : int ret = 0;
1339 : :
1340 : : dev = get_device(vid);
1341 [ # # ]: 0 : if (!dev)
1342 : : return -1;
1343 : :
1344 [ # # ]: 0 : if (vring_idx >= VHOST_MAX_VRING)
1345 : : return -1;
1346 : :
1347 : 0 : vq = dev->virtqueue[vring_idx];
1348 [ # # ]: 0 : if (!vq)
1349 : : return -1;
1350 : :
1351 : 0 : rte_rwlock_read_lock(&vq->access_lock);
1352 : :
1353 [ # # ]: 0 : if (unlikely(!vq->access_ok)) {
1354 : : ret = -1;
1355 : 0 : goto out_unlock;
1356 : : }
1357 : :
1358 [ # # ]: 0 : if (vq_is_packed(dev))
1359 : : vhost_vring_call_packed(dev, vq);
1360 : : else
1361 : : vhost_vring_call_split(dev, vq);
1362 : :
1363 : 0 : out_unlock:
1364 : : rte_rwlock_read_unlock(&vq->access_lock);
1365 : :
1366 : 0 : return ret;
1367 : : }
1368 : :
1369 : : RTE_EXPORT_SYMBOL(rte_vhost_vring_call_nonblock)
1370 : : int
1371 [ # # ]: 0 : rte_vhost_vring_call_nonblock(int vid, uint16_t vring_idx)
1372 : : {
1373 : : struct virtio_net *dev;
1374 : : struct vhost_virtqueue *vq;
1375 : : int ret = 0;
1376 : :
1377 : : dev = get_device(vid);
1378 [ # # ]: 0 : if (!dev)
1379 : : return -1;
1380 : :
1381 [ # # ]: 0 : if (vring_idx >= VHOST_MAX_VRING)
1382 : : return -1;
1383 : :
1384 : 0 : vq = dev->virtqueue[vring_idx];
1385 [ # # ]: 0 : if (!vq)
1386 : : return -1;
1387 : :
1388 [ # # ]: 0 : if (rte_rwlock_read_trylock(&vq->access_lock))
1389 : : return -EAGAIN;
1390 : :
1391 [ # # ]: 0 : if (unlikely(!vq->access_ok)) {
1392 : : ret = -1;
1393 : 0 : goto out_unlock;
1394 : : }
1395 : :
1396 [ # # ]: 0 : if (vq_is_packed(dev))
1397 : : vhost_vring_call_packed(dev, vq);
1398 : : else
1399 : : vhost_vring_call_split(dev, vq);
1400 : :
1401 : 0 : out_unlock:
1402 : : rte_rwlock_read_unlock(&vq->access_lock);
1403 : :
1404 : 0 : return ret;
1405 : : }
1406 : :
1407 : : RTE_EXPORT_SYMBOL(rte_vhost_avail_entries)
1408 : : uint16_t
1409 [ # # ]: 0 : rte_vhost_avail_entries(int vid, uint16_t queue_id)
1410 : : {
1411 : : struct virtio_net *dev;
1412 : : struct vhost_virtqueue *vq;
1413 : : uint16_t ret = 0;
1414 : :
1415 : : dev = get_device(vid);
1416 [ # # ]: 0 : if (!dev)
1417 : : return 0;
1418 : :
1419 [ # # ]: 0 : if (queue_id >= VHOST_MAX_VRING)
1420 : : return 0;
1421 : :
1422 : 0 : vq = dev->virtqueue[queue_id];
1423 [ # # ]: 0 : if (!vq)
1424 : : return 0;
1425 : :
1426 : 0 : rte_rwlock_write_lock(&vq->access_lock);
1427 : :
1428 [ # # ]: 0 : if (unlikely(!vq->access_ok))
1429 : 0 : goto out;
1430 : :
1431 [ # # ]: 0 : if (unlikely(!vq->enabled))
1432 : 0 : goto out;
1433 : :
1434 : 0 : ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1435 : :
1436 : 0 : out:
1437 : : rte_rwlock_write_unlock(&vq->access_lock);
1438 : 0 : return ret;
1439 : : }
1440 : :
1441 : : static inline int
1442 : : vhost_enable_notify_split(struct virtio_net *dev,
1443 : : struct vhost_virtqueue *vq, int enable)
1444 : : {
1445 [ # # ]: 0 : if (vq->used == NULL)
1446 : : return -1;
1447 : :
1448 [ # # ]: 0 : if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1449 [ # # ]: 0 : if (enable)
1450 : 0 : vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1451 : : else
1452 : 0 : vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1453 : : } else {
1454 [ # # ]: 0 : if (enable)
1455 : 0 : vhost_avail_event(vq) = vq->last_avail_idx;
1456 : : }
1457 : : return 0;
1458 : : }
1459 : :
1460 : : static inline int
1461 : 0 : vhost_enable_notify_packed(struct virtio_net *dev,
1462 : : struct vhost_virtqueue *vq, int enable)
1463 : : {
1464 : : uint16_t flags;
1465 : :
1466 [ # # ]: 0 : if (vq->device_event == NULL)
1467 : : return -1;
1468 : :
1469 [ # # ]: 0 : if (!enable) {
1470 : 0 : vq->device_event->flags = VRING_EVENT_F_DISABLE;
1471 : 0 : return 0;
1472 : : }
1473 : :
1474 : : flags = VRING_EVENT_F_ENABLE;
1475 [ # # ]: 0 : if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1476 : : flags = VRING_EVENT_F_DESC;
1477 : 0 : vq->device_event->off_wrap = vq->last_avail_idx |
1478 : 0 : vq->avail_wrap_counter << 15;
1479 : : }
1480 : :
1481 : : rte_atomic_thread_fence(rte_memory_order_release);
1482 : :
1483 : 0 : vq->device_event->flags = flags;
1484 : 0 : return 0;
1485 : : }
1486 : :
1487 : : int
1488 : 0 : vhost_enable_guest_notification(struct virtio_net *dev,
1489 : : struct vhost_virtqueue *vq, int enable)
1490 : : {
1491 : : /*
1492 : : * If the virtqueue is not ready yet, it will be applied
1493 : : * when it will become ready.
1494 : : */
1495 [ # # ]: 0 : if (!vq->ready)
1496 : : return 0;
1497 : :
1498 [ # # ]: 0 : if (vq_is_packed(dev))
1499 : 0 : return vhost_enable_notify_packed(dev, vq, enable);
1500 : : else
1501 : 0 : return vhost_enable_notify_split(dev, vq, enable);
1502 : : }
1503 : :
1504 : : RTE_EXPORT_SYMBOL(rte_vhost_enable_guest_notification)
1505 : : int
1506 [ # # ]: 0 : rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1507 : : {
1508 : : struct virtio_net *dev = get_device(vid);
1509 : : struct vhost_virtqueue *vq;
1510 : : int ret;
1511 : :
1512 [ # # ]: 0 : if (!dev)
1513 : : return -1;
1514 : :
1515 [ # # ]: 0 : if (queue_id >= VHOST_MAX_VRING)
1516 : : return -1;
1517 : :
1518 : 0 : vq = dev->virtqueue[queue_id];
1519 [ # # ]: 0 : if (!vq)
1520 : : return -1;
1521 : :
1522 : 0 : rte_rwlock_write_lock(&vq->access_lock);
1523 : :
1524 [ # # ]: 0 : if (unlikely(!vq->access_ok)) {
1525 : : ret = -1;
1526 : 0 : goto out_unlock;
1527 : : }
1528 : :
1529 : 0 : vq->notif_enable = enable;
1530 : 0 : ret = vhost_enable_guest_notification(dev, vq, enable);
1531 : :
1532 : 0 : out_unlock:
1533 : : rte_rwlock_write_unlock(&vq->access_lock);
1534 : :
1535 : 0 : return ret;
1536 : : }
1537 : :
1538 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vhost_notify_guest, 23.07)
1539 : : void
1540 [ # # ]: 0 : rte_vhost_notify_guest(int vid, uint16_t queue_id)
1541 : : {
1542 : : struct virtio_net *dev = get_device(vid);
1543 : : struct vhost_virtqueue *vq;
1544 : :
1545 [ # # ]: 0 : if (!dev || queue_id >= VHOST_MAX_VRING)
1546 : : return;
1547 : :
1548 : 0 : vq = dev->virtqueue[queue_id];
1549 [ # # ]: 0 : if (!vq)
1550 : : return;
1551 : :
1552 : 0 : rte_rwlock_read_lock(&vq->access_lock);
1553 : :
1554 [ # # ]: 0 : if (unlikely(!vq->access_ok))
1555 : 0 : goto out_unlock;
1556 : :
1557 : 0 : rte_atomic_store_explicit(&vq->irq_pending, false, rte_memory_order_release);
1558 : :
1559 [ # # ]: 0 : if (dev->backend_ops->inject_irq(dev, vq)) {
1560 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
1561 : 0 : rte_atomic_fetch_add_explicit(&vq->stats.guest_notifications_error,
1562 : : 1, rte_memory_order_relaxed);
1563 : : } else {
1564 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
1565 : 0 : rte_atomic_fetch_add_explicit(&vq->stats.guest_notifications,
1566 : : 1, rte_memory_order_relaxed);
1567 [ # # ]: 0 : if (dev->notify_ops->guest_notified)
1568 : 0 : dev->notify_ops->guest_notified(dev->vid);
1569 : : }
1570 : :
1571 : 0 : out_unlock:
1572 : : rte_rwlock_read_unlock(&vq->access_lock);
1573 : : }
1574 : :
1575 : : RTE_EXPORT_SYMBOL(rte_vhost_log_write)
1576 : : void
1577 [ # # ]: 0 : rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1578 : : {
1579 : : struct virtio_net *dev = get_device(vid);
1580 : :
1581 [ # # ]: 0 : if (dev == NULL)
1582 : : return;
1583 : :
1584 : : vhost_log_write(dev, addr, len);
1585 : : }
1586 : :
1587 : : RTE_EXPORT_SYMBOL(rte_vhost_log_used_vring)
1588 : : void
1589 [ # # ]: 0 : rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1590 : : uint64_t offset, uint64_t len)
1591 : : {
1592 : : struct virtio_net *dev;
1593 : : struct vhost_virtqueue *vq;
1594 : :
1595 : : dev = get_device(vid);
1596 [ # # ]: 0 : if (dev == NULL)
1597 : : return;
1598 : :
1599 [ # # ]: 0 : if (vring_idx >= VHOST_MAX_VRING)
1600 : : return;
1601 : 0 : vq = dev->virtqueue[vring_idx];
1602 [ # # ]: 0 : if (!vq)
1603 : : return;
1604 : :
1605 : : vhost_log_used_vring(dev, vq, offset, len);
1606 : : }
1607 : :
1608 : : RTE_EXPORT_SYMBOL(rte_vhost_rx_queue_count)
1609 : : uint32_t
1610 [ # # ]: 0 : rte_vhost_rx_queue_count(int vid, uint16_t qid)
1611 : : {
1612 : : struct virtio_net *dev;
1613 : : struct vhost_virtqueue *vq;
1614 : : uint32_t ret = 0;
1615 : :
1616 : : dev = get_device(vid);
1617 [ # # ]: 0 : if (dev == NULL)
1618 : : return 0;
1619 : :
1620 [ # # # # ]: 0 : if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1621 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
1622 : : "%s: invalid virtqueue idx %d.",
1623 : : __func__, qid);
1624 : 0 : return 0;
1625 : : }
1626 : :
1627 : 0 : vq = dev->virtqueue[qid];
1628 [ # # ]: 0 : if (vq == NULL)
1629 : : return 0;
1630 : :
1631 : 0 : rte_rwlock_write_lock(&vq->access_lock);
1632 : :
1633 [ # # ]: 0 : if (unlikely(!vq->access_ok))
1634 : 0 : goto out;
1635 : :
1636 [ # # ]: 0 : if (unlikely(!vq->enabled))
1637 : 0 : goto out;
1638 : :
1639 : 0 : ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1640 : :
1641 : 0 : out:
1642 : : rte_rwlock_write_unlock(&vq->access_lock);
1643 : 0 : return ret;
1644 : : }
1645 : :
1646 : : RTE_EXPORT_SYMBOL(rte_vhost_get_vdpa_device)
1647 : : struct rte_vdpa_device *
1648 [ # # ]: 0 : rte_vhost_get_vdpa_device(int vid)
1649 : : {
1650 : : struct virtio_net *dev = get_device(vid);
1651 : :
1652 [ # # ]: 0 : if (dev == NULL)
1653 : : return NULL;
1654 : :
1655 : 0 : return dev->vdpa_dev;
1656 : : }
1657 : :
1658 : : RTE_EXPORT_SYMBOL(rte_vhost_get_log_base)
1659 : : int
1660 [ # # ]: 0 : rte_vhost_get_log_base(int vid, uint64_t *log_base,
1661 : : uint64_t *log_size)
1662 : : {
1663 : : struct virtio_net *dev = get_device(vid);
1664 : :
1665 [ # # # # ]: 0 : if (dev == NULL || log_base == NULL || log_size == NULL)
1666 : : return -1;
1667 : :
1668 : 0 : *log_base = dev->log_base;
1669 : 0 : *log_size = dev->log_size;
1670 : :
1671 : 0 : return 0;
1672 : : }
1673 : :
1674 : : RTE_EXPORT_SYMBOL(rte_vhost_get_vring_base)
1675 : : int
1676 [ # # ]: 0 : rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1677 : : uint16_t *last_avail_idx, uint16_t *last_used_idx)
1678 : : {
1679 : : struct vhost_virtqueue *vq;
1680 : : struct virtio_net *dev = get_device(vid);
1681 : :
1682 [ # # # # ]: 0 : if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1683 : : return -1;
1684 : :
1685 [ # # ]: 0 : if (queue_id >= VHOST_MAX_VRING)
1686 : : return -1;
1687 : :
1688 : 0 : vq = dev->virtqueue[queue_id];
1689 [ # # ]: 0 : if (!vq)
1690 : : return -1;
1691 : :
1692 [ # # ]: 0 : if (vq_is_packed(dev)) {
1693 : 0 : *last_avail_idx = (vq->avail_wrap_counter << 15) |
1694 : 0 : vq->last_avail_idx;
1695 : 0 : *last_used_idx = (vq->used_wrap_counter << 15) |
1696 : 0 : vq->last_used_idx;
1697 : : } else {
1698 : 0 : *last_avail_idx = vq->last_avail_idx;
1699 : 0 : *last_used_idx = vq->last_used_idx;
1700 : : }
1701 : :
1702 : : return 0;
1703 : : }
1704 : :
1705 : : RTE_EXPORT_SYMBOL(rte_vhost_set_vring_base)
1706 : : int
1707 [ # # ]: 0 : rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1708 : : uint16_t last_avail_idx, uint16_t last_used_idx)
1709 : : {
1710 : : struct vhost_virtqueue *vq;
1711 : : struct virtio_net *dev = get_device(vid);
1712 : :
1713 [ # # ]: 0 : if (!dev)
1714 : : return -1;
1715 : :
1716 [ # # ]: 0 : if (queue_id >= VHOST_MAX_VRING)
1717 : : return -1;
1718 : :
1719 : 0 : vq = dev->virtqueue[queue_id];
1720 [ # # ]: 0 : if (!vq)
1721 : : return -1;
1722 : :
1723 [ # # ]: 0 : if (vq_is_packed(dev)) {
1724 : 0 : vq->last_avail_idx = last_avail_idx & 0x7fff;
1725 : 0 : vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1726 : 0 : vq->last_used_idx = last_used_idx & 0x7fff;
1727 [ # # ]: 0 : vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1728 : : vhost_virtqueue_reconnect_log_packed(vq);
1729 : : } else {
1730 : 0 : vq->last_avail_idx = last_avail_idx;
1731 [ # # ]: 0 : vq->last_used_idx = last_used_idx;
1732 : : vhost_virtqueue_reconnect_log_split(vq);
1733 : : }
1734 : :
1735 : : return 0;
1736 : : }
1737 : :
1738 : : RTE_EXPORT_SYMBOL(rte_vhost_get_vring_base_from_inflight)
1739 : : int
1740 [ # # ]: 0 : rte_vhost_get_vring_base_from_inflight(int vid,
1741 : : uint16_t queue_id,
1742 : : uint16_t *last_avail_idx,
1743 : : uint16_t *last_used_idx)
1744 : : {
1745 : : struct rte_vhost_inflight_info_packed *inflight_info;
1746 : : struct vhost_virtqueue *vq;
1747 : : struct virtio_net *dev = get_device(vid);
1748 : :
1749 [ # # # # ]: 0 : if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1750 : : return -1;
1751 : :
1752 [ # # ]: 0 : if (queue_id >= VHOST_MAX_VRING)
1753 : : return -1;
1754 : :
1755 : 0 : vq = dev->virtqueue[queue_id];
1756 [ # # ]: 0 : if (!vq)
1757 : : return -1;
1758 : :
1759 [ # # ]: 0 : if (!vq_is_packed(dev))
1760 : : return -1;
1761 : :
1762 : 0 : inflight_info = vq->inflight_packed;
1763 [ # # ]: 0 : if (!inflight_info)
1764 : : return -1;
1765 : :
1766 : 0 : *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1767 : 0 : inflight_info->old_used_idx;
1768 : 0 : *last_used_idx = *last_avail_idx;
1769 : :
1770 : 0 : return 0;
1771 : : }
1772 : :
1773 : : RTE_EXPORT_SYMBOL(rte_vhost_extern_callback_register)
1774 : : int
1775 [ # # ]: 0 : rte_vhost_extern_callback_register(int vid,
1776 : : struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1777 : : {
1778 : : struct virtio_net *dev = get_device(vid);
1779 : :
1780 [ # # ]: 0 : if (dev == NULL || ops == NULL)
1781 : : return -1;
1782 : :
1783 : 0 : dev->extern_ops = *ops;
1784 : 0 : dev->extern_data = ctx;
1785 : 0 : return 0;
1786 : : }
1787 : :
1788 : : static __rte_always_inline int
1789 : : async_channel_register(struct virtio_net *dev, struct vhost_virtqueue *vq)
1790 : : __rte_requires_capability(&vq->access_lock)
1791 : : {
1792 : : struct vhost_async *async;
1793 : 0 : int node = vq->numa_node;
1794 : :
1795 [ # # # # ]: 0 : if (unlikely(vq->async)) {
1796 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1797 : : "async register failed: already registered (qid: %d)",
1798 : : vq->index);
1799 : 0 : return -1;
1800 : : }
1801 : :
1802 : 0 : async = rte_zmalloc_socket(NULL, sizeof(struct vhost_async), 0, node);
1803 [ # # # # ]: 0 : if (!async) {
1804 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1805 : : "failed to allocate async metadata (qid: %d)",
1806 : : vq->index);
1807 : 0 : return -1;
1808 : : }
1809 : :
1810 : 0 : async->pkts_info = rte_malloc_socket(NULL, vq->size * sizeof(struct async_inflight_info),
1811 : : RTE_CACHE_LINE_SIZE, node);
1812 [ # # # # ]: 0 : if (!async->pkts_info) {
1813 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1814 : : "failed to allocate async_pkts_info (qid: %d)",
1815 : : vq->index);
1816 : 0 : goto out_free_async;
1817 : : }
1818 : :
1819 : 0 : async->pkts_cmpl_flag = rte_zmalloc_socket(NULL, vq->size * sizeof(bool),
1820 : : RTE_CACHE_LINE_SIZE, node);
1821 [ # # # # ]: 0 : if (!async->pkts_cmpl_flag) {
1822 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1823 : : "failed to allocate async pkts_cmpl_flag (qid: %d)",
1824 : : vq->index);
1825 : 0 : goto out_free_async;
1826 : : }
1827 : :
1828 [ # # # # ]: 0 : if (vq_is_packed(dev)) {
1829 : 0 : async->buffers_packed = rte_malloc_socket(NULL,
1830 : 0 : vq->size * sizeof(struct vring_used_elem_packed),
1831 : : RTE_CACHE_LINE_SIZE, node);
1832 [ # # # # ]: 0 : if (!async->buffers_packed) {
1833 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1834 : : "failed to allocate async buffers (qid: %d)",
1835 : : vq->index);
1836 : 0 : goto out_free_inflight;
1837 : : }
1838 : : } else {
1839 : 0 : async->descs_split = rte_malloc_socket(NULL,
1840 : 0 : vq->size * sizeof(struct vring_used_elem),
1841 : : RTE_CACHE_LINE_SIZE, node);
1842 [ # # # # ]: 0 : if (!async->descs_split) {
1843 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1844 : : "failed to allocate async descs (qid: %d)",
1845 : : vq->index);
1846 : 0 : goto out_free_inflight;
1847 : : }
1848 : : }
1849 : :
1850 : 0 : vq->async = async;
1851 : :
1852 : 0 : return 0;
1853 : 0 : out_free_inflight:
1854 : 0 : rte_free(async->pkts_info);
1855 : 0 : out_free_async:
1856 : 0 : rte_free(async);
1857 : :
1858 : 0 : return -1;
1859 : : }
1860 : :
1861 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vhost_async_channel_register, 20.08)
1862 : : int
1863 [ # # ]: 0 : rte_vhost_async_channel_register(int vid, uint16_t queue_id)
1864 : : {
1865 : : struct vhost_virtqueue *vq;
1866 : : struct virtio_net *dev = get_device(vid);
1867 : : int ret;
1868 : :
1869 [ # # ]: 0 : if (dev == NULL)
1870 : : return -1;
1871 : :
1872 [ # # ]: 0 : if (queue_id >= VHOST_MAX_VRING)
1873 : : return -1;
1874 : :
1875 : 0 : vq = dev->virtqueue[queue_id];
1876 : :
1877 [ # # # # : 0 : if (unlikely(vq == NULL || !dev->async_copy || dev->vdpa_dev != NULL))
# # ]
1878 : : return -1;
1879 : :
1880 : 0 : rte_rwlock_write_lock(&vq->access_lock);
1881 : :
1882 [ # # ]: 0 : if (unlikely(!vq->access_ok)) {
1883 : : ret = -1;
1884 : 0 : goto out_unlock;
1885 : : }
1886 : :
1887 : : ret = async_channel_register(dev, vq);
1888 : :
1889 : 0 : out_unlock:
1890 : : rte_rwlock_write_unlock(&vq->access_lock);
1891 : :
1892 : 0 : return ret;
1893 : : }
1894 : :
1895 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vhost_async_channel_register_thread_unsafe, 21.08)
1896 : : int
1897 [ # # ]: 0 : rte_vhost_async_channel_register_thread_unsafe(int vid, uint16_t queue_id)
1898 : : {
1899 : : struct vhost_virtqueue *vq;
1900 : : struct virtio_net *dev = get_device(vid);
1901 : :
1902 [ # # ]: 0 : if (dev == NULL)
1903 : : return -1;
1904 : :
1905 [ # # ]: 0 : if (queue_id >= VHOST_MAX_VRING)
1906 : : return -1;
1907 : :
1908 : 0 : vq = dev->virtqueue[queue_id];
1909 : :
1910 [ # # # # : 0 : if (unlikely(vq == NULL || !dev->async_copy || dev->vdpa_dev != NULL))
# # ]
1911 : : return -1;
1912 : :
1913 : 0 : vq_assert_lock(dev, vq);
1914 : :
1915 : : return async_channel_register(dev, vq);
1916 : : }
1917 : :
1918 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vhost_async_channel_unregister, 20.08)
1919 : : int
1920 [ # # ]: 0 : rte_vhost_async_channel_unregister(int vid, uint16_t queue_id)
1921 : : {
1922 : : struct vhost_virtqueue *vq;
1923 : : struct virtio_net *dev = get_device(vid);
1924 : : int ret = -1;
1925 : :
1926 [ # # ]: 0 : if (dev == NULL)
1927 : : return ret;
1928 : :
1929 [ # # ]: 0 : if (queue_id >= VHOST_MAX_VRING)
1930 : : return ret;
1931 : :
1932 : 0 : vq = dev->virtqueue[queue_id];
1933 : :
1934 [ # # ]: 0 : if (vq == NULL)
1935 : : return ret;
1936 : :
1937 : : if (rte_rwlock_write_trylock(&vq->access_lock)) {
1938 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1939 : : "failed to unregister async channel, virtqueue busy.");
1940 : 0 : return ret;
1941 : : }
1942 : :
1943 [ # # ]: 0 : if (unlikely(!vq->access_ok)) {
1944 : : ret = -1;
1945 : 0 : goto out_unlock;
1946 : : }
1947 : :
1948 [ # # ]: 0 : if (!vq->async) {
1949 : : ret = 0;
1950 [ # # ]: 0 : } else if (vq->async->pkts_inflight_n) {
1951 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to unregister async channel.");
1952 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1953 : : "inflight packets must be completed before unregistration.");
1954 : : } else {
1955 : 0 : vhost_free_async_mem(vq);
1956 : : ret = 0;
1957 : : }
1958 : :
1959 : 0 : out_unlock:
1960 : : rte_rwlock_write_unlock(&vq->access_lock);
1961 : :
1962 : 0 : return ret;
1963 : : }
1964 : :
1965 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vhost_async_channel_unregister_thread_unsafe, 21.08)
1966 : : int
1967 [ # # ]: 0 : rte_vhost_async_channel_unregister_thread_unsafe(int vid, uint16_t queue_id)
1968 : : {
1969 : : struct vhost_virtqueue *vq;
1970 : : struct virtio_net *dev = get_device(vid);
1971 : :
1972 [ # # ]: 0 : if (dev == NULL)
1973 : : return -1;
1974 : :
1975 [ # # ]: 0 : if (queue_id >= VHOST_MAX_VRING)
1976 : : return -1;
1977 : :
1978 : 0 : vq = dev->virtqueue[queue_id];
1979 : :
1980 [ # # ]: 0 : if (vq == NULL)
1981 : : return -1;
1982 : :
1983 : 0 : vq_assert_lock(dev, vq);
1984 : :
1985 [ # # ]: 0 : if (!vq->async)
1986 : : return 0;
1987 : :
1988 [ # # ]: 0 : if (vq->async->pkts_inflight_n) {
1989 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to unregister async channel.");
1990 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
1991 : : "inflight packets must be completed before unregistration.");
1992 : 0 : return -1;
1993 : : }
1994 : :
1995 : 0 : vhost_free_async_mem(vq);
1996 : :
1997 : 0 : return 0;
1998 : : }
1999 : :
2000 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vhost_async_dma_configure, 22.03)
2001 : : int
2002 : 0 : rte_vhost_async_dma_configure(int16_t dma_id, uint16_t vchan_id)
2003 : : {
2004 : : struct rte_dma_info info;
2005 : : void *pkts_cmpl_flag_addr;
2006 : : uint16_t max_desc;
2007 : :
2008 : 0 : pthread_mutex_lock(&vhost_dma_lock);
2009 : :
2010 [ # # ]: 0 : if (!rte_dma_is_valid(dma_id)) {
2011 : 0 : VHOST_CONFIG_LOG("dma", ERR, "DMA %d is not found.", dma_id);
2012 : 0 : goto error;
2013 : : }
2014 : :
2015 [ # # ]: 0 : if (rte_dma_info_get(dma_id, &info) != 0) {
2016 : 0 : VHOST_CONFIG_LOG("dma", ERR, "Fail to get DMA %d information.", dma_id);
2017 : 0 : goto error;
2018 : : }
2019 : :
2020 [ # # ]: 0 : if (vchan_id >= info.max_vchans) {
2021 : 0 : VHOST_CONFIG_LOG("dma", ERR, "Invalid DMA %d vChannel %u.", dma_id, vchan_id);
2022 : 0 : goto error;
2023 : : }
2024 : :
2025 [ # # ]: 0 : if (!dma_copy_track[dma_id].vchans) {
2026 : : struct async_dma_vchan_info *vchans;
2027 : :
2028 : 0 : vchans = rte_zmalloc(NULL, sizeof(struct async_dma_vchan_info) * info.max_vchans,
2029 : : RTE_CACHE_LINE_SIZE);
2030 [ # # ]: 0 : if (vchans == NULL) {
2031 : 0 : VHOST_CONFIG_LOG("dma", ERR,
2032 : : "Failed to allocate vchans for DMA %d vChannel %u.",
2033 : : dma_id, vchan_id);
2034 : 0 : goto error;
2035 : : }
2036 : :
2037 : 0 : dma_copy_track[dma_id].vchans = vchans;
2038 : : }
2039 : :
2040 [ # # ]: 0 : if (dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr) {
2041 : 0 : VHOST_CONFIG_LOG("dma", INFO, "DMA %d vChannel %u already registered.",
2042 : : dma_id, vchan_id);
2043 : 0 : pthread_mutex_unlock(&vhost_dma_lock);
2044 : 0 : return 0;
2045 : : }
2046 : :
2047 : 0 : max_desc = info.max_desc;
2048 [ # # ]: 0 : if (!rte_is_power_of_2(max_desc))
2049 : 0 : max_desc = rte_align32pow2(max_desc);
2050 : :
2051 : 0 : pkts_cmpl_flag_addr = rte_zmalloc(NULL, sizeof(bool *) * max_desc, RTE_CACHE_LINE_SIZE);
2052 [ # # ]: 0 : if (!pkts_cmpl_flag_addr) {
2053 : 0 : VHOST_CONFIG_LOG("dma", ERR,
2054 : : "Failed to allocate pkts_cmpl_flag_addr for DMA %d vChannel %u.",
2055 : : dma_id, vchan_id);
2056 : :
2057 [ # # ]: 0 : if (dma_copy_track[dma_id].nr_vchans == 0) {
2058 : 0 : rte_free(dma_copy_track[dma_id].vchans);
2059 : 0 : dma_copy_track[dma_id].vchans = NULL;
2060 : : }
2061 : 0 : goto error;
2062 : : }
2063 : :
2064 : 0 : dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr = pkts_cmpl_flag_addr;
2065 : 0 : dma_copy_track[dma_id].vchans[vchan_id].ring_size = max_desc;
2066 : 0 : dma_copy_track[dma_id].vchans[vchan_id].ring_mask = max_desc - 1;
2067 : 0 : dma_copy_track[dma_id].nr_vchans++;
2068 : :
2069 : 0 : pthread_mutex_unlock(&vhost_dma_lock);
2070 : 0 : return 0;
2071 : :
2072 : 0 : error:
2073 : 0 : pthread_mutex_unlock(&vhost_dma_lock);
2074 : 0 : return -1;
2075 : : }
2076 : :
2077 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vhost_async_get_inflight, 21.08)
2078 : : int
2079 [ # # ]: 0 : rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
2080 : : {
2081 : : struct vhost_virtqueue *vq;
2082 : : struct virtio_net *dev = get_device(vid);
2083 : : int ret = -1;
2084 : :
2085 [ # # ]: 0 : if (dev == NULL)
2086 : : return ret;
2087 : :
2088 [ # # ]: 0 : if (queue_id >= VHOST_MAX_VRING)
2089 : : return ret;
2090 : :
2091 : 0 : vq = dev->virtqueue[queue_id];
2092 : :
2093 [ # # ]: 0 : if (vq == NULL)
2094 : : return ret;
2095 : :
2096 : : if (rte_rwlock_write_trylock(&vq->access_lock)) {
2097 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG,
2098 : : "failed to check in-flight packets. virtqueue busy.");
2099 : 0 : return ret;
2100 : : }
2101 : :
2102 [ # # ]: 0 : if (unlikely(!vq->access_ok)) {
2103 : : ret = -1;
2104 : 0 : goto out_unlock;
2105 : : }
2106 : :
2107 [ # # ]: 0 : if (vq->async)
2108 : 0 : ret = vq->async->pkts_inflight_n;
2109 : :
2110 : 0 : out_unlock:
2111 : : rte_rwlock_write_unlock(&vq->access_lock);
2112 : :
2113 : 0 : return ret;
2114 : : }
2115 : :
2116 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vhost_async_get_inflight_thread_unsafe, 22.07)
2117 : : int
2118 [ # # ]: 0 : rte_vhost_async_get_inflight_thread_unsafe(int vid, uint16_t queue_id)
2119 : : {
2120 : : struct vhost_virtqueue *vq;
2121 : : struct virtio_net *dev = get_device(vid);
2122 : : int ret = -1;
2123 : :
2124 [ # # ]: 0 : if (dev == NULL)
2125 : : return ret;
2126 : :
2127 [ # # ]: 0 : if (queue_id >= VHOST_MAX_VRING)
2128 : : return ret;
2129 : :
2130 : 0 : vq = dev->virtqueue[queue_id];
2131 : :
2132 [ # # ]: 0 : if (vq == NULL)
2133 : : return ret;
2134 : :
2135 : 0 : vq_assert_lock(dev, vq);
2136 : :
2137 [ # # ]: 0 : if (!vq->async)
2138 : : return ret;
2139 : :
2140 : 0 : ret = vq->async->pkts_inflight_n;
2141 : :
2142 : 0 : return ret;
2143 : : }
2144 : :
2145 : : RTE_EXPORT_SYMBOL(rte_vhost_get_monitor_addr)
2146 : : int
2147 [ # # ]: 0 : rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
2148 : : struct rte_vhost_power_monitor_cond *pmc)
2149 : : {
2150 : : struct virtio_net *dev = get_device(vid);
2151 : : struct vhost_virtqueue *vq;
2152 : : int ret = 0;
2153 : :
2154 [ # # ]: 0 : if (dev == NULL)
2155 : : return -1;
2156 [ # # ]: 0 : if (queue_id >= VHOST_MAX_VRING)
2157 : : return -1;
2158 : :
2159 : 0 : vq = dev->virtqueue[queue_id];
2160 [ # # ]: 0 : if (vq == NULL)
2161 : : return -1;
2162 : :
2163 : 0 : rte_rwlock_read_lock(&vq->access_lock);
2164 : :
2165 [ # # ]: 0 : if (unlikely(!vq->access_ok)) {
2166 : : ret = -1;
2167 : 0 : goto out_unlock;
2168 : : }
2169 : :
2170 [ # # ]: 0 : if (vq_is_packed(dev)) {
2171 : : struct vring_packed_desc *desc;
2172 : 0 : desc = vq->desc_packed;
2173 : 0 : pmc->addr = &desc[vq->last_avail_idx].flags;
2174 [ # # ]: 0 : if (vq->avail_wrap_counter)
2175 : 0 : pmc->val = VRING_DESC_F_AVAIL;
2176 : : else
2177 : 0 : pmc->val = VRING_DESC_F_USED;
2178 : 0 : pmc->mask = VRING_DESC_F_AVAIL | VRING_DESC_F_USED;
2179 : 0 : pmc->size = sizeof(desc[vq->last_avail_idx].flags);
2180 : 0 : pmc->match = 1;
2181 : : } else {
2182 : 0 : pmc->addr = &vq->avail->idx;
2183 : 0 : pmc->val = vq->last_avail_idx & (vq->size - 1);
2184 : 0 : pmc->mask = vq->size - 1;
2185 : 0 : pmc->size = sizeof(vq->avail->idx);
2186 : 0 : pmc->match = 0;
2187 : : }
2188 : :
2189 : 0 : out_unlock:
2190 : : rte_rwlock_read_unlock(&vq->access_lock);
2191 : :
2192 : 0 : return ret;
2193 : : }
2194 : :
2195 : :
2196 : : RTE_EXPORT_SYMBOL(rte_vhost_vring_stats_get_names)
2197 : : int
2198 [ # # ]: 0 : rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id,
2199 : : struct rte_vhost_stat_name *name, unsigned int size)
2200 : : {
2201 : : struct virtio_net *dev = get_device(vid);
2202 : : unsigned int i;
2203 : :
2204 [ # # ]: 0 : if (dev == NULL)
2205 : : return -1;
2206 : :
2207 [ # # ]: 0 : if (queue_id >= dev->nr_vring)
2208 : : return -1;
2209 : :
2210 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
2211 : : return -1;
2212 : :
2213 [ # # ]: 0 : if (name == NULL || size < VHOST_NB_VQ_STATS)
2214 : : return VHOST_NB_VQ_STATS;
2215 : :
2216 [ # # ]: 0 : for (i = 0; i < VHOST_NB_VQ_STATS; i++)
2217 : 0 : snprintf(name[i].name, sizeof(name[i].name), "%s_q%u_%s",
2218 : : (queue_id & 1) ? "rx" : "tx",
2219 [ # # ]: 0 : queue_id / 2, vhost_vq_stat_strings[i].name);
2220 : :
2221 : : return VHOST_NB_VQ_STATS;
2222 : : }
2223 : :
2224 : : RTE_EXPORT_SYMBOL(rte_vhost_vring_stats_get)
2225 : : int
2226 [ # # ]: 0 : rte_vhost_vring_stats_get(int vid, uint16_t queue_id,
2227 : : struct rte_vhost_stat *stats, unsigned int n)
2228 : : {
2229 : : struct virtio_net *dev = get_device(vid);
2230 : : struct vhost_virtqueue *vq;
2231 : : unsigned int i;
2232 : : int ret = VHOST_NB_VQ_STATS;
2233 : :
2234 [ # # ]: 0 : if (dev == NULL)
2235 : : return -1;
2236 : :
2237 [ # # ]: 0 : if (queue_id >= dev->nr_vring)
2238 : : return -1;
2239 : :
2240 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
2241 : : return -1;
2242 : :
2243 [ # # ]: 0 : if (stats == NULL || n < VHOST_NB_VQ_STATS)
2244 : : return VHOST_NB_VQ_STATS;
2245 : :
2246 : 0 : vq = dev->virtqueue[queue_id];
2247 : :
2248 : 0 : rte_rwlock_write_lock(&vq->access_lock);
2249 : :
2250 [ # # ]: 0 : if (unlikely(!vq->access_ok)) {
2251 : : ret = -1;
2252 : 0 : goto out_unlock;
2253 : : }
2254 : :
2255 [ # # ]: 0 : for (i = 0; i < VHOST_NB_VQ_STATS; i++) {
2256 : : /*
2257 : : * No need to the read atomic counters as such, due to the
2258 : : * above write access_lock preventing them to be updated.
2259 : : */
2260 : 0 : stats[i].value =
2261 : 0 : *(uint64_t *)(((char *)vq) + vhost_vq_stat_strings[i].offset);
2262 : 0 : stats[i].id = i;
2263 : : }
2264 : :
2265 : 0 : out_unlock:
2266 : : rte_rwlock_write_unlock(&vq->access_lock);
2267 : :
2268 : 0 : return ret;
2269 : : }
2270 : :
2271 : : RTE_EXPORT_SYMBOL(rte_vhost_vring_stats_reset)
2272 [ # # ]: 0 : int rte_vhost_vring_stats_reset(int vid, uint16_t queue_id)
2273 : : {
2274 : : struct virtio_net *dev = get_device(vid);
2275 : : struct vhost_virtqueue *vq;
2276 : : int ret = 0;
2277 : :
2278 [ # # ]: 0 : if (dev == NULL)
2279 : : return -1;
2280 : :
2281 [ # # ]: 0 : if (queue_id >= dev->nr_vring)
2282 : : return -1;
2283 : :
2284 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
2285 : : return -1;
2286 : :
2287 : 0 : vq = dev->virtqueue[queue_id];
2288 : :
2289 : 0 : rte_rwlock_write_lock(&vq->access_lock);
2290 : :
2291 [ # # ]: 0 : if (unlikely(!vq->access_ok)) {
2292 : : ret = -1;
2293 : 0 : goto out_unlock;
2294 : : }
2295 : : /*
2296 : : * No need to the reset atomic counters as such, due to the
2297 : : * above write access_lock preventing them to be updated.
2298 : : */
2299 : 0 : memset(&vq->stats, 0, sizeof(vq->stats));
2300 : :
2301 : 0 : out_unlock:
2302 : : rte_rwlock_write_unlock(&vq->access_lock);
2303 : :
2304 : 0 : return ret;
2305 : : }
2306 : :
2307 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vhost_async_dma_unconfigure, 22.11)
2308 : : int
2309 : 0 : rte_vhost_async_dma_unconfigure(int16_t dma_id, uint16_t vchan_id)
2310 : : {
2311 : : struct rte_dma_info info;
2312 : 0 : struct rte_dma_stats stats = { 0 };
2313 : :
2314 : 0 : pthread_mutex_lock(&vhost_dma_lock);
2315 : :
2316 [ # # ]: 0 : if (!rte_dma_is_valid(dma_id)) {
2317 : 0 : VHOST_CONFIG_LOG("dma", ERR, "DMA %d is not found.", dma_id);
2318 : 0 : goto error;
2319 : : }
2320 : :
2321 [ # # ]: 0 : if (rte_dma_info_get(dma_id, &info) != 0) {
2322 : 0 : VHOST_CONFIG_LOG("dma", ERR, "Fail to get DMA %d information.", dma_id);
2323 : 0 : goto error;
2324 : : }
2325 : :
2326 [ # # # # ]: 0 : if (vchan_id >= info.max_vchans || !dma_copy_track[dma_id].vchans ||
2327 [ # # ]: 0 : !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr) {
2328 : 0 : VHOST_CONFIG_LOG("dma", ERR, "Invalid channel %d:%u.", dma_id, vchan_id);
2329 : 0 : goto error;
2330 : : }
2331 : :
2332 [ # # ]: 0 : if (rte_dma_stats_get(dma_id, vchan_id, &stats) != 0) {
2333 : 0 : VHOST_CONFIG_LOG("dma", ERR,
2334 : : "Failed to get stats for DMA %d vChannel %u.", dma_id, vchan_id);
2335 : 0 : goto error;
2336 : : }
2337 : :
2338 [ # # ]: 0 : if (stats.submitted - stats.completed != 0) {
2339 : 0 : VHOST_CONFIG_LOG("dma", ERR,
2340 : : "Do not unconfigure when there are inflight packets.");
2341 : 0 : goto error;
2342 : : }
2343 : :
2344 : 0 : rte_free(dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr);
2345 : 0 : dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr = NULL;
2346 : 0 : dma_copy_track[dma_id].nr_vchans--;
2347 : :
2348 [ # # ]: 0 : if (dma_copy_track[dma_id].nr_vchans == 0) {
2349 : 0 : rte_free(dma_copy_track[dma_id].vchans);
2350 : 0 : dma_copy_track[dma_id].vchans = NULL;
2351 : : }
2352 : :
2353 : 0 : pthread_mutex_unlock(&vhost_dma_lock);
2354 : 0 : return 0;
2355 : :
2356 : 0 : error:
2357 : 0 : pthread_mutex_unlock(&vhost_dma_lock);
2358 : 0 : return -1;
2359 : : }
2360 : :
2361 [ - + ]: 253 : RTE_LOG_REGISTER_SUFFIX(vhost_config_log_level, config, INFO);
2362 [ - + ]: 253 : RTE_LOG_REGISTER_SUFFIX(vhost_data_log_level, data, WARNING);
|