samples/bpf/cookie_uid_helper_example.c
Source file repositories/reference/linux-study-clean/samples/bpf/cookie_uid_helper_example.c
File Facts
- System
- Linux kernel
- Corpus path
samples/bpf/cookie_uid_helper_example.c- Extension
.c- Size
- 9643 bytes
- Lines
- 333
- Domain
- Support Tooling And Documentation
- Bucket
- samples
- 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
arpa/inet.herrno.herror.hlimits.hlinux/bpf.hlinux/if_ether.hnet/if.hsignal.hstdbool.hstdint.hstdio.hstdlib.hstring.hsys/socket.hsys/stat.hsys/types.hunistd.hbpf/bpf.hbpf_insn.h
Detected Declarations
struct statsfunction maps_createfunction prog_loadfunction prog_attach_iptablesfunction print_tablefunction udp_clientfunction usagefunction finishfunction main
Annotated Snippet
struct stats {
uint32_t uid;
uint64_t packets;
uint64_t bytes;
};
static int map_fd, prog_fd;
static bool test_finish;
static void maps_create(void)
{
map_fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(uint32_t),
sizeof(struct stats), 100, NULL);
if (map_fd < 0)
error(1, errno, "map create failed!\n");
}
static void prog_load(void)
{
static char log_buf[1 << 16];
struct bpf_insn prog[] = {
/*
* Save sk_buff for future usage. value stored in R6 to R10 will
* not be reset after a bpf helper function call.
*/
BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
/*
* pc1: BPF_FUNC_get_socket_cookie takes one parameter,
* R1: sk_buff
*/
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
BPF_FUNC_get_socket_cookie),
/* pc2-4: save &socketCookie to r7 for future usage*/
BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
BPF_MOV64_REG(BPF_REG_7, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, -8),
/*
* pc5-8: set up the registers for BPF_FUNC_map_lookup_elem,
* it takes two parameters (R1: map_fd, R2: &socket_cookie)
*/
BPF_LD_MAP_FD(BPF_REG_1, map_fd),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
BPF_FUNC_map_lookup_elem),
/*
* pc9. if r0 != 0x0, go to pc+14, since we have the cookie
* stored already
* Otherwise do pc10-22 to setup a new data entry.
*/
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 14),
BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
BPF_FUNC_get_socket_uid),
/*
* Place a struct stats in the R10 stack and sequentially
* place the member value into the memory. Packets value
* is set by directly place a IMM value 1 into the stack.
*/
BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0,
-32 + (__s16)offsetof(struct stats, uid)),
BPF_ST_MEM(BPF_DW, BPF_REG_10,
-32 + (__s16)offsetof(struct stats, packets), 1),
/*
* __sk_buff is a special struct used for eBPF program to
* directly access some sk_buff field.
*/
BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_6,
offsetof(struct __sk_buff, len)),
BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1,
-32 + (__s16)offsetof(struct stats, bytes)),
/*
* add new map entry using BPF_FUNC_map_update_elem, it takes
* 4 parameters (R1: map_fd, R2: &socket_cookie, R3: &stats,
* R4: flags)
*/
BPF_LD_MAP_FD(BPF_REG_1, map_fd),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
BPF_MOV64_REG(BPF_REG_3, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, -32),
BPF_MOV64_IMM(BPF_REG_4, 0),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
BPF_FUNC_map_update_elem),
BPF_JMP_IMM(BPF_JA, 0, 0, 5),
/*
* pc24-30 update the packet info to a exist data entry, it can
* be done by directly write to pointers instead of using
* BPF_FUNC_map_update_elem helper function
*/
Annotation
- Immediate include surface: `arpa/inet.h`, `errno.h`, `error.h`, `limits.h`, `linux/bpf.h`, `linux/if_ether.h`, `net/if.h`, `signal.h`.
- Detected declarations: `struct stats`, `function maps_create`, `function prog_load`, `function prog_attach_iptables`, `function print_table`, `function udp_client`, `function usage`, `function finish`, `function main`.
- Atlas domain: Support Tooling And Documentation / samples.
- 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.