Path: blob/master/tools/testing/selftests/drivers/net/team/team_lib.sh
171056 views
#!/bin/bash1# SPDX-License-Identifier: GPL-2.023test_dir="$(dirname "$0")"4export REQUIRE_MZ=no5export NUM_NETIFS=06# shellcheck disable=SC10917source "${test_dir}/../../../net/forwarding/lib.sh"89TCP_PORT="43434"1011# Create a team interface inside of a given network namespace with a given12# mode, members, and IP address.13# Arguments:14# namespace - Network namespace to put the team interface into.15# team - The name of the team interface to setup.16# mode - The team mode of the interface.17# ip_address - The IP address to assign to the team interface.18# prefix_length - The prefix length for the IP address subnet.19# $@ - members - The member interfaces of the aggregation.20setup_team()21{22local namespace=$123local team=$224local mode=$325local ip_address=$426local prefix_length=$527shift 528local members=("$@")2930# Prerequisite: team must have no members31for member in "${members[@]}"; do32ip -n "${namespace}" link set "${member}" nomaster33done3435# Prerequisite: team must have no address in order to set it36# shellcheck disable=SC208637ip -n "${namespace}" addr del "${ip_address}/${prefix_length}" \38${NODAD} dev "${team}"3940echo "Setting team in ${namespace} to mode ${mode}"4142if ! ip -n "${namespace}" link set "${team}" down; then43echo "Failed to bring team device down"44return 145fi46if ! ip netns exec "${namespace}" teamnl "${team}" setoption mode \47"${mode}"; then48echo "Failed to set ${team} mode to '${mode}'"49return 150fi5152# Aggregate the members into teams.53for member in "${members[@]}"; do54ip -n "${namespace}" link set "${member}" master "${team}"55done5657# Bring team devices up and give them addresses.58if ! ip -n "${namespace}" link set "${team}" up; then59echo "Failed to set ${team} up"60return 161fi6263# shellcheck disable=SC208664if ! ip -n "${namespace}" addr add "${ip_address}/${prefix_length}" \65${NODAD} dev "${team}"; then66echo "Failed to give ${team} IP address in ${namespace}"67return 168fi69}7071# This is global used to keep track of the sender's iperf3 process, so that it72# can be terminated.73declare sender_pid7475# Start sending and receiving TCP traffic with iperf3.76# Globals:77# sender_pid - The process ID of the iperf3 sender process. Used to kill it78# later.79start_listening_and_sending()80{81ip netns exec "${NS2}" iperf3 -s -p "${TCP_PORT}" --logfile /dev/null &82# Wait for server to become reachable before starting client.83slowwait 5 ip netns exec "${NS1}" iperf3 -c "${NS2_IP}" -p \84"${TCP_PORT}" -t 1 --logfile /dev/null85ip netns exec "${NS1}" iperf3 -c "${NS2_IP}" -p "${TCP_PORT}" -b 1M -l \861K -t 0 --logfile /dev/null &87sender_pid=$!88}8990# Stop sending TCP traffic with iperf3.91# Globals:92# sender_pid - The process ID of the iperf3 sender process.93stop_sending_and_listening()94{95kill "${sender_pid}" && wait "${sender_pid}" 2>/dev/null || true96}9798# Monitor for TCP traffic with Tcpdump, save results to temp files.99# Arguments:100# namespace - The network namespace to run tcpdump inside of.101# $@ - interfaces - The interfaces to listen to.102save_tcpdump_outputs()103{104local namespace=$1105shift 1106local interfaces=("$@")107108for interface in "${interfaces[@]}"; do109tcpdump_start "${interface}" "${namespace}"110done111112sleep 1113114for interface in "${interfaces[@]}"; do115tcpdump_stop_nosleep "${interface}"116done117}118119clear_tcpdump_outputs()120{121local interfaces=("$@")122123for interface in "${interfaces[@]}"; do124tcpdump_cleanup "${interface}"125done126}127128# Read Tcpdump output, determine packet counts.129# Arguments:130# interface - The name of the interface to count packets for.131# ip_address - The destination IP address.132did_interface_receive()133{134local interface="$1"135local ip_address="$2"136local packet_count137138packet_count=$(tcpdump_show "$interface" | grep -c \139"> ${ip_address}.${TCP_PORT}")140echo "Packet count for ${interface} was ${packet_count}"141142if [[ "${packet_count}" -gt 0 ]]; then143true144else145false146fi147}148149# Return true if the given interface in the given namespace does NOT receive150# traffic over a 1 second period.151# Arguments:152# interface - The name of the interface.153# ip_address - The destination IP address.154# namespace - The name of the namespace that the interface is in.155check_no_traffic()156{157local interface="$1"158local ip_address="$2"159local namespace="$3"160local rc161162save_tcpdump_outputs "${namespace}" "${interface}"163did_interface_receive "${interface}" "${ip_address}"164rc=$?165166clear_tcpdump_outputs "${interface}"167168if [[ "${rc}" -eq 0 ]]; then169return 1170else171return 0172fi173}174175176