tools/testing/selftests/bpf/progs/vrf_socket_lookup.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/vrf_socket_lookup.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/vrf_socket_lookup.c- Extension
.c- Size
- 1671 bytes
- Lines
- 90
- 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.hbpf/bpf_endian.hlinux/ip.hlinux/in.hlinux/if_ether.hlinux/pkt_cls.hstdbool.h
Detected Declarations
function socket_lookupfunction tc_socket_lookupfunction xdp_socket_lookup
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/if_ether.h>
#include <linux/pkt_cls.h>
#include <stdbool.h>
int lookup_status;
bool test_xdp;
bool tcp_skc;
#define CUR_NS BPF_F_CURRENT_NETNS
static void socket_lookup(void *ctx, void *data_end, void *data)
{
struct ethhdr *eth = data;
struct bpf_sock_tuple *tp;
struct bpf_sock *sk;
struct iphdr *iph;
int tplen;
if (eth + 1 > data_end)
return;
if (eth->h_proto != bpf_htons(ETH_P_IP))
return;
iph = (struct iphdr *)(eth + 1);
if (iph + 1 > data_end)
return;
tp = (struct bpf_sock_tuple *)&iph->saddr;
tplen = sizeof(tp->ipv4);
if ((void *)tp + tplen > data_end)
return;
switch (iph->protocol) {
case IPPROTO_TCP:
if (tcp_skc)
sk = bpf_skc_lookup_tcp(ctx, tp, tplen, CUR_NS, 0);
else
sk = bpf_sk_lookup_tcp(ctx, tp, tplen, CUR_NS, 0);
break;
case IPPROTO_UDP:
sk = bpf_sk_lookup_udp(ctx, tp, tplen, CUR_NS, 0);
break;
default:
return;
}
lookup_status = 0;
if (sk) {
bpf_sk_release(sk);
lookup_status = 1;
}
}
SEC("tc")
int tc_socket_lookup(struct __sk_buff *skb)
{
void *data_end = (void *)(long)skb->data_end;
void *data = (void *)(long)skb->data;
if (test_xdp)
return TC_ACT_UNSPEC;
socket_lookup(skb, data_end, data);
return TC_ACT_UNSPEC;
}
SEC("xdp")
int xdp_socket_lookup(struct xdp_md *xdp)
{
void *data_end = (void *)(long)xdp->data_end;
void *data = (void *)(long)xdp->data;
if (!test_xdp)
return XDP_PASS;
socket_lookup(xdp, data_end, data);
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";
Annotation
- Immediate include surface: `linux/bpf.h`, `bpf/bpf_helpers.h`, `bpf/bpf_endian.h`, `linux/ip.h`, `linux/in.h`, `linux/if_ether.h`, `linux/pkt_cls.h`, `stdbool.h`.
- Detected declarations: `function socket_lookup`, `function tc_socket_lookup`, `function xdp_socket_lookup`.
- 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.