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

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'selftests-bpf-migrate-test_xdp_redirect-sh-to-test_progs'

Bastien Curutchet says:

====================
This patch series continues the work to migrate the *.sh tests into
prog_tests.

test_xdp_redirect.sh tests the XDP redirections done through
bpf_redirect().

These XDP redirections are already tested by prog_tests/xdp_do_redirect.c
but IMO it doesn't cover the exact same code path because
xdp_do_redirect.c uses bpf_prog_test_run_opts() to trigger redirections
of 'fake packets' while test_xdp_redirect.sh redirects packets coming
from the network. Also, the test_xdp_redirect.sh script tests the
redirections with both SKB and DRV modes while xdp_do_redirect.c only
tests the DRV mode.

The patch series adds two new test cases in prog_tests/xdp_do_redirect.c
to replace the test_xdp_redirect.sh script.
====================

Link: https://patch.msgid.link/20250110-xdp_redirect-v2-0-b8f3ae53e894@bootlin.com
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

+176 -106
-1
tools/testing/selftests/bpf/Makefile
··· 100 100 101 101 # Order correspond to 'make run_tests' order 102 102 TEST_PROGS := test_kmod.sh \ 103 - test_xdp_redirect.sh \ 104 103 test_xdp_redirect_multi.sh \ 105 104 test_xdp_meta.sh \ 106 105 test_tunnel.sh \
+164
tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c
··· 11 11 #include <bpf/bpf_endian.h> 12 12 #include <uapi/linux/netdev.h> 13 13 #include "test_xdp_do_redirect.skel.h" 14 + #include "xdp_dummy.skel.h" 14 15 15 16 struct udp_packet { 16 17 struct ethhdr eth; ··· 247 246 SYS_NOFAIL("ip netns del testns"); 248 247 test_xdp_do_redirect__destroy(skel); 249 248 } 249 + 250 + #define NS_NB 3 251 + #define NS0 "NS0" 252 + #define NS1 "NS1" 253 + #define NS2 "NS2" 254 + #define IPV4_NETWORK "10.1.1" 255 + #define VETH1_INDEX 111 256 + #define VETH2_INDEX 222 257 + 258 + struct test_data { 259 + struct netns_obj *ns[NS_NB]; 260 + u32 xdp_flags; 261 + }; 262 + 263 + static void cleanup(struct test_data *data) 264 + { 265 + int i; 266 + 267 + for (i = 0; i < NS_NB; i++) 268 + netns_free(data->ns[i]); 269 + } 270 + 271 + /** 272 + * ping_setup - 273 + * Create two veth peers and forward packets in-between using XDP 274 + * 275 + * ------------ ------------ 276 + * | NS1 | | NS2 | 277 + * | veth0 | | veth0 | 278 + * | 10.1.1.1 | | 10.1.1.2 | 279 + * -----|------ ------|----- 280 + * | | 281 + * | | 282 + * -----|-----------------------|------- 283 + * | veth1 veth2 | 284 + * | (id:111) (id:222) | 285 + * | | | | 286 + * | ----- xdp forwarding ----- | 287 + * | | 288 + * | NS0 | 289 + * ------------------------------------- 290 + */ 291 + static int ping_setup(struct test_data *data) 292 + { 293 + int i; 294 + 295 + data->ns[0] = netns_new(NS0, false); 296 + if (!ASSERT_OK_PTR(data->ns[0], "create ns")) 297 + return -1; 298 + 299 + for (i = 1; i < NS_NB; i++) { 300 + char ns_name[4] = {}; 301 + 302 + snprintf(ns_name, 4, "NS%d", i); 303 + data->ns[i] = netns_new(ns_name, false); 304 + if (!ASSERT_OK_PTR(data->ns[i], "create ns")) 305 + goto fail; 306 + 307 + SYS(fail, 308 + "ip -n %s link add veth%d index %d%d%d type veth peer name veth0 netns %s", 309 + NS0, i, i, i, i, ns_name); 310 + SYS(fail, "ip -n %s link set veth%d up", NS0, i); 311 + 312 + SYS(fail, "ip -n %s addr add %s.%d/24 dev veth0", ns_name, IPV4_NETWORK, i); 313 + SYS(fail, "ip -n %s link set veth0 up", ns_name); 314 + } 315 + 316 + return 0; 317 + 318 + fail: 319 + cleanup(data); 320 + return -1; 321 + } 322 + 323 + static void ping_test(struct test_data *data) 324 + { 325 + struct test_xdp_do_redirect *skel = NULL; 326 + struct xdp_dummy *skel_dummy = NULL; 327 + struct nstoken *nstoken = NULL; 328 + int i, ret; 329 + 330 + skel_dummy = xdp_dummy__open_and_load(); 331 + if (!ASSERT_OK_PTR(skel_dummy, "open and load xdp_dummy skeleton")) 332 + goto close; 333 + 334 + for (i = 1; i < NS_NB; i++) { 335 + char ns_name[4] = {}; 336 + 337 + snprintf(ns_name, 4, "NS%d", i); 338 + nstoken = open_netns(ns_name); 339 + if (!ASSERT_OK_PTR(nstoken, "open ns")) 340 + goto close; 341 + 342 + ret = bpf_xdp_attach(if_nametoindex("veth0"), 343 + bpf_program__fd(skel_dummy->progs.xdp_dummy_prog), 344 + data->xdp_flags, NULL); 345 + if (!ASSERT_GE(ret, 0, "bpf_xdp_attach dummy_prog")) 346 + goto close; 347 + 348 + close_netns(nstoken); 349 + nstoken = NULL; 350 + } 351 + 352 + skel = test_xdp_do_redirect__open_and_load(); 353 + if (!ASSERT_OK_PTR(skel, "open and load skeleton")) 354 + goto close; 355 + 356 + nstoken = open_netns(NS0); 357 + if (!ASSERT_OK_PTR(nstoken, "open NS0")) 358 + goto close; 359 + 360 + ret = bpf_xdp_attach(VETH2_INDEX, 361 + bpf_program__fd(skel->progs.xdp_redirect_to_111), 362 + data->xdp_flags, NULL); 363 + if (!ASSERT_GE(ret, 0, "bpf_xdp_attach")) 364 + goto close; 365 + 366 + ret = bpf_xdp_attach(VETH1_INDEX, 367 + bpf_program__fd(skel->progs.xdp_redirect_to_222), 368 + data->xdp_flags, NULL); 369 + if (!ASSERT_GE(ret, 0, "bpf_xdp_attach")) 370 + goto close; 371 + 372 + close_netns(nstoken); 373 + nstoken = NULL; 374 + 375 + nstoken = open_netns(NS1); 376 + if (!ASSERT_OK_PTR(nstoken, "open NS1")) 377 + goto close; 378 + 379 + SYS(close, "ping -c 1 %s.2 > /dev/null", IPV4_NETWORK); 380 + 381 + close: 382 + close_netns(nstoken); 383 + xdp_dummy__destroy(skel_dummy); 384 + test_xdp_do_redirect__destroy(skel); 385 + } 386 + 387 + 388 + static void xdp_redirect_ping(u32 xdp_flags) 389 + { 390 + struct test_data data = {}; 391 + 392 + if (ping_setup(&data) < 0) 393 + return; 394 + 395 + data.xdp_flags = xdp_flags; 396 + ping_test(&data); 397 + cleanup(&data); 398 + } 399 + 400 + void test_xdp_index_redirect(void) 401 + { 402 + if (test__start_subtest("noflag")) 403 + xdp_redirect_ping(0); 404 + 405 + if (test__start_subtest("drvflag")) 406 + xdp_redirect_ping(XDP_FLAGS_DRV_MODE); 407 + 408 + if (test__start_subtest("skbflag")) 409 + xdp_redirect_ping(XDP_FLAGS_SKB_MODE); 410 + } 411 +
+12
tools/testing/selftests/bpf/progs/test_xdp_do_redirect.c
··· 98 98 return XDP_DROP; 99 99 } 100 100 101 + SEC("xdp") 102 + int xdp_redirect_to_111(struct xdp_md *xdp) 103 + { 104 + return bpf_redirect(111, 0); 105 + } 106 + 107 + SEC("xdp") 108 + int xdp_redirect_to_222(struct xdp_md *xdp) 109 + { 110 + return bpf_redirect(222, 0); 111 + } 112 + 101 113 SEC("tc") 102 114 int tc_count_pkts(struct __sk_buff *skb) 103 115 {
-26
tools/testing/selftests/bpf/progs/test_xdp_redirect.c
··· 1 - /* Copyright (c) 2017 VMware 2 - * 3 - * This program is free software; you can redistribute it and/or 4 - * modify it under the terms of version 2 of the GNU General Public 5 - * License as published by the Free Software Foundation. 6 - * 7 - * This program is distributed in the hope that it will be useful, but 8 - * WITHOUT ANY WARRANTY; without even the implied warranty of 9 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 - * General Public License for more details. 11 - */ 12 - #include <linux/bpf.h> 13 - #include <bpf/bpf_helpers.h> 14 - 15 - SEC("redirect_to_111") 16 - int xdp_redirect_to_111(struct xdp_md *xdp) 17 - { 18 - return bpf_redirect(111, 0); 19 - } 20 - SEC("redirect_to_222") 21 - int xdp_redirect_to_222(struct xdp_md *xdp) 22 - { 23 - return bpf_redirect(222, 0); 24 - } 25 - 26 - char _license[] SEC("license") = "GPL";
-79
tools/testing/selftests/bpf/test_xdp_redirect.sh
··· 1 - #!/bin/bash 2 - # Create 2 namespaces with two veth peers, and 3 - # forward packets in-between using generic XDP 4 - # 5 - # NS1(veth11) NS2(veth22) 6 - # | | 7 - # | | 8 - # (veth1, ------ (veth2, 9 - # id:111) id:222) 10 - # | xdp forwarding | 11 - # ------------------ 12 - 13 - readonly NS1="ns1-$(mktemp -u XXXXXX)" 14 - readonly NS2="ns2-$(mktemp -u XXXXXX)" 15 - ret=0 16 - 17 - setup() 18 - { 19 - 20 - local xdpmode=$1 21 - 22 - ip netns add ${NS1} 23 - ip netns add ${NS2} 24 - 25 - ip link add veth1 index 111 type veth peer name veth11 netns ${NS1} 26 - ip link add veth2 index 222 type veth peer name veth22 netns ${NS2} 27 - 28 - ip link set veth1 up 29 - ip link set veth2 up 30 - ip -n ${NS1} link set dev veth11 up 31 - ip -n ${NS2} link set dev veth22 up 32 - 33 - ip -n ${NS1} addr add 10.1.1.11/24 dev veth11 34 - ip -n ${NS2} addr add 10.1.1.22/24 dev veth22 35 - } 36 - 37 - cleanup() 38 - { 39 - ip link del veth1 2> /dev/null 40 - ip link del veth2 2> /dev/null 41 - ip netns del ${NS1} 2> /dev/null 42 - ip netns del ${NS2} 2> /dev/null 43 - } 44 - 45 - test_xdp_redirect() 46 - { 47 - local xdpmode=$1 48 - 49 - setup 50 - 51 - ip link set dev veth1 $xdpmode off &> /dev/null 52 - if [ $? -ne 0 ];then 53 - echo "selftests: test_xdp_redirect $xdpmode [SKIP]" 54 - return 0 55 - fi 56 - 57 - ip -n ${NS1} link set veth11 $xdpmode obj xdp_dummy.bpf.o sec xdp &> /dev/null 58 - ip -n ${NS2} link set veth22 $xdpmode obj xdp_dummy.bpf.o sec xdp &> /dev/null 59 - ip link set dev veth1 $xdpmode obj test_xdp_redirect.bpf.o sec redirect_to_222 &> /dev/null 60 - ip link set dev veth2 $xdpmode obj test_xdp_redirect.bpf.o sec redirect_to_111 &> /dev/null 61 - 62 - if ip netns exec ${NS1} ping -c 1 10.1.1.22 &> /dev/null && 63 - ip netns exec ${NS2} ping -c 1 10.1.1.11 &> /dev/null; then 64 - echo "selftests: test_xdp_redirect $xdpmode [PASS]"; 65 - else 66 - ret=1 67 - echo "selftests: test_xdp_redirect $xdpmode [FAILED]"; 68 - fi 69 - 70 - cleanup 71 - } 72 - 73 - set -e 74 - trap cleanup 2 3 6 9 75 - 76 - test_xdp_redirect xdpgeneric 77 - test_xdp_redirect xdpdrv 78 - 79 - exit $ret