Linux Audio

Check our new training course

Loading...
v3.15
  1#define _GNU_SOURCE
  2#include <getopt.h>
  3#include <string.h>
  4#include <poll.h>
  5#include <sys/eventfd.h>
  6#include <stdlib.h>
  7#include <assert.h>
  8#include <unistd.h>
  9#include <sys/ioctl.h>
 10#include <sys/stat.h>
 11#include <sys/types.h>
 12#include <fcntl.h>
 13#include <stdbool.h>
 14#include <linux/vhost.h>
 15#include <linux/virtio.h>
 16#include <linux/virtio_ring.h>
 17#include "../../drivers/vhost/test.h"
 18
 19/* Unused */
 20void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
 21
 22struct vq_info {
 23	int kick;
 24	int call;
 25	int num;
 26	int idx;
 27	void *ring;
 28	/* copy used for control */
 29	struct vring vring;
 30	struct virtqueue *vq;
 31};
 32
 33struct vdev_info {
 34	struct virtio_device vdev;
 35	int control;
 36	struct pollfd fds[1];
 37	struct vq_info vqs[1];
 38	int nvqs;
 39	void *buf;
 40	size_t buf_size;
 41	struct vhost_memory *mem;
 42};
 43
 44bool vq_notify(struct virtqueue *vq)
 45{
 46	struct vq_info *info = vq->priv;
 47	unsigned long long v = 1;
 48	int r;
 49	r = write(info->kick, &v, sizeof v);
 50	assert(r == sizeof v);
 51	return true;
 52}
 53
 54void vq_callback(struct virtqueue *vq)
 55{
 56}
 57
 58
 59void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
 60{
 61	struct vhost_vring_state state = { .index = info->idx };
 62	struct vhost_vring_file file = { .index = info->idx };
 63	unsigned long long features = dev->vdev.features[0];
 64	struct vhost_vring_addr addr = {
 65		.index = info->idx,
 66		.desc_user_addr = (uint64_t)(unsigned long)info->vring.desc,
 67		.avail_user_addr = (uint64_t)(unsigned long)info->vring.avail,
 68		.used_user_addr = (uint64_t)(unsigned long)info->vring.used,
 69	};
 70	int r;
 71	r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
 72	assert(r >= 0);
 73	state.num = info->vring.num;
 74	r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
 75	assert(r >= 0);
 76	state.num = 0;
 77	r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
 78	assert(r >= 0);
 79	r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
 80	assert(r >= 0);
 81	file.fd = info->kick;
 82	r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
 83	assert(r >= 0);
 84	file.fd = info->call;
 85	r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
 86	assert(r >= 0);
 87}
 88
 89static void vq_info_add(struct vdev_info *dev, int num)
 90{
 91	struct vq_info *info = &dev->vqs[dev->nvqs];
 92	int r;
 93	info->idx = dev->nvqs;
 94	info->kick = eventfd(0, EFD_NONBLOCK);
 95	info->call = eventfd(0, EFD_NONBLOCK);
 96	r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
 97	assert(r >= 0);
 98	memset(info->ring, 0, vring_size(num, 4096));
 99	vring_init(&info->vring, num, info->ring, 4096);
100	info->vq = vring_new_virtqueue(info->idx,
101				       info->vring.num, 4096, &dev->vdev,
102				       true, info->ring,
103				       vq_notify, vq_callback, "test");
104	assert(info->vq);
105	info->vq->priv = info;
106	vhost_vq_setup(dev, info);
107	dev->fds[info->idx].fd = info->call;
108	dev->fds[info->idx].events = POLLIN;
109	dev->nvqs++;
110}
111
112static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
113{
114	int r;
115	memset(dev, 0, sizeof *dev);
116	dev->vdev.features[0] = features;
117	dev->vdev.features[1] = features >> 32;
118	dev->buf_size = 1024;
119	dev->buf = malloc(dev->buf_size);
120	assert(dev->buf);
121        dev->control = open("/dev/vhost-test", O_RDWR);
122	assert(dev->control >= 0);
123	r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
124	assert(r >= 0);
125	dev->mem = malloc(offsetof(struct vhost_memory, regions) +
126			  sizeof dev->mem->regions[0]);
127	assert(dev->mem);
128	memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
129                          sizeof dev->mem->regions[0]);
130	dev->mem->nregions = 1;
131	dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
132	dev->mem->regions[0].userspace_addr = (long)dev->buf;
133	dev->mem->regions[0].memory_size = dev->buf_size;
134	r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
135	assert(r >= 0);
136}
137
138/* TODO: this is pretty bad: we get a cache line bounce
139 * for the wait queue on poll and another one on read,
140 * plus the read which is there just to clear the
141 * current state. */
142static void wait_for_interrupt(struct vdev_info *dev)
143{
144	int i;
145	unsigned long long val;
146	poll(dev->fds, dev->nvqs, -1);
147	for (i = 0; i < dev->nvqs; ++i)
148		if (dev->fds[i].revents & POLLIN) {
149			read(dev->fds[i].fd, &val, sizeof val);
150		}
151}
152
153static void run_test(struct vdev_info *dev, struct vq_info *vq,
154		     bool delayed, int bufs)
155{
156	struct scatterlist sl;
157	long started = 0, completed = 0;
158	long completed_before;
159	int r, test = 1;
160	unsigned len;
161	long long spurious = 0;
162	r = ioctl(dev->control, VHOST_TEST_RUN, &test);
163	assert(r >= 0);
164	for (;;) {
165		virtqueue_disable_cb(vq->vq);
166		completed_before = completed;
167		do {
168			if (started < bufs) {
169				sg_init_one(&sl, dev->buf, dev->buf_size);
170				r = virtqueue_add_outbuf(vq->vq, &sl, 1,
171							 dev->buf + started,
172							 GFP_ATOMIC);
173				if (likely(r == 0)) {
174					++started;
175					if (unlikely(!virtqueue_kick(vq->vq)))
176						r = -1;
177				}
178			} else
179				r = -1;
180
181			/* Flush out completed bufs if any */
182			if (virtqueue_get_buf(vq->vq, &len)) {
183				++completed;
184				r = 0;
185			}
186
187		} while (r == 0);
188		if (completed == completed_before)
189			++spurious;
190		assert(completed <= bufs);
191		assert(started <= bufs);
192		if (completed == bufs)
193			break;
194		if (delayed) {
195			if (virtqueue_enable_cb_delayed(vq->vq))
196				wait_for_interrupt(dev);
197		} else {
198			if (virtqueue_enable_cb(vq->vq))
199				wait_for_interrupt(dev);
200		}
201	}
202	test = 0;
203	r = ioctl(dev->control, VHOST_TEST_RUN, &test);
204	assert(r >= 0);
205	fprintf(stderr, "spurious wakeus: 0x%llx\n", spurious);
206}
207
208const char optstring[] = "h";
209const struct option longopts[] = {
210	{
211		.name = "help",
212		.val = 'h',
213	},
214	{
215		.name = "event-idx",
216		.val = 'E',
217	},
218	{
219		.name = "no-event-idx",
220		.val = 'e',
221	},
222	{
223		.name = "indirect",
224		.val = 'I',
225	},
226	{
227		.name = "no-indirect",
228		.val = 'i',
229	},
230	{
231		.name = "delayed-interrupt",
232		.val = 'D',
233	},
234	{
235		.name = "no-delayed-interrupt",
236		.val = 'd',
237	},
238	{
239	}
240};
241
242static void help(void)
243{
244	fprintf(stderr, "Usage: virtio_test [--help]"
245		" [--no-indirect]"
246		" [--no-event-idx]"
247		" [--delayed-interrupt]"
248		"\n");
249}
250
251int main(int argc, char **argv)
252{
253	struct vdev_info dev;
254	unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
255		(1ULL << VIRTIO_RING_F_EVENT_IDX);
256	int o;
257	bool delayed = false;
258
259	for (;;) {
260		o = getopt_long(argc, argv, optstring, longopts, NULL);
261		switch (o) {
262		case -1:
263			goto done;
264		case '?':
265			help();
266			exit(2);
267		case 'e':
268			features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
269			break;
270		case 'h':
271			help();
272			goto done;
273		case 'i':
274			features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
275			break;
276		case 'D':
277			delayed = true;
278			break;
279		default:
280			assert(0);
281			break;
282		}
283	}
284
285done:
286	vdev_info_init(&dev, features);
287	vq_info_add(&dev, 256);
288	run_test(&dev, &dev.vqs[0], delayed, 0x100000);
289	return 0;
290}
v3.1
  1#define _GNU_SOURCE
  2#include <getopt.h>
  3#include <string.h>
  4#include <poll.h>
  5#include <sys/eventfd.h>
  6#include <stdlib.h>
  7#include <assert.h>
  8#include <unistd.h>
  9#include <sys/ioctl.h>
 10#include <sys/stat.h>
 11#include <sys/types.h>
 12#include <fcntl.h>
 
 13#include <linux/vhost.h>
 14#include <linux/virtio.h>
 15#include <linux/virtio_ring.h>
 16#include "../../drivers/vhost/test.h"
 17
 
 
 
 18struct vq_info {
 19	int kick;
 20	int call;
 21	int num;
 22	int idx;
 23	void *ring;
 24	/* copy used for control */
 25	struct vring vring;
 26	struct virtqueue *vq;
 27};
 28
 29struct vdev_info {
 30	struct virtio_device vdev;
 31	int control;
 32	struct pollfd fds[1];
 33	struct vq_info vqs[1];
 34	int nvqs;
 35	void *buf;
 36	size_t buf_size;
 37	struct vhost_memory *mem;
 38};
 39
 40void vq_notify(struct virtqueue *vq)
 41{
 42	struct vq_info *info = vq->priv;
 43	unsigned long long v = 1;
 44	int r;
 45	r = write(info->kick, &v, sizeof v);
 46	assert(r == sizeof v);
 
 47}
 48
 49void vq_callback(struct virtqueue *vq)
 50{
 51}
 52
 53
 54void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
 55{
 56	struct vhost_vring_state state = { .index = info->idx };
 57	struct vhost_vring_file file = { .index = info->idx };
 58	unsigned long long features = dev->vdev.features[0];
 59	struct vhost_vring_addr addr = {
 60		.index = info->idx,
 61		.desc_user_addr = (uint64_t)(unsigned long)info->vring.desc,
 62		.avail_user_addr = (uint64_t)(unsigned long)info->vring.avail,
 63		.used_user_addr = (uint64_t)(unsigned long)info->vring.used,
 64	};
 65	int r;
 66	r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
 67	assert(r >= 0);
 68	state.num = info->vring.num;
 69	r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
 70	assert(r >= 0);
 71	state.num = 0;
 72	r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
 73	assert(r >= 0);
 74	r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
 75	assert(r >= 0);
 76	file.fd = info->kick;
 77	r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
 78	assert(r >= 0);
 79	file.fd = info->call;
 80	r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
 81	assert(r >= 0);
 82}
 83
 84static void vq_info_add(struct vdev_info *dev, int num)
 85{
 86	struct vq_info *info = &dev->vqs[dev->nvqs];
 87	int r;
 88	info->idx = dev->nvqs;
 89	info->kick = eventfd(0, EFD_NONBLOCK);
 90	info->call = eventfd(0, EFD_NONBLOCK);
 91	r = posix_memalign(&info->ring, 4096, vring_size(num, 4096));
 92	assert(r >= 0);
 93	memset(info->ring, 0, vring_size(num, 4096));
 94	vring_init(&info->vring, num, info->ring, 4096);
 95	info->vq = vring_new_virtqueue(info->vring.num, 4096, &dev->vdev, info->ring,
 
 
 96				       vq_notify, vq_callback, "test");
 97	assert(info->vq);
 98	info->vq->priv = info;
 99	vhost_vq_setup(dev, info);
100	dev->fds[info->idx].fd = info->call;
101	dev->fds[info->idx].events = POLLIN;
102	dev->nvqs++;
103}
104
105static void vdev_info_init(struct vdev_info* dev, unsigned long long features)
106{
107	int r;
108	memset(dev, 0, sizeof *dev);
109	dev->vdev.features[0] = features;
110	dev->vdev.features[1] = features >> 32;
111	dev->buf_size = 1024;
112	dev->buf = malloc(dev->buf_size);
113	assert(dev->buf);
114        dev->control = open("/dev/vhost-test", O_RDWR);
115	assert(dev->control >= 0);
116	r = ioctl(dev->control, VHOST_SET_OWNER, NULL);
117	assert(r >= 0);
118	dev->mem = malloc(offsetof(struct vhost_memory, regions) +
119			  sizeof dev->mem->regions[0]);
120	assert(dev->mem);
121	memset(dev->mem, 0, offsetof(struct vhost_memory, regions) +
122                          sizeof dev->mem->regions[0]);
123	dev->mem->nregions = 1;
124	dev->mem->regions[0].guest_phys_addr = (long)dev->buf;
125	dev->mem->regions[0].userspace_addr = (long)dev->buf;
126	dev->mem->regions[0].memory_size = dev->buf_size;
127	r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
128	assert(r >= 0);
129}
130
131/* TODO: this is pretty bad: we get a cache line bounce
132 * for the wait queue on poll and another one on read,
133 * plus the read which is there just to clear the
134 * current state. */
135static void wait_for_interrupt(struct vdev_info *dev)
136{
137	int i;
138	unsigned long long val;
139	poll(dev->fds, dev->nvqs, -1);
140	for (i = 0; i < dev->nvqs; ++i)
141		if (dev->fds[i].revents & POLLIN) {
142			read(dev->fds[i].fd, &val, sizeof val);
143		}
144}
145
146static void run_test(struct vdev_info *dev, struct vq_info *vq, int bufs)
 
147{
148	struct scatterlist sl;
149	long started = 0, completed = 0;
150	long completed_before;
151	int r, test = 1;
152	unsigned len;
153	long long spurious = 0;
154	r = ioctl(dev->control, VHOST_TEST_RUN, &test);
155	assert(r >= 0);
156	for (;;) {
157		virtqueue_disable_cb(vq->vq);
158		completed_before = completed;
159		do {
160			if (started < bufs) {
161				sg_init_one(&sl, dev->buf, dev->buf_size);
162				r = virtqueue_add_buf(vq->vq, &sl, 1, 0,
163						      dev->buf + started);
164				if (likely(r >= 0)) {
 
165					++started;
166					virtqueue_kick(vq->vq);
 
167				}
168			} else
169				r = -1;
170
171			/* Flush out completed bufs if any */
172			if (virtqueue_get_buf(vq->vq, &len)) {
173				++completed;
174				r = 0;
175			}
176
177		} while (r >= 0);
178		if (completed == completed_before)
179			++spurious;
180		assert(completed <= bufs);
181		assert(started <= bufs);
182		if (completed == bufs)
183			break;
184		if (virtqueue_enable_cb(vq->vq)) {
185			wait_for_interrupt(dev);
 
 
 
 
186		}
187	}
188	test = 0;
189	r = ioctl(dev->control, VHOST_TEST_RUN, &test);
190	assert(r >= 0);
191	fprintf(stderr, "spurious wakeus: 0x%llx\n", spurious);
192}
193
194const char optstring[] = "h";
195const struct option longopts[] = {
196	{
197		.name = "help",
198		.val = 'h',
199	},
200	{
201		.name = "event-idx",
202		.val = 'E',
203	},
204	{
205		.name = "no-event-idx",
206		.val = 'e',
207	},
208	{
209		.name = "indirect",
210		.val = 'I',
211	},
212	{
213		.name = "no-indirect",
214		.val = 'i',
215	},
216	{
 
 
 
 
 
 
 
 
217	}
218};
219
220static void help()
221{
222	fprintf(stderr, "Usage: virtio_test [--help]"
223		" [--no-indirect]"
224		" [--no-event-idx]"
 
225		"\n");
226}
227
228int main(int argc, char **argv)
229{
230	struct vdev_info dev;
231	unsigned long long features = (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
232		(1ULL << VIRTIO_RING_F_EVENT_IDX);
233	int o;
 
234
235	for (;;) {
236		o = getopt_long(argc, argv, optstring, longopts, NULL);
237		switch (o) {
238		case -1:
239			goto done;
240		case '?':
241			help();
242			exit(2);
243		case 'e':
244			features &= ~(1ULL << VIRTIO_RING_F_EVENT_IDX);
245			break;
246		case 'h':
247			help();
248			goto done;
249		case 'i':
250			features &= ~(1ULL << VIRTIO_RING_F_INDIRECT_DESC);
251			break;
 
 
 
252		default:
253			assert(0);
254			break;
255		}
256	}
257
258done:
259	vdev_info_init(&dev, features);
260	vq_info_add(&dev, 256);
261	run_test(&dev, &dev.vqs[0], 0x100000);
262	return 0;
263}