Skip to content

Commit 996139e

Browse files
williamtudavem330
authored andcommitted
selftests: bpf: add a test for XDP redirect
Add test for xdp_redirect by creating two namespaces with two veth peers, then forward packets in-between. Signed-off-by: William Tu <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: John Fastabend <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Acked-by: John Fastabend <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ea6404c commit 996139e

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

tools/include/uapi/linux/bpf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,8 @@ union bpf_attr {
592592
FN(get_socket_uid), \
593593
FN(set_hash), \
594594
FN(setsockopt), \
595-
FN(skb_adjust_room),
595+
FN(skb_adjust_room), \
596+
FN(redirect_map),
596597

597598
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
598599
* function eBPF program intends to call

tools/testing/selftests/bpf/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
1515
test_align
1616

1717
TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test_obj_id.o \
18-
test_pkt_md_access.o
18+
test_pkt_md_access.o test_xdp_redirect.o
1919

20-
TEST_PROGS := test_kmod.sh
20+
TEST_PROGS := test_kmod.sh test_xdp_redirect.sh
2121

2222
include ../lib.mk
2323

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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_helpers.h"
14+
15+
int _version SEC("version") = 1;
16+
17+
SEC("redirect_to_111")
18+
int xdp_redirect_to_111(struct xdp_md *xdp)
19+
{
20+
return bpf_redirect(111, 0);
21+
}
22+
SEC("redirect_to_222")
23+
int xdp_redirect_to_222(struct xdp_md *xdp)
24+
{
25+
return bpf_redirect(222, 0);
26+
}
27+
28+
char _license[] SEC("license") = "GPL";
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/sh
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+
cleanup()
14+
{
15+
if [ "$?" = "0" ]; then
16+
echo "selftests: test_xdp_redirect [PASS]";
17+
else
18+
echo "selftests: test_xdp_redirect [FAILED]";
19+
fi
20+
21+
set +e
22+
ip netns del ns1 2> /dev/null
23+
ip netns del ns2 2> /dev/null
24+
}
25+
26+
set -e
27+
28+
ip netns add ns1
29+
ip netns add ns2
30+
31+
trap cleanup 0 2 3 6 9
32+
33+
ip link add veth1 index 111 type veth peer name veth11
34+
ip link add veth2 index 222 type veth peer name veth22
35+
36+
ip link set veth11 netns ns1
37+
ip link set veth22 netns ns2
38+
39+
ip link set veth1 up
40+
ip link set veth2 up
41+
42+
ip netns exec ns1 ip addr add 10.1.1.11/24 dev veth11
43+
ip netns exec ns2 ip addr add 10.1.1.22/24 dev veth22
44+
45+
ip netns exec ns1 ip link set dev veth11 up
46+
ip netns exec ns2 ip link set dev veth22 up
47+
48+
ip link set dev veth1 xdpgeneric obj test_xdp_redirect.o sec redirect_to_222
49+
ip link set dev veth2 xdpgeneric obj test_xdp_redirect.o sec redirect_to_111
50+
51+
ip netns exec ns1 ping -c 1 10.1.1.22
52+
ip netns exec ns2 ping -c 1 10.1.1.11
53+
54+
exit 0

0 commit comments

Comments
 (0)