Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * VDPA simulator for networking device.
0004  *
0005  * Copyright (c) 2020, Red Hat Inc. All rights reserved.
0006  *     Author: Jason Wang <jasowang@redhat.com>
0007  *
0008  */
0009 
0010 #include <linux/init.h>
0011 #include <linux/module.h>
0012 #include <linux/device.h>
0013 #include <linux/kernel.h>
0014 #include <linux/sched.h>
0015 #include <linux/etherdevice.h>
0016 #include <linux/vringh.h>
0017 #include <linux/vdpa.h>
0018 #include <uapi/linux/virtio_net.h>
0019 #include <uapi/linux/vdpa.h>
0020 
0021 #include "vdpa_sim.h"
0022 
0023 #define DRV_VERSION  "0.1"
0024 #define DRV_AUTHOR   "Jason Wang <jasowang@redhat.com>"
0025 #define DRV_DESC     "vDPA Device Simulator for networking device"
0026 #define DRV_LICENSE  "GPL v2"
0027 
0028 #define VDPASIM_NET_FEATURES    (VDPASIM_FEATURES | \
0029                  (1ULL << VIRTIO_NET_F_MAC) | \
0030                  (1ULL << VIRTIO_NET_F_MTU) | \
0031                  (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
0032                  (1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR))
0033 
0034 /* 3 virtqueues, 2 address spaces, 2 virtqueue groups */
0035 #define VDPASIM_NET_VQ_NUM  3
0036 #define VDPASIM_NET_AS_NUM  2
0037 #define VDPASIM_NET_GROUP_NUM   2
0038 
0039 static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t len)
0040 {
0041     /* Make sure data is wrote before advancing index */
0042     smp_wmb();
0043 
0044     vringh_complete_iotlb(&vq->vring, vq->head, len);
0045 
0046     /* Make sure used is visible before rasing the interrupt. */
0047     smp_wmb();
0048 
0049     local_bh_disable();
0050     if (vringh_need_notify_iotlb(&vq->vring) > 0)
0051         vringh_notify(&vq->vring);
0052     local_bh_enable();
0053 }
0054 
0055 static bool receive_filter(struct vdpasim *vdpasim, size_t len)
0056 {
0057     bool modern = vdpasim->features & (1ULL << VIRTIO_F_VERSION_1);
0058     size_t hdr_len = modern ? sizeof(struct virtio_net_hdr_v1) :
0059                   sizeof(struct virtio_net_hdr);
0060     struct virtio_net_config *vio_config = vdpasim->config;
0061 
0062     if (len < ETH_ALEN + hdr_len)
0063         return false;
0064 
0065     if (!strncmp(vdpasim->buffer + hdr_len, vio_config->mac, ETH_ALEN))
0066         return true;
0067 
0068     return false;
0069 }
0070 
0071 static virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
0072                            u8 cmd)
0073 {
0074     struct virtio_net_config *vio_config = vdpasim->config;
0075     struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
0076     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
0077     size_t read;
0078 
0079     switch (cmd) {
0080     case VIRTIO_NET_CTRL_MAC_ADDR_SET:
0081         read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov,
0082                          vio_config->mac, ETH_ALEN);
0083         if (read == ETH_ALEN)
0084             status = VIRTIO_NET_OK;
0085         break;
0086     default:
0087         break;
0088     }
0089 
0090     return status;
0091 }
0092 
0093 static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
0094 {
0095     struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
0096     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
0097     struct virtio_net_ctrl_hdr ctrl;
0098     size_t read, write;
0099     int err;
0100 
0101     if (!(vdpasim->features & (1ULL << VIRTIO_NET_F_CTRL_VQ)))
0102         return;
0103 
0104     if (!cvq->ready)
0105         return;
0106 
0107     while (true) {
0108         err = vringh_getdesc_iotlb(&cvq->vring, &cvq->in_iov,
0109                        &cvq->out_iov,
0110                        &cvq->head, GFP_ATOMIC);
0111         if (err <= 0)
0112             break;
0113 
0114         read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov, &ctrl,
0115                          sizeof(ctrl));
0116         if (read != sizeof(ctrl))
0117             break;
0118 
0119         switch (ctrl.class) {
0120         case VIRTIO_NET_CTRL_MAC:
0121             status = vdpasim_handle_ctrl_mac(vdpasim, ctrl.cmd);
0122             break;
0123         default:
0124             break;
0125         }
0126 
0127         /* Make sure data is wrote before advancing index */
0128         smp_wmb();
0129 
0130         write = vringh_iov_push_iotlb(&cvq->vring, &cvq->out_iov,
0131                           &status, sizeof(status));
0132         vringh_complete_iotlb(&cvq->vring, cvq->head, write);
0133         vringh_kiov_cleanup(&cvq->in_iov);
0134         vringh_kiov_cleanup(&cvq->out_iov);
0135 
0136         /* Make sure used is visible before rasing the interrupt. */
0137         smp_wmb();
0138 
0139         local_bh_disable();
0140         if (cvq->cb)
0141             cvq->cb(cvq->private);
0142         local_bh_enable();
0143     }
0144 }
0145 
0146 static void vdpasim_net_work(struct work_struct *work)
0147 {
0148     struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
0149     struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
0150     struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
0151     ssize_t read, write;
0152     int pkts = 0;
0153     int err;
0154 
0155     spin_lock(&vdpasim->lock);
0156 
0157     if (!vdpasim->running)
0158         goto out;
0159 
0160     if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
0161         goto out;
0162 
0163     vdpasim_handle_cvq(vdpasim);
0164 
0165     if (!txq->ready || !rxq->ready)
0166         goto out;
0167 
0168     while (true) {
0169         err = vringh_getdesc_iotlb(&txq->vring, &txq->out_iov, NULL,
0170                        &txq->head, GFP_ATOMIC);
0171         if (err <= 0)
0172             break;
0173 
0174         read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
0175                          vdpasim->buffer,
0176                          PAGE_SIZE);
0177 
0178         if (!receive_filter(vdpasim, read)) {
0179             vdpasim_net_complete(txq, 0);
0180             continue;
0181         }
0182 
0183         err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->in_iov,
0184                        &rxq->head, GFP_ATOMIC);
0185         if (err <= 0) {
0186             vdpasim_net_complete(txq, 0);
0187             break;
0188         }
0189 
0190         write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
0191                           vdpasim->buffer, read);
0192         if (write <= 0)
0193             break;
0194 
0195         vdpasim_net_complete(txq, 0);
0196         vdpasim_net_complete(rxq, write);
0197 
0198         if (++pkts > 4) {
0199             schedule_work(&vdpasim->work);
0200             goto out;
0201         }
0202     }
0203 
0204 out:
0205     spin_unlock(&vdpasim->lock);
0206 }
0207 
0208 static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
0209 {
0210     struct virtio_net_config *net_config = config;
0211 
0212     net_config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
0213 }
0214 
0215 static void vdpasim_net_setup_config(struct vdpasim *vdpasim,
0216                      const struct vdpa_dev_set_config *config)
0217 {
0218     struct virtio_net_config *vio_config = vdpasim->config;
0219 
0220     if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR))
0221         memcpy(vio_config->mac, config->net.mac, ETH_ALEN);
0222     if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MTU))
0223         vio_config->mtu = cpu_to_vdpasim16(vdpasim, config->net.mtu);
0224     else
0225         /* Setup default MTU to be 1500 */
0226         vio_config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
0227 }
0228 
0229 static void vdpasim_net_mgmtdev_release(struct device *dev)
0230 {
0231 }
0232 
0233 static struct device vdpasim_net_mgmtdev = {
0234     .init_name = "vdpasim_net",
0235     .release = vdpasim_net_mgmtdev_release,
0236 };
0237 
0238 static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
0239                    const struct vdpa_dev_set_config *config)
0240 {
0241     struct vdpasim_dev_attr dev_attr = {};
0242     struct vdpasim *simdev;
0243     int ret;
0244 
0245     dev_attr.mgmt_dev = mdev;
0246     dev_attr.name = name;
0247     dev_attr.id = VIRTIO_ID_NET;
0248     dev_attr.supported_features = VDPASIM_NET_FEATURES;
0249     dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
0250     dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
0251     dev_attr.nas = VDPASIM_NET_AS_NUM;
0252     dev_attr.config_size = sizeof(struct virtio_net_config);
0253     dev_attr.get_config = vdpasim_net_get_config;
0254     dev_attr.work_fn = vdpasim_net_work;
0255     dev_attr.buffer_size = PAGE_SIZE;
0256 
0257     simdev = vdpasim_create(&dev_attr);
0258     if (IS_ERR(simdev))
0259         return PTR_ERR(simdev);
0260 
0261     vdpasim_net_setup_config(simdev, config);
0262 
0263     ret = _vdpa_register_device(&simdev->vdpa, VDPASIM_NET_VQ_NUM);
0264     if (ret)
0265         goto reg_err;
0266 
0267     return 0;
0268 
0269 reg_err:
0270     put_device(&simdev->vdpa.dev);
0271     return ret;
0272 }
0273 
0274 static void vdpasim_net_dev_del(struct vdpa_mgmt_dev *mdev,
0275                 struct vdpa_device *dev)
0276 {
0277     struct vdpasim *simdev = container_of(dev, struct vdpasim, vdpa);
0278 
0279     _vdpa_unregister_device(&simdev->vdpa);
0280 }
0281 
0282 static const struct vdpa_mgmtdev_ops vdpasim_net_mgmtdev_ops = {
0283     .dev_add = vdpasim_net_dev_add,
0284     .dev_del = vdpasim_net_dev_del
0285 };
0286 
0287 static struct virtio_device_id id_table[] = {
0288     { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
0289     { 0 },
0290 };
0291 
0292 static struct vdpa_mgmt_dev mgmt_dev = {
0293     .device = &vdpasim_net_mgmtdev,
0294     .id_table = id_table,
0295     .ops = &vdpasim_net_mgmtdev_ops,
0296     .config_attr_mask = (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR |
0297                  1 << VDPA_ATTR_DEV_NET_CFG_MTU),
0298     .max_supported_vqs = VDPASIM_NET_VQ_NUM,
0299     .supported_features = VDPASIM_NET_FEATURES,
0300 };
0301 
0302 static int __init vdpasim_net_init(void)
0303 {
0304     int ret;
0305 
0306     ret = device_register(&vdpasim_net_mgmtdev);
0307     if (ret)
0308         return ret;
0309 
0310     ret = vdpa_mgmtdev_register(&mgmt_dev);
0311     if (ret)
0312         goto parent_err;
0313     return 0;
0314 
0315 parent_err:
0316     device_unregister(&vdpasim_net_mgmtdev);
0317     return ret;
0318 }
0319 
0320 static void __exit vdpasim_net_exit(void)
0321 {
0322     vdpa_mgmtdev_unregister(&mgmt_dev);
0323     device_unregister(&vdpasim_net_mgmtdev);
0324 }
0325 
0326 module_init(vdpasim_net_init);
0327 module_exit(vdpasim_net_exit);
0328 
0329 MODULE_VERSION(DRV_VERSION);
0330 MODULE_LICENSE(DRV_LICENSE);
0331 MODULE_AUTHOR(DRV_AUTHOR);
0332 MODULE_DESCRIPTION(DRV_DESC);