1 #!/bin/sh 2 # SPDX-License-Identifier: GPL-2.0 3 4 # Test various socket options that can be set by attaching programs to cgroups. 5 6 MY_DIR=$(dirname $0) 7 TEST=$MY_DIR/test_cgrp2_sock 8 CGRP_MNT="/tmp/cgroupv2-test_cgrp2_sock" 9 10 ################################################################################ 11 # 12 print_result() 13 { 14 local rc=$1 15 local status=" OK " 16 17 [ $rc -ne 0 ] && status="FAIL" 18 19 printf "%-50s [%4s]\n" "$2" "$status" 20 } 21 22 check_sock() 23 { 24 out=$($TEST) 25 echo $out | grep -q "$1" 26 if [ $? -ne 0 ]; then 27 print_result 1 "IPv4: $2" 28 echo " expected: $1" 29 echo " have: $out" 30 rc=1 31 else 32 print_result 0 "IPv4: $2" 33 fi 34 } 35 36 check_sock6() 37 { 38 out=$($TEST -6) 39 echo $out | grep -q "$1" 40 if [ $? -ne 0 ]; then 41 print_result 1 "IPv6: $2" 42 echo " expected: $1" 43 echo " have: $out" 44 rc=1 45 else 46 print_result 0 "IPv6: $2" 47 fi 48 } 49 50 ################################################################################ 51 # 52 53 cleanup() 54 { 55 echo $$ >> ${CGRP_MNT}/cgroup.procs 56 rmdir ${CGRP_MNT}/sockopts 57 } 58 59 cleanup_and_exit() 60 { 61 local rc=$1 62 local msg="$2" 63 64 [ -n "$msg" ] && echo "ERROR: $msg" 65 66 $TEST -d ${CGRP_MNT}/sockopts 67 ip li del cgrp2_sock 68 umount ${CGRP_MNT} 69 70 exit $rc 71 } 72 73 74 ################################################################################ 75 # main 76 77 rc=0 78 79 ip li add cgrp2_sock type dummy 2>/dev/null 80 81 set -e 82 mkdir -p ${CGRP_MNT} 83 mount -t cgroup2 none ${CGRP_MNT} 84 set +e 85 86 87 # make sure we have a known start point 88 cleanup 2>/dev/null 89 90 mkdir -p ${CGRP_MNT}/sockopts 91 [ $? -ne 0 ] && cleanup_and_exit 1 "Failed to create cgroup hierarchy" 92 93 94 # set pid into cgroup 95 echo $$ > ${CGRP_MNT}/sockopts/cgroup.procs 96 97 # no bpf program attached, so socket should show no settings 98 check_sock "dev , mark 0, priority 0" "No programs attached" 99 check_sock6 "dev , mark 0, priority 0" "No programs attached" 100 101 # verify device is set 102 # 103 $TEST -b cgrp2_sock ${CGRP_MNT}/sockopts 104 if [ $? -ne 0 ]; then 105 cleanup_and_exit 1 "Failed to install program to set device" 106 fi 107 check_sock "dev cgrp2_sock, mark 0, priority 0" "Device set" 108 check_sock6 "dev cgrp2_sock, mark 0, priority 0" "Device set" 109 110 # verify mark is set 111 # 112 $TEST -m 666 ${CGRP_MNT}/sockopts 113 if [ $? -ne 0 ]; then 114 cleanup_and_exit 1 "Failed to install program to set mark" 115 fi 116 check_sock "dev , mark 666, priority 0" "Mark set" 117 check_sock6 "dev , mark 666, priority 0" "Mark set" 118 119 # verify priority is set 120 # 121 $TEST -p 123 ${CGRP_MNT}/sockopts 122 if [ $? -ne 0 ]; then 123 cleanup_and_exit 1 "Failed to install program to set priority" 124 fi 125 check_sock "dev , mark 0, priority 123" "Priority set" 126 check_sock6 "dev , mark 0, priority 123" "Priority set" 127 128 # all 3 at once 129 # 130 $TEST -b cgrp2_sock -m 666 -p 123 ${CGRP_MNT}/sockopts 131 if [ $? -ne 0 ]; then 132 cleanup_and_exit 1 "Failed to install program to set device, mark and priority" 133 fi 134 check_sock "dev cgrp2_sock, mark 666, priority 123" "Priority set" 135 check_sock6 "dev cgrp2_sock, mark 666, priority 123" "Priority set" 136 137 cleanup_and_exit $rc
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.