tools/testing/selftests/bpf/progs/test_sk_lookup.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/test_sk_lookup.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/test_sk_lookup.c- Extension
.c- Size
- 18993 bytes
- Lines
- 661
- 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
errno.hstdbool.hstddef.hlinux/bpf.hlinux/in.hsys/socket.hbpf/bpf_endian.hbpf/bpf_helpers.h
Detected Declarations
function lookup_passfunction lookup_dropfunction check_ifindexfunction reuseport_passfunction reuseport_dropfunction redir_portfunction redir_ip4function redir_ip6function select_sock_afunction select_sock_a_no_reuseportfunction select_sock_bfunction sk_assign_eexistfunction sk_assign_replace_flagfunction sk_assign_nullfunction access_ctx_skfunction ctx_narrow_accessfunction sk_assign_esocknosupportfunction multi_prog_pass1function multi_prog_pass2function multi_prog_drop1function multi_prog_drop2function select_server_afunction multi_prog_redir1function multi_prog_redir2
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
// Copyright (c) 2020 Cloudflare
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <linux/bpf.h>
#include <linux/in.h>
#include <sys/socket.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>
#define IP4(a, b, c, d) \
bpf_htonl((((__u32)(a) & 0xffU) << 24) | \
(((__u32)(b) & 0xffU) << 16) | \
(((__u32)(c) & 0xffU) << 8) | \
(((__u32)(d) & 0xffU) << 0))
#define IP6(aaaa, bbbb, cccc, dddd) \
{ bpf_htonl(aaaa), bpf_htonl(bbbb), bpf_htonl(cccc), bpf_htonl(dddd) }
/* Macros for least-significant byte and word accesses. */
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define LSE_INDEX(index, size) (index)
#else
#define LSE_INDEX(index, size) ((size) - (index) - 1)
#endif
#define LSB(value, index) \
(((__u8 *)&(value))[LSE_INDEX((index), sizeof(value))])
#define LSW(value, index) \
(((__u16 *)&(value))[LSE_INDEX((index), sizeof(value) / 2)])
#define MAX_SOCKS 32
struct {
__uint(type, BPF_MAP_TYPE_SOCKMAP);
__uint(max_entries, MAX_SOCKS);
__type(key, __u32);
__type(value, __u64);
} redir_map SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 2);
__type(key, int);
__type(value, int);
} run_map SEC(".maps");
enum {
PROG1 = 0,
PROG2,
};
enum {
SERVER_A = 0,
SERVER_B,
};
/* Addressable key/value constants for convenience */
static const int KEY_PROG1 = PROG1;
static const int KEY_PROG2 = PROG2;
static const int PROG_DONE = 1;
static const __u32 KEY_SERVER_A = SERVER_A;
static const __u32 KEY_SERVER_B = SERVER_B;
static const __u16 SRC_PORT = bpf_htons(8008);
static const __u32 SRC_IP4 = IP4(127, 0, 0, 2);
static const __u32 SRC_IP6[] = IP6(0xfd000000, 0x0, 0x0, 0x00000002);
static const __u16 DST_PORT = 7007; /* Host byte order */
static const __u32 DST_IP4 = IP4(127, 0, 0, 1);
static const __u32 DST_IP6[] = IP6(0xfd000000, 0x0, 0x0, 0x00000001);
SEC("sk_lookup")
int lookup_pass(struct bpf_sk_lookup *ctx)
{
return SK_PASS;
}
SEC("sk_lookup")
int lookup_drop(struct bpf_sk_lookup *ctx)
{
return SK_DROP;
}
SEC("sk_lookup")
int check_ifindex(struct bpf_sk_lookup *ctx)
{
if (ctx->ingress_ifindex == 1)
Annotation
- Immediate include surface: `errno.h`, `stdbool.h`, `stddef.h`, `linux/bpf.h`, `linux/in.h`, `sys/socket.h`, `bpf/bpf_endian.h`, `bpf/bpf_helpers.h`.
- Detected declarations: `function lookup_pass`, `function lookup_drop`, `function check_ifindex`, `function reuseport_pass`, `function reuseport_drop`, `function redir_port`, `function redir_ip4`, `function redir_ip6`, `function select_sock_a`, `function select_sock_a_no_reuseport`.
- 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.