Skip to content

Commit b337031

Browse files
pchaignoKernel Patches Daemon
authored andcommitted
selftests/bpf: Move macros to bpf_misc.h
Move the sizeof_field and offsetofend macros from individual test files to the common bpf_misc.h to avoid duplication. Signed-off-by: Paul Chaignon <[email protected]> Acked-by: Eduard Zingerman <[email protected]>
1 parent bfa64de commit b337031

File tree

5 files changed

+6
-13
lines changed

5 files changed

+6
-13
lines changed

tools/testing/selftests/bpf/progs/bpf_misc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@
156156
#define __imm_ptr(name) [name]"r"(&name)
157157
#define __imm_insn(name, expr) [name]"i"(*(long *)&(expr))
158158

159+
#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
160+
#define offsetofend(TYPE, MEMBER) \
161+
(offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
162+
159163
/* Magic constants used with __retval() */
160164
#define POINTER_VALUE 0xbadcafe
161165
#define TEST_DATA_LEN 64

tools/testing/selftests/bpf/progs/test_cls_redirect.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "bpf_compiler.h"
2424
#include "test_cls_redirect.h"
25+
#include "bpf_misc.h"
2526

2627
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
2728

@@ -31,9 +32,6 @@
3132
#define INLINING __always_inline
3233
#endif
3334

34-
#define offsetofend(TYPE, MEMBER) \
35-
(offsetof(TYPE, MEMBER) + sizeof((((TYPE *)0)->MEMBER)))
36-
3735
#define IP_OFFSET_MASK (0x1FFF)
3836
#define IP_MF (0x2000)
3937

tools/testing/selftests/bpf/progs/test_tcp_hdr_options.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
#include <bpf/bpf_endian.h>
1515
#define BPF_PROG_TEST_TCP_HDR_OPTIONS
1616
#include "test_tcp_hdr_options.h"
17-
18-
#ifndef sizeof_field
19-
#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
20-
#endif
17+
#include "bpf_misc.h"
2118

2219
__u8 test_kind = TCPOPT_EXP;
2320
__u16 test_magic = 0xeB9F;

tools/testing/selftests/bpf/progs/verifier_ctx.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <bpf/bpf_helpers.h>
66
#include "bpf_misc.h"
77

8-
#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
9-
108
SEC("tc")
119
__description("context stores via BPF_ATOMIC")
1210
__failure __msg("BPF_ATOMIC stores into R1 ctx is not allowed")

tools/testing/selftests/bpf/progs/verifier_sock.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
#include <bpf/bpf_helpers.h>
66
#include "bpf_misc.h"
77

8-
#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
9-
#define offsetofend(TYPE, MEMBER) \
10-
(offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
11-
128
struct {
139
__uint(type, BPF_MAP_TYPE_REUSEPORT_SOCKARRAY);
1410
__uint(max_entries, 1);

0 commit comments

Comments
 (0)