Skip to content

Commit 9d655e4

Browse files
committed
add case for vm transfer packet to remote host by udp
xxxx-300100:[virtual network][virtual-nic-device]The local VM can transfer length 1473-1480 packets to remote host through UDP Signed-off-by: nanli <[email protected]>
1 parent d4a34d0 commit 9d655e4

File tree

4 files changed

+354
-0
lines changed

4 files changed

+354
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
- virtual_network.qemu_test.transfer_packets_to_remote_host_through_UDP:
2+
type = transfer_packets_to_remote_host_through_UDP
3+
take_regular_screendumps = "no"
4+
create_vm_libvirt = "yes"
5+
kill_vm_libvirt = "yes"
6+
# Network configuration
7+
udp_packet_size = 1473
8+
netperf_timeout = 120
9+
netperf_version = "netperf-2.7.1"
10+
firewall_cmd = "systemctl stop firewalld"
11+
del_log_cmd = "rm -rf /home/netperf_log*"
12+
# Netperf and log commands
13+
netperf_cmd = "cd /home; netperf -H %s -t UDP_STREAM -- -m %s > %s &"
14+
data_match = "UDP STREAM TEST from"
15+
viewlog_cmd = "cat /home/%s"
16+
netperf_pkg = '${netperf_version}.tar.bz2'
17+
netperf_install_dest = "/home"
18+
netperf_install_cmd = "yum -y install automake autoconf libtool && tar jxf /home/${netperf_version}.tar.bz2 -C /home/ && cd /home/${netperf_version} && export CFLAGS='-D_GNU_SOURCE' && ./autogen.sh && ./configure && make && make install"
19+
netperf_verify_cmd = "which netperf"
20+
# Windows netperf installation path
21+
Windows:
22+
firewall_cmd = "netsh firewall set opmode mode=disable"
23+
netperf_cmd = "netperf.exe -H %s -t UDP_STREAM -- -m %s > %s &"
24+
viewlog_cmd = "type %s"
25+
netperf_check_cmd = "cd %s"
26+
netperf_pkg = 'netperf.exe'
27+
netperf_install_path = "C:\\Program Files"
28+
netperf_install_dest = "${netperf_install_path}"
29+
netperf_install_cmd = ""
30+
netperf_verify_cmd = 'dir "${netperf_install_path}"|findstr /I netperf'
31+
del_log_cmd = 'cd ${netperf_install_path} && del netperf_log*'
1.48 MB
Binary file not shown.
131 KB
Binary file not shown.
Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
1+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2+
#
3+
# Copyright Redhat
4+
#
5+
# SPDX-License-Identifier: GPL-2.0
6+
7+
# Author: Nannan Li<[email protected]>
8+
#
9+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10+
11+
12+
import os
13+
import re
14+
15+
from avocado.utils import process
16+
17+
from virttest import data_dir
18+
from virttest import utils_misc
19+
from virttest import utils_package
20+
from virttest import remote
21+
from virttest.libvirt_xml import vm_xml
22+
23+
from provider.virtual_network import network_base
24+
25+
26+
def run(test, params, env):
27+
"""
28+
Test UDP packet transfer from VM to remote host.
29+
30+
Steps:
31+
1. Setup VM and remote host environment
32+
2. Install and configure netperf on remote host and VM
33+
3. Start packet capture on remote host
34+
4. Run UDP transfer test from VM to remote host
35+
5. Verify packets are captured correctly
36+
6. Clean up environment
37+
38+
"""
39+
40+
def disable_firewall(session, params, guest_os_type="linux"):
41+
"""
42+
Disable firewall on the target system.
43+
:param session: Session object for executing commands
44+
:param params: Params object
45+
:param guest_os_type: OS type ("linux" or "windows")
46+
"""
47+
firewall_cmd = params.get("firewall_cmd")
48+
49+
if guest_os_type == "linux":
50+
status = session.cmd_status(firewall_cmd)
51+
if status != 0:
52+
test.log.debug("Failed to disable firewall or already disabled")
53+
else:
54+
try:
55+
session.cmd(firewall_cmd, timeout=30)
56+
test.log.debug("Disabled legacy Windows firewall")
57+
session.cmd('netsh advfirewall set allprofiles state off', timeout=30)
58+
test.log.debug("Disabled Windows advanced firewall")
59+
except Exception as e:
60+
test.log.debug("Could not disable Windows firewall (may not exist or already disabled): %s", e)
61+
62+
def transfer_netperf(guest_session):
63+
"""
64+
Transfer and install netperf.
65+
66+
:param guest_session: Guest session object
67+
"""
68+
# Get OS-specific configuration (parameters are OS-aware from config)
69+
netperf_source = os.path.join(data_dir.get_deps_dir("netperf"), params.get('netperf_pkg'))
70+
netperf_dest = params.get("netperf_install_dest")
71+
install_cmd = params.get("netperf_install_cmd")
72+
verify_cmd = params.get("netperf_verify_cmd")
73+
74+
test.log.debug('Transfer netperf file to guest')
75+
vm.copy_files_to(netperf_source, netperf_dest, timeout=300)
76+
test.log.debug("Successfully transferred netperf file to guest VM")
77+
78+
# Install netperf (Linux only - Windows is just copy)
79+
if install_cmd:
80+
test.log.debug('Install netperf in guest')
81+
guest_session.cmd(install_cmd, timeout=600)
82+
83+
# Verify installation
84+
test.log.debug('Verify netperf installation')
85+
status, output = guest_session.cmd_status_output(verify_cmd)
86+
if status == 0 and 'netperf' in output:
87+
test.log.debug('Netperf installation verified successfully')
88+
else:
89+
test.fail('Failed to install/transfer netperf to guest: status=%s, output=%s' % (status, output))
90+
91+
def run_netserver(host_session):
92+
"""
93+
Install and run netserver.
94+
95+
:param host_session: Host session object for executing commands
96+
"""
97+
test.log.debug('Install netserver on remote host firstly')
98+
remote_ip = params.get("remote_ip")
99+
remote_user = params.get("remote_user", "root")
100+
remote_passwd = params.get("remote_pwd")
101+
102+
test.log.debug('Scp netperf to remote host.')
103+
utils_misc.make_dirs(os.path.dirname(netperf_linux_path), host_session)
104+
remote.copy_files_to(remote_ip, 'scp', remote_user, remote_passwd,
105+
'22', netperf_linux_path, netperf_linux_path)
106+
107+
list_cmd = 'ls -ld /home/%s' % netperf_version
108+
if r'No such file or directory' not in \
109+
host_session.cmd_output(list_cmd):
110+
host_session.cmd_output('rm -rf /home/%s' % netperf_version)
111+
112+
host_session.cmd_output("yum -y install automake autoconf libtool")
113+
install_cmd = 'tar jxf %s -C /home/ && ' \
114+
'cd /home/%s && export CFLAGS="-D_GNU_SOURCE" && ./autogen.sh && ' \
115+
'./configure && make && make install' % (netperf_linux_path, netperf_version)
116+
output = host_session.cmd_output(cmd=install_cmd)
117+
if r'No such file or directory' in host_session.cmd_output(
118+
list_cmd):
119+
test.fail('Fail to install netperf on host')
120+
o = host_session.cmd_output('netstat -anp |grep 12865')
121+
if o:
122+
used_pid = o.split('LISTEN')[1].strip().split('/')[0]
123+
host_session.cmd_output('kill -9 %s' % used_pid)
124+
output = host_session.cmd_output('netserver')
125+
else:
126+
output = host_session.cmd_output('netserver')
127+
if re.search(r'libsctp', output):
128+
host_session.cmd_output('yum install -y libsctp*')
129+
output = host_session.cmd_output('netserver')
130+
if 'netserver' not in host_session.cmd_output(
131+
'pgrep -xl netserver') or ('Starting netserver' not in output):
132+
test.fail("Fail to start netserver")
133+
134+
def run_netperf(guest_session, dst_host_ip, guest_os_type, packet_size, netperf_install_path='C:\\Program Files'):
135+
"""
136+
Run netperf UDP test.
137+
138+
:param guest_session: Guest session object
139+
:param dst_host_ip: Destination host IP address
140+
:param guest_os_type: Guest OS type ("linux" or "windows")
141+
:param packet_size: UDP packet size for netperf test
142+
:param netperf_install_path: Installation path for Windows netperf
143+
:return: netperf log filename
144+
"""
145+
netperf_cmd = params.get("netperf_cmd")
146+
147+
netperf_log = 'netperf_log_%s' % utils_misc.generate_random_string(6)
148+
if guest_os_type == 'linux':
149+
guest_session.cmd(netperf_cmd % (dst_host_ip, packet_size, netperf_log))
150+
else:
151+
guest_session.cmd('cd "%s" && %s' % (netperf_install_path, netperf_cmd % (dst_host_ip, packet_size, netperf_log)))
152+
return netperf_log
153+
154+
def check_netperf_log(guest_session, netperf_log, guest_os_type, packet_size, netperf_install_path='C:\\Program Files'):
155+
"""
156+
Check netperf log results.
157+
:param guest_session: Guest session object
158+
:param netperf_log: Netperf log filename to check
159+
:param guest_os_type: Guest OS type ("linux" or "windows")
160+
:param packet_size: Expected UDP packet size in log
161+
:param netperf_install_path: Installation path for Windows netperf
162+
:return: netperf log filename
163+
"""
164+
if guest_os_type == 'linux':
165+
if utils_misc.wait_for(
166+
lambda: 'netperf' not in guest_session.cmd_output('pgrep -xl netperf'), 120, step=3.0):
167+
test.log.debug('Finish to execute netperf in guest')
168+
else:
169+
test.fail('Timeout to execute netperf in guest under 120s')
170+
else:
171+
cmd = 'tasklist /FI "imagename eq netperf.exe"'
172+
guest_session.cmd('cd %s' % netperf_install_path)
173+
if utils_misc.wait_for(lambda: not re.search(
174+
r'netperf.exe', guest_session.cmd_output(cmd)), 120, step=3.0):
175+
test.log.debug('Finish to execute netperf in guest')
176+
else:
177+
test.fail('Timeout to execute netperf in guest under 120s')
178+
179+
# Check netperf log content
180+
data_match = params.get("data_match")
181+
viewlog_cmd = params.get("viewlog_cmd") % netperf_log
182+
183+
output = guest_session.cmd_output(viewlog_cmd)
184+
if data_match and str(packet_size) in output:
185+
test.log.debug('The log of netperf checking is PASS')
186+
else:
187+
test.fail("The log of netperf isn't right:%s" % output)
188+
return netperf_log
189+
190+
def verify_packet_capture(remote_session, vm_ip, remote_ip, packet_size, tcpdump_log_file):
191+
"""
192+
Verify UDP packets are captured in tcpdump log file.
193+
194+
:param remote_session: Remote session object
195+
:param vm_ip: VM IP address
196+
:param remote_ip: Remote host IP address
197+
:param packet_size: Expected UDP packet size
198+
:param tcpdump_log_file: Path to tcpdump log file
199+
:raises: test.fail if packets not found after debugging
200+
"""
201+
# Search for expected UDP packets in tcpdump log
202+
expected_pattern = r'IP %s\.[0-9]+ > %s\.[0-9]+: UDP, length %s' % (vm_ip, remote_ip, packet_size)
203+
grep_cmd = 'grep -E "%s" %s | head -5' % (expected_pattern, tcpdump_log_file)
204+
205+
test.log.debug("Searching for pattern: %s", expected_pattern)
206+
test.log.debug("vm_ip: %s, remote_ip: %s, packet_size: %s", vm_ip, remote_ip, packet_size)
207+
208+
try:
209+
grep_output = remote_session.cmd_output(grep_cmd).strip()
210+
if grep_output:
211+
test.log.debug("Found matching packets:")
212+
for line in grep_output.split('\n')[:3]: # Show first 3 matches
213+
test.log.debug(" %s", line.strip())
214+
test.log.debug('Packet capture verification successful')
215+
return
216+
217+
except Exception as e:
218+
test.log.debug("Grep search failed: %s", str(e))
219+
220+
# No packets found - show debug info and fail
221+
test.log.debug("No matching packets found. Showing tcpdump log sample:")
222+
try:
223+
sample_output = remote_session.cmd_output('head -10 %s' % tcpdump_log_file)
224+
test.log.debug("Sample log content:\n%s", sample_output)
225+
except Exception as e:
226+
test.log.debug("Could not read log file: %s", str(e))
227+
228+
test.fail('No UDP packets captured matching expected pattern')
229+
230+
vm_name = params.get("main_vm")
231+
vm = env.get_vm(vm_name)
232+
netperf_version = params.get("netperf_version")
233+
234+
# Handle variable substitution in paths
235+
remote_ip = params.get("remote_ip")
236+
remote_user = params.get("remote_user", "root")
237+
remote_passwd = params.get("remote_pwd")
238+
local_passwd = params.get("local_pwd")
239+
guest_passwd = params.get("password")
240+
guest_os_type = params.get("os_type", "linux")
241+
packet_size = params.get("udp_packet_size")
242+
del_log_cmd = params.get("del_log_cmd")
243+
netperf_install_path = params.get("netperf_install_path")
244+
tcpdump_log_file = '/tmp/UDP_tcpdump.log'
245+
246+
netperf_pkg = params.get('netperf_pkg')
247+
netperf_linux_path = os.path.join(data_dir.get_deps_dir("netperf"), netperf_pkg)
248+
netperf_windows_path = os.path.join(data_dir.get_deps_dir("netperf"), netperf_pkg)
249+
250+
remote_session = remote.remote_login(
251+
"ssh", remote_ip, "22", remote_user, remote_passwd, r"[\#\$]\s*$")
252+
253+
def run_test():
254+
"""
255+
Execute the main test steps for UDP packet transfer verification.
256+
257+
This function performs the complete test workflow:
258+
1. Boot up the guest VM and get its IP address
259+
2. Start netperf server on the remote host
260+
3. Start packet capture (tcpdump) on remote host
261+
4. Transfer and install netperf on the guest
262+
5. Run netperf UDP test from guest to remote host
263+
6. Verify packets were captured correctly on remote host
264+
"""
265+
test.log.info("TEST_STEP1: Boot up a guest on src host")
266+
if not vm.is_alive():
267+
vm.start()
268+
vm_session = vm.wait_for_login()
269+
test.log.debug("Guest xml:\n%s", vm_xml.VMXML.new_from_dumpxml(vm_name))
270+
271+
vm_ip = network_base.get_vm_ip(vm_session, vm_xml.VMXML.get_first_mac_by_name(vm_name))
272+
test.log.debug("VM IP: %s and Remote host: %s", vm_ip, remote_ip)
273+
274+
test.log.info("TEST_STEP2: Start netperf server on remote host")
275+
run_netserver(remote_session)
276+
277+
test.log.debug("TEST_STEP3: Capture the packet from guest")
278+
if not utils_package.package_install('tcpdump', session=remote_session):
279+
test.fail("tcpdump package install failed")
280+
remote_session.sendline('tcpdump -n udp and src %s > %s 2>&1 &'
281+
% (vm_ip, tcpdump_log_file))
282+
283+
test.log.info("TEST_STEP4: Transfer and Run netperf in the guest")
284+
test.log.debug("ssssssssssssssssssssssssss:%s", guest_passwd)
285+
if guest_os_type == 'linux':
286+
process.run("yum -y install sshpass", ignore_status=True)
287+
288+
# Generate SSH key if it doesn't exist
289+
process.run('mkdir -p /root/.ssh', ignore_status=True)
290+
if not os.path.exists('/root/.ssh/id_rsa'):
291+
process.run('ssh-keygen -t rsa -b 2048 -f /root/.ssh/id_rsa -N ""', ignore_status=True)
292+
293+
process.run('sshpass -p %s ssh-copy-id -o "StrictHostKeyChecking no" -i '
294+
'/root/.ssh/id_rsa.pub root@%s' % (guest_passwd, vm_ip), ignore_status=True)
295+
296+
disable_firewall(vm_session, params, guest_os_type)
297+
disable_firewall(remote_session, params, "linux")
298+
299+
transfer_netperf(vm_session)
300+
301+
netperf_log = run_netperf(vm_session, remote_ip, guest_os_type, packet_size, netperf_install_path)
302+
check_netperf_log(vm_session, netperf_log, guest_os_type, packet_size, netperf_install_path)
303+
304+
test.log.info("TEST_STEP5: Verify packet capture")
305+
verify_packet_capture(remote_session, vm_ip, remote_ip, packet_size, tcpdump_log_file)
306+
vm_session.close()
307+
308+
def teardown():
309+
310+
test.log.info("TEST_TEARDOWN: Clean up env.")
311+
vm_session = vm.wait_for_login()
312+
vm_session.cmd_status(del_log_cmd)
313+
vm_session.close()
314+
315+
remote_session.cmd_status("pkill tcpdump; pkill netserver")
316+
remote_session.cmd_status(f"rm -f {tcpdump_log_file}")
317+
remote_session.close()
318+
319+
try:
320+
run_test()
321+
322+
finally:
323+
teardown()

0 commit comments

Comments
 (0)