1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * VDPA simulator for networking device. 4 * 5 * Copyright (c) 2020, Red Hat Inc. All rights reserved. 6 * Author: Jason Wang <jasowang@redhat.com> 7 * 8 */ 9 10 #include <linux/init.h> 11 #include <linux/module.h> 12 #include <linux/device.h> 13 #include <linux/kernel.h> 14 #include <linux/sched.h> 15 #include <linux/etherdevice.h> 16 #include <linux/vringh.h> 17 #include <linux/vdpa.h> 18 #include <uapi/linux/virtio_net.h> 19 #include <uapi/linux/vdpa.h> 20 21 #include "vdpa_sim.h" 22 23 #define DRV_VERSION "0.1" 24 #define DRV_AUTHOR "Jason Wang <jasowang@redhat.com>" 25 #define DRV_DESC "vDPA Device Simulator for networking device" 26 #define DRV_LICENSE "GPL v2" 27 28 #define VDPASIM_NET_FEATURES (VDPASIM_FEATURES | \ 29 (1ULL << VIRTIO_NET_F_MAC)) 30 31 #define VDPASIM_NET_VQ_NUM 2 32 33 static void vdpasim_net_work(struct work_struct *work) 34 { 35 struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); 36 struct vdpasim_virtqueue *txq = &vdpasim->vqs[1]; 37 struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0]; 38 ssize_t read, write; 39 size_t total_write; 40 int pkts = 0; 41 int err; 42 43 spin_lock(&vdpasim->lock); 44 45 if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK)) 46 goto out; 47 48 if (!txq->ready || !rxq->ready) 49 goto out; 50 51 while (true) { 52 total_write = 0; 53 err = vringh_getdesc_iotlb(&txq->vring, &txq->out_iov, NULL, 54 &txq->head, GFP_ATOMIC); 55 if (err <= 0) 56 break; 57 58 err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->in_iov, 59 &rxq->head, GFP_ATOMIC); 60 if (err <= 0) { 61 vringh_complete_iotlb(&txq->vring, txq->head, 0); 62 break; 63 } 64 65 while (true) { 66 read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov, 67 vdpasim->buffer, 68 PAGE_SIZE); 69 if (read <= 0) 70 break; 71 72 write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov, 73 vdpasim->buffer, read); 74 if (write <= 0) 75 break; 76 77 total_write += write; 78 } 79 80 /* Make sure data is wrote before advancing index */ 81 smp_wmb(); 82 83 vringh_complete_iotlb(&txq->vring, txq->head, 0); 84 vringh_complete_iotlb(&rxq->vring, rxq->head, total_write); 85 86 /* Make sure used is visible before rasing the interrupt. */ 87 smp_wmb(); 88 89 local_bh_disable(); 90 if (vringh_need_notify_iotlb(&txq->vring) > 0) 91 vringh_notify(&txq->vring); 92 if (vringh_need_notify_iotlb(&rxq->vring) > 0) 93 vringh_notify(&rxq->vring); 94 local_bh_enable(); 95 96 if (++pkts > 4) { 97 schedule_work(&vdpasim->work); 98 goto out; 99 } 100 } 101 102 out: 103 spin_unlock(&vdpasim->lock); 104 } 105 106 static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config) 107 { 108 struct virtio_net_config *net_config = config; 109 110 net_config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP); 111 } 112 113 static void vdpasim_net_setup_config(struct vdpasim *vdpasim, 114 const struct vdpa_dev_set_config *config) 115 { 116 struct virtio_net_config *vio_config = vdpasim->config; 117 118 if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR)) 119 memcpy(vio_config->mac, config->net.mac, ETH_ALEN); 120 if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MTU)) 121 vio_config->mtu = cpu_to_vdpasim16(vdpasim, config->net.mtu); 122 else 123 /* Setup default MTU to be 1500 */ 124 vio_config->mtu = cpu_to_vdpasim16(vdpasim, 1500); 125 } 126 127 static void vdpasim_net_mgmtdev_release(struct device *dev) 128 { 129 } 130 131 static struct device vdpasim_net_mgmtdev = { 132 .init_name = "vdpasim_net", 133 .release = vdpasim_net_mgmtdev_release, 134 }; 135 136 static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name, 137 const struct vdpa_dev_set_config *config) 138 { 139 struct vdpasim_dev_attr dev_attr = {}; 140 struct vdpasim *simdev; 141 int ret; 142 143 dev_attr.mgmt_dev = mdev; 144 dev_attr.name = name; 145 dev_attr.id = VIRTIO_ID_NET; 146 dev_attr.supported_features = VDPASIM_NET_FEATURES; 147 dev_attr.nvqs = VDPASIM_NET_VQ_NUM; 148 dev_attr.config_size = sizeof(struct virtio_net_config); 149 dev_attr.get_config = vdpasim_net_get_config; 150 dev_attr.work_fn = vdpasim_net_work; 151 dev_attr.buffer_size = PAGE_SIZE; 152 153 simdev = vdpasim_create(&dev_attr); 154 if (IS_ERR(simdev)) 155 return PTR_ERR(simdev); 156 157 vdpasim_net_setup_config(simdev, config); 158 159 ret = _vdpa_register_device(&simdev->vdpa, VDPASIM_NET_VQ_NUM); 160 if (ret) 161 goto reg_err; 162 163 return 0; 164 165 reg_err: 166 put_device(&simdev->vdpa.dev); 167 return ret; 168 } 169 170 static void vdpasim_net_dev_del(struct vdpa_mgmt_dev *mdev, 171 struct vdpa_device *dev) 172 { 173 struct vdpasim *simdev = container_of(dev, struct vdpasim, vdpa); 174 175 _vdpa_unregister_device(&simdev->vdpa); 176 } 177 178 static const struct vdpa_mgmtdev_ops vdpasim_net_mgmtdev_ops = { 179 .dev_add = vdpasim_net_dev_add, 180 .dev_del = vdpasim_net_dev_del 181 }; 182 183 static struct virtio_device_id id_table[] = { 184 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 185 { 0 }, 186 }; 187 188 static struct vdpa_mgmt_dev mgmt_dev = { 189 .device = &vdpasim_net_mgmtdev, 190 .id_table = id_table, 191 .ops = &vdpasim_net_mgmtdev_ops, 192 .config_attr_mask = (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR | 193 1 << VDPA_ATTR_DEV_NET_CFG_MTU), 194 }; 195 196 static int __init vdpasim_net_init(void) 197 { 198 int ret; 199 200 ret = device_register(&vdpasim_net_mgmtdev); 201 if (ret) 202 return ret; 203 204 ret = vdpa_mgmtdev_register(&mgmt_dev); 205 if (ret) 206 goto parent_err; 207 return 0; 208 209 parent_err: 210 device_unregister(&vdpasim_net_mgmtdev); 211 return ret; 212 } 213 214 static void __exit vdpasim_net_exit(void) 215 { 216 vdpa_mgmtdev_unregister(&mgmt_dev); 217 device_unregister(&vdpasim_net_mgmtdev); 218 } 219 220 module_init(vdpasim_net_init); 221 module_exit(vdpasim_net_exit); 222 223 MODULE_VERSION(DRV_VERSION); 224 MODULE_LICENSE(DRV_LICENSE); 225 MODULE_AUTHOR(DRV_AUTHOR); 226 MODULE_DESCRIPTION(DRV_DESC); 227