提交 769f5f3e 编写于 作者: P Parav Pandit 提交者: Pengyuan Zhao

vdpa_sim_net: Add support for user supported devices

stable inclusion
from stable-v5.12
commit a3c06ae1
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I5WXCZ
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=a3c06ae158dd6fa8336157c31d9234689d068d02

----------------------------------------------------------------------

Enable user to create vdpasim net simulate devices.

Show vdpa management device that supports creating, deleting vdpa devices.

$ vdpa mgmtdev show
vdpasim_net:
  supported_classes
    net

$ vdpa mgmtdev show -jp
{
    "show": {
        "vdpasim_net": {
            "supported_classes": {
              "net"
        }
    }
}

Create a vdpa device of type networking named as "foo2" from
the management device vdpasim:

$ vdpa dev add mgmtdev vdpasim_net name foo2

Show the newly created vdpa device by its name:
$ vdpa dev show foo2
foo2: type network mgmtdev vdpasim_net vendor_id 0 max_vqs 2 max_vq_size 256

$ vdpa dev show foo2 -jp
{
    "dev": {
        "foo2": {
            "type": "network",
            "mgmtdev": "vdpasim_net",
            "vendor_id": 0,
            "max_vqs": 2,
            "max_vq_size": 256
        }
    }
}

Delete the vdpa device after its use:
$ vdpa dev del foo2
Signed-off-by: NParav Pandit <parav@nvidia.com>
Reviewed-by: NEli Cohen <elic@nvidia.com>
Acked-by: NJason Wang <jasowang@redhat.com>
Link: https://lore.kernel.org/r/20210105103203.82508-7-parav@nvidia.comSigned-off-by: NMichael S. Tsirkin <mst@redhat.com>
Signed-off-by: NPengyuan Zhao <zhaopengyuan@hisilicon.com>
上级 ca1f46fa
...@@ -222,7 +222,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr) ...@@ -222,7 +222,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
ops = &vdpasim_net_config_ops; ops = &vdpasim_net_config_ops;
vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
NULL); dev_attr->name);
if (!vdpasim) if (!vdpasim)
goto err_alloc; goto err_alloc;
...@@ -236,6 +236,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr) ...@@ -236,6 +236,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
goto err_iommu; goto err_iommu;
set_dma_ops(dev, &vdpasim_dma_ops); set_dma_ops(dev, &vdpasim_dma_ops);
vdpasim->vdpa.mdev = dev_attr->mgmt_dev;
vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL); vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL);
if (!vdpasim->config) if (!vdpasim->config)
......
...@@ -33,6 +33,8 @@ struct vdpasim_virtqueue { ...@@ -33,6 +33,8 @@ struct vdpasim_virtqueue {
}; };
struct vdpasim_dev_attr { struct vdpasim_dev_attr {
struct vdpa_mgmt_dev *mgmt_dev;
const char *name;
u64 supported_features; u64 supported_features;
size_t config_size; size_t config_size;
size_t buffer_size; size_t buffer_size;
......
...@@ -35,8 +35,6 @@ MODULE_PARM_DESC(macaddr, "Ethernet MAC address"); ...@@ -35,8 +35,6 @@ MODULE_PARM_DESC(macaddr, "Ethernet MAC address");
u8 macaddr_buf[ETH_ALEN]; u8 macaddr_buf[ETH_ALEN];
static struct vdpasim *vdpasim_net_dev;
static void vdpasim_net_work(struct work_struct *work) static void vdpasim_net_work(struct work_struct *work)
{ {
struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
...@@ -120,21 +118,23 @@ static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config) ...@@ -120,21 +118,23 @@ static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
memcpy(net_config->mac, macaddr_buf, ETH_ALEN); memcpy(net_config->mac, macaddr_buf, ETH_ALEN);
} }
static int __init vdpasim_net_init(void) static void vdpasim_net_mgmtdev_release(struct device *dev)
{
}
static struct device vdpasim_net_mgmtdev = {
.init_name = "vdpasim_net",
.release = vdpasim_net_mgmtdev_release,
};
static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name)
{ {
struct vdpasim_dev_attr dev_attr = {}; struct vdpasim_dev_attr dev_attr = {};
struct vdpasim *simdev;
int ret; int ret;
if (macaddr) { dev_attr.mgmt_dev = mdev;
mac_pton(macaddr, macaddr_buf); dev_attr.name = name;
if (!is_valid_ether_addr(macaddr_buf)) {
ret = -EADDRNOTAVAIL;
goto out;
}
} else {
eth_random_addr(macaddr_buf);
}
dev_attr.id = VIRTIO_ID_NET; dev_attr.id = VIRTIO_ID_NET;
dev_attr.supported_features = VDPASIM_NET_FEATURES; dev_attr.supported_features = VDPASIM_NET_FEATURES;
dev_attr.nvqs = VDPASIM_NET_VQ_NUM; dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
...@@ -143,29 +143,75 @@ static int __init vdpasim_net_init(void) ...@@ -143,29 +143,75 @@ static int __init vdpasim_net_init(void)
dev_attr.work_fn = vdpasim_net_work; dev_attr.work_fn = vdpasim_net_work;
dev_attr.buffer_size = PAGE_SIZE; dev_attr.buffer_size = PAGE_SIZE;
vdpasim_net_dev = vdpasim_create(&dev_attr); simdev = vdpasim_create(&dev_attr);
if (IS_ERR(vdpasim_net_dev)) { if (IS_ERR(simdev))
ret = PTR_ERR(vdpasim_net_dev); return PTR_ERR(simdev);
goto out;
ret = _vdpa_register_device(&simdev->vdpa, VDPASIM_NET_VQ_NUM);
if (ret)
goto reg_err;
return 0;
reg_err:
put_device(&simdev->vdpa.dev);
return ret;
}
static void vdpasim_net_dev_del(struct vdpa_mgmt_dev *mdev,
struct vdpa_device *dev)
{
struct vdpasim *simdev = container_of(dev, struct vdpasim, vdpa);
_vdpa_unregister_device(&simdev->vdpa);
}
static const struct vdpa_mgmtdev_ops vdpasim_net_mgmtdev_ops = {
.dev_add = vdpasim_net_dev_add,
.dev_del = vdpasim_net_dev_del
};
static struct virtio_device_id id_table[] = {
{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
{ 0 },
};
static struct vdpa_mgmt_dev mgmt_dev = {
.device = &vdpasim_net_mgmtdev,
.id_table = id_table,
.ops = &vdpasim_net_mgmtdev_ops,
};
static int __init vdpasim_net_init(void)
{
int ret;
if (macaddr) {
mac_pton(macaddr, macaddr_buf);
if (!is_valid_ether_addr(macaddr_buf))
return -EADDRNOTAVAIL;
} else {
eth_random_addr(macaddr_buf);
} }
ret = vdpa_register_device(&vdpasim_net_dev->vdpa, VDPASIM_NET_VQ_NUM); ret = device_register(&vdpasim_net_mgmtdev);
if (ret) if (ret)
goto put_dev; return ret;
ret = vdpa_mgmtdev_register(&mgmt_dev);
if (ret)
goto parent_err;
return 0; return 0;
put_dev: parent_err:
put_device(&vdpasim_net_dev->vdpa.dev); device_unregister(&vdpasim_net_mgmtdev);
out:
return ret; return ret;
} }
static void __exit vdpasim_net_exit(void) static void __exit vdpasim_net_exit(void)
{ {
struct vdpa_device *vdpa = &vdpasim_net_dev->vdpa; vdpa_mgmtdev_unregister(&mgmt_dev);
device_unregister(&vdpasim_net_mgmtdev);
vdpa_unregister_device(vdpa);
} }
module_init(vdpasim_net_init); module_init(vdpasim_net_init);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册