Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # Connects 6 network namespaces through veths.
0003 # Each NS may have different IPv6 global scope addresses :
0004 #   NS1 ---- NS2 ---- NS3 ---- NS4 ---- NS5 ---- NS6
0005 # fb00::1           fd00::1  fd00::2  fd00::3  fb00::6
0006 #                   fc42::1           fd00::4
0007 #
0008 # All IPv6 packets going to fb00::/16 through NS2 will be encapsulated in a
0009 # IPv6 header with a Segment Routing Header, with segments :
0010 #       fd00::1 -> fd00::2 -> fd00::3 -> fd00::4
0011 #
0012 # 3 fd00::/16 IPv6 addresses are binded to seg6local End.BPF actions :
0013 # - fd00::1 : add a TLV, change the flags and apply a End.X action to fc42::1
0014 # - fd00::2 : remove the TLV, change the flags, add a tag
0015 # - fd00::3 : apply an End.T action to fd00::4, through routing table 117
0016 #
0017 # fd00::4 is a simple Segment Routing node decapsulating the inner IPv6 packet.
0018 # Each End.BPF action will validate the operations applied on the SRH by the
0019 # previous BPF program in the chain, otherwise the packet is dropped.
0020 #
0021 # An UDP datagram is sent from fb00::1 to fb00::6. The test succeeds if this
0022 # datagram can be read on NS6 when binding to fb00::6.
0023 
0024 # Kselftest framework requirement - SKIP code is 4.
0025 ksft_skip=4
0026 readonly NS1="ns1-$(mktemp -u XXXXXX)"
0027 readonly NS2="ns2-$(mktemp -u XXXXXX)"
0028 readonly NS3="ns3-$(mktemp -u XXXXXX)"
0029 readonly NS4="ns4-$(mktemp -u XXXXXX)"
0030 readonly NS5="ns5-$(mktemp -u XXXXXX)"
0031 readonly NS6="ns6-$(mktemp -u XXXXXX)"
0032 
0033 msg="skip all tests:"
0034 if [ $UID != 0 ]; then
0035         echo $msg please run this as root >&2
0036         exit $ksft_skip
0037 fi
0038 
0039 TMP_FILE="/tmp/selftest_lwt_seg6local.txt"
0040 
0041 cleanup()
0042 {
0043         if [ "$?" = "0" ]; then
0044                 echo "selftests: test_lwt_seg6local [PASS]";
0045         else
0046                 echo "selftests: test_lwt_seg6local [FAILED]";
0047         fi
0048 
0049         set +e
0050         ip netns del ${NS1} 2> /dev/null
0051         ip netns del ${NS2} 2> /dev/null
0052         ip netns del ${NS3} 2> /dev/null
0053         ip netns del ${NS4} 2> /dev/null
0054         ip netns del ${NS5} 2> /dev/null
0055         ip netns del ${NS6} 2> /dev/null
0056         rm -f $TMP_FILE
0057 }
0058 
0059 set -e
0060 
0061 ip netns add ${NS1}
0062 ip netns add ${NS2}
0063 ip netns add ${NS3}
0064 ip netns add ${NS4}
0065 ip netns add ${NS5}
0066 ip netns add ${NS6}
0067 
0068 trap cleanup 0 2 3 6 9
0069 
0070 ip link add veth1 type veth peer name veth2
0071 ip link add veth3 type veth peer name veth4
0072 ip link add veth5 type veth peer name veth6
0073 ip link add veth7 type veth peer name veth8
0074 ip link add veth9 type veth peer name veth10
0075 
0076 ip link set veth1 netns ${NS1}
0077 ip link set veth2 netns ${NS2}
0078 ip link set veth3 netns ${NS2}
0079 ip link set veth4 netns ${NS3}
0080 ip link set veth5 netns ${NS3}
0081 ip link set veth6 netns ${NS4}
0082 ip link set veth7 netns ${NS4}
0083 ip link set veth8 netns ${NS5}
0084 ip link set veth9 netns ${NS5}
0085 ip link set veth10 netns ${NS6}
0086 
0087 ip netns exec ${NS1} ip link set dev veth1 up
0088 ip netns exec ${NS2} ip link set dev veth2 up
0089 ip netns exec ${NS2} ip link set dev veth3 up
0090 ip netns exec ${NS3} ip link set dev veth4 up
0091 ip netns exec ${NS3} ip link set dev veth5 up
0092 ip netns exec ${NS4} ip link set dev veth6 up
0093 ip netns exec ${NS4} ip link set dev veth7 up
0094 ip netns exec ${NS5} ip link set dev veth8 up
0095 ip netns exec ${NS5} ip link set dev veth9 up
0096 ip netns exec ${NS6} ip link set dev veth10 up
0097 ip netns exec ${NS6} ip link set dev lo up
0098 
0099 # All link scope addresses and routes required between veths
0100 ip netns exec ${NS1} ip -6 addr add fb00::12/16 dev veth1 scope link
0101 ip netns exec ${NS1} ip -6 route add fb00::21 dev veth1 scope link
0102 ip netns exec ${NS2} ip -6 addr add fb00::21/16 dev veth2 scope link
0103 ip netns exec ${NS2} ip -6 addr add fb00::34/16 dev veth3 scope link
0104 ip netns exec ${NS2} ip -6 route add fb00::43 dev veth3 scope link
0105 ip netns exec ${NS3} ip -6 route add fb00::65 dev veth5 scope link
0106 ip netns exec ${NS3} ip -6 addr add fb00::43/16 dev veth4 scope link
0107 ip netns exec ${NS3} ip -6 addr add fb00::56/16 dev veth5 scope link
0108 ip netns exec ${NS4} ip -6 addr add fb00::65/16 dev veth6 scope link
0109 ip netns exec ${NS4} ip -6 addr add fb00::78/16 dev veth7 scope link
0110 ip netns exec ${NS4} ip -6 route add fb00::87 dev veth7 scope link
0111 ip netns exec ${NS5} ip -6 addr add fb00::87/16 dev veth8 scope link
0112 ip netns exec ${NS5} ip -6 addr add fb00::910/16 dev veth9 scope link
0113 ip netns exec ${NS5} ip -6 route add fb00::109 dev veth9 scope link
0114 ip netns exec ${NS5} ip -6 route add fb00::109 table 117 dev veth9 scope link
0115 ip netns exec ${NS6} ip -6 addr add fb00::109/16 dev veth10 scope link
0116 
0117 ip netns exec ${NS1} ip -6 addr add fb00::1/16 dev lo
0118 ip netns exec ${NS1} ip -6 route add fb00::6 dev veth1 via fb00::21
0119 
0120 ip netns exec ${NS2} ip -6 route add fb00::6 encap bpf in obj test_lwt_seg6local.o sec encap_srh dev veth2
0121 ip netns exec ${NS2} ip -6 route add fd00::1 dev veth3 via fb00::43 scope link
0122 
0123 ip netns exec ${NS3} ip -6 route add fc42::1 dev veth5 via fb00::65
0124 ip netns exec ${NS3} ip -6 route add fd00::1 encap seg6local action End.BPF endpoint obj test_lwt_seg6local.o sec add_egr_x dev veth4
0125 
0126 ip netns exec ${NS4} ip -6 route add fd00::2 encap seg6local action End.BPF endpoint obj test_lwt_seg6local.o sec pop_egr dev veth6
0127 ip netns exec ${NS4} ip -6 addr add fc42::1 dev lo
0128 ip netns exec ${NS4} ip -6 route add fd00::3 dev veth7 via fb00::87
0129 
0130 ip netns exec ${NS5} ip -6 route add fd00::4 table 117 dev veth9 via fb00::109
0131 ip netns exec ${NS5} ip -6 route add fd00::3 encap seg6local action End.BPF endpoint obj test_lwt_seg6local.o sec inspect_t dev veth8
0132 
0133 ip netns exec ${NS6} ip -6 addr add fb00::6/16 dev lo
0134 ip netns exec ${NS6} ip -6 addr add fd00::4/16 dev lo
0135 
0136 ip netns exec ${NS1} sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
0137 ip netns exec ${NS2} sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
0138 ip netns exec ${NS3} sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
0139 ip netns exec ${NS4} sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
0140 ip netns exec ${NS5} sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
0141 
0142 ip netns exec ${NS6} sysctl net.ipv6.conf.all.seg6_enabled=1 > /dev/null
0143 ip netns exec ${NS6} sysctl net.ipv6.conf.lo.seg6_enabled=1 > /dev/null
0144 ip netns exec ${NS6} sysctl net.ipv6.conf.veth10.seg6_enabled=1 > /dev/null
0145 
0146 ip netns exec ${NS6} nc -l -6 -u -d 7330 > $TMP_FILE &
0147 ip netns exec ${NS1} bash -c "echo 'foobar' | nc -w0 -6 -u -p 2121 -s fb00::1 fb00::6 7330"
0148 sleep 5 # wait enough time to ensure the UDP datagram arrived to the last segment
0149 kill -TERM $!
0150 
0151 if [[ $(< $TMP_FILE) != "foobar" ]]; then
0152         exit 1
0153 fi
0154 
0155 exit 0