Skip to content

Commit b648972

Browse files
mrpreKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
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 <[email protected]> Reviewed-by: John Fastabend <[email protected]>
1 parent 957b5e9 commit b648972

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,95 @@ static void test_sockmap_ktls_tx_no_buf(int family, int sotype, bool push)
314314
test_sockmap_ktls__destroy(skel);
315315
}
316316

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+
317406
static void run_tests(int family, enum bpf_map_type map_type)
318407
{
319408
int map;
@@ -338,6 +427,8 @@ static void run_ktls_test(int family, int sotype)
338427
test_sockmap_ktls_tx_cork(family, sotype, true);
339428
if (test__start_subtest("tls tx egress with no buf"))
340429
test_sockmap_ktls_tx_no_buf(family, sotype, true);
430+
if (test__start_subtest("tls tx with pop"))
431+
test_sockmap_ktls_tx_pop(family, sotype);
341432
}
342433

343434
void test_sockmap_ktls(void)

tools/testing/selftests/bpf/progs/test_sockmap_ktls.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ int cork_byte;
77
int push_start;
88
int push_end;
99
int apply_bytes;
10+
int pop_start;
11+
int pop_end;
1012

1113
struct {
1214
__uint(type, BPF_MAP_TYPE_SOCKMAP);
@@ -22,6 +24,8 @@ int prog_sk_policy(struct sk_msg_md *msg)
2224
bpf_msg_cork_bytes(msg, cork_byte);
2325
if (push_start > 0 && push_end > 0)
2426
bpf_msg_push_data(msg, push_start, push_end, 0);
27+
if (pop_start >= 0 && pop_end > 0)
28+
bpf_msg_pop_data(msg, pop_start, pop_end, 0);
2529

2630
return SK_PASS;
2731
}

0 commit comments

Comments
 (0)