tools/testing/selftests/bpf/progs/type_cast.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/type_cast.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/type_cast.c- Extension
.c- Size
- 1904 bytes
- Lines
- 80
- 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.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
vmlinux.hbpf/bpf_helpers.hbpf/bpf_tracing.hbpf/bpf_core_read.hbpf_kfuncs.h
Detected Declarations
function md_xdpfunction md_skbfunction BPF_PROGfunction kctx_u64
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "bpf_kfuncs.h"
struct {
__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
__uint(map_flags, BPF_F_NO_PREALLOC);
__type(key, int);
__type(value, long);
} enter_id SEC(".maps");
#define IFNAMSIZ 16
int ifindex, ingress_ifindex;
char name[IFNAMSIZ];
unsigned int inum;
unsigned int meta_len, frag0_len, kskb_len, kskb2_len;
SEC("?xdp")
int md_xdp(struct xdp_md *ctx)
{
struct xdp_buff *kctx = bpf_cast_to_kern_ctx(ctx);
struct net_device *dev;
dev = kctx->rxq->dev;
ifindex = dev->ifindex;
inum = dev->nd_net.net->ns.inum;
__builtin_memcpy(name, dev->name, IFNAMSIZ);
ingress_ifindex = ctx->ingress_ifindex;
return XDP_PASS;
}
SEC("?tc")
int md_skb(struct __sk_buff *skb)
{
struct sk_buff *kskb = bpf_cast_to_kern_ctx(skb);
struct skb_shared_info *shared_info;
struct sk_buff *kskb2;
kskb_len = kskb->len;
/* Simulate the following kernel macro:
* #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
*/
shared_info = bpf_core_cast(kskb->head + kskb->end, struct skb_shared_info);
meta_len = shared_info->meta_len;
frag0_len = shared_info->frag_list->len;
/* kskb2 should be equal to kskb */
kskb2 = bpf_core_cast(kskb, typeof(*kskb2));
kskb2_len = kskb2->len;
return 0;
}
SEC("?tp_btf/sys_enter")
int BPF_PROG(untrusted_ptr, struct pt_regs *regs, long id)
{
struct task_struct *task, *task_dup;
task = bpf_get_current_task_btf();
task_dup = bpf_core_cast(task, struct task_struct);
(void)bpf_task_storage_get(&enter_id, task_dup, 0, 0);
return 0;
}
SEC("?tracepoint/syscalls/sys_enter_nanosleep")
int kctx_u64(void *ctx)
{
u64 *kctx = bpf_core_cast(ctx, u64);
(void)kctx;
return 0;
}
char _license[] SEC("license") = "GPL";
Annotation
- Immediate include surface: `vmlinux.h`, `bpf/bpf_helpers.h`, `bpf/bpf_tracing.h`, `bpf/bpf_core_read.h`, `bpf_kfuncs.h`.
- Detected declarations: `function md_xdp`, `function md_skb`, `function BPF_PROG`, `function kctx_u64`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
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.