tools/virtio/virtio_test.c
Source file repositories/reference/linux-study-clean/tools/virtio/virtio_test.c
File Facts
- System
- Linux kernel
- Corpus path
tools/virtio/virtio_test.c- Extension
.c- Size
- 9088 bytes
- Lines
- 404
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
getopt.hlimits.hstring.hpoll.hsys/eventfd.hstdlib.hassert.hunistd.hsys/ioctl.hsys/stat.hsys/types.hfcntl.hstdbool.hlinux/virtio_types.hlinux/vhost.hlinux/virtio.hlinux/virtio_ring.h../../drivers/vhost/test.h
Detected Declarations
struct vq_infostruct vdev_infofunction vq_notifyfunction vq_callbackfunction vq_resetfunction vq_info_addfunction vdev_info_initfunction wait_for_interruptfunction run_testfunction helpfunction main
Annotated Snippet
struct vq_info {
int kick;
int call;
int num;
int idx;
void *ring;
/* copy used for control */
struct vring vring;
struct virtqueue *vq;
};
struct vdev_info {
struct virtio_device vdev;
int control;
struct pollfd fds[1];
struct vq_info vqs[1];
int nvqs;
void *buf;
size_t buf_size;
struct vhost_memory *mem;
};
static const struct vhost_vring_file no_backend = { .fd = -1 },
backend = { .fd = 1 };
static const struct vhost_vring_state null_state = {};
bool vq_notify(struct virtqueue *vq)
{
struct vq_info *info = vq->priv;
unsigned long long v = 1;
int r;
r = write(info->kick, &v, sizeof v);
assert(r == sizeof v);
return true;
}
void vq_callback(struct virtqueue *vq)
{
}
void vhost_vq_setup(struct vdev_info *dev, struct vq_info *info)
{
struct vhost_vring_state state = { .index = info->idx };
struct vhost_vring_file file = { .index = info->idx };
unsigned long long features = dev->vdev.features;
struct vhost_vring_addr addr = {
.index = info->idx,
.desc_user_addr = (uint64_t)(unsigned long)info->vring.desc,
.avail_user_addr = (uint64_t)(unsigned long)info->vring.avail,
.used_user_addr = (uint64_t)(unsigned long)info->vring.used,
};
int r;
r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
assert(r >= 0);
state.num = info->vring.num;
r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
assert(r >= 0);
state.num = 0;
r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
assert(r >= 0);
r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
assert(r >= 0);
file.fd = info->kick;
r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
assert(r >= 0);
file.fd = info->call;
r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
assert(r >= 0);
}
static void vq_reset(struct vq_info *info, int num, struct virtio_device *vdev)
{
if (info->vq)
vring_del_virtqueue(info->vq);
memset(info->ring, 0, vring_size(num, 4096));
vring_init(&info->vring, num, info->ring, 4096);
info->vq = vring_new_virtqueue(info->idx, num, 4096, vdev, true, false,
info->ring, vq_notify, vq_callback, "test");
assert(info->vq);
info->vq->priv = info;
}
static void vq_info_add(struct vdev_info *dev, int num)
{
struct vq_info *info = &dev->vqs[dev->nvqs];
int r;
info->idx = dev->nvqs;
info->kick = eventfd(0, EFD_NONBLOCK);
Annotation
- Immediate include surface: `getopt.h`, `limits.h`, `string.h`, `poll.h`, `sys/eventfd.h`, `stdlib.h`, `assert.h`, `unistd.h`.
- Detected declarations: `struct vq_info`, `struct vdev_info`, `function vq_notify`, `function vq_callback`, `function vq_reset`, `function vq_info_add`, `function vdev_info_init`, `function wait_for_interrupt`, `function run_test`, `function help`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.