summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/ksock_wq.c
blob: 16a1873d132ede2835ded4ce2c384ae564d39cd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Isovalent */

#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include "bpf_experimental.h"
#include "bpf_tracing_net.h"
#include "errno.h"
#include "ksock_common.h"

struct ksock_wq_value {
	struct bpf_wq work;
};

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__uint(max_entries, 1);
	__type(key, u32);
	__type(value, struct ksock_wq_value);
} work_map SEC(".maps");

int create_err;
u32 callback_done;

static int ksock_wq_callback(void *map, int *key, void *value)
{
	struct bpf_ksock_create_opts opts = {
		.family = AF_INET,
		.type = SOCK_DGRAM,
		.protocol = IPPROTO_UDP,
	};
	struct bpf_ksock *ks;
	int err = 0;

	ks = bpf_ksock_create(&opts, sizeof(opts), &err);
	if (ks)
		bpf_ksock_release(ks);
	create_err = err;
	__sync_fetch_and_add(&callback_done, 1);
	return 0;
}

SEC("syscall")
int ksock_wq_start(void *ctx)
{
	struct ksock_wq_value *value;
	u32 key = 0;
	int err;

	value = bpf_map_lookup_elem(&work_map, &key);
	if (!value)
		return -ENOENT;
	err = bpf_wq_init(&value->work, &work_map, 0);
	if (err)
		return err;
	err = bpf_wq_set_callback(&value->work, ksock_wq_callback, 0);
	if (err)
		return err;
	return bpf_wq_start(&value->work, 0);
}

char __license[] SEC("license") = "GPL";