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 __u32 retval, tgt_prog_id, info_len; 62 struct bpf_prog_info prog_info = {}; 63 struct bpf_program **prog = NULL, *p; 64 struct bpf_link **link = NULL; 65 int err, tgt_fd, i; 66 struct btf *btf; 67 68 err = bpf_prog_load(target_obj_file, BPF_PROG_TYPE_UNSPEC, 69 &tgt_obj, &tgt_fd); 70 if (!ASSERT_OK(err, "tgt_prog_load")) 71 return; 72 73 info_len = sizeof(prog_info); 74 err = bpf_obj_get_info_by_fd(tgt_fd, &prog_info, &info_len); 75 if (!ASSERT_OK(err, "tgt_fd_get_info")) 76 goto close_prog; 77 78 tgt_prog_id = prog_info.id; 79 btf = bpf_object__btf(tgt_obj); 80 81 link = calloc(sizeof(struct bpf_link *), prog_cnt); 82 if (!ASSERT_OK_PTR(link, "link_ptr")) 83 goto close_prog; 84 85 prog = calloc(sizeof(struct bpf_program *), prog_cnt); 86 if (!ASSERT_OK_PTR(prog, "prog_ptr")) 87 goto close_prog; 88 89 obj = bpf_object__open_file(obj_file, NULL); 90 if (!ASSERT_OK_PTR(obj, "obj_open")) 91 goto close_prog; 92 93 bpf_object__for_each_program(p, obj) { 94 err = bpf_program__set_attach_target(p, tgt_fd, NULL); 95 ASSERT_OK(err, "set_attach_target"); 96 } 97 98 err = bpf_object__load(obj); 99 if (!ASSERT_OK(err, "obj_load")) 100 goto close_prog; 101 102 for (i = 0; i < prog_cnt; i++) { 103 struct bpf_link_info link_info; 104 char *tgt_name; 105 __s32 btf_id; 106 107 tgt_name = strstr(prog_name[i], "/"); 108 if (!ASSERT_OK_PTR(tgt_name, "tgt_name")) 109 goto close_prog; 110 btf_id = btf__find_by_name_kind(btf, tgt_name + 1, BTF_KIND_FUNC); 111 112 prog[i] = bpf_object__find_program_by_title(obj, prog_name[i]); 113 if (!ASSERT_OK_PTR(prog[i], prog_name[i])) 114 goto close_prog; 115 116 link[i] = bpf_program__attach_trace(prog[i]); 117 if (!ASSERT_OK_PTR(link[i], "attach_trace")) 118 goto close_prog; 119 120 info_len = sizeof(link_info); 121 memset(&link_info, 0, sizeof(link_info)); 122 err = bpf_obj_get_info_by_fd(bpf_link__fd(link[i]), 123 &link_info, &info_len); 124 ASSERT_OK(err, "link_fd_get_info"); 125 ASSERT_EQ(link_info.tracing.attach_type, 126 bpf_program__get_expected_attach_type(prog[i]), 127 "link_attach_type"); 128 ASSERT_EQ(link_info.tracing.target_obj_id, tgt_prog_id, "link_tgt_obj_id"); 129 ASSERT_EQ(link_info.tracing.target_btf_id, btf_id, "link_tgt_btf_id"); 130 } 131 132 if (cb) { 133 err = cb(obj); 134 if (err) 135 goto close_prog; 136 } 137 138 if (!run_prog) 139 goto close_prog; 140 141 err = bpf_prog_test_run(tgt_fd, 1, &pkt_v6, sizeof(pkt_v6), 142 NULL, NULL, &retval, NULL); 143 ASSERT_OK(err, "prog_run"); 144 ASSERT_EQ(retval, 0, "prog_run_ret"); 145 146 if (check_data_map(obj, prog_cnt, false)) 147 goto close_prog; 148 149 close_prog: 150 for (i = 0; i < prog_cnt; i++) 151 bpf_link__destroy(link[i]); 152 bpf_object__close(obj); 153 bpf_object__close(tgt_obj); 154 free(link); 155 free(prog); 156 } 157 158 static void test_target_no_callees(void) 159 { 160 const char *prog_name[] = { 161 "fexit/test_pkt_md_access", 162 }; 163 test_fexit_bpf2bpf_common("./fexit_bpf2bpf_simple.o", 164 "./test_pkt_md_access.o", 165 ARRAY_SIZE(prog_name), 166 prog_name, true, NULL); 167 } 168 169 static void test_target_yes_callees(void) 170 { 171 const char *prog_name[] = { 172 "fexit/test_pkt_access", 173 "fexit/test_pkt_access_subprog1", 174 "fexit/test_pkt_access_subprog2", 175 "fexit/test_pkt_access_subprog3", 176 }; 177 test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o", 178 "./test_pkt_access.o", 179 ARRAY_SIZE(prog_name), 180 prog_name, true, NULL); 181 } 182 183 static void test_func_replace(void) 184 { 185 const char *prog_name[] = { 186 "fexit/test_pkt_access", 187 "fexit/test_pkt_access_subprog1", 188 "fexit/test_pkt_access_subprog2", 189 "fexit/test_pkt_access_subprog3", 190 "freplace/get_skb_len", 191 "freplace/get_skb_ifindex", 192 "freplace/get_constant", 193 "freplace/test_pkt_write_access_subprog", 194 }; 195 test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o", 196 "./test_pkt_access.o", 197 ARRAY_SIZE(prog_name), 198 prog_name, true, NULL); 199 } 200 201 static void test_func_replace_verify(void) 202 { 203 const char *prog_name[] = { 204 "freplace/do_bind", 205 }; 206 test_fexit_bpf2bpf_common("./freplace_connect4.o", 207 "./connect4_prog.o", 208 ARRAY_SIZE(prog_name), 209 prog_name, false, NULL); 210 } 211 212 static int test_second_attach(struct bpf_object *obj) 213 { 214 const char *prog_name = "freplace/get_constant"; 215 const char *tgt_name = prog_name + 9; /* cut off freplace/ */ 216 const char *tgt_obj_file = "./test_pkt_access.o"; 217 struct bpf_program *prog = NULL; 218 struct bpf_object *tgt_obj; 219 __u32 duration = 0, retval; 220 struct bpf_link *link; 221 int err = 0, tgt_fd; 222 223 prog = bpf_object__find_program_by_title(obj, prog_name); 224 if (CHECK(!prog, "find_prog", "prog %s not found\n", prog_name)) 225 return -ENOENT; 226 227 err = bpf_prog_load(tgt_obj_file, BPF_PROG_TYPE_UNSPEC, 228 &tgt_obj, &tgt_fd); 229 if (CHECK(err, "second_prog_load", "file %s err %d errno %d\n", 230 tgt_obj_file, err, errno)) 231 return err; 232 233 link = bpf_program__attach_freplace(prog, tgt_fd, tgt_name); 234 if (!ASSERT_OK_PTR(link, "second_link")) 235 goto out; 236 237 err = bpf_prog_test_run(tgt_fd, 1, &pkt_v6, sizeof(pkt_v6), 238 NULL, NULL, &retval, &duration); 239 if (CHECK(err || retval, "ipv6", 240 "err %d errno %d retval %d duration %d\n", 241 err, errno, retval, duration)) 242 goto out; 243 244 err = check_data_map(obj, 1, true); 245 if (err) 246 goto out; 247 248 out: 249 bpf_link__destroy(link); 250 bpf_object__close(tgt_obj); 251 return err; 252 } 253 254 static void test_func_replace_multi(void) 255 { 256 const char *prog_name[] = { 257 "freplace/get_constant", 258 }; 259 test_fexit_bpf2bpf_common("./freplace_get_constant.o", 260 "./test_pkt_access.o", 261 ARRAY_SIZE(prog_name), 262 prog_name, true, test_second_attach); 263 } 264 265 static void test_fmod_ret_freplace(void) 266 { 267 struct bpf_object *freplace_obj = NULL, *pkt_obj, *fmod_obj = NULL; 268 const char *freplace_name = "./freplace_get_constant.o"; 269 const char *fmod_ret_name = "./fmod_ret_freplace.o"; 270 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts); 271 const char *tgt_name = "./test_pkt_access.o"; 272 struct bpf_link *freplace_link = NULL; 273 struct bpf_program *prog; 274 __u32 duration = 0; 275 int err, pkt_fd, attach_prog_fd; 276 277 err = bpf_prog_load(tgt_name, BPF_PROG_TYPE_UNSPEC, 278 &pkt_obj, &pkt_fd); 279 /* the target prog should load fine */ 280 if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n", 281 tgt_name, err, errno)) 282 return; 283 284 freplace_obj = bpf_object__open_file(freplace_name, NULL); 285 if (!ASSERT_OK_PTR(freplace_obj, "freplace_obj_open")) 286 goto out; 287 288 prog = bpf_object__next_program(freplace_obj, NULL); 289 err = bpf_program__set_attach_target(prog, pkt_fd, NULL); 290 ASSERT_OK(err, "freplace__set_attach_target"); 291 292 err = bpf_object__load(freplace_obj); 293 if (CHECK(err, "freplace_obj_load", "err %d\n", err)) 294 goto out; 295 296 freplace_link = bpf_program__attach_trace(prog); 297 if (!ASSERT_OK_PTR(freplace_link, "freplace_attach_trace")) 298 goto out; 299 300 fmod_obj = bpf_object__open_file(fmod_ret_name, NULL); 301 if (!ASSERT_OK_PTR(fmod_obj, "fmod_obj_open")) 302 goto out; 303 304 attach_prog_fd = bpf_program__fd(prog); 305 prog = bpf_object__next_program(fmod_obj, NULL); 306 err = bpf_program__set_attach_target(prog, attach_prog_fd, NULL); 307 ASSERT_OK(err, "fmod_ret_set_attach_target"); 308 309 err = bpf_object__load(fmod_obj); 310 if (CHECK(!err, "fmod_obj_load", "loading fmod_ret should fail\n")) 311 goto out; 312 313 out: 314 bpf_link__destroy(freplace_link); 315 bpf_object__close(freplace_obj); 316 bpf_object__close(fmod_obj); 317 bpf_object__close(pkt_obj); 318 } 319 320 321 static void test_func_sockmap_update(void) 322 { 323 const char *prog_name[] = { 324 "freplace/cls_redirect", 325 }; 326 test_fexit_bpf2bpf_common("./freplace_cls_redirect.o", 327 "./test_cls_redirect.o", 328 ARRAY_SIZE(prog_name), 329 prog_name, false, NULL); 330 } 331 332 static void test_obj_load_failure_common(const char *obj_file, 333 const char *target_obj_file) 334 { 335 /* 336 * standalone test that asserts failure to load freplace prog 337 * because of invalid return code. 338 */ 339 struct bpf_object *obj = NULL, *pkt_obj; 340 struct bpf_program *prog; 341 int err, pkt_fd; 342 __u32 duration = 0; 343 344 err = bpf_prog_load(target_obj_file, BPF_PROG_TYPE_UNSPEC, 345 &pkt_obj, &pkt_fd); 346 /* the target prog should load fine */ 347 if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n", 348 target_obj_file, err, errno)) 349 return; 350 351 obj = bpf_object__open_file(obj_file, NULL); 352 if (!ASSERT_OK_PTR(obj, "obj_open")) 353 goto close_prog; 354 355 prog = bpf_object__next_program(obj, NULL); 356 err = bpf_program__set_attach_target(prog, pkt_fd, NULL); 357 ASSERT_OK(err, "set_attach_target"); 358 359 /* It should fail to load the program */ 360 err = bpf_object__load(obj); 361 if (CHECK(!err, "bpf_obj_load should fail", "err %d\n", err)) 362 goto close_prog; 363 364 close_prog: 365 bpf_object__close(obj); 366 bpf_object__close(pkt_obj); 367 } 368 369 static void test_func_replace_return_code(void) 370 { 371 /* test invalid return code in the replaced program */ 372 test_obj_load_failure_common("./freplace_connect_v4_prog.o", 373 "./connect4_prog.o"); 374 } 375 376 static void test_func_map_prog_compatibility(void) 377 { 378 /* test with spin lock map value in the replaced program */ 379 test_obj_load_failure_common("./freplace_attach_probe.o", 380 "./test_attach_probe.o"); 381 } 382 383 /* NOTE: affect other tests, must run in serial mode */ 384 void serial_test_fexit_bpf2bpf(void) 385 { 386 if (test__start_subtest("target_no_callees")) 387 test_target_no_callees(); 388 if (test__start_subtest("target_yes_callees")) 389 test_target_yes_callees(); 390 if (test__start_subtest("func_replace")) 391 test_func_replace(); 392 if (test__start_subtest("func_replace_verify")) 393 test_func_replace_verify(); 394 if (test__start_subtest("func_sockmap_update")) 395 test_func_sockmap_update(); 396 if (test__start_subtest("func_replace_return_code")) 397 test_func_replace_return_code(); 398 if (test__start_subtest("func_map_prog_compatibility")) 399 test_func_map_prog_compatibility(); 400 if (test__start_subtest("func_replace_multi")) 401 test_func_replace_multi(); 402 if (test__start_subtest("fmod_ret_freplace")) 403 test_fmod_ret_freplace(); 404 } 405