2023-05-22 19:56:13 -07:00
|
|
|
#ifndef __SOCKMAP_HELPERS__
|
|
|
|
#define __SOCKMAP_HELPERS__
|
|
|
|
|
2024-12-12 19:40:56 -08:00
|
|
|
#include "socket_helpers.h"
|
2023-05-22 19:56:13 -07:00
|
|
|
|
|
|
|
#define MAX_TEST_NAME 80
|
|
|
|
|
2025-05-15 00:15:26 +02:00
|
|
|
#define u32(v) ((u32){(v)})
|
|
|
|
#define u64(v) ((u64){(v)})
|
|
|
|
|
2023-05-22 19:56:13 -07:00
|
|
|
#define __always_unused __attribute__((__unused__))
|
|
|
|
|
|
|
|
#define xbpf_map_delete_elem(fd, key) \
|
|
|
|
({ \
|
|
|
|
int __ret = bpf_map_delete_elem((fd), (key)); \
|
2025-05-15 00:15:26 +02:00
|
|
|
if (__ret < 0) \
|
2023-05-22 19:56:13 -07:00
|
|
|
FAIL_ERRNO("map_delete"); \
|
|
|
|
__ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define xbpf_map_lookup_elem(fd, key, val) \
|
|
|
|
({ \
|
|
|
|
int __ret = bpf_map_lookup_elem((fd), (key), (val)); \
|
2025-05-15 00:15:26 +02:00
|
|
|
if (__ret < 0) \
|
2023-05-22 19:56:13 -07:00
|
|
|
FAIL_ERRNO("map_lookup"); \
|
|
|
|
__ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define xbpf_map_update_elem(fd, key, val, flags) \
|
|
|
|
({ \
|
|
|
|
int __ret = bpf_map_update_elem((fd), (key), (val), (flags)); \
|
2025-05-15 00:15:26 +02:00
|
|
|
if (__ret < 0) \
|
2023-05-22 19:56:13 -07:00
|
|
|
FAIL_ERRNO("map_update"); \
|
|
|
|
__ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define xbpf_prog_attach(prog, target, type, flags) \
|
|
|
|
({ \
|
|
|
|
int __ret = \
|
|
|
|
bpf_prog_attach((prog), (target), (type), (flags)); \
|
2025-05-15 00:15:26 +02:00
|
|
|
if (__ret < 0) \
|
2023-05-22 19:56:13 -07:00
|
|
|
FAIL_ERRNO("prog_attach(" #type ")"); \
|
|
|
|
__ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define xbpf_prog_detach2(prog, target, type) \
|
|
|
|
({ \
|
|
|
|
int __ret = bpf_prog_detach2((prog), (target), (type)); \
|
2025-05-15 00:15:26 +02:00
|
|
|
if (__ret < 0) \
|
2023-05-22 19:56:13 -07:00
|
|
|
FAIL_ERRNO("prog_detach2(" #type ")"); \
|
|
|
|
__ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define xpthread_create(thread, attr, func, arg) \
|
|
|
|
({ \
|
|
|
|
int __ret = pthread_create((thread), (attr), (func), (arg)); \
|
|
|
|
errno = __ret; \
|
|
|
|
if (__ret) \
|
|
|
|
FAIL_ERRNO("pthread_create"); \
|
|
|
|
__ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define xpthread_join(thread, retval) \
|
|
|
|
({ \
|
|
|
|
int __ret = pthread_join((thread), (retval)); \
|
|
|
|
errno = __ret; \
|
|
|
|
if (__ret) \
|
|
|
|
FAIL_ERRNO("pthread_join"); \
|
|
|
|
__ret; \
|
|
|
|
})
|
2023-09-01 11:10:37 +08:00
|
|
|
|
2025-05-15 00:15:26 +02:00
|
|
|
static inline int add_to_sockmap(int mapfd, int fd1, int fd2)
|
2023-05-22 19:56:14 -07:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2025-05-15 00:15:26 +02:00
|
|
|
err = xbpf_map_update_elem(mapfd, &u32(0), &u64(fd1), BPF_NOEXIST);
|
2023-05-22 19:56:14 -07:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2025-05-15 00:15:26 +02:00
|
|
|
return xbpf_map_update_elem(mapfd, &u32(1), &u64(fd2), BPF_NOEXIST);
|
2023-05-22 19:56:14 -07:00
|
|
|
}
|
|
|
|
|
2023-05-22 19:56:13 -07:00
|
|
|
#endif // __SOCKMAP_HELPERS__
|