net/core/skbuff.c
Source file repositories/reference/linux-study-clean/net/core/skbuff.c
File Facts
- System
- Linux kernel
- Corpus path
net/core/skbuff.c- Extension
.c- Size
- 192030 bytes
- Lines
- 7526
- Domain
- Networking Core
- Bucket
- Sockets, Protocols, Packet Path, And Network Policy
- Inferred role
- Networking Core: exported/initcall integration point
- Status
- integration 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.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- 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/module.hlinux/types.hlinux/kernel.hlinux/mm.hlinux/interrupt.hlinux/in.hlinux/inet.hlinux/slab.hlinux/tcp.hlinux/udp.hlinux/sctp.hlinux/netdevice.hnet/pkt_sched.hlinux/string.hlinux/skbuff.hlinux/skbuff_ref.hlinux/splice.hlinux/cache.hlinux/rtnetlink.hlinux/init.hlinux/scatterlist.hlinux/errqueue.hlinux/prefetch.hlinux/bitfield.hlinux/if_vlan.hlinux/mpls.hlinux/kcov.hlinux/iov_iter.hlinux/crc32.hnet/protocol.hnet/dst.hnet/sock.h
Detected Declarations
struct napi_alloc_cachestruct skb_free_arrayfunction drop_reasons_register_subsysfunction drop_reasons_unregister_subsysfunction skb_over_panicfunction skb_over_panicfunction skb_under_panicfunction skbuff_clearfunction memsetfunction __finalize_skb_aroundfunction __build_skb_aroundfunction vmallocfunction __build_skbfunction __napi_build_skbfunction clonedfunction kmallocfunction kmallocfunction skb_coalesce_rx_fragfunction skb_drop_listfunction skb_drop_fraglistfunction skb_clone_fraglistfunction skb_pp_cow_datafunction skb_cow_data_for_xdpfunction napi_pp_put_pagefunction skb_pp_recyclefunction skb_pp_frag_reffunction skb_kfree_headfunction skb_free_headfunction skb_release_datafunction kfree_skbmemfunction skb_release_head_statefunction skb_release_allfunction __kfree_skbfunction __sk_skb_reason_dropfunction sk_skb_reason_dropfunction kfree_skb_add_bulkfunction kfree_skb_list_reasonfunction net_ratelimitfunction skb_frag_foreach_pagefunction skb_tx_errorfunction consume_skbfunction consume_skbfunction napi_skb_cache_putfunction __napi_kfree_skbfunction napi_skb_free_stolen_headfunction napi_consume_skbfunction __copy_skb_headerfunction alloc_skb_for_msg
Annotated Snippet
struct napi_alloc_cache {
local_lock_t bh_lock;
struct page_frag_cache page;
unsigned int skb_count;
void *skb_cache[NAPI_SKB_CACHE_SIZE];
};
static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache) = {
.bh_lock = INIT_LOCAL_LOCK(bh_lock),
};
void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
{
struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
void *data;
fragsz = SKB_DATA_ALIGN(fragsz);
local_lock_nested_bh(&napi_alloc_cache.bh_lock);
data = __page_frag_alloc_align(&nc->page, fragsz,
GFP_ATOMIC | __GFP_NOWARN, align_mask);
local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
return data;
}
EXPORT_SYMBOL(__napi_alloc_frag_align);
void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
{
void *data;
if (in_hardirq() || irqs_disabled()) {
struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache);
fragsz = SKB_DATA_ALIGN(fragsz);
data = __page_frag_alloc_align(nc, fragsz,
GFP_ATOMIC | __GFP_NOWARN,
align_mask);
} else {
local_bh_disable();
data = __napi_alloc_frag_align(fragsz, align_mask);
local_bh_enable();
}
return data;
}
EXPORT_SYMBOL(__netdev_alloc_frag_align);
/* Cache kmem_cache_size(net_hotdata.skbuff_cache) to help the compiler
* remove dead code (and skbuff_cache_size) when CONFIG_KASAN is unset.
*/
static u32 skbuff_cache_size __read_mostly;
static inline struct sk_buff *napi_skb_cache_get(bool alloc)
{
struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
struct sk_buff *skb;
local_lock_nested_bh(&napi_alloc_cache.bh_lock);
if (unlikely(!nc->skb_count)) {
if (alloc && kmem_cache_alloc_bulk(net_hotdata.skbuff_cache,
GFP_ATOMIC | __GFP_NOWARN,
NAPI_SKB_CACHE_BULK,
nc->skb_cache))
nc->skb_count = NAPI_SKB_CACHE_BULK;
if (unlikely(!nc->skb_count)) {
local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
return NULL;
}
}
skb = nc->skb_cache[--nc->skb_count];
if (nc->skb_count)
prefetch(nc->skb_cache[nc->skb_count - 1]);
local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
kasan_mempool_unpoison_object(skb, skbuff_cache_size);
return skb;
}
/*
* Only clear those fields we need to clear, not those that we will
* actually initialise later. Hence, don't put any more fields after
* the tail pointer in struct sk_buff!
*/
static inline void skbuff_clear(struct sk_buff *skb)
{
/* Replace memset(skb, 0, offsetof(struct sk_buff, tail))
* with two smaller memset(), with a barrier() between them.
* This forces the compiler to inline both calls.
Annotation
- Immediate include surface: `linux/module.h`, `linux/types.h`, `linux/kernel.h`, `linux/mm.h`, `linux/interrupt.h`, `linux/in.h`, `linux/inet.h`, `linux/slab.h`.
- Detected declarations: `struct napi_alloc_cache`, `struct skb_free_array`, `function drop_reasons_register_subsys`, `function drop_reasons_unregister_subsys`, `function skb_over_panic`, `function skb_over_panic`, `function skb_under_panic`, `function skbuff_clear`, `function memset`, `function __finalize_skb_around`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- Implementation status: integration 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.