net/core/gen_estimator.c
Source file repositories/reference/linux-study-clean/net/core/gen_estimator.c
File Facts
- System
- Linux kernel
- Corpus path
net/core/gen_estimator.c- Extension
.c- Size
- 7545 bytes
- Lines
- 281
- 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/uaccess.hlinux/bitops.hlinux/module.hlinux/types.hlinux/kernel.hlinux/jiffies.hlinux/string.hlinux/mm.hlinux/socket.hlinux/sockios.hlinux/in.hlinux/errno.hlinux/interrupt.hlinux/netdevice.hlinux/skbuff.hlinux/rtnetlink.hlinux/init.hlinux/slab.hlinux/seqlock.hnet/sock.hnet/gen_stats.h
Detected Declarations
struct net_rate_estimatorfunction est_fetch_countersfunction est_timerfunction gen_new_estimatorfunction gen_kill_estimatorfunction gen_kill_estimatorfunction gen_estimator_activefunction gen_estimator_readexport gen_new_estimatorexport gen_kill_estimatorexport gen_replace_estimatorexport gen_estimator_activeexport gen_estimator_read
Annotated Snippet
struct net_rate_estimator {
struct gnet_stats_basic_sync *bstats;
spinlock_t *stats_lock;
bool running;
struct gnet_stats_basic_sync __percpu *cpu_bstats;
u8 ewma_log;
u8 intvl_log; /* period : (250ms << intvl_log) */
seqcount_t seq;
u64 last_packets;
u64 last_bytes;
u64 avpps;
u64 avbps;
unsigned long next_jiffies;
struct timer_list timer;
struct rcu_head rcu;
};
static void est_fetch_counters(struct net_rate_estimator *e,
struct gnet_stats_basic_sync *b)
{
gnet_stats_basic_sync_init(b);
if (e->stats_lock)
spin_lock(e->stats_lock);
gnet_stats_add_basic(b, e->cpu_bstats, e->bstats, e->running);
if (e->stats_lock)
spin_unlock(e->stats_lock);
}
static void est_timer(struct timer_list *t)
{
struct net_rate_estimator *est = timer_container_of(est, t, timer);
struct gnet_stats_basic_sync b;
u64 b_bytes, b_packets;
u64 rate, brate;
est_fetch_counters(est, &b);
b_bytes = u64_stats_read(&b.bytes);
b_packets = u64_stats_read(&b.packets);
brate = (b_bytes - est->last_bytes) << (10 - est->intvl_log);
brate = (brate >> est->ewma_log) - (est->avbps >> est->ewma_log);
rate = (b_packets - est->last_packets) << (10 - est->intvl_log);
rate = (rate >> est->ewma_log) - (est->avpps >> est->ewma_log);
preempt_disable_nested();
write_seqcount_begin(&est->seq);
est->avbps += brate;
est->avpps += rate;
write_seqcount_end(&est->seq);
preempt_enable_nested();
est->last_bytes = b_bytes;
est->last_packets = b_packets;
est->next_jiffies += ((HZ/4) << est->intvl_log);
if (unlikely(time_after_eq(jiffies, est->next_jiffies))) {
/* Ouch... timer was delayed. */
est->next_jiffies = jiffies + 1;
}
mod_timer(&est->timer, est->next_jiffies);
}
/**
* gen_new_estimator - create a new rate estimator
* @bstats: basic statistics
* @cpu_bstats: bstats per cpu
* @rate_est: rate estimator statistics
* @lock: lock for statistics and control path
* @running: true if @bstats represents a running qdisc, thus @bstats'
* internal values might change during basic reads. Only used
* if @bstats_cpu is NULL
* @opt: rate estimator configuration TLV
*
* Creates a new rate estimator with &bstats as source and &rate_est
* as destination. A new timer with the interval specified in the
* configuration TLV is created. Upon each interval, the latest statistics
* will be read from &bstats and the estimated rate will be stored in
* &rate_est with the statistics lock grabbed during this period.
*
* Returns 0 on success or a negative error code.
*
*/
Annotation
- Immediate include surface: `linux/uaccess.h`, `linux/bitops.h`, `linux/module.h`, `linux/types.h`, `linux/kernel.h`, `linux/jiffies.h`, `linux/string.h`, `linux/mm.h`.
- Detected declarations: `struct net_rate_estimator`, `function est_fetch_counters`, `function est_timer`, `function gen_new_estimator`, `function gen_kill_estimator`, `function gen_kill_estimator`, `function gen_estimator_active`, `function gen_estimator_read`, `export gen_new_estimator`, `export gen_kill_estimator`.
- 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.