Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2018 Intel Corporation
3 : : */
4 : :
5 : : #ifndef _VHOST_NET_CDEV_H_
6 : : #define _VHOST_NET_CDEV_H_
7 : : #include <stdint.h>
8 : : #include <stdio.h>
9 : : #include <stdbool.h>
10 : : #include <stdlib.h>
11 : : #include <sys/queue.h>
12 : : #include <unistd.h>
13 : : #include <linux/virtio_net.h>
14 : : #include <sys/socket.h>
15 : : #include <linux/if.h>
16 : : #include <sys/mman.h>
17 : :
18 : : #include <rte_log.h>
19 : : #include <rte_ether.h>
20 : : #include <rte_malloc.h>
21 : : #include <rte_dmadev.h>
22 : :
23 : : #include "rte_vhost.h"
24 : : #include "vdpa_driver.h"
25 : :
26 : : #include "rte_vhost_async.h"
27 : :
28 : : /* Used to indicate that the device is running on a data core */
29 : : #define VIRTIO_DEV_RUNNING ((uint32_t)1 << 0)
30 : : /* Used to indicate that the device is ready to operate */
31 : : #define VIRTIO_DEV_READY ((uint32_t)1 << 1)
32 : : /* Used to indicate that the built-in vhost net device backend is enabled */
33 : : #define VIRTIO_DEV_BUILTIN_VIRTIO_NET ((uint32_t)1 << 2)
34 : : /* Used to indicate that the device has its own data path and configured */
35 : : #define VIRTIO_DEV_VDPA_CONFIGURED ((uint32_t)1 << 3)
36 : : /* Used to indicate that the feature negotiation failed */
37 : : #define VIRTIO_DEV_FEATURES_FAILED ((uint32_t)1 << 4)
38 : : /* Used to indicate that the virtio_net tx code should fill TX ol_flags */
39 : : #define VIRTIO_DEV_LEGACY_OL_FLAGS ((uint32_t)1 << 5)
40 : : /* Used to indicate the application has requested statistics collection */
41 : : #define VIRTIO_DEV_STATS_ENABLED ((uint32_t)1 << 6)
42 : : /* Used to indicate the application has requested iommu support */
43 : : #define VIRTIO_DEV_SUPPORT_IOMMU ((uint32_t)1 << 7)
44 : :
45 : : /* Backend value set by guest. */
46 : : #define VIRTIO_DEV_STOPPED -1
47 : :
48 : : #define BUF_VECTOR_MAX 256
49 : :
50 : : #define VHOST_LOG_CACHE_NR 32
51 : :
52 : : #define MAX_PKT_BURST 32
53 : :
54 : : #define VHOST_MAX_ASYNC_IT (MAX_PKT_BURST)
55 : : #define VHOST_MAX_ASYNC_VEC 2048
56 : : #define VIRTIO_MAX_RX_PKTLEN 9728U
57 : : #define VHOST_DMA_MAX_COPY_COMPLETE ((VIRTIO_MAX_RX_PKTLEN / RTE_MBUF_DEFAULT_DATAROOM) \
58 : : * MAX_PKT_BURST)
59 : :
60 : : #define PACKED_DESC_ENQUEUE_USED_FLAG(w) \
61 : : ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \
62 : : VRING_DESC_F_WRITE)
63 : : #define PACKED_DESC_DEQUEUE_USED_FLAG(w) \
64 : : ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED) : 0x0)
65 : : #define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \
66 : : VRING_DESC_F_INDIRECT)
67 : :
68 : : #define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \
69 : : sizeof(struct vring_packed_desc))
70 : : #define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
71 : :
72 : : #ifdef VHOST_GCC_UNROLL_PRAGMA
73 : : #define vhost_for_each_try_unroll(iter, val, size) _Pragma("GCC unroll 4") \
74 : : for (iter = val; iter < size; iter++)
75 : : #endif
76 : :
77 : : #ifdef VHOST_CLANG_UNROLL_PRAGMA
78 : : #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \
79 : : for (iter = val; iter < size; iter++)
80 : : #endif
81 : :
82 : : #ifdef VHOST_ICC_UNROLL_PRAGMA
83 : : #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \
84 : : for (iter = val; iter < size; iter++)
85 : : #endif
86 : :
87 : : #ifndef vhost_for_each_try_unroll
88 : : #define vhost_for_each_try_unroll(iter, val, num) \
89 : : for (iter = val; iter < num; iter++)
90 : : #endif
91 : :
92 : : struct virtio_net;
93 : : struct vhost_virtqueue;
94 : :
95 : : typedef void (*vhost_iotlb_remove_notify)(uint64_t addr, uint64_t off, uint64_t size);
96 : :
97 : : typedef int (*vhost_iotlb_miss_cb)(struct virtio_net *dev, uint64_t iova, uint8_t perm);
98 : :
99 : : typedef int (*vhost_vring_inject_irq_cb)(struct virtio_net *dev, struct vhost_virtqueue *vq);
100 : : /**
101 : : * Structure that contains backend-specific ops.
102 : : */
103 : : struct vhost_backend_ops {
104 : : vhost_iotlb_remove_notify iotlb_remove_notify;
105 : : vhost_iotlb_miss_cb iotlb_miss;
106 : : vhost_vring_inject_irq_cb inject_irq;
107 : : };
108 : :
109 : : /**
110 : : * Structure contains buffer address, length and descriptor index
111 : : * from vring to do scatter RX.
112 : : */
113 : : struct buf_vector {
114 : : uint64_t buf_iova;
115 : : uint64_t buf_addr;
116 : : uint32_t buf_len;
117 : : uint32_t desc_idx;
118 : : };
119 : :
120 : : /*
121 : : * Structure contains the info for each batched memory copy.
122 : : */
123 : : struct batch_copy_elem {
124 : : void *dst;
125 : : void *src;
126 : : uint32_t len;
127 : : uint64_t log_addr;
128 : : };
129 : :
130 : : /*
131 : : * Structure that contains the info for batched dirty logging.
132 : : */
133 : : struct log_cache_entry {
134 : : uint32_t offset;
135 : : unsigned long val;
136 : : };
137 : :
138 : : struct vring_used_elem_packed {
139 : : uint16_t id;
140 : : uint16_t flags;
141 : : uint32_t len;
142 : : uint32_t count;
143 : : };
144 : :
145 : : /**
146 : : * Virtqueue statistics
147 : : */
148 : : struct virtqueue_stats {
149 : : uint64_t packets;
150 : : uint64_t bytes;
151 : : uint64_t multicast;
152 : : uint64_t broadcast;
153 : : /* Size bins in array as RFC 2819, undersized [0], 64 [1], etc */
154 : : uint64_t size_bins[8];
155 : : uint64_t iotlb_hits;
156 : : uint64_t iotlb_misses;
157 : : uint64_t inflight_submitted;
158 : : uint64_t inflight_completed;
159 : : uint64_t guest_notifications_suppressed;
160 : : /* Counters below are atomic, and should be incremented as such. */
161 : : RTE_ATOMIC(uint64_t) guest_notifications;
162 : : RTE_ATOMIC(uint64_t) guest_notifications_offloaded;
163 : : RTE_ATOMIC(uint64_t) guest_notifications_error;
164 : : };
165 : :
166 : : /**
167 : : * iovec
168 : : */
169 : : struct vhost_iovec {
170 : : void *src_addr;
171 : : void *dst_addr;
172 : : size_t len;
173 : : };
174 : :
175 : : /**
176 : : * iovec iterator
177 : : */
178 : : struct vhost_iov_iter {
179 : : /** pointer to the iovec array */
180 : : struct vhost_iovec *iov;
181 : : /** number of iovec in this iterator */
182 : : unsigned long nr_segs;
183 : : };
184 : :
185 : : struct async_dma_vchan_info {
186 : : /* circular array to track if packet copy completes */
187 : : bool **pkts_cmpl_flag_addr;
188 : :
189 : : /* max elements in 'pkts_cmpl_flag_addr' */
190 : : uint16_t ring_size;
191 : : /* ring index mask for 'pkts_cmpl_flag_addr' */
192 : : uint16_t ring_mask;
193 : :
194 : : /**
195 : : * DMA virtual channel lock. Although it is able to bind DMA
196 : : * virtual channels to data plane threads, vhost control plane
197 : : * thread could call data plane functions too, thus causing
198 : : * DMA device contention.
199 : : *
200 : : * For example, in VM exit case, vhost control plane thread needs
201 : : * to clear in-flight packets before disable vring, but there could
202 : : * be anotther data plane thread is enqueuing packets to the same
203 : : * vring with the same DMA virtual channel. As dmadev PMD functions
204 : : * are lock-free, the control plane and data plane threads could
205 : : * operate the same DMA virtual channel at the same time.
206 : : */
207 : : rte_spinlock_t dma_lock;
208 : : };
209 : :
210 : : struct async_dma_info {
211 : : struct async_dma_vchan_info *vchans;
212 : : /* number of registered virtual channels */
213 : : uint16_t nr_vchans;
214 : : };
215 : :
216 : : extern struct async_dma_info dma_copy_track[RTE_DMADEV_DEFAULT_MAX];
217 : :
218 : : /**
219 : : * inflight async packet information
220 : : */
221 : : struct async_inflight_info {
222 : : struct rte_mbuf *mbuf;
223 : : uint16_t descs; /* num of descs inflight */
224 : : uint16_t nr_buffers; /* num of buffers inflight for packed ring */
225 : : struct virtio_net_hdr nethdr;
226 : : };
227 : :
228 : : struct vhost_async {
229 : : struct vhost_iov_iter iov_iter[VHOST_MAX_ASYNC_IT];
230 : : struct vhost_iovec iovec[VHOST_MAX_ASYNC_VEC];
231 : : uint16_t iter_idx;
232 : : uint16_t iovec_idx;
233 : :
234 : : /* data transfer status */
235 : : struct async_inflight_info *pkts_info;
236 : : /**
237 : : * Packet reorder array. "true" indicates that DMA device
238 : : * completes all copies for the packet.
239 : : *
240 : : * Note that this array could be written by multiple threads
241 : : * simultaneously. For example, in the case of thread0 and
242 : : * thread1 RX packets from NIC and then enqueue packets to
243 : : * vring0 and vring1 with own DMA device DMA0 and DMA1, it's
244 : : * possible for thread0 to get completed copies belonging to
245 : : * vring1 from DMA0, while thread0 is calling rte_vhost_poll
246 : : * _enqueue_completed() for vring0 and thread1 is calling
247 : : * rte_vhost_submit_enqueue_burst() for vring1. In this case,
248 : : * vq->access_lock cannot protect pkts_cmpl_flag of vring1.
249 : : *
250 : : * However, since offloading is per-packet basis, each packet
251 : : * flag will only be written by one thread. And single byte
252 : : * write is atomic, so no lock for pkts_cmpl_flag is needed.
253 : : */
254 : : bool *pkts_cmpl_flag;
255 : : uint16_t pkts_idx;
256 : : uint16_t pkts_inflight_n;
257 : : union {
258 : : struct vring_used_elem *descs_split;
259 : : struct vring_used_elem_packed *buffers_packed;
260 : : };
261 : : union {
262 : : uint16_t desc_idx_split;
263 : : uint16_t buffer_idx_packed;
264 : : };
265 : : union {
266 : : uint16_t last_desc_idx_split;
267 : : uint16_t last_buffer_idx_packed;
268 : : };
269 : : };
270 : :
271 : : /**
272 : : * Structure contains variables relevant to RX/TX virtqueues.
273 : : */
274 : : struct vhost_virtqueue {
275 : : union {
276 : : struct vring_desc *desc;
277 : : struct vring_packed_desc *desc_packed;
278 : : };
279 : : union {
280 : : struct vring_avail *avail;
281 : : struct vring_packed_desc_event *driver_event;
282 : : };
283 : : union {
284 : : struct vring_used *used;
285 : : struct vring_packed_desc_event *device_event;
286 : : };
287 : : uint16_t size;
288 : :
289 : : uint16_t last_avail_idx;
290 : : uint16_t last_used_idx;
291 : : /* Last used index we notify to front end. */
292 : : uint16_t signalled_used;
293 : : bool signalled_used_valid;
294 : : #define VIRTIO_INVALID_EVENTFD (-1)
295 : : #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
296 : :
297 : : bool enabled;
298 : : bool access_ok;
299 : : bool ready;
300 : :
301 : : rte_rwlock_t access_lock;
302 : :
303 : :
304 : : union {
305 : : struct vring_used_elem *shadow_used_split;
306 : : struct vring_used_elem_packed *shadow_used_packed;
307 : : };
308 : : uint16_t shadow_used_idx;
309 : : /* Record packed ring enqueue latest desc cache aligned index */
310 : : uint16_t shadow_aligned_idx;
311 : : /* Record packed ring first dequeue desc index */
312 : : uint16_t shadow_last_used_idx;
313 : :
314 : : uint16_t batch_copy_nb_elems;
315 : : struct batch_copy_elem *batch_copy_elems;
316 : : int numa_node;
317 : : bool used_wrap_counter;
318 : : bool avail_wrap_counter;
319 : :
320 : : /* Physical address of used ring, for logging */
321 : : uint16_t log_cache_nb_elem;
322 : : uint64_t log_guest_addr;
323 : : struct log_cache_entry *log_cache;
324 : :
325 : : rte_rwlock_t iotlb_lock;
326 : :
327 : : /* Used to notify the guest (trigger interrupt) */
328 : : int callfd;
329 : : /* Currently unused as polling mode is enabled */
330 : : int kickfd;
331 : :
332 : : /* Index of this vq in dev->virtqueue[] */
333 : : uint32_t index;
334 : :
335 : : /* inflight share memory info */
336 : : union {
337 : : struct rte_vhost_inflight_info_split *inflight_split;
338 : : struct rte_vhost_inflight_info_packed *inflight_packed;
339 : : };
340 : : struct rte_vhost_resubmit_info *resubmit_inflight;
341 : : uint64_t global_counter;
342 : :
343 : : struct vhost_async *async __rte_guarded_var;
344 : :
345 : : int notif_enable;
346 : : #define VIRTIO_UNINITIALIZED_NOTIF (-1)
347 : :
348 : : struct vhost_vring_addr ring_addrs;
349 : : struct virtqueue_stats stats;
350 : :
351 : : RTE_ATOMIC(bool) irq_pending;
352 : : } __rte_cache_aligned;
353 : :
354 : : /* Virtio device status as per Virtio specification */
355 : : #define VIRTIO_DEVICE_STATUS_RESET 0x00
356 : : #define VIRTIO_DEVICE_STATUS_ACK 0x01
357 : : #define VIRTIO_DEVICE_STATUS_DRIVER 0x02
358 : : #define VIRTIO_DEVICE_STATUS_DRIVER_OK 0x04
359 : : #define VIRTIO_DEVICE_STATUS_FEATURES_OK 0x08
360 : : #define VIRTIO_DEVICE_STATUS_DEV_NEED_RESET 0x40
361 : : #define VIRTIO_DEVICE_STATUS_FAILED 0x80
362 : :
363 : : #define VHOST_MAX_VRING 0x100
364 : : #define VHOST_MAX_QUEUE_PAIRS 0x80
365 : :
366 : : /* Declare IOMMU related bits for older kernels */
367 : : #ifndef VIRTIO_F_IOMMU_PLATFORM
368 : :
369 : : #define VIRTIO_F_IOMMU_PLATFORM 33
370 : :
371 : : struct vhost_iotlb_msg {
372 : : __u64 iova;
373 : : __u64 size;
374 : : __u64 uaddr;
375 : : #define VHOST_ACCESS_RO 0x1
376 : : #define VHOST_ACCESS_WO 0x2
377 : : #define VHOST_ACCESS_RW 0x3
378 : : __u8 perm;
379 : : #define VHOST_IOTLB_MISS 1
380 : : #define VHOST_IOTLB_UPDATE 2
381 : : #define VHOST_IOTLB_INVALIDATE 3
382 : : #define VHOST_IOTLB_ACCESS_FAIL 4
383 : : __u8 type;
384 : : };
385 : :
386 : : #define VHOST_IOTLB_MSG 0x1
387 : :
388 : : struct vhost_msg {
389 : : int type;
390 : : union {
391 : : struct vhost_iotlb_msg iotlb;
392 : : __u8 padding[64];
393 : : };
394 : : };
395 : : #endif
396 : :
397 : : /*
398 : : * Define virtio 1.0 for older kernels
399 : : */
400 : : #ifndef VIRTIO_F_VERSION_1
401 : : #define VIRTIO_F_VERSION_1 32
402 : : #endif
403 : :
404 : : /* Declare packed ring related bits for older kernels */
405 : : #ifndef VIRTIO_F_RING_PACKED
406 : :
407 : : #define VIRTIO_F_RING_PACKED 34
408 : :
409 : : struct vring_packed_desc {
410 : : uint64_t addr;
411 : : uint32_t len;
412 : : uint16_t id;
413 : : uint16_t flags;
414 : : };
415 : :
416 : : struct vring_packed_desc_event {
417 : : uint16_t off_wrap;
418 : : uint16_t flags;
419 : : };
420 : : #endif
421 : :
422 : : /*
423 : : * Declare below packed ring defines unconditionally
424 : : * as Kernel header might use different names.
425 : : */
426 : : #define VRING_DESC_F_AVAIL (1ULL << 7)
427 : : #define VRING_DESC_F_USED (1ULL << 15)
428 : :
429 : : #define VRING_EVENT_F_ENABLE 0x0
430 : : #define VRING_EVENT_F_DISABLE 0x1
431 : : #define VRING_EVENT_F_DESC 0x2
432 : :
433 : : /*
434 : : * Available and used descs are in same order
435 : : */
436 : : #ifndef VIRTIO_F_IN_ORDER
437 : : #define VIRTIO_F_IN_ORDER 35
438 : : #endif
439 : :
440 : : /* Features supported by this builtin vhost-user net driver. */
441 : : #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
442 : : (1ULL << VIRTIO_F_ANY_LAYOUT) | \
443 : : (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
444 : : (1ULL << VIRTIO_NET_F_MQ) | \
445 : : (1ULL << VIRTIO_F_VERSION_1) | \
446 : : (1ULL << VIRTIO_NET_F_GSO) | \
447 : : (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
448 : : (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
449 : : (1ULL << VIRTIO_NET_F_HOST_UFO) | \
450 : : (1ULL << VIRTIO_NET_F_HOST_ECN) | \
451 : : (1ULL << VIRTIO_NET_F_CSUM) | \
452 : : (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
453 : : (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
454 : : (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
455 : : (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
456 : : (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
457 : : (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
458 : : (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
459 : : (1ULL << VIRTIO_F_IN_ORDER) | \
460 : : (1ULL << VIRTIO_F_IOMMU_PLATFORM))
461 : :
462 : :
463 : : struct guest_page {
464 : : uint64_t guest_phys_addr;
465 : : uint64_t host_iova;
466 : : uint64_t host_user_addr;
467 : : uint64_t size;
468 : : };
469 : :
470 : : struct inflight_mem_info {
471 : : int fd;
472 : : void *addr;
473 : : uint64_t size;
474 : : };
475 : :
476 : : /**
477 : : * Device structure contains all configuration information relating
478 : : * to the device.
479 : : */
480 : : struct virtio_net {
481 : : /* Frontend (QEMU) memory and memory region information */
482 : : struct rte_vhost_memory *mem;
483 : : uint64_t features;
484 : : uint64_t protocol_features;
485 : : int vid;
486 : : uint32_t flags;
487 : : uint16_t vhost_hlen;
488 : : /* to tell if we need broadcast rarp packet */
489 : : RTE_ATOMIC(int16_t) broadcast_rarp;
490 : : uint32_t nr_vring;
491 : : int async_copy;
492 : :
493 : : int extbuf;
494 : : int linearbuf;
495 : : struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
496 : :
497 : : rte_rwlock_t iotlb_pending_lock;
498 : : struct vhost_iotlb_entry *iotlb_pool;
499 : : TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
500 : : TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
501 : : int iotlb_cache_nr;
502 : : rte_spinlock_t iotlb_free_lock;
503 : : SLIST_HEAD(, vhost_iotlb_entry) iotlb_free_list;
504 : :
505 : : struct inflight_mem_info *inflight_info;
506 : : #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
507 : : char ifname[IF_NAME_SZ];
508 : : uint64_t log_size;
509 : : uint64_t log_base;
510 : : uint64_t log_addr;
511 : : struct rte_ether_addr mac;
512 : : uint16_t mtu;
513 : : uint8_t status;
514 : :
515 : : struct rte_vhost_device_ops const *notify_ops;
516 : :
517 : : uint32_t nr_guest_pages;
518 : : uint32_t max_guest_pages;
519 : : struct guest_page *guest_pages;
520 : :
521 : : int backend_req_fd;
522 : : rte_spinlock_t backend_req_lock;
523 : :
524 : : int postcopy_ufd;
525 : : int postcopy_listening;
526 : : int vduse_ctrl_fd;
527 : : int vduse_dev_fd;
528 : :
529 : : struct vhost_virtqueue *cvq;
530 : :
531 : : struct rte_vdpa_device *vdpa_dev;
532 : :
533 : : /* context data for the external message handlers */
534 : : void *extern_data;
535 : : /* pre and post vhost user message handlers for the device */
536 : : struct rte_vhost_user_extern_ops extern_ops;
537 : :
538 : : struct vhost_backend_ops *backend_ops;
539 : : } __rte_cache_aligned;
540 : :
541 : : static inline void
542 [ # # ]: 0 : vq_assert_lock__(struct virtio_net *dev, struct vhost_virtqueue *vq, const char *func)
543 : : __rte_assert_exclusive_lock(&vq->access_lock)
544 : : {
545 [ # # ]: 0 : if (unlikely(!rte_rwlock_write_is_locked(&vq->access_lock)))
546 : 0 : rte_panic("VHOST_CONFIG: (%s) %s() called without access lock taken.\n",
547 : : dev->ifname, func);
548 : 0 : }
549 : : #define vq_assert_lock(dev, vq) vq_assert_lock__(dev, vq, __func__)
550 : :
551 : : static __rte_always_inline bool
552 : : vq_is_packed(struct virtio_net *dev)
553 : : {
554 [ # # # # : 0 : return dev->features & (1ull << VIRTIO_F_RING_PACKED);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
555 : : }
556 : :
557 : : static inline bool
558 : 0 : desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
559 : : {
560 : 0 : uint16_t flags = rte_atomic_load_explicit((unsigned short __rte_atomic *)&desc->flags,
561 : : rte_memory_order_acquire);
562 : :
563 [ # # ]: 0 : return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
564 [ # # ]: 0 : wrap_counter != !!(flags & VRING_DESC_F_USED);
565 : : }
566 : :
567 : : static inline void
568 : : vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num)
569 : : {
570 : 0 : vq->last_used_idx += num;
571 [ # # # # : 0 : if (vq->last_used_idx >= vq->size) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
572 : 0 : vq->used_wrap_counter ^= 1;
573 : 0 : vq->last_used_idx -= vq->size;
574 : : }
575 : : }
576 : :
577 : : static inline void
578 : : vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num)
579 : : {
580 : 0 : vq->last_avail_idx += num;
581 [ # # # # : 0 : if (vq->last_avail_idx >= vq->size) {
# # # # #
# # # # #
# # # # #
# # # #
# ]
582 : 0 : vq->avail_wrap_counter ^= 1;
583 : 0 : vq->last_avail_idx -= vq->size;
584 : : }
585 : : }
586 : :
587 : : void __vhost_log_cache_write(struct virtio_net *dev,
588 : : struct vhost_virtqueue *vq,
589 : : uint64_t addr, uint64_t len);
590 : : void __vhost_log_cache_write_iova(struct virtio_net *dev,
591 : : struct vhost_virtqueue *vq,
592 : : uint64_t iova, uint64_t len)
593 : : __rte_shared_locks_required(&vq->iotlb_lock);
594 : : void __vhost_log_cache_sync(struct virtio_net *dev,
595 : : struct vhost_virtqueue *vq);
596 : :
597 : : void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
598 : : void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
599 : : uint64_t iova, uint64_t len)
600 : : __rte_shared_locks_required(&vq->iotlb_lock);
601 : :
602 : : static __rte_always_inline void
603 : : vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
604 : : {
605 [ # # ]: 0 : if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
606 : 0 : __vhost_log_write(dev, addr, len);
607 : : }
608 : :
609 : : static __rte_always_inline void
610 : : vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
611 : : {
612 [ # # # # : 0 : if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
# # # # #
# # # # #
# # # # #
# # # ]
613 : 0 : __vhost_log_cache_sync(dev, vq);
614 : : }
615 : :
616 : : static __rte_always_inline void
617 : : vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
618 : : uint64_t addr, uint64_t len)
619 : : {
620 : : if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
621 : : __vhost_log_cache_write(dev, vq, addr, len);
622 : : }
623 : :
624 : : static __rte_always_inline void
625 : : vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
626 : : uint64_t offset, uint64_t len)
627 : : {
628 [ # # # # : 0 : if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
629 [ # # # # : 0 : if (unlikely(vq->log_guest_addr == 0))
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
630 : : return;
631 : 0 : __vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset,
632 : : len);
633 : : }
634 : : }
635 : :
636 : : static __rte_always_inline void
637 : : vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
638 : : uint64_t offset, uint64_t len)
639 : : {
640 [ # # # # : 0 : if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
# # ]
641 [ # # # # : 0 : if (unlikely(vq->log_guest_addr == 0))
# # ]
642 : : return;
643 : 0 : __vhost_log_write(dev, vq->log_guest_addr + offset, len);
644 : : }
645 : : }
646 : :
647 : : static __rte_always_inline void
648 : : vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
649 : : uint64_t iova, uint64_t len)
650 : : __rte_shared_locks_required(&vq->iotlb_lock)
651 : : {
652 [ # # # # : 0 : if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
# # # # #
# # # # #
# # # # #
# ]
653 : : return;
654 : :
655 [ # # # # : 0 : if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
# # # # #
# # # # #
# # # # #
# ]
656 : 0 : __vhost_log_cache_write_iova(dev, vq, iova, len);
657 : : else
658 : 0 : __vhost_log_cache_write(dev, vq, iova, len);
659 : : }
660 : :
661 : : static __rte_always_inline void
662 : : vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
663 : : uint64_t iova, uint64_t len)
664 : : __rte_shared_locks_required(&vq->iotlb_lock)
665 : : {
666 [ # # ]: 0 : if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
667 : : return;
668 : :
669 [ # # ]: 0 : if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
670 : 0 : __vhost_log_write_iova(dev, vq, iova, len);
671 : : else
672 : 0 : __vhost_log_write(dev, iova, len);
673 : : }
674 : :
675 : : extern int vhost_config_log_level;
676 : : #define RTE_LOGTYPE_VHOST_CONFIG vhost_config_log_level
677 : : extern int vhost_data_log_level;
678 : : #define RTE_LOGTYPE_VHOST_DATA vhost_data_log_level
679 : :
680 : : #define VHOST_CONFIG_LOG(prefix, level, fmt, args...) \
681 : : RTE_LOG_LINE(level, VHOST_CONFIG, "(%s) " fmt, prefix, ##args)
682 : :
683 : : #define VHOST_DATA_LOG(prefix, level, fmt, args...) \
684 : : RTE_LOG_DP_LINE(level, VHOST_DATA, "(%s) " fmt, prefix, ##args)
685 : :
686 : : #ifdef RTE_LIBRTE_VHOST_DEBUG
687 : : #define VHOST_MAX_PRINT_BUFF 6072
688 : : #define PRINT_PACKET(device, addr, size, header) do { \
689 : : char *pkt_addr = (char *)(addr); \
690 : : unsigned int index; \
691 : : char packet[VHOST_MAX_PRINT_BUFF]; \
692 : : \
693 : : if ((header)) \
694 : : snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
695 : : else \
696 : : snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
697 : : for (index = 0; index < (size); index++) { \
698 : : snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
699 : : "%02hhx ", pkt_addr[index]); \
700 : : } \
701 : : snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
702 : : \
703 : : RTE_LOG_DP(DEBUG, VHOST_DATA, "(%s) %s", dev->ifname, packet); \
704 : : } while (0)
705 : : #else
706 : : #define PRINT_PACKET(device, addr, size, header) do {} while (0)
707 : : #endif
708 : :
709 : : extern struct virtio_net *vhost_devices[RTE_MAX_VHOST_DEVICE];
710 : :
711 : : #define VHOST_BINARY_SEARCH_THRESH 256
712 : :
713 : 0 : static __rte_always_inline int guest_page_addrcmp(const void *p1,
714 : : const void *p2)
715 : : {
716 : : const struct guest_page *page1 = (const struct guest_page *)p1;
717 : : const struct guest_page *page2 = (const struct guest_page *)p2;
718 : :
719 [ # # ]: 0 : if (page1->guest_phys_addr > page2->guest_phys_addr)
720 : : return 1;
721 [ # # ]: 0 : if (page1->guest_phys_addr < page2->guest_phys_addr)
722 : 0 : return -1;
723 : :
724 : : return 0;
725 : : }
726 : :
727 : : static __rte_always_inline int guest_page_rangecmp(const void *p1, const void *p2)
728 : : {
729 : : const struct guest_page *page1 = (const struct guest_page *)p1;
730 : : const struct guest_page *page2 = (const struct guest_page *)p2;
731 : :
732 [ # # # # : 0 : if (page1->guest_phys_addr >= page2->guest_phys_addr) {
# # # # #
# # # # #
# # # # #
# ]
733 [ # # # # : 0 : if (page1->guest_phys_addr < page2->guest_phys_addr + page2->size)
# # # # #
# # # # #
# # # # #
# ]
734 : : return 0;
735 : : else
736 : : return 1;
737 : : } else
738 : : return -1;
739 : : }
740 : :
741 : : static __rte_always_inline rte_iova_t
742 : : gpa_to_first_hpa(struct virtio_net *dev, uint64_t gpa,
743 : : uint64_t gpa_size, uint64_t *hpa_size)
744 : : {
745 : : uint32_t i;
746 : : struct guest_page *page;
747 : : struct guest_page key;
748 : :
749 : 0 : *hpa_size = gpa_size;
750 [ # # # # : 0 : if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
# # # # #
# # # # #
# # # # #
# ]
751 : : key.guest_phys_addr = gpa;
752 : 0 : page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages,
753 : : sizeof(struct guest_page), guest_page_rangecmp);
754 [ # # # # : 0 : if (page) {
# # # # #
# # # # #
# # # # #
# ]
755 : 0 : if (gpa + gpa_size <=
756 [ # # # # : 0 : page->guest_phys_addr + page->size) {
# # # # #
# # # # #
# # # # #
# ]
757 : 0 : return gpa - page->guest_phys_addr +
758 : 0 : page->host_iova;
759 [ # # # # : 0 : } else if (gpa < page->guest_phys_addr +
# # # # #
# # # # #
# # # # #
# ]
760 : : page->size) {
761 : 0 : *hpa_size = page->guest_phys_addr +
762 : 0 : page->size - gpa;
763 : 0 : return gpa - page->guest_phys_addr +
764 : 0 : page->host_iova;
765 : : }
766 : : }
767 : : } else {
768 [ # # # # : 0 : for (i = 0; i < dev->nr_guest_pages; i++) {
# # # # #
# # # # #
# # # # #
# ]
769 : 0 : page = &dev->guest_pages[i];
770 : :
771 [ # # # # : 0 : if (gpa >= page->guest_phys_addr) {
# # # # #
# # # # #
# # # # #
# ]
772 : 0 : if (gpa + gpa_size <=
773 [ # # # # : 0 : page->guest_phys_addr + page->size) {
# # # # #
# # # # #
# # # # #
# ]
774 : 0 : return gpa - page->guest_phys_addr +
775 : 0 : page->host_iova;
776 [ # # # # : 0 : } else if (gpa < page->guest_phys_addr +
# # # # #
# # # # #
# # # # #
# ]
777 : : page->size) {
778 : 0 : *hpa_size = page->guest_phys_addr +
779 : 0 : page->size - gpa;
780 : 0 : return gpa - page->guest_phys_addr +
781 : 0 : page->host_iova;
782 : : }
783 : : }
784 : : }
785 : : }
786 : :
787 : 0 : *hpa_size = 0;
788 : 0 : return 0;
789 : : }
790 : :
791 : : /* Convert guest physical address to host physical address */
792 : : static __rte_always_inline rte_iova_t
793 : : gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
794 : : {
795 : : rte_iova_t hpa;
796 : : uint64_t hpa_size;
797 : :
798 : : hpa = gpa_to_first_hpa(dev, gpa, size, &hpa_size);
799 [ # # # # : 0 : return hpa_size == size ? hpa : 0;
# # # # #
# # # # #
# # # # #
# ]
800 : : }
801 : :
802 : : static __rte_always_inline uint64_t
803 : : hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
804 : : {
805 : : struct rte_vhost_mem_region *r;
806 : : uint32_t i;
807 : :
808 [ # # # # : 0 : if (unlikely(!dev || !dev->mem))
# # # # #
# ]
809 : : return 0;
810 : :
811 [ # # # # : 0 : for (i = 0; i < dev->mem->nregions; i++) {
# # ]
812 : : r = &dev->mem->regions[i];
813 : :
814 [ # # # # : 0 : if (vva >= r->host_user_addr &&
# # ]
815 [ # # # # : 0 : vva + len < r->host_user_addr + r->size) {
# # ]
816 : 0 : return r->guest_phys_addr + vva - r->host_user_addr;
817 : : }
818 : : }
819 : : return 0;
820 : : }
821 : :
822 : : static __rte_always_inline struct virtio_net *
823 : : get_device(int vid)
824 : : {
825 : : struct virtio_net *dev = NULL;
826 : :
827 [ # # # # : 0 : if (likely(vid >= 0 && vid < RTE_MAX_VHOST_DEVICE))
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
828 : 0 : dev = vhost_devices[vid];
829 : :
830 [ # # # # : 0 : if (unlikely(!dev)) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
831 : 0 : VHOST_CONFIG_LOG("device", ERR, "(%d) device not found.", vid);
832 : : }
833 : :
834 : : return dev;
835 : : }
836 : :
837 : : int vhost_new_device(struct vhost_backend_ops *ops);
838 : : void cleanup_device(struct virtio_net *dev, int destroy);
839 : : void reset_device(struct virtio_net *dev);
840 : : void vhost_destroy_device(int);
841 : : void vhost_destroy_device_notify(struct virtio_net *dev);
842 : :
843 : : void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
844 : : void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
845 : : void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
846 : :
847 : : int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
848 : :
849 : : void vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *dev);
850 : :
851 : : void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
852 : : void vhost_setup_virtio_net(int vid, bool enable, bool legacy_ol_flags, bool stats_enabled,
853 : : bool support_iommu);
854 : : void vhost_enable_extbuf(int vid);
855 : : void vhost_enable_linearbuf(int vid);
856 : : int vhost_enable_guest_notification(struct virtio_net *dev,
857 : : struct vhost_virtqueue *vq, int enable);
858 : :
859 : : struct rte_vhost_device_ops const *vhost_driver_callback_get(const char *path);
860 : :
861 : : /*
862 : : * Backend-specific cleanup.
863 : : *
864 : : * TODO: fix it; we have one backend now
865 : : */
866 : : void vhost_backend_cleanup(struct virtio_net *dev);
867 : :
868 : : uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
869 : : uint64_t iova, uint64_t *len, uint8_t perm)
870 : : __rte_shared_locks_required(&vq->iotlb_lock);
871 : : void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
872 : : struct vhost_virtqueue *vq,
873 : : uint64_t desc_addr, uint64_t desc_len)
874 : : __rte_shared_locks_required(&vq->iotlb_lock);
875 : : int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
876 : : __rte_shared_locks_required(&vq->iotlb_lock);
877 : : uint64_t translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
878 : : uint64_t log_addr)
879 : : __rte_shared_locks_required(&vq->iotlb_lock);
880 : : void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
881 : :
882 : : static __rte_always_inline uint64_t
883 : : vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
884 : : uint64_t iova, uint64_t *len, uint8_t perm)
885 : : __rte_shared_locks_required(&vq->iotlb_lock)
886 : : {
887 [ # # # # : 0 : if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
888 : 0 : return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
889 : :
890 : 0 : return __vhost_iova_to_vva(dev, vq, iova, len, perm);
891 : : }
892 : :
893 : : #define vhost_avail_event(vr) \
894 : : (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
895 : : #define vhost_used_event(vr) \
896 : : (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
897 : :
898 : : /*
899 : : * The following is used with VIRTIO_RING_F_EVENT_IDX.
900 : : * Assuming a given event_idx value from the other size, if we have
901 : : * just incremented index from old to new_idx, should we trigger an
902 : : * event?
903 : : */
904 : : static __rte_always_inline int
905 : : vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
906 : : {
907 : 0 : return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
908 : : }
909 : :
910 : : static __rte_always_inline void
911 : : vhost_vring_inject_irq(struct virtio_net *dev, struct vhost_virtqueue *vq)
912 : : {
913 : 0 : bool expected = false;
914 : :
915 [ # # # # : 0 : if (dev->notify_ops->guest_notify) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
916 [ # # # # : 0 : if (rte_atomic_compare_exchange_strong_explicit(&vq->irq_pending, &expected, true,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
917 : : rte_memory_order_release, rte_memory_order_relaxed)) {
918 [ # # # # : 0 : if (dev->notify_ops->guest_notify(dev->vid, vq->index)) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
919 [ # # # # : 0 : if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
920 : 0 : rte_atomic_fetch_add_explicit(
921 : : &vq->stats.guest_notifications_offloaded,
922 : : 1, rte_memory_order_relaxed);
923 : 0 : return;
924 : : }
925 : :
926 : : /* Offloading failed, fallback to direct IRQ injection */
927 : 0 : rte_atomic_store_explicit(&vq->irq_pending, false,
928 : : rte_memory_order_release);
929 : : } else {
930 : 0 : vq->stats.guest_notifications_suppressed++;
931 : 0 : return;
932 : : }
933 : : }
934 : :
935 [ # # # # : 0 : if (dev->backend_ops->inject_irq(dev, vq)) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
936 [ # # # # : 0 : if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
937 : 0 : rte_atomic_fetch_add_explicit(&vq->stats.guest_notifications_error,
938 : : 1, rte_memory_order_relaxed);
939 : : return;
940 : : }
941 : :
942 [ # # # # : 0 : if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
943 : 0 : rte_atomic_fetch_add_explicit(&vq->stats.guest_notifications,
944 : : 1, rte_memory_order_relaxed);
945 [ # # # # : 0 : if (dev->notify_ops->guest_notified)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
946 : 0 : dev->notify_ops->guest_notified(dev->vid);
947 : : }
948 : :
949 : : static __rte_always_inline void
950 : : vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
951 : : {
952 : : /* Flush used->idx update before we read avail->flags. */
953 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
954 : :
955 : : /* Don't kick guest if we don't reach index specified by guest. */
956 [ # # # # : 0 : if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
# # # # #
# # # # #
# # # # #
# # # #
# ]
957 : 0 : uint16_t old = vq->signalled_used;
958 : 0 : uint16_t new = vq->last_used_idx;
959 : 0 : bool signalled_used_valid = vq->signalled_used_valid;
960 : :
961 : 0 : vq->signalled_used = new;
962 : 0 : vq->signalled_used_valid = true;
963 : :
964 : : VHOST_DATA_LOG(dev->ifname, DEBUG,
965 : : "%s: used_event_idx=%d, old=%d, new=%d",
966 : : __func__, vhost_used_event(vq), old, new);
967 : :
968 [ # # # # : 0 : if (vhost_need_event(vhost_used_event(vq), new, old) ||
# # # # #
# # # # #
# # # # #
# # # #
# ]
969 [ # # # # : 0 : unlikely(!signalled_used_valid))
# # # # #
# # # # #
# # # # #
# # # #
# ]
970 : : vhost_vring_inject_irq(dev, vq);
971 : : } else {
972 : : /* Kick the guest if necessary. */
973 [ # # # # : 0 : if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
# # # # #
# # # # #
# # # # #
# # # #
# ]
974 : : vhost_vring_inject_irq(dev, vq);
975 : : }
976 : : }
977 : :
978 : : static __rte_always_inline void
979 : : vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
980 : : {
981 : : uint16_t old, new, off, off_wrap;
982 : : bool signalled_used_valid, kick = false;
983 : :
984 : : /* Flush used desc update. */
985 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
986 : :
987 [ # # # # : 0 : if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
# # # # #
# # # # #
# # # # #
# # # #
# ]
988 [ # # # # : 0 : if (vq->driver_event->flags !=
# # # # #
# # # # #
# # # # #
# # # #
# ]
989 : : VRING_EVENT_F_DISABLE)
990 : : kick = true;
991 : 0 : goto kick;
992 : : }
993 : :
994 : 0 : old = vq->signalled_used;
995 : 0 : new = vq->last_used_idx;
996 : 0 : vq->signalled_used = new;
997 : 0 : signalled_used_valid = vq->signalled_used_valid;
998 : 0 : vq->signalled_used_valid = true;
999 : :
1000 [ # # # # : 0 : if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
# # # # #
# # # # #
# # # # #
# # # #
# ]
1001 [ # # # # : 0 : if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
# # # # #
# # # # #
# # # # #
# # # #
# ]
1002 : : kick = true;
1003 : 0 : goto kick;
1004 : : }
1005 : :
1006 [ # # # # : 0 : if (unlikely(!signalled_used_valid)) {
# # # # #
# # # # #
# # # # #
# # # #
# ]
1007 : : kick = true;
1008 : 0 : goto kick;
1009 : : }
1010 : :
1011 : : rte_atomic_thread_fence(rte_memory_order_acquire);
1012 : :
1013 : 0 : off_wrap = vq->driver_event->off_wrap;
1014 : 0 : off = off_wrap & ~(1 << 15);
1015 : :
1016 [ # # # # : 0 : if (new <= old)
# # # # #
# # # # #
# # # # #
# # # #
# ]
1017 : 0 : old -= vq->size;
1018 : :
1019 [ # # # # : 0 : if (vq->used_wrap_counter != off_wrap >> 15)
# # # # #
# # # # #
# # # # #
# # # #
# ]
1020 : 0 : off -= vq->size;
1021 : :
1022 [ # # # # : 0 : if (vhost_need_event(off, new, old))
# # # # #
# # # # #
# # # # #
# # # #
# ]
1023 : : kick = true;
1024 : 0 : kick:
1025 : : if (kick)
1026 : : vhost_vring_inject_irq(dev, vq);
1027 : : }
1028 : :
1029 : : static __rte_always_inline void
1030 : : free_ind_table(void *idesc)
1031 : : {
1032 : 0 : rte_free(idesc);
1033 : 0 : }
1034 : :
1035 : : static __rte_always_inline void
1036 : : restore_mbuf(struct rte_mbuf *m)
1037 : : {
1038 : : uint32_t mbuf_size, priv_size;
1039 : :
1040 : : while (m) {
1041 : : priv_size = rte_pktmbuf_priv_size(m->pool);
1042 : : mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1043 : : /* start of buffer is after mbuf structure and priv data */
1044 : :
1045 : : m->buf_addr = (char *)m + mbuf_size;
1046 : : rte_mbuf_iova_set(m, rte_mempool_virt2iova(m) + mbuf_size);
1047 : : m = m->next;
1048 : : }
1049 : : }
1050 : :
1051 : : static __rte_always_inline bool
1052 : : mbuf_is_consumed(struct rte_mbuf *m)
1053 : : {
1054 : : while (m) {
1055 : : if (rte_mbuf_refcnt_read(m) > 1)
1056 : : return false;
1057 : : m = m->next;
1058 : : }
1059 : :
1060 : : return true;
1061 : : }
1062 : :
1063 : : void mem_set_dump(struct virtio_net *dev, void *ptr, size_t size, bool enable, uint64_t alignment);
1064 : :
1065 : : #endif /* _VHOST_NET_CDEV_H_ */
|