linux/tools/testing/selftests/net/bind_bhash.sh
Joanne Koong c35ecb95c4 selftests/net: Add test for timing a bind request to a port with a populated bhash entry
This test populates the bhash table for a given port with
MAX_THREADS * MAX_CONNECTIONS sockets, and then times how long
a bind request on the port takes.

When populating the bhash table, we create the sockets and then bind
the sockets to the same address and port (SO_REUSEADDR and SO_REUSEPORT
are set). When timing how long a bind on the port takes, we bind on a
different address without SO_REUSEPORT set. We do not set SO_REUSEPORT
because we are interested in the case where the bind request does not
go through the tb->fastreuseport path, which is fragile (eg
tb->fastreuseport path does not work if binding with a different uid).

To run the script:
    Usage: ./bind_bhash.sh [-6 | -4] [-p port] [-a address]
	    6: use ipv6
	    4: use ipv4
	    port: Port number
	    address: ip address

Without any arguments, ./bind_bhash.sh defaults to ipv6 using ip address
"2001:0db8:0:f101::1" on port 443.

On my local machine, I see:
ipv4:
before - 0.002317 seconds
with bhash2 - 0.000020 seconds

ipv6:
before - 0.002431 seconds
with bhash2 - 0.000021 seconds

Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2022-08-24 19:30:09 -07:00

67 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
NR_FILES=32768
SAVED_NR_FILES=$(ulimit -n)
# default values
port=443
addr_v6="2001:0db8:0:f101::1"
addr_v4="10.8.8.8"
use_v6=true
addr=""
usage() {
echo "Usage: $0 [-6 | -4] [-p port] [-a address]"
echo -e "\t6: use ipv6"
echo -e "\t4: use ipv4"
echo -e "\tport: Port number"
echo -e "\taddress: ip address"
}
while getopts "ha:p:64" opt; do
case ${opt} in
h)
usage $0
exit 0
;;
a) addr=$OPTARG;;
p)
port=$OPTARG;;
6)
use_v6=true;;
4)
use_v6=false;;
esac
done
setup() {
if [[ "$use_v6" == true ]]; then
ip addr add $addr_v6 nodad dev eth0
else
ip addr add $addr_v4 dev lo
fi
ulimit -n $NR_FILES
}
cleanup() {
if [[ "$use_v6" == true ]]; then
ip addr del $addr_v6 dev eth0
else
ip addr del $addr_v4/32 dev lo
fi
ulimit -n $SAVED_NR_FILES
}
if [[ "$addr" != "" ]]; then
addr_v4=$addr;
addr_v6=$addr;
fi
setup
if [[ "$use_v6" == true ]] ; then
./bind_bhash $port "ipv6" $addr_v6
else
./bind_bhash $port "ipv4" $addr_v4
fi
cleanup