tools/virtio/ringtest/virtio_ring_0_9.c
Source file repositories/reference/linux-study-clean/tools/virtio/ringtest/virtio_ring_0_9.c
File Facts
- System
- Linux kernel
- Corpus path
tools/virtio/ringtest/virtio_ring_0_9.c- Extension
.c- Size
- 7155 bytes
- Lines
- 334
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
main.hstdlib.hstdio.hassert.hstring.hlinux/virtio_ring.h
Detected Declarations
struct datastruct gueststruct hostfunction alloc_ringfunction add_inbuffunction used_emptyfunction disable_callfunction kick_availablefunction disable_kickfunction avail_emptyfunction use_buffunction call_used
Annotated Snippet
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 */
/* 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
/* 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;
#ifndef INORDER
unsigned short free_head;
#else
unsigned short reserved_free_head;
#endif
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;
#ifndef INORDER
/* Put everything in free lists. */
guest.free_head = 0;
#endif
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)
{
unsigned head;
#ifndef INORDER
unsigned avail;
#endif
struct vring_desc *desc;
if (!guest.num_free)
Annotation
- Immediate include surface: `main.h`, `stdlib.h`, `stdio.h`, `assert.h`, `string.h`, `linux/virtio_ring.h`.
- Detected declarations: `struct data`, `struct guest`, `struct host`, `function alloc_ring`, `function add_inbuf`, `function used_empty`, `function disable_call`, `function kick_available`, `function disable_kick`, `function avail_empty`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.