1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2019 Facebook */ 3 #include <test_progs.h> 4 #include <network_helpers.h> 5 #include <bpf/btf.h> 6 7 typedef int (*test_cb)(struct bpf_object *obj); 8 9 static int check_data_map(struct bpf_object *obj, int prog_cnt, bool reset) 10 { 11 struct bpf_map *data_map = NULL, *map; 12 __u64 *result = NULL; 13 const int zero = 0; 14 __u32 duration = 0; 15 int ret = -1, i; 16 17 result = malloc((prog_cnt + 32 /* spare */) * sizeof(__u64)); 18 if (CHECK(!result, "alloc_memory", "failed to alloc memory")) 19 return -ENOMEM; 20 21 bpf_object__for_each_map(map, obj) 22 if (bpf_map__is_internal(map)) { 23 data_map = map; 24 break; 25 } 26 if (CHECK(!data_map, "find_data_map", "data map not found\n")) 27 goto out; 28 29 ret = bpf_map_lookup_elem(bpf_map__fd(data_map), &zero, result); 30 if (CHECK(ret, "get_result", 31 "failed to get output data: %d\n", ret)) 32 goto out; 33 34 for (i = 0; i < prog_cnt; i++) { 35 if (CHECK(result[i] != 1, "result", 36 "fexit_bpf2bpf result[%d] failed err %llu\n", 37 i, result[i])) 38 goto out; 39 result[i] = 0; 40 } 41 if (reset) { 42 ret = bpf_map_update_elem(bpf_map__fd(data_map), &zero, result, 0); 43 if (CHECK(ret, "reset_result", "failed to reset result\n")) 44 goto out; 45 } 46 47 ret = 0; 48 out: 49 free(result); 50 return ret; 51 } 52 53 static void test_fexit_bpf2bpf_common(const char *obj_file, 54 const char *target_obj_file, 55 int prog_cnt, 56 const char **prog_name, 57 bool run_prog, 58 test_cb cb) 59 { 60 struct bpf_object *obj = NULL, *tgt_obj; 61 struct bpf_program **prog = NULL; 62 struct bpf_link **link = NULL; 63 __u32 duration = 0, retval; 64 int err, tgt_fd, i; 65 66 err = bpf_prog_load(target_obj_file, BPF_PROG_TYPE_UNSPEC, 67 &tgt_obj, &tgt_fd); 68 if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n", 69 target_obj_file, err, errno)) 70 return; 71 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, 72 .attach_prog_fd = tgt_fd, 73 ); 74 75 link = calloc(sizeof(struct bpf_link *), prog_cnt); 76 prog = calloc(sizeof(struct bpf_program *), prog_cnt); 77 if (CHECK(!link || !prog, "alloc_memory", "failed to alloc memory")) 78 goto close_prog; 79 80 obj = bpf_object__open_file(obj_file, &opts); 81 if (CHECK(IS_ERR_OR_NULL(obj), "obj_open", 82 "failed to open %s: %ld\n", obj_file, 83 PTR_ERR(obj))) 84 goto close_prog; 85 86 err = bpf_object__load(obj); 87 if (CHECK(err, "obj_load", "err %d\n", err)) 88 goto close_prog; 89 90 for (i = 0; i < prog_cnt; i++) { 91 prog[i] = bpf_object__find_program_by_title(obj, prog_name[i]); 92 if (CHECK(!prog[i], "find_prog", "prog %s not found\n", prog_name[i])) 93 goto close_prog; 94 link[i] = bpf_program__attach_trace(prog[i]); 95 if (CHECK(IS_ERR(link[i]), "attach_trace", "failed to link\n")) 96 goto close_prog; 97 } 98 99 if (cb) { 100 err = cb(obj); 101 if (err) 102 goto close_prog; 103 } 104 105 if (!run_prog) 106 goto close_prog; 107 108 err = bpf_prog_test_run(tgt_fd, 1, &pkt_v6, sizeof(pkt_v6), 109 NULL, NULL, &retval, &duration); 110 CHECK(err || retval, "ipv6", 111 "err %d errno %d retval %d duration %d\n", 112 err, errno, retval, duration); 113 114 if (check_data_map(obj, prog_cnt, false)) 115 goto close_prog; 116 117 close_prog: 118 for (i = 0; i < prog_cnt; i++) 119 if (!IS_ERR_OR_NULL(link[i])) 120 bpf_link__destroy(link[i]); 121 if (!IS_ERR_OR_NULL(obj)) 122 bpf_object__close(obj); 123 bpf_object__close(tgt_obj); 124 free(link); 125 free(prog); 126 } 127 128 static void test_target_no_callees(void) 129 { 130 const char *prog_name[] = { 131 "fexit/test_pkt_md_access", 132 }; 133 test_fexit_bpf2bpf_common("./fexit_bpf2bpf_simple.o", 134 "./test_pkt_md_access.o", 135 ARRAY_SIZE(prog_name), 136 prog_name, true, NULL); 137 } 138 139 static void test_target_yes_callees(void) 140 { 141 const char *prog_name[] = { 142 "fexit/test_pkt_access", 143 "fexit/test_pkt_access_subprog1", 144 "fexit/test_pkt_access_subprog2", 145 "fexit/test_pkt_access_subprog3", 146 }; 147 test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o", 148 "./test_pkt_access.o", 149 ARRAY_SIZE(prog_name), 150 prog_name, true, NULL); 151 } 152 153 static void test_func_replace(void) 154 { 155 const char *prog_name[] = { 156 "fexit/test_pkt_access", 157 "fexit/test_pkt_access_subprog1", 158 "fexit/test_pkt_access_subprog2", 159 "fexit/test_pkt_access_subprog3", 160 "freplace/get_skb_len", 161 "freplace/get_skb_ifindex", 162 "freplace/get_constant", 163 "freplace/test_pkt_write_access_subprog", 164 }; 165 test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o", 166 "./test_pkt_access.o", 167 ARRAY_SIZE(prog_name), 168 prog_name, true, NULL); 169 } 170 171 static void test_func_replace_verify(void) 172 { 173 const char *prog_name[] = { 174 "freplace/do_bind", 175 }; 176 test_fexit_bpf2bpf_common("./freplace_connect4.o", 177 "./connect4_prog.o", 178 ARRAY_SIZE(prog_name), 179 prog_name, false, NULL); 180 } 181 182 static int test_second_attach(struct bpf_object *obj) 183 { 184 const char *prog_name = "freplace/get_constant"; 185 const char *tgt_name = prog_name + 9; /* cut off freplace/ */ 186 const char *tgt_obj_file = "./test_pkt_access.o"; 187 struct bpf_program *prog = NULL; 188 struct bpf_object *tgt_obj; 189 __u32 duration = 0, retval; 190 struct bpf_link *link; 191 int err = 0, tgt_fd; 192 193 prog = bpf_object__find_program_by_title(obj, prog_name); 194 if (CHECK(!prog, "find_prog", "prog %s not found\n", prog_name)) 195 return -ENOENT; 196 197 err = bpf_prog_load(tgt_obj_file, BPF_PROG_TYPE_UNSPEC, 198 &tgt_obj, &tgt_fd); 199 if (CHECK(err, "second_prog_load", "file %s err %d errno %d\n", 200 tgt_obj_file, err, errno)) 201 return err; 202 203 link = bpf_program__attach_freplace(prog, tgt_fd, tgt_name); 204 if (CHECK(IS_ERR(link), "second_link", "failed to attach second link prog_fd %d tgt_fd %d\n", bpf_program__fd(prog), tgt_fd)) 205 goto out; 206 207 err = bpf_prog_test_run(tgt_fd, 1, &pkt_v6, sizeof(pkt_v6), 208 NULL, NULL, &retval, &duration); 209 if (CHECK(err || retval, "ipv6", 210 "err %d errno %d retval %d duration %d\n", 211 err, errno, retval, duration)) 212 goto out; 213 214 err = check_data_map(obj, 1, true); 215 if (err) 216 goto out; 217 218 out: 219 bpf_link__destroy(link); 220 bpf_object__close(tgt_obj); 221 return err; 222 } 223 224 static void test_func_replace_multi(void) 225 { 226 const char *prog_name[] = { 227 "freplace/get_constant", 228 }; 229 test_fexit_bpf2bpf_common("./freplace_get_constant.o", 230 "./test_pkt_access.o", 231 ARRAY_SIZE(prog_name), 232 prog_name, true, test_second_attach); 233 } 234 235 static void test_func_sockmap_update(void) 236 { 237 const char *prog_name[] = { 238 "freplace/cls_redirect", 239 }; 240 test_fexit_bpf2bpf_common("./freplace_cls_redirect.o", 241 "./test_cls_redirect.o", 242 ARRAY_SIZE(prog_name), 243 prog_name, false, NULL); 244 } 245 246 static void test_obj_load_failure_common(const char *obj_file, 247 const char *target_obj_file) 248 249 { 250 /* 251 * standalone test that asserts failure to load freplace prog 252 * because of invalid return code. 253 */ 254 struct bpf_object *obj = NULL, *pkt_obj; 255 int err, pkt_fd; 256 __u32 duration = 0; 257 258 err = bpf_prog_load(target_obj_file, BPF_PROG_TYPE_UNSPEC, 259 &pkt_obj, &pkt_fd); 260 /* the target prog should load fine */ 261 if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n", 262 target_obj_file, err, errno)) 263 return; 264 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, 265 .attach_prog_fd = pkt_fd, 266 ); 267 268 obj = bpf_object__open_file(obj_file, &opts); 269 if (CHECK(IS_ERR_OR_NULL(obj), "obj_open", 270 "failed to open %s: %ld\n", obj_file, 271 PTR_ERR(obj))) 272 goto close_prog; 273 274 /* It should fail to load the program */ 275 err = bpf_object__load(obj); 276 if (CHECK(!err, "bpf_obj_load should fail", "err %d\n", err)) 277 goto close_prog; 278 279 close_prog: 280 if (!IS_ERR_OR_NULL(obj)) 281 bpf_object__close(obj); 282 bpf_object__close(pkt_obj); 283 } 284 285 static void test_func_replace_return_code(void) 286 { 287 /* test invalid return code in the replaced program */ 288 test_obj_load_failure_common("./freplace_connect_v4_prog.o", 289 "./connect4_prog.o"); 290 } 291 292 static void test_func_map_prog_compatibility(void) 293 { 294 /* test with spin lock map value in the replaced program */ 295 test_obj_load_failure_common("./freplace_attach_probe.o", 296 "./test_attach_probe.o"); 297 } 298 299 void test_fexit_bpf2bpf(void) 300 { 301 if (test__start_subtest("target_no_callees")) 302 test_target_no_callees(); 303 if (test__start_subtest("target_yes_callees")) 304 test_target_yes_callees(); 305 if (test__start_subtest("func_replace")) 306 test_func_replace(); 307 if (test__start_subtest("func_replace_verify")) 308 test_func_replace_verify(); 309 if (test__start_subtest("func_sockmap_update")) 310 test_func_sockmap_update(); 311 if (test__start_subtest("func_replace_return_code")) 312 test_func_replace_return_code(); 313 if (test__start_subtest("func_map_prog_compatibility")) 314 test_func_map_prog_compatibility(); 315 if (test__start_subtest("func_replace_multi")) 316 test_func_replace_multi(); 317 } 318