1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
5 #ifndef _VHOST_NET_CDEV_H_
6 #define _VHOST_NET_CDEV_H_
10 #include <sys/types.h>
11 #include <sys/queue.h>
13 #include <linux/vhost.h>
14 #include <linux/virtio_net.h>
15 #include <sys/socket.h>
19 #include <rte_ether.h>
20 #include <rte_rwlock.h>
21 #include <rte_malloc.h>
23 #include "rte_vhost.h"
26 /* Used to indicate that the device is running on a data core */
27 #define VIRTIO_DEV_RUNNING 1
28 /* Used to indicate that the device is ready to operate */
29 #define VIRTIO_DEV_READY 2
30 /* Used to indicate that the built-in vhost net device backend is enabled */
31 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
32 /* Used to indicate that the device has its own data path and configured */
33 #define VIRTIO_DEV_VDPA_CONFIGURED 8
35 /* Backend value set by guest. */
36 #define VIRTIO_DEV_STOPPED -1
38 #define BUF_VECTOR_MAX 256
40 #define VHOST_LOG_CACHE_NR 32
43 * Structure contains buffer address, length and descriptor index
44 * from vring to do scatter RX.
54 * A structure to hold some fields needed in zero copy code path,
55 * mainly for associating an mbuf with the right desc_idx.
58 struct rte_mbuf *mbuf;
63 TAILQ_ENTRY(zcopy_mbuf) next;
65 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
68 * Structure contains the info for each batched memory copy.
70 struct batch_copy_elem {
78 * Structure that contains the info for batched dirty logging.
80 struct log_cache_entry {
85 struct vring_used_elem_packed {
92 * Structure contains variables relevant to RX/TX virtqueues.
94 struct vhost_virtqueue {
96 struct vring_desc *desc;
97 struct vring_packed_desc *desc_packed;
100 struct vring_avail *avail;
101 struct vring_packed_desc_event *driver_event;
104 struct vring_used *used;
105 struct vring_packed_desc_event *device_event;
109 uint16_t last_avail_idx;
110 uint16_t last_used_idx;
111 /* Last used index we notify to front end. */
112 uint16_t signalled_used;
113 bool signalled_used_valid;
114 #define VIRTIO_INVALID_EVENTFD (-1)
115 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
117 /* Backend value to determine if device should started/stopped */
121 rte_spinlock_t access_lock;
123 /* Used to notify the guest (trigger interrupt) */
125 /* Currently unused as polling mode is enabled */
128 /* Physical address of used ring, for logging */
129 uint64_t log_guest_addr;
131 /* inflight share memory info */
133 struct rte_vhost_inflight_info_split *inflight_split;
134 struct rte_vhost_inflight_info_packed *inflight_packed;
136 struct rte_vhost_resubmit_info *resubmit_inflight;
137 uint64_t global_counter;
141 uint16_t last_zmbuf_idx;
142 struct zcopy_mbuf *zmbufs;
143 struct zcopy_mbuf_list zmbuf_list;
146 struct vring_used_elem *shadow_used_split;
147 struct vring_used_elem_packed *shadow_used_packed;
149 uint16_t shadow_used_idx;
150 struct vhost_vring_addr ring_addrs;
152 struct batch_copy_elem *batch_copy_elems;
153 uint16_t batch_copy_nb_elems;
154 bool used_wrap_counter;
155 bool avail_wrap_counter;
157 struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
158 uint16_t log_cache_nb_elem;
160 rte_rwlock_t iotlb_lock;
161 rte_rwlock_t iotlb_pending_lock;
162 struct rte_mempool *iotlb_pool;
163 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
165 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
166 } __rte_cache_aligned;
168 /* Old kernels have no such macros defined */
169 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
170 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
173 #ifndef VIRTIO_NET_F_MQ
174 #define VIRTIO_NET_F_MQ 22
177 #define VHOST_MAX_VRING 0x100
178 #define VHOST_MAX_QUEUE_PAIRS 0x80
180 #ifndef VIRTIO_NET_F_MTU
181 #define VIRTIO_NET_F_MTU 3
184 #ifndef VIRTIO_F_ANY_LAYOUT
185 #define VIRTIO_F_ANY_LAYOUT 27
188 /* Declare IOMMU related bits for older kernels */
189 #ifndef VIRTIO_F_IOMMU_PLATFORM
191 #define VIRTIO_F_IOMMU_PLATFORM 33
193 struct vhost_iotlb_msg {
197 #define VHOST_ACCESS_RO 0x1
198 #define VHOST_ACCESS_WO 0x2
199 #define VHOST_ACCESS_RW 0x3
201 #define VHOST_IOTLB_MISS 1
202 #define VHOST_IOTLB_UPDATE 2
203 #define VHOST_IOTLB_INVALIDATE 3
204 #define VHOST_IOTLB_ACCESS_FAIL 4
208 #define VHOST_IOTLB_MSG 0x1
213 struct vhost_iotlb_msg iotlb;
220 * Define virtio 1.0 for older kernels
222 #ifndef VIRTIO_F_VERSION_1
223 #define VIRTIO_F_VERSION_1 32
226 /* Declare packed ring related bits for older kernels */
227 #ifndef VIRTIO_F_RING_PACKED
229 #define VIRTIO_F_RING_PACKED 34
231 struct vring_packed_desc {
238 struct vring_packed_desc_event {
245 * Declare below packed ring defines unconditionally
246 * as Kernel header might use different names.
248 #define VRING_DESC_F_AVAIL (1ULL << 7)
249 #define VRING_DESC_F_USED (1ULL << 15)
251 #define VRING_EVENT_F_ENABLE 0x0
252 #define VRING_EVENT_F_DISABLE 0x1
253 #define VRING_EVENT_F_DESC 0x2
256 * Available and used descs are in same order
258 #ifndef VIRTIO_F_IN_ORDER
259 #define VIRTIO_F_IN_ORDER 35
262 /* Features supported by this builtin vhost-user net driver. */
263 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
264 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
265 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
266 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
267 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
268 (1ULL << VIRTIO_NET_F_MQ) | \
269 (1ULL << VIRTIO_F_VERSION_1) | \
270 (1ULL << VHOST_F_LOG_ALL) | \
271 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
272 (1ULL << VIRTIO_NET_F_GSO) | \
273 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
274 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
275 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
276 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
277 (1ULL << VIRTIO_NET_F_CSUM) | \
278 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
279 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
280 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
281 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
282 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
283 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
284 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
285 (1ULL << VIRTIO_NET_F_MTU) | \
286 (1ULL << VIRTIO_F_IN_ORDER) | \
287 (1ULL << VIRTIO_F_IOMMU_PLATFORM) | \
288 (1ULL << VIRTIO_F_RING_PACKED))
292 uint64_t guest_phys_addr;
293 uint64_t host_phys_addr;
297 struct inflight_mem_info {
304 * Device structure contains all configuration information relating
308 /* Frontend (QEMU) memory and memory region information */
309 struct rte_vhost_memory *mem;
311 uint64_t protocol_features;
315 /* to tell if we need broadcast rarp packet */
316 rte_atomic16_t broadcast_rarp;
318 int dequeue_zero_copy;
319 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
320 struct inflight_mem_info *inflight_info;
321 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
322 char ifname[IF_NAME_SZ];
326 struct rte_ether_addr mac;
329 struct vhost_device_ops const *notify_ops;
331 uint32_t nr_guest_pages;
332 uint32_t max_guest_pages;
333 struct guest_page *guest_pages;
336 rte_spinlock_t slave_req_lock;
339 int postcopy_listening;
342 * Device id to identify a specific backend device.
343 * It's set to -1 for the default software implementation.
347 /* context data for the external message handlers */
349 /* pre and post vhost user message handlers for the device */
350 struct rte_vhost_user_extern_ops extern_ops;
351 } __rte_cache_aligned;
353 static __rte_always_inline bool
354 vq_is_packed(struct virtio_net *dev)
356 return dev->features & (1ull << VIRTIO_F_RING_PACKED);
360 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
362 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
364 return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
365 wrap_counter != !!(flags & VRING_DESC_F_USED);
368 void __vhost_log_cache_write(struct virtio_net *dev,
369 struct vhost_virtqueue *vq,
370 uint64_t addr, uint64_t len);
371 void __vhost_log_cache_write_iova(struct virtio_net *dev,
372 struct vhost_virtqueue *vq,
373 uint64_t iova, uint64_t len);
374 void __vhost_log_cache_sync(struct virtio_net *dev,
375 struct vhost_virtqueue *vq);
376 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
377 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
378 uint64_t iova, uint64_t len);
380 static __rte_always_inline void
381 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
383 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
384 __vhost_log_write(dev, addr, len);
387 static __rte_always_inline void
388 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
390 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
391 __vhost_log_cache_sync(dev, vq);
394 static __rte_always_inline void
395 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
396 uint64_t addr, uint64_t len)
398 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
399 __vhost_log_cache_write(dev, vq, addr, len);
402 static __rte_always_inline void
403 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
404 uint64_t offset, uint64_t len)
406 vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
409 static __rte_always_inline void
410 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
411 uint64_t offset, uint64_t len)
413 vhost_log_write(dev, vq->log_guest_addr + offset, len);
416 static __rte_always_inline void
417 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
418 uint64_t iova, uint64_t len)
420 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
423 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
424 __vhost_log_cache_write_iova(dev, vq, iova, len);
426 __vhost_log_cache_write(dev, vq, iova, len);
429 static __rte_always_inline void
430 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
431 uint64_t iova, uint64_t len)
433 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
436 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
437 __vhost_log_write_iova(dev, vq, iova, len);
439 __vhost_log_write(dev, iova, len);
442 /* Macros for printing using RTE_LOG */
443 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
444 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
446 #ifdef RTE_LIBRTE_VHOST_DEBUG
447 #define VHOST_MAX_PRINT_BUFF 6072
448 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
449 RTE_LOG(DEBUG, log_type, fmt, ##args)
450 #define PRINT_PACKET(device, addr, size, header) do { \
451 char *pkt_addr = (char *)(addr); \
452 unsigned int index; \
453 char packet[VHOST_MAX_PRINT_BUFF]; \
456 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
458 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
459 for (index = 0; index < (size); index++) { \
460 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
461 "%02hhx ", pkt_addr[index]); \
463 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
465 VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
468 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
469 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
472 extern uint64_t VHOST_FEATURES;
473 #define MAX_VHOST_DEVICE 1024
474 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
476 /* Convert guest physical address to host physical address */
477 static __rte_always_inline rte_iova_t
478 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
481 struct guest_page *page;
483 for (i = 0; i < dev->nr_guest_pages; i++) {
484 page = &dev->guest_pages[i];
486 if (gpa >= page->guest_phys_addr &&
487 gpa + size < page->guest_phys_addr + page->size) {
488 return gpa - page->guest_phys_addr +
489 page->host_phys_addr;
496 static __rte_always_inline uint64_t
497 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
499 struct rte_vhost_mem_region *r;
502 if (unlikely(!dev || !dev->mem))
505 for (i = 0; i < dev->mem->nregions; i++) {
506 r = &dev->mem->regions[i];
508 if (vva >= r->host_user_addr &&
509 vva + len < r->host_user_addr + r->size) {
510 return r->guest_phys_addr + vva - r->host_user_addr;
516 static __rte_always_inline struct virtio_net *
519 struct virtio_net *dev = vhost_devices[vid];
521 if (unlikely(!dev)) {
522 RTE_LOG(ERR, VHOST_CONFIG,
523 "(%d) device not found.\n", vid);
529 int vhost_new_device(void);
530 void cleanup_device(struct virtio_net *dev, int destroy);
531 void reset_device(struct virtio_net *dev);
532 void vhost_destroy_device(int);
533 void vhost_destroy_device_notify(struct virtio_net *dev);
535 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
536 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
537 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
539 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
541 void vhost_attach_vdpa_device(int vid, int did);
543 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
544 void vhost_enable_dequeue_zero_copy(int vid);
545 void vhost_set_builtin_virtio_net(int vid, bool enable);
547 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
550 * Backend-specific cleanup.
552 * TODO: fix it; we have one backend now
554 void vhost_backend_cleanup(struct virtio_net *dev);
556 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
557 uint64_t iova, uint64_t *len, uint8_t perm);
558 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
559 struct vhost_virtqueue *vq,
560 uint64_t desc_addr, uint64_t desc_len);
561 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
562 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
564 static __rte_always_inline uint64_t
565 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
566 uint64_t iova, uint64_t *len, uint8_t perm)
568 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
569 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
571 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
574 #define vhost_avail_event(vr) \
575 (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
576 #define vhost_used_event(vr) \
577 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
580 * The following is used with VIRTIO_RING_F_EVENT_IDX.
581 * Assuming a given event_idx value from the other size, if we have
582 * just incremented index from old to new_idx, should we trigger an
585 static __rte_always_inline int
586 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
588 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
591 static __rte_always_inline void
592 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
594 /* Flush used->idx update before we read avail->flags. */
597 /* Don't kick guest if we don't reach index specified by guest. */
598 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
599 uint16_t old = vq->signalled_used;
600 uint16_t new = vq->last_used_idx;
601 bool signalled_used_valid = vq->signalled_used_valid;
603 vq->signalled_used = new;
604 vq->signalled_used_valid = true;
606 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
608 vhost_used_event(vq),
611 if ((vhost_need_event(vhost_used_event(vq), new, old) &&
612 (vq->callfd >= 0)) ||
613 unlikely(!signalled_used_valid)) {
614 eventfd_write(vq->callfd, (eventfd_t) 1);
615 if (dev->notify_ops->guest_notified)
616 dev->notify_ops->guest_notified(dev->vid);
619 /* Kick the guest if necessary. */
620 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
621 && (vq->callfd >= 0)) {
622 eventfd_write(vq->callfd, (eventfd_t)1);
623 if (dev->notify_ops->guest_notified)
624 dev->notify_ops->guest_notified(dev->vid);
629 static __rte_always_inline void
630 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
632 uint16_t old, new, off, off_wrap;
633 bool signalled_used_valid, kick = false;
635 /* Flush used desc update. */
638 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
639 if (vq->driver_event->flags !=
640 VRING_EVENT_F_DISABLE)
645 old = vq->signalled_used;
646 new = vq->last_used_idx;
647 vq->signalled_used = new;
648 signalled_used_valid = vq->signalled_used_valid;
649 vq->signalled_used_valid = true;
651 if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
652 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
657 if (unlikely(!signalled_used_valid)) {
664 off_wrap = vq->driver_event->off_wrap;
665 off = off_wrap & ~(1 << 15);
670 if (vq->used_wrap_counter != off_wrap >> 15)
673 if (vhost_need_event(off, new, old))
677 eventfd_write(vq->callfd, (eventfd_t)1);
678 if (dev->notify_ops->guest_notified)
679 dev->notify_ops->guest_notified(dev->vid);
683 static __rte_always_inline void
684 free_ind_table(void *idesc)
689 static __rte_always_inline void
690 restore_mbuf(struct rte_mbuf *m)
692 uint32_t mbuf_size, priv_size;
695 priv_size = rte_pktmbuf_priv_size(m->pool);
696 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
697 /* start of buffer is after mbuf structure and priv data */
699 m->buf_addr = (char *)m + mbuf_size;
700 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
705 static __rte_always_inline bool
706 mbuf_is_consumed(struct rte_mbuf *m)
709 if (rte_mbuf_refcnt_read(m) > 1)
717 static __rte_always_inline void
718 put_zmbuf(struct zcopy_mbuf *zmbuf)
723 #endif /* _VHOST_NET_CDEV_H_ */