2020-07-01 16:55:38 +02:00
|
|
|
/*
|
|
|
|
* vhost-vdpa.c
|
|
|
|
*
|
|
|
|
* Copyright(c) 2017-2018 Intel Corporation.
|
|
|
|
* Copyright(c) 2020 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "clients.h"
|
|
|
|
#include "net/vhost_net.h"
|
|
|
|
#include "net/vhost-vdpa.h"
|
|
|
|
#include "hw/virtio/vhost-vdpa.h"
|
|
|
|
#include "qemu/config-file.h"
|
|
|
|
#include "qemu/error-report.h"
|
|
|
|
#include "qemu/option.h"
|
|
|
|
#include "qapi/error.h"
|
2021-10-20 06:56:00 +02:00
|
|
|
#include <linux/vhost.h>
|
2020-07-01 16:55:38 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include "standard-headers/linux/virtio_net.h"
|
|
|
|
#include "monitor/monitor.h"
|
|
|
|
#include "hw/virtio/vhost.h"
|
|
|
|
|
|
|
|
/* Todo:need to add the multiqueue support here */
|
|
|
|
typedef struct VhostVDPAState {
|
|
|
|
NetClientState nc;
|
|
|
|
struct vhost_vdpa vhost_vdpa;
|
|
|
|
VHostNetState *vhost_net;
|
|
|
|
bool started;
|
|
|
|
} VhostVDPAState;
|
|
|
|
|
|
|
|
const int vdpa_feature_bits[] = {
|
|
|
|
VIRTIO_F_NOTIFY_ON_EMPTY,
|
|
|
|
VIRTIO_RING_F_INDIRECT_DESC,
|
|
|
|
VIRTIO_RING_F_EVENT_IDX,
|
|
|
|
VIRTIO_F_ANY_LAYOUT,
|
|
|
|
VIRTIO_F_VERSION_1,
|
|
|
|
VIRTIO_NET_F_CSUM,
|
|
|
|
VIRTIO_NET_F_GUEST_CSUM,
|
|
|
|
VIRTIO_NET_F_GSO,
|
|
|
|
VIRTIO_NET_F_GUEST_TSO4,
|
|
|
|
VIRTIO_NET_F_GUEST_TSO6,
|
|
|
|
VIRTIO_NET_F_GUEST_ECN,
|
|
|
|
VIRTIO_NET_F_GUEST_UFO,
|
|
|
|
VIRTIO_NET_F_HOST_TSO4,
|
|
|
|
VIRTIO_NET_F_HOST_TSO6,
|
|
|
|
VIRTIO_NET_F_HOST_ECN,
|
|
|
|
VIRTIO_NET_F_HOST_UFO,
|
|
|
|
VIRTIO_NET_F_MRG_RXBUF,
|
|
|
|
VIRTIO_NET_F_MTU,
|
2021-10-20 06:56:00 +02:00
|
|
|
VIRTIO_NET_F_CTRL_RX,
|
|
|
|
VIRTIO_NET_F_CTRL_RX_EXTRA,
|
|
|
|
VIRTIO_NET_F_CTRL_VLAN,
|
|
|
|
VIRTIO_NET_F_GUEST_ANNOUNCE,
|
|
|
|
VIRTIO_NET_F_CTRL_MAC_ADDR,
|
|
|
|
VIRTIO_NET_F_RSS,
|
|
|
|
VIRTIO_NET_F_MQ,
|
|
|
|
VIRTIO_NET_F_CTRL_VQ,
|
2020-07-01 16:55:38 +02:00
|
|
|
VIRTIO_F_IOMMU_PLATFORM,
|
|
|
|
VIRTIO_F_RING_PACKED,
|
2021-05-14 13:48:33 +02:00
|
|
|
VIRTIO_NET_F_RSS,
|
|
|
|
VIRTIO_NET_F_HASH_REPORT,
|
2020-07-01 16:55:38 +02:00
|
|
|
VIRTIO_NET_F_GUEST_ANNOUNCE,
|
2020-10-01 22:09:45 +02:00
|
|
|
VIRTIO_NET_F_STATUS,
|
2020-07-01 16:55:38 +02:00
|
|
|
VHOST_INVALID_FEATURE_BIT
|
|
|
|
};
|
|
|
|
|
|
|
|
VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc)
|
|
|
|
{
|
|
|
|
VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
|
|
|
|
assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
|
|
|
|
return s->vhost_net;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vhost_vdpa_net_check_device_id(struct vhost_net *net)
|
|
|
|
{
|
|
|
|
uint32_t device_id;
|
|
|
|
int ret;
|
|
|
|
struct vhost_dev *hdev;
|
|
|
|
|
|
|
|
hdev = (struct vhost_dev *)&net->dev;
|
|
|
|
ret = hdev->vhost_ops->vhost_get_device_id(hdev, &device_id);
|
|
|
|
if (device_id != VIRTIO_ID_NET) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-10-20 06:56:00 +02:00
|
|
|
static int vhost_vdpa_add(NetClientState *ncs, void *be,
|
|
|
|
int queue_pair_index, int nvqs)
|
2020-07-01 16:55:38 +02:00
|
|
|
{
|
|
|
|
VhostNetOptions options;
|
|
|
|
struct vhost_net *net = NULL;
|
|
|
|
VhostVDPAState *s;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
options.backend_type = VHOST_BACKEND_TYPE_VDPA;
|
|
|
|
assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
|
|
|
|
s = DO_UPCAST(VhostVDPAState, nc, ncs);
|
|
|
|
options.net_backend = ncs;
|
|
|
|
options.opaque = be;
|
|
|
|
options.busyloop_timeout = 0;
|
2021-10-20 06:56:00 +02:00
|
|
|
options.nvqs = nvqs;
|
2020-07-01 16:55:38 +02:00
|
|
|
|
|
|
|
net = vhost_net_init(&options);
|
|
|
|
if (!net) {
|
|
|
|
error_report("failed to init vhost_net for queue");
|
2021-09-03 11:10:19 +02:00
|
|
|
goto err_init;
|
2020-07-01 16:55:38 +02:00
|
|
|
}
|
|
|
|
s->vhost_net = net;
|
|
|
|
ret = vhost_vdpa_net_check_device_id(net);
|
|
|
|
if (ret) {
|
2021-09-03 11:10:19 +02:00
|
|
|
goto err_check;
|
2020-07-01 16:55:38 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2021-09-03 11:10:19 +02:00
|
|
|
err_check:
|
|
|
|
vhost_net_cleanup(net);
|
|
|
|
g_free(net);
|
|
|
|
err_init:
|
2020-07-01 16:55:38 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vhost_vdpa_cleanup(NetClientState *nc)
|
|
|
|
{
|
|
|
|
VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
|
|
|
|
|
|
|
|
if (s->vhost_net) {
|
|
|
|
vhost_net_cleanup(s->vhost_net);
|
|
|
|
g_free(s->vhost_net);
|
|
|
|
s->vhost_net = NULL;
|
|
|
|
}
|
2020-10-16 05:09:08 +02:00
|
|
|
if (s->vhost_vdpa.device_fd >= 0) {
|
|
|
|
qemu_close(s->vhost_vdpa.device_fd);
|
|
|
|
s->vhost_vdpa.device_fd = -1;
|
|
|
|
}
|
2020-07-01 16:55:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool vhost_vdpa_has_vnet_hdr(NetClientState *nc)
|
|
|
|
{
|
|
|
|
assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool vhost_vdpa_has_ufo(NetClientState *nc)
|
|
|
|
{
|
|
|
|
assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
|
|
|
|
VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
|
|
|
|
uint64_t features = 0;
|
|
|
|
features |= (1ULL << VIRTIO_NET_F_HOST_UFO);
|
|
|
|
features = vhost_net_get_features(s->vhost_net, features);
|
|
|
|
return !!(features & (1ULL << VIRTIO_NET_F_HOST_UFO));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-08 15:34:30 +02:00
|
|
|
static bool vhost_vdpa_check_peer_type(NetClientState *nc, ObjectClass *oc,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
const char *driver = object_class_get_name(oc);
|
|
|
|
|
|
|
|
if (!g_str_has_prefix(driver, "virtio-net-")) {
|
|
|
|
error_setg(errp, "vhost-vdpa requires frontend driver virtio-net-*");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-25 11:16:13 +01:00
|
|
|
/** Dummy receive in case qemu falls back to userland tap networking */
|
|
|
|
static ssize_t vhost_vdpa_receive(NetClientState *nc, const uint8_t *buf,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-01 16:55:38 +02:00
|
|
|
static NetClientInfo net_vhost_vdpa_info = {
|
|
|
|
.type = NET_CLIENT_DRIVER_VHOST_VDPA,
|
|
|
|
.size = sizeof(VhostVDPAState),
|
2021-11-25 11:16:13 +01:00
|
|
|
.receive = vhost_vdpa_receive,
|
2020-07-01 16:55:38 +02:00
|
|
|
.cleanup = vhost_vdpa_cleanup,
|
|
|
|
.has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
|
|
|
|
.has_ufo = vhost_vdpa_has_ufo,
|
2021-10-08 15:34:30 +02:00
|
|
|
.check_peer_type = vhost_vdpa_check_peer_type,
|
2020-07-01 16:55:38 +02:00
|
|
|
};
|
|
|
|
|
2021-10-20 06:55:54 +02:00
|
|
|
static NetClientState *net_vhost_vdpa_init(NetClientState *peer,
|
|
|
|
const char *device,
|
|
|
|
const char *name,
|
2021-10-20 06:56:00 +02:00
|
|
|
int vdpa_device_fd,
|
|
|
|
int queue_pair_index,
|
|
|
|
int nvqs,
|
|
|
|
bool is_datapath)
|
2020-07-01 16:55:38 +02:00
|
|
|
{
|
|
|
|
NetClientState *nc = NULL;
|
|
|
|
VhostVDPAState *s;
|
|
|
|
int ret = 0;
|
|
|
|
assert(name);
|
2021-10-20 06:56:00 +02:00
|
|
|
if (is_datapath) {
|
|
|
|
nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, device,
|
|
|
|
name);
|
|
|
|
} else {
|
|
|
|
nc = qemu_new_net_control_client(&net_vhost_vdpa_info, peer,
|
|
|
|
device, name);
|
|
|
|
}
|
2021-04-02 05:03:33 +02:00
|
|
|
snprintf(nc->info_str, sizeof(nc->info_str), TYPE_VHOST_VDPA);
|
2020-07-01 16:55:38 +02:00
|
|
|
s = DO_UPCAST(VhostVDPAState, nc, nc);
|
2021-10-20 06:55:51 +02:00
|
|
|
|
2020-07-01 16:55:38 +02:00
|
|
|
s->vhost_vdpa.device_fd = vdpa_device_fd;
|
2021-10-20 06:56:00 +02:00
|
|
|
s->vhost_vdpa.index = queue_pair_index;
|
|
|
|
ret = vhost_vdpa_add(nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs);
|
2021-09-03 11:10:20 +02:00
|
|
|
if (ret) {
|
|
|
|
qemu_del_net_client(nc);
|
2021-10-20 06:55:54 +02:00
|
|
|
return NULL;
|
2021-09-03 11:10:20 +02:00
|
|
|
}
|
2021-10-20 06:55:54 +02:00
|
|
|
return nc;
|
2020-07-01 16:55:38 +02:00
|
|
|
}
|
|
|
|
|
2021-10-20 06:56:00 +02:00
|
|
|
static int vhost_vdpa_get_max_queue_pairs(int fd, int *has_cvq, Error **errp)
|
|
|
|
{
|
|
|
|
unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
|
2021-11-02 16:51:57 +01:00
|
|
|
g_autofree struct vhost_vdpa_config *config = NULL;
|
2021-10-20 06:56:00 +02:00
|
|
|
__virtio16 *max_queue_pairs;
|
|
|
|
uint64_t features;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ioctl(fd, VHOST_GET_FEATURES, &features);
|
|
|
|
if (ret) {
|
|
|
|
error_setg(errp, "Fail to query features from vhost-vDPA device");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (features & (1 << VIRTIO_NET_F_CTRL_VQ)) {
|
|
|
|
*has_cvq = 1;
|
|
|
|
} else {
|
|
|
|
*has_cvq = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (features & (1 << VIRTIO_NET_F_MQ)) {
|
|
|
|
config = g_malloc0(config_size + sizeof(*max_queue_pairs));
|
|
|
|
config->off = offsetof(struct virtio_net_config, max_virtqueue_pairs);
|
|
|
|
config->len = sizeof(*max_queue_pairs);
|
|
|
|
|
|
|
|
ret = ioctl(fd, VHOST_VDPA_GET_CONFIG, config);
|
|
|
|
if (ret) {
|
|
|
|
error_setg(errp, "Fail to get config from vhost-vDPA device");
|
|
|
|
return -ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
max_queue_pairs = (__virtio16 *)&config->buf;
|
|
|
|
|
|
|
|
return lduw_le_p(max_queue_pairs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-01 16:55:38 +02:00
|
|
|
int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
|
|
|
|
NetClientState *peer, Error **errp)
|
|
|
|
{
|
|
|
|
const NetdevVhostVDPAOptions *opts;
|
2021-10-20 06:55:54 +02:00
|
|
|
int vdpa_device_fd;
|
2021-10-20 06:56:00 +02:00
|
|
|
NetClientState **ncs, *nc;
|
|
|
|
int queue_pairs, i, has_cvq = 0;
|
2020-07-01 16:55:38 +02:00
|
|
|
|
|
|
|
assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA);
|
|
|
|
opts = &netdev->u.vhost_vdpa;
|
2021-11-12 20:34:31 +01:00
|
|
|
if (!opts->vhostdev) {
|
|
|
|
error_setg(errp, "vdpa character device not specified with vhostdev");
|
|
|
|
return -1;
|
|
|
|
}
|
2021-10-20 06:55:51 +02:00
|
|
|
|
2021-11-12 20:34:30 +01:00
|
|
|
vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR, errp);
|
2021-10-20 06:55:51 +02:00
|
|
|
if (vdpa_device_fd == -1) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2021-10-20 06:56:00 +02:00
|
|
|
queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd,
|
|
|
|
&has_cvq, errp);
|
|
|
|
if (queue_pairs < 0) {
|
2021-10-20 06:55:51 +02:00
|
|
|
qemu_close(vdpa_device_fd);
|
2021-10-20 06:56:00 +02:00
|
|
|
return queue_pairs;
|
|
|
|
}
|
|
|
|
|
|
|
|
ncs = g_malloc0(sizeof(*ncs) * queue_pairs);
|
|
|
|
|
|
|
|
for (i = 0; i < queue_pairs; i++) {
|
|
|
|
ncs[i] = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
|
|
|
|
vdpa_device_fd, i, 2, true);
|
|
|
|
if (!ncs[i])
|
|
|
|
goto err;
|
2021-10-20 06:55:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-20 06:56:00 +02:00
|
|
|
if (has_cvq) {
|
|
|
|
nc = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
|
|
|
|
vdpa_device_fd, i, 1, false);
|
|
|
|
if (!nc)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(ncs);
|
2021-10-20 06:55:54 +02:00
|
|
|
return 0;
|
2021-10-20 06:56:00 +02:00
|
|
|
|
|
|
|
err:
|
|
|
|
if (i) {
|
|
|
|
qemu_del_net_client(ncs[0]);
|
|
|
|
}
|
|
|
|
qemu_close(vdpa_device_fd);
|
|
|
|
g_free(ncs);
|
|
|
|
|
|
|
|
return -1;
|
2020-07-01 16:55:38 +02:00
|
|
|
}
|