1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# xdping tests
5#   Here we setup and teardown configuration required to run
6#   xdping, exercising its options.
7#
8#   Setup is similar to test_tunnel tests but without the tunnel.
9#
10# Topology:
11# ---------
12#     root namespace   |     tc_ns0 namespace
13#                      |
14#      ----------      |     ----------
15#      |  veth1  | --------- |  veth0  |
16#      ----------    peer    ----------
17#
18# Device Configuration
19# --------------------
20# Root namespace with BPF
21# Device names and addresses:
22#	veth1 IP: 10.1.1.200
23#	xdp added to veth1, xdpings originate from here.
24#
25# Namespace tc_ns0 with BPF
26# Device names and addresses:
27#       veth0 IPv4: 10.1.1.100
28#	For some tests xdping run in server mode here.
29#
30
31readonly TARGET_IP="10.1.1.100"
32readonly TARGET_NS="xdp_ns0"
33
34readonly LOCAL_IP="10.1.1.200"
35
36setup()
37{
38	ip netns add $TARGET_NS
39	ip link add veth0 type veth peer name veth1
40	ip link set veth0 netns $TARGET_NS
41	ip netns exec $TARGET_NS ip addr add ${TARGET_IP}/24 dev veth0
42	ip addr add ${LOCAL_IP}/24 dev veth1
43	ip netns exec $TARGET_NS ip link set veth0 up
44	ip link set veth1 up
45}
46
47cleanup()
48{
49	set +e
50	ip netns delete $TARGET_NS 2>/dev/null
51	ip link del veth1 2>/dev/null
52	if [[ $server_pid -ne 0 ]]; then
53		kill -TERM $server_pid
54	fi
55}
56
57test()
58{
59	client_args="$1"
60	server_args="$2"
61
62	echo "Test client args '$client_args'; server args '$server_args'"
63
64	server_pid=0
65	if [[ -n "$server_args" ]]; then
66		ip netns exec $TARGET_NS ./xdping $server_args &
67		server_pid=$!
68		sleep 10
69	fi
70	./xdping $client_args $TARGET_IP
71
72	if [[ $server_pid -ne 0 ]]; then
73		kill -TERM $server_pid
74		server_pid=0
75	fi
76
77	echo "Test client args '$client_args'; server args '$server_args': PASS"
78}
79
80set -e
81
82server_pid=0
83
84trap cleanup EXIT
85
86setup
87
88for server_args in "" "-I veth0 -s -S" ; do
89	# client in skb mode
90	client_args="-I veth1 -S"
91	test "$client_args" "$server_args"
92
93	# client with count of 10 RTT measurements.
94	client_args="-I veth1 -S -c 10"
95	test "$client_args" "$server_args"
96done
97
98echo "OK. All tests passed"
99exit 0
100