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