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