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