test_cgrp2_sock2.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. BPFFS=/sys/fs/bpf
  4. MY_DIR=$(dirname $0)
  5. TEST=$MY_DIR/test_cgrp2_sock2
  6. LINK_PIN=$BPFFS/test_cgrp2_sock2
  7. BPF_PROG=$MY_DIR/sock_flags.bpf.o
  8. function config_device {
  9. ip netns add at_ns0
  10. ip link add veth0 type veth peer name veth0b
  11. ip link set veth0 netns at_ns0
  12. ip netns exec at_ns0 sysctl -q net.ipv6.conf.veth0.disable_ipv6=0
  13. ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
  14. ip netns exec at_ns0 ip addr add 2401:db00::1/64 dev veth0 nodad
  15. ip netns exec at_ns0 ip link set dev veth0 up
  16. sysctl -q net.ipv6.conf.veth0b.disable_ipv6=0
  17. ip addr add 172.16.1.101/24 dev veth0b
  18. ip addr add 2401:db00::2/64 dev veth0b nodad
  19. ip link set veth0b up
  20. }
  21. function config_cgroup {
  22. rm -rf /tmp/cgroupv2
  23. mkdir -p /tmp/cgroupv2
  24. mount -t cgroup2 none /tmp/cgroupv2
  25. mkdir -p /tmp/cgroupv2/foo
  26. echo $$ >> /tmp/cgroupv2/foo/cgroup.procs
  27. }
  28. function config_bpffs {
  29. if mount | grep $BPFFS > /dev/null; then
  30. echo "bpffs already mounted"
  31. else
  32. echo "bpffs not mounted. Mounting..."
  33. mount -t bpf none $BPFFS
  34. fi
  35. }
  36. function attach_bpf {
  37. $TEST /tmp/cgroupv2/foo $BPF_PROG $1
  38. [ $? -ne 0 ] && exit 1
  39. }
  40. function cleanup {
  41. rm -rf $LINK_PIN
  42. ip link del veth0b
  43. ip netns delete at_ns0
  44. umount /tmp/cgroupv2
  45. rm -rf /tmp/cgroupv2
  46. }
  47. cleanup 2>/dev/null
  48. set -e
  49. config_device
  50. config_cgroup
  51. config_bpffs
  52. set +e
  53. #
  54. # Test 1 - fail ping6
  55. #
  56. attach_bpf 0
  57. ping -c1 -w1 172.16.1.100
  58. if [ $? -ne 0 ]; then
  59. echo "ping failed when it should succeed"
  60. cleanup
  61. exit 1
  62. fi
  63. ping6 -c1 -w1 2401:db00::1
  64. if [ $? -eq 0 ]; then
  65. echo "ping6 succeeded when it should not"
  66. cleanup
  67. exit 1
  68. fi
  69. rm -rf $LINK_PIN
  70. sleep 1 # Wait for link detach
  71. #
  72. # Test 2 - fail ping
  73. #
  74. attach_bpf 1
  75. ping6 -c1 -w1 2401:db00::1
  76. if [ $? -ne 0 ]; then
  77. echo "ping6 failed when it should succeed"
  78. cleanup
  79. exit 1
  80. fi
  81. ping -c1 -w1 172.16.1.100
  82. if [ $? -eq 0 ]; then
  83. echo "ping succeeded when it should not"
  84. cleanup
  85. exit 1
  86. fi
  87. cleanup
  88. echo
  89. echo "*** PASS ***"