17d157501SCoco Li#!/bin/bash 27d157501SCoco Li# SPDX-License-Identifier: GPL-2.0 3*9af771d2SPaolo Abeni 4*9af771d2SPaolo Abenireadonly FLUSH_PATH="/sys/class/net/${dev}/gro_flush_timeout" 5*9af771d2SPaolo Abenireadonly IRQ_PATH="/sys/class/net/${dev}/napi_defer_hard_irqs" 6*9af771d2SPaolo Abenireadonly FLUSH_TIMEOUT="$(< ${FLUSH_PATH})" 7*9af771d2SPaolo Abenireadonly HARD_IRQS="$(< ${IRQ_PATH})" 8*9af771d2SPaolo Abeni 97d157501SCoco Linetdev_check_for_carrier() { 107d157501SCoco Li local -r dev="$1" 117d157501SCoco Li 127d157501SCoco Li for i in {1..5}; do 137d157501SCoco Li carrier="$(cat /sys/class/net/${dev}/carrier)" 147d157501SCoco Li if [[ "${carrier}" -ne 1 ]] ; then 157d157501SCoco Li echo "carrier not ready yet..." >&2 167d157501SCoco Li sleep 1 177d157501SCoco Li else 187d157501SCoco Li echo "carrier ready" >&2 197d157501SCoco Li break 207d157501SCoco Li fi 217d157501SCoco Li done 227d157501SCoco Li echo "${carrier}" 237d157501SCoco Li} 247d157501SCoco Li 257d157501SCoco Li# Assumes that there is no existing ipvlan device on the physical device 267d157501SCoco Lisetup_loopback_environment() { 277d157501SCoco Li local dev="$1" 287d157501SCoco Li 297d157501SCoco Li # Fail hard if cannot turn on loopback mode for current NIC 307d157501SCoco Li ethtool -K "${dev}" loopback on || exit 1 317d157501SCoco Li sleep 1 327d157501SCoco Li 337d157501SCoco Li # Check for the carrier 347d157501SCoco Li carrier=$(netdev_check_for_carrier ${dev}) 357d157501SCoco Li if [[ "${carrier}" -ne 1 ]] ; then 367d157501SCoco Li echo "setup_loopback_environment failed" 377d157501SCoco Li exit 1 387d157501SCoco Li fi 397d157501SCoco Li} 407d157501SCoco Li 417d157501SCoco Lisetup_macvlan_ns(){ 427d157501SCoco Li local -r link_dev="$1" 437d157501SCoco Li local -r ns_name="$2" 447d157501SCoco Li local -r ns_dev="$3" 457d157501SCoco Li local -r ns_mac="$4" 467d157501SCoco Li local -r addr="$5" 477d157501SCoco Li 487d157501SCoco Li ip link add link "${link_dev}" dev "${ns_dev}" \ 497d157501SCoco Li address "${ns_mac}" type macvlan 507d157501SCoco Li exit_code=$? 517d157501SCoco Li if [[ "${exit_code}" -ne 0 ]]; then 527d157501SCoco Li echo "setup_macvlan_ns failed" 537d157501SCoco Li exit $exit_code 547d157501SCoco Li fi 557d157501SCoco Li 567d157501SCoco Li [[ -e /var/run/netns/"${ns_name}" ]] || ip netns add "${ns_name}" 577d157501SCoco Li ip link set dev "${ns_dev}" netns "${ns_name}" 587d157501SCoco Li ip -netns "${ns_name}" link set dev "${ns_dev}" up 597d157501SCoco Li if [[ -n "${addr}" ]]; then 607d157501SCoco Li ip -netns "${ns_name}" addr add dev "${ns_dev}" "${addr}" 617d157501SCoco Li fi 627d157501SCoco Li 637d157501SCoco Li sleep 1 647d157501SCoco Li} 657d157501SCoco Li 667d157501SCoco Licleanup_macvlan_ns(){ 677d157501SCoco Li while (( $# >= 2 )); do 687d157501SCoco Li ns_name="$1" 697d157501SCoco Li ns_dev="$2" 707d157501SCoco Li ip -netns "${ns_name}" link del dev "${ns_dev}" 717d157501SCoco Li ip netns del "${ns_name}" 727d157501SCoco Li shift 2 737d157501SCoco Li done 747d157501SCoco Li} 757d157501SCoco Li 767d157501SCoco Licleanup_loopback(){ 777d157501SCoco Li local -r dev="$1" 787d157501SCoco Li 797d157501SCoco Li ethtool -K "${dev}" loopback off 807d157501SCoco Li sleep 1 817d157501SCoco Li 827d157501SCoco Li # Check for the carrier 837d157501SCoco Li carrier=$(netdev_check_for_carrier ${dev}) 847d157501SCoco Li if [[ "${carrier}" -ne 1 ]] ; then 857d157501SCoco Li echo "setup_loopback_environment failed" 867d157501SCoco Li exit 1 877d157501SCoco Li fi 887d157501SCoco Li} 89*9af771d2SPaolo Abeni 90*9af771d2SPaolo Abenisetup_interrupt() { 91*9af771d2SPaolo Abeni # Use timer on host to trigger the network stack 92*9af771d2SPaolo Abeni # Also disable device interrupt to not depend on NIC interrupt 93*9af771d2SPaolo Abeni # Reduce test flakiness caused by unexpected interrupts 94*9af771d2SPaolo Abeni echo 100000 >"${FLUSH_PATH}" 95*9af771d2SPaolo Abeni echo 50 >"${IRQ_PATH}" 96*9af771d2SPaolo Abeni} 97*9af771d2SPaolo Abeni 98*9af771d2SPaolo Abenisetup_ns() { 99*9af771d2SPaolo Abeni # Set up server_ns namespace and client_ns namespace 100*9af771d2SPaolo Abeni setup_macvlan_ns "${dev}" server_ns server "${SERVER_MAC}" 101*9af771d2SPaolo Abeni setup_macvlan_ns "${dev}" client_ns client "${CLIENT_MAC}" 102*9af771d2SPaolo Abeni} 103*9af771d2SPaolo Abeni 104*9af771d2SPaolo Abenicleanup_ns() { 105*9af771d2SPaolo Abeni cleanup_macvlan_ns server_ns server client_ns client 106*9af771d2SPaolo Abeni} 107*9af771d2SPaolo Abeni 108*9af771d2SPaolo Abenisetup() { 109*9af771d2SPaolo Abeni setup_loopback_environment "${dev}" 110*9af771d2SPaolo Abeni setup_interrupt 111*9af771d2SPaolo Abeni} 112*9af771d2SPaolo Abeni 113*9af771d2SPaolo Abenicleanup() { 114*9af771d2SPaolo Abeni cleanup_loopback "${dev}" 115*9af771d2SPaolo Abeni 116*9af771d2SPaolo Abeni echo "${FLUSH_TIMEOUT}" >"${FLUSH_PATH}" 117*9af771d2SPaolo Abeni echo "${HARD_IRQS}" >"${IRQ_PATH}" 118*9af771d2SPaolo Abeni} 119