2017-12-19 15:49:03 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
2018-04-05 16:01:30 +00:00
|
|
|
* Copyright(c) 2010-2018 Intel Corporation
|
2014-02-10 13:57:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _VHOST_NET_CDEV_H_
|
|
|
|
#define _VHOST_NET_CDEV_H_
|
2014-10-08 18:54:54 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2018-03-28 05:49:24 +00:00
|
|
|
#include <stdbool.h>
|
2014-10-08 18:54:54 +00:00
|
|
|
#include <sys/types.h>
|
vhost: add dequeue zero copy
The basic idea of dequeue zero copy is, instead of copying data from
the desc buf, here we let the mbuf reference the desc buf addr directly.
Doing so, however, has one major issue: we can't update the used ring
at the end of rte_vhost_dequeue_burst. Because we don't do the copy
here, an update of the used ring would let the driver to reclaim the
desc buf. As a result, DPDK might reference a stale memory region.
To update the used ring properly, this patch does several tricks:
- when mbuf references a desc buf, refcnt is added by 1.
This is to pin lock the mbuf, so that a mbuf free from the DPDK
won't actually free it, instead, refcnt is subtracted by 1.
- We chain all those mbuf together (by tailq)
And we check it every time on the rte_vhost_dequeue_burst entrance,
to see if the mbuf is freed (when refcnt equals to 1). If that
happens, it means we are the last user of this mbuf and we are
safe to update the used ring.
- "struct zcopy_mbuf" is introduced, to associate an mbuf with the
right desc idx.
Dequeue zero copy is introduced for performance reason, and some rough
tests show about 50% perfomance boost for packet size 1500B. For small
packets, (e.g. 64B), it actually slows a bit down (well, it could up to
15%). That is expected because this patch introduces some extra works,
and it outweighs the benefit from saving few bytes copy.
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
Tested-by: Qian Xu <qian.q.xu@intel.com>
2016-10-09 07:27:57 +00:00
|
|
|
#include <sys/queue.h>
|
2014-10-08 18:54:54 +00:00
|
|
|
#include <unistd.h>
|
2014-02-10 13:57:48 +00:00
|
|
|
#include <linux/vhost.h>
|
2017-04-01 07:22:51 +00:00
|
|
|
#include <linux/virtio_net.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <linux/if.h>
|
2014-02-10 13:57:48 +00:00
|
|
|
|
2014-10-08 18:54:54 +00:00
|
|
|
#include <rte_log.h>
|
2017-04-01 07:22:51 +00:00
|
|
|
#include <rte_ether.h>
|
2017-10-05 08:36:14 +00:00
|
|
|
#include <rte_rwlock.h>
|
2018-12-18 08:02:00 +00:00
|
|
|
#include <rte_malloc.h>
|
2014-10-08 18:54:54 +00:00
|
|
|
|
2017-04-01 07:22:57 +00:00
|
|
|
#include "rte_vhost.h"
|
2018-04-02 11:46:54 +00:00
|
|
|
#include "rte_vdpa.h"
|
2020-06-26 14:04:41 +00:00
|
|
|
#include "rte_vdpa_dev.h"
|
2015-02-23 17:36:31 +00:00
|
|
|
|
2020-07-07 05:07:08 +00:00
|
|
|
#include "rte_vhost_async.h"
|
|
|
|
|
2016-04-30 05:11:19 +00:00
|
|
|
/* Used to indicate that the device is running on a data core */
|
|
|
|
#define VIRTIO_DEV_RUNNING 1
|
2017-03-12 16:34:00 +00:00
|
|
|
/* Used to indicate that the device is ready to operate */
|
|
|
|
#define VIRTIO_DEV_READY 2
|
2018-01-31 17:46:50 +00:00
|
|
|
/* Used to indicate that the built-in vhost net device backend is enabled */
|
|
|
|
#define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
|
2018-04-02 11:46:55 +00:00
|
|
|
/* Used to indicate that the device has its own data path and configured */
|
|
|
|
#define VIRTIO_DEV_VDPA_CONFIGURED 8
|
2016-04-30 05:11:19 +00:00
|
|
|
|
|
|
|
/* Backend value set by guest. */
|
|
|
|
#define VIRTIO_DEV_STOPPED -1
|
|
|
|
|
|
|
|
#define BUF_VECTOR_MAX 256
|
|
|
|
|
2018-05-17 11:44:47 +00:00
|
|
|
#define VHOST_LOG_CACHE_NR 32
|
|
|
|
|
2020-07-07 05:07:08 +00:00
|
|
|
#define MAX_PKT_BURST 32
|
|
|
|
|
|
|
|
#define VHOST_MAX_ASYNC_IT (MAX_PKT_BURST * 2)
|
|
|
|
#define VHOST_MAX_ASYNC_VEC (BUF_VECTOR_MAX * 2)
|
|
|
|
|
2019-10-24 16:08:27 +00:00
|
|
|
#define PACKED_DESC_ENQUEUE_USED_FLAG(w) \
|
|
|
|
((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \
|
|
|
|
VRING_DESC_F_WRITE)
|
2019-10-24 16:08:28 +00:00
|
|
|
#define PACKED_DESC_DEQUEUE_USED_FLAG(w) \
|
|
|
|
((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED) : 0x0)
|
2019-10-24 16:08:25 +00:00
|
|
|
#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \
|
|
|
|
VRING_DESC_F_INDIRECT)
|
|
|
|
|
2019-10-24 16:08:22 +00:00
|
|
|
#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \
|
|
|
|
sizeof(struct vring_packed_desc))
|
|
|
|
#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
|
|
|
|
|
|
|
|
#ifdef VHOST_GCC_UNROLL_PRAGMA
|
|
|
|
#define vhost_for_each_try_unroll(iter, val, size) _Pragma("GCC unroll 4") \
|
|
|
|
for (iter = val; iter < size; iter++)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef VHOST_CLANG_UNROLL_PRAGMA
|
|
|
|
#define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \
|
|
|
|
for (iter = val; iter < size; iter++)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef VHOST_ICC_UNROLL_PRAGMA
|
|
|
|
#define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \
|
|
|
|
for (iter = val; iter < size; iter++)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef vhost_for_each_try_unroll
|
|
|
|
#define vhost_for_each_try_unroll(iter, val, num) \
|
|
|
|
for (iter = val; iter < num; iter++)
|
|
|
|
#endif
|
|
|
|
|
2016-04-30 05:11:19 +00:00
|
|
|
/**
|
|
|
|
* Structure contains buffer address, length and descriptor index
|
|
|
|
* from vring to do scatter RX.
|
|
|
|
*/
|
|
|
|
struct buf_vector {
|
2018-07-06 07:04:46 +00:00
|
|
|
uint64_t buf_iova;
|
2016-04-30 05:11:19 +00:00
|
|
|
uint64_t buf_addr;
|
|
|
|
uint32_t buf_len;
|
|
|
|
uint32_t desc_idx;
|
|
|
|
};
|
|
|
|
|
vhost: add dequeue zero copy
The basic idea of dequeue zero copy is, instead of copying data from
the desc buf, here we let the mbuf reference the desc buf addr directly.
Doing so, however, has one major issue: we can't update the used ring
at the end of rte_vhost_dequeue_burst. Because we don't do the copy
here, an update of the used ring would let the driver to reclaim the
desc buf. As a result, DPDK might reference a stale memory region.
To update the used ring properly, this patch does several tricks:
- when mbuf references a desc buf, refcnt is added by 1.
This is to pin lock the mbuf, so that a mbuf free from the DPDK
won't actually free it, instead, refcnt is subtracted by 1.
- We chain all those mbuf together (by tailq)
And we check it every time on the rte_vhost_dequeue_burst entrance,
to see if the mbuf is freed (when refcnt equals to 1). If that
happens, it means we are the last user of this mbuf and we are
safe to update the used ring.
- "struct zcopy_mbuf" is introduced, to associate an mbuf with the
right desc idx.
Dequeue zero copy is introduced for performance reason, and some rough
tests show about 50% perfomance boost for packet size 1500B. For small
packets, (e.g. 64B), it actually slows a bit down (well, it could up to
15%). That is expected because this patch introduces some extra works,
and it outweighs the benefit from saving few bytes copy.
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
Tested-by: Qian Xu <qian.q.xu@intel.com>
2016-10-09 07:27:57 +00:00
|
|
|
/*
|
|
|
|
* A structure to hold some fields needed in zero copy code path,
|
|
|
|
* mainly for associating an mbuf with the right desc_idx.
|
|
|
|
*/
|
|
|
|
struct zcopy_mbuf {
|
|
|
|
struct rte_mbuf *mbuf;
|
|
|
|
uint32_t desc_idx;
|
2018-07-06 07:07:20 +00:00
|
|
|
uint16_t desc_count;
|
vhost: add dequeue zero copy
The basic idea of dequeue zero copy is, instead of copying data from
the desc buf, here we let the mbuf reference the desc buf addr directly.
Doing so, however, has one major issue: we can't update the used ring
at the end of rte_vhost_dequeue_burst. Because we don't do the copy
here, an update of the used ring would let the driver to reclaim the
desc buf. As a result, DPDK might reference a stale memory region.
To update the used ring properly, this patch does several tricks:
- when mbuf references a desc buf, refcnt is added by 1.
This is to pin lock the mbuf, so that a mbuf free from the DPDK
won't actually free it, instead, refcnt is subtracted by 1.
- We chain all those mbuf together (by tailq)
And we check it every time on the rte_vhost_dequeue_burst entrance,
to see if the mbuf is freed (when refcnt equals to 1). If that
happens, it means we are the last user of this mbuf and we are
safe to update the used ring.
- "struct zcopy_mbuf" is introduced, to associate an mbuf with the
right desc idx.
Dequeue zero copy is introduced for performance reason, and some rough
tests show about 50% perfomance boost for packet size 1500B. For small
packets, (e.g. 64B), it actually slows a bit down (well, it could up to
15%). That is expected because this patch introduces some extra works,
and it outweighs the benefit from saving few bytes copy.
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
Tested-by: Qian Xu <qian.q.xu@intel.com>
2016-10-09 07:27:57 +00:00
|
|
|
uint16_t in_use;
|
|
|
|
|
|
|
|
TAILQ_ENTRY(zcopy_mbuf) next;
|
|
|
|
};
|
|
|
|
TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
|
|
|
|
|
2017-09-08 12:50:46 +00:00
|
|
|
/*
|
|
|
|
* Structure contains the info for each batched memory copy.
|
|
|
|
*/
|
|
|
|
struct batch_copy_elem {
|
|
|
|
void *dst;
|
|
|
|
void *src;
|
|
|
|
uint32_t len;
|
|
|
|
uint64_t log_addr;
|
|
|
|
};
|
|
|
|
|
2018-05-17 11:44:47 +00:00
|
|
|
/*
|
|
|
|
* Structure that contains the info for batched dirty logging.
|
|
|
|
*/
|
|
|
|
struct log_cache_entry {
|
|
|
|
uint32_t offset;
|
|
|
|
unsigned long val;
|
|
|
|
};
|
|
|
|
|
2018-07-06 07:07:16 +00:00
|
|
|
struct vring_used_elem_packed {
|
|
|
|
uint16_t id;
|
2019-10-24 16:08:28 +00:00
|
|
|
uint16_t flags;
|
2018-07-06 07:07:16 +00:00
|
|
|
uint32_t len;
|
|
|
|
uint32_t count;
|
|
|
|
};
|
|
|
|
|
2016-04-30 05:11:19 +00:00
|
|
|
/**
|
|
|
|
* Structure contains variables relevant to RX/TX virtqueues.
|
|
|
|
*/
|
|
|
|
struct vhost_virtqueue {
|
2018-07-06 07:07:10 +00:00
|
|
|
union {
|
|
|
|
struct vring_desc *desc;
|
|
|
|
struct vring_packed_desc *desc_packed;
|
|
|
|
};
|
2018-07-06 07:07:21 +00:00
|
|
|
union {
|
|
|
|
struct vring_avail *avail;
|
|
|
|
struct vring_packed_desc_event *driver_event;
|
|
|
|
};
|
|
|
|
union {
|
|
|
|
struct vring_used *used;
|
|
|
|
struct vring_packed_desc_event *device_event;
|
|
|
|
};
|
2016-04-30 05:11:19 +00:00
|
|
|
uint32_t size;
|
|
|
|
|
2016-10-09 07:27:56 +00:00
|
|
|
uint16_t last_avail_idx;
|
2016-10-14 09:34:32 +00:00
|
|
|
uint16_t last_used_idx;
|
2018-01-09 11:03:48 +00:00
|
|
|
/* Last used index we notify to front end. */
|
|
|
|
uint16_t signalled_used;
|
2018-07-06 07:07:21 +00:00
|
|
|
bool signalled_used_valid;
|
2016-04-30 05:11:19 +00:00
|
|
|
#define VIRTIO_INVALID_EVENTFD (-1)
|
|
|
|
#define VIRTIO_UNINITIALIZED_EVENTFD (-2)
|
|
|
|
|
|
|
|
/* Backend value to determine if device should started/stopped */
|
|
|
|
int backend;
|
2018-01-17 13:49:25 +00:00
|
|
|
int enabled;
|
|
|
|
int access_ok;
|
2020-06-29 14:08:18 +00:00
|
|
|
int ready;
|
2018-01-17 13:49:25 +00:00
|
|
|
rte_spinlock_t access_lock;
|
|
|
|
|
2016-04-30 05:11:19 +00:00
|
|
|
/* Used to notify the guest (trigger interrupt) */
|
|
|
|
int callfd;
|
|
|
|
/* Currently unused as polling mode is enabled */
|
|
|
|
int kickfd;
|
|
|
|
|
|
|
|
/* Physical address of used ring, for logging */
|
|
|
|
uint64_t log_guest_addr;
|
vhost: add dequeue zero copy
The basic idea of dequeue zero copy is, instead of copying data from
the desc buf, here we let the mbuf reference the desc buf addr directly.
Doing so, however, has one major issue: we can't update the used ring
at the end of rte_vhost_dequeue_burst. Because we don't do the copy
here, an update of the used ring would let the driver to reclaim the
desc buf. As a result, DPDK might reference a stale memory region.
To update the used ring properly, this patch does several tricks:
- when mbuf references a desc buf, refcnt is added by 1.
This is to pin lock the mbuf, so that a mbuf free from the DPDK
won't actually free it, instead, refcnt is subtracted by 1.
- We chain all those mbuf together (by tailq)
And we check it every time on the rte_vhost_dequeue_burst entrance,
to see if the mbuf is freed (when refcnt equals to 1). If that
happens, it means we are the last user of this mbuf and we are
safe to update the used ring.
- "struct zcopy_mbuf" is introduced, to associate an mbuf with the
right desc idx.
Dequeue zero copy is introduced for performance reason, and some rough
tests show about 50% perfomance boost for packet size 1500B. For small
packets, (e.g. 64B), it actually slows a bit down (well, it could up to
15%). That is expected because this patch introduces some extra works,
and it outweighs the benefit from saving few bytes copy.
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
Tested-by: Qian Xu <qian.q.xu@intel.com>
2016-10-09 07:27:57 +00:00
|
|
|
|
2019-10-09 20:48:33 +00:00
|
|
|
/* inflight share memory info */
|
|
|
|
union {
|
|
|
|
struct rte_vhost_inflight_info_split *inflight_split;
|
|
|
|
struct rte_vhost_inflight_info_packed *inflight_packed;
|
|
|
|
};
|
|
|
|
struct rte_vhost_resubmit_info *resubmit_inflight;
|
|
|
|
uint64_t global_counter;
|
|
|
|
|
vhost: add dequeue zero copy
The basic idea of dequeue zero copy is, instead of copying data from
the desc buf, here we let the mbuf reference the desc buf addr directly.
Doing so, however, has one major issue: we can't update the used ring
at the end of rte_vhost_dequeue_burst. Because we don't do the copy
here, an update of the used ring would let the driver to reclaim the
desc buf. As a result, DPDK might reference a stale memory region.
To update the used ring properly, this patch does several tricks:
- when mbuf references a desc buf, refcnt is added by 1.
This is to pin lock the mbuf, so that a mbuf free from the DPDK
won't actually free it, instead, refcnt is subtracted by 1.
- We chain all those mbuf together (by tailq)
And we check it every time on the rte_vhost_dequeue_burst entrance,
to see if the mbuf is freed (when refcnt equals to 1). If that
happens, it means we are the last user of this mbuf and we are
safe to update the used ring.
- "struct zcopy_mbuf" is introduced, to associate an mbuf with the
right desc idx.
Dequeue zero copy is introduced for performance reason, and some rough
tests show about 50% perfomance boost for packet size 1500B. For small
packets, (e.g. 64B), it actually slows a bit down (well, it could up to
15%). That is expected because this patch introduces some extra works,
and it outweighs the benefit from saving few bytes copy.
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
Tested-by: Qian Xu <qian.q.xu@intel.com>
2016-10-09 07:27:57 +00:00
|
|
|
uint16_t nr_zmbuf;
|
|
|
|
uint16_t zmbuf_size;
|
|
|
|
uint16_t last_zmbuf_idx;
|
|
|
|
struct zcopy_mbuf *zmbufs;
|
|
|
|
struct zcopy_mbuf_list zmbuf_list;
|
2016-10-14 09:34:36 +00:00
|
|
|
|
2018-07-06 07:07:16 +00:00
|
|
|
union {
|
|
|
|
struct vring_used_elem *shadow_used_split;
|
|
|
|
struct vring_used_elem_packed *shadow_used_packed;
|
|
|
|
};
|
2016-10-14 09:34:36 +00:00
|
|
|
uint16_t shadow_used_idx;
|
2019-10-24 16:08:26 +00:00
|
|
|
/* Record packed ring enqueue latest desc cache aligned index */
|
|
|
|
uint16_t shadow_aligned_idx;
|
2019-10-24 16:08:28 +00:00
|
|
|
/* Record packed ring first dequeue desc index */
|
|
|
|
uint16_t shadow_last_used_idx;
|
2017-10-05 08:36:23 +00:00
|
|
|
struct vhost_vring_addr ring_addrs;
|
2017-09-08 12:50:46 +00:00
|
|
|
|
|
|
|
struct batch_copy_elem *batch_copy_elems;
|
|
|
|
uint16_t batch_copy_nb_elems;
|
2018-07-06 07:07:10 +00:00
|
|
|
bool used_wrap_counter;
|
|
|
|
bool avail_wrap_counter;
|
2017-10-05 08:36:14 +00:00
|
|
|
|
2018-05-17 11:44:47 +00:00
|
|
|
struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
|
|
|
|
uint16_t log_cache_nb_elem;
|
|
|
|
|
2017-10-05 08:36:14 +00:00
|
|
|
rte_rwlock_t iotlb_lock;
|
2017-10-05 08:36:15 +00:00
|
|
|
rte_rwlock_t iotlb_pending_lock;
|
2017-10-05 08:36:14 +00:00
|
|
|
struct rte_mempool *iotlb_pool;
|
|
|
|
TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
|
|
|
|
int iotlb_cache_nr;
|
2017-10-05 08:36:15 +00:00
|
|
|
TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
|
2020-07-07 05:07:08 +00:00
|
|
|
|
|
|
|
/* operation callbacks for async dma */
|
|
|
|
struct rte_vhost_async_channel_ops async_ops;
|
|
|
|
|
|
|
|
struct rte_vhost_iov_iter it_pool[VHOST_MAX_ASYNC_IT];
|
|
|
|
struct iovec vec_pool[VHOST_MAX_ASYNC_VEC];
|
|
|
|
|
|
|
|
/* async data transfer status */
|
|
|
|
uintptr_t **async_pkts_pending;
|
|
|
|
#define ASYNC_PENDING_INFO_N_MSK 0xFFFF
|
|
|
|
#define ASYNC_PENDING_INFO_N_SFT 16
|
|
|
|
uint64_t *async_pending_info;
|
|
|
|
uint16_t async_pkts_idx;
|
|
|
|
uint16_t async_pkts_inflight_n;
|
|
|
|
|
|
|
|
/* vq async features */
|
|
|
|
bool async_inorder;
|
|
|
|
bool async_registered;
|
|
|
|
uint16_t async_threshold;
|
2016-04-30 05:11:19 +00:00
|
|
|
} __rte_cache_aligned;
|
|
|
|
|
2020-07-06 11:24:49 +00:00
|
|
|
/* Virtio device status as per Virtio specification */
|
|
|
|
#define VIRTIO_DEVICE_STATUS_ACK 0x01
|
|
|
|
#define VIRTIO_DEVICE_STATUS_DRIVER 0x02
|
|
|
|
#define VIRTIO_DEVICE_STATUS_DRIVER_OK 0x04
|
|
|
|
#define VIRTIO_DEVICE_STATUS_FEATURES_OK 0x08
|
|
|
|
#define VIRTIO_DEVICE_STATUS_DEV_NEED_RESET 0x40
|
|
|
|
#define VIRTIO_DEVICE_STATUS_FAILED 0x80
|
|
|
|
|
2017-04-01 07:22:45 +00:00
|
|
|
#define VHOST_MAX_VRING 0x100
|
2017-03-01 10:41:59 +00:00
|
|
|
#define VHOST_MAX_QUEUE_PAIRS 0x80
|
2016-04-30 05:11:19 +00:00
|
|
|
|
2017-10-05 08:36:13 +00:00
|
|
|
/* Declare IOMMU related bits for older kernels */
|
|
|
|
#ifndef VIRTIO_F_IOMMU_PLATFORM
|
|
|
|
|
|
|
|
#define VIRTIO_F_IOMMU_PLATFORM 33
|
|
|
|
|
|
|
|
struct vhost_iotlb_msg {
|
|
|
|
__u64 iova;
|
|
|
|
__u64 size;
|
|
|
|
__u64 uaddr;
|
|
|
|
#define VHOST_ACCESS_RO 0x1
|
|
|
|
#define VHOST_ACCESS_WO 0x2
|
|
|
|
#define VHOST_ACCESS_RW 0x3
|
|
|
|
__u8 perm;
|
|
|
|
#define VHOST_IOTLB_MISS 1
|
|
|
|
#define VHOST_IOTLB_UPDATE 2
|
|
|
|
#define VHOST_IOTLB_INVALIDATE 3
|
|
|
|
#define VHOST_IOTLB_ACCESS_FAIL 4
|
|
|
|
__u8 type;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define VHOST_IOTLB_MSG 0x1
|
|
|
|
|
|
|
|
struct vhost_msg {
|
|
|
|
int type;
|
|
|
|
union {
|
|
|
|
struct vhost_iotlb_msg iotlb;
|
|
|
|
__u8 padding[64];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2016-04-30 05:11:19 +00:00
|
|
|
/*
|
|
|
|
* Define virtio 1.0 for older kernels
|
|
|
|
*/
|
|
|
|
#ifndef VIRTIO_F_VERSION_1
|
|
|
|
#define VIRTIO_F_VERSION_1 32
|
|
|
|
#endif
|
|
|
|
|
2018-07-06 07:07:08 +00:00
|
|
|
/* Declare packed ring related bits for older kernels */
|
|
|
|
#ifndef VIRTIO_F_RING_PACKED
|
|
|
|
|
|
|
|
#define VIRTIO_F_RING_PACKED 34
|
|
|
|
|
|
|
|
struct vring_packed_desc {
|
|
|
|
uint64_t addr;
|
|
|
|
uint32_t len;
|
|
|
|
uint16_t id;
|
|
|
|
uint16_t flags;
|
|
|
|
};
|
2018-07-06 07:07:21 +00:00
|
|
|
|
|
|
|
struct vring_packed_desc_event {
|
|
|
|
uint16_t off_wrap;
|
|
|
|
uint16_t flags;
|
|
|
|
};
|
2018-07-06 07:07:08 +00:00
|
|
|
#endif
|
|
|
|
|
2018-11-22 17:09:22 +00:00
|
|
|
/*
|
|
|
|
* Declare below packed ring defines unconditionally
|
|
|
|
* as Kernel header might use different names.
|
|
|
|
*/
|
|
|
|
#define VRING_DESC_F_AVAIL (1ULL << 7)
|
|
|
|
#define VRING_DESC_F_USED (1ULL << 15)
|
|
|
|
|
|
|
|
#define VRING_EVENT_F_ENABLE 0x0
|
|
|
|
#define VRING_EVENT_F_DISABLE 0x1
|
|
|
|
#define VRING_EVENT_F_DESC 0x2
|
|
|
|
|
2018-07-02 13:56:34 +00:00
|
|
|
/*
|
|
|
|
* Available and used descs are in same order
|
|
|
|
*/
|
|
|
|
#ifndef VIRTIO_F_IN_ORDER
|
|
|
|
#define VIRTIO_F_IN_ORDER 35
|
|
|
|
#endif
|
|
|
|
|
2017-04-01 07:22:41 +00:00
|
|
|
/* Features supported by this builtin vhost-user net driver. */
|
|
|
|
#define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
|
2018-01-19 19:02:50 +00:00
|
|
|
(1ULL << VIRTIO_F_ANY_LAYOUT) | \
|
2017-04-01 07:22:41 +00:00
|
|
|
(1ULL << VIRTIO_NET_F_CTRL_VQ) | \
|
|
|
|
(1ULL << VIRTIO_NET_F_CTRL_RX) | \
|
|
|
|
(1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
|
|
|
|
(1ULL << VIRTIO_NET_F_MQ) | \
|
|
|
|
(1ULL << VIRTIO_F_VERSION_1) | \
|
|
|
|
(1ULL << VHOST_F_LOG_ALL) | \
|
|
|
|
(1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
|
2017-11-28 05:28:33 +00:00
|
|
|
(1ULL << VIRTIO_NET_F_GSO) | \
|
2017-04-01 07:22:41 +00:00
|
|
|
(1ULL << VIRTIO_NET_F_HOST_TSO4) | \
|
|
|
|
(1ULL << VIRTIO_NET_F_HOST_TSO6) | \
|
2017-11-21 06:56:52 +00:00
|
|
|
(1ULL << VIRTIO_NET_F_HOST_UFO) | \
|
2017-11-22 03:19:42 +00:00
|
|
|
(1ULL << VIRTIO_NET_F_HOST_ECN) | \
|
2017-04-01 07:22:41 +00:00
|
|
|
(1ULL << VIRTIO_NET_F_CSUM) | \
|
|
|
|
(1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
|
|
|
|
(1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
|
|
|
|
(1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
|
2017-11-21 06:56:52 +00:00
|
|
|
(1ULL << VIRTIO_NET_F_GUEST_UFO) | \
|
2017-11-22 03:19:42 +00:00
|
|
|
(1ULL << VIRTIO_NET_F_GUEST_ECN) | \
|
2017-04-01 07:22:41 +00:00
|
|
|
(1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
|
2018-01-09 11:03:48 +00:00
|
|
|
(1ULL << VIRTIO_RING_F_EVENT_IDX) | \
|
2018-07-02 13:56:34 +00:00
|
|
|
(1ULL << VIRTIO_NET_F_MTU) | \
|
|
|
|
(1ULL << VIRTIO_F_IN_ORDER) | \
|
2018-10-31 10:26:40 +00:00
|
|
|
(1ULL << VIRTIO_F_IOMMU_PLATFORM) | \
|
|
|
|
(1ULL << VIRTIO_F_RING_PACKED))
|
2017-04-01 07:22:41 +00:00
|
|
|
|
|
|
|
|
2016-10-09 07:27:55 +00:00
|
|
|
struct guest_page {
|
|
|
|
uint64_t guest_phys_addr;
|
|
|
|
uint64_t host_phys_addr;
|
|
|
|
uint64_t size;
|
|
|
|
};
|
|
|
|
|
2019-10-09 20:48:32 +00:00
|
|
|
struct inflight_mem_info {
|
|
|
|
int fd;
|
|
|
|
void *addr;
|
|
|
|
uint64_t size;
|
|
|
|
};
|
|
|
|
|
2016-04-30 05:11:19 +00:00
|
|
|
/**
|
|
|
|
* Device structure contains all configuration information relating
|
|
|
|
* to the device.
|
|
|
|
*/
|
|
|
|
struct virtio_net {
|
|
|
|
/* Frontend (QEMU) memory and memory region information */
|
2017-04-01 07:22:43 +00:00
|
|
|
struct rte_vhost_memory *mem;
|
2016-04-30 05:11:19 +00:00
|
|
|
uint64_t features;
|
|
|
|
uint64_t protocol_features;
|
|
|
|
int vid;
|
|
|
|
uint32_t flags;
|
2016-05-01 23:58:52 +00:00
|
|
|
uint16_t vhost_hlen;
|
2016-05-03 00:46:18 +00:00
|
|
|
/* to tell if we need broadcast rarp packet */
|
2020-04-23 16:54:49 +00:00
|
|
|
int16_t broadcast_rarp;
|
2017-04-01 07:22:47 +00:00
|
|
|
uint32_t nr_vring;
|
vhost: add dequeue zero copy
The basic idea of dequeue zero copy is, instead of copying data from
the desc buf, here we let the mbuf reference the desc buf addr directly.
Doing so, however, has one major issue: we can't update the used ring
at the end of rte_vhost_dequeue_burst. Because we don't do the copy
here, an update of the used ring would let the driver to reclaim the
desc buf. As a result, DPDK might reference a stale memory region.
To update the used ring properly, this patch does several tricks:
- when mbuf references a desc buf, refcnt is added by 1.
This is to pin lock the mbuf, so that a mbuf free from the DPDK
won't actually free it, instead, refcnt is subtracted by 1.
- We chain all those mbuf together (by tailq)
And we check it every time on the rte_vhost_dequeue_burst entrance,
to see if the mbuf is freed (when refcnt equals to 1). If that
happens, it means we are the last user of this mbuf and we are
safe to update the used ring.
- "struct zcopy_mbuf" is introduced, to associate an mbuf with the
right desc idx.
Dequeue zero copy is introduced for performance reason, and some rough
tests show about 50% perfomance boost for packet size 1500B. For small
packets, (e.g. 64B), it actually slows a bit down (well, it could up to
15%). That is expected because this patch introduces some extra works,
and it outweighs the benefit from saving few bytes copy.
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
Tested-by: Qian Xu <qian.q.xu@intel.com>
2016-10-09 07:27:57 +00:00
|
|
|
int dequeue_zero_copy;
|
2020-07-07 05:07:08 +00:00
|
|
|
int async_copy;
|
2019-10-15 18:59:51 +00:00
|
|
|
int extbuf;
|
|
|
|
int linearbuf;
|
2016-05-03 00:46:18 +00:00
|
|
|
struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
|
2019-10-09 20:48:32 +00:00
|
|
|
struct inflight_mem_info *inflight_info;
|
2016-04-30 05:11:19 +00:00
|
|
|
#define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
|
|
|
|
char ifname[IF_NAME_SZ];
|
|
|
|
uint64_t log_size;
|
|
|
|
uint64_t log_base;
|
2016-06-16 09:16:37 +00:00
|
|
|
uint64_t log_addr;
|
2019-05-21 16:13:03 +00:00
|
|
|
struct rte_ether_addr mac;
|
2017-03-12 16:33:59 +00:00
|
|
|
uint16_t mtu;
|
2020-07-06 11:24:49 +00:00
|
|
|
uint8_t status;
|
2016-04-30 05:11:19 +00:00
|
|
|
|
2017-04-01 07:22:52 +00:00
|
|
|
struct vhost_device_ops const *notify_ops;
|
2017-04-01 07:22:42 +00:00
|
|
|
|
2016-10-09 07:27:55 +00:00
|
|
|
uint32_t nr_guest_pages;
|
|
|
|
uint32_t max_guest_pages;
|
|
|
|
struct guest_page *guest_pages;
|
2017-10-05 08:36:12 +00:00
|
|
|
|
|
|
|
int slave_req_fd;
|
2018-06-08 03:22:23 +00:00
|
|
|
rte_spinlock_t slave_req_lock;
|
2018-04-02 11:46:54 +00:00
|
|
|
|
2018-10-12 12:40:39 +00:00
|
|
|
int postcopy_ufd;
|
2018-10-12 12:40:40 +00:00
|
|
|
int postcopy_listening;
|
2018-10-12 12:40:39 +00:00
|
|
|
|
2020-06-26 14:04:33 +00:00
|
|
|
struct rte_vdpa_device *vdpa_dev;
|
2016-04-30 05:11:19 +00:00
|
|
|
|
2019-01-17 15:32:26 +00:00
|
|
|
/* context data for the external message handlers */
|
2018-04-05 16:01:30 +00:00
|
|
|
void *extern_data;
|
|
|
|
/* pre and post vhost user message handlers for the device */
|
2019-01-17 15:32:26 +00:00
|
|
|
struct rte_vhost_user_extern_ops extern_ops;
|
2018-04-05 16:01:30 +00:00
|
|
|
} __rte_cache_aligned;
|
2017-04-01 07:22:55 +00:00
|
|
|
|
2018-07-06 07:07:09 +00:00
|
|
|
static __rte_always_inline bool
|
|
|
|
vq_is_packed(struct virtio_net *dev)
|
|
|
|
{
|
|
|
|
return dev->features & (1ull << VIRTIO_F_RING_PACKED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
|
|
|
|
{
|
2019-09-17 05:28:25 +00:00
|
|
|
uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
|
2018-12-05 15:09:26 +00:00
|
|
|
|
|
|
|
return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
|
|
|
|
wrap_counter != !!(flags & VRING_DESC_F_USED);
|
2018-07-06 07:07:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 16:08:20 +00:00
|
|
|
static inline void
|
|
|
|
vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num)
|
|
|
|
{
|
|
|
|
vq->last_used_idx += num;
|
|
|
|
if (vq->last_used_idx >= vq->size) {
|
|
|
|
vq->used_wrap_counter ^= 1;
|
|
|
|
vq->last_used_idx -= vq->size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num)
|
|
|
|
{
|
|
|
|
vq->last_avail_idx += num;
|
|
|
|
if (vq->last_avail_idx >= vq->size) {
|
|
|
|
vq->avail_wrap_counter ^= 1;
|
|
|
|
vq->last_avail_idx -= vq->size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 13:04:16 +00:00
|
|
|
void __vhost_log_cache_write(struct virtio_net *dev,
|
|
|
|
struct vhost_virtqueue *vq,
|
|
|
|
uint64_t addr, uint64_t len);
|
2019-10-09 11:54:31 +00:00
|
|
|
void __vhost_log_cache_write_iova(struct virtio_net *dev,
|
|
|
|
struct vhost_virtqueue *vq,
|
|
|
|
uint64_t iova, uint64_t len);
|
2019-05-29 13:04:16 +00:00
|
|
|
void __vhost_log_cache_sync(struct virtio_net *dev,
|
|
|
|
struct vhost_virtqueue *vq);
|
|
|
|
void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
|
2019-10-09 11:54:31 +00:00
|
|
|
void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
|
|
|
|
uint64_t iova, uint64_t len);
|
2017-04-01 07:22:55 +00:00
|
|
|
|
2017-05-13 09:27:25 +00:00
|
|
|
static __rte_always_inline void
|
2017-04-01 07:22:55 +00:00
|
|
|
vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
|
|
|
|
{
|
2019-05-29 13:04:16 +00:00
|
|
|
if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
|
|
|
|
__vhost_log_write(dev, addr, len);
|
2017-04-01 07:22:55 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 11:44:47 +00:00
|
|
|
static __rte_always_inline void
|
|
|
|
vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
|
|
|
|
{
|
2019-05-29 13:04:16 +00:00
|
|
|
if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
|
|
|
|
__vhost_log_cache_sync(dev, vq);
|
2018-05-17 11:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __rte_always_inline void
|
|
|
|
vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
|
|
|
|
uint64_t addr, uint64_t len)
|
|
|
|
{
|
2019-05-29 13:04:16 +00:00
|
|
|
if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
|
|
|
|
__vhost_log_cache_write(dev, vq, addr, len);
|
2018-05-17 11:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __rte_always_inline void
|
|
|
|
vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
|
|
|
|
uint64_t offset, uint64_t len)
|
|
|
|
{
|
2020-02-13 10:04:58 +00:00
|
|
|
if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
|
|
|
|
if (unlikely(vq->log_guest_addr == 0))
|
|
|
|
return;
|
|
|
|
__vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset,
|
|
|
|
len);
|
|
|
|
}
|
2018-05-17 11:44:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 09:27:25 +00:00
|
|
|
static __rte_always_inline void
|
2017-04-01 07:22:55 +00:00
|
|
|
vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
|
|
|
|
uint64_t offset, uint64_t len)
|
|
|
|
{
|
2020-02-13 10:04:58 +00:00
|
|
|
if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
|
|
|
|
if (unlikely(vq->log_guest_addr == 0))
|
|
|
|
return;
|
|
|
|
__vhost_log_write(dev, vq->log_guest_addr + offset, len);
|
|
|
|
}
|
2017-04-01 07:22:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 11:54:31 +00:00
|
|
|
static __rte_always_inline void
|
|
|
|
vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
|
|
|
|
uint64_t iova, uint64_t len)
|
|
|
|
{
|
|
|
|
if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
|
|
|
|
__vhost_log_cache_write_iova(dev, vq, iova, len);
|
|
|
|
else
|
|
|
|
__vhost_log_cache_write(dev, vq, iova, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __rte_always_inline void
|
|
|
|
vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
|
|
|
|
uint64_t iova, uint64_t len)
|
|
|
|
{
|
|
|
|
if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
|
|
|
|
__vhost_log_write_iova(dev, vq, iova, len);
|
|
|
|
else
|
|
|
|
__vhost_log_write(dev, iova, len);
|
|
|
|
}
|
|
|
|
|
2019-12-04 15:07:29 +00:00
|
|
|
extern int vhost_config_log_level;
|
|
|
|
extern int vhost_data_log_level;
|
|
|
|
|
|
|
|
#define VHOST_LOG_CONFIG(level, fmt, args...) \
|
|
|
|
rte_log(RTE_LOG_ ## level, vhost_config_log_level, \
|
|
|
|
"VHOST_CONFIG: " fmt, ##args)
|
|
|
|
|
|
|
|
#define VHOST_LOG_DATA(level, fmt, args...) \
|
|
|
|
(void)((RTE_LOG_ ## level <= RTE_LOG_DP_LEVEL) ? \
|
|
|
|
rte_log(RTE_LOG_ ## level, vhost_data_log_level, \
|
|
|
|
"VHOST_DATA : " fmt, ##args) : \
|
|
|
|
0)
|
2014-10-08 18:54:52 +00:00
|
|
|
|
|
|
|
#ifdef RTE_LIBRTE_VHOST_DEBUG
|
|
|
|
#define VHOST_MAX_PRINT_BUFF 6072
|
|
|
|
#define PRINT_PACKET(device, addr, size, header) do { \
|
|
|
|
char *pkt_addr = (char *)(addr); \
|
|
|
|
unsigned int index; \
|
|
|
|
char packet[VHOST_MAX_PRINT_BUFF]; \
|
|
|
|
\
|
|
|
|
if ((header)) \
|
2016-05-23 08:36:33 +00:00
|
|
|
snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
|
2014-10-08 18:54:52 +00:00
|
|
|
else \
|
2016-05-23 08:36:33 +00:00
|
|
|
snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
|
2014-10-08 18:54:52 +00:00
|
|
|
for (index = 0; index < (size); index++) { \
|
|
|
|
snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
|
|
|
|
"%02hhx ", pkt_addr[index]); \
|
|
|
|
} \
|
|
|
|
snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
|
|
|
|
\
|
2019-12-04 15:07:29 +00:00
|
|
|
VHOST_LOG_DATA(DEBUG, "%s", packet); \
|
2014-10-08 18:54:52 +00:00
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
#define PRINT_PACKET(device, addr, size, header) do {} while (0)
|
|
|
|
#endif
|
|
|
|
|
vhost: refactor code structure
The code structure is a bit messy now. For example, vhost-user message
handling is spread to three different files:
vhost-net-user.c virtio-net.c virtio-net-user.c
Where, vhost-net-user.c is the entrance to handle all those messages
and then invoke the right method for a specific message. Some of them
are stored at virtio-net.c, while others are stored at virtio-net-user.c.
The truth is all of them should be in one file, vhost_user.c.
So this patch refactors the source code structure: mainly on renaming
files and moving code from one file to another file that is more suitable
for storing it. Thus, no functional changes are made.
After the refactor, the code structure becomes to:
- socket.c handles all vhost-user socket file related stuff, such
as, socket file creation for server mode, reconnection
for client mode.
- vhost.c mainly on stuff like vhost device creation/destroy/reset.
Most of the vhost API implementation are there, too.
- vhost_user.c all stuff about vhost-user messages handling goes there.
- virtio_net.c all stuff about virtio-net should go there. It has virtio
net Rx/Tx implementation only so far: it's just a rename
from vhost_rxtx.c
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
2016-08-18 08:48:39 +00:00
|
|
|
#define MAX_VHOST_DEVICE 1024
|
|
|
|
extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
|
|
|
|
|
2020-04-29 01:04:22 +00:00
|
|
|
#define VHOST_BINARY_SEARCH_THRESH 256
|
|
|
|
|
|
|
|
static __rte_always_inline int guest_page_addrcmp(const void *p1,
|
|
|
|
const void *p2)
|
|
|
|
{
|
|
|
|
const struct guest_page *page1 = (const struct guest_page *)p1;
|
|
|
|
const struct guest_page *page2 = (const struct guest_page *)p2;
|
|
|
|
|
|
|
|
if (page1->guest_phys_addr > page2->guest_phys_addr)
|
|
|
|
return 1;
|
|
|
|
if (page1->guest_phys_addr < page2->guest_phys_addr)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-09 07:27:55 +00:00
|
|
|
/* Convert guest physical address to host physical address */
|
2017-10-20 12:31:31 +00:00
|
|
|
static __rte_always_inline rte_iova_t
|
2016-10-09 07:27:55 +00:00
|
|
|
gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
struct guest_page *page;
|
2020-04-29 01:04:22 +00:00
|
|
|
struct guest_page key;
|
|
|
|
|
|
|
|
if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
|
|
|
|
key.guest_phys_addr = gpa;
|
|
|
|
page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages,
|
|
|
|
sizeof(struct guest_page), guest_page_addrcmp);
|
|
|
|
if (page) {
|
|
|
|
if (gpa + size < page->guest_phys_addr + page->size)
|
|
|
|
return gpa - page->guest_phys_addr +
|
|
|
|
page->host_phys_addr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < dev->nr_guest_pages; i++) {
|
|
|
|
page = &dev->guest_pages[i];
|
|
|
|
|
|
|
|
if (gpa >= page->guest_phys_addr &&
|
|
|
|
gpa + size < page->guest_phys_addr +
|
|
|
|
page->size)
|
|
|
|
return gpa - page->guest_phys_addr +
|
|
|
|
page->host_phys_addr;
|
2016-10-09 07:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-09 11:54:30 +00:00
|
|
|
static __rte_always_inline uint64_t
|
|
|
|
hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
|
|
|
|
{
|
|
|
|
struct rte_vhost_mem_region *r;
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
if (unlikely(!dev || !dev->mem))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < dev->mem->nregions; i++) {
|
|
|
|
r = &dev->mem->regions[i];
|
|
|
|
|
|
|
|
if (vva >= r->host_user_addr &&
|
|
|
|
vva + len < r->host_user_addr + r->size) {
|
|
|
|
return r->guest_phys_addr + vva - r->host_user_addr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-14 04:01:00 +00:00
|
|
|
static __rte_always_inline struct virtio_net *
|
|
|
|
get_device(int vid)
|
|
|
|
{
|
|
|
|
struct virtio_net *dev = vhost_devices[vid];
|
|
|
|
|
|
|
|
if (unlikely(!dev)) {
|
2019-12-04 15:07:29 +00:00
|
|
|
VHOST_LOG_CONFIG(ERR,
|
2018-02-14 04:01:00 +00:00
|
|
|
"(%d) device not found.\n", vid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
2016-04-30 05:25:42 +00:00
|
|
|
|
2016-04-29 23:24:27 +00:00
|
|
|
int vhost_new_device(void);
|
vhost: refactor code structure
The code structure is a bit messy now. For example, vhost-user message
handling is spread to three different files:
vhost-net-user.c virtio-net.c virtio-net-user.c
Where, vhost-net-user.c is the entrance to handle all those messages
and then invoke the right method for a specific message. Some of them
are stored at virtio-net.c, while others are stored at virtio-net-user.c.
The truth is all of them should be in one file, vhost_user.c.
So this patch refactors the source code structure: mainly on renaming
files and moving code from one file to another file that is more suitable
for storing it. Thus, no functional changes are made.
After the refactor, the code structure becomes to:
- socket.c handles all vhost-user socket file related stuff, such
as, socket file creation for server mode, reconnection
for client mode.
- vhost.c mainly on stuff like vhost device creation/destroy/reset.
Most of the vhost API implementation are there, too.
- vhost_user.c all stuff about vhost-user messages handling goes there.
- virtio_net.c all stuff about virtio-net should go there. It has virtio
net Rx/Tx implementation only so far: it's just a rename
from vhost_rxtx.c
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
2016-08-18 08:48:39 +00:00
|
|
|
void cleanup_device(struct virtio_net *dev, int destroy);
|
|
|
|
void reset_device(struct virtio_net *dev);
|
2016-04-29 23:24:27 +00:00
|
|
|
void vhost_destroy_device(int);
|
2018-06-13 11:54:18 +00:00
|
|
|
void vhost_destroy_device_notify(struct virtio_net *dev);
|
2014-02-10 13:57:48 +00:00
|
|
|
|
2017-12-13 08:51:08 +00:00
|
|
|
void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
|
2019-10-09 20:48:33 +00:00
|
|
|
void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
|
2018-07-06 07:07:16 +00:00
|
|
|
void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
|
2017-12-13 08:51:08 +00:00
|
|
|
|
2017-04-01 07:22:47 +00:00
|
|
|
int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
|
2014-02-10 13:57:48 +00:00
|
|
|
|
2020-06-26 14:04:33 +00:00
|
|
|
void vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *dev);
|
2018-04-02 11:46:54 +00:00
|
|
|
|
vhost: refactor code structure
The code structure is a bit messy now. For example, vhost-user message
handling is spread to three different files:
vhost-net-user.c virtio-net.c virtio-net-user.c
Where, vhost-net-user.c is the entrance to handle all those messages
and then invoke the right method for a specific message. Some of them
are stored at virtio-net.c, while others are stored at virtio-net-user.c.
The truth is all of them should be in one file, vhost_user.c.
So this patch refactors the source code structure: mainly on renaming
files and moving code from one file to another file that is more suitable
for storing it. Thus, no functional changes are made.
After the refactor, the code structure becomes to:
- socket.c handles all vhost-user socket file related stuff, such
as, socket file creation for server mode, reconnection
for client mode.
- vhost.c mainly on stuff like vhost device creation/destroy/reset.
Most of the vhost API implementation are there, too.
- vhost_user.c all stuff about vhost-user messages handling goes there.
- virtio_net.c all stuff about virtio-net should go there. It has virtio
net Rx/Tx implementation only so far: it's just a rename
from vhost_rxtx.c
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
2016-08-18 08:48:39 +00:00
|
|
|
void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
|
2016-10-09 07:27:58 +00:00
|
|
|
void vhost_enable_dequeue_zero_copy(int vid);
|
2018-01-31 17:46:50 +00:00
|
|
|
void vhost_set_builtin_virtio_net(int vid, bool enable);
|
2019-10-15 18:59:51 +00:00
|
|
|
void vhost_enable_extbuf(int vid);
|
|
|
|
void vhost_enable_linearbuf(int vid);
|
2016-02-10 18:40:55 +00:00
|
|
|
|
2017-04-01 07:22:52 +00:00
|
|
|
struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
|
2017-04-01 07:22:42 +00:00
|
|
|
|
2016-02-10 18:40:55 +00:00
|
|
|
/*
|
2016-11-02 03:15:01 +00:00
|
|
|
* Backend-specific cleanup.
|
|
|
|
*
|
|
|
|
* TODO: fix it; we have one backend now
|
2016-02-10 18:40:55 +00:00
|
|
|
*/
|
|
|
|
void vhost_backend_cleanup(struct virtio_net *dev);
|
|
|
|
|
2017-10-05 08:36:19 +00:00
|
|
|
uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
|
2018-01-23 13:26:02 +00:00
|
|
|
uint64_t iova, uint64_t *len, uint8_t perm);
|
2019-05-29 13:04:18 +00:00
|
|
|
void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
|
|
|
|
struct vhost_virtqueue *vq,
|
|
|
|
uint64_t desc_addr, uint64_t desc_len);
|
2017-10-05 08:36:25 +00:00
|
|
|
int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
|
2020-02-13 10:04:58 +00:00
|
|
|
uint64_t translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
|
|
|
|
uint64_t log_addr);
|
2017-10-05 08:36:26 +00:00
|
|
|
void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
|
2017-10-05 08:36:19 +00:00
|
|
|
|
|
|
|
static __rte_always_inline uint64_t
|
|
|
|
vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
|
2018-01-23 13:26:02 +00:00
|
|
|
uint64_t iova, uint64_t *len, uint8_t perm)
|
2017-10-05 08:36:19 +00:00
|
|
|
{
|
|
|
|
if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
|
2018-01-23 13:37:50 +00:00
|
|
|
return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
|
2017-10-05 08:36:19 +00:00
|
|
|
|
2018-01-23 13:26:02 +00:00
|
|
|
return __vhost_iova_to_vva(dev, vq, iova, len, perm);
|
2017-10-05 08:36:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-04 23:55:55 +00:00
|
|
|
#define vhost_avail_event(vr) \
|
|
|
|
(*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
|
2018-01-09 11:03:48 +00:00
|
|
|
#define vhost_used_event(vr) \
|
|
|
|
(*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following is used with VIRTIO_RING_F_EVENT_IDX.
|
|
|
|
* Assuming a given event_idx value from the other size, if we have
|
|
|
|
* just incremented index from old to new_idx, should we trigger an
|
|
|
|
* event?
|
|
|
|
*/
|
|
|
|
static __rte_always_inline int
|
|
|
|
vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
|
|
|
|
{
|
|
|
|
return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
|
|
|
|
}
|
|
|
|
|
2018-01-02 09:31:35 +00:00
|
|
|
static __rte_always_inline void
|
2018-07-06 07:07:21 +00:00
|
|
|
vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
|
2018-01-02 09:31:35 +00:00
|
|
|
{
|
|
|
|
/* Flush used->idx update before we read avail->flags. */
|
2018-06-07 14:38:26 +00:00
|
|
|
rte_smp_mb();
|
2018-01-02 09:31:35 +00:00
|
|
|
|
2018-01-09 11:03:48 +00:00
|
|
|
/* Don't kick guest if we don't reach index specified by guest. */
|
|
|
|
if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
|
|
|
|
uint16_t old = vq->signalled_used;
|
2020-07-07 05:07:08 +00:00
|
|
|
uint16_t new = vq->async_pkts_inflight_n ?
|
|
|
|
vq->used->idx:vq->last_used_idx;
|
2019-03-17 06:38:32 +00:00
|
|
|
bool signalled_used_valid = vq->signalled_used_valid;
|
|
|
|
|
|
|
|
vq->signalled_used = new;
|
|
|
|
vq->signalled_used_valid = true;
|
2018-01-09 11:03:48 +00:00
|
|
|
|
2019-12-04 15:07:29 +00:00
|
|
|
VHOST_LOG_DATA(DEBUG, "%s: used_event_idx=%d, old=%d, new=%d\n",
|
2018-01-09 11:03:48 +00:00
|
|
|
__func__,
|
|
|
|
vhost_used_event(vq),
|
|
|
|
old, new);
|
2019-03-17 06:38:32 +00:00
|
|
|
|
|
|
|
if ((vhost_need_event(vhost_used_event(vq), new, old) &&
|
|
|
|
(vq->callfd >= 0)) ||
|
2019-08-28 14:49:39 +00:00
|
|
|
unlikely(!signalled_used_valid)) {
|
2018-01-09 11:03:48 +00:00
|
|
|
eventfd_write(vq->callfd, (eventfd_t) 1);
|
2019-08-28 14:49:39 +00:00
|
|
|
if (dev->notify_ops->guest_notified)
|
|
|
|
dev->notify_ops->guest_notified(dev->vid);
|
|
|
|
}
|
2018-01-09 11:03:48 +00:00
|
|
|
} else {
|
|
|
|
/* Kick the guest if necessary. */
|
|
|
|
if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
|
2019-08-28 14:49:39 +00:00
|
|
|
&& (vq->callfd >= 0)) {
|
2018-01-09 11:03:48 +00:00
|
|
|
eventfd_write(vq->callfd, (eventfd_t)1);
|
2019-08-28 14:49:39 +00:00
|
|
|
if (dev->notify_ops->guest_notified)
|
|
|
|
dev->notify_ops->guest_notified(dev->vid);
|
|
|
|
}
|
2018-01-09 11:03:48 +00:00
|
|
|
}
|
2018-01-02 09:31:35 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 07:07:21 +00:00
|
|
|
static __rte_always_inline void
|
|
|
|
vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
|
|
|
|
{
|
|
|
|
uint16_t old, new, off, off_wrap;
|
|
|
|
bool signalled_used_valid, kick = false;
|
|
|
|
|
|
|
|
/* Flush used desc update. */
|
|
|
|
rte_smp_mb();
|
|
|
|
|
|
|
|
if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
|
|
|
|
if (vq->driver_event->flags !=
|
|
|
|
VRING_EVENT_F_DISABLE)
|
|
|
|
kick = true;
|
|
|
|
goto kick;
|
|
|
|
}
|
|
|
|
|
|
|
|
old = vq->signalled_used;
|
|
|
|
new = vq->last_used_idx;
|
|
|
|
vq->signalled_used = new;
|
|
|
|
signalled_used_valid = vq->signalled_used_valid;
|
|
|
|
vq->signalled_used_valid = true;
|
|
|
|
|
|
|
|
if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
|
|
|
|
if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
|
|
|
|
kick = true;
|
|
|
|
goto kick;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(!signalled_used_valid)) {
|
|
|
|
kick = true;
|
|
|
|
goto kick;
|
|
|
|
}
|
|
|
|
|
|
|
|
rte_smp_rmb();
|
|
|
|
|
|
|
|
off_wrap = vq->driver_event->off_wrap;
|
|
|
|
off = off_wrap & ~(1 << 15);
|
|
|
|
|
|
|
|
if (new <= old)
|
|
|
|
old -= vq->size;
|
|
|
|
|
|
|
|
if (vq->used_wrap_counter != off_wrap >> 15)
|
|
|
|
off -= vq->size;
|
|
|
|
|
|
|
|
if (vhost_need_event(off, new, old))
|
|
|
|
kick = true;
|
|
|
|
kick:
|
2019-08-28 14:49:39 +00:00
|
|
|
if (kick) {
|
2018-07-06 07:07:21 +00:00
|
|
|
eventfd_write(vq->callfd, (eventfd_t)1);
|
2019-08-28 14:49:39 +00:00
|
|
|
if (dev->notify_ops->guest_notified)
|
|
|
|
dev->notify_ops->guest_notified(dev->vid);
|
|
|
|
}
|
2018-07-06 07:07:21 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 08:02:00 +00:00
|
|
|
static __rte_always_inline void
|
|
|
|
free_ind_table(void *idesc)
|
|
|
|
{
|
|
|
|
rte_free(idesc);
|
|
|
|
}
|
|
|
|
|
2019-02-22 02:42:06 +00:00
|
|
|
static __rte_always_inline void
|
|
|
|
restore_mbuf(struct rte_mbuf *m)
|
|
|
|
{
|
|
|
|
uint32_t mbuf_size, priv_size;
|
|
|
|
|
|
|
|
while (m) {
|
|
|
|
priv_size = rte_pktmbuf_priv_size(m->pool);
|
|
|
|
mbuf_size = sizeof(struct rte_mbuf) + priv_size;
|
|
|
|
/* start of buffer is after mbuf structure and priv data */
|
|
|
|
|
|
|
|
m->buf_addr = (char *)m + mbuf_size;
|
|
|
|
m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
|
|
|
|
m = m->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 02:42:07 +00:00
|
|
|
static __rte_always_inline bool
|
|
|
|
mbuf_is_consumed(struct rte_mbuf *m)
|
|
|
|
{
|
|
|
|
while (m) {
|
|
|
|
if (rte_mbuf_refcnt_read(m) > 1)
|
|
|
|
return false;
|
|
|
|
m = m->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-22 02:42:08 +00:00
|
|
|
static __rte_always_inline void
|
|
|
|
put_zmbuf(struct zcopy_mbuf *zmbuf)
|
|
|
|
{
|
|
|
|
zmbuf->in_use = 0;
|
|
|
|
}
|
|
|
|
|
2014-02-10 13:57:48 +00:00
|
|
|
#endif /* _VHOST_NET_CDEV_H_ */
|