net/netfilter/nf_conntrack_core.c
Source file repositories/reference/linux-study-clean/net/netfilter/nf_conntrack_core.c
File Facts
- System
- Linux kernel
- Corpus path
net/netfilter/nf_conntrack_core.c- Extension
.c- Size
- 74082 bytes
- Lines
- 2791
- 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.
- 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/types.hlinux/netfilter.hlinux/module.hlinux/sched.hlinux/skbuff.hlinux/proc_fs.hlinux/vmalloc.hlinux/stddef.hlinux/slab.hlinux/random.hlinux/siphash.hlinux/err.hlinux/percpu.hlinux/moduleparam.hlinux/notifier.hlinux/kernel.hlinux/netdevice.hlinux/socket.hlinux/mm.hlinux/nsproxy.hlinux/rculist_nulls.hnet/netfilter/nf_conntrack.hnet/netfilter/nf_conntrack_bpf.hnet/netfilter/nf_conntrack_l4proto.hnet/netfilter/nf_conntrack_expect.hnet/netfilter/nf_conntrack_helper.hnet/netfilter/nf_conntrack_core.hnet/netfilter/nf_conntrack_extend.hnet/netfilter/nf_conntrack_acct.hnet/netfilter/nf_conntrack_ecache.hnet/netfilter/nf_conntrack_zones.hnet/netfilter/nf_conntrack_timestamp.h
Detected Declarations
struct conntrack_gc_workfunction nf_conntrack_lockfunction nf_conntrack_double_unlockfunction nf_conntrack_double_lockfunction nf_conntrack_all_lockfunction nf_conntrack_all_unlockfunction hash_conntrack_rawfunction scale_hashfunction __hash_conntrackfunction hash_conntrackfunction nf_ct_get_tuple_portsfunction nf_ct_get_tuplefunction ipv4_get_l4protofunction ipv6_get_l4protofunction get_l4protofunction nf_ct_get_tupleprfunction nf_ct_invert_tuplefunction nf_ct_get_idfunction nf_conntrack_get_idfunction clean_from_listsfunction nf_ct_tmpl_freefunction destroy_gre_conntrackfunction warn_on_keymap_list_leakfunction nf_ct_destroyfunction __nf_ct_delete_from_listsfunction nf_ct_delete_from_listsfunction nf_ct_add_to_ecache_listfunction nf_ct_deletefunction nf_ct_key_equalfunction nf_ct_matchfunction nf_ct_gc_expiredfunction nf_ct_tuple_equalfunction hlist_nulls_for_each_entry_rcufunction __nf_conntrack_find_getfunction nf_conntrack_find_getfunction __nf_conntrack_hash_insertfunction nf_conntrack_hash_check_insertfunction hlist_nulls_for_each_entryfunction nf_ct_acct_addfunction nf_ct_acct_mergefunction __nf_conntrack_insert_preparefunction nf_ct_match_reversefunction nf_ct_can_mergefunction __nf_ct_resolve_clashfunction nf_ct_resolve_clash_harderfunction nf_ct_resolve_clashfunction __nf_conntrack_confirmfunction nf_ct_get_next_corpse
Annotated Snippet
struct conntrack_gc_work {
struct delayed_work dwork;
u32 next_bucket;
u32 avg_timeout;
u32 count;
u32 start_time;
bool exiting;
bool early_drop;
};
static __read_mostly struct kmem_cache *nf_conntrack_cachep;
static DEFINE_SPINLOCK(nf_conntrack_locks_all_lock);
static __read_mostly bool nf_conntrack_locks_all;
/* serialize hash resizes and nf_ct_iterate_cleanup */
static DEFINE_MUTEX(nf_conntrack_mutex);
#define GC_SCAN_INTERVAL_MAX (60ul * HZ)
#define GC_SCAN_INTERVAL_MIN (1ul * HZ)
/* clamp timeouts to this value (TCP unacked) */
#define GC_SCAN_INTERVAL_CLAMP (300ul * HZ)
/* Initial bias pretending we have 100 entries at the upper bound so we don't
* wakeup often just because we have three entries with a 1s timeout while still
* allowing non-idle machines to wakeup more often when needed.
*/
#define GC_SCAN_INITIAL_COUNT 100
#define GC_SCAN_INTERVAL_INIT GC_SCAN_INTERVAL_MAX
#define GC_SCAN_MAX_DURATION msecs_to_jiffies(10)
#define GC_SCAN_EXPIRED_MAX (64000u / HZ)
#define MIN_CHAINLEN 50u
#define MAX_CHAINLEN (80u - MIN_CHAINLEN)
static struct conntrack_gc_work conntrack_gc_work;
void nf_conntrack_lock(spinlock_t *lock) __acquires(lock)
{
/* 1) Acquire the lock */
spin_lock(lock);
/* 2) read nf_conntrack_locks_all, with ACQUIRE semantics
* It pairs with the smp_store_release() in nf_conntrack_all_unlock()
*/
if (likely(smp_load_acquire(&nf_conntrack_locks_all) == false))
return;
/* fast path failed, unlock */
spin_unlock(lock);
/* Slow path 1) get global lock */
spin_lock(&nf_conntrack_locks_all_lock);
/* Slow path 2) get the lock we want */
spin_lock(lock);
/* Slow path 3) release the global lock */
spin_unlock(&nf_conntrack_locks_all_lock);
}
EXPORT_SYMBOL_GPL(nf_conntrack_lock);
static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2)
{
h1 %= CONNTRACK_LOCKS;
h2 %= CONNTRACK_LOCKS;
spin_unlock(&nf_conntrack_locks[h1]);
if (h1 != h2)
spin_unlock(&nf_conntrack_locks[h2]);
}
/* return true if we need to recompute hashes (in case hash table was resized) */
static bool nf_conntrack_double_lock(unsigned int h1, unsigned int h2,
unsigned int sequence)
{
h1 %= CONNTRACK_LOCKS;
h2 %= CONNTRACK_LOCKS;
if (h1 <= h2) {
nf_conntrack_lock(&nf_conntrack_locks[h1]);
if (h1 != h2)
spin_lock_nested(&nf_conntrack_locks[h2],
SINGLE_DEPTH_NESTING);
} else {
nf_conntrack_lock(&nf_conntrack_locks[h2]);
spin_lock_nested(&nf_conntrack_locks[h1],
SINGLE_DEPTH_NESTING);
}
if (read_seqcount_retry(&nf_conntrack_generation, sequence)) {
nf_conntrack_double_unlock(h1, h2);
Annotation
- Immediate include surface: `linux/types.h`, `linux/netfilter.h`, `linux/module.h`, `linux/sched.h`, `linux/skbuff.h`, `linux/proc_fs.h`, `linux/vmalloc.h`, `linux/stddef.h`.
- Detected declarations: `struct conntrack_gc_work`, `function nf_conntrack_lock`, `function nf_conntrack_double_unlock`, `function nf_conntrack_double_lock`, `function nf_conntrack_all_lock`, `function nf_conntrack_all_unlock`, `function hash_conntrack_raw`, `function scale_hash`, `function __hash_conntrack`, `function hash_conntrack`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- Implementation status: integration 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.