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>
22 #include "rte_vhost.h"
25 /* Used to indicate that the device is running on a data core */
26 #define VIRTIO_DEV_RUNNING 1
27 /* Used to indicate that the device is ready to operate */
28 #define VIRTIO_DEV_READY 2
29 /* Used to indicate that the built-in vhost net device backend is enabled */
30 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
31 /* Used to indicate that the device has its own data path and configured */
32 #define VIRTIO_DEV_VDPA_CONFIGURED 8
34 /* Backend value set by guest. */
35 #define VIRTIO_DEV_STOPPED -1
37 #define BUF_VECTOR_MAX 256
40 * Structure contains buffer address, length and descriptor index
41 * from vring to do scatter RX.
50 * A structure to hold some fields needed in zero copy code path,
51 * mainly for associating an mbuf with the right desc_idx.
54 struct rte_mbuf *mbuf;
58 TAILQ_ENTRY(zcopy_mbuf) next;
60 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
63 * Structure contains the info for each batched memory copy.
65 struct batch_copy_elem {
73 * Structure contains variables relevant to RX/TX virtqueues.
75 struct vhost_virtqueue {
76 struct vring_desc *desc;
77 struct vring_avail *avail;
78 struct vring_used *used;
81 uint16_t last_avail_idx;
82 uint16_t last_used_idx;
83 /* Last used index we notify to front end. */
84 uint16_t signalled_used;
85 #define VIRTIO_INVALID_EVENTFD (-1)
86 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
88 /* Backend value to determine if device should started/stopped */
92 rte_spinlock_t access_lock;
94 /* Used to notify the guest (trigger interrupt) */
96 /* Currently unused as polling mode is enabled */
99 /* Physical address of used ring, for logging */
100 uint64_t log_guest_addr;
104 uint16_t last_zmbuf_idx;
105 struct zcopy_mbuf *zmbufs;
106 struct zcopy_mbuf_list zmbuf_list;
108 struct vring_used_elem *shadow_used_ring;
109 uint16_t shadow_used_idx;
110 struct vhost_vring_addr ring_addrs;
112 struct batch_copy_elem *batch_copy_elems;
113 uint16_t batch_copy_nb_elems;
115 rte_rwlock_t iotlb_lock;
116 rte_rwlock_t iotlb_pending_lock;
117 struct rte_mempool *iotlb_pool;
118 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
120 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
121 } __rte_cache_aligned;
123 /* Old kernels have no such macros defined */
124 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
125 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
128 #ifndef VIRTIO_NET_F_MQ
129 #define VIRTIO_NET_F_MQ 22
132 #define VHOST_MAX_VRING 0x100
133 #define VHOST_MAX_QUEUE_PAIRS 0x80
135 #ifndef VIRTIO_NET_F_MTU
136 #define VIRTIO_NET_F_MTU 3
139 #ifndef VIRTIO_F_ANY_LAYOUT
140 #define VIRTIO_F_ANY_LAYOUT 27
143 /* Declare IOMMU related bits for older kernels */
144 #ifndef VIRTIO_F_IOMMU_PLATFORM
146 #define VIRTIO_F_IOMMU_PLATFORM 33
148 struct vhost_iotlb_msg {
152 #define VHOST_ACCESS_RO 0x1
153 #define VHOST_ACCESS_WO 0x2
154 #define VHOST_ACCESS_RW 0x3
156 #define VHOST_IOTLB_MISS 1
157 #define VHOST_IOTLB_UPDATE 2
158 #define VHOST_IOTLB_INVALIDATE 3
159 #define VHOST_IOTLB_ACCESS_FAIL 4
163 #define VHOST_IOTLB_MSG 0x1
168 struct vhost_iotlb_msg iotlb;
175 * Define virtio 1.0 for older kernels
177 #ifndef VIRTIO_F_VERSION_1
178 #define VIRTIO_F_VERSION_1 32
181 /* Features supported by this builtin vhost-user net driver. */
182 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
183 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
184 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
185 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
186 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
187 (1ULL << VIRTIO_NET_F_MQ) | \
188 (1ULL << VIRTIO_F_VERSION_1) | \
189 (1ULL << VHOST_F_LOG_ALL) | \
190 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
191 (1ULL << VIRTIO_NET_F_GSO) | \
192 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
193 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
194 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
195 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
196 (1ULL << VIRTIO_NET_F_CSUM) | \
197 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
198 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
199 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
200 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
201 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
202 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
203 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
204 (1ULL << VIRTIO_NET_F_MTU) | \
205 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
209 uint64_t guest_phys_addr;
210 uint64_t host_phys_addr;
215 * function prototype for the vhost backend to handler specific vhost user
216 * messages prior to the master message handling
222 * @param require_reply
223 * If the handler requires sending a reply, this varaible shall be written 1,
226 * If the handler requires skipping the master message handling, this variable
227 * shall be written 1, otherwise 0.
229 * 0 on success, -1 on failure
231 typedef int (*vhost_msg_pre_handle)(int vid, void *msg,
232 uint32_t *require_reply, uint32_t *skip_master);
235 * function prototype for the vhost backend to handler specific vhost user
236 * messages after the master message handling is done
242 * @param require_reply
243 * If the handler requires sending a reply, this varaible shall be written 1,
246 * 0 on success, -1 on failure
248 typedef int (*vhost_msg_post_handle)(int vid, void *msg,
249 uint32_t *require_reply);
252 * pre and post vhost user message handlers
254 struct vhost_user_extern_ops {
255 vhost_msg_pre_handle pre_msg_handle;
256 vhost_msg_post_handle post_msg_handle;
260 * Device structure contains all configuration information relating
264 /* Frontend (QEMU) memory and memory region information */
265 struct rte_vhost_memory *mem;
267 uint64_t protocol_features;
271 /* to tell if we need broadcast rarp packet */
272 rte_atomic16_t broadcast_rarp;
274 int dequeue_zero_copy;
275 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
276 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
277 char ifname[IF_NAME_SZ];
281 struct ether_addr mac;
284 struct vhost_device_ops const *notify_ops;
286 uint32_t nr_guest_pages;
287 uint32_t max_guest_pages;
288 struct guest_page *guest_pages;
293 * Device id to identify a specific backend device.
294 * It's set to -1 for the default software implementation.
298 /* private data for virtio device */
300 /* pre and post vhost user message handlers for the device */
301 struct vhost_user_extern_ops extern_ops;
302 } __rte_cache_aligned;
304 #define VHOST_LOG_PAGE 4096
307 * Atomically set a bit in memory.
309 static __rte_always_inline void
310 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
312 __sync_fetch_and_or_8(addr, (1U << nr));
315 static __rte_always_inline void
316 vhost_log_page(uint8_t *log_base, uint64_t page)
318 vhost_set_bit(page % 8, &log_base[page / 8]);
321 static __rte_always_inline void
322 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
326 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
327 !dev->log_base || !len))
330 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
333 /* To make sure guest memory updates are committed before logging */
336 page = addr / VHOST_LOG_PAGE;
337 while (page * VHOST_LOG_PAGE < addr + len) {
338 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
343 static __rte_always_inline void
344 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
345 uint64_t offset, uint64_t len)
347 vhost_log_write(dev, vq->log_guest_addr + offset, len);
350 /* Macros for printing using RTE_LOG */
351 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
352 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
354 #ifdef RTE_LIBRTE_VHOST_DEBUG
355 #define VHOST_MAX_PRINT_BUFF 6072
356 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
357 RTE_LOG(DEBUG, log_type, fmt, ##args)
358 #define PRINT_PACKET(device, addr, size, header) do { \
359 char *pkt_addr = (char *)(addr); \
360 unsigned int index; \
361 char packet[VHOST_MAX_PRINT_BUFF]; \
364 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
366 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
367 for (index = 0; index < (size); index++) { \
368 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
369 "%02hhx ", pkt_addr[index]); \
371 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
373 VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
376 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
377 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
380 extern uint64_t VHOST_FEATURES;
381 #define MAX_VHOST_DEVICE 1024
382 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
384 /* Convert guest physical address to host physical address */
385 static __rte_always_inline rte_iova_t
386 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
389 struct guest_page *page;
391 for (i = 0; i < dev->nr_guest_pages; i++) {
392 page = &dev->guest_pages[i];
394 if (gpa >= page->guest_phys_addr &&
395 gpa + size < page->guest_phys_addr + page->size) {
396 return gpa - page->guest_phys_addr +
397 page->host_phys_addr;
404 static __rte_always_inline struct virtio_net *
407 struct virtio_net *dev = vhost_devices[vid];
409 if (unlikely(!dev)) {
410 RTE_LOG(ERR, VHOST_CONFIG,
411 "(%d) device not found.\n", vid);
417 int vhost_new_device(void);
418 void cleanup_device(struct virtio_net *dev, int destroy);
419 void reset_device(struct virtio_net *dev);
420 void vhost_destroy_device(int);
422 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
423 void free_vq(struct vhost_virtqueue *vq);
425 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
427 void vhost_attach_vdpa_device(int vid, int did);
428 void vhost_detach_vdpa_device(int vid);
430 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
431 void vhost_enable_dequeue_zero_copy(int vid);
432 void vhost_set_builtin_virtio_net(int vid, bool enable);
434 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
437 * Backend-specific cleanup.
439 * TODO: fix it; we have one backend now
441 void vhost_backend_cleanup(struct virtio_net *dev);
443 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
444 uint64_t iova, uint64_t *len, uint8_t perm);
445 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
446 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
448 static __rte_always_inline uint64_t
449 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
450 uint64_t iova, uint64_t *len, uint8_t perm)
452 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
453 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
455 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
458 #define vhost_used_event(vr) \
459 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
462 * The following is used with VIRTIO_RING_F_EVENT_IDX.
463 * Assuming a given event_idx value from the other size, if we have
464 * just incremented index from old to new_idx, should we trigger an
467 static __rte_always_inline int
468 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
470 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
473 static __rte_always_inline void
474 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
476 /* Flush used->idx update before we read avail->flags. */
479 /* Don't kick guest if we don't reach index specified by guest. */
480 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
481 uint16_t old = vq->signalled_used;
482 uint16_t new = vq->last_used_idx;
484 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
486 vhost_used_event(vq),
488 if (vhost_need_event(vhost_used_event(vq), new, old)
489 && (vq->callfd >= 0)) {
490 vq->signalled_used = vq->last_used_idx;
491 eventfd_write(vq->callfd, (eventfd_t) 1);
494 /* Kick the guest if necessary. */
495 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
496 && (vq->callfd >= 0))
497 eventfd_write(vq->callfd, (eventfd_t)1);
501 #endif /* _VHOST_NET_CDEV_H_ */