tools/testing/selftests/bpf/progs/test_cls_redirect_dynptr.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/test_cls_redirect_dynptr.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/test_cls_redirect_dynptr.c- Extension
.c- Size
- 25559 bytes
- Lines
- 982
- 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
stdbool.hstddef.hstdint.hstring.hlinux/bpf.hlinux/icmp.hlinux/icmpv6.hlinux/if_ether.hlinux/in.hlinux/ip.hlinux/ipv6.hlinux/pkt_cls.hlinux/tcp.hnetinet/udp.hbpf/bpf_helpers.hbpf/bpf_endian.htest_cls_redirect.hbpf_kfuncs.h
Detected Declarations
struct iphdr_infofunction ipv4_is_fragmentfunction pkt_parse_ipv4function pkt_parse_icmp_l4_portsfunction pkt_checksum_foldfunction pkt_ipv4_checksumfunction pkt_skip_ipv6_extension_headersfunction pkt_parse_ipv6function accept_locallyfunction forward_with_grefunction neededfunction forward_to_next_hopfunction skip_next_hopsfunction get_next_hopfunction fill_tuplefunction classify_tcpfunction classify_udpfunction classify_icmpfunction process_icmpv4function process_icmpv6function process_tcpfunction process_udpfunction process_ipv4function process_ipv6function cls_redirect
Annotated Snippet
struct iphdr_info {
void *hdr;
__u64 len;
};
typedef int ret_t;
/* This is a bit of a hack. We need a return value which allows us to
* indicate that the regular flow of the program should continue,
* while allowing functions to use XDP_PASS and XDP_DROP, etc.
*/
static const ret_t CONTINUE_PROCESSING = -1;
/* Convenience macro to call functions which return ret_t.
*/
#define MAYBE_RETURN(x) \
do { \
ret_t __ret = x; \
if (__ret != CONTINUE_PROCESSING) \
return __ret; \
} while (0)
static bool ipv4_is_fragment(const struct iphdr *ip)
{
uint16_t frag_off = ip->frag_off & bpf_htons(IP_OFFSET_MASK);
return (ip->frag_off & bpf_htons(IP_MF)) != 0 || frag_off > 0;
}
static int pkt_parse_ipv4(struct bpf_dynptr *dynptr, __u64 *offset, struct iphdr *iphdr)
{
if (bpf_dynptr_read(iphdr, sizeof(*iphdr), dynptr, *offset, 0))
return -1;
*offset += sizeof(*iphdr);
if (iphdr->ihl < 5)
return -1;
/* skip ipv4 options */
*offset += (iphdr->ihl - 5) * 4;
return 0;
}
/* Parse the L4 ports from a packet, assuming a layout like TCP or UDP. */
static bool pkt_parse_icmp_l4_ports(struct bpf_dynptr *dynptr, __u64 *offset, flow_ports_t *ports)
{
if (bpf_dynptr_read(ports, sizeof(*ports), dynptr, *offset, 0))
return false;
*offset += sizeof(*ports);
/* Ports in the L4 headers are reversed, since we are parsing an ICMP
* payload which is going towards the eyeball.
*/
uint16_t dst = ports->src;
ports->src = ports->dst;
ports->dst = dst;
return true;
}
static uint16_t pkt_checksum_fold(uint32_t csum)
{
/* The highest reasonable value for an IPv4 header
* checksum requires two folds, so we just do that always.
*/
csum = (csum & 0xffff) + (csum >> 16);
csum = (csum & 0xffff) + (csum >> 16);
return (uint16_t)~csum;
}
static void pkt_ipv4_checksum(struct iphdr *iph)
{
iph->check = 0;
/* An IP header without options is 20 bytes. Two of those
* are the checksum, which we always set to zero. Hence,
* the maximum accumulated value is 18 / 2 * 0xffff = 0x8fff7,
* which fits in 32 bit.
*/
_Static_assert(sizeof(struct iphdr) == 20, "iphdr must be 20 bytes");
uint32_t acc = 0;
uint16_t *ipw = (uint16_t *)iph;
for (size_t i = 0; i < sizeof(struct iphdr) / 2; i++)
acc += ipw[i];
iph->check = pkt_checksum_fold(acc);
}
Annotation
- Immediate include surface: `stdbool.h`, `stddef.h`, `stdint.h`, `string.h`, `linux/bpf.h`, `linux/icmp.h`, `linux/icmpv6.h`, `linux/if_ether.h`.
- Detected declarations: `struct iphdr_info`, `function ipv4_is_fragment`, `function pkt_parse_ipv4`, `function pkt_parse_icmp_l4_ports`, `function pkt_checksum_fold`, `function pkt_ipv4_checksum`, `function pkt_skip_ipv6_extension_headers`, `function pkt_parse_ipv6`, `function accept_locally`, `function forward_with_gre`.
- 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.