tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c
Extension
.c
Size
3573 bytes
Lines
188
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.

Dependency Surface

Detected Declarations

Annotated Snippet

struct sock {
} __attribute__((preserve_access_index));

struct bpf_iter__sockmap {
	union {
		struct sock *sk;
	};
} __attribute__((preserve_access_index));

struct {
	__uint(type, BPF_MAP_TYPE_SOCKHASH);
	__uint(max_entries, 1);
	__type(key, int);
	__type(value, int);
} sockhash SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_SOCKMAP);
	__uint(max_entries, 1);
	__type(key, int);
	__type(value, int);
} sockmap SEC(".maps");

enum { CG_OK = 1 };

int zero = 0;

static __always_inline void test_sockmap_delete(void)
{
	bpf_map_delete_elem(&sockmap, &zero);
	bpf_map_delete_elem(&sockhash, &zero);
}

static __always_inline void test_sockmap_update(void *sk)
{
	if (sk) {
		bpf_map_update_elem(&sockmap, &zero, sk, BPF_ANY);
		bpf_map_update_elem(&sockhash, &zero, sk, BPF_ANY);
	}
}

static __always_inline void test_sockmap_lookup_and_update(void)
{
	struct bpf_sock *sk = bpf_map_lookup_elem(&sockmap, &zero);

	if (sk) {
		test_sockmap_update(sk);
		bpf_sk_release(sk);
	}
}

static __always_inline void test_sockmap_mutate(void *sk)
{
	test_sockmap_delete();
	test_sockmap_update(sk);
}

static __always_inline void test_sockmap_lookup_and_mutate(void)
{
	test_sockmap_delete();
	test_sockmap_lookup_and_update();
}

SEC("action")
__success
int test_sched_act(struct __sk_buff *skb)
{
	test_sockmap_mutate(skb->sk);
	return 0;
}

SEC("classifier")
__success
int test_sched_cls(struct __sk_buff *skb)
{
	test_sockmap_mutate(skb->sk);
	return 0;
}

SEC("flow_dissector")
__success
int test_flow_dissector_delete(struct __sk_buff *skb __always_unused)
{
	test_sockmap_delete();
	return 0;
}

SEC("flow_dissector")
__failure __msg("program of this type cannot use helper bpf_sk_release")
int test_flow_dissector_update(struct __sk_buff *skb __always_unused)

Annotation

Implementation Notes