xref: /aosp_15_r20/kernel/tests/net/test/net_test.sh (revision 2f2c4c7ab4226c71756b9c31670392fdd6887c4f)
1*2f2c4c7aSAndroid Build Coastguard Worker#!/bin/bash
2*2f2c4c7aSAndroid Build Coastguard Workerif [[ -n "${verbose}" ]]; then
3*2f2c4c7aSAndroid Build Coastguard Worker  echo 'Current working directory:'
4*2f2c4c7aSAndroid Build Coastguard Worker  echo " - according to builtin:  [$(pwd)]"
5*2f2c4c7aSAndroid Build Coastguard Worker  echo " - according to /bin/pwd: [$(/bin/pwd)]"
6*2f2c4c7aSAndroid Build Coastguard Worker  echo
7*2f2c4c7aSAndroid Build Coastguard Worker
8*2f2c4c7aSAndroid Build Coastguard Worker  echo 'Shell environment:'
9*2f2c4c7aSAndroid Build Coastguard Worker  env
10*2f2c4c7aSAndroid Build Coastguard Worker  echo
11*2f2c4c7aSAndroid Build Coastguard Worker
12*2f2c4c7aSAndroid Build Coastguard Worker  echo -n "net_test.sh (pid $$, parent ${PPID}, tty $(tty)) running [$0] with args:"
13*2f2c4c7aSAndroid Build Coastguard Worker  for arg in "$@"; do
14*2f2c4c7aSAndroid Build Coastguard Worker    echo -n " [${arg}]"
15*2f2c4c7aSAndroid Build Coastguard Worker  done
16*2f2c4c7aSAndroid Build Coastguard Worker  echo
17*2f2c4c7aSAndroid Build Coastguard Worker  echo
18*2f2c4c7aSAndroid Build Coastguard Workerfi
19*2f2c4c7aSAndroid Build Coastguard Worker
20*2f2c4c7aSAndroid Build Coastguard Workerif [[ "$(tty)" == 'not a tty' ]]; then
21*2f2c4c7aSAndroid Build Coastguard Worker  echo 'not a tty? perhaps not quite real kernel default /dev/console - trying to fix.'
22*2f2c4c7aSAndroid Build Coastguard Worker  if [[ -c /dev/console ]]; then
23*2f2c4c7aSAndroid Build Coastguard Worker    [[ "$(readlink /proc/$$/fd/0)" != '/dev/console' ]] || exec < /dev/console
24*2f2c4c7aSAndroid Build Coastguard Worker    [[ "$(readlink /proc/$$/fd/1)" != '/dev/console' ]] || exec > /dev/console
25*2f2c4c7aSAndroid Build Coastguard Worker    [[ "$(readlink /proc/$$/fd/2)" != '/dev/console' ]] || exec 2> /dev/console
26*2f2c4c7aSAndroid Build Coastguard Worker  fi
27*2f2c4c7aSAndroid Build Coastguard Workerfi
28*2f2c4c7aSAndroid Build Coastguard Worker
29*2f2c4c7aSAndroid Build Coastguard Workerif [[ "$(tty)" == '/dev/console' ]]; then
30*2f2c4c7aSAndroid Build Coastguard Worker  ARCH="$(uname -m)"
31*2f2c4c7aSAndroid Build Coastguard Worker  # Underscore is illegal in hostname, replace with hyphen
32*2f2c4c7aSAndroid Build Coastguard Worker  ARCH="${ARCH//_/-}"
33*2f2c4c7aSAndroid Build Coastguard Worker
34*2f2c4c7aSAndroid Build Coastguard Worker  # setsid + /dev/tty{,AMA,S}0 allows bash's job control to work, ie. Ctrl+C/Z
35*2f2c4c7aSAndroid Build Coastguard Worker  if [[ -e '/proc/exitcode' ]]; then
36*2f2c4c7aSAndroid Build Coastguard Worker    # exists only in UML
37*2f2c4c7aSAndroid Build Coastguard Worker    CON='/dev/tty0'
38*2f2c4c7aSAndroid Build Coastguard Worker    hostname "uml-${ARCH}"
39*2f2c4c7aSAndroid Build Coastguard Worker  elif [[ -c '/dev/ttyAMA0' ]]; then
40*2f2c4c7aSAndroid Build Coastguard Worker    # Qemu for arm (note: /dev/ttyS0 also exists for exitcode)
41*2f2c4c7aSAndroid Build Coastguard Worker    CON='/dev/ttyAMA0'
42*2f2c4c7aSAndroid Build Coastguard Worker    hostname "qemu-${ARCH}"
43*2f2c4c7aSAndroid Build Coastguard Worker  elif [[ -c '/dev/ttyS0' ]]; then
44*2f2c4c7aSAndroid Build Coastguard Worker    # Qemu for x86 (note: /dev/ttyS1 also exists for exitcode)
45*2f2c4c7aSAndroid Build Coastguard Worker    CON='/dev/ttyS0'
46*2f2c4c7aSAndroid Build Coastguard Worker    hostname "qemu-${ARCH}"
47*2f2c4c7aSAndroid Build Coastguard Worker  else
48*2f2c4c7aSAndroid Build Coastguard Worker    # Can't figure it out, job control won't work, tough luck
49*2f2c4c7aSAndroid Build Coastguard Worker    echo 'Unable to figure out proper console - job control will not work.' >&2
50*2f2c4c7aSAndroid Build Coastguard Worker    CON=''
51*2f2c4c7aSAndroid Build Coastguard Worker    hostname "local-${ARCH}"
52*2f2c4c7aSAndroid Build Coastguard Worker  fi
53*2f2c4c7aSAndroid Build Coastguard Worker
54*2f2c4c7aSAndroid Build Coastguard Worker  unset ARCH
55*2f2c4c7aSAndroid Build Coastguard Worker
56*2f2c4c7aSAndroid Build Coastguard Worker  echo -n "$(hostname): Currently tty[/dev/console], but it should be [${CON}]..."
57*2f2c4c7aSAndroid Build Coastguard Worker
58*2f2c4c7aSAndroid Build Coastguard Worker  if [[ -n "${CON}" ]]; then
59*2f2c4c7aSAndroid Build Coastguard Worker    # Redirect std{in,out,err} to the console equivalent tty
60*2f2c4c7aSAndroid Build Coastguard Worker    # which actually supports all standard tty ioctls
61*2f2c4c7aSAndroid Build Coastguard Worker    exec <"${CON}" >&"${CON}"
62*2f2c4c7aSAndroid Build Coastguard Worker
63*2f2c4c7aSAndroid Build Coastguard Worker    # Bash wants to be session leader, hence need for setsid
64*2f2c4c7aSAndroid Build Coastguard Worker    echo " re-executing..."
65*2f2c4c7aSAndroid Build Coastguard Worker    exec /usr/bin/setsid "$0" "$@"
66*2f2c4c7aSAndroid Build Coastguard Worker    # If the above exec fails, we just fall through...
67*2f2c4c7aSAndroid Build Coastguard Worker    # (this implies failure to *find* setsid, not error return from bash,
68*2f2c4c7aSAndroid Build Coastguard Worker    #  in practice due to image construction this cannot happen)
69*2f2c4c7aSAndroid Build Coastguard Worker  else
70*2f2c4c7aSAndroid Build Coastguard Worker    echo
71*2f2c4c7aSAndroid Build Coastguard Worker  fi
72*2f2c4c7aSAndroid Build Coastguard Worker
73*2f2c4c7aSAndroid Build Coastguard Worker  # In case we fall through, clean up
74*2f2c4c7aSAndroid Build Coastguard Worker  unset CON
75*2f2c4c7aSAndroid Build Coastguard Workerfi
76*2f2c4c7aSAndroid Build Coastguard Worker
77*2f2c4c7aSAndroid Build Coastguard Workerif [[ -n "${verbose}" ]]; then
78*2f2c4c7aSAndroid Build Coastguard Worker  echo 'TTY settings:'
79*2f2c4c7aSAndroid Build Coastguard Worker  stty
80*2f2c4c7aSAndroid Build Coastguard Worker  echo
81*2f2c4c7aSAndroid Build Coastguard Worker
82*2f2c4c7aSAndroid Build Coastguard Worker  echo 'TTY settings (verbose):'
83*2f2c4c7aSAndroid Build Coastguard Worker  stty -a
84*2f2c4c7aSAndroid Build Coastguard Worker  echo
85*2f2c4c7aSAndroid Build Coastguard Worker
86*2f2c4c7aSAndroid Build Coastguard Worker  echo 'Restoring TTY sanity...'
87*2f2c4c7aSAndroid Build Coastguard Workerfi
88*2f2c4c7aSAndroid Build Coastguard Worker
89*2f2c4c7aSAndroid Build Coastguard Workerstty sane
90*2f2c4c7aSAndroid Build Coastguard Workerstty 115200
91*2f2c4c7aSAndroid Build Coastguard Worker[[ -z "${console_cols}" ]] || stty columns "${console_cols}"
92*2f2c4c7aSAndroid Build Coastguard Worker[[ -z "${console_rows}" ]] || stty rows    "${console_rows}"
93*2f2c4c7aSAndroid Build Coastguard Worker
94*2f2c4c7aSAndroid Build Coastguard Workerif [[ -n "${verbose}" ]]; then
95*2f2c4c7aSAndroid Build Coastguard Worker  echo
96*2f2c4c7aSAndroid Build Coastguard Worker
97*2f2c4c7aSAndroid Build Coastguard Worker  echo 'TTY settings:'
98*2f2c4c7aSAndroid Build Coastguard Worker  stty
99*2f2c4c7aSAndroid Build Coastguard Worker  echo
100*2f2c4c7aSAndroid Build Coastguard Worker
101*2f2c4c7aSAndroid Build Coastguard Worker  echo 'TTY settings (verbose):'
102*2f2c4c7aSAndroid Build Coastguard Worker  stty -a
103*2f2c4c7aSAndroid Build Coastguard Worker  echo
104*2f2c4c7aSAndroid Build Coastguard Workerfi
105*2f2c4c7aSAndroid Build Coastguard Worker
106*2f2c4c7aSAndroid Build Coastguard Worker# By the time we get here we should have a sane console:
107*2f2c4c7aSAndroid Build Coastguard Worker#  - 115200 baud rate
108*2f2c4c7aSAndroid Build Coastguard Worker#  - appropriate (and known) width and height (note: this assumes
109*2f2c4c7aSAndroid Build Coastguard Worker#    that the terminal doesn't get further resized)
110*2f2c4c7aSAndroid Build Coastguard Worker#  - it is no longer /dev/console, so job control should function
111*2f2c4c7aSAndroid Build Coastguard Worker#    (this means working ctrl+c [abort] and ctrl+z [suspend])
112*2f2c4c7aSAndroid Build Coastguard Worker
113*2f2c4c7aSAndroid Build Coastguard Worker
114*2f2c4c7aSAndroid Build Coastguard Worker# This defaults to 60 which is needlessly long during boot
115*2f2c4c7aSAndroid Build Coastguard Worker# (we will reset it back to the default later)
116*2f2c4c7aSAndroid Build Coastguard Workerecho 0 > /proc/sys/kernel/random/urandom_min_reseed_secs
117*2f2c4c7aSAndroid Build Coastguard Worker
118*2f2c4c7aSAndroid Build Coastguard Workerif [[ -n "${entropy}" ]]; then
119*2f2c4c7aSAndroid Build Coastguard Worker  echo "adding entropy from hex string [${entropy}]" >&2
120*2f2c4c7aSAndroid Build Coastguard Worker
121*2f2c4c7aSAndroid Build Coastguard Worker  # In kernel/include/uapi/linux/random.h RNDADDENTROPY is defined as
122*2f2c4c7aSAndroid Build Coastguard Worker  # _IOW('R', 0x03, int[2]) =(R is 0x52)= 0x40085203 = 1074287107
123*2f2c4c7aSAndroid Build Coastguard Worker  /usr/bin/python3 3>/dev/random <<EOF
124*2f2c4c7aSAndroid Build Coastguard Workerimport base64, fcntl, struct
125*2f2c4c7aSAndroid Build Coastguard Workerrnd = base64.b64decode('${entropy}')
126*2f2c4c7aSAndroid Build Coastguard Workerfcntl.ioctl(3, 0x40085203, struct.pack('ii', len(rnd) * 8, len(rnd)) + rnd)
127*2f2c4c7aSAndroid Build Coastguard WorkerEOF
128*2f2c4c7aSAndroid Build Coastguard Worker
129*2f2c4c7aSAndroid Build Coastguard Workerfi
130*2f2c4c7aSAndroid Build Coastguard Worker
131*2f2c4c7aSAndroid Build Coastguard Worker# Make sure the urandom pool has a chance to initialize before we reset
132*2f2c4c7aSAndroid Build Coastguard Worker# the reseed timer back to 60 seconds.  One timer tick should be enough.
133*2f2c4c7aSAndroid Build Coastguard Workersleep 1.1
134*2f2c4c7aSAndroid Build Coastguard Worker
135*2f2c4c7aSAndroid Build Coastguard Worker# By this point either 'random: crng init done' (newer kernels)
136*2f2c4c7aSAndroid Build Coastguard Worker# or 'random: nonblocking pool is initialized' (older kernels)
137*2f2c4c7aSAndroid Build Coastguard Worker# should have been printed out to dmesg/console.
138*2f2c4c7aSAndroid Build Coastguard Worker
139*2f2c4c7aSAndroid Build Coastguard Worker# Reset it back to boot time default
140*2f2c4c7aSAndroid Build Coastguard Workerecho 60 > /proc/sys/kernel/random/urandom_min_reseed_secs
141*2f2c4c7aSAndroid Build Coastguard Worker
142*2f2c4c7aSAndroid Build Coastguard Worker# Make sure /sys is mounted
143*2f2c4c7aSAndroid Build Coastguard Worker[[ -d /sys/fs ]] || mount -t sysfs sysfs -o nosuid,nodev,noexec /sys
144*2f2c4c7aSAndroid Build Coastguard Worker
145*2f2c4c7aSAndroid Build Coastguard Workerif ! [[ "$(uname -r)" =~ ^([0-3]|4[.][0-8])[.] ]]; then
146*2f2c4c7aSAndroid Build Coastguard Worker  # Mount the bpf filesystem on Linux version 4.9+
147*2f2c4c7aSAndroid Build Coastguard Worker  mount -t bpf bpf -o nosuid,nodev,noexec /sys/fs/bpf
148*2f2c4c7aSAndroid Build Coastguard Workerfi
149*2f2c4c7aSAndroid Build Coastguard Worker
150*2f2c4c7aSAndroid Build Coastguard Workerif ! [[ "$(uname -r)" =~ ^([0-3]|4[.][0-9]|4[.]1[0-3])[.] ]]; then
151*2f2c4c7aSAndroid Build Coastguard Worker  # Mount the Cgroup v2 filesystem on Linux version 4.14+
152*2f2c4c7aSAndroid Build Coastguard Worker  mount -t cgroup2 cgroup2 -o nosuid,nodev,noexec /sys/fs/cgroup
153*2f2c4c7aSAndroid Build Coastguard Workerfi
154*2f2c4c7aSAndroid Build Coastguard Worker
155*2f2c4c7aSAndroid Build Coastguard Worker# In case IPv6 is compiled as a module.
156*2f2c4c7aSAndroid Build Coastguard Worker[ -f /proc/net/if_inet6 ] || insmod $DIR/kernel/net-next/net/ipv6/ipv6.ko
157*2f2c4c7aSAndroid Build Coastguard Worker
158*2f2c4c7aSAndroid Build Coastguard Worker# Minimal network setup.
159*2f2c4c7aSAndroid Build Coastguard Workerip link set lo up
160*2f2c4c7aSAndroid Build Coastguard Workerip link set lo mtu 16436
161*2f2c4c7aSAndroid Build Coastguard Workerif [[ -d /sys/class/net/eth0 ]]; then
162*2f2c4c7aSAndroid Build Coastguard Worker  ip link set eth0 up
163*2f2c4c7aSAndroid Build Coastguard Workerfi
164*2f2c4c7aSAndroid Build Coastguard Worker
165*2f2c4c7aSAndroid Build Coastguard Worker# Allow people to run ping.
166*2f2c4c7aSAndroid Build Coastguard Workerecho '0 2147483647' > /proc/sys/net/ipv4/ping_group_range
167*2f2c4c7aSAndroid Build Coastguard Worker
168*2f2c4c7aSAndroid Build Coastguard Worker# Adjust tcp_rmem_default on UML as needed by Linux 6.6
169*2f2c4c7aSAndroid Build Coastguard Workerif [[ -e /proc/exitcode ]]; then
170*2f2c4c7aSAndroid Build Coastguard Worker  # UML with mem=512M defaults to '4096 131072 ~4021664'
171*2f2c4c7aSAndroid Build Coastguard Worker  read tcp_rmem_min tcp_rmem_default tcp_rmem_max < /proc/sys/net/ipv4/tcp_rmem
172*2f2c4c7aSAndroid Build Coastguard Worker  if [[ tcp_rmem_default -lt 262144 ]]; then
173*2f2c4c7aSAndroid Build Coastguard Worker    echo "${tcp_rmem_min} 262144 ${tcp_rmem_max}" > /proc/sys/net/ipv4/tcp_rmem
174*2f2c4c7aSAndroid Build Coastguard Worker  fi
175*2f2c4c7aSAndroid Build Coastguard Workerfi
176*2f2c4c7aSAndroid Build Coastguard Worker
177*2f2c4c7aSAndroid Build Coastguard Worker# Allow unprivileged use of eBPF (matches Android OS)
178*2f2c4c7aSAndroid Build Coastguard Workerif [[ "$(< /proc/sys/kernel/unprivileged_bpf_disabled)" != '0' ]]; then
179*2f2c4c7aSAndroid Build Coastguard Worker  echo 0 > /proc/sys/kernel/unprivileged_bpf_disabled
180*2f2c4c7aSAndroid Build Coastguard Workerfi
181*2f2c4c7aSAndroid Build Coastguard Worker
182*2f2c4c7aSAndroid Build Coastguard Worker# Read environment variables passed to the kernel to determine if script is
183*2f2c4c7aSAndroid Build Coastguard Worker# running on builder and to find which test to run.
184*2f2c4c7aSAndroid Build Coastguard Worker
185*2f2c4c7aSAndroid Build Coastguard Workerif [ "$net_test_mode" != "builder" ]; then
186*2f2c4c7aSAndroid Build Coastguard Worker  # Fall out to a shell once the test completes or if there's an error.
187*2f2c4c7aSAndroid Build Coastguard Worker  trap "exec /bin/bash" ERR EXIT
188*2f2c4c7aSAndroid Build Coastguard Workerfi
189*2f2c4c7aSAndroid Build Coastguard Worker
190*2f2c4c7aSAndroid Build Coastguard Workerecho -e "Running $net_test $net_test_args\n"
191*2f2c4c7aSAndroid Build Coastguard Worker$net_test $net_test_args
192*2f2c4c7aSAndroid Build Coastguard Workerrv="$?"
193*2f2c4c7aSAndroid Build Coastguard Worker
194*2f2c4c7aSAndroid Build Coastguard Worker# Write exit code of net_test to a file so that the builder can use it
195*2f2c4c7aSAndroid Build Coastguard Worker# to signal failure if any tests fail.
196*2f2c4c7aSAndroid Build Coastguard Workerecho "${rv}" > "${exitcode}"
197*2f2c4c7aSAndroid Build Coastguard Worker
198*2f2c4c7aSAndroid Build Coastguard Worker# Additionally on UML make it the exit code of UML kernel binary itself.
199*2f2c4c7aSAndroid Build Coastguard Workerif [[ -e '/proc/exitcode' ]]; then
200*2f2c4c7aSAndroid Build Coastguard Worker  echo "${rv}" > /proc/exitcode
201*2f2c4c7aSAndroid Build Coastguard Workerfi
202