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 				 (1ULL << VIRTIO_NET_F_MTU) | \
31 				 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
32 				 (1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR))
33 
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
38 
39 static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t len)
40 {
41 	/* Make sure data is wrote before advancing index */
42 	smp_wmb();
43 
44 	vringh_complete_iotlb(&vq->vring, vq->head, len);
45 
46 	/* Make sure used is visible before rasing the interrupt. */
47 	smp_wmb();
48 
49 	local_bh_disable();
50 	if (vringh_need_notify_iotlb(&vq->vring) > 0)
51 		vringh_notify(&vq->vring);
52 	local_bh_enable();
53 }
54 
55 static bool receive_filter(struct vdpasim *vdpasim, size_t len)
56 {
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;
61 
62 	if (len < ETH_ALEN + hdr_len)
63 		return false;
64 
65 	if (!strncmp(vdpasim->buffer + hdr_len, vio_config->mac, ETH_ALEN))
66 		return true;
67 
68 	return false;
69 }
70 
71 static virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
72 						   u8 cmd)
73 {
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;
77 	size_t read;
78 
79 	switch (cmd) {
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);
83 		if (read == ETH_ALEN)
84 			status = VIRTIO_NET_OK;
85 		break;
86 	default:
87 		break;
88 	}
89 
90 	return status;
91 }
92 
93 static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
94 {
95 	struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
96 	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
97 	struct virtio_net_ctrl_hdr ctrl;
98 	size_t read, write;
99 	int err;
100 
101 	if (!(vdpasim->features & (1ULL << VIRTIO_NET_F_CTRL_VQ)))
102 		return;
103 
104 	if (!cvq->ready)
105 		return;
106 
107 	while (true) {
108 		err = vringh_getdesc_iotlb(&cvq->vring, &cvq->in_iov,
109 					   &cvq->out_iov,
110 					   &cvq->head, GFP_ATOMIC);
111 		if (err <= 0)
112 			break;
113 
114 		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov, &ctrl,
115 					     sizeof(ctrl));
116 		if (read != sizeof(ctrl))
117 			break;
118 
119 		switch (ctrl.class) {
120 		case VIRTIO_NET_CTRL_MAC:
121 			status = vdpasim_handle_ctrl_mac(vdpasim, ctrl.cmd);
122 			break;
123 		default:
124 			break;
125 		}
126 
127 		/* Make sure data is wrote before advancing index */
128 		smp_wmb();
129 
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);
135 
136 		/* Make sure used is visible before rasing the interrupt. */
137 		smp_wmb();
138 
139 		local_bh_disable();
140 		if (cvq->cb)
141 			cvq->cb(cvq->private);
142 		local_bh_enable();
143 	}
144 }
145 
146 static void vdpasim_net_work(struct work_struct *work)
147 {
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];
151 	ssize_t read, write;
152 	int pkts = 0;
153 	int err;
154 
155 	spin_lock(&vdpasim->lock);
156 
157 	if (!vdpasim->running)
158 		goto out;
159 
160 	if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
161 		goto out;
162 
163 	vdpasim_handle_cvq(vdpasim);
164 
165 	if (!txq->ready || !rxq->ready)
166 		goto out;
167 
168 	while (true) {
169 		err = vringh_getdesc_iotlb(&txq->vring, &txq->out_iov, NULL,
170 					   &txq->head, GFP_ATOMIC);
171 		if (err <= 0)
172 			break;
173 
174 		read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
175 					     vdpasim->buffer,
176 					     PAGE_SIZE);
177 
178 		if (!receive_filter(vdpasim, read)) {
179 			vdpasim_net_complete(txq, 0);
180 			continue;
181 		}
182 
183 		err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->in_iov,
184 					   &rxq->head, GFP_ATOMIC);
185 		if (err <= 0) {
186 			vdpasim_net_complete(txq, 0);
187 			break;
188 		}
189 
190 		write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
191 					      vdpasim->buffer, read);
192 		if (write <= 0)
193 			break;
194 
195 		vdpasim_net_complete(txq, 0);
196 		vdpasim_net_complete(rxq, write);
197 
198 		if (++pkts > 4) {
199 			schedule_work(&vdpasim->work);
200 			goto out;
201 		}
202 	}
203 
204 out:
205 	spin_unlock(&vdpasim->lock);
206 }
207 
208 static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
209 {
210 	struct virtio_net_config *net_config = config;
211 
212 	net_config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
213 }
214 
215 static void vdpasim_net_setup_config(struct vdpasim *vdpasim,
216 				     const struct vdpa_dev_set_config *config)
217 {
218 	struct virtio_net_config *vio_config = vdpasim->config;
219 
220 	if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR))
221 		memcpy(vio_config->mac, config->net.mac, ETH_ALEN);
222 	if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MTU))
223 		vio_config->mtu = cpu_to_vdpasim16(vdpasim, config->net.mtu);
224 	else
225 		/* Setup default MTU to be 1500 */
226 		vio_config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
227 }
228 
229 static void vdpasim_net_mgmtdev_release(struct device *dev)
230 {
231 }
232 
233 static struct device vdpasim_net_mgmtdev = {
234 	.init_name = "vdpasim_net",
235 	.release = vdpasim_net_mgmtdev_release,
236 };
237 
238 static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
239 			       const struct vdpa_dev_set_config *config)
240 {
241 	struct vdpasim_dev_attr dev_attr = {};
242 	struct vdpasim *simdev;
243 	int ret;
244 
245 	dev_attr.mgmt_dev = mdev;
246 	dev_attr.name = name;
247 	dev_attr.id = VIRTIO_ID_NET;
248 	dev_attr.supported_features = VDPASIM_NET_FEATURES;
249 	dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
250 	dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
251 	dev_attr.nas = VDPASIM_NET_AS_NUM;
252 	dev_attr.config_size = sizeof(struct virtio_net_config);
253 	dev_attr.get_config = vdpasim_net_get_config;
254 	dev_attr.work_fn = vdpasim_net_work;
255 	dev_attr.buffer_size = PAGE_SIZE;
256 
257 	simdev = vdpasim_create(&dev_attr, config);
258 	if (IS_ERR(simdev))
259 		return PTR_ERR(simdev);
260 
261 	vdpasim_net_setup_config(simdev, config);
262 
263 	ret = _vdpa_register_device(&simdev->vdpa, VDPASIM_NET_VQ_NUM);
264 	if (ret)
265 		goto reg_err;
266 
267 	return 0;
268 
269 reg_err:
270 	put_device(&simdev->vdpa.dev);
271 	return ret;
272 }
273 
274 static void vdpasim_net_dev_del(struct vdpa_mgmt_dev *mdev,
275 				struct vdpa_device *dev)
276 {
277 	struct vdpasim *simdev = container_of(dev, struct vdpasim, vdpa);
278 
279 	_vdpa_unregister_device(&simdev->vdpa);
280 }
281 
282 static const struct vdpa_mgmtdev_ops vdpasim_net_mgmtdev_ops = {
283 	.dev_add = vdpasim_net_dev_add,
284 	.dev_del = vdpasim_net_dev_del
285 };
286 
287 static struct virtio_device_id id_table[] = {
288 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
289 	{ 0 },
290 };
291 
292 static struct vdpa_mgmt_dev mgmt_dev = {
293 	.device = &vdpasim_net_mgmtdev,
294 	.id_table = id_table,
295 	.ops = &vdpasim_net_mgmtdev_ops,
296 	.config_attr_mask = (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR |
297 			     1 << VDPA_ATTR_DEV_NET_CFG_MTU |
298 		             1 << VDPA_ATTR_DEV_FEATURES),
299 	.max_supported_vqs = VDPASIM_NET_VQ_NUM,
300 	.supported_features = VDPASIM_NET_FEATURES,
301 };
302 
303 static int __init vdpasim_net_init(void)
304 {
305 	int ret;
306 
307 	ret = device_register(&vdpasim_net_mgmtdev);
308 	if (ret)
309 		return ret;
310 
311 	ret = vdpa_mgmtdev_register(&mgmt_dev);
312 	if (ret)
313 		goto parent_err;
314 	return 0;
315 
316 parent_err:
317 	device_unregister(&vdpasim_net_mgmtdev);
318 	return ret;
319 }
320 
321 static void __exit vdpasim_net_exit(void)
322 {
323 	vdpa_mgmtdev_unregister(&mgmt_dev);
324 	device_unregister(&vdpasim_net_mgmtdev);
325 }
326 
327 module_init(vdpasim_net_init);
328 module_exit(vdpasim_net_exit);
329 
330 MODULE_VERSION(DRV_VERSION);
331 MODULE_LICENSE(DRV_LICENSE);
332 MODULE_AUTHOR(DRV_AUTHOR);
333 MODULE_DESCRIPTION(DRV_DESC);
334