1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3 #include <test_progs.h>
5 static void test_fexit_bpf2bpf_common(const char *obj_file
,
6 const char *target_obj_file
,
8 const char **prog_name
)
10 struct bpf_object
*obj
= NULL
, *pkt_obj
;
12 struct bpf_link
**link
= NULL
;
13 struct bpf_program
**prog
= NULL
;
14 __u32 duration
= 0, retval
;
15 struct bpf_map
*data_map
;
19 err
= bpf_prog_load(target_obj_file
, BPF_PROG_TYPE_UNSPEC
,
21 if (CHECK(err
, "prog_load sched cls", "err %d errno %d\n", err
, errno
))
23 DECLARE_LIBBPF_OPTS(bpf_object_open_opts
, opts
,
24 .attach_prog_fd
= pkt_fd
,
27 link
= calloc(sizeof(struct bpf_link
*), prog_cnt
);
28 prog
= calloc(sizeof(struct bpf_program
*), prog_cnt
);
29 result
= malloc((prog_cnt
+ 32 /* spare */) * sizeof(u64
));
30 if (CHECK(!link
|| !prog
|| !result
, "alloc_memory",
31 "failed to alloc memory"))
34 obj
= bpf_object__open_file(obj_file
, &opts
);
35 if (CHECK(IS_ERR_OR_NULL(obj
), "obj_open",
36 "failed to open fexit_bpf2bpf: %ld\n",
40 err
= bpf_object__load(obj
);
41 if (CHECK(err
, "obj_load", "err %d\n", err
))
44 for (i
= 0; i
< prog_cnt
; i
++) {
45 prog
[i
] = bpf_object__find_program_by_title(obj
, prog_name
[i
]);
46 if (CHECK(!prog
[i
], "find_prog", "prog %s not found\n", prog_name
[i
]))
48 link
[i
] = bpf_program__attach_trace(prog
[i
]);
49 if (CHECK(IS_ERR(link
[i
]), "attach_trace", "failed to link\n"))
52 data_map
= bpf_object__find_map_by_name(obj
, "fexit_bp.bss");
53 if (CHECK(!data_map
, "find_data_map", "data map not found\n"))
56 err
= bpf_prog_test_run(pkt_fd
, 1, &pkt_v6
, sizeof(pkt_v6
),
57 NULL
, NULL
, &retval
, &duration
);
58 CHECK(err
|| retval
, "ipv6",
59 "err %d errno %d retval %d duration %d\n",
60 err
, errno
, retval
, duration
);
62 err
= bpf_map_lookup_elem(bpf_map__fd(data_map
), &zero
, result
);
63 if (CHECK(err
, "get_result",
64 "failed to get output data: %d\n", err
))
67 for (i
= 0; i
< prog_cnt
; i
++)
68 if (CHECK(result
[i
] != 1, "result", "fexit_bpf2bpf failed err %ld\n",
73 for (i
= 0; i
< prog_cnt
; i
++)
74 if (!IS_ERR_OR_NULL(link
[i
]))
75 bpf_link__destroy(link
[i
]);
76 if (!IS_ERR_OR_NULL(obj
))
77 bpf_object__close(obj
);
78 bpf_object__close(pkt_obj
);
84 static void test_target_no_callees(void)
86 const char *prog_name
[] = {
87 "fexit/test_pkt_md_access",
89 test_fexit_bpf2bpf_common("./fexit_bpf2bpf_simple.o",
90 "./test_pkt_md_access.o",
91 ARRAY_SIZE(prog_name
),
95 static void test_target_yes_callees(void)
97 const char *prog_name
[] = {
98 "fexit/test_pkt_access",
99 "fexit/test_pkt_access_subprog1",
100 "fexit/test_pkt_access_subprog2",
101 "fexit/test_pkt_access_subprog3",
103 test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o",
104 "./test_pkt_access.o",
105 ARRAY_SIZE(prog_name
),
109 static void test_func_replace(void)
111 const char *prog_name
[] = {
112 "fexit/test_pkt_access",
113 "fexit/test_pkt_access_subprog1",
114 "fexit/test_pkt_access_subprog2",
115 "fexit/test_pkt_access_subprog3",
116 "freplace/get_skb_len",
117 "freplace/get_skb_ifindex",
118 "freplace/get_constant",
120 test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o",
121 "./test_pkt_access.o",
122 ARRAY_SIZE(prog_name
),
126 void test_fexit_bpf2bpf(void)
128 test_target_no_callees();
129 test_target_yes_callees();