test_kmod.sh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Usage:
  4. # ./test_kmod.sh [module_param]...
  5. # Ex.: ./test_kmod.sh test_range=1,3
  6. # All the parameters are passed to the kernel module.
  7. # Kselftest framework requirement - SKIP code is 4.
  8. ksft_skip=4
  9. msg="skip all tests:"
  10. if [ "$(id -u)" != "0" ]; then
  11. echo $msg please run this as root >&2
  12. exit $ksft_skip
  13. fi
  14. if [ "$building_out_of_srctree" ]; then
  15. # We are in linux-build/kselftest/bpf
  16. OUTPUT=../../
  17. else
  18. # We are in linux/tools/testing/selftests/bpf
  19. OUTPUT=../../../../
  20. fi
  21. test_run()
  22. {
  23. sysctl -w net.core.bpf_jit_enable=$1 2>&1 > /dev/null
  24. sysctl -w net.core.bpf_jit_harden=$2 2>&1 > /dev/null
  25. echo "[ JIT enabled:$1 hardened:$2 ]"
  26. shift 2
  27. dmesg -C
  28. if [ -f ${OUTPUT}/lib/test_bpf.ko ]; then
  29. insmod ${OUTPUT}/lib/test_bpf.ko "$@" 2> /dev/null
  30. if [ $? -ne 0 ]; then
  31. rc=1
  32. fi
  33. else
  34. # Use modprobe dry run to check for missing test_bpf module
  35. if ! /sbin/modprobe -q -n test_bpf "$@"; then
  36. echo "test_bpf: [SKIP]"
  37. elif /sbin/modprobe -q test_bpf "$@"; then
  38. echo "test_bpf: ok"
  39. else
  40. echo "test_bpf: [FAIL]"
  41. rc=1
  42. fi
  43. fi
  44. rmmod test_bpf 2> /dev/null
  45. dmesg | grep FAIL
  46. }
  47. test_save()
  48. {
  49. JE=`sysctl -n net.core.bpf_jit_enable`
  50. JH=`sysctl -n net.core.bpf_jit_harden`
  51. }
  52. test_restore()
  53. {
  54. sysctl -w net.core.bpf_jit_enable=$JE 2>&1 > /dev/null
  55. sysctl -w net.core.bpf_jit_harden=$JH 2>&1 > /dev/null
  56. }
  57. rc=0
  58. test_save
  59. test_run 0 0 "$@"
  60. test_run 1 0 "$@"
  61. test_run 1 1 "$@"
  62. test_run 1 2 "$@"
  63. test_restore
  64. exit $rc