~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/bpf/test_cgrp2_sock.sh

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php