Merge tag 'usb-4.19-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
[linux-2.6/btrfs-unstable.git] / samples / bpf / test_cgrp2_sock.sh
blob9f617423685618fab136aaf41743578f1d4f2bd2
1 #!/bin/sh
2 # SPDX-License-Identifier: GPL-2.0
4 # Test various socket options that can be set by attaching programs to cgroups.
6 CGRP_MNT="/tmp/cgroupv2-test_cgrp2_sock"
8 ################################################################################
10 print_result()
12 local rc=$1
13 local status=" OK "
15 [ $rc -ne 0 ] && status="FAIL"
17 printf "%-50s [%4s]\n" "$2" "$status"
20 check_sock()
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"
34 check_sock6()
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"
48 ################################################################################
51 cleanup()
53 echo $$ >> ${CGRP_MNT}/cgroup.procs
54 rmdir ${CGRP_MNT}/sockopts
57 cleanup_and_exit()
59 local rc=$1
60 local msg="$2"
62 [ -n "$msg" ] && echo "ERROR: $msg"
64 test_cgrp2_sock -d ${CGRP_MNT}/sockopts
65 ip li del cgrp2_sock
66 umount ${CGRP_MNT}
68 exit $rc
72 ################################################################################
73 # main
75 rc=0
77 ip li add cgrp2_sock type dummy 2>/dev/null
79 set -e
80 mkdir -p ${CGRP_MNT}
81 mount -t cgroup2 none ${CGRP_MNT}
82 set +e
85 # make sure we have a known start point
86 cleanup 2>/dev/null
88 mkdir -p ${CGRP_MNT}/sockopts
89 [ $? -ne 0 ] && cleanup_and_exit 1 "Failed to create cgroup hierarchy"
92 # set pid into cgroup
93 echo $$ > ${CGRP_MNT}/sockopts/cgroup.procs
95 # no bpf program attached, so socket should show no settings
96 check_sock "dev , mark 0, priority 0" "No programs attached"
97 check_sock6 "dev , mark 0, priority 0" "No programs attached"
99 # verify device is set
101 test_cgrp2_sock -b cgrp2_sock ${CGRP_MNT}/sockopts
102 if [ $? -ne 0 ]; then
103 cleanup_and_exit 1 "Failed to install program to set device"
105 check_sock "dev cgrp2_sock, mark 0, priority 0" "Device set"
106 check_sock6 "dev cgrp2_sock, mark 0, priority 0" "Device set"
108 # verify mark is set
110 test_cgrp2_sock -m 666 ${CGRP_MNT}/sockopts
111 if [ $? -ne 0 ]; then
112 cleanup_and_exit 1 "Failed to install program to set mark"
114 check_sock "dev , mark 666, priority 0" "Mark set"
115 check_sock6 "dev , mark 666, priority 0" "Mark set"
117 # verify priority is set
119 test_cgrp2_sock -p 123 ${CGRP_MNT}/sockopts
120 if [ $? -ne 0 ]; then
121 cleanup_and_exit 1 "Failed to install program to set priority"
123 check_sock "dev , mark 0, priority 123" "Priority set"
124 check_sock6 "dev , mark 0, priority 123" "Priority set"
126 # all 3 at once
128 test_cgrp2_sock -b cgrp2_sock -m 666 -p 123 ${CGRP_MNT}/sockopts
129 if [ $? -ne 0 ]; then
130 cleanup_and_exit 1 "Failed to install program to set device, mark and priority"
132 check_sock "dev cgrp2_sock, mark 666, priority 123" "Priority set"
133 check_sock6 "dev cgrp2_sock, mark 666, priority 123" "Priority set"
135 cleanup_and_exit $rc