tools/testing/selftests/bpf/prog_tests/xdp_metadata.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/xdp_metadata.c- Extension
.c- Size
- 14861 bytes
- Lines
- 546
- 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
test_progs.hnetwork_helpers.hxdp_metadata.skel.hxdp_metadata2.skel.hxdp_metadata.hxsk.hbpf/btf.hlinux/errqueue.hlinux/if_link.hlinux/net_tstamp.hnetinet/udp.hsys/mman.hnet/if.hpoll.h
Detected Declarations
struct xskfunction open_xskfunction close_xskfunction generate_packetfunction generate_packet_inetfunction complete_txfunction refill_rxfunction verify_xsk_metadatafunction switch_ns_to_rxfunction switch_ns_to_txfunction test_xdp_metadata
Annotated Snippet
struct xsk {
void *umem_area;
struct xsk_umem *umem;
struct xsk_ring_prod fill;
struct xsk_ring_cons comp;
struct xsk_ring_prod tx;
struct xsk_ring_cons rx;
struct xsk_socket *socket;
};
static int open_xsk(int ifindex, struct xsk *xsk)
{
int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
const struct xsk_socket_config socket_config = {
.rx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
.bind_flags = XDP_COPY,
};
const struct xsk_umem_config umem_config = {
.fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
.comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
.frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE,
.flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG | XDP_UMEM_TX_SW_CSUM |
XDP_UMEM_TX_METADATA_LEN,
.tx_metadata_len = sizeof(struct xsk_tx_metadata),
};
__u32 idx;
u64 addr;
int ret;
int i;
xsk->umem_area = mmap(NULL, UMEM_SIZE, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
if (!ASSERT_NEQ(xsk->umem_area, MAP_FAILED, "mmap"))
return -1;
ret = xsk_umem__create(&xsk->umem,
xsk->umem_area, UMEM_SIZE,
&xsk->fill,
&xsk->comp,
&umem_config);
if (!ASSERT_OK(ret, "xsk_umem__create"))
return ret;
ret = xsk_socket__create(&xsk->socket, ifindex, QUEUE_ID,
xsk->umem,
&xsk->rx,
&xsk->tx,
&socket_config);
if (!ASSERT_OK(ret, "xsk_socket__create"))
return ret;
/* First half of umem is for TX. This way address matches 1-to-1
* to the completion queue index.
*/
for (i = 0; i < UMEM_NUM / 2; i++) {
addr = i * UMEM_FRAME_SIZE;
printf("%p: tx_desc[%d] -> %lx\n", xsk, i, addr);
}
/* Second half of umem is for RX. */
ret = xsk_ring_prod__reserve(&xsk->fill, UMEM_NUM / 2, &idx);
if (!ASSERT_EQ(UMEM_NUM / 2, ret, "xsk_ring_prod__reserve"))
return ret;
if (!ASSERT_EQ(idx, 0, "fill idx != 0"))
return -1;
for (i = 0; i < UMEM_NUM / 2; i++) {
addr = (UMEM_NUM / 2 + i) * UMEM_FRAME_SIZE;
printf("%p: rx_desc[%d] -> %lx\n", xsk, i, addr);
*xsk_ring_prod__fill_addr(&xsk->fill, i) = addr;
}
xsk_ring_prod__submit(&xsk->fill, ret);
return 0;
}
static void close_xsk(struct xsk *xsk)
{
if (xsk->socket)
xsk_socket__delete(xsk->socket);
if (xsk->umem)
xsk_umem__delete(xsk->umem);
munmap(xsk->umem_area, UMEM_SIZE);
}
static int generate_packet(struct xsk *xsk, __u16 dst_port)
{
struct xsk_tx_metadata *meta;
Annotation
- Immediate include surface: `test_progs.h`, `network_helpers.h`, `xdp_metadata.skel.h`, `xdp_metadata2.skel.h`, `xdp_metadata.h`, `xsk.h`, `bpf/btf.h`, `linux/errqueue.h`.
- Detected declarations: `struct xsk`, `function open_xsk`, `function close_xsk`, `function generate_packet`, `function generate_packet_inet`, `function complete_tx`, `function refill_rx`, `function verify_xsk_metadata`, `function switch_ns_to_rx`, `function switch_ns_to_tx`.
- 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.