2 # SPDX-License-Identifier: GPL-2.0
5 # Details on the bpf prog
6 BPF_CGRP2_ARRAY_NAME='test_cgrp2_array_pin'
7 BPF_PROG="$MY_DIR/test_cgrp2_tc.bpf.o"
10 [ -z "$TC" ] && TC='tc'
11 [ -z "$IP" ] && IP='ip'
13 # Names of the veth interface, net namespace...etc.
20 awk '{ if ($3 == "'$1'" && mnt == "") { mnt = $2 }} END { print mnt }'
25 CGRP2_ROOT=$(find_mnt cgroup2)
26 if [ -z "$CGRP2_ROOT" ]
28 CGRP2_ROOT='/mnt/cgroup2'
31 CGRP2_TC="$CGRP2_ROOT/tc"
32 CGRP2_TC_LEAF="$CGRP2_TC/leaf"
37 local bpf_fs_root=$(find_mnt bpf)
38 [ -n "$bpf_fs_root" ] || return -1
39 BPF_FS_TC_SHARE="$bpf_fs_root/tc/globals"
45 if [ "$MOUNT_CGRP2" == 'yes' ]
47 [ -d $CGRP2_ROOT ] || mkdir -p $CGRP2_ROOT
48 mount -t cgroup2 none $CGRP2_ROOT || return $?
50 mkdir -p $CGRP2_TC_LEAF
53 rmdir $CGRP2_TC_LEAF && rmdir $CGRP2_TC
54 [ "$MOUNT_CGRP2" == 'yes' ] && umount $CGRP2_ROOT
59 setup_bpf_cgrp2_array() {
60 local bpf_cgrp2_array="$BPF_FS_TC_SHARE/$BPF_CGRP2_ARRAY_NAME"
63 $MY_DIR/test_cgrp2_array_pin -U $bpf_cgrp2_array -v $CGRP2_TC
66 [ -d "$BPF_FS_TC_SHARE" ] && rm -f $bpf_cgrp2_array
74 $IP link add $HOST_IFC type veth peer name $NS_IFC || return $?
75 $IP link set dev $HOST_IFC up || return $?
76 sysctl -q net.ipv6.conf.$HOST_IFC.disable_ipv6=0
77 sysctl -q net.ipv6.conf.$HOST_IFC.accept_dad=0
79 $IP netns add $NS || return $?
80 $IP link set dev $NS_IFC netns $NS || return $?
81 $IP -n $NS link set dev $NS_IFC up || return $?
82 $IP netns exec $NS sysctl -q net.ipv6.conf.$NS_IFC.disable_ipv6=0
83 $IP netns exec $NS sysctl -q net.ipv6.conf.$NS_IFC.accept_dad=0
84 $TC qdisc add dev $HOST_IFC clsact || return $?
85 $TC filter add dev $HOST_IFC egress bpf da obj $BPF_PROG sec $BPF_SECTION || return $?
89 $IP link del $HOST_IFC
95 # Fork another bash and move it under the specified cgroup.
96 # It makes the cgroup cleanup easier at the end of the test.
98 cmd="$cmd $1/cgroup.procs; exec $2"
103 run_in_cgrp $CGRP2_TC_LEAF "ping -6 -c3 ff02::1%$HOST_IFC >& /dev/null"
104 local dropped=$($TC -s qdisc show dev $HOST_IFC | tail -3 | \
105 awk '/drop/{print substr($7, 0, index($7, ",")-1)}')
106 if [[ $dropped -eq 0 ]]
111 echo "Successfully filtered $dropped packets"
117 if [ "$DEBUG" == "yes" ] && [ "$MODE" != 'cleanuponly' ]
119 echo "------ DEBUG ------"
120 echo "mount: "; mount | grep -E '(cgroup2|bpf)'; echo
121 echo "$CGRP2_TC_LEAF: "; ls -l $CGRP2_TC_LEAF; echo
122 if [ -d "$BPF_FS_TC_SHARE" ]
124 echo "$BPF_FS_TC_SHARE: "; ls -l $BPF_FS_TC_SHARE; echo
128 $IP link show dev $HOST_IFC
129 $IP -6 a show dev $HOST_IFC
130 $TC -s qdisc show dev $HOST_IFC
133 $IP -n $NS link show dev $NS_IFC
134 $IP -n $NS -6 link show dev $NS_IFC
135 echo "------ DEBUG ------"
139 if [ "$MODE" != 'nocleanup' ]
142 setup_bpf_cgrp2_array stop
167 echo "test_cgrp2_tc [debug] [cleanup-only | no-cleanup]"
168 echo " debug: Print cgrp and network setup details at the end of the test"
169 echo " cleanup-only: Try to cleanup things from last test. No test will be run"
170 echo " no-cleanup: Run the test but don't do cleanup at the end"
171 echo "[Note: If no arg is given, it will run the test and do cleanup at the end]"
180 [ "$MODE" == 'cleanuponly' ] && exit
182 setup_cgrp2 start || exit $?
183 setup_net start || exit $?
184 init_bpf_fs_vars || exit $?
185 setup_bpf_cgrp2_array start || exit $?