xref: /openbmc/linux/tools/testing/selftests/bpf/test_lwt_seg6local.sh (revision de167752a889d19b9bb018f8eecbc1ebbfe07b2f)
1#!/bin/bash
2# Connects 6 network namespaces through veths.
3# Each NS may have different IPv6 global scope addresses :
4#   NS1 ---- NS2 ---- NS3 ---- NS4 ---- NS5 ---- NS6
5# fb00::1           fd00::1  fd00::2  fd00::3  fb00::6
6#                   fc42::1           fd00::4
7#
8# All IPv6 packets going to fb00::/16 through NS2 will be encapsulated in a
9# IPv6 header with a Segment Routing Header, with segments :
10# 	fd00::1 -> fd00::2 -> fd00::3 -> fd00::4
11#
12# 3 fd00::/16 IPv6 addresses are binded to seg6local End.BPF actions :
13# - fd00::1 : add a TLV, change the flags and apply a End.X action to fc42::1
14# - fd00::2 : remove the TLV, change the flags, add a tag
15# - fd00::3 : apply an End.T action to fd00::4, through routing table 117
16#
17# fd00::4 is a simple Segment Routing node decapsulating the inner IPv6 packet.
18# Each End.BPF action will validate the operations applied on the SRH by the
19# previous BPF program in the chain, otherwise the packet is dropped.
20#
21# An UDP datagram is sent from fb00::1 to fb00::6. The test succeeds if this
22# datagram can be read on NS6 when binding to fb00::6.
23
24TMP_FILE="/tmp/selftest_lwt_seg6local.txt"
25
26cleanup()
27{
28	if [ "$?" = "0" ]; then
29		echo "selftests: test_lwt_seg6local [PASS]";
30	else
31		echo "selftests: test_lwt_seg6local [FAILED]";
32	fi
33
34	set +e
35	ip netns del ns1 2> /dev/null
36	ip netns del ns2 2> /dev/null
37	ip netns del ns3 2> /dev/null
38	ip netns del ns4 2> /dev/null
39	ip netns del ns5 2> /dev/null
40	ip netns del ns6 2> /dev/null
41	rm -f $TMP_FILE
42}
43
44set -e
45
46ip netns add ns1
47ip netns add ns2
48ip netns add ns3
49ip netns add ns4
50ip netns add ns5
51ip netns add ns6
52
53trap cleanup 0 2 3 6 9
54
55ip link add veth1 type veth peer name veth2
56ip link add veth3 type veth peer name veth4
57ip link add veth5 type veth peer name veth6
58ip link add veth7 type veth peer name veth8
59ip link add veth9 type veth peer name veth10
60
61ip link set veth1 netns ns1
62ip link set veth2 netns ns2
63ip link set veth3 netns ns2
64ip link set veth4 netns ns3
65ip link set veth5 netns ns3
66ip link set veth6 netns ns4
67ip link set veth7 netns ns4
68ip link set veth8 netns ns5
69ip link set veth9 netns ns5
70ip link set veth10 netns ns6
71
72ip netns exec ns1 ip link set dev veth1 up
73ip netns exec ns2 ip link set dev veth2 up
74ip netns exec ns2 ip link set dev veth3 up
75ip netns exec ns3 ip link set dev veth4 up
76ip netns exec ns3 ip link set dev veth5 up
77ip netns exec ns4 ip link set dev veth6 up
78ip netns exec ns4 ip link set dev veth7 up
79ip netns exec ns5 ip link set dev veth8 up
80ip netns exec ns5 ip link set dev veth9 up
81ip netns exec ns6 ip link set dev veth10 up
82ip netns exec ns6 ip link set dev lo up
83
84# All link scope addresses and routes required between veths
85ip netns exec ns1 ip -6 addr add fb00::12/16 dev veth1 scope link
86ip netns exec ns1 ip -6 route add fb00::21 dev veth1 scope link
87ip netns exec ns2 ip -6 addr add fb00::21/16 dev veth2 scope link
88ip netns exec ns2 ip -6 addr add fb00::34/16 dev veth3 scope link
89ip netns exec ns2 ip -6 route add fb00::43 dev veth3 scope link
90ip netns exec ns3 ip -6 route add fb00::65 dev veth5 scope link
91ip netns exec ns3 ip -6 addr add fb00::43/16 dev veth4 scope link
92ip netns exec ns3 ip -6 addr add fb00::56/16 dev veth5 scope link
93ip netns exec ns4 ip -6 addr add fb00::65/16 dev veth6 scope link
94ip netns exec ns4 ip -6 addr add fb00::78/16 dev veth7 scope link
95ip netns exec ns4 ip -6 route add fb00::87 dev veth7 scope link
96ip netns exec ns5 ip -6 addr add fb00::87/16 dev veth8 scope link
97ip netns exec ns5 ip -6 addr add fb00::910/16 dev veth9 scope link
98ip netns exec ns5 ip -6 route add fb00::109 dev veth9 scope link
99ip netns exec ns5 ip -6 route add fb00::109 table 117 dev veth9 scope link
100ip netns exec ns6 ip -6 addr add fb00::109/16 dev veth10 scope link
101
102ip netns exec ns1 ip -6 addr add fb00::1/16 dev lo
103ip netns exec ns1 ip -6 route add fb00::6 dev veth1 via fb00::21
104
105ip netns exec ns2 ip -6 route add fb00::6 encap bpf in obj test_lwt_seg6local.o sec encap_srh dev veth2
106ip netns exec ns2 ip -6 route add fd00::1 dev veth3 via fb00::43 scope link
107
108ip netns exec ns3 ip -6 route add fc42::1 dev veth5 via fb00::65
109ip netns exec ns3 ip -6 route add fd00::1 encap seg6local action End.BPF obj test_lwt_seg6local.o sec add_egr_x dev veth4
110
111ip netns exec ns4 ip -6 route add fd00::2 encap seg6local action End.BPF obj test_lwt_seg6local.o sec pop_egr dev veth6
112ip netns exec ns4 ip -6 addr add fc42::1 dev lo
113ip netns exec ns4 ip -6 route add fd00::3 dev veth7 via fb00::87
114
115ip netns exec ns5 ip -6 route add fd00::4 table 117 dev veth9 via fb00::109
116ip netns exec ns5 ip -6 route add fd00::3 encap seg6local action End.BPF obj test_lwt_seg6local.o sec inspect_t dev veth8
117
118ip netns exec ns6 ip -6 addr add fb00::6/16 dev lo
119ip netns exec ns6 ip -6 addr add fd00::4/16 dev lo
120
121ip netns exec ns1 sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
122ip netns exec ns2 sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
123ip netns exec ns3 sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
124ip netns exec ns4 sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
125ip netns exec ns5 sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
126
127ip netns exec ns6 sysctl net.ipv6.conf.all.seg6_enabled=1 > /dev/null
128ip netns exec ns6 sysctl net.ipv6.conf.lo.seg6_enabled=1 > /dev/null
129ip netns exec ns6 sysctl net.ipv6.conf.veth10.seg6_enabled=1 > /dev/null
130
131ip netns exec ns6 nc -l -6 -u -d 7330 > $TMP_FILE &
132ip netns exec ns1 bash -c "echo 'foobar' | nc -w0 -6 -u -p 2121 -s fb00::1 fb00::6 7330"
133sleep 5 # wait enough time to ensure the UDP datagram arrived to the last segment
134kill -INT $!
135
136if [[ $(< $TMP_FILE) != "foobar" ]]; then
137	exit 1
138fi
139
140exit 0
141