Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add test to cover ktls with bpf_msg_pop_data

The selftest can reproduce an issue where using bpf_msg_pop_data() in
ktls causes errors on the receiving end.

Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20250609020910.397930-3-jiayuan.chen@linux.dev

authored by

Jiayuan Chen and committed by
Daniel Borkmann
f1c02577 178f6a5c

+95
+91
tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c
··· 314 314 test_sockmap_ktls__destroy(skel); 315 315 } 316 316 317 + static void test_sockmap_ktls_tx_pop(int family, int sotype) 318 + { 319 + char msg[37] = "0123456789abcdefghijklmnopqrstuvwxyz\0"; 320 + int c = 0, p = 0, one = 1, sent, recvd; 321 + struct test_sockmap_ktls *skel; 322 + int prog_fd, map_fd; 323 + char rcv[50] = {0}; 324 + int err; 325 + int i, m, r; 326 + 327 + skel = test_sockmap_ktls__open_and_load(); 328 + if (!ASSERT_TRUE(skel, "open ktls skel")) 329 + return; 330 + 331 + err = create_pair(family, sotype, &c, &p); 332 + if (!ASSERT_OK(err, "create_pair()")) 333 + goto out; 334 + 335 + prog_fd = bpf_program__fd(skel->progs.prog_sk_policy); 336 + map_fd = bpf_map__fd(skel->maps.sock_map); 337 + 338 + err = bpf_prog_attach(prog_fd, map_fd, BPF_SK_MSG_VERDICT, 0); 339 + if (!ASSERT_OK(err, "bpf_prog_attach sk msg")) 340 + goto out; 341 + 342 + err = bpf_map_update_elem(map_fd, &one, &c, BPF_NOEXIST); 343 + if (!ASSERT_OK(err, "bpf_map_update_elem(c)")) 344 + goto out; 345 + 346 + err = init_ktls_pairs(c, p); 347 + if (!ASSERT_OK(err, "init_ktls_pairs(c, p)")) 348 + goto out; 349 + 350 + struct { 351 + int pop_start; 352 + int pop_len; 353 + } pop_policy[] = { 354 + /* trim the start */ 355 + {0, 2}, 356 + {0, 10}, 357 + {1, 2}, 358 + {1, 10}, 359 + /* trim the end */ 360 + {35, 2}, 361 + /* New entries should be added before this line */ 362 + {-1, -1}, 363 + }; 364 + 365 + i = 0; 366 + while (pop_policy[i].pop_start >= 0) { 367 + skel->bss->pop_start = pop_policy[i].pop_start; 368 + skel->bss->pop_end = pop_policy[i].pop_len; 369 + 370 + sent = send(c, msg, sizeof(msg), 0); 371 + if (!ASSERT_EQ(sent, sizeof(msg), "send(msg)")) 372 + goto out; 373 + 374 + recvd = recv_timeout(p, rcv, sizeof(rcv), MSG_DONTWAIT, 1); 375 + if (!ASSERT_EQ(recvd, sizeof(msg) - pop_policy[i].pop_len, "pop len mismatch")) 376 + goto out; 377 + 378 + /* verify the data 379 + * msg: 0123456789a bcdefghij klmnopqrstuvwxyz 380 + * | | 381 + * popped data 382 + */ 383 + for (m = 0, r = 0; m < sizeof(msg);) { 384 + /* skip checking the data that has been popped */ 385 + if (m >= pop_policy[i].pop_start && 386 + m <= pop_policy[i].pop_start + pop_policy[i].pop_len - 1) { 387 + m++; 388 + continue; 389 + } 390 + 391 + if (!ASSERT_EQ(msg[m], rcv[r], "data mismatch")) 392 + goto out; 393 + m++; 394 + r++; 395 + } 396 + i++; 397 + } 398 + out: 399 + if (c) 400 + close(c); 401 + if (p) 402 + close(p); 403 + test_sockmap_ktls__destroy(skel); 404 + } 405 + 317 406 static void run_tests(int family, enum bpf_map_type map_type) 318 407 { 319 408 int map; ··· 427 338 test_sockmap_ktls_tx_cork(family, sotype, true); 428 339 if (test__start_subtest("tls tx egress with no buf")) 429 340 test_sockmap_ktls_tx_no_buf(family, sotype, true); 341 + if (test__start_subtest("tls tx with pop")) 342 + test_sockmap_ktls_tx_pop(family, sotype); 430 343 } 431 344 432 345 void test_sockmap_ktls(void)
+4
tools/testing/selftests/bpf/progs/test_sockmap_ktls.c
··· 7 7 int push_start; 8 8 int push_end; 9 9 int apply_bytes; 10 + int pop_start; 11 + int pop_end; 10 12 11 13 struct { 12 14 __uint(type, BPF_MAP_TYPE_SOCKMAP); ··· 24 22 bpf_msg_cork_bytes(msg, cork_byte); 25 23 if (push_start > 0 && push_end > 0) 26 24 bpf_msg_push_data(msg, push_start, push_end, 0); 25 + if (pop_start >= 0 && pop_end > 0) 26 + bpf_msg_pop_data(msg, pop_start, pop_end, 0); 27 27 28 28 return SK_PASS; 29 29 }