virtio_ring_0_9.c 7 KB
Newer Older
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
/*
 * Copyright (C) 2016 Red Hat, Inc.
 * Author: Michael S. Tsirkin <mst@redhat.com>
 * This work is licensed under the terms of the GNU GPL, version 2.
 *
 * Partial implementation of virtio 0.9. event index is used for signalling,
 * unconditionally. Design roughly follows linux kernel implementation in order
 * to be able to judge its performance.
 */
#define _GNU_SOURCE
#include "main.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <linux/virtio_ring.h>

struct data {
	void *data;
} *data;

struct vring ring;

/* enabling the below activates experimental ring polling code
 * (which skips index reads on consumer in favor of looking at
 * high bits of ring id ^ 0x8000).
 */
/* #ifdef RING_POLL */
29 30 31 32 33 34 35 36
/* enabling the below activates experimental in-order code
 * (which skips ring updates and reads and writes len in descriptor).
 */
/* #ifdef INORDER */

#if defined(RING_POLL) && defined(INORDER)
#error "RING_POLL and INORDER are mutually exclusive"
#endif
37 38 39 40 41 42 43 44 45

/* how much padding is needed to avoid false cache sharing */
#define HOST_GUEST_PADDING 0x80

struct guest {
	unsigned short avail_idx;
	unsigned short last_used_idx;
	unsigned short num_free;
	unsigned short kicked_avail_idx;
46
#ifndef INORDER
47
	unsigned short free_head;
48 49 50
#else
	unsigned short reserved_free_head;
#endif
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
	unsigned char reserved[HOST_GUEST_PADDING - 10];
} guest;

struct host {
	/* we do not need to track last avail index
	 * unless we have more than one in flight.
	 */
	unsigned short used_idx;
	unsigned short called_used_idx;
	unsigned char reserved[HOST_GUEST_PADDING - 4];
} host;

/* implemented by ring */
void alloc_ring(void)
{
	int ret;
	int i;
	void *p;

	ret = posix_memalign(&p, 0x1000, vring_size(ring_size, 0x1000));
	if (ret) {
		perror("Unable to allocate ring buffer.\n");
		exit(3);
	}
	memset(p, 0, vring_size(ring_size, 0x1000));
	vring_init(&ring, ring_size, p, 0x1000);

	guest.avail_idx = 0;
	guest.kicked_avail_idx = -1;
	guest.last_used_idx = 0;
81
#ifndef INORDER
82 83
	/* Put everything in free lists. */
	guest.free_head = 0;
84
#endif
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	for (i = 0; i < ring_size - 1; i++)
		ring.desc[i].next = i + 1;
	host.used_idx = 0;
	host.called_used_idx = -1;
	guest.num_free = ring_size;
	data = malloc(ring_size * sizeof *data);
	if (!data) {
		perror("Unable to allocate data buffer.\n");
		exit(3);
	}
	memset(data, 0, ring_size * sizeof *data);
}

/* guest side */
int add_inbuf(unsigned len, void *buf, void *datap)
{
101 102 103 104
	unsigned head;
#ifndef INORDER
	unsigned avail;
#endif
105 106 107 108 109
	struct vring_desc *desc;

	if (!guest.num_free)
		return -1;

110 111 112
#ifdef INORDER
	head = (ring_size - 1) & (guest.avail_idx++);
#else
113
	head = guest.free_head;
114
#endif
115 116 117 118 119 120 121 122 123 124 125
	guest.num_free--;

	desc = ring.desc;
	desc[head].flags = VRING_DESC_F_NEXT;
	desc[head].addr = (unsigned long)(void *)buf;
	desc[head].len = len;
	/* We do it like this to simulate the way
	 * we'd have to flip it if we had multiple
	 * descriptors.
	 */
	desc[head].flags &= ~VRING_DESC_F_NEXT;
126
#ifndef INORDER
127
	guest.free_head = desc[head].next;
128
#endif
129 130 131 132 133 134 135 136 137 138

	data[head].data = datap;

#ifdef RING_POLL
	/* Barrier A (for pairing) */
	smp_release();
	avail = guest.avail_idx++;
	ring.avail->ring[avail & (ring_size - 1)] =
		(head | (avail & ~(ring_size - 1))) ^ 0x8000;
#else
139 140 141
#ifndef INORDER
	/* Barrier A (for pairing) */
	smp_release();
142 143
	avail = (ring_size - 1) & (guest.avail_idx++);
	ring.avail->ring[avail] = head;
144
#endif
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
	/* Barrier A (for pairing) */
	smp_release();
#endif
	ring.avail->idx = guest.avail_idx;
	return 0;
}

void *get_buf(unsigned *lenp, void **bufp)
{
	unsigned head;
	unsigned index;
	void *datap;

#ifdef RING_POLL
	head = (ring_size - 1) & guest.last_used_idx;
	index = ring.used->ring[head].id;
	if ((index ^ guest.last_used_idx ^ 0x8000) & ~(ring_size - 1))
		return NULL;
	/* Barrier B (for pairing) */
	smp_acquire();
	index &= ring_size - 1;
#else
	if (ring.used->idx == guest.last_used_idx)
		return NULL;
	/* Barrier B (for pairing) */
	smp_acquire();
171 172 173 174
#ifdef INORDER
	head = (ring_size - 1) & guest.last_used_idx;
	index = head;
#else
175 176 177
	head = (ring_size - 1) & guest.last_used_idx;
	index = ring.used->ring[head].id;
#endif
178 179 180 181 182

#endif
#ifdef INORDER
	*lenp = ring.desc[index].len;
#else
183
	*lenp = ring.used->ring[head].len;
184
#endif
185 186 187
	datap = data[index].data;
	*bufp = (void*)(unsigned long)ring.desc[index].addr;
	data[index].data = NULL;
188
#ifndef INORDER
189 190
	ring.desc[index].next = guest.free_head;
	guest.free_head = index;
191
#endif
192 193 194 195 196
	guest.num_free++;
	guest.last_used_idx++;
	return datap;
}

197
bool used_empty()
198
{
199
	unsigned short last_used_idx = guest.last_used_idx;
200
#ifdef RING_POLL
201 202
	unsigned short head = last_used_idx & (ring_size - 1);
	unsigned index = ring.used->ring[head].id;
203

204
	return (index ^ last_used_idx ^ 0x8000) & ~(ring_size - 1);
205
#else
206
	return ring.used->idx == last_used_idx;
207 208 209 210 211 212 213 214 215 216 217 218
#endif
}

void disable_call()
{
	/* Doing nothing to disable calls might cause
	 * extra interrupts, but reduces the number of cache misses.
	 */
}

bool enable_call()
{
219
	vring_used_event(&ring) = guest.last_used_idx;
220 221 222
	/* Flush call index write */
	/* Barrier D (for pairing) */
	smp_mb();
223
	return used_empty();
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
}

void kick_available(void)
{
	/* Flush in previous flags write */
	/* Barrier C (for pairing) */
	smp_mb();
	if (!vring_need_event(vring_avail_event(&ring),
			      guest.avail_idx,
			      guest.kicked_avail_idx))
		return;

	guest.kicked_avail_idx = guest.avail_idx;
	kick();
}

/* host side */
void disable_kick()
{
	/* Doing nothing to disable kicks might cause
	 * extra interrupts, but reduces the number of cache misses.
	 */
}

bool enable_kick()
{
250
	vring_avail_event(&ring) = host.used_idx;
251 252
	/* Barrier C (for pairing) */
	smp_mb();
253
	return avail_empty();
254 255
}

256
bool avail_empty()
257 258 259
{
	unsigned head = host.used_idx;
#ifdef RING_POLL
260 261 262
	unsigned index = ring.avail->ring[head & (ring_size - 1)];

	return ((index ^ head ^ 0x8000) & ~(ring_size - 1));
263
#else
264
	return head == ring.avail->idx;
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
#endif
}

bool use_buf(unsigned *lenp, void **bufp)
{
	unsigned used_idx = host.used_idx;
	struct vring_desc *desc;
	unsigned head;

#ifdef RING_POLL
	head = ring.avail->ring[used_idx & (ring_size - 1)];
	if ((used_idx ^ head ^ 0x8000) & ~(ring_size - 1))
		return false;
	/* Barrier A (for pairing) */
	smp_acquire();

	used_idx &= ring_size - 1;
	desc = &ring.desc[head & (ring_size - 1)];
#else
	if (used_idx == ring.avail->idx)
		return false;

	/* Barrier A (for pairing) */
	smp_acquire();

	used_idx &= ring_size - 1;
291 292 293
#ifdef INORDER
	head = used_idx;
#else
294
	head = ring.avail->ring[used_idx];
295
#endif
296 297 298 299 300 301
	desc = &ring.desc[head];
#endif

	*lenp = desc->len;
	*bufp = (void *)(unsigned long)desc->addr;

302 303 304
#ifdef INORDER
	desc->len = desc->len - 1;
#else
305 306 307
	/* now update used ring */
	ring.used->ring[used_idx].id = head;
	ring.used->ring[used_idx].len = desc->len - 1;
308
#endif
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	/* Barrier B (for pairing) */
	smp_release();
	host.used_idx++;
	ring.used->idx = host.used_idx;
	
	return true;
}

void call_used(void)
{
	/* Flush in previous flags write */
	/* Barrier D (for pairing) */
	smp_mb();
	if (!vring_need_event(vring_used_event(&ring),
			      host.used_idx,
			      host.called_used_idx))
		return;

	host.called_used_idx = host.used_idx;
	call();
}