samples/bpf/sockex3_kern.c
Source file repositories/reference/linux-study-clean/samples/bpf/sockex3_kern.c
File Facts
- System
- Linux kernel
- Corpus path
samples/bpf/sockex3_kern.c- Extension
.c- Size
- 6627 bytes
- Lines
- 305
- Domain
- Support Tooling And Documentation
- Bucket
- samples
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
uapi/linux/bpf.huapi/linux/in.huapi/linux/if.huapi/linux/if_ether.huapi/linux/ip.huapi/linux/ipv6.huapi/linux/if_tunnel.huapi/linux/mpls.hbpf/bpf_helpers.hbpf_legacy.h
Detected Declarations
struct vlan_hdrstruct flow_key_recordstruct globalsstruct pairstruct gre_hdrfunction ip_is_fragmentfunction ipv6_addr_hashfunction update_statsfunction parse_ip_protofunction bpf_func_ipfunction bpf_func_ipv6function bpf_func_vlanfunction bpf_func_mplsfunction parse_eth_protofunction main_prog
Annotated Snippet
struct vlan_hdr {
__be16 h_vlan_TCI;
__be16 h_vlan_encapsulated_proto;
};
struct flow_key_record {
__be32 src;
__be32 dst;
union {
__be32 ports;
__be16 port16[2];
};
__u32 ip_proto;
};
static inline void parse_eth_proto(struct __sk_buff *skb, u32 proto);
static inline int ip_is_fragment(struct __sk_buff *ctx, __u64 nhoff)
{
return load_half(ctx, nhoff + offsetof(struct iphdr, frag_off))
& (IP_MF | IP_OFFSET);
}
static inline __u32 ipv6_addr_hash(struct __sk_buff *ctx, __u64 off)
{
__u64 w0 = load_word(ctx, off);
__u64 w1 = load_word(ctx, off + 4);
__u64 w2 = load_word(ctx, off + 8);
__u64 w3 = load_word(ctx, off + 12);
return (__u32)(w0 ^ w1 ^ w2 ^ w3);
}
struct globals {
struct flow_key_record flow;
};
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, __u32);
__type(value, struct globals);
__uint(max_entries, 32);
} percpu_map SEC(".maps");
/* user poor man's per_cpu until native support is ready */
static struct globals *this_cpu_globals(void)
{
u32 key = bpf_get_smp_processor_id();
return bpf_map_lookup_elem(&percpu_map, &key);
}
/* some simple stats for user space consumption */
struct pair {
__u64 packets;
__u64 bytes;
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, struct flow_key_record);
__type(value, struct pair);
__uint(max_entries, 1024);
} hash_map SEC(".maps");
static void update_stats(struct __sk_buff *skb, struct globals *g)
{
struct flow_key_record key = g->flow;
struct pair *value;
value = bpf_map_lookup_elem(&hash_map, &key);
if (value) {
__sync_fetch_and_add(&value->packets, 1);
__sync_fetch_and_add(&value->bytes, skb->len);
} else {
struct pair val = {1, skb->len};
bpf_map_update_elem(&hash_map, &key, &val, BPF_ANY);
}
}
static __always_inline void parse_ip_proto(struct __sk_buff *skb,
struct globals *g, __u32 ip_proto)
{
__u32 nhoff = skb->cb[0];
int poff;
switch (ip_proto) {
case IPPROTO_GRE: {
struct gre_hdr {
Annotation
- Immediate include surface: `uapi/linux/bpf.h`, `uapi/linux/in.h`, `uapi/linux/if.h`, `uapi/linux/if_ether.h`, `uapi/linux/ip.h`, `uapi/linux/ipv6.h`, `uapi/linux/if_tunnel.h`, `uapi/linux/mpls.h`.
- Detected declarations: `struct vlan_hdr`, `struct flow_key_record`, `struct globals`, `struct pair`, `struct gre_hdr`, `function ip_is_fragment`, `function ipv6_addr_hash`, `function update_stats`, `function parse_ip_proto`, `function bpf_func_ip`.
- Atlas domain: Support Tooling And Documentation / samples.
- Implementation status: source implementation candidate.
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.