blob: 9f617423685618fab136aaf41743578f1d4f2bd2 [file] [log] [blame]
David Ahern33aeb5e2017-08-31 15:05:49 -07001#!/bin/sh
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01002# SPDX-License-Identifier: GPL-2.0
David Ahernad2805d2016-12-01 08:48:05 -08003
David Ahern33aeb5e2017-08-31 15:05:49 -07004# 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"
David Ahernad2805d2016-12-01 08:48:05 -080018}
19
David Ahern33aeb5e2017-08-31 15:05:49 -070020check_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
David Ahernad2805d2016-12-01 08:48:05 -080032}
33
David Ahern33aeb5e2017-08-31 15:05:49 -070034check_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
David Ahernad2805d2016-12-01 08:48:05 -080046}
47
David Ahern33aeb5e2017-08-31 15:05:49 -070048################################################################################
49#
50
51cleanup()
52{
53 echo $$ >> ${CGRP_MNT}/cgroup.procs
54 rmdir ${CGRP_MNT}/sockopts
David Ahernad2805d2016-12-01 08:48:05 -080055}
56
David Ahern33aeb5e2017-08-31 15:05:49 -070057cleanup_and_exit()
58{
59 local rc=$1
60 local msg="$2"
61
62 [ -n "$msg" ] && echo "ERROR: $msg"
63
Prashant Bholec8745e02018-03-01 14:47:40 +090064 test_cgrp2_sock -d ${CGRP_MNT}/sockopts
David Ahern33aeb5e2017-08-31 15:05:49 -070065 ip li del cgrp2_sock
66 umount ${CGRP_MNT}
67
68 exit $rc
69}
70
71
72################################################################################
73# main
74
75rc=0
76
77ip li add cgrp2_sock type dummy 2>/dev/null
78
79set -e
80mkdir -p ${CGRP_MNT}
81mount -t cgroup2 none ${CGRP_MNT}
82set +e
83
84
85# make sure we have a known start point
David Ahernad2805d2016-12-01 08:48:05 -080086cleanup 2>/dev/null
David Ahern33aeb5e2017-08-31 15:05:49 -070087
88mkdir -p ${CGRP_MNT}/sockopts
89[ $? -ne 0 ] && cleanup_and_exit 1 "Failed to create cgroup hierarchy"
90
91
92# set pid into cgroup
93echo $$ > ${CGRP_MNT}/sockopts/cgroup.procs
94
95# no bpf program attached, so socket should show no settings
96check_sock "dev , mark 0, priority 0" "No programs attached"
97check_sock6 "dev , mark 0, priority 0" "No programs attached"
98
99# verify device is set
100#
101test_cgrp2_sock -b cgrp2_sock ${CGRP_MNT}/sockopts
102if [ $? -ne 0 ]; then
103 cleanup_and_exit 1 "Failed to install program to set device"
104fi
105check_sock "dev cgrp2_sock, mark 0, priority 0" "Device set"
106check_sock6 "dev cgrp2_sock, mark 0, priority 0" "Device set"
107
108# verify mark is set
109#
110test_cgrp2_sock -m 666 ${CGRP_MNT}/sockopts
111if [ $? -ne 0 ]; then
112 cleanup_and_exit 1 "Failed to install program to set mark"
113fi
114check_sock "dev , mark 666, priority 0" "Mark set"
115check_sock6 "dev , mark 666, priority 0" "Mark set"
116
117# verify priority is set
118#
119test_cgrp2_sock -p 123 ${CGRP_MNT}/sockopts
120if [ $? -ne 0 ]; then
121 cleanup_and_exit 1 "Failed to install program to set priority"
122fi
123check_sock "dev , mark 0, priority 123" "Priority set"
124check_sock6 "dev , mark 0, priority 123" "Priority set"
125
126# all 3 at once
127#
128test_cgrp2_sock -b cgrp2_sock -m 666 -p 123 ${CGRP_MNT}/sockopts
129if [ $? -ne 0 ]; then
130 cleanup_and_exit 1 "Failed to install program to set device, mark and priority"
131fi
132check_sock "dev cgrp2_sock, mark 666, priority 123" "Priority set"
133check_sock6 "dev cgrp2_sock, mark 666, priority 123" "Priority set"
134
135cleanup_and_exit $rc