Skip to content

Commit ca61ba8

Browse files
parth-opensrcgvisor-bot
authored andcommitted
packetdrill: Fix packetdrill test script
packetdrill_test.sh: - Find the interface tied with the TEST network instead of assuming `eth2`. - Set packetdrill ip_version to ipv4 as the test assumes ipv4 while creating docker networks. - Linter related fixes. packetdrill/Dockerfile: - Update to Ubuntu24.04 - Install `iproute`, easier to parse interface name with iproute than with ifconfig. PiperOrigin-RevId: 828171476
1 parent 6db7459 commit ca61ba8

File tree

2 files changed

+54
-25
lines changed

2 files changed

+54
-25
lines changed

images/packetdrill/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
FROM ubuntu:jammy
1+
FROM ubuntu:24.04
22
RUN apt-get update && apt-get install -y net-tools git iptables iputils-ping \
3-
netcat tcpdump jq tar bison flex make
3+
netcat-traditional tcpdump jq tar bison flex make iproute2
44
# Pick up updated git.
55
RUN hash -r
66
RUN git clone --depth 1 https://github.com/google/packetdrill.git

test/packetdrill/packetdrill_test.sh

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ if [[ "${DUT_PLATFORM-}" == "netstack" ]]; then
7878
echo "FAIL: Missing --runtime argument: ${RUNTIME-}"
7979
exit 2
8080
fi
81-
declare -r RUNTIME_ARG="--runtime ${RUNTIME}"
81+
# Using array to pass multiple arguments; https://www.shellcheck.net/wiki/SC2086.
82+
declare -a RUNTIME_ARG=("--runtime" "${RUNTIME}")
8283
elif [[ "${DUT_PLATFORM-}" == "linux" ]]; then
83-
declare -r RUNTIME_ARG=""
84+
# Declare an empty array.
85+
declare -a RUNTIME_ARG
8486
else
8587
echo "FAIL: Bad or missing --dut_platform argument: ${DUT_PLATFORM-}"
8688
exit 2
@@ -101,13 +103,11 @@ function new_net_prefix() {
101103
# Variables specific to the test runner start with TEST_RUNNER_.
102104
declare -r PACKETDRILL="/packetdrill/gtests/net/packetdrill/packetdrill"
103105
# Use random numbers so that test networks don't collide.
104-
declare CTRL_NET="ctrl_net-$(shuf -i 0-99999999 -n 1)"
105-
declare CTRL_NET_PREFIX=$(new_net_prefix)
106-
declare TEST_NET="test_net-$(shuf -i 0-99999999 -n 1)"
107-
declare TEST_NET_PREFIX=$(new_net_prefix)
106+
CTRL_NET="ctrl_net-$(shuf -i 0-99999999 -n 1)"
107+
CTRL_NET_PREFIX=$(new_net_prefix)
108+
TEST_NET="test_net-$(shuf -i 0-99999999 -n 1)"
109+
TEST_NET_PREFIX=$(new_net_prefix)
108110
declare -r tolerance_usecs=100000
109-
# On both DUT and test runner, testing packets are on the eth2 interface.
110-
declare -r TEST_DEVICE="eth2"
111111
# Number of bits in the *_NET_PREFIX variables.
112112
declare -r NET_MASK="24"
113113
# Last bits of the DUT's IP address.
@@ -144,6 +144,16 @@ function finish {
144144
}
145145
trap finish EXIT
146146

147+
# Finds the interface name from the IP address.
148+
function get_container_if_from_ip() {
149+
local container="$1"
150+
local ip="$2"
151+
if [[ -z "$container" || -z "$ip" ]]; then
152+
return 1
153+
fi
154+
docker exec "$container" ip -o addr show | grep "$ip"/ | awk '{print $2; exit}'
155+
}
156+
147157
# Subnet for control packets between test runner and DUT.
148158
while ! docker network create \
149159
"--subnet=${CTRL_NET_PREFIX}.0/${NET_MASK}" "${CTRL_NET}"; do
@@ -161,39 +171,56 @@ while ! docker network create \
161171
done
162172

163173
# Create the DUT container and connect to network.
164-
DUT=$(docker create ${RUNTIME_ARG} --privileged --rm \
174+
DUT_TEST_NET_IP="${TEST_NET_PREFIX}${DUT_NET_SUFFIX}"
175+
DUT=$(docker create "${RUNTIME_ARG[@]}" --privileged --rm \
165176
--stop-timeout ${TIMEOUT} -it ${IMAGE_TAG})
166177
docker network connect "${CTRL_NET}" \
167178
--ip "${CTRL_NET_PREFIX}${DUT_NET_SUFFIX}" "${DUT}" \
168-
|| (docker kill ${DUT}; docker rm ${DUT}; false)
179+
|| (docker kill "${DUT}"; docker rm "${DUT}"; false)
169180
docker network connect "${TEST_NET}" \
170-
--ip "${TEST_NET_PREFIX}${DUT_NET_SUFFIX}" "${DUT}" \
171-
|| (docker kill ${DUT}; docker rm ${DUT}; false)
181+
--ip "${DUT_TEST_NET_IP}" "${DUT}" \
182+
|| (docker kill "${DUT}"; docker rm "${DUT}"; false)
172183
docker start "${DUT}"
173184

185+
DUT_IF=$(get_container_if_from_ip "${DUT}" "$DUT_TEST_NET_IP")
186+
if [[ -z "$DUT_IF" ]]; then
187+
echo "Error: Could not find interface for DUT_TEST_NET_IP: ${DUT_TEST_NET_IP}. Exiting."
188+
exit 1
189+
fi
190+
174191
# Create the test runner container and connect to network.
192+
TEST_RUNNER_TEST_NET_IP="${TEST_NET_PREFIX}${TEST_RUNNER_NET_SUFFIX}"
175193
TEST_RUNNER=$(docker create --privileged --rm \
176-
--stop-timeout ${TIMEOUT} -it ${IMAGE_TAG})
194+
--stop-timeout "${TIMEOUT}" -it "${IMAGE_TAG}")
177195
docker network connect "${CTRL_NET}" \
178196
--ip "${CTRL_NET_PREFIX}${TEST_RUNNER_NET_SUFFIX}" "${TEST_RUNNER}" \
179-
|| (docker kill ${TEST_RUNNER}; docker rm ${REST_RUNNER}; false)
197+
|| (docker kill "${TEST_RUNNER}"; docker rm "${TEST_RUNNER}"; false)
180198
docker network connect "${TEST_NET}" \
181-
--ip "${TEST_NET_PREFIX}${TEST_RUNNER_NET_SUFFIX}" "${TEST_RUNNER}" \
182-
|| (docker kill ${TEST_RUNNER}; docker rm ${REST_RUNNER}; false)
199+
--ip "${TEST_RUNNER_TEST_NET_IP}" "${TEST_RUNNER}" \
200+
|| (docker kill "${TEST_RUNNER}"; docker rm "${TEST_RUNNER}"; false)
183201
docker start "${TEST_RUNNER}"
184202

203+
TEST_RUNNER_IF=$(get_container_if_from_ip "${TEST_RUNNER}" \
204+
"$TEST_RUNNER_TEST_NET_IP")
205+
if [[ -z "$TEST_RUNNER_IF" ]]; then
206+
echo "Error: Could not find interface for TEST_RUNNER_TEST_NET_IP: ${TEST_RUNNER_TEST_NET_IP}. Exiting."
207+
exit 1
208+
fi
209+
185210
# Run tcpdump in the test runner unbuffered, without dns resolution, just on the
186211
# interface with the test packets.
187-
docker exec -t ${TEST_RUNNER} tcpdump -U -n -i "${TEST_DEVICE}" &
212+
docker exec -t "${TEST_RUNNER}" tcpdump -U -n -i "${TEST_RUNNER_IF}" &
188213

189214
# Start a packetdrill server on the test_runner. The packetdrill server sends
190215
# packets and asserts that they are received.
191216
docker exec -d "${TEST_RUNNER}" \
192-
${PACKETDRILL} --wire_server --wire_server_dev="${TEST_DEVICE}" \
217+
${PACKETDRILL} \
218+
--ip_version=ipv4 \
219+
--wire_server --wire_server_dev="${TEST_RUNNER_IF}" \
193220
--wire_server_ip="${CTRL_NET_PREFIX}${TEST_RUNNER_NET_SUFFIX}" \
194221
--wire_server_port="${CTRL_PORT}" \
195-
--local_ip="${TEST_NET_PREFIX}${TEST_RUNNER_NET_SUFFIX}" \
196-
--remote_ip="${TEST_NET_PREFIX}${DUT_NET_SUFFIX}"
222+
--local_ip="${TEST_RUNNER_TEST_NET_IP}" \
223+
--remote_ip="${DUT_TEST_NET_IP}"
197224

198225
# Because the Linux kernel receives the SYN-ACK but didn't send the SYN it will
199226
# issue a RST. To prevent this IPtables can be used to filter those out.
@@ -220,11 +247,13 @@ done
220247
# Start a packetdrill client on the DUT. The packetdrill client runs POSIX
221248
# socket commands and also sends instructions to the server.
222249
docker exec -t "${DUT}" \
223-
${PACKETDRILL} --wire_client --wire_client_dev="${TEST_DEVICE}" \
250+
${PACKETDRILL} \
251+
--ip_version=ipv4 \
252+
--wire_client --wire_client_dev="${DUT_IF}" \
224253
--wire_server_ip="${CTRL_NET_PREFIX}${TEST_RUNNER_NET_SUFFIX}" \
225254
--wire_server_port="${CTRL_PORT}" \
226-
--local_ip="${TEST_NET_PREFIX}${DUT_NET_SUFFIX}" \
227-
--remote_ip="${TEST_NET_PREFIX}${TEST_RUNNER_NET_SUFFIX}" \
255+
--local_ip="${DUT_TEST_NET_IP}" \
256+
--remote_ip="${TEST_RUNNER_TEST_NET_IP}" \
228257
--init_scripts=/packetdrill_setup.sh \
229258
--tolerance_usecs="${tolerance_usecs}" "${dut_scripts[@]}"
230259

0 commit comments

Comments
 (0)