Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # extended toeplitz test: test rxhash plus, optionally, either (1) rss mapping
0005 # from rxhash to rx queue ('-rss') or (2) rps mapping from rxhash to cpu
0006 # ('-rps <rps_map>')
0007 #
0008 # irq-pattern-prefix can be derived from /sys/kernel/irq/*/action,
0009 # which is a driver-specific encoding.
0010 #
0011 # invoke as ./toeplitz.sh (-i <iface>) -u|-t -4|-6 \
0012 # [(-rss -irq_prefix <irq-pattern-prefix>)|(-rps <rps_map>)]
0013 
0014 source setup_loopback.sh
0015 readonly SERVER_IP4="192.168.1.200/24"
0016 readonly SERVER_IP6="fda8::1/64"
0017 readonly SERVER_MAC="aa:00:00:00:00:02"
0018 
0019 readonly CLIENT_IP4="192.168.1.100/24"
0020 readonly CLIENT_IP6="fda8::2/64"
0021 readonly CLIENT_MAC="aa:00:00:00:00:01"
0022 
0023 PORT=8000
0024 KEY="$(</proc/sys/net/core/netdev_rss_key)"
0025 TEST_RSS=false
0026 RPS_MAP=""
0027 PROTO_FLAG=""
0028 IP_FLAG=""
0029 DEV="eth0"
0030 
0031 # Return the number of rxqs among which RSS is configured to spread packets.
0032 # This is determined by reading the RSS indirection table using ethtool.
0033 get_rss_cfg_num_rxqs() {
0034         echo $(ethtool -x "${DEV}" |
0035                 egrep [[:space:]]+[0-9]+:[[:space:]]+ |
0036                 cut -d: -f2- |
0037                 awk '{$1=$1};1' |
0038                 tr ' ' '\n' |
0039                 sort -u |
0040                 wc -l)
0041 }
0042 
0043 # Return a list of the receive irq handler cpus.
0044 # The list is ordered by the irqs, so first rxq-0 cpu, then rxq-1 cpu, etc.
0045 # Reads /sys/kernel/irq/ in order, so algorithm depends on
0046 # irq_{rxq-0} < irq_{rxq-1}, etc.
0047 get_rx_irq_cpus() {
0048         CPUS=""
0049         # sort so that irq 2 is read before irq 10
0050         SORTED_IRQS=$(for i in /sys/kernel/irq/*; do echo $i; done | sort -V)
0051         # Consider only as many queues as RSS actually uses. We assume that
0052         # if RSS_CFG_NUM_RXQS=N, then RSS uses rxqs 0-(N-1).
0053         RSS_CFG_NUM_RXQS=$(get_rss_cfg_num_rxqs)
0054         RXQ_COUNT=0
0055 
0056         for i in ${SORTED_IRQS}
0057         do
0058                 [[ "${RXQ_COUNT}" -lt "${RSS_CFG_NUM_RXQS}" ]] || break
0059                 # lookup relevant IRQs by action name
0060                 [[ -e "$i/actions" ]] || continue
0061                 cat "$i/actions" | grep -q "${IRQ_PATTERN}" || continue
0062                 irqname=$(<"$i/actions")
0063 
0064                 # does the IRQ get called
0065                 irqcount=$(cat "$i/per_cpu_count" | tr -d '0,')
0066                 [[ -n "${irqcount}" ]] || continue
0067 
0068                 # lookup CPU
0069                 irq=$(basename "$i")
0070                 cpu=$(cat "/proc/irq/$irq/smp_affinity_list")
0071 
0072                 if [[ -z "${CPUS}" ]]; then
0073                         CPUS="${cpu}"
0074                 else
0075                         CPUS="${CPUS},${cpu}"
0076                 fi
0077                 RXQ_COUNT=$((RXQ_COUNT+1))
0078         done
0079 
0080         echo "${CPUS}"
0081 }
0082 
0083 get_disable_rfs_cmd() {
0084         echo "echo 0 > /proc/sys/net/core/rps_sock_flow_entries;"
0085 }
0086 
0087 get_set_rps_bitmaps_cmd() {
0088         CMD=""
0089         for i in /sys/class/net/${DEV}/queues/rx-*/rps_cpus
0090         do
0091                 CMD="${CMD} echo $1 > ${i};"
0092         done
0093 
0094         echo "${CMD}"
0095 }
0096 
0097 get_disable_rps_cmd() {
0098         echo "$(get_set_rps_bitmaps_cmd 0)"
0099 }
0100 
0101 die() {
0102         echo "$1"
0103         exit 1
0104 }
0105 
0106 check_nic_rxhash_enabled() {
0107         local -r pattern="receive-hashing:\ on"
0108 
0109         ethtool -k "${DEV}" | grep -q "${pattern}" || die "rxhash must be enabled"
0110 }
0111 
0112 parse_opts() {
0113         local prog=$0
0114         shift 1
0115 
0116         while [[ "$1" =~ "-" ]]; do
0117                 if [[ "$1" = "-irq_prefix" ]]; then
0118                         shift
0119                         IRQ_PATTERN="^$1-[0-9]*$"
0120                 elif [[ "$1" = "-u" || "$1" = "-t" ]]; then
0121                         PROTO_FLAG="$1"
0122                 elif [[ "$1" = "-4" ]]; then
0123                         IP_FLAG="$1"
0124                         SERVER_IP="${SERVER_IP4}"
0125                         CLIENT_IP="${CLIENT_IP4}"
0126                 elif [[ "$1" = "-6" ]]; then
0127                         IP_FLAG="$1"
0128                         SERVER_IP="${SERVER_IP6}"
0129                         CLIENT_IP="${CLIENT_IP6}"
0130                 elif [[ "$1" = "-rss" ]]; then
0131                         TEST_RSS=true
0132                 elif [[ "$1" = "-rps" ]]; then
0133                         shift
0134                         RPS_MAP="$1"
0135                 elif [[ "$1" = "-i" ]]; then
0136                         shift
0137                         DEV="$1"
0138                 else
0139                         die "Usage: ${prog} (-i <iface>) -u|-t -4|-6 \
0140                              [(-rss -irq_prefix <irq-pattern-prefix>)|(-rps <rps_map>)]"
0141                 fi
0142                 shift
0143         done
0144 }
0145 
0146 setup() {
0147         setup_loopback_environment "${DEV}"
0148 
0149         # Set up server_ns namespace and client_ns namespace
0150         setup_macvlan_ns "${DEV}" server_ns server \
0151         "${SERVER_MAC}" "${SERVER_IP}"
0152         setup_macvlan_ns "${DEV}" client_ns client \
0153         "${CLIENT_MAC}" "${CLIENT_IP}"
0154 }
0155 
0156 cleanup() {
0157         cleanup_macvlan_ns server_ns server client_ns client
0158         cleanup_loopback "${DEV}"
0159 }
0160 
0161 parse_opts $0 $@
0162 
0163 setup
0164 trap cleanup EXIT
0165 
0166 check_nic_rxhash_enabled
0167 
0168 # Actual test starts here
0169 if [[ "${TEST_RSS}" = true ]]; then
0170         # RPS/RFS must be disabled because they move packets between cpus,
0171         # which breaks the PACKET_FANOUT_CPU identification of RSS decisions.
0172         eval "$(get_disable_rfs_cmd) $(get_disable_rps_cmd)" \
0173           ip netns exec server_ns ./toeplitz "${IP_FLAG}" "${PROTO_FLAG}" \
0174           -d "${PORT}" -i "${DEV}" -k "${KEY}" -T 1000 \
0175           -C "$(get_rx_irq_cpus)" -s -v &
0176 elif [[ ! -z "${RPS_MAP}" ]]; then
0177         eval "$(get_disable_rfs_cmd) $(get_set_rps_bitmaps_cmd ${RPS_MAP})" \
0178           ip netns exec server_ns ./toeplitz "${IP_FLAG}" "${PROTO_FLAG}" \
0179           -d "${PORT}" -i "${DEV}" -k "${KEY}" -T 1000 \
0180           -r "0x${RPS_MAP}" -s -v &
0181 else
0182         ip netns exec server_ns ./toeplitz "${IP_FLAG}" "${PROTO_FLAG}" \
0183           -d "${PORT}" -i "${DEV}" -k "${KEY}" -T 1000 -s -v &
0184 fi
0185 
0186 server_pid=$!
0187 
0188 ip netns exec client_ns ./toeplitz_client.sh "${PROTO_FLAG}" \
0189   "${IP_FLAG}" "${SERVER_IP%%/*}" "${PORT}" &
0190 
0191 client_pid=$!
0192 
0193 wait "${server_pid}"
0194 exit_code=$?
0195 kill -9 "${client_pid}"
0196 if [[ "${exit_code}" -eq 0 ]]; then
0197         echo "Test Succeeded!"
0198 fi
0199 exit "${exit_code}"