Skip to content

Commit b57482b

Browse files
mmhalMartin KaFai Lau
authored andcommitted
selftests/bpf: Add u32()/u64() to sockmap_helpers
Add integer wrappers for convenient sockmap usage. While there, fix misaligned trailing slashes. Suggested-by: Jakub Sitnicki <[email protected]> Signed-off-by: Michal Luczaj <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Reviewed-by: Jakub Sitnicki <[email protected]> Acked-by: John Fastabend <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent d878579 commit b57482b

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,31 @@
55

66
#define MAX_TEST_NAME 80
77

8+
#define u32(v) ((u32){(v)})
9+
#define u64(v) ((u64){(v)})
10+
811
#define __always_unused __attribute__((__unused__))
912

1013
#define xbpf_map_delete_elem(fd, key) \
1114
({ \
1215
int __ret = bpf_map_delete_elem((fd), (key)); \
13-
if (__ret < 0) \
16+
if (__ret < 0) \
1417
FAIL_ERRNO("map_delete"); \
1518
__ret; \
1619
})
1720

1821
#define xbpf_map_lookup_elem(fd, key, val) \
1922
({ \
2023
int __ret = bpf_map_lookup_elem((fd), (key), (val)); \
21-
if (__ret < 0) \
24+
if (__ret < 0) \
2225
FAIL_ERRNO("map_lookup"); \
2326
__ret; \
2427
})
2528

2629
#define xbpf_map_update_elem(fd, key, val, flags) \
2730
({ \
2831
int __ret = bpf_map_update_elem((fd), (key), (val), (flags)); \
29-
if (__ret < 0) \
32+
if (__ret < 0) \
3033
FAIL_ERRNO("map_update"); \
3134
__ret; \
3235
})
@@ -35,15 +38,15 @@
3538
({ \
3639
int __ret = \
3740
bpf_prog_attach((prog), (target), (type), (flags)); \
38-
if (__ret < 0) \
41+
if (__ret < 0) \
3942
FAIL_ERRNO("prog_attach(" #type ")"); \
4043
__ret; \
4144
})
4245

4346
#define xbpf_prog_detach2(prog, target, type) \
4447
({ \
4548
int __ret = bpf_prog_detach2((prog), (target), (type)); \
46-
if (__ret < 0) \
49+
if (__ret < 0) \
4750
FAIL_ERRNO("prog_detach2(" #type ")"); \
4851
__ret; \
4952
})
@@ -66,21 +69,15 @@
6669
__ret; \
6770
})
6871

69-
static inline int add_to_sockmap(int sock_mapfd, int fd1, int fd2)
72+
static inline int add_to_sockmap(int mapfd, int fd1, int fd2)
7073
{
71-
u64 value;
72-
u32 key;
7374
int err;
7475

75-
key = 0;
76-
value = fd1;
77-
err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
76+
err = xbpf_map_update_elem(mapfd, &u32(0), &u64(fd1), BPF_NOEXIST);
7877
if (err)
7978
return err;
8079

81-
key = 1;
82-
value = fd2;
83-
return xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
80+
return xbpf_map_update_elem(mapfd, &u32(1), &u64(fd2), BPF_NOEXIST);
8481
}
8582

8683
#endif // __SOCKMAP_HELPERS__

0 commit comments

Comments
 (0)