Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | // SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> /* test_tailcall_1 checks basic functionality by patching multiple locations * in a single program for a single tail call slot with nop->jmp, jmp->nop * and jmp->jmp rewrites. Also checks for nop->nop. */ static void test_tailcall_1(void) { int err, map_fd, prog_fd, main_fd, i, j; struct bpf_map *prog_array; struct bpf_program *prog; struct bpf_object *obj; __u32 retval, duration; char prog_name[32]; char buff[128] = {}; err = bpf_prog_load("tailcall1.o", BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); if (CHECK_FAIL(err)) return; prog = bpf_object__find_program_by_title(obj, "classifier"); if (CHECK_FAIL(!prog)) goto out; main_fd = bpf_program__fd(prog); if (CHECK_FAIL(main_fd < 0)) goto out; prog_array = bpf_object__find_map_by_name(obj, "jmp_table"); if (CHECK_FAIL(!prog_array)) goto out; map_fd = bpf_map__fd(prog_array); if (CHECK_FAIL(map_fd < 0)) goto out; for (i = 0; i < bpf_map__def(prog_array)->max_entries; i++) { snprintf(prog_name, sizeof(prog_name), "classifier/%i", i); prog = bpf_object__find_program_by_title(obj, prog_name); if (CHECK_FAIL(!prog)) goto out; prog_fd = bpf_program__fd(prog); if (CHECK_FAIL(prog_fd < 0)) goto out; err = bpf_map_update_elem(map_fd, &i, &prog_fd, BPF_ANY); if (CHECK_FAIL(err)) goto out; } for (i = 0; i < bpf_map__def(prog_array)->max_entries; i++) { err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != i, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); err = bpf_map_delete_elem(map_fd, &i); if (CHECK_FAIL(err)) goto out; } err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != 3, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); for (i = 0; i < bpf_map__def(prog_array)->max_entries; i++) { snprintf(prog_name, sizeof(prog_name), "classifier/%i", i); prog = bpf_object__find_program_by_title(obj, prog_name); if (CHECK_FAIL(!prog)) goto out; prog_fd = bpf_program__fd(prog); if (CHECK_FAIL(prog_fd < 0)) goto out; err = bpf_map_update_elem(map_fd, &i, &prog_fd, BPF_ANY); if (CHECK_FAIL(err)) goto out; } err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != 0, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); for (i = 0; i < bpf_map__def(prog_array)->max_entries; i++) { j = bpf_map__def(prog_array)->max_entries - 1 - i; snprintf(prog_name, sizeof(prog_name), "classifier/%i", j); prog = bpf_object__find_program_by_title(obj, prog_name); if (CHECK_FAIL(!prog)) goto out; prog_fd = bpf_program__fd(prog); if (CHECK_FAIL(prog_fd < 0)) goto out; err = bpf_map_update_elem(map_fd, &i, &prog_fd, BPF_ANY); if (CHECK_FAIL(err)) goto out; } for (i = 0; i < bpf_map__def(prog_array)->max_entries; i++) { j = bpf_map__def(prog_array)->max_entries - 1 - i; err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != j, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); err = bpf_map_delete_elem(map_fd, &i); if (CHECK_FAIL(err)) goto out; } err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != 3, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); for (i = 0; i < bpf_map__def(prog_array)->max_entries; i++) { err = bpf_map_delete_elem(map_fd, &i); if (CHECK_FAIL(err >= 0 || errno != ENOENT)) goto out; err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != 3, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); } out: bpf_object__close(obj); } /* test_tailcall_2 checks that patching multiple programs for a single * tail call slot works. It also jumps through several programs and tests * the tail call limit counter. */ static void test_tailcall_2(void) { int err, map_fd, prog_fd, main_fd, i; struct bpf_map *prog_array; struct bpf_program *prog; struct bpf_object *obj; __u32 retval, duration; char prog_name[32]; char buff[128] = {}; err = bpf_prog_load("tailcall2.o", BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); if (CHECK_FAIL(err)) return; prog = bpf_object__find_program_by_title(obj, "classifier"); if (CHECK_FAIL(!prog)) goto out; main_fd = bpf_program__fd(prog); if (CHECK_FAIL(main_fd < 0)) goto out; prog_array = bpf_object__find_map_by_name(obj, "jmp_table"); if (CHECK_FAIL(!prog_array)) goto out; map_fd = bpf_map__fd(prog_array); if (CHECK_FAIL(map_fd < 0)) goto out; for (i = 0; i < bpf_map__def(prog_array)->max_entries; i++) { snprintf(prog_name, sizeof(prog_name), "classifier/%i", i); prog = bpf_object__find_program_by_title(obj, prog_name); if (CHECK_FAIL(!prog)) goto out; prog_fd = bpf_program__fd(prog); if (CHECK_FAIL(prog_fd < 0)) goto out; err = bpf_map_update_elem(map_fd, &i, &prog_fd, BPF_ANY); if (CHECK_FAIL(err)) goto out; } err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != 2, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); i = 2; err = bpf_map_delete_elem(map_fd, &i); if (CHECK_FAIL(err)) goto out; err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != 1, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); i = 0; err = bpf_map_delete_elem(map_fd, &i); if (CHECK_FAIL(err)) goto out; err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != 3, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); out: bpf_object__close(obj); } /* test_tailcall_3 checks that the count value of the tail call limit * enforcement matches with expectations. */ static void test_tailcall_3(void) { int err, map_fd, prog_fd, main_fd, data_fd, i, val; struct bpf_map *prog_array, *data_map; struct bpf_program *prog; struct bpf_object *obj; __u32 retval, duration; char buff[128] = {}; err = bpf_prog_load("tailcall3.o", BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); if (CHECK_FAIL(err)) return; prog = bpf_object__find_program_by_title(obj, "classifier"); if (CHECK_FAIL(!prog)) goto out; main_fd = bpf_program__fd(prog); if (CHECK_FAIL(main_fd < 0)) goto out; prog_array = bpf_object__find_map_by_name(obj, "jmp_table"); if (CHECK_FAIL(!prog_array)) goto out; map_fd = bpf_map__fd(prog_array); if (CHECK_FAIL(map_fd < 0)) goto out; prog = bpf_object__find_program_by_title(obj, "classifier/0"); if (CHECK_FAIL(!prog)) goto out; prog_fd = bpf_program__fd(prog); if (CHECK_FAIL(prog_fd < 0)) goto out; i = 0; err = bpf_map_update_elem(map_fd, &i, &prog_fd, BPF_ANY); if (CHECK_FAIL(err)) goto out; err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != 1, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); data_map = bpf_object__find_map_by_name(obj, "tailcall.bss"); if (CHECK_FAIL(!data_map || !bpf_map__is_internal(data_map))) return; data_fd = bpf_map__fd(data_map); if (CHECK_FAIL(map_fd < 0)) return; i = 0; err = bpf_map_lookup_elem(data_fd, &i, &val); CHECK(err || val != 33, "tailcall count", "err %d errno %d count %d\n", err, errno, val); i = 0; err = bpf_map_delete_elem(map_fd, &i); if (CHECK_FAIL(err)) goto out; err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != 0, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); out: bpf_object__close(obj); } /* test_tailcall_4 checks that the kernel properly selects indirect jump * for the case where the key is not known. Latter is passed via global * data to select different targets we can compare return value of. */ static void test_tailcall_4(void) { int err, map_fd, prog_fd, main_fd, data_fd, i; struct bpf_map *prog_array, *data_map; struct bpf_program *prog; struct bpf_object *obj; __u32 retval, duration; static const int zero = 0; char buff[128] = {}; char prog_name[32]; err = bpf_prog_load("tailcall4.o", BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); if (CHECK_FAIL(err)) return; prog = bpf_object__find_program_by_title(obj, "classifier"); if (CHECK_FAIL(!prog)) goto out; main_fd = bpf_program__fd(prog); if (CHECK_FAIL(main_fd < 0)) goto out; prog_array = bpf_object__find_map_by_name(obj, "jmp_table"); if (CHECK_FAIL(!prog_array)) goto out; map_fd = bpf_map__fd(prog_array); if (CHECK_FAIL(map_fd < 0)) goto out; data_map = bpf_object__find_map_by_name(obj, "tailcall.bss"); if (CHECK_FAIL(!data_map || !bpf_map__is_internal(data_map))) return; data_fd = bpf_map__fd(data_map); if (CHECK_FAIL(map_fd < 0)) return; for (i = 0; i < bpf_map__def(prog_array)->max_entries; i++) { snprintf(prog_name, sizeof(prog_name), "classifier/%i", i); prog = bpf_object__find_program_by_title(obj, prog_name); if (CHECK_FAIL(!prog)) goto out; prog_fd = bpf_program__fd(prog); if (CHECK_FAIL(prog_fd < 0)) goto out; err = bpf_map_update_elem(map_fd, &i, &prog_fd, BPF_ANY); if (CHECK_FAIL(err)) goto out; } for (i = 0; i < bpf_map__def(prog_array)->max_entries; i++) { err = bpf_map_update_elem(data_fd, &zero, &i, BPF_ANY); if (CHECK_FAIL(err)) goto out; err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != i, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); } for (i = 0; i < bpf_map__def(prog_array)->max_entries; i++) { err = bpf_map_update_elem(data_fd, &zero, &i, BPF_ANY); if (CHECK_FAIL(err)) goto out; err = bpf_map_delete_elem(map_fd, &i); if (CHECK_FAIL(err)) goto out; err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != 3, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); } out: bpf_object__close(obj); } /* test_tailcall_5 probes similarly to test_tailcall_4 that the kernel generates * an indirect jump when the keys are const but different from different branches. */ static void test_tailcall_5(void) { int err, map_fd, prog_fd, main_fd, data_fd, i, key[] = { 1111, 1234, 5678 }; struct bpf_map *prog_array, *data_map; struct bpf_program *prog; struct bpf_object *obj; __u32 retval, duration; static const int zero = 0; char buff[128] = {}; char prog_name[32]; err = bpf_prog_load("tailcall5.o", BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); if (CHECK_FAIL(err)) return; prog = bpf_object__find_program_by_title(obj, "classifier"); if (CHECK_FAIL(!prog)) goto out; main_fd = bpf_program__fd(prog); if (CHECK_FAIL(main_fd < 0)) goto out; prog_array = bpf_object__find_map_by_name(obj, "jmp_table"); if (CHECK_FAIL(!prog_array)) goto out; map_fd = bpf_map__fd(prog_array); if (CHECK_FAIL(map_fd < 0)) goto out; data_map = bpf_object__find_map_by_name(obj, "tailcall.bss"); if (CHECK_FAIL(!data_map || !bpf_map__is_internal(data_map))) return; data_fd = bpf_map__fd(data_map); if (CHECK_FAIL(map_fd < 0)) return; for (i = 0; i < bpf_map__def(prog_array)->max_entries; i++) { snprintf(prog_name, sizeof(prog_name), "classifier/%i", i); prog = bpf_object__find_program_by_title(obj, prog_name); if (CHECK_FAIL(!prog)) goto out; prog_fd = bpf_program__fd(prog); if (CHECK_FAIL(prog_fd < 0)) goto out; err = bpf_map_update_elem(map_fd, &i, &prog_fd, BPF_ANY); if (CHECK_FAIL(err)) goto out; } for (i = 0; i < bpf_map__def(prog_array)->max_entries; i++) { err = bpf_map_update_elem(data_fd, &zero, &key[i], BPF_ANY); if (CHECK_FAIL(err)) goto out; err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != i, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); } for (i = 0; i < bpf_map__def(prog_array)->max_entries; i++) { err = bpf_map_update_elem(data_fd, &zero, &key[i], BPF_ANY); if (CHECK_FAIL(err)) goto out; err = bpf_map_delete_elem(map_fd, &i); if (CHECK_FAIL(err)) goto out; err = bpf_prog_test_run(main_fd, 1, buff, sizeof(buff), 0, &duration, &retval, NULL); CHECK(err || retval != 3, "tailcall", "err %d errno %d retval %d\n", err, errno, retval); } out: bpf_object__close(obj); } void test_tailcalls(void) { if (test__start_subtest("tailcall_1")) test_tailcall_1(); if (test__start_subtest("tailcall_2")) test_tailcall_2(); if (test__start_subtest("tailcall_3")) test_tailcall_3(); if (test__start_subtest("tailcall_4")) test_tailcall_4(); if (test__start_subtest("tailcall_5")) test_tailcall_5(); } |