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