1 #!/bin/bash 1 #!/bin/bash 2 # SPDX-License-Identifier: GPL-2.0 2 # SPDX-License-Identifier: GPL-2.0 3 3 4 readonly FLUSH_PATH="/sys/class/net/${dev}/gro 4 readonly FLUSH_PATH="/sys/class/net/${dev}/gro_flush_timeout" 5 readonly IRQ_PATH="/sys/class/net/${dev}/napi_ 5 readonly IRQ_PATH="/sys/class/net/${dev}/napi_defer_hard_irqs" 6 readonly FLUSH_TIMEOUT="$(< ${FLUSH_PATH})" 6 readonly FLUSH_TIMEOUT="$(< ${FLUSH_PATH})" 7 readonly HARD_IRQS="$(< ${IRQ_PATH})" 7 readonly HARD_IRQS="$(< ${IRQ_PATH})" 8 readonly server_ns=$(mktemp -u server-XXXXXXXX 8 readonly server_ns=$(mktemp -u server-XXXXXXXX) 9 readonly client_ns=$(mktemp -u client-XXXXXXXX 9 readonly client_ns=$(mktemp -u client-XXXXXXXX) 10 10 11 netdev_check_for_carrier() { 11 netdev_check_for_carrier() { 12 local -r dev="$1" 12 local -r dev="$1" 13 13 14 for i in {1..5}; do 14 for i in {1..5}; do 15 carrier="$(cat /sys/class/net/ 15 carrier="$(cat /sys/class/net/${dev}/carrier)" 16 if [[ "${carrier}" -ne 1 ]] ; 16 if [[ "${carrier}" -ne 1 ]] ; then 17 echo "carrier not read 17 echo "carrier not ready yet..." >&2 18 sleep 1 18 sleep 1 19 else 19 else 20 echo "carrier ready" > 20 echo "carrier ready" >&2 21 break 21 break 22 fi 22 fi 23 done 23 done 24 echo "${carrier}" 24 echo "${carrier}" 25 } 25 } 26 26 27 # Assumes that there is no existing ipvlan dev 27 # Assumes that there is no existing ipvlan device on the physical device 28 setup_loopback_environment() { 28 setup_loopback_environment() { 29 local dev="$1" 29 local dev="$1" 30 30 31 # Fail hard if cannot turn on loopback 31 # Fail hard if cannot turn on loopback mode for current NIC 32 ethtool -K "${dev}" loopback on || exi 32 ethtool -K "${dev}" loopback on || exit 1 33 sleep 1 33 sleep 1 34 34 35 # Check for the carrier 35 # Check for the carrier 36 carrier=$(netdev_check_for_carrier ${d 36 carrier=$(netdev_check_for_carrier ${dev}) 37 if [[ "${carrier}" -ne 1 ]] ; then 37 if [[ "${carrier}" -ne 1 ]] ; then 38 echo "setup_loopback_environme 38 echo "setup_loopback_environment failed" 39 exit 1 39 exit 1 40 fi 40 fi 41 } 41 } 42 42 43 setup_macvlan_ns(){ 43 setup_macvlan_ns(){ 44 local -r link_dev="$1" 44 local -r link_dev="$1" 45 local -r ns_name="$2" 45 local -r ns_name="$2" 46 local -r ns_dev="$3" 46 local -r ns_dev="$3" 47 local -r ns_mac="$4" 47 local -r ns_mac="$4" 48 local -r addr="$5" 48 local -r addr="$5" 49 49 50 ip link add link "${link_dev}" dev "${ 50 ip link add link "${link_dev}" dev "${ns_dev}" \ 51 address "${ns_mac}" type macvl 51 address "${ns_mac}" type macvlan 52 exit_code=$? 52 exit_code=$? 53 if [[ "${exit_code}" -ne 0 ]]; then 53 if [[ "${exit_code}" -ne 0 ]]; then 54 echo "setup_macvlan_ns failed" 54 echo "setup_macvlan_ns failed" 55 exit $exit_code 55 exit $exit_code 56 fi 56 fi 57 57 58 [[ -e /var/run/netns/"${ns_name}" ]] | 58 [[ -e /var/run/netns/"${ns_name}" ]] || ip netns add "${ns_name}" 59 ip link set dev "${ns_dev}" netns "${n 59 ip link set dev "${ns_dev}" netns "${ns_name}" 60 ip -netns "${ns_name}" link set dev "$ 60 ip -netns "${ns_name}" link set dev "${ns_dev}" up 61 if [[ -n "${addr}" ]]; then 61 if [[ -n "${addr}" ]]; then 62 ip -netns "${ns_name}" addr ad 62 ip -netns "${ns_name}" addr add dev "${ns_dev}" "${addr}" 63 fi 63 fi 64 64 65 sleep 1 65 sleep 1 66 } 66 } 67 67 68 cleanup_macvlan_ns(){ 68 cleanup_macvlan_ns(){ 69 while (( $# >= 2 )); do 69 while (( $# >= 2 )); do 70 ns_name="$1" 70 ns_name="$1" 71 ns_dev="$2" 71 ns_dev="$2" 72 ip -netns "${ns_name}" link de 72 ip -netns "${ns_name}" link del dev "${ns_dev}" 73 ip netns del "${ns_name}" 73 ip netns del "${ns_name}" 74 shift 2 74 shift 2 75 done 75 done 76 } 76 } 77 77 78 cleanup_loopback(){ 78 cleanup_loopback(){ 79 local -r dev="$1" 79 local -r dev="$1" 80 80 81 ethtool -K "${dev}" loopback off 81 ethtool -K "${dev}" loopback off 82 sleep 1 82 sleep 1 83 83 84 # Check for the carrier 84 # Check for the carrier 85 carrier=$(netdev_check_for_carrier ${d 85 carrier=$(netdev_check_for_carrier ${dev}) 86 if [[ "${carrier}" -ne 1 ]] ; then 86 if [[ "${carrier}" -ne 1 ]] ; then 87 echo "setup_loopback_environme 87 echo "setup_loopback_environment failed" 88 exit 1 88 exit 1 89 fi 89 fi 90 } 90 } 91 91 92 setup_interrupt() { 92 setup_interrupt() { 93 # Use timer on host to trigger the ne 93 # Use timer on host to trigger the network stack 94 # Also disable device interrupt to not 94 # Also disable device interrupt to not depend on NIC interrupt 95 # Reduce test flakiness caused by unex 95 # Reduce test flakiness caused by unexpected interrupts 96 echo 100000 >"${FLUSH_PATH}" 96 echo 100000 >"${FLUSH_PATH}" 97 echo 50 >"${IRQ_PATH}" 97 echo 50 >"${IRQ_PATH}" 98 } 98 } 99 99 100 setup_ns() { 100 setup_ns() { 101 # Set up server_ns namespace and clien 101 # Set up server_ns namespace and client_ns namespace 102 setup_macvlan_ns "${dev}" ${server_ns} 102 setup_macvlan_ns "${dev}" ${server_ns} server "${SERVER_MAC}" 103 setup_macvlan_ns "${dev}" ${client_ns} 103 setup_macvlan_ns "${dev}" ${client_ns} client "${CLIENT_MAC}" 104 } 104 } 105 105 106 cleanup_ns() { 106 cleanup_ns() { 107 cleanup_macvlan_ns ${server_ns} server 107 cleanup_macvlan_ns ${server_ns} server ${client_ns} client 108 } 108 } 109 109 110 setup() { 110 setup() { 111 setup_loopback_environment "${dev}" 111 setup_loopback_environment "${dev}" 112 setup_interrupt 112 setup_interrupt 113 } 113 } 114 114 115 cleanup() { 115 cleanup() { 116 cleanup_loopback "${dev}" 116 cleanup_loopback "${dev}" 117 117 118 echo "${FLUSH_TIMEOUT}" >"${FLUSH_PATH 118 echo "${FLUSH_TIMEOUT}" >"${FLUSH_PATH}" 119 echo "${HARD_IRQS}" >"${IRQ_PATH}" 119 echo "${HARD_IRQS}" >"${IRQ_PATH}" 120 } 120 }
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.