net/packet/af_packet.c
Source file repositories/reference/linux-study-clean/net/packet/af_packet.c
File Facts
- System
- Linux kernel
- Corpus path
net/packet/af_packet.c- Extension
.c- Size
- 118896 bytes
- Lines
- 4820
- Domain
- Networking Core
- Bucket
- Sockets, Protocols, Packet Path, And Network Policy
- Inferred role
- Networking Core: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- 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.
- 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/ethtool.hlinux/uio.hlinux/filter.hlinux/types.hlinux/mm.hlinux/capability.hlinux/fcntl.hlinux/socket.hlinux/in.hlinux/inet.hlinux/netdevice.hlinux/if_packet.hlinux/wireless.hlinux/kernel.hlinux/kmod.hlinux/slab.hlinux/vmalloc.hnet/net_namespace.hnet/ip.hnet/protocol.hlinux/skbuff.hnet/sock.hlinux/errno.hlinux/timer.hlinux/uaccess.hasm/ioctls.hasm/page.hasm/cacheflush.hasm/io.hlinux/proc_fs.hlinux/seq_file.hlinux/poll.h
Detected Declarations
struct packet_mreq_maxstruct packet_sockstruct packet_skb_cbfunction packet_xmitfunction packet_cached_dev_assignfunction packet_cached_dev_resetfunction packet_pick_tx_queuefunction possiblefunction register_prot_hookfunction __unregister_prot_hookfunction unregister_prot_hookfunction pgv_to_pagefunction __packet_set_statusfunction __packet_get_statusfunction tpacket_get_timestampfunction __packet_set_timestampfunction vlan_get_tcifunction vlan_get_protocol_dgramfunction prb_shutdown_retire_blk_timerfunction prb_calc_retire_blk_tmofunction prb_init_ft_opsfunction init_prb_bdqcfunction prb_calc_retire_blk_tmofunction copy_bitsfunction prb_flush_blockfunction prb_close_blockfunction prb_thaw_queuefunction prb_open_blockfunction prb_retire_current_blockfunction prb_retire_current_blockfunction copy_bitsfunction prb_curr_blk_in_usefunction prb_queue_frozenfunction prb_clear_blk_fill_statusfunction prb_fill_rxhashfunction prb_clear_rxhashfunction prb_fill_vlan_infofunction prb_run_all_ft_opsfunction prb_fill_curr_blockfunction prb_previous_blk_numfunction packet_increment_rx_headfunction packet_increment_headfunction packet_inc_pendingfunction packet_dec_pendingfunction packet_read_pendingfunction packet_alloc_pendingfunction packet_free_pendingfunction __tpacket_has_room
Annotated Snippet
const struct net_device_ops *ops = dev->netdev_ops;
int cpu = raw_smp_processor_id();
u16 queue_index;
#ifdef CONFIG_XPS
skb->sender_cpu = cpu + 1;
#endif
skb_record_rx_queue(skb, cpu % dev->real_num_tx_queues);
if (ops->ndo_select_queue) {
queue_index = ops->ndo_select_queue(dev, skb, NULL);
queue_index = netdev_cap_txqueue(dev, queue_index);
} else {
queue_index = netdev_pick_tx(dev, skb, NULL);
}
return queue_index;
}
/* __register_prot_hook must be invoked through register_prot_hook
* or from a context in which asynchronous accesses to the packet
* socket is not possible (packet_create()).
*/
static void __register_prot_hook(struct sock *sk)
{
struct packet_sock *po = pkt_sk(sk);
if (!packet_sock_flag(po, PACKET_SOCK_RUNNING)) {
if (po->fanout)
__fanout_link(sk, po);
else
dev_add_pack(&po->prot_hook);
sock_hold(sk);
packet_sock_flag_set(po, PACKET_SOCK_RUNNING, 1);
}
}
static void register_prot_hook(struct sock *sk)
{
lockdep_assert_held_once(&pkt_sk(sk)->bind_lock);
__register_prot_hook(sk);
}
/* If the sync parameter is true, we will temporarily drop
* the po->bind_lock and do a synchronize_net to make sure no
* asynchronous packet processing paths still refer to the elements
* of po->prot_hook. If the sync parameter is false, it is the
* callers responsibility to take care of this.
*/
static void __unregister_prot_hook(struct sock *sk, bool sync)
{
struct packet_sock *po = pkt_sk(sk);
lockdep_assert_held_once(&po->bind_lock);
packet_sock_flag_set(po, PACKET_SOCK_RUNNING, 0);
if (po->fanout)
__fanout_unlink(sk, po);
else
__dev_remove_pack(&po->prot_hook);
__sock_put(sk);
if (sync) {
spin_unlock(&po->bind_lock);
synchronize_net();
spin_lock(&po->bind_lock);
}
}
static void unregister_prot_hook(struct sock *sk, bool sync)
{
struct packet_sock *po = pkt_sk(sk);
if (packet_sock_flag(po, PACKET_SOCK_RUNNING))
__unregister_prot_hook(sk, sync);
}
static inline struct page * __pure pgv_to_page(void *addr)
{
if (is_vmalloc_addr(addr))
return vmalloc_to_page(addr);
return virt_to_page(addr);
}
static void __packet_set_status(struct packet_sock *po, void *frame, int status)
{
union tpacket_uhdr h;
Annotation
- Immediate include surface: `linux/ethtool.h`, `linux/uio.h`, `linux/filter.h`, `linux/types.h`, `linux/mm.h`, `linux/capability.h`, `linux/fcntl.h`, `linux/socket.h`.
- Detected declarations: `struct packet_mreq_max`, `struct packet_sock`, `struct packet_skb_cb`, `function packet_xmit`, `function packet_cached_dev_assign`, `function packet_cached_dev_reset`, `function packet_pick_tx_queue`, `function possible`, `function register_prot_hook`, `function __unregister_prot_hook`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- 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.
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.