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

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 <martin.lau@kernel.org>
Link: https://lore.kernel.org/r/20251112232331.1566074-2-martin.lau@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Martin KaFai Lau and committed by
Alexei Starovoitov
6cc73f35 d946f3c9

+34 -1
+22 -1
tools/testing/selftests/bpf/prog_tests/check_mtu.c
··· 153 153 ASSERT_EQ(mtu_result, mtu_expect, "MTU-compare-user"); 154 154 } 155 155 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 + } 156 176 157 177 static void test_check_mtu_tc(__u32 mtu, __u32 ifindex) 158 178 { ··· 197 177 test_check_mtu_run_tc(skel, skel->progs.tc_minus_delta, mtu); 198 178 test_check_mtu_run_tc(skel, skel->progs.tc_input_len, mtu); 199 179 test_check_mtu_run_tc(skel, skel->progs.tc_input_len_exceed, mtu); 180 + test_chk_segs_flag(skel, mtu); 200 181 cleanup: 201 182 test_check_mtu__destroy(skel); 202 183 } 203 184 204 - void serial_test_check_mtu(void) 185 + void test_ns_check_mtu(void) 205 186 { 206 187 int mtu_lo; 207 188
+12
tools/testing/selftests/bpf/progs/test_check_mtu.c
··· 7 7 8 8 #include <stddef.h> 9 9 #include <stdint.h> 10 + #include <errno.h> 10 11 11 12 char _license[] SEC("license") = "GPL"; 12 13 ··· 288 287 289 288 global_bpf_mtu_xdp = mtu_len; 290 289 return retval; 290 + } 291 + 292 + SEC("tc") 293 + int tc_chk_segs_flag(struct __sk_buff *ctx) 294 + { 295 + __u32 mtu_len = 0; 296 + int err; 297 + 298 + err = bpf_check_mtu(ctx, GLOBAL_USER_IFINDEX, &mtu_len, 0, BPF_MTU_CHK_SEGS); 299 + 300 + return err == -EINVAL ? BPF_OK : BPF_DROP; 291 301 }