Skip to content

Commit 5940466

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 5940466

File tree

4 files changed

+344
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)