test_flow_dissector.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Load BPF flow dissector and verify it correctly dissects traffic
  5. export TESTNAME=test_flow_dissector
  6. unmount=0
  7. # Kselftest framework requirement - SKIP code is 4.
  8. ksft_skip=4
  9. msg="skip all tests:"
  10. if [ $UID != 0 ]; then
  11. echo $msg please run this as root >&2
  12. exit $ksft_skip
  13. fi
  14. # This test needs to be run in a network namespace with in_netns.sh. Check if
  15. # this is the case and run it with in_netns.sh if it is being run in the root
  16. # namespace.
  17. if [[ -z $(ip netns identify $$) ]]; then
  18. ../net/in_netns.sh "$0" "$@"
  19. exit $?
  20. fi
  21. # Determine selftest success via shell exit code
  22. exit_handler()
  23. {
  24. if (( $? == 0 )); then
  25. echo "selftests: $TESTNAME [PASS]";
  26. else
  27. echo "selftests: $TESTNAME [FAILED]";
  28. fi
  29. set +e
  30. # Cleanup
  31. tc filter del dev lo ingress pref 1337 2> /dev/null
  32. tc qdisc del dev lo ingress 2> /dev/null
  33. ./flow_dissector_load -d 2> /dev/null
  34. if [ $unmount -ne 0 ]; then
  35. umount bpffs 2> /dev/null
  36. fi
  37. }
  38. # Exit script immediately (well catched by trap handler) if any
  39. # program/thing exits with a non-zero status.
  40. set -e
  41. # (Use 'trap -l' to list meaning of numbers)
  42. trap exit_handler 0 2 3 6 9
  43. # Mount BPF file system
  44. if /bin/mount | grep /sys/fs/bpf > /dev/null; then
  45. echo "bpffs already mounted"
  46. else
  47. echo "bpffs not mounted. Mounting..."
  48. unmount=1
  49. /bin/mount bpffs /sys/fs/bpf -t bpf
  50. fi
  51. # Attach BPF program
  52. ./flow_dissector_load -p bpf_flow.o -s dissect
  53. # Setup
  54. tc qdisc add dev lo ingress
  55. echo "Testing IPv4..."
  56. # Drops all IP/UDP packets coming from port 9
  57. tc filter add dev lo parent ffff: protocol ip pref 1337 flower ip_proto \
  58. udp src_port 9 action drop
  59. # Send 10 IPv4/UDP packets from port 8. Filter should not drop any.
  60. ./test_flow_dissector -i 4 -f 8
  61. # Send 10 IPv4/UDP packets from port 9. Filter should drop all.
  62. ./test_flow_dissector -i 4 -f 9 -F
  63. # Send 10 IPv4/UDP packets from port 10. Filter should not drop any.
  64. ./test_flow_dissector -i 4 -f 10
  65. echo "Testing IPIP..."
  66. # Send 10 IPv4/IPv4/UDP packets from port 8. Filter should not drop any.
  67. ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e bare -i 4 \
  68. -D 192.168.0.1 -S 1.1.1.1 -f 8
  69. # Send 10 IPv4/IPv4/UDP packets from port 9. Filter should drop all.
  70. ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e bare -i 4 \
  71. -D 192.168.0.1 -S 1.1.1.1 -f 9 -F
  72. # Send 10 IPv4/IPv4/UDP packets from port 10. Filter should not drop any.
  73. ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e bare -i 4 \
  74. -D 192.168.0.1 -S 1.1.1.1 -f 10
  75. echo "Testing IPv4 + GRE..."
  76. # Send 10 IPv4/GRE/IPv4/UDP packets from port 8. Filter should not drop any.
  77. ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e gre -i 4 \
  78. -D 192.168.0.1 -S 1.1.1.1 -f 8
  79. # Send 10 IPv4/GRE/IPv4/UDP packets from port 9. Filter should drop all.
  80. ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e gre -i 4 \
  81. -D 192.168.0.1 -S 1.1.1.1 -f 9 -F
  82. # Send 10 IPv4/GRE/IPv4/UDP packets from port 10. Filter should not drop any.
  83. ./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e gre -i 4 \
  84. -D 192.168.0.1 -S 1.1.1.1 -f 10
  85. tc filter del dev lo ingress pref 1337
  86. echo "Testing IPv6..."
  87. # Drops all IPv6/UDP packets coming from port 9
  88. tc filter add dev lo parent ffff: protocol ipv6 pref 1337 flower ip_proto \
  89. udp src_port 9 action drop
  90. # Send 10 IPv6/UDP packets from port 8. Filter should not drop any.
  91. ./test_flow_dissector -i 6 -f 8
  92. # Send 10 IPv6/UDP packets from port 9. Filter should drop all.
  93. ./test_flow_dissector -i 6 -f 9 -F
  94. # Send 10 IPv6/UDP packets from port 10. Filter should not drop any.
  95. ./test_flow_dissector -i 6 -f 10
  96. exit 0