drivers/net/virtio_net.c
Source file repositories/reference/linux-study-clean/drivers/net/virtio_net.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/net/virtio_net.c- Extension
.c- Size
- 194142 bytes
- Lines
- 7302
- Domain
- Driver Families
- Bucket
- drivers/net
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- 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
linux/netdevice.hlinux/etherdevice.hlinux/ethtool.hlinux/module.hlinux/virtio.hlinux/virtio_net.hlinux/bpf.hlinux/bpf_trace.hlinux/scatterlist.hlinux/if_vlan.hlinux/slab.hlinux/cpu.hlinux/average.hlinux/filter.hlinux/kernel.hlinux/dim.hnet/route.hnet/xdp.hnet/net_failover.hnet/netdev_rx_queue.hnet/netdev_queues.hnet/xdp_sock_drv.hnet/page_pool/helpers.h
Detected Declarations
struct virtnet_stat_descstruct virtnet_sq_free_statsstruct virtnet_sq_statsstruct virtnet_rq_statsstruct virtnet_interrupt_coalescestruct send_queuestruct receive_queuestruct control_bufstruct virtnet_infostruct padded_vnet_hdrstruct virtio_net_common_hdrstruct virtnet_stats_ctxenum virtnet_xmit_typefunction virtio_is_mapped_offloadfunction virtio_offload_to_featurefunction virtnet_rss_hdr_sizefunction virtnet_rss_trailer_sizefunction virtnet_xmit_ptr_unpackfunction virtnet_add_outbuffunction virtnet_ptr_to_xsk_buff_lenfunction sg_fill_dmafunction __free_old_xmitfunction virtnet_free_old_xmitfunction vq2txqfunction txq2vqfunction vq2rxqfunction rxq2vqfunction vq_typefunction skb_vnet_common_hdrfunction give_pagesfunction virtnet_rq_free_buffunction enable_rx_mode_workfunction disable_rx_mode_workfunction virtqueue_napi_schedulefunction virtqueue_napi_completefunction virtnet_tx_wake_queuefunction skb_xmit_donefunction mergeable_ctx_to_headroomfunction mergeable_ctx_to_truesizefunction check_mergeable_lenfunction virtnet_rq_unmap_free_buffunction free_old_xmitfunction is_xdp_raw_buffer_queuefunction tx_may_stopfunction check_sq_full_and_disablefunction xsk_drop_follow_bufsfunction xsk_append_merge_bufferfunction virtnet_receive_xsk_buf
Annotated Snippet
static const struct net_device_ops virtnet_netdev = {
.ndo_open = virtnet_open,
.ndo_stop = virtnet_close,
.ndo_start_xmit = start_xmit,
.ndo_validate_addr = eth_validate_addr,
.ndo_set_mac_address = virtnet_set_mac_address,
.ndo_set_rx_mode = virtnet_set_rx_mode,
.ndo_get_stats64 = virtnet_stats,
.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
.ndo_bpf = virtnet_xdp,
.ndo_xdp_xmit = virtnet_xdp_xmit,
.ndo_xsk_wakeup = virtnet_xsk_wakeup,
.ndo_features_check = virtnet_features_check,
.ndo_get_phys_port_name = virtnet_get_phys_port_name,
.ndo_set_features = virtnet_set_features,
.ndo_tx_timeout = virtnet_tx_timeout,
};
static void virtnet_config_changed_work(struct work_struct *work)
{
struct virtnet_info *vi =
container_of(work, struct virtnet_info, config_work);
u16 v;
if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
struct virtio_net_config, status, &v) < 0)
return;
if (v & VIRTIO_NET_S_ANNOUNCE) {
netdev_notify_peers(vi->dev);
virtnet_ack_link_announce(vi);
}
/* Ignore unknown (future) status bits */
v &= VIRTIO_NET_S_LINK_UP;
if (vi->status == v)
return;
vi->status = v;
if (vi->status & VIRTIO_NET_S_LINK_UP) {
virtnet_update_settings(vi);
netif_carrier_on(vi->dev);
netif_tx_wake_all_queues(vi->dev);
} else {
netif_carrier_off(vi->dev);
netif_tx_stop_all_queues(vi->dev);
}
}
static void virtnet_config_changed(struct virtio_device *vdev)
{
struct virtnet_info *vi = vdev->priv;
schedule_work(&vi->config_work);
}
static void virtnet_free_queues(struct virtnet_info *vi)
{
int i;
for (i = 0; i < vi->max_queue_pairs; i++) {
__netif_napi_del(&vi->rq[i].napi);
__netif_napi_del(&vi->sq[i].napi);
}
/* We called __netif_napi_del(),
* we need to respect an RCU grace period before freeing vi->rq
*/
synchronize_net();
kfree(vi->rq);
kfree(vi->sq);
kfree(vi->ctrl);
}
static void _free_receive_bufs(struct virtnet_info *vi)
{
struct bpf_prog *old_prog;
int i;
for (i = 0; i < vi->max_queue_pairs; i++) {
while (vi->rq[i].pages)
__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
if (old_prog)
Annotation
- Immediate include surface: `linux/netdevice.h`, `linux/etherdevice.h`, `linux/ethtool.h`, `linux/module.h`, `linux/virtio.h`, `linux/virtio_net.h`, `linux/bpf.h`, `linux/bpf_trace.h`.
- Detected declarations: `struct virtnet_stat_desc`, `struct virtnet_sq_free_stats`, `struct virtnet_sq_stats`, `struct virtnet_rq_stats`, `struct virtnet_interrupt_coalesce`, `struct send_queue`, `struct receive_queue`, `struct control_buf`, `struct virtnet_info`, `struct padded_vnet_hdr`.
- Atlas domain: Driver Families / drivers/net.
- Implementation status: pattern 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.