test_cgrp2_sock2.sh 1.8 KB

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