Skip to content

Commit 80e24d2

Browse files
jrfastabborkmann
authored andcommitted
bpf, sockmap: Test FIONREAD returns correct bytes in rx buffer with drops
When BPF program drops pkts the sockmap logic 'eats' the packet and updates copied_seq. In the PASS case where the sk_buff is accepted we update copied_seq from recvmsg path so we need a new test to handle the drop case. Original patch series broke this resulting in test_sockmap_skb_verdict_fionread:PASS:ioctl(FIONREAD) error 0 nsec test_sockmap_skb_verdict_fionread:FAIL:ioctl(FIONREAD) unexpected ioctl(FIONREAD): actual 1503041772 != expected 256 After updated patch with fix. Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: Jakub Sitnicki <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent bb516f9 commit 80e24d2

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

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

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "test_sockmap_skb_verdict_attach.skel.h"
1212
#include "test_sockmap_progs_query.skel.h"
1313
#include "test_sockmap_pass_prog.skel.h"
14+
#include "test_sockmap_drop_prog.skel.h"
1415
#include "bpf_iter_sockmap.skel.h"
1516

1617
#include "sockmap_helpers.h"
@@ -410,19 +411,31 @@ static void test_sockmap_skb_verdict_shutdown(void)
410411
test_sockmap_pass_prog__destroy(skel);
411412
}
412413

413-
static void test_sockmap_skb_verdict_fionread(void)
414+
static void test_sockmap_skb_verdict_fionread(bool pass_prog)
414415
{
416+
int expected, zero = 0, sent, recvd, avail;
415417
int err, map, verdict, s, c0, c1, p0, p1;
416-
struct test_sockmap_pass_prog *skel;
417-
int zero = 0, sent, recvd, avail;
418+
struct test_sockmap_pass_prog *pass;
419+
struct test_sockmap_drop_prog *drop;
418420
char buf[256] = "0123456789";
419421

420-
skel = test_sockmap_pass_prog__open_and_load();
421-
if (!ASSERT_OK_PTR(skel, "open_and_load"))
422-
return;
422+
if (pass_prog) {
423+
pass = test_sockmap_pass_prog__open_and_load();
424+
if (!ASSERT_OK_PTR(pass, "open_and_load"))
425+
return;
426+
verdict = bpf_program__fd(pass->progs.prog_skb_verdict);
427+
map = bpf_map__fd(pass->maps.sock_map_rx);
428+
expected = sizeof(buf);
429+
} else {
430+
drop = test_sockmap_drop_prog__open_and_load();
431+
if (!ASSERT_OK_PTR(drop, "open_and_load"))
432+
return;
433+
verdict = bpf_program__fd(drop->progs.prog_skb_verdict);
434+
map = bpf_map__fd(drop->maps.sock_map_rx);
435+
/* On drop data is consumed immediately and copied_seq inc'd */
436+
expected = 0;
437+
}
423438

424-
verdict = bpf_program__fd(skel->progs.prog_skb_verdict);
425-
map = bpf_map__fd(skel->maps.sock_map_rx);
426439

427440
err = bpf_prog_attach(verdict, map, BPF_SK_SKB_STREAM_VERDICT, 0);
428441
if (!ASSERT_OK(err, "bpf_prog_attach"))
@@ -443,17 +456,23 @@ static void test_sockmap_skb_verdict_fionread(void)
443456
ASSERT_EQ(sent, sizeof(buf), "xsend(p0)");
444457
err = ioctl(c1, FIONREAD, &avail);
445458
ASSERT_OK(err, "ioctl(FIONREAD) error");
446-
ASSERT_EQ(avail, sizeof(buf), "ioctl(FIONREAD)");
447-
recvd = recv_timeout(c1, &buf, sizeof(buf), SOCK_NONBLOCK, IO_TIMEOUT_SEC);
448-
ASSERT_EQ(recvd, sizeof(buf), "recv_timeout(c0)");
459+
ASSERT_EQ(avail, expected, "ioctl(FIONREAD)");
460+
/* On DROP test there will be no data to read */
461+
if (pass_prog) {
462+
recvd = recv_timeout(c1, &buf, sizeof(buf), SOCK_NONBLOCK, IO_TIMEOUT_SEC);
463+
ASSERT_EQ(recvd, sizeof(buf), "recv_timeout(c0)");
464+
}
449465

450466
out_close:
451467
close(c0);
452468
close(p0);
453469
close(c1);
454470
close(p1);
455471
out:
456-
test_sockmap_pass_prog__destroy(skel);
472+
if (pass_prog)
473+
test_sockmap_pass_prog__destroy(pass);
474+
else
475+
test_sockmap_drop_prog__destroy(drop);
457476
}
458477

459478
void test_sockmap_basic(void)
@@ -493,5 +512,7 @@ void test_sockmap_basic(void)
493512
if (test__start_subtest("sockmap skb_verdict shutdown"))
494513
test_sockmap_skb_verdict_shutdown();
495514
if (test__start_subtest("sockmap skb_verdict fionread"))
496-
test_sockmap_skb_verdict_fionread();
515+
test_sockmap_skb_verdict_fionread(true);
516+
if (test__start_subtest("sockmap skb_verdict fionread on drop"))
517+
test_sockmap_skb_verdict_fionread(false);
497518
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <linux/bpf.h>
2+
#include <bpf/bpf_helpers.h>
3+
#include <bpf/bpf_endian.h>
4+
5+
struct {
6+
__uint(type, BPF_MAP_TYPE_SOCKMAP);
7+
__uint(max_entries, 20);
8+
__type(key, int);
9+
__type(value, int);
10+
} sock_map_rx SEC(".maps");
11+
12+
struct {
13+
__uint(type, BPF_MAP_TYPE_SOCKMAP);
14+
__uint(max_entries, 20);
15+
__type(key, int);
16+
__type(value, int);
17+
} sock_map_tx SEC(".maps");
18+
19+
struct {
20+
__uint(type, BPF_MAP_TYPE_SOCKMAP);
21+
__uint(max_entries, 20);
22+
__type(key, int);
23+
__type(value, int);
24+
} sock_map_msg SEC(".maps");
25+
26+
SEC("sk_skb")
27+
int prog_skb_verdict(struct __sk_buff *skb)
28+
{
29+
return SK_DROP;
30+
}
31+
32+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)