public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob 5d58e56204b3bb4e816c0c8d5571e64886a7e32a 10037 bytes (raw)

  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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
 
// SPDX-License-Identifier: GPL-2.0-or-later

/* some parts copied from QEMU subprojects/libvhost-user/libvhost-user.c */

#include <stddef.h>
#include <endian.h>
#include <string.h>
#include <errno.h>
#include <sys/eventfd.h>
#include <sys/socket.h>

#include "util.h"
#include "virtio.h"

#define VIRTQUEUE_MAX_SIZE 1024

/* Translate guest physical address to our virtual address.  */
static void *vu_gpa_to_va(VuDev *dev, uint64_t *plen, uint64_t guest_addr)
{
	unsigned int i;

	if (*plen == 0)
		return NULL;

	/* Find matching memory region.  */
	for (i = 0; i < dev->nregions; i++) {
		const VuDevRegion *r = &dev->regions[i];

		if ((guest_addr >= r->gpa) && (guest_addr < (r->gpa + r->size))) {
			if ((guest_addr + *plen) > (r->gpa + r->size))
				*plen = r->gpa + r->size - guest_addr;
			/* NOLINTNEXTLINE(performance-no-int-to-ptr) */
			return (void *)(guest_addr - r->gpa + r->mmap_addr + r->mmap_offset);
		}
	}

	return NULL;
}

static inline uint16_t vring_avail_flags(const VuVirtq *vq)
{
	return le16toh(vq->vring.avail->flags);
}

static inline uint16_t vring_avail_idx(VuVirtq *vq)
{
	vq->shadow_avail_idx = le16toh(vq->vring.avail->idx);

	return vq->shadow_avail_idx;
}

static inline uint16_t vring_avail_ring(const VuVirtq *vq, int i)
{
	return le16toh(vq->vring.avail->ring[i]);
}

static inline uint16_t vring_get_used_event(const VuVirtq *vq)
{
	return vring_avail_ring(vq, vq->vring.num);
}

static bool virtqueue_get_head(VuDev *dev, const VuVirtq *vq,
			       unsigned int idx, unsigned int *head)
{
	/* Grab the next descriptor number they're advertising, and increment
	 * the index we've seen.
	 */
	*head = vring_avail_ring(vq, idx % vq->vring.num);

	/* If their number is silly, that's a fatal mistake. */
	if (*head >= vq->vring.num) {
		vu_panic(dev, "Guest says index %u is available", *head);
		return false;
	}

	return true;
}

static int
virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc,
			     uint64_t addr, size_t len)
{
	uint64_t read_len;

	if (len > (VIRTQUEUE_MAX_SIZE * sizeof(struct vring_desc)))
		return -1;

	if (len == 0)
		return -1;

	while (len) {
		const struct vring_desc *ori_desc;

		read_len = len;
		ori_desc = vu_gpa_to_va(dev, &read_len, addr);
		if (!ori_desc)
			return -1;

		memcpy(desc, ori_desc, read_len);
		len -= read_len;
		addr += read_len;
		desc += read_len;
	}

	return 0;
}

enum {
	VIRTQUEUE_READ_DESC_ERROR = -1,
	VIRTQUEUE_READ_DESC_DONE = 0,   /* end of chain */
	VIRTQUEUE_READ_DESC_MORE = 1,   /* more buffers in chain */
};

static int
virtqueue_read_next_desc(VuDev *dev, const struct vring_desc *desc,
			 int i, unsigned int max, unsigned int *next)
{
	/* If this descriptor says it doesn't chain, we're done. */
	if (!(le16toh(desc[i].flags) & VRING_DESC_F_NEXT))
		return VIRTQUEUE_READ_DESC_DONE;

	/* Check they're not leading us off end of descriptors. */
	*next = le16toh(desc[i].next);
	/* Make sure compiler knows to grab that: we don't want it changing! */
	smp_wmb();

	if (*next >= max) {
		vu_panic(dev, "Desc next is %u", *next);
		return VIRTQUEUE_READ_DESC_ERROR;
	}

	return VIRTQUEUE_READ_DESC_MORE;
}

bool vu_queue_empty(const VuDev *dev, VuVirtq *vq)
{
	if (dev->broken ||
		!vq->vring.avail) {
		return true;
	}

	if (vq->shadow_avail_idx != vq->last_avail_idx)
		return false;

	return vring_avail_idx(vq) == vq->last_avail_idx;
}

static bool vring_notify(const VuDev *dev, VuVirtq *vq)
{
	uint16_t old, new;
	bool v;

	/* We need to expose used array entries before checking used event. */
	smp_mb();

	/* Always notify when queue is empty (when feature acknowledge) */
	if (vu_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
		!vq->inuse && vu_queue_empty(dev, vq)) {
		return true;
	}

	if (!vu_has_feature(dev, VIRTIO_RING_F_EVENT_IDX))
		return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);

	v = vq->signalled_used_valid;
	vq->signalled_used_valid = true;
	old = vq->signalled_used;
	new = vq->signalled_used = vq->used_idx;
	return !v || vring_need_event(vring_get_used_event(vq), new, old);
}

void vu_queue_notify(VuDev *dev, VuVirtq *vq)
{
	if (dev->broken || !vq->vring.avail)
		return;

	if (!vring_notify(dev, vq)) {
		debug("skipped notify...");
		return;
	}

	if (eventfd_write(vq->call_fd, 1) < 0)
		vu_panic(dev, "Error writing eventfd: %s", strerror(errno));
}

static inline void vring_set_avail_event(VuVirtq *vq, uint16_t val)
{
	uint16_t val_le = htole16(val);

	if (!vq->notification)
		return;

	memcpy(&vq->vring.used->ring[vq->vring.num], &val_le, sizeof(uint16_t));
}

static bool virtqueue_map_desc(VuDev *dev,
			       unsigned int *p_num_sg, struct iovec *iov,
			       unsigned int max_num_sg,
			       uint64_t pa, size_t sz)
{
	unsigned int num_sg = *p_num_sg;

	ASSERT(num_sg <= max_num_sg);

	if (!sz) {
		vu_panic(dev, "virtio: zero sized buffers are not allowed");
		return false;
	}

	while (sz) {
		uint64_t len = sz;

		if (num_sg == max_num_sg) {
			vu_panic(dev, "virtio: too many descriptors in indirect table");
			return false;
		}

		iov[num_sg].iov_base = vu_gpa_to_va(dev, &len, pa);
		if (iov[num_sg].iov_base == NULL) {
			vu_panic(dev, "virtio: invalid address for buffers");
			return false;
		}
		iov[num_sg].iov_len = len;
		num_sg++;
		sz -= len;
		pa += len;
	}

	*p_num_sg = num_sg;
	return true;
}

static int
vu_queue_map_desc(VuDev *dev, VuVirtq *vq, unsigned int idx,
		  VuVirtqElement *elem)
{
	const struct vring_desc *desc = vq->vring.desc;
	struct vring_desc desc_buf[VIRTQUEUE_MAX_SIZE];
	unsigned int out_num = 0, in_num = 0;
	unsigned int max = vq->vring.num;
	unsigned int i = idx;
	uint64_t read_len;
	int rc;

	if (le16toh(desc[i].flags) & VRING_DESC_F_INDIRECT) {
		unsigned int desc_len;
		uint64_t desc_addr;

		if (le32toh(desc[i].len) % sizeof(struct vring_desc)) {
			vu_panic(dev, "Invalid size for indirect buffer table");
			return -1;
		}

		/* loop over the indirect descriptor table */
		desc_addr = le64toh(desc[i].addr);
		desc_len = le32toh(desc[i].len);
		max = desc_len / sizeof(struct vring_desc);
		read_len = desc_len;
		desc = vu_gpa_to_va(dev, &read_len, desc_addr);
		if (desc && read_len != desc_len) {
			/* Failed to use zero copy */
			desc = NULL;
			if (!virtqueue_read_indirect_desc(dev, desc_buf, desc_addr, desc_len))
				desc = desc_buf;
		}
		if (!desc) {
			vu_panic(dev, "Invalid indirect buffer table");
			return -1;
		}
		i = 0;
	}

	/* Collect all the descriptors */
	do {
		if (le16toh(desc[i].flags) & VRING_DESC_F_WRITE) {
			if (!virtqueue_map_desc(dev, &in_num, elem->in_sg,
						elem->in_num,
						le64toh(desc[i].addr),
						le32toh(desc[i].len))) {
				return -1;
			}
		} else {
			if (in_num) {
				vu_panic(dev, "Incorrect order for descriptors");
				return -1;
			}
			if (!virtqueue_map_desc(dev, &out_num, elem->out_sg,
						elem->out_num,
						le64toh(desc[i].addr),
						le32toh(desc[i].len))) {
				return -1;
			}
		}

		/* If we've got too many, that implies a descriptor loop. */
		if ((in_num + out_num) > max) {
			vu_panic(dev, "Looped descriptor");
			return -1;
		}
		rc = virtqueue_read_next_desc(dev, desc, i, max, &i);
	} while (rc == VIRTQUEUE_READ_DESC_MORE);

	if (rc == VIRTQUEUE_READ_DESC_ERROR) {
		vu_panic(dev, "read descriptor error");
		return -1;
	}

	elem->index = idx;
	elem->in_num = in_num;
	elem->out_num = out_num;

	return 0;
}

int vu_queue_pop(VuDev *dev, VuVirtq *vq, VuVirtqElement *elem)
{
	unsigned int head;
	int ret;

	if (dev->broken || !vq->vring.avail)
		return -1;

	if (vu_queue_empty(dev, vq))
		return -1;

	/*
	 * Needed after virtio_queue_empty(), see comment in
	 * virtqueue_num_heads().
	 */
	smp_rmb();

	if (vq->inuse >= vq->vring.num) {
		vu_panic(dev, "Virtqueue size exceeded");
		return -1;
	}

	if (!virtqueue_get_head(dev, vq, vq->last_avail_idx++, &head))
		return -1;

	if (vu_has_feature(dev, VIRTIO_RING_F_EVENT_IDX))
		vring_set_avail_event(vq, vq->last_avail_idx);

	ret = vu_queue_map_desc(dev, vq, head, elem);

	if (ret < 0)
		return ret;

	vq->inuse++;

	return 0;
}

void vu_queue_detach_element(VuDev *dev, VuVirtq *vq,
			     unsigned int index, size_t len)
{
	(void)dev;
	(void)index;
	(void)len;

	vq->inuse--;
	/* unmap, when DMA support is added */
}

void vu_queue_unpop(VuDev *dev, VuVirtq *vq, unsigned int index, size_t len)
{
	vq->last_avail_idx--;
	vu_queue_detach_element(dev, vq, index, len);
}

/* cppcheck-suppress unusedFunction */
bool vu_queue_rewind(VuDev *dev, VuVirtq *vq, unsigned int num)
{
	(void)dev;
	if (num > vq->inuse)
		return false;

	vq->last_avail_idx -= num;
	vq->inuse -= num;
	return true;
}

static inline void vring_used_write(VuVirtq *vq,
				    const struct vring_used_elem *uelem, int i)
{
	struct vring_used *used = vq->vring.used;

	used->ring[i] = *uelem;
}

void vu_queue_fill_by_index(const VuDev *dev, VuVirtq *vq, unsigned int index,
			    unsigned int len, unsigned int idx)
{
	struct vring_used_elem uelem;

	if (dev->broken || !vq->vring.avail)
		return;

	idx = (idx + vq->used_idx) % vq->vring.num;

	uelem.id = htole32(index);
	uelem.len = htole32(len);
	vring_used_write(vq, &uelem, idx);
}

void vu_queue_fill(const VuDev *dev, VuVirtq *vq, const VuVirtqElement *elem,
		   unsigned int len, unsigned int idx)
{
	vu_queue_fill_by_index(dev, vq, elem->index, len, idx);
}

static inline void vring_used_idx_set(VuVirtq *vq, uint16_t val)
{
	vq->vring.used->idx = htole16(val);

	vq->used_idx = val;
}

void vu_queue_flush(const VuDev *dev, VuVirtq *vq, unsigned int count)
{
	uint16_t old, new;

	if (dev->broken || !vq->vring.avail)
		return;

	/* Make sure buffer is written before we update index. */
	smp_wmb();

	old = vq->used_idx;
	new = old + count;
	vring_used_idx_set(vq, new);
	vq->inuse -= count;
	if ((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old))
		vq->signalled_used_valid = false;
}

/* cppcheck-suppress unusedFunction */
void vu_queue_push(const VuDev *dev, VuVirtq *vq,
		   const VuVirtqElement *elem, unsigned int len)
{
	vu_queue_fill(dev, vq, elem, len, 0);
	vu_queue_flush(dev, vq, 1);
}


debug log:

solving 5d58e56204b3 ...
found 5d58e56204b3 in https://archives.passt.top/passt-dev/20240621145640.1914287-4-lvivier@redhat.com/
found 50ec8b5119ed in https://archives.passt.top/passt-dev/20240621145640.1914287-3-lvivier@redhat.com/

applying [1/2] https://archives.passt.top/passt-dev/20240621145640.1914287-3-lvivier@redhat.com/
diff --git a/virtio.c b/virtio.c
new file mode 100644
index 000000000000..50ec8b5119ed


applying [2/2] https://archives.passt.top/passt-dev/20240621145640.1914287-4-lvivier@redhat.com/
diff --git a/virtio.c b/virtio.c
index 50ec8b5119ed..5d58e56204b3 100644

Checking patch virtio.c...
2:452: new blank line at EOF.
+
Applied patch virtio.c cleanly.
Checking patch virtio.c...
Applied patch virtio.c cleanly.
warning: 1 line adds whitespace errors.

index at:
100644 5d58e56204b3bb4e816c0c8d5571e64886a7e32a	virtio.c

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).