samples/bpf/parse_varlen.c
Source file repositories/reference/linux-study-clean/samples/bpf/parse_varlen.c
File Facts
- System
- Linux kernel
- Corpus path
samples/bpf/parse_varlen.c- Extension
.c- Size
- 3487 bytes
- Lines
- 151
- 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
linux/if_ether.hlinux/if_vlan.hlinux/ip.hlinux/ipv6.hlinux/in.hlinux/tcp.hlinux/udp.huapi/linux/bpf.hnet/ip.hbpf/bpf_helpers.h
Detected Declarations
function tcpfunction udpfunction parse_ipv4function parse_ipv6function handle_ingress
Annotated Snippet
if (DEBUG) {
char fmt[] = "udp port 9 indeed\n";
bpf_trace_printk(fmt, sizeof(fmt));
}
return TC_ACT_SHOT;
}
return 0;
}
static int parse_ipv4(void *data, uint64_t nh_off, void *data_end)
{
struct iphdr *iph;
uint64_t ihl_len;
iph = data + nh_off;
if (iph + 1 > data_end)
return 0;
if (ip_is_fragment(iph))
return 0;
ihl_len = iph->ihl * 4;
if (iph->protocol == IPPROTO_IPIP) {
iph = data + nh_off + ihl_len;
if (iph + 1 > data_end)
return 0;
ihl_len += iph->ihl * 4;
}
if (iph->protocol == IPPROTO_TCP)
return tcp(data, nh_off + ihl_len, data_end);
else if (iph->protocol == IPPROTO_UDP)
return udp(data, nh_off + ihl_len, data_end);
return 0;
}
static int parse_ipv6(void *data, uint64_t nh_off, void *data_end)
{
struct ipv6hdr *ip6h;
struct iphdr *iph;
uint64_t ihl_len = sizeof(struct ipv6hdr);
uint64_t nexthdr;
ip6h = data + nh_off;
if (ip6h + 1 > data_end)
return 0;
nexthdr = ip6h->nexthdr;
if (nexthdr == IPPROTO_IPIP) {
iph = data + nh_off + ihl_len;
if (iph + 1 > data_end)
return 0;
ihl_len += iph->ihl * 4;
nexthdr = iph->protocol;
} else if (nexthdr == IPPROTO_IPV6) {
ip6h = data + nh_off + ihl_len;
if (ip6h + 1 > data_end)
return 0;
ihl_len += sizeof(struct ipv6hdr);
nexthdr = ip6h->nexthdr;
}
if (nexthdr == IPPROTO_TCP)
return tcp(data, nh_off + ihl_len, data_end);
else if (nexthdr == IPPROTO_UDP)
return udp(data, nh_off + ihl_len, data_end);
return 0;
}
SEC("varlen")
int handle_ingress(struct __sk_buff *skb)
{
void *data = (void *)(long)skb->data;
struct ethhdr *eth = data;
void *data_end = (void *)(long)skb->data_end;
uint64_t h_proto, nh_off;
nh_off = sizeof(*eth);
if (data + nh_off > data_end)
return 0;
h_proto = eth->h_proto;
if (h_proto == ETH_P_8021Q || h_proto == ETH_P_8021AD) {
struct vlan_hdr *vhdr;
vhdr = data + nh_off;
nh_off += sizeof(struct vlan_hdr);
Annotation
- Immediate include surface: `linux/if_ether.h`, `linux/if_vlan.h`, `linux/ip.h`, `linux/ipv6.h`, `linux/in.h`, `linux/tcp.h`, `linux/udp.h`, `uapi/linux/bpf.h`.
- Detected declarations: `function tcp`, `function udp`, `function parse_ipv4`, `function parse_ipv6`, `function handle_ingress`.
- 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.