drivers/net/tun.c
Source file repositories/reference/linux-study-clean/drivers/net/tun.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/net/tun.c- Extension
.c- Size
- 91396 bytes
- Lines
- 3842
- 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.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- 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
linux/module.hlinux/errno.hlinux/kernel.hlinux/sched/signal.hlinux/major.hlinux/slab.hlinux/poll.hlinux/fcntl.hlinux/init.hlinux/skbuff.hlinux/netdevice.hlinux/etherdevice.hlinux/miscdevice.hlinux/ethtool.hlinux/rtnetlink.hlinux/compat.hlinux/if.hlinux/if_arp.hlinux/if_ether.hlinux/if_tun.hlinux/if_vlan.hlinux/crc32.hlinux/math.hlinux/nsproxy.hlinux/virtio_net.hlinux/rcupdate.hnet/net_namespace.hnet/netns/generic.hnet/rtnetlink.hnet/sock.hnet/xdp.hnet/ip_tunnels.h
Detected Declarations
struct tap_filterstruct tun_filestruct tun_pagestruct tun_flow_entrystruct tun_progstruct tun_structstruct vethfunction tun_napi_receivefunction tun_napi_pollfunction tun_napi_initfunction tun_napi_enablefunction tun_napi_disablefunction tun_napi_delfunction tun_napi_frags_enabledfunction tun_hashfnfunction hlist_for_each_entry_rcufunction tun_flow_deletefunction tun_flow_flushfunction tun_flow_delete_by_queuefunction hlist_for_each_entry_safefunction tun_flow_cleanupfunction hlist_for_each_entry_safefunction tun_flow_updatefunction tun_flow_save_rps_rxhashfunction tun_automq_select_queuefunction tun_ebpf_select_queuefunction tun_select_queuefunction tun_not_capablefunction tun_set_real_num_queuesfunction tun_disable_queuefunction tun_ptr_freefunction tun_queue_purgefunction __tun_detachfunction tun_detachfunction tun_detach_allfunction tun_attachfunction tun_putfunction addr_hash_setfunction addr_hash_testfunction update_filterfunction run_filterfunction check_filterfunction tun_net_initfunction tun_net_uninitfunction tun_net_openfunction tun_net_closefunction tun_automq_xmitfunction run_ebpf_filter
Annotated Snippet
static const struct net_device_ops tun_netdev_ops = {
.ndo_init = tun_net_init,
.ndo_uninit = tun_net_uninit,
.ndo_open = tun_net_open,
.ndo_stop = tun_net_close,
.ndo_start_xmit = tun_net_xmit,
.ndo_fix_features = tun_net_fix_features,
.ndo_select_queue = tun_select_queue,
.ndo_set_rx_headroom = tun_set_headroom,
.ndo_get_stats64 = tun_net_get_stats64,
.ndo_change_carrier = tun_net_change_carrier,
};
static void __tun_xdp_flush_tfile(struct tun_file *tfile)
{
/* Notify and wake up reader process */
if (tfile->flags & TUN_FASYNC)
kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
tfile->socket.sk->sk_data_ready(tfile->socket.sk);
}
static int tun_xdp_xmit(struct net_device *dev, int n,
struct xdp_frame **frames, u32 flags)
{
struct tun_struct *tun = netdev_priv(dev);
struct tun_file *tfile;
u32 numqueues;
int nxmit = 0;
int i;
if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
return -EINVAL;
rcu_read_lock();
resample:
numqueues = READ_ONCE(tun->numqueues);
if (!numqueues) {
rcu_read_unlock();
return -ENXIO; /* Caller will free/return all frames */
}
tfile = rcu_dereference(tun->tfiles[smp_processor_id() %
numqueues]);
if (unlikely(!tfile))
goto resample;
spin_lock(&tfile->tx_ring.producer_lock);
for (i = 0; i < n; i++) {
struct xdp_frame *xdp = frames[i];
/* Encode the XDP flag into lowest bit for consumer to differ
* XDP buffer from sk_buff.
*/
void *frame = tun_xdp_to_ptr(xdp);
if (__ptr_ring_produce(&tfile->tx_ring, frame)) {
dev_core_stats_tx_dropped_inc(dev);
break;
}
nxmit++;
}
spin_unlock(&tfile->tx_ring.producer_lock);
if (flags & XDP_XMIT_FLUSH)
__tun_xdp_flush_tfile(tfile);
rcu_read_unlock();
return nxmit;
}
static int tun_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
{
struct xdp_frame *frame = xdp_convert_buff_to_frame(xdp);
int nxmit;
if (unlikely(!frame))
return -EOVERFLOW;
nxmit = tun_xdp_xmit(dev, 1, &frame, XDP_XMIT_FLUSH);
if (!nxmit)
xdp_return_frame_rx_napi(frame);
return nxmit;
}
static const struct net_device_ops tap_netdev_ops = {
.ndo_init = tun_net_init,
.ndo_uninit = tun_net_uninit,
.ndo_open = tun_net_open,
.ndo_stop = tun_net_close,
.ndo_start_xmit = tun_net_xmit,
Annotation
- Immediate include surface: `linux/module.h`, `linux/errno.h`, `linux/kernel.h`, `linux/sched/signal.h`, `linux/major.h`, `linux/slab.h`, `linux/poll.h`, `linux/fcntl.h`.
- Detected declarations: `struct tap_filter`, `struct tun_file`, `struct tun_page`, `struct tun_flow_entry`, `struct tun_prog`, `struct tun_struct`, `struct veth`, `function tun_napi_receive`, `function tun_napi_poll`, `function tun_napi_init`.
- Atlas domain: Driver Families / drivers/net.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- 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.