samples/bpf/xdp_router_ipv4.bpf.c
Source file repositories/reference/linux-study-clean/samples/bpf/xdp_router_ipv4.bpf.c
File Facts
- System
- Linux kernel
- Corpus path
samples/bpf/xdp_router_ipv4.bpf.c- Extension
.c- Size
- 4103 bytes
- Lines
- 190
- 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
vmlinux.hxdp_sample.bpf.hxdp_sample_shared.h
Detected Declarations
struct trie_valuestruct arp_entrystruct direct_mapfunction xdp_router_ipv4_prog
Annotated Snippet
struct trie_value {
__u8 prefix[4];
__be64 value;
int ifindex;
int metric;
__be32 gw;
};
/* Key for lpm_trie */
union key_4 {
u32 b32[2];
u8 b8[8];
};
struct arp_entry {
__be64 mac;
__be32 dst;
};
struct direct_map {
struct arp_entry arp;
int ifindex;
__be64 mac;
};
/* Map for trie implementation */
struct {
__uint(type, BPF_MAP_TYPE_LPM_TRIE);
__uint(key_size, 8);
__uint(value_size, sizeof(struct trie_value));
__uint(max_entries, 50);
__uint(map_flags, BPF_F_NO_PREALLOC);
} lpm_map SEC(".maps");
/* Map for ARP table */
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, __be32);
__type(value, __be64);
__uint(max_entries, 50);
} arp_table SEC(".maps");
/* Map to keep the exact match entries in the route table */
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, __be32);
__type(value, struct direct_map);
__uint(max_entries, 50);
} exact_match SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_DEVMAP);
__uint(key_size, sizeof(int));
__uint(value_size, sizeof(int));
__uint(max_entries, 100);
} tx_port SEC(".maps");
SEC("xdp")
int xdp_router_ipv4_prog(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct ethhdr *eth = data;
u64 nh_off = sizeof(*eth);
struct datarec *rec;
__be16 h_proto;
u32 key = 0;
rec = bpf_map_lookup_elem(&rx_cnt, &key);
if (rec)
NO_TEAR_INC(rec->processed);
if (data + nh_off > data_end)
goto drop;
h_proto = eth->h_proto;
if (h_proto == bpf_htons(ETH_P_8021Q) ||
h_proto == bpf_htons(ETH_P_8021AD)) {
struct vlan_hdr *vhdr;
vhdr = data + nh_off;
nh_off += sizeof(struct vlan_hdr);
if (data + nh_off > data_end)
goto drop;
h_proto = vhdr->h_vlan_encapsulated_proto;
}
switch (bpf_ntohs(h_proto)) {
case ETH_P_ARP:
Annotation
- Immediate include surface: `vmlinux.h`, `xdp_sample.bpf.h`, `xdp_sample_shared.h`.
- Detected declarations: `struct trie_value`, `struct arp_entry`, `struct direct_map`, `function xdp_router_ipv4_prog`.
- 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.