tools/virtio/vhost_net_test.c
Source file repositories/reference/linux-study-clean/tools/virtio/vhost_net_test.c
File Facts
- System
- Linux kernel
- Corpus path
tools/virtio/vhost_net_test.c- Extension
.c- Size
- 11699 bytes
- Lines
- 533
- 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.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- 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/vhost.hlinux/if.hlinux/if_tun.hlinux/in.hlinux/if_packet.hlinux/virtio_net.hnetinet/ether.h
Detected Declarations
struct vq_infostruct vdev_infofunction tun_allocfunction vdev_create_socketfunction vdev_send_packetfunction vq_notifyfunction vhost_vq_setupfunction vq_resetfunction vq_info_addfunction vdev_info_initfunction wait_for_interruptfunction verify_res_buffunction run_tx_testfunction run_rx_testfunction helpfunction main
Annotated Snippet
struct vq_info {
int kick;
int call;
int idx;
long started;
long completed;
struct pollfd fds;
void *ring;
/* copy used for control */
struct vring vring;
struct virtqueue *vq;
};
struct vdev_info {
struct virtio_device vdev;
int control;
struct vq_info vqs[2];
int nvqs;
void *buf;
size_t buf_size;
char *test_buf;
char *res_buf;
struct vhost_memory *mem;
int sock;
int ifindex;
unsigned char mac[ETHER_ADDR_LEN];
};
static int tun_alloc(struct vdev_info *dev, char *tun_name)
{
struct ifreq ifr;
int len = HDR_LEN;
int fd, e;
fd = open("/dev/net/tun", O_RDWR);
if (fd < 0) {
perror("Cannot open /dev/net/tun");
return fd;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
strncpy(ifr.ifr_name, tun_name, IFNAMSIZ);
e = ioctl(fd, TUNSETIFF, &ifr);
if (e < 0) {
perror("ioctl[TUNSETIFF]");
close(fd);
return e;
}
e = ioctl(fd, TUNSETVNETHDRSZ, &len);
if (e < 0) {
perror("ioctl[TUNSETVNETHDRSZ]");
close(fd);
return e;
}
e = ioctl(fd, SIOCGIFHWADDR, &ifr);
if (e < 0) {
perror("ioctl[SIOCGIFHWADDR]");
close(fd);
return e;
}
memcpy(dev->mac, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
return fd;
}
static void vdev_create_socket(struct vdev_info *dev, char *tun_name)
{
struct ifreq ifr;
dev->sock = socket(AF_PACKET, SOCK_RAW, htons(TEST_PTYPE));
assert(dev->sock != -1);
strncpy(ifr.ifr_name, tun_name, IFNAMSIZ);
assert(ioctl(dev->sock, SIOCGIFINDEX, &ifr) >= 0);
dev->ifindex = ifr.ifr_ifindex;
/* Set the flags that bring the device up */
assert(ioctl(dev->sock, SIOCGIFFLAGS, &ifr) >= 0);
ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
assert(ioctl(dev->sock, SIOCSIFFLAGS, &ifr) >= 0);
}
static void vdev_send_packet(struct vdev_info *dev)
{
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 tun_alloc`, `function vdev_create_socket`, `function vdev_send_packet`, `function vq_notify`, `function vhost_vq_setup`, `function vq_reset`, `function vq_info_add`, `function vdev_info_init`.
- 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.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
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.