Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: GPL-2.0
0003 
0004 BPFFS=/sys/fs/bpf
0005 LINK_PIN=$BPFFS/test_cgrp2_sock2
0006 
0007 function config_device {
0008         ip netns add at_ns0
0009         ip link add veth0 type veth peer name veth0b
0010         ip link set veth0b up
0011         ip link set veth0 netns at_ns0
0012         ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
0013         ip netns exec at_ns0 ip addr add 2401:db00::1/64 dev veth0 nodad
0014         ip netns exec at_ns0 ip link set dev veth0 up
0015         ip addr add 172.16.1.101/24 dev veth0b
0016         ip addr add 2401:db00::2/64 dev veth0b nodad
0017 }
0018 
0019 function config_cgroup {
0020         rm -rf /tmp/cgroupv2
0021         mkdir -p /tmp/cgroupv2
0022         mount -t cgroup2 none /tmp/cgroupv2
0023         mkdir -p /tmp/cgroupv2/foo
0024         echo $$ >> /tmp/cgroupv2/foo/cgroup.procs
0025 }
0026 
0027 function config_bpffs {
0028         if mount | grep $BPFFS > /dev/null; then
0029                 echo "bpffs already mounted"
0030         else
0031                 echo "bpffs not mounted. Mounting..."
0032                 mount -t bpf none $BPFFS
0033         fi
0034 }
0035 
0036 function attach_bpf {
0037         ./test_cgrp2_sock2 /tmp/cgroupv2/foo sock_flags_kern.o $1
0038         [ $? -ne 0 ] && exit 1
0039 }
0040 
0041 function cleanup {
0042         rm -rf $LINK_PIN
0043         ip link del veth0b
0044         ip netns delete at_ns0
0045         umount /tmp/cgroupv2
0046         rm -rf /tmp/cgroupv2
0047 }
0048 
0049 cleanup 2>/dev/null
0050 
0051 set -e
0052 config_device
0053 config_cgroup
0054 config_bpffs
0055 set +e
0056 
0057 #
0058 # Test 1 - fail ping6
0059 #
0060 attach_bpf 0
0061 ping -c1 -w1 172.16.1.100
0062 if [ $? -ne 0 ]; then
0063         echo "ping failed when it should succeed"
0064         cleanup
0065         exit 1
0066 fi
0067 
0068 ping6 -c1 -w1 2401:db00::1
0069 if [ $? -eq 0 ]; then
0070         echo "ping6 succeeded when it should not"
0071         cleanup
0072         exit 1
0073 fi
0074 
0075 rm -rf $LINK_PIN
0076 sleep 1                 # Wait for link detach
0077 
0078 #
0079 # Test 2 - fail ping
0080 #
0081 attach_bpf 1
0082 ping6 -c1 -w1 2401:db00::1
0083 if [ $? -ne 0 ]; then
0084         echo "ping6 failed when it should succeed"
0085         cleanup
0086         exit 1
0087 fi
0088 
0089 ping -c1 -w1 172.16.1.100
0090 if [ $? -eq 0 ]; then
0091         echo "ping succeeded when it should not"
0092         cleanup
0093         exit 1
0094 fi
0095 
0096 cleanup
0097 echo
0098 echo "*** PASS ***"