xref: /linux/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h (revision 1623bc27a85a93e82194c8d077eccc464efa67db)
1 #ifndef __SOCKMAP_HELPERS__
2 #define __SOCKMAP_HELPERS__
3 
4 #include "socket_helpers.h"
5 
6 #define MAX_TEST_NAME 80
7 
8 #define __always_unused	__attribute__((__unused__))
9 
10 #define xbpf_map_delete_elem(fd, key)                                          \
11 	({                                                                     \
12 		int __ret = bpf_map_delete_elem((fd), (key));                  \
13 		if (__ret < 0)                                               \
14 			FAIL_ERRNO("map_delete");                              \
15 		__ret;                                                         \
16 	})
17 
18 #define xbpf_map_lookup_elem(fd, key, val)                                     \
19 	({                                                                     \
20 		int __ret = bpf_map_lookup_elem((fd), (key), (val));           \
21 		if (__ret < 0)                                               \
22 			FAIL_ERRNO("map_lookup");                              \
23 		__ret;                                                         \
24 	})
25 
26 #define xbpf_map_update_elem(fd, key, val, flags)                              \
27 	({                                                                     \
28 		int __ret = bpf_map_update_elem((fd), (key), (val), (flags));  \
29 		if (__ret < 0)                                               \
30 			FAIL_ERRNO("map_update");                              \
31 		__ret;                                                         \
32 	})
33 
34 #define xbpf_prog_attach(prog, target, type, flags)                            \
35 	({                                                                     \
36 		int __ret =                                                    \
37 			bpf_prog_attach((prog), (target), (type), (flags));    \
38 		if (__ret < 0)                                               \
39 			FAIL_ERRNO("prog_attach(" #type ")");                  \
40 		__ret;                                                         \
41 	})
42 
43 #define xbpf_prog_detach2(prog, target, type)                                  \
44 	({                                                                     \
45 		int __ret = bpf_prog_detach2((prog), (target), (type));        \
46 		if (__ret < 0)                                               \
47 			FAIL_ERRNO("prog_detach2(" #type ")");                 \
48 		__ret;                                                         \
49 	})
50 
51 #define xpthread_create(thread, attr, func, arg)                               \
52 	({                                                                     \
53 		int __ret = pthread_create((thread), (attr), (func), (arg));   \
54 		errno = __ret;                                                 \
55 		if (__ret)                                                     \
56 			FAIL_ERRNO("pthread_create");                          \
57 		__ret;                                                         \
58 	})
59 
60 #define xpthread_join(thread, retval)                                          \
61 	({                                                                     \
62 		int __ret = pthread_join((thread), (retval));                  \
63 		errno = __ret;                                                 \
64 		if (__ret)                                                     \
65 			FAIL_ERRNO("pthread_join");                            \
66 		__ret;                                                         \
67 	})
68 
69 static inline int add_to_sockmap(int sock_mapfd, int fd1, int fd2)
70 {
71 	u64 value;
72 	u32 key;
73 	int err;
74 
75 	key = 0;
76 	value = fd1;
77 	err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
78 	if (err)
79 		return err;
80 
81 	key = 1;
82 	value = fd2;
83 	return xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
84 }
85 
86 #endif // __SOCKMAP_HELPERS__
87