aboutsummaryrefslogtreecommitdiff
path: root/vfio-netem.c
blob: 324c80cd32f71a39bd5cd6a143492fb0954524d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * vfio-netmap - the implementation of netmap mediated pass-through framework
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 */

#include <linux/init.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/mm.h>
#include <linux/mmu_context.h>
#include <linux/types.h>
#include <linux/list.h>
#include <linux/rbtree.h>
#include <linux/spinlock.h>
#include <linux/eventfd.h>
#include <linux/uuid.h>
#include <linux/kvm_host.h>
#include <linux/vfio.h>
#include <linux/mdev.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include "vfio-netem.h"

static char *intfs[8];
static int intf_cnt;
module_param_array(intfs, charp, &intf_cnt, 0);

static ssize_t available_instances_show(struct kobject *kobj,
					struct device *dev, char *buf)
{
	/* XXX FIXME */
	return sprintf(buf, "%u\n", 1);
}

static ssize_t device_api_show(struct kobject *kobj,
					struct device *dev, char *buf)
{
	return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
}

static ssize_t description_show(struct kobject *kobj,
					struct device *dev, char *buf)
{
	return sprintf(buf, "\n");
}

static MDEV_TYPE_ATTR_RO(available_instances);
static MDEV_TYPE_ATTR_RO(device_api);
static MDEV_TYPE_ATTR_RO(description);

static struct attribute *vfio_supported_type_attrs[] = {
	&mdev_type_attr_available_instances.attr,
	&mdev_type_attr_device_api.attr,
	&mdev_type_attr_description.attr,
	NULL,
};

static struct attribute_group vfio_supported_net_group = {
	.name = "vfio-mdev-supported",
	.attrs = vfio_supported_type_attrs,
};

/* only 1 for now */
static struct attribute_group *vfio_supported_type_groups[] = {
        &vfio_supported_net_group,
	NULL,
};

static ssize_t veth_id_show(struct device *dev, struct device_attribute *attr,
			    char *buf)
{
	return sprintf(buf, "\n");
}

static ssize_t veth_id_store(struct device *dev, struct device_attribute *attr,
			     const char *buf, size_t count)
{
	return count;
}


static ssize_t hw_id_show(struct device *dev, struct device_attribute *attr,
			  char *buf)
{
	return sprintf(buf, "\n");
}

static DEVICE_ATTR_RW(veth_id);
static DEVICE_ATTR_RO(hw_id);

static struct attribute *vfio_net_attrs[] = {
	&dev_attr_veth_id.attr,
	&dev_attr_hw_id.attr,
	NULL,
};

static struct attribute_group vfio_net_attr_group = {
	.name = "vfio-mdev-attr",
	.attrs = vfio_net_attrs,
};

/* Only 1 supported for now */
static const struct attribute_group *vfio_attr_groups[] = {
	&vfio_net_attr_group,
	NULL,
};

static int vfio_net_create(struct kobject *kobj, struct mdev_device *mdev)
{
	struct device *dev = mdev_parent_dev(mdev);

	dev_info(dev, "MDEV-NETEM: Create on parent\n");

	return 0;
}

static int vfio_net_remove(struct mdev_device *mdev)
{
	struct device *dev = mdev_parent_dev(mdev);

	dev_info(dev, "MDEV-NETEM: Destroy from parent\n");

	return 0;
}

static int vfio_net_open(struct mdev_device *mdev)
{
	return 0;
}

static void vfio_net_release(struct mdev_device *mdev)
{
	return;
}

static int vfio_net_mmap(struct mdev_device *mdev,
			    struct vm_area_struct *vma)
{
	struct device *dev = mdev_parent_dev(mdev);
	struct pci_dev *pdev;
	unsigned int index;
	u64 phys_len, req_len, pgoff, req_start;

	pdev = to_pci_dev(dev->parent);
	index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);

	phys_len = PAGE_ALIGN(pci_resource_len(pdev, index));
	req_len = vma->vm_end - vma->vm_start;
	pgoff = vma->vm_pgoff &
		((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
	req_start = pgoff << PAGE_SHIFT;

	if (req_start + req_len > phys_len)
		return -EINVAL;

	vma->vm_private_data = NULL;
	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
	vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;

	return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
			       req_len, vma->vm_page_prot);
}

static long vfio_net_ioctl(struct mdev_device *mdev, unsigned int cmd,
			    unsigned long arg)
{
	unsigned long minsz;
	struct device *dev = mdev_parent_dev(mdev);
	struct vfio_device_info info;
	struct pci_dev *pdev;
	struct pci_bus *bus;

	if (!mdev)
		return -EINVAL;

	/* We verified this exists on register */
	pdev = to_pci_dev(dev->parent);
	bus = pdev->bus;

	if (cmd == VFIO_DEVICE_GET_INFO) {
		minsz = offsetofend(struct vfio_device_info, num_irqs);

		if (copy_from_user(&info, (void __user *)arg, minsz))
			return -EFAULT;

		if (info.argsz < minsz)
			return -EINVAL;

		info.flags = VFIO_DEVICE_FLAGS_PCI;
		info.flags |= VFIO_DEVICE_FLAGS_RESET;
		info.num_regions = VFIO_PCI_NUM_REGIONS;
		info.num_irqs = 1;

		return copy_to_user((void __user *)arg, &info, minsz) ?
				    -EFAULT : 0;
	} else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
		struct vfio_region_info info;

		minsz = offsetofend(struct vfio_region_info, offset);

		if (copy_from_user(&info, (void __user *)arg, minsz))
			return -EFAULT;

		if (info.argsz < minsz)
			return -EINVAL;

		switch (info.index) {
		case VFIO_PCI_CONFIG_REGION_INDEX:
			info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
			info.size = pdev->cfg_size;
			info.flags = VFIO_REGION_INFO_FLAG_READ |
				     VFIO_REGION_INFO_FLAG_WRITE;
			break;
		case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
			info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
			info.size = pci_resource_len(pdev, info.index);
			if (!info.size) {
				info.flags = 0;
				break;
			}

			info.flags = VFIO_REGION_INFO_FLAG_READ |
				     VFIO_REGION_INFO_FLAG_WRITE;
			/* FIXME not all BARS are not mappable */
			info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
#if 0
			if (vdev->bar_mmap_supported[info.index]) {
				info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
				if (info.index == vdev->msix_bar) {
					ret = msix_sparse_mmap_cap(vdev, &caps);
					if (ret)
						return ret;
				}
			}
#endif
			break;
		default:
			return -EINVAL;
		}

		return copy_to_user((void __user *)arg, &info, minsz) ?
				    -EFAULT : 0;

	} else if (cmd == VFIO_DEVICE_RESET) {
		/*
		 * FIXME check if hardware supports this before blindly
		 * resetting
		 */
		return pci_try_reset_function(pdev);
	}


	return 0;
}

static const struct mdev_parent_ops vfio_net_ops = {
	.supported_type_groups	= vfio_supported_type_groups,
	.mdev_attr_groups       = vfio_attr_groups,
	.create			= vfio_net_create,
	.remove			= vfio_net_remove,

	.open			= vfio_net_open,
	.release		= vfio_net_release,

	.read			= NULL,
	.write			= NULL,
	.mmap			= vfio_net_mmap,
	.ioctl			= vfio_net_ioctl,
};

static inline bool vfio_pci_is_net(struct pci_dev *pdev)
{
	return (pdev->class >> 8) == PCI_CLASS_NETWORK_ETHERNET;
}

int vfio_vring_init(struct device *dev)
{
	int err = 0;
	struct pci_dev *pdev = NULL;

	if (dev->parent && dev_is_pci(dev->parent)) {
		pdev = to_pci_dev(dev->parent);
		if (!vfio_pci_is_net(pdev))
			goto out_inv_dev;
	} else {
		goto out_inv_dev;
	}

	err = mdev_register_device(dev, &vfio_net_ops);
	if (!err)
		dev_info(dev, "MDEV-NETEM: Register for driver %s",
			 pdev->driver->name);

	return err;

out_inv_dev:
	dev_err(dev, "MDEV-NETEM: Only supports network pci devices\n");
	return -EINVAL;
}

void vfio_vring_destroy(struct device *dev)
{
	dev_info(dev, "MDEV-NETEM: Unregistering\n");
	mdev_unregister_device(dev);
}

static int __init vfio_net_init(void)
{
	struct net_device *ndev;
	int i;

	for (i = 0; i < intf_cnt; i++) {
		ndev = dev_get_by_name(&init_net, intfs[i]);
		if (!ndev) {
			printk("Can't find interace %s\n", intfs[i]);
			continue;
		}
		vfio_vring_init(&ndev->dev);
	}
	return 0;
}

static void __exit vfio_net_exit(void)
{
	struct net_device *ndev;
	int i;

	for (i = 0; i < intf_cnt; i++) {
		ndev = dev_get_by_name(&init_net, intfs[i]);
		if (!ndev) {
			printk("Can't find interace %s\n", intfs[i]);
			continue;
		}
		vfio_vring_destroy(&ndev->dev);
	}
	return;
}

module_init(vfio_net_init);
module_exit(vfio_net_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Ilias Apalodimas <ilias.apalodimas@linaro.org>");