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
6MY_DIR=$(dirname $0)
7TEST=$MY_DIR/test_cgrp2_sock
8CGRP_MNT="/tmp/cgroupv2-test_cgrp2_sock"
9
10################################################################################
11#
12print_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
22check_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
36check_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
53cleanup()
54{
55	echo $$ >> ${CGRP_MNT}/cgroup.procs
56	rmdir ${CGRP_MNT}/sockopts
57}
58
59cleanup_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
77rc=0
78
79ip li add cgrp2_sock type dummy 2>/dev/null
80
81set -e
82mkdir -p ${CGRP_MNT}
83mount -t cgroup2 none ${CGRP_MNT}
84set +e
85
86
87# make sure we have a known start point
88cleanup 2>/dev/null
89
90mkdir -p ${CGRP_MNT}/sockopts
91[ $? -ne 0 ] && cleanup_and_exit 1 "Failed to create cgroup hierarchy"
92
93
94# set pid into cgroup
95echo $$ > ${CGRP_MNT}/sockopts/cgroup.procs
96
97# no bpf program attached, so socket should show no settings
98check_sock "dev , mark 0, priority 0" "No programs attached"
99check_sock6 "dev , mark 0, priority 0" "No programs attached"
100
101# verify device is set
102#
103$TEST -b cgrp2_sock ${CGRP_MNT}/sockopts
104if [ $? -ne 0 ]; then
105	cleanup_and_exit 1 "Failed to install program to set device"
106fi
107check_sock "dev cgrp2_sock, mark 0, priority 0" "Device set"
108check_sock6 "dev cgrp2_sock, mark 0, priority 0" "Device set"
109
110# verify mark is set
111#
112$TEST -m 666 ${CGRP_MNT}/sockopts
113if [ $? -ne 0 ]; then
114	cleanup_and_exit 1 "Failed to install program to set mark"
115fi
116check_sock "dev , mark 666, priority 0" "Mark set"
117check_sock6 "dev , mark 666, priority 0" "Mark set"
118
119# verify priority is set
120#
121$TEST -p 123 ${CGRP_MNT}/sockopts
122if [ $? -ne 0 ]; then
123	cleanup_and_exit 1 "Failed to install program to set priority"
124fi
125check_sock "dev , mark 0, priority 123" "Priority set"
126check_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
131if [ $? -ne 0 ]; then
132	cleanup_and_exit 1 "Failed to install program to set device, mark and priority"
133fi
134check_sock "dev cgrp2_sock, mark 666, priority 123" "Priority set"
135check_sock6 "dev cgrp2_sock, mark 666, priority 123" "Priority set"
136
137cleanup_and_exit $rc
138