tools/testing/selftests/bpf/progs/xdp_redirect_map.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/xdp_redirect_map.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/xdp_redirect_map.c- Extension
.c- Size
- 2272 bytes
- Lines
- 120
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- 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/bpf.hbpf/bpf_helpers.hbpf/bpf_endian.h
Detected Declarations
function xdp_redirect_map_0function xdp_redirect_map_1function xdp_redirect_map_2function xdp_countfunction xdp_count_0function xdp_count_1function xdp_count_2function store_macfunction store_mac_1function store_mac_2
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
#include <linux/if_ether.h>
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
struct {
__uint(type, BPF_MAP_TYPE_DEVMAP);
__uint(max_entries, 8);
__uint(key_size, sizeof(int));
__uint(value_size, sizeof(int));
} tx_port SEC(".maps");
SEC("xdp")
int xdp_redirect_map_0(struct xdp_md *xdp)
{
return bpf_redirect_map(&tx_port, 0, 0);
}
SEC("xdp")
int xdp_redirect_map_1(struct xdp_md *xdp)
{
return bpf_redirect_map(&tx_port, 1, 0);
}
SEC("xdp")
int xdp_redirect_map_2(struct xdp_md *xdp)
{
return bpf_redirect_map(&tx_port, 2, 0);
}
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 3);
__type(key, __u32);
__type(value, __u64);
} rxcnt SEC(".maps");
static int xdp_count(struct xdp_md *xdp, __u32 key)
{
void *data_end = (void *)(long)xdp->data_end;
void *data = (void *)(long)xdp->data;
struct ethhdr *eth = data;
__u64 *count;
if (data + sizeof(*eth) > data_end)
return XDP_DROP;
if (bpf_htons(eth->h_proto) == ETH_P_IP) {
/* We only count IPv4 packets */
count = bpf_map_lookup_elem(&rxcnt, &key);
if (count)
*count += 1;
}
return XDP_PASS;
}
SEC("xdp")
int xdp_count_0(struct xdp_md *xdp)
{
return xdp_count(xdp, 0);
}
SEC("xdp")
int xdp_count_1(struct xdp_md *xdp)
{
return xdp_count(xdp, 1);
}
SEC("xdp")
int xdp_count_2(struct xdp_md *xdp)
{
return xdp_count(xdp, 2);
}
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 2);
__type(key, __u32);
__type(value, __be64);
} rx_mac SEC(".maps");
static int store_mac(struct xdp_md *xdp, __u32 id)
{
void *data_end = (void *)(long)xdp->data_end;
void *data = (void *)(long)xdp->data;
struct ethhdr *eth = data;
Annotation
- Immediate include surface: `linux/if_ether.h`, `linux/bpf.h`, `bpf/bpf_helpers.h`, `bpf/bpf_endian.h`.
- Detected declarations: `function xdp_redirect_map_0`, `function xdp_redirect_map_1`, `function xdp_redirect_map_2`, `function xdp_count`, `function xdp_count_0`, `function xdp_count_1`, `function xdp_count_2`, `function store_mac`, `function store_mac_1`, `function store_mac_2`.
- Atlas domain: Support Tooling And Documentation / tools.
- 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.