tools/testing/selftests/bpf/progs/test_check_mtu.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/test_check_mtu.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/test_check_mtu.c- Extension
.c- Size
- 7321 bytes
- Lines
- 303
- 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/bpf.hbpf/bpf_helpers.hlinux/if_ether.hstddef.hstdint.herrno.h
Detected Declarations
function xdp_use_helper_basicfunction xdp_use_helperfunction xdp_exceed_mtufunction xdp_minus_deltafunction xdp_input_lenfunction xdp_input_len_exceedfunction tc_use_helperfunction tc_exceed_mtufunction tc_exceed_mtu_dafunction tc_minus_deltafunction tc_input_lenfunction tc_input_len_exceedfunction tc_chk_segs_flag
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Jesper Dangaard Brouer */
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <linux/if_ether.h>
#include <stddef.h>
#include <stdint.h>
#include <errno.h>
char _license[] SEC("license") = "GPL";
/* Userspace will update with MTU it can see on device */
volatile const int GLOBAL_USER_MTU;
volatile const __u32 GLOBAL_USER_IFINDEX;
/* BPF-prog will update these with MTU values it can see */
__u32 global_bpf_mtu_xdp = 0;
__u32 global_bpf_mtu_tc = 0;
SEC("xdp")
int xdp_use_helper_basic(struct xdp_md *ctx)
{
__u32 mtu_len = 0;
if (bpf_check_mtu(ctx, 0, &mtu_len, 0, 0))
return XDP_ABORTED;
return XDP_PASS;
}
SEC("xdp")
int xdp_use_helper(struct xdp_md *ctx)
{
int retval = XDP_PASS; /* Expected retval on successful test */
__u32 mtu_len = 0;
__u32 ifindex = 0;
int delta = 0;
/* When ifindex is zero, save net_device lookup and use ctx netdev */
if (GLOBAL_USER_IFINDEX > 0)
ifindex = GLOBAL_USER_IFINDEX;
if (bpf_check_mtu(ctx, ifindex, &mtu_len, delta, 0)) {
/* mtu_len is also valid when check fail */
retval = XDP_ABORTED;
goto out;
}
if (mtu_len != GLOBAL_USER_MTU)
retval = XDP_DROP;
out:
global_bpf_mtu_xdp = mtu_len;
return retval;
}
SEC("xdp")
int xdp_exceed_mtu(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
__u32 ifindex = GLOBAL_USER_IFINDEX;
__u32 data_len = data_end - data;
int retval = XDP_ABORTED; /* Fail */
__u32 mtu_len = 0;
int delta;
int err;
/* Exceed MTU with 1 via delta adjust */
delta = GLOBAL_USER_MTU - (data_len - ETH_HLEN) + 1;
err = bpf_check_mtu(ctx, ifindex, &mtu_len, delta, 0);
if (err) {
retval = XDP_PASS; /* Success in exceeding MTU check */
if (err != BPF_MTU_CHK_RET_FRAG_NEEDED)
retval = XDP_DROP;
}
global_bpf_mtu_xdp = mtu_len;
return retval;
}
SEC("xdp")
int xdp_minus_delta(struct xdp_md *ctx)
{
int retval = XDP_PASS; /* Expected retval on successful test */
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
Annotation
- Immediate include surface: `linux/bpf.h`, `bpf/bpf_helpers.h`, `linux/if_ether.h`, `stddef.h`, `stdint.h`, `errno.h`.
- Detected declarations: `function xdp_use_helper_basic`, `function xdp_use_helper`, `function xdp_exceed_mtu`, `function xdp_minus_delta`, `function xdp_input_len`, `function xdp_input_len_exceed`, `function tc_use_helper`, `function tc_exceed_mtu`, `function tc_exceed_mtu_da`, `function tc_minus_delta`.
- 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.