net/netfilter/nft_counter.c
Source file repositories/reference/linux-study-clean/net/netfilter/nft_counter.c
File Facts
- System
- Linux kernel
- Corpus path
net/netfilter/nft_counter.c- Extension
.c- Size
- 8627 bytes
- Lines
- 328
- Domain
- Networking Core
- Bucket
- Sockets, Protocols, Packet Path, And Network Policy
- Inferred role
- Networking Core: implementation source
- Status
- source 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kernel.hlinux/init.hlinux/module.hlinux/u64_stats_sync.hlinux/netlink.hlinux/netfilter.hlinux/netfilter/nf_tables.hnet/netfilter/nf_tables.hnet/netfilter/nf_tables_core.hnet/netfilter/nf_tables_offload.h
Detected Declarations
struct nft_counterstruct nft_counter_totstruct nft_counter_percpu_privfunction nft_counter_do_evalfunction nft_counter_obj_evalfunction nft_counter_do_initfunction nft_counter_obj_initfunction nft_counter_do_destroyfunction nft_counter_obj_destroyfunction nft_counter_resetfunction nft_counter_fetchfunction nft_counter_fetch_and_resetfunction nft_counter_do_dumpfunction nft_counter_obj_dumpfunction nft_counter_evalfunction nft_counter_dumpfunction nft_counter_initfunction nft_counter_destroyfunction nft_counter_clonefunction nft_counter_offloadfunction nft_counter_offload_statsfunction nft_counter_init_seqcount
Annotated Snippet
struct nft_counter {
u64_stats_t bytes;
u64_stats_t packets;
};
struct nft_counter_tot {
s64 bytes;
s64 packets;
};
struct nft_counter_percpu_priv {
struct nft_counter __percpu *counter;
};
static DEFINE_PER_CPU(struct u64_stats_sync, nft_counter_sync);
/* control plane only: sync fetch+reset */
static DEFINE_SPINLOCK(nft_counter_lock);
static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv,
struct nft_regs *regs,
const struct nft_pktinfo *pkt)
{
struct u64_stats_sync *nft_sync;
struct nft_counter *this_cpu;
local_bh_disable();
this_cpu = this_cpu_ptr(priv->counter);
nft_sync = this_cpu_ptr(&nft_counter_sync);
u64_stats_update_begin(nft_sync);
u64_stats_add(&this_cpu->bytes, pkt->skb->len);
u64_stats_inc(&this_cpu->packets);
u64_stats_update_end(nft_sync);
local_bh_enable();
}
static inline void nft_counter_obj_eval(struct nft_object *obj,
struct nft_regs *regs,
const struct nft_pktinfo *pkt)
{
struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
nft_counter_do_eval(priv, regs, pkt);
}
static int nft_counter_do_init(const struct nlattr * const tb[],
struct nft_counter_percpu_priv *priv)
{
struct nft_counter __percpu *cpu_stats;
struct nft_counter *this_cpu;
cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_KERNEL_ACCOUNT);
if (cpu_stats == NULL)
return -ENOMEM;
this_cpu = raw_cpu_ptr(cpu_stats);
if (tb[NFTA_COUNTER_PACKETS]) {
u64_stats_set(&this_cpu->packets,
be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])));
}
if (tb[NFTA_COUNTER_BYTES]) {
u64_stats_set(&this_cpu->bytes,
be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])));
}
priv->counter = cpu_stats;
return 0;
}
static int nft_counter_obj_init(const struct nft_ctx *ctx,
const struct nlattr * const tb[],
struct nft_object *obj)
{
struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
return nft_counter_do_init(tb, priv);
}
static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv)
{
free_percpu(priv->counter);
}
static void nft_counter_obj_destroy(const struct nft_ctx *ctx,
struct nft_object *obj)
{
struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/init.h`, `linux/module.h`, `linux/u64_stats_sync.h`, `linux/netlink.h`, `linux/netfilter.h`, `linux/netfilter/nf_tables.h`, `net/netfilter/nf_tables.h`.
- Detected declarations: `struct nft_counter`, `struct nft_counter_tot`, `struct nft_counter_percpu_priv`, `function nft_counter_do_eval`, `function nft_counter_obj_eval`, `function nft_counter_do_init`, `function nft_counter_obj_init`, `function nft_counter_do_destroy`, `function nft_counter_obj_destroy`, `function nft_counter_reset`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- Implementation status: source 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.