David Ahern | 33aeb5e | 2017-08-31 15:05:49 -0700 | [diff] [blame] | 1 | #!/bin/sh |
David Ahern | ad2805d | 2016-12-01 08:48:05 -0800 | [diff] [blame] | 2 | |
David Ahern | 33aeb5e | 2017-08-31 15:05:49 -0700 | [diff] [blame] | 3 | # Test various socket options that can be set by attaching programs to cgroups. |
| 4 | |
| 5 | CGRP_MNT="/tmp/cgroupv2-test_cgrp2_sock" |
| 6 | |
| 7 | ################################################################################ |
| 8 | # |
| 9 | print_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" |
David Ahern | ad2805d | 2016-12-01 08:48:05 -0800 | [diff] [blame] | 17 | } |
| 18 | |
David Ahern | 33aeb5e | 2017-08-31 15:05:49 -0700 | [diff] [blame] | 19 | check_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 |
David Ahern | ad2805d | 2016-12-01 08:48:05 -0800 | [diff] [blame] | 31 | } |
| 32 | |
David Ahern | 33aeb5e | 2017-08-31 15:05:49 -0700 | [diff] [blame] | 33 | check_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 |
David Ahern | ad2805d | 2016-12-01 08:48:05 -0800 | [diff] [blame] | 45 | } |
| 46 | |
David Ahern | 33aeb5e | 2017-08-31 15:05:49 -0700 | [diff] [blame] | 47 | ################################################################################ |
| 48 | # |
| 49 | |
| 50 | cleanup() |
| 51 | { |
| 52 | echo $$ >> ${CGRP_MNT}/cgroup.procs |
| 53 | rmdir ${CGRP_MNT}/sockopts |
David Ahern | ad2805d | 2016-12-01 08:48:05 -0800 | [diff] [blame] | 54 | } |
| 55 | |
David Ahern | 33aeb5e | 2017-08-31 15:05:49 -0700 | [diff] [blame] | 56 | cleanup_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 | |
| 73 | rc=0 |
| 74 | |
| 75 | ip li add cgrp2_sock type dummy 2>/dev/null |
| 76 | |
| 77 | set -e |
| 78 | mkdir -p ${CGRP_MNT} |
| 79 | mount -t cgroup2 none ${CGRP_MNT} |
| 80 | set +e |
| 81 | |
| 82 | |
| 83 | # make sure we have a known start point |
David Ahern | ad2805d | 2016-12-01 08:48:05 -0800 | [diff] [blame] | 84 | cleanup 2>/dev/null |
David Ahern | 33aeb5e | 2017-08-31 15:05:49 -0700 | [diff] [blame] | 85 | |
| 86 | mkdir -p ${CGRP_MNT}/sockopts |
| 87 | [ $? -ne 0 ] && cleanup_and_exit 1 "Failed to create cgroup hierarchy" |
| 88 | |
| 89 | |
| 90 | # set pid into cgroup |
| 91 | echo $$ > ${CGRP_MNT}/sockopts/cgroup.procs |
| 92 | |
| 93 | # no bpf program attached, so socket should show no settings |
| 94 | check_sock "dev , mark 0, priority 0" "No programs attached" |
| 95 | check_sock6 "dev , mark 0, priority 0" "No programs attached" |
| 96 | |
| 97 | # verify device is set |
| 98 | # |
| 99 | test_cgrp2_sock -b cgrp2_sock ${CGRP_MNT}/sockopts |
| 100 | if [ $? -ne 0 ]; then |
| 101 | cleanup_and_exit 1 "Failed to install program to set device" |
| 102 | fi |
| 103 | check_sock "dev cgrp2_sock, mark 0, priority 0" "Device set" |
| 104 | check_sock6 "dev cgrp2_sock, mark 0, priority 0" "Device set" |
| 105 | |
| 106 | # verify mark is set |
| 107 | # |
| 108 | test_cgrp2_sock -m 666 ${CGRP_MNT}/sockopts |
| 109 | if [ $? -ne 0 ]; then |
| 110 | cleanup_and_exit 1 "Failed to install program to set mark" |
| 111 | fi |
| 112 | check_sock "dev , mark 666, priority 0" "Mark set" |
| 113 | check_sock6 "dev , mark 666, priority 0" "Mark set" |
| 114 | |
| 115 | # verify priority is set |
| 116 | # |
| 117 | test_cgrp2_sock -p 123 ${CGRP_MNT}/sockopts |
| 118 | if [ $? -ne 0 ]; then |
| 119 | cleanup_and_exit 1 "Failed to install program to set priority" |
| 120 | fi |
| 121 | check_sock "dev , mark 0, priority 123" "Priority set" |
| 122 | check_sock6 "dev , mark 0, priority 123" "Priority set" |
| 123 | |
| 124 | # all 3 at once |
| 125 | # |
| 126 | test_cgrp2_sock -b cgrp2_sock -m 666 -p 123 ${CGRP_MNT}/sockopts |
| 127 | if [ $? -ne 0 ]; then |
| 128 | cleanup_and_exit 1 "Failed to install program to set device, mark and priority" |
| 129 | fi |
| 130 | check_sock "dev cgrp2_sock, mark 666, priority 123" "Priority set" |
| 131 | check_sock6 "dev cgrp2_sock, mark 666, priority 123" "Priority set" |
| 132 | |
| 133 | cleanup_and_exit $rc |