1 // SPDX-License-Identifier: GPL-2.0-only
3 * VDPA simulator for networking device.
5 * Copyright (c) 2020, Red Hat Inc. All rights reserved.
6 * Author: Jason Wang <jasowang@redhat.com>
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>
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"
28 #define VDPASIM_NET_FEATURES (VDPASIM_FEATURES | \
29 (1ULL << VIRTIO_NET_F_MAC) | \
30 (1ULL << VIRTIO_NET_F_MTU) | \
31 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
32 (1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR))
34 /* 3 virtqueues, 2 address spaces, 2 virtqueue groups */
35 #define VDPASIM_NET_VQ_NUM 3
36 #define VDPASIM_NET_AS_NUM 2
37 #define VDPASIM_NET_GROUP_NUM 2
39 static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t len)
41 /* Make sure data is wrote before advancing index */
44 vringh_complete_iotlb(&vq->vring, vq->head, len);
46 /* Make sure used is visible before rasing the interrupt. */
50 if (vringh_need_notify_iotlb(&vq->vring) > 0)
51 vringh_notify(&vq->vring);
55 static bool receive_filter(struct vdpasim *vdpasim, size_t len)
57 bool modern = vdpasim->features & (1ULL << VIRTIO_F_VERSION_1);
58 size_t hdr_len = modern ? sizeof(struct virtio_net_hdr_v1) :
59 sizeof(struct virtio_net_hdr);
60 struct virtio_net_config *vio_config = vdpasim->config;
62 if (len < ETH_ALEN + hdr_len)
65 if (!strncmp(vdpasim->buffer + hdr_len, vio_config->mac, ETH_ALEN))
71 static virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
74 struct virtio_net_config *vio_config = vdpasim->config;
75 struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
76 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
80 case VIRTIO_NET_CTRL_MAC_ADDR_SET:
81 read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov,
82 vio_config->mac, ETH_ALEN);
84 status = VIRTIO_NET_OK;
93 static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
95 struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
96 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
97 struct virtio_net_ctrl_hdr ctrl;
101 if (!(vdpasim->features & (1ULL << VIRTIO_NET_F_CTRL_VQ)))
108 err = vringh_getdesc_iotlb(&cvq->vring, &cvq->in_iov,
110 &cvq->head, GFP_ATOMIC);
114 read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov, &ctrl,
116 if (read != sizeof(ctrl))
119 switch (ctrl.class) {
120 case VIRTIO_NET_CTRL_MAC:
121 status = vdpasim_handle_ctrl_mac(vdpasim, ctrl.cmd);
127 /* Make sure data is wrote before advancing index */
130 write = vringh_iov_push_iotlb(&cvq->vring, &cvq->out_iov,
131 &status, sizeof(status));
132 vringh_complete_iotlb(&cvq->vring, cvq->head, write);
133 vringh_kiov_cleanup(&cvq->in_iov);
134 vringh_kiov_cleanup(&cvq->out_iov);
136 /* Make sure used is visible before rasing the interrupt. */
141 cvq->cb(cvq->private);
146 static void vdpasim_net_work(struct work_struct *work)
148 struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
149 struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
150 struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
155 spin_lock(&vdpasim->lock);
157 if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
160 vdpasim_handle_cvq(vdpasim);
162 if (!txq->ready || !rxq->ready)
166 err = vringh_getdesc_iotlb(&txq->vring, &txq->out_iov, NULL,
167 &txq->head, GFP_ATOMIC);
171 read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
175 if (!receive_filter(vdpasim, read)) {
176 vdpasim_net_complete(txq, 0);
180 err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->in_iov,
181 &rxq->head, GFP_ATOMIC);
183 vdpasim_net_complete(txq, 0);
187 write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
188 vdpasim->buffer, read);
192 vdpasim_net_complete(txq, 0);
193 vdpasim_net_complete(rxq, write);
196 schedule_work(&vdpasim->work);
202 spin_unlock(&vdpasim->lock);
205 static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
207 struct virtio_net_config *net_config = config;
209 net_config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
212 static void vdpasim_net_setup_config(struct vdpasim *vdpasim,
213 const struct vdpa_dev_set_config *config)
215 struct virtio_net_config *vio_config = vdpasim->config;
217 if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR))
218 memcpy(vio_config->mac, config->net.mac, ETH_ALEN);
219 if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MTU))
220 vio_config->mtu = cpu_to_vdpasim16(vdpasim, config->net.mtu);
222 /* Setup default MTU to be 1500 */
223 vio_config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
226 static void vdpasim_net_mgmtdev_release(struct device *dev)
230 static struct device vdpasim_net_mgmtdev = {
231 .init_name = "vdpasim_net",
232 .release = vdpasim_net_mgmtdev_release,
235 static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
236 const struct vdpa_dev_set_config *config)
238 struct vdpasim_dev_attr dev_attr = {};
239 struct vdpasim *simdev;
242 dev_attr.mgmt_dev = mdev;
243 dev_attr.name = name;
244 dev_attr.id = VIRTIO_ID_NET;
245 dev_attr.supported_features = VDPASIM_NET_FEATURES;
246 dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
247 dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
248 dev_attr.nas = VDPASIM_NET_AS_NUM;
249 dev_attr.config_size = sizeof(struct virtio_net_config);
250 dev_attr.get_config = vdpasim_net_get_config;
251 dev_attr.work_fn = vdpasim_net_work;
252 dev_attr.buffer_size = PAGE_SIZE;
254 simdev = vdpasim_create(&dev_attr);
256 return PTR_ERR(simdev);
258 vdpasim_net_setup_config(simdev, config);
260 ret = _vdpa_register_device(&simdev->vdpa, VDPASIM_NET_VQ_NUM);
267 put_device(&simdev->vdpa.dev);
271 static void vdpasim_net_dev_del(struct vdpa_mgmt_dev *mdev,
272 struct vdpa_device *dev)
274 struct vdpasim *simdev = container_of(dev, struct vdpasim, vdpa);
276 _vdpa_unregister_device(&simdev->vdpa);
279 static const struct vdpa_mgmtdev_ops vdpasim_net_mgmtdev_ops = {
280 .dev_add = vdpasim_net_dev_add,
281 .dev_del = vdpasim_net_dev_del
284 static struct virtio_device_id id_table[] = {
285 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
289 static struct vdpa_mgmt_dev mgmt_dev = {
290 .device = &vdpasim_net_mgmtdev,
291 .id_table = id_table,
292 .ops = &vdpasim_net_mgmtdev_ops,
293 .config_attr_mask = (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR |
294 1 << VDPA_ATTR_DEV_NET_CFG_MTU),
295 .max_supported_vqs = VDPASIM_NET_VQ_NUM,
296 .supported_features = VDPASIM_NET_FEATURES,
299 static int __init vdpasim_net_init(void)
303 ret = device_register(&vdpasim_net_mgmtdev);
307 ret = vdpa_mgmtdev_register(&mgmt_dev);
313 device_unregister(&vdpasim_net_mgmtdev);
317 static void __exit vdpasim_net_exit(void)
319 vdpa_mgmtdev_unregister(&mgmt_dev);
320 device_unregister(&vdpasim_net_mgmtdev);
323 module_init(vdpasim_net_init);
324 module_exit(vdpasim_net_exit);
326 MODULE_VERSION(DRV_VERSION);
327 MODULE_LICENSE(DRV_LICENSE);
328 MODULE_AUTHOR(DRV_AUTHOR);
329 MODULE_DESCRIPTION(DRV_DESC);