test_cgrp2_tc.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/bin/bash
  2. MY_DIR=$(dirname $0)
  3. # Details on the bpf prog
  4. BPF_CGRP2_ARRAY_NAME='test_cgrp2_array_pin'
  5. BPF_PROG="$MY_DIR/test_cgrp2_tc_kern.o"
  6. BPF_SECTION='filter'
  7. [ -z "$TC" ] && TC='tc'
  8. [ -z "$IP" ] && IP='ip'
  9. # Names of the veth interface, net namespace...etc.
  10. HOST_IFC='ve'
  11. NS_IFC='vens'
  12. NS='ns'
  13. find_mnt() {
  14. cat /proc/mounts | \
  15. awk '{ if ($3 == "'$1'" && mnt == "") { mnt = $2 }} END { print mnt }'
  16. }
  17. # Init cgroup2 vars
  18. init_cgrp2_vars() {
  19. CGRP2_ROOT=$(find_mnt cgroup2)
  20. if [ -z "$CGRP2_ROOT" ]
  21. then
  22. CGRP2_ROOT='/mnt/cgroup2'
  23. MOUNT_CGRP2="yes"
  24. fi
  25. CGRP2_TC="$CGRP2_ROOT/tc"
  26. CGRP2_TC_LEAF="$CGRP2_TC/leaf"
  27. }
  28. # Init bpf fs vars
  29. init_bpf_fs_vars() {
  30. local bpf_fs_root=$(find_mnt bpf)
  31. [ -n "$bpf_fs_root" ] || return -1
  32. BPF_FS_TC_SHARE="$bpf_fs_root/tc/globals"
  33. }
  34. setup_cgrp2() {
  35. case $1 in
  36. start)
  37. if [ "$MOUNT_CGRP2" == 'yes' ]
  38. then
  39. [ -d $CGRP2_ROOT ] || mkdir -p $CGRP2_ROOT
  40. mount -t cgroup2 none $CGRP2_ROOT || return $?
  41. fi
  42. mkdir -p $CGRP2_TC_LEAF
  43. ;;
  44. *)
  45. rmdir $CGRP2_TC_LEAF && rmdir $CGRP2_TC
  46. [ "$MOUNT_CGRP2" == 'yes' ] && umount $CGRP2_ROOT
  47. ;;
  48. esac
  49. }
  50. setup_bpf_cgrp2_array() {
  51. local bpf_cgrp2_array="$BPF_FS_TC_SHARE/$BPF_CGRP2_ARRAY_NAME"
  52. case $1 in
  53. start)
  54. $MY_DIR/test_cgrp2_array_pin -U $bpf_cgrp2_array -v $CGRP2_TC
  55. ;;
  56. *)
  57. [ -d "$BPF_FS_TC_SHARE" ] && rm -f $bpf_cgrp2_array
  58. ;;
  59. esac
  60. }
  61. setup_net() {
  62. case $1 in
  63. start)
  64. $IP link add $HOST_IFC type veth peer name $NS_IFC || return $?
  65. $IP link set dev $HOST_IFC up || return $?
  66. sysctl -q net.ipv6.conf.$HOST_IFC.accept_dad=0
  67. $IP netns add ns || return $?
  68. $IP link set dev $NS_IFC netns ns || return $?
  69. $IP -n $NS link set dev $NS_IFC up || return $?
  70. $IP netns exec $NS sysctl -q net.ipv6.conf.$NS_IFC.accept_dad=0
  71. $TC qdisc add dev $HOST_IFC clsact || return $?
  72. $TC filter add dev $HOST_IFC egress bpf da obj $BPF_PROG sec $BPF_SECTION || return $?
  73. ;;
  74. *)
  75. $IP netns del $NS
  76. $IP link del $HOST_IFC
  77. ;;
  78. esac
  79. }
  80. run_in_cgrp() {
  81. # Fork another bash and move it under the specified cgroup.
  82. # It makes the cgroup cleanup easier at the end of the test.
  83. cmd='echo $$ > '
  84. cmd="$cmd $1/cgroup.procs; exec $2"
  85. bash -c "$cmd"
  86. }
  87. do_test() {
  88. run_in_cgrp $CGRP2_TC_LEAF "ping -6 -c3 ff02::1%$HOST_IFC >& /dev/null"
  89. local dropped=$($TC -s qdisc show dev $HOST_IFC | tail -3 | \
  90. awk '/drop/{print substr($7, 0, index($7, ",")-1)}')
  91. if [[ $dropped -eq 0 ]]
  92. then
  93. echo "FAIL"
  94. return 1
  95. else
  96. echo "Successfully filtered $dropped packets"
  97. return 0
  98. fi
  99. }
  100. do_exit() {
  101. if [ "$DEBUG" == "yes" ] && [ "$MODE" != 'cleanuponly' ]
  102. then
  103. echo "------ DEBUG ------"
  104. echo "mount: "; mount | egrep '(cgroup2|bpf)'; echo
  105. echo "$CGRP2_TC_LEAF: "; ls -l $CGRP2_TC_LEAF; echo
  106. if [ -d "$BPF_FS_TC_SHARE" ]
  107. then
  108. echo "$BPF_FS_TC_SHARE: "; ls -l $BPF_FS_TC_SHARE; echo
  109. fi
  110. echo "Host net:"
  111. $IP netns
  112. $IP link show dev $HOST_IFC
  113. $IP -6 a show dev $HOST_IFC
  114. $TC -s qdisc show dev $HOST_IFC
  115. echo
  116. echo "$NS net:"
  117. $IP -n $NS link show dev $NS_IFC
  118. $IP -n $NS -6 link show dev $NS_IFC
  119. echo "------ DEBUG ------"
  120. echo
  121. fi
  122. if [ "$MODE" != 'nocleanup' ]
  123. then
  124. setup_net stop
  125. setup_bpf_cgrp2_array stop
  126. setup_cgrp2 stop
  127. fi
  128. }
  129. init_cgrp2_vars
  130. init_bpf_fs_vars
  131. while [[ $# -ge 1 ]]
  132. do
  133. a="$1"
  134. case $a in
  135. debug)
  136. DEBUG='yes'
  137. shift 1
  138. ;;
  139. cleanup-only)
  140. MODE='cleanuponly'
  141. shift 1
  142. ;;
  143. no-cleanup)
  144. MODE='nocleanup'
  145. shift 1
  146. ;;
  147. *)
  148. echo "test_cgrp2_tc [debug] [cleanup-only | no-cleanup]"
  149. echo " debug: Print cgrp and network setup details at the end of the test"
  150. echo " cleanup-only: Try to cleanup things from last test. No test will be run"
  151. echo " no-cleanup: Run the test but don't do cleanup at the end"
  152. echo "[Note: If no arg is given, it will run the test and do cleanup at the end]"
  153. echo
  154. exit -1
  155. ;;
  156. esac
  157. done
  158. trap do_exit 0
  159. [ "$MODE" == 'cleanuponly' ] && exit
  160. setup_cgrp2 start || exit $?
  161. setup_net start || exit $?
  162. init_bpf_fs_vars || exit $?
  163. setup_bpf_cgrp2_array start || exit $?
  164. do_test
  165. echo