351955c2bdfd8048bc25e50e57b9c86b6d7c3be7
[platform/kernel/linux-rpi.git] / tools / testing / selftests / bpf / test_xdp_redirect_multi.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 #
4 # Test topology:
5 #     - - - - - - - - - - - - - - - - - - - - - - - - -
6 #    | veth1         veth2         veth3 |  ... init net
7 #     - -| - - - - - - | - - - - - - | - -
8 #    ---------     ---------     ---------
9 #    | veth0 |     | veth0 |     | veth0 |  ...
10 #    ---------     ---------     ---------
11 #       ns1           ns2           ns3
12 #
13 # Test modules:
14 # XDP modes: generic, native, native + egress_prog
15 #
16 # Test cases:
17 #   ARP: Testing BPF_F_BROADCAST, the ingress interface also should receive
18 #   the redirects.
19 #      ns1 -> gw: ns1, ns2, ns3, should receive the arp request
20 #   IPv4: Testing BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS, the ingress
21 #   interface should not receive the redirects.
22 #      ns1 -> gw: ns1 should not receive, ns2, ns3 should receive redirects.
23 #   IPv6: Testing none flag, all the pkts should be redirected back
24 #      ping test: ns1 -> ns2 (block), echo requests will be redirect back
25 #   egress_prog:
26 #      all src mac should be egress interface's mac
27
28 # netns numbers
29 NUM=3
30 IFACES=""
31 DRV_MODE="xdpgeneric xdpdrv xdpegress"
32 PASS=0
33 FAIL=0
34
35 test_pass()
36 {
37         echo "Pass: $@"
38         PASS=$((PASS + 1))
39 }
40
41 test_fail()
42 {
43         echo "fail: $@"
44         FAIL=$((FAIL + 1))
45 }
46
47 clean_up()
48 {
49         for i in $(seq $NUM); do
50                 ip link del veth$i 2> /dev/null
51                 ip netns del ns$i 2> /dev/null
52         done
53 }
54
55 # Kselftest framework requirement - SKIP code is 4.
56 check_env()
57 {
58         ip link set dev lo xdpgeneric off &>/dev/null
59         if [ $? -ne 0 ];then
60                 echo "selftests: [SKIP] Could not run test without the ip xdpgeneric support"
61                 exit 4
62         fi
63
64         which tcpdump &>/dev/null
65         if [ $? -ne 0 ];then
66                 echo "selftests: [SKIP] Could not run test without tcpdump"
67                 exit 4
68         fi
69 }
70
71 setup_ns()
72 {
73         local mode=$1
74         IFACES=""
75
76         if [ "$mode" = "xdpegress" ]; then
77                 mode="xdpdrv"
78         fi
79
80         for i in $(seq $NUM); do
81                 ip netns add ns$i
82                 ip link add veth$i type veth peer name veth0 netns ns$i
83                 ip link set veth$i up
84                 ip -n ns$i link set veth0 up
85
86                 ip -n ns$i addr add 192.0.2.$i/24 dev veth0
87                 ip -n ns$i addr add 2001:db8::$i/64 dev veth0
88                 # Add a neigh entry for IPv4 ping test
89                 ip -n ns$i neigh add 192.0.2.253 lladdr 00:00:00:00:00:01 dev veth0
90                 ip -n ns$i link set veth0 $mode obj \
91                         xdp_dummy.o sec xdp &> /dev/null || \
92                         { test_fail "Unable to load dummy xdp" && exit 1; }
93                 IFACES="$IFACES veth$i"
94                 veth_mac[$i]=$(ip link show veth$i | awk '/link\/ether/ {print $2}')
95         done
96 }
97
98 do_egress_tests()
99 {
100         local mode=$1
101
102         # mac test
103         ip netns exec ns2 tcpdump -e -i veth0 -nn -l -e &> mac_ns1-2_${mode}.log &
104         ip netns exec ns3 tcpdump -e -i veth0 -nn -l -e &> mac_ns1-3_${mode}.log &
105         sleep 0.5
106         ip netns exec ns1 ping 192.0.2.254 -i 0.1 -c 4 &> /dev/null
107         sleep 0.5
108         pkill -9 tcpdump
109
110         # mac check
111         grep -q "${veth_mac[2]} > ff:ff:ff:ff:ff:ff" mac_ns1-2_${mode}.log && \
112                test_pass "$mode mac ns1-2" || test_fail "$mode mac ns1-2"
113         grep -q "${veth_mac[3]} > ff:ff:ff:ff:ff:ff" mac_ns1-3_${mode}.log && \
114                 test_pass "$mode mac ns1-3" || test_fail "$mode mac ns1-3"
115 }
116
117 do_ping_tests()
118 {
119         local mode=$1
120
121         # ping6 test: echo request should be redirect back to itself, not others
122         ip netns exec ns1 ip neigh add 2001:db8::2 dev veth0 lladdr 00:00:00:00:00:02
123
124         ip netns exec ns1 tcpdump -i veth0 -nn -l -e &> ns1-1_${mode}.log &
125         ip netns exec ns2 tcpdump -i veth0 -nn -l -e &> ns1-2_${mode}.log &
126         ip netns exec ns3 tcpdump -i veth0 -nn -l -e &> ns1-3_${mode}.log &
127         sleep 0.5
128         # ARP test
129         ip netns exec ns1 ping 192.0.2.254 -i 0.1 -c 4 &> /dev/null
130         # IPv4 test
131         ip netns exec ns1 ping 192.0.2.253 -i 0.1 -c 4 &> /dev/null
132         # IPv6 test
133         ip netns exec ns1 ping6 2001:db8::2 -i 0.1 -c 2 &> /dev/null
134         sleep 0.5
135         pkill -9 tcpdump
136
137         # All netns should receive the redirect arp requests
138         [ $(grep -c "who-has 192.0.2.254" ns1-1_${mode}.log) -gt 4 ] && \
139                 test_pass "$mode arp(F_BROADCAST) ns1-1" || \
140                 test_fail "$mode arp(F_BROADCAST) ns1-1"
141         [ $(grep -c "who-has 192.0.2.254" ns1-2_${mode}.log) -le 4 ] && \
142                 test_pass "$mode arp(F_BROADCAST) ns1-2" || \
143                 test_fail "$mode arp(F_BROADCAST) ns1-2"
144         [ $(grep -c "who-has 192.0.2.254" ns1-3_${mode}.log) -le 4 ] && \
145                 test_pass "$mode arp(F_BROADCAST) ns1-3" || \
146                 test_fail "$mode arp(F_BROADCAST) ns1-3"
147
148         # ns1 should not receive the redirect echo request, others should
149         [ $(grep -c "ICMP echo request" ns1-1_${mode}.log) -eq 4 ] && \
150                 test_pass "$mode IPv4 (F_BROADCAST|F_EXCLUDE_INGRESS) ns1-1" || \
151                 test_fail "$mode IPv4 (F_BROADCAST|F_EXCLUDE_INGRESS) ns1-1"
152         [ $(grep -c "ICMP echo request" ns1-2_${mode}.log) -eq 4 ] && \
153                 test_pass "$mode IPv4 (F_BROADCAST|F_EXCLUDE_INGRESS) ns1-2" || \
154                 test_fail "$mode IPv4 (F_BROADCAST|F_EXCLUDE_INGRESS) ns1-2"
155         [ $(grep -c "ICMP echo request" ns1-3_${mode}.log) -eq 4 ] && \
156                 test_pass "$mode IPv4 (F_BROADCAST|F_EXCLUDE_INGRESS) ns1-3" || \
157                 test_fail "$mode IPv4 (F_BROADCAST|F_EXCLUDE_INGRESS) ns1-3"
158
159         # ns1 should receive the echo request, ns2 should not
160         [ $(grep -c "ICMP6, echo request" ns1-1_${mode}.log) -eq 4 ] && \
161                 test_pass "$mode IPv6 (no flags) ns1-1" || \
162                 test_fail "$mode IPv6 (no flags) ns1-1"
163         [ $(grep -c "ICMP6, echo request" ns1-2_${mode}.log) -eq 0 ] && \
164                 test_pass "$mode IPv6 (no flags) ns1-2" || \
165                 test_fail "$mode IPv6 (no flags) ns1-2"
166 }
167
168 do_tests()
169 {
170         local mode=$1
171         local drv_p
172
173         case ${mode} in
174                 xdpdrv)  drv_p="-N";;
175                 xdpegress) drv_p="-X";;
176                 xdpgeneric) drv_p="-S";;
177         esac
178
179         ./xdp_redirect_multi $drv_p $IFACES &> xdp_redirect_${mode}.log &
180         xdp_pid=$!
181         sleep 1
182
183         if [ "$mode" = "xdpegress" ]; then
184                 do_egress_tests $mode
185         else
186                 do_ping_tests $mode
187         fi
188
189         kill $xdp_pid
190 }
191
192 trap clean_up 0 2 3 6 9
193
194 check_env
195 rm -f xdp_redirect_*.log ns*.log mac_ns*.log
196
197 for mode in ${DRV_MODE}; do
198         setup_ns $mode
199         do_tests $mode
200         clean_up
201 done
202
203 echo "Summary: PASS $PASS, FAIL $FAIL"
204 [ $FAIL -eq 0 ] && exit 0 || exit 1