Skip to content

Commit 57a73fe

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf, selftests: Make redirect_neigh test more extensible
Rename into test_tc_redirect.sh and move setup and test code into separate functions so they can be reused for newly added tests in here. Also remove the crude hack to override ifindex inside the object file via xxd and sed and just use a simple map instead. Map given iproute2 does not support BTF fully and therefore neither global data at this point. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/20201010234006.7075-6-daniel@iogearbox.net
1 parent 6775dab commit 57a73fe

File tree

3 files changed

+219
-186
lines changed

3 files changed

+219
-186
lines changed

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,10 @@
1313
#include <bpf/bpf_helpers.h>
1414
#include <bpf/bpf_endian.h>
1515

16-
#ifndef barrier_data
17-
# define barrier_data(ptr) asm volatile("": :"r"(ptr) :"memory")
18-
#endif
19-
2016
#ifndef ctx_ptr
2117
# define ctx_ptr(field) (void *)(long)(field)
2218
#endif
2319

24-
#define dst_to_src_tmp 0xeeddddeeU
25-
#define src_to_dst_tmp 0xeeffffeeU
26-
2720
#define ip4_src 0xac100164 /* 172.16.1.100 */
2821
#define ip4_dst 0xac100264 /* 172.16.2.100 */
2922

@@ -39,6 +32,18 @@
3932
a.s6_addr32[3] == b.s6_addr32[3])
4033
#endif
4134

35+
enum {
36+
dev_src,
37+
dev_dst,
38+
};
39+
40+
struct bpf_map_def SEC("maps") ifindex_map = {
41+
.type = BPF_MAP_TYPE_ARRAY,
42+
.key_size = sizeof(int),
43+
.value_size = sizeof(int),
44+
.max_entries = 2,
45+
};
46+
4247
static __always_inline bool is_remote_ep_v4(struct __sk_buff *skb,
4348
__be32 addr)
4449
{
@@ -73,7 +78,14 @@ static __always_inline bool is_remote_ep_v6(struct __sk_buff *skb,
7378
return v6_equal(ip6h->daddr, addr);
7479
}
7580

76-
SEC("chk_neigh") int tc_chk(struct __sk_buff *skb)
81+
static __always_inline int get_dev_ifindex(int which)
82+
{
83+
int *ifindex = bpf_map_lookup_elem(&ifindex_map, &which);
84+
85+
return ifindex ? *ifindex : 0;
86+
}
87+
88+
SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
7789
{
7890
void *data_end = ctx_ptr(skb->data_end);
7991
void *data = ctx_ptr(skb->data);
@@ -87,7 +99,6 @@ SEC("chk_neigh") int tc_chk(struct __sk_buff *skb)
8799

88100
SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
89101
{
90-
int idx = dst_to_src_tmp;
91102
__u8 zero[ETH_ALEN * 2];
92103
bool redirect = false;
93104

@@ -103,19 +114,15 @@ SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
103114
if (!redirect)
104115
return TC_ACT_OK;
105116

106-
barrier_data(&idx);
107-
idx = bpf_ntohl(idx);
108-
109117
__builtin_memset(&zero, 0, sizeof(zero));
110118
if (bpf_skb_store_bytes(skb, 0, &zero, sizeof(zero), 0) < 0)
111119
return TC_ACT_SHOT;
112120

113-
return bpf_redirect_neigh(idx, 0);
121+
return bpf_redirect_neigh(get_dev_ifindex(dev_src), 0);
114122
}
115123

116124
SEC("src_ingress") int tc_src(struct __sk_buff *skb)
117125
{
118-
int idx = src_to_dst_tmp;
119126
__u8 zero[ETH_ALEN * 2];
120127
bool redirect = false;
121128

@@ -131,14 +138,11 @@ SEC("src_ingress") int tc_src(struct __sk_buff *skb)
131138
if (!redirect)
132139
return TC_ACT_OK;
133140

134-
barrier_data(&idx);
135-
idx = bpf_ntohl(idx);
136-
137141
__builtin_memset(&zero, 0, sizeof(zero));
138142
if (bpf_skb_store_bytes(skb, 0, &zero, sizeof(zero), 0) < 0)
139143
return TC_ACT_SHOT;
140144

141-
return bpf_redirect_neigh(idx, 0);
145+
return bpf_redirect_neigh(get_dev_ifindex(dev_dst), 0);
142146
}
143147

144148
char __license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/test_tc_neigh.sh

Lines changed: 0 additions & 168 deletions
This file was deleted.

0 commit comments

Comments
 (0)