Skip to content

Commit 1d8a0af

Browse files
tohojoAlexei Starovoitov
authored andcommitted
selftests/bpf: Add test for freplace program with expected_attach_type
This adds a new selftest that tests the ability to attach an freplace program to a program type that relies on the expected_attach_type of the target program to pass verification. Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/158773526831.293902.16011743438619684815.stgit@toke.dk
1 parent 03f87c0 commit 1d8a0af

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
static void test_fexit_bpf2bpf_common(const char *obj_file,
66
const char *target_obj_file,
77
int prog_cnt,
8-
const char **prog_name)
8+
const char **prog_name,
9+
bool run_prog)
910
{
1011
struct bpf_object *obj = NULL, *pkt_obj;
1112
int err, pkt_fd, i;
@@ -18,7 +19,8 @@ static void test_fexit_bpf2bpf_common(const char *obj_file,
1819

1920
err = bpf_prog_load(target_obj_file, BPF_PROG_TYPE_UNSPEC,
2021
&pkt_obj, &pkt_fd);
21-
if (CHECK(err, "prog_load sched cls", "err %d errno %d\n", err, errno))
22+
if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n",
23+
target_obj_file, err, errno))
2224
return;
2325
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
2426
.attach_prog_fd = pkt_fd,
@@ -33,7 +35,7 @@ static void test_fexit_bpf2bpf_common(const char *obj_file,
3335

3436
obj = bpf_object__open_file(obj_file, &opts);
3537
if (CHECK(IS_ERR_OR_NULL(obj), "obj_open",
36-
"failed to open fexit_bpf2bpf: %ld\n",
38+
"failed to open %s: %ld\n", obj_file,
3739
PTR_ERR(obj)))
3840
goto close_prog;
3941

@@ -49,6 +51,10 @@ static void test_fexit_bpf2bpf_common(const char *obj_file,
4951
if (CHECK(IS_ERR(link[i]), "attach_trace", "failed to link\n"))
5052
goto close_prog;
5153
}
54+
55+
if (!run_prog)
56+
goto close_prog;
57+
5258
data_map = bpf_object__find_map_by_name(obj, "fexit_bp.bss");
5359
if (CHECK(!data_map, "find_data_map", "data map not found\n"))
5460
goto close_prog;
@@ -89,7 +95,7 @@ static void test_target_no_callees(void)
8995
test_fexit_bpf2bpf_common("./fexit_bpf2bpf_simple.o",
9096
"./test_pkt_md_access.o",
9197
ARRAY_SIZE(prog_name),
92-
prog_name);
98+
prog_name, true);
9399
}
94100

95101
static void test_target_yes_callees(void)
@@ -103,7 +109,7 @@ static void test_target_yes_callees(void)
103109
test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o",
104110
"./test_pkt_access.o",
105111
ARRAY_SIZE(prog_name),
106-
prog_name);
112+
prog_name, true);
107113
}
108114

109115
static void test_func_replace(void)
@@ -120,12 +126,24 @@ static void test_func_replace(void)
120126
test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o",
121127
"./test_pkt_access.o",
122128
ARRAY_SIZE(prog_name),
123-
prog_name);
129+
prog_name, true);
130+
}
131+
132+
static void test_func_replace_verify(void)
133+
{
134+
const char *prog_name[] = {
135+
"freplace/do_bind",
136+
};
137+
test_fexit_bpf2bpf_common("./freplace_connect4.o",
138+
"./connect4_prog.o",
139+
ARRAY_SIZE(prog_name),
140+
prog_name, false);
124141
}
125142

126143
void test_fexit_bpf2bpf(void)
127144
{
128145
test_target_no_callees();
129146
test_target_yes_callees();
130147
test_func_replace();
148+
test_func_replace_verify();
131149
}

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,25 @@
1818

1919
int _version SEC("version") = 1;
2020

21+
__attribute__ ((noinline))
22+
int do_bind(struct bpf_sock_addr *ctx)
23+
{
24+
struct sockaddr_in sa = {};
25+
26+
sa.sin_family = AF_INET;
27+
sa.sin_port = bpf_htons(0);
28+
sa.sin_addr.s_addr = bpf_htonl(SRC_REWRITE_IP4);
29+
30+
if (bpf_bind(ctx, (struct sockaddr *)&sa, sizeof(sa)) != 0)
31+
return 0;
32+
33+
return 1;
34+
}
35+
2136
SEC("cgroup/connect4")
2237
int connect_v4_prog(struct bpf_sock_addr *ctx)
2338
{
2439
struct bpf_sock_tuple tuple = {};
25-
struct sockaddr_in sa;
2640
struct bpf_sock *sk;
2741

2842
/* Verify that new destination is available. */
@@ -56,17 +70,7 @@ int connect_v4_prog(struct bpf_sock_addr *ctx)
5670
ctx->user_ip4 = bpf_htonl(DST_REWRITE_IP4);
5771
ctx->user_port = bpf_htons(DST_REWRITE_PORT4);
5872

59-
/* Rewrite source. */
60-
memset(&sa, 0, sizeof(sa));
61-
62-
sa.sin_family = AF_INET;
63-
sa.sin_port = bpf_htons(0);
64-
sa.sin_addr.s_addr = bpf_htonl(SRC_REWRITE_IP4);
65-
66-
if (bpf_bind(ctx, (struct sockaddr *)&sa, sizeof(sa)) != 0)
67-
return 0;
68-
69-
return 1;
73+
return do_bind(ctx) ? 1 : 0;
7074
}
7175

7276
char _license[] SEC("license") = "GPL";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <linux/stddef.h>
2+
#include <linux/ipv6.h>
3+
#include <linux/bpf.h>
4+
#include <linux/in.h>
5+
#include <sys/socket.h>
6+
#include <bpf/bpf_helpers.h>
7+
#include <bpf/bpf_endian.h>
8+
9+
SEC("freplace/do_bind")
10+
int new_do_bind(struct bpf_sock_addr *ctx)
11+
{
12+
struct sockaddr_in sa = {};
13+
14+
bpf_bind(ctx, (struct sockaddr *)&sa, sizeof(sa));
15+
return 0;
16+
}
17+
18+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)