summaryrefslogtreecommitdiffstats
path: root/samples/bpf/test_cgrp2_sock.sh
blob: a81f38eef417443858ddcaea3bb63711589e9fd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/sh

# Test various socket options that can be set by attaching programs to cgroups.

CGRP_MNT="/tmp/cgroupv2-test_cgrp2_sock"

################################################################################
#
print_result()
{
	local rc=$1
	local status=" OK "

	[ $rc -ne 0 ] && status="FAIL"

	printf "%-50s    [%4s]\n" "$2" "$status"
}

check_sock()
{
	out=$(test_cgrp2_sock)
	echo $out | grep -q "$1"
	if [ $? -ne 0 ]; then
		print_result 1 "IPv4: $2"
		echo "    expected: $1"
		echo "        have: $out"
		rc=1
	else
		print_result 0 "IPv4: $2"
	fi
}

check_sock6()
{
	out=$(test_cgrp2_sock -6)
	echo $out | grep -q "$1"
	if [ $? -ne 0 ]; then
		print_result 1 "IPv6: $2"
		echo "    expected: $1"
		echo "        have: $out"
		rc=1
	else
		print_result 0 "IPv6: $2"
	fi
}

################################################################################
#

cleanup()
{
	echo $$ >> ${CGRP_MNT}/cgroup.procs
	rmdir ${CGRP_MNT}/sockopts
}

cleanup_and_exit()
{
	local rc=$1
	local msg="$2"

	[ -n "$msg" ] && echo "ERROR: $msg"

	ip li del cgrp2_sock
	umount ${CGRP_MNT}

	exit $rc
}


################################################################################
# main

rc=0

ip li add cgrp2_sock type dummy 2>/dev/null

set -e
mkdir -p ${CGRP_MNT}
mount -t cgroup2 none ${CGRP_MNT}
set +e


# make sure we have a known start point
cleanup 2>/dev/null

mkdir -p ${CGRP_MNT}/sockopts
[ $? -ne 0 ] && cleanup_and_exit 1 "Failed to create cgroup hierarchy"


# set pid into cgroup
echo $$ > ${CGRP_MNT}/sockopts/cgroup.procs

# no bpf program attached, so socket should show no settings
check_sock "dev , mark 0, priority 0" "No programs attached"
check_sock6 "dev , mark 0, priority 0" "No programs attached"

# verify device is set
#
test_cgrp2_sock -b cgrp2_sock ${CGRP_MNT}/sockopts
if [ $? -ne 0 ]; then
	cleanup_and_exit 1 "Failed to install program to set device"
fi
check_sock "dev cgrp2_sock, mark 0, priority 0" "Device set"
check_sock6 "dev cgrp2_sock, mark 0, priority 0" "Device set"

# verify mark is set
#
test_cgrp2_sock -m 666 ${CGRP_MNT}/sockopts
if [ $? -ne 0 ]; then
	cleanup_and_exit 1 "Failed to install program to set mark"
fi
check_sock "dev , mark 666, priority 0" "Mark set"
check_sock6 "dev , mark 666, priority 0" "Mark set"

# verify priority is set
#
test_cgrp2_sock -p 123 ${CGRP_MNT}/sockopts
if [ $? -ne 0 ]; then
	cleanup_and_exit 1 "Failed to install program to set priority"
fi
check_sock "dev , mark 0, priority 123" "Priority set"
check_sock6 "dev , mark 0, priority 123" "Priority set"

# all 3 at once
#
test_cgrp2_sock -b cgrp2_sock -m 666 -p 123 ${CGRP_MNT}/sockopts
if [ $? -ne 0 ]; then
	cleanup_and_exit 1 "Failed to install program to set device, mark and priority"
fi
check_sock "dev cgrp2_sock, mark 666, priority 123" "Priority set"
check_sock6 "dev cgrp2_sock, mark 666, priority 123" "Priority set"

cleanup_and_exit $rc