xref: /linux/tools/testing/selftests/bpf/progs/ksock_wq.c (revision 5a8cd539ac19f7a68e68e1d25ef9ca2ff55b8500)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2026 Isovalent */
3 
4 #include "vmlinux.h"
5 #include <bpf/bpf_helpers.h>
6 #include "bpf_experimental.h"
7 #include "bpf_tracing_net.h"
8 #include "errno.h"
9 #include "ksock_common.h"
10 
11 struct ksock_wq_value {
12 	struct bpf_wq work;
13 };
14 
15 struct {
16 	__uint(type, BPF_MAP_TYPE_ARRAY);
17 	__uint(max_entries, 1);
18 	__type(key, u32);
19 	__type(value, struct ksock_wq_value);
20 } work_map SEC(".maps");
21 
22 int create_err;
23 u32 callback_done;
24 
ksock_wq_callback(void * map,int * key,void * value)25 static int ksock_wq_callback(void *map, int *key, void *value)
26 {
27 	struct bpf_ksock_create_opts opts = {
28 		.family = AF_INET,
29 		.type = SOCK_DGRAM,
30 		.protocol = IPPROTO_UDP,
31 	};
32 	struct bpf_ksock *ks;
33 	int err = 0;
34 
35 	ks = bpf_ksock_create(&opts, sizeof(opts), &err);
36 	if (ks)
37 		bpf_ksock_release(ks);
38 	create_err = err;
39 	__sync_fetch_and_add(&callback_done, 1);
40 	return 0;
41 }
42 
43 SEC("syscall")
ksock_wq_start(void * ctx)44 int ksock_wq_start(void *ctx)
45 {
46 	struct ksock_wq_value *value;
47 	u32 key = 0;
48 	int err;
49 
50 	value = bpf_map_lookup_elem(&work_map, &key);
51 	if (!value)
52 		return -ENOENT;
53 	err = bpf_wq_init(&value->work, &work_map, 0);
54 	if (err)
55 		return err;
56 	err = bpf_wq_set_callback(&value->work, ksock_wq_callback, 0);
57 	if (err)
58 		return err;
59 	return bpf_wq_start(&value->work, 0);
60 }
61 
62 char __license[] SEC("license") = "GPL";
63