ksft_setup_loopback.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Setup script for running ksft tests over a real interface in loopback mode.
  4. # This scripts replaces the historical setup_loopback.sh. It puts
  5. # a (presumably) real hardware interface into loopback mode, creates macvlan
  6. # interfaces on top and places them in a network namespace for isolation.
  7. #
  8. # NETIF env variable must be exported to indicate the real target device.
  9. # Note that the test will override NETIF with one of the macvlans, the
  10. # actual ksft test will only see the macvlans.
  11. #
  12. # Example use:
  13. # export NETIF=eth0
  14. # ./net/lib/ksft_setup_loopback.sh ./drivers/net/gro.py
  15. if [ -z "$NETIF" ]; then
  16. echo "Error: NETIF variable not set"
  17. exit 1
  18. fi
  19. if ! [ -d "/sys/class/net/$NETIF" ]; then
  20. echo "Error: Can't find $NETIF, invalid netdevice"
  21. exit 1
  22. fi
  23. # Save original settings for cleanup
  24. readonly FLUSH_PATH="/sys/class/net/${NETIF}/gro_flush_timeout"
  25. readonly IRQ_PATH="/sys/class/net/${NETIF}/napi_defer_hard_irqs"
  26. FLUSH_TIMEOUT="$(< "${FLUSH_PATH}")"
  27. readonly FLUSH_TIMEOUT
  28. HARD_IRQS="$(< "${IRQ_PATH}")"
  29. readonly HARD_IRQS
  30. SERVER_NS=$(mktemp -u server-XXXXXXXX)
  31. readonly SERVER_NS
  32. CLIENT_NS=$(mktemp -u client-XXXXXXXX)
  33. readonly CLIENT_NS
  34. readonly SERVER_MAC="aa:00:00:00:00:02"
  35. readonly CLIENT_MAC="aa:00:00:00:00:01"
  36. # ksft expects addresses to communicate with remote
  37. export LOCAL_V6=2001:db8:1::1
  38. export REMOTE_V6=2001:db8:1::2
  39. cleanup() {
  40. local exit_code=$?
  41. echo "Cleaning up..."
  42. # Remove macvlan interfaces and namespaces
  43. ip -netns "${SERVER_NS}" link del dev server 2>/dev/null || true
  44. ip netns del "${SERVER_NS}" 2>/dev/null || true
  45. ip -netns "${CLIENT_NS}" link del dev client 2>/dev/null || true
  46. ip netns del "${CLIENT_NS}" 2>/dev/null || true
  47. # Disable loopback
  48. ethtool -K "${NETIF}" loopback off 2>/dev/null || true
  49. sleep 1
  50. echo "${FLUSH_TIMEOUT}" >"${FLUSH_PATH}"
  51. echo "${HARD_IRQS}" >"${IRQ_PATH}"
  52. exit $exit_code
  53. }
  54. trap cleanup EXIT INT TERM
  55. # Enable loopback mode
  56. echo "Enabling loopback on ${NETIF}..."
  57. ethtool -K "${NETIF}" loopback on || {
  58. echo "Failed to enable loopback mode"
  59. exit 1
  60. }
  61. # The interface may need time to get carrier back, but selftests
  62. # will wait for carrier, so no need to wait / sleep here.
  63. # Use timer on host to trigger the network stack
  64. # Also disable device interrupt to not depend on NIC interrupt
  65. # Reduce test flakiness caused by unexpected interrupts
  66. echo 100000 >"${FLUSH_PATH}"
  67. echo 50 >"${IRQ_PATH}"
  68. # Create server namespace with macvlan
  69. ip netns add "${SERVER_NS}"
  70. ip link add link "${NETIF}" dev server address "${SERVER_MAC}" type macvlan
  71. ip link set dev server netns "${SERVER_NS}"
  72. ip -netns "${SERVER_NS}" link set dev server up
  73. ip -netns "${SERVER_NS}" addr add $LOCAL_V6/64 dev server
  74. ip -netns "${SERVER_NS}" link set dev lo up
  75. # Create client namespace with macvlan
  76. ip netns add "${CLIENT_NS}"
  77. ip link add link "${NETIF}" dev client address "${CLIENT_MAC}" type macvlan
  78. ip link set dev client netns "${CLIENT_NS}"
  79. ip -netns "${CLIENT_NS}" link set dev client up
  80. ip -netns "${CLIENT_NS}" addr add $REMOTE_V6/64 dev client
  81. ip -netns "${CLIENT_NS}" link set dev lo up
  82. echo "Setup complete!"
  83. echo " Device: ${NETIF}"
  84. echo " Server NS: ${SERVER_NS}"
  85. echo " Client NS: ${CLIENT_NS}"
  86. echo ""
  87. # Setup environment variables for tests
  88. export NETIF=server
  89. export REMOTE_TYPE=netns
  90. export REMOTE_ARGS="${CLIENT_NS}"
  91. # Run the command
  92. ip netns exec "${SERVER_NS}" "$@"