mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-05 16:54:27 +00:00

Add integer wrappers for convenient sockmap usage. While there, fix misaligned trailing slashes. Suggested-by: Jakub Sitnicki <jakub@cloudflare.com> Signed-off-by: Michal Luczaj <mhal@rbox.co> Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org> Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/r/20250515-selftests-sockmap-redir-v3-3-a1ea723f7e7e@rbox.co
83 lines
3.5 KiB
C
83 lines
3.5 KiB
C
#ifndef __SOCKMAP_HELPERS__
|
|
#define __SOCKMAP_HELPERS__
|
|
|
|
#include "socket_helpers.h"
|
|
|
|
#define MAX_TEST_NAME 80
|
|
|
|
#define u32(v) ((u32){(v)})
|
|
#define u64(v) ((u64){(v)})
|
|
|
|
#define __always_unused __attribute__((__unused__))
|
|
|
|
#define xbpf_map_delete_elem(fd, key) \
|
|
({ \
|
|
int __ret = bpf_map_delete_elem((fd), (key)); \
|
|
if (__ret < 0) \
|
|
FAIL_ERRNO("map_delete"); \
|
|
__ret; \
|
|
})
|
|
|
|
#define xbpf_map_lookup_elem(fd, key, val) \
|
|
({ \
|
|
int __ret = bpf_map_lookup_elem((fd), (key), (val)); \
|
|
if (__ret < 0) \
|
|
FAIL_ERRNO("map_lookup"); \
|
|
__ret; \
|
|
})
|
|
|
|
#define xbpf_map_update_elem(fd, key, val, flags) \
|
|
({ \
|
|
int __ret = bpf_map_update_elem((fd), (key), (val), (flags)); \
|
|
if (__ret < 0) \
|
|
FAIL_ERRNO("map_update"); \
|
|
__ret; \
|
|
})
|
|
|
|
#define xbpf_prog_attach(prog, target, type, flags) \
|
|
({ \
|
|
int __ret = \
|
|
bpf_prog_attach((prog), (target), (type), (flags)); \
|
|
if (__ret < 0) \
|
|
FAIL_ERRNO("prog_attach(" #type ")"); \
|
|
__ret; \
|
|
})
|
|
|
|
#define xbpf_prog_detach2(prog, target, type) \
|
|
({ \
|
|
int __ret = bpf_prog_detach2((prog), (target), (type)); \
|
|
if (__ret < 0) \
|
|
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; \
|
|
})
|
|
|
|
static inline int add_to_sockmap(int mapfd, int fd1, int fd2)
|
|
{
|
|
int err;
|
|
|
|
err = xbpf_map_update_elem(mapfd, &u32(0), &u64(fd1), BPF_NOEXIST);
|
|
if (err)
|
|
return err;
|
|
|
|
return xbpf_map_update_elem(mapfd, &u32(1), &u64(fd2), BPF_NOEXIST);
|
|
}
|
|
|
|
#endif // __SOCKMAP_HELPERS__
|