xref: /linux/samples/bpf/test_cgrp2_sock.sh (revision 5cc619db5c34146ee821acd08382b9d78579b52e)
1#!/bin/sh
2
3# Test various socket options that can be set by attaching programs to cgroups.
4
5CGRP_MNT="/tmp/cgroupv2-test_cgrp2_sock"
6
7################################################################################
8#
9print_result()
10{
11	local rc=$1
12	local status=" OK "
13
14	[ $rc -ne 0 ] && status="FAIL"
15
16	printf "%-50s    [%4s]\n" "$2" "$status"
17}
18
19check_sock()
20{
21	out=$(test_cgrp2_sock)
22	echo $out | grep -q "$1"
23	if [ $? -ne 0 ]; then
24		print_result 1 "IPv4: $2"
25		echo "    expected: $1"
26		echo "        have: $out"
27		rc=1
28	else
29		print_result 0 "IPv4: $2"
30	fi
31}
32
33check_sock6()
34{
35	out=$(test_cgrp2_sock -6)
36	echo $out | grep -q "$1"
37	if [ $? -ne 0 ]; then
38		print_result 1 "IPv6: $2"
39		echo "    expected: $1"
40		echo "        have: $out"
41		rc=1
42	else
43		print_result 0 "IPv6: $2"
44	fi
45}
46
47################################################################################
48#
49
50cleanup()
51{
52	echo $$ >> ${CGRP_MNT}/cgroup.procs
53	rmdir ${CGRP_MNT}/sockopts
54}
55
56cleanup_and_exit()
57{
58	local rc=$1
59	local msg="$2"
60
61	[ -n "$msg" ] && echo "ERROR: $msg"
62
63	ip li del cgrp2_sock
64	umount ${CGRP_MNT}
65
66	exit $rc
67}
68
69
70################################################################################
71# main
72
73rc=0
74
75ip li add cgrp2_sock type dummy 2>/dev/null
76
77set -e
78mkdir -p ${CGRP_MNT}
79mount -t cgroup2 none ${CGRP_MNT}
80set +e
81
82
83# make sure we have a known start point
84cleanup 2>/dev/null
85
86mkdir -p ${CGRP_MNT}/sockopts
87[ $? -ne 0 ] && cleanup_and_exit 1 "Failed to create cgroup hierarchy"
88
89
90# set pid into cgroup
91echo $$ > ${CGRP_MNT}/sockopts/cgroup.procs
92
93# no bpf program attached, so socket should show no settings
94check_sock "dev , mark 0, priority 0" "No programs attached"
95check_sock6 "dev , mark 0, priority 0" "No programs attached"
96
97# verify device is set
98#
99test_cgrp2_sock -b cgrp2_sock ${CGRP_MNT}/sockopts
100if [ $? -ne 0 ]; then
101	cleanup_and_exit 1 "Failed to install program to set device"
102fi
103check_sock "dev cgrp2_sock, mark 0, priority 0" "Device set"
104check_sock6 "dev cgrp2_sock, mark 0, priority 0" "Device set"
105
106# verify mark is set
107#
108test_cgrp2_sock -m 666 ${CGRP_MNT}/sockopts
109if [ $? -ne 0 ]; then
110	cleanup_and_exit 1 "Failed to install program to set mark"
111fi
112check_sock "dev , mark 666, priority 0" "Mark set"
113check_sock6 "dev , mark 666, priority 0" "Mark set"
114
115# verify priority is set
116#
117test_cgrp2_sock -p 123 ${CGRP_MNT}/sockopts
118if [ $? -ne 0 ]; then
119	cleanup_and_exit 1 "Failed to install program to set priority"
120fi
121check_sock "dev , mark 0, priority 123" "Priority set"
122check_sock6 "dev , mark 0, priority 123" "Priority set"
123
124# all 3 at once
125#
126test_cgrp2_sock -b cgrp2_sock -m 666 -p 123 ${CGRP_MNT}/sockopts
127if [ $? -ne 0 ]; then
128	cleanup_and_exit 1 "Failed to install program to set device, mark and priority"
129fi
130check_sock "dev cgrp2_sock, mark 666, priority 123" "Priority set"
131check_sock6 "dev cgrp2_sock, mark 666, priority 123" "Priority set"
132
133cleanup_and_exit $rc
134