Skip to content

Commit 6cc73f3

Browse files
Martin KaFai LauAlexei Starovoitov
authored andcommitted
selftests/bpf: Test bpf_skb_check_mtu(BPF_MTU_CHK_SEGS) when transport_header is not set
Add a test to check that bpf_skb_check_mtu(BPF_MTU_CHK_SEGS) is rejected (-EINVAL) if skb->transport_header is not set. The test needs to lower the MTU of the loopback device. Thus, take this opportunity to run the test in a netns by adding "ns_" to the test name. The "serial_" prefix can then be removed. Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent d946f3c commit 6cc73f3

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,26 @@ static void test_check_mtu_run_tc(struct test_check_mtu *skel,
153153
ASSERT_EQ(mtu_result, mtu_expect, "MTU-compare-user");
154154
}
155155

156+
static void test_chk_segs_flag(struct test_check_mtu *skel, __u32 mtu)
157+
{
158+
int err, prog_fd = bpf_program__fd(skel->progs.tc_chk_segs_flag);
159+
struct __sk_buff skb = {
160+
.gso_size = 10,
161+
};
162+
LIBBPF_OPTS(bpf_test_run_opts, topts,
163+
.data_in = &pkt_v4,
164+
.data_size_in = sizeof(pkt_v4),
165+
.ctx_in = &skb,
166+
.ctx_size_in = sizeof(skb),
167+
);
168+
169+
/* Lower the mtu to test the BPF_MTU_CHK_SEGS */
170+
SYS_NOFAIL("ip link set dev lo mtu 10");
171+
err = bpf_prog_test_run_opts(prog_fd, &topts);
172+
SYS_NOFAIL("ip link set dev lo mtu %u", mtu);
173+
ASSERT_OK(err, "test_run");
174+
ASSERT_EQ(topts.retval, BPF_OK, "retval");
175+
}
156176

157177
static void test_check_mtu_tc(__u32 mtu, __u32 ifindex)
158178
{
@@ -177,11 +197,12 @@ static void test_check_mtu_tc(__u32 mtu, __u32 ifindex)
177197
test_check_mtu_run_tc(skel, skel->progs.tc_minus_delta, mtu);
178198
test_check_mtu_run_tc(skel, skel->progs.tc_input_len, mtu);
179199
test_check_mtu_run_tc(skel, skel->progs.tc_input_len_exceed, mtu);
200+
test_chk_segs_flag(skel, mtu);
180201
cleanup:
181202
test_check_mtu__destroy(skel);
182203
}
183204

184-
void serial_test_check_mtu(void)
205+
void test_ns_check_mtu(void)
185206
{
186207
int mtu_lo;
187208

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <stddef.h>
99
#include <stdint.h>
10+
#include <errno.h>
1011

1112
char _license[] SEC("license") = "GPL";
1213

@@ -288,3 +289,14 @@ int tc_input_len_exceed(struct __sk_buff *ctx)
288289
global_bpf_mtu_xdp = mtu_len;
289290
return retval;
290291
}
292+
293+
SEC("tc")
294+
int tc_chk_segs_flag(struct __sk_buff *ctx)
295+
{
296+
__u32 mtu_len = 0;
297+
int err;
298+
299+
err = bpf_check_mtu(ctx, GLOBAL_USER_IFINDEX, &mtu_len, 0, BPF_MTU_CHK_SEGS);
300+
301+
return err == -EINVAL ? BPF_OK : BPF_DROP;
302+
}

0 commit comments

Comments
 (0)