1+ import os
2+ import re
3+ import logging as log
4+
5+ from avocado .utils import process
6+
7+ from virttest import virsh
8+ from virttest import utils_misc
9+ from virttest import utils_net
10+ from virttest import remote
11+ from virttest .utils_test import libvirt
12+ from virttest .libvirt_xml import vm_xml
13+ from virttest .staging import utils_memory
14+
15+ from provider .migration import base_steps
16+
17+ # Using as lower capital is not the best way to do, but this is just a
18+ # workaround to avoid changing the entire file.
19+ logging = log .getLogger ('avocado.' + __name__ )
20+
21+
22+ def run (test , params , env ):
23+ """
24+ Test UDP packet transfer from VM to remote host during migration.
25+
26+ 1. Setup VM and remote host environment
27+ 2. Install and configure netperf on remote host and VM
28+ 3. Start packet capture on remote host
29+ 4. Run UDP transfer test from VM to remote host
30+ 5. Verify packets are captured correctly
31+ 6. Clean up environment
32+ """
33+
34+ def disable_firewall (session , guest_os_type = "linux" ):
35+ """Disable firewall on the target system."""
36+ if guest_os_type == "linux" :
37+ cmd = "systemctl stop firewalld"
38+ status = session .cmd_status (cmd )
39+ if status == 0 :
40+ logging .info ("Firewall disabled successfully" )
41+ else :
42+ logging .warning ("Failed to disable firewall or already disabled" )
43+ else :
44+ # Windows firewall
45+ cmd = 'netsh firewall set opmode mode=disable'
46+ output = session .cmd_output (cmd )
47+ logging .info ("Windows firewall disabled: %s" , output )
48+
49+ def install_netperf (session , guest_os_type = "linux" ):
50+ """Install netperf on the target system."""
51+ if guest_os_type == "linux" :
52+ # Check if netperf is already installed
53+ status = session .cmd_status ("which netperf" )
54+ if status == 0 :
55+ logging .info ("netperf already installed" )
56+ return
57+
58+ # Install netperf
59+ install_cmd = ("yum install -y netperf || "
60+ "apt-get update && apt-get install -y netperf" )
61+ session .cmd (install_cmd , timeout = 300 )
62+
63+ # Verify installation
64+ status = session .cmd_status ("which netperf" )
65+ if status != 0 :
66+ test .fail ("Failed to install netperf" )
67+ else :
68+ # For Windows, we would need to transfer netperf.exe
69+ # This is simplified for the basic structure
70+ logging .info ("Windows netperf setup would be handled here" )
71+
72+ def start_netserver (session ):
73+ """Start netperf server."""
74+ # Kill any existing netserver
75+ session .cmd_status ("pkill netserver" )
76+
77+ # Start netserver
78+ output = session .cmd_output ("netserver" , timeout = 30 )
79+ if "Starting netserver" not in output :
80+ # Check if it's already running
81+ status = session .cmd_status ("pgrep netserver" )
82+ if status != 0 :
83+ test .fail ("Failed to start netserver" )
84+ logging .info ("netserver started successfully" )
85+
86+ def start_tcpdump (session , vm_ip , log_file ):
87+ """Start tcpdump to capture UDP packets."""
88+ # Install tcpdump if not available
89+ session .cmd_status ("yum install -y tcpdump || apt-get install -y tcpdump" )
90+
91+ # Start tcpdump in background
92+ tcpdump_cmd = f"tcpdump -n udp and src { vm_ip } > { log_file } 2>&1 &"
93+ session .cmd (tcpdump_cmd )
94+ logging .info ("tcpdump started, capturing packets from %s" , vm_ip )
95+
96+ def run_netperf_udp_test (vm_session , target_ip ):
97+ """Run netperf UDP test from VM to target."""
98+ netperf_cmd = f"netperf -H { target_ip } -t UDP_STREAM -- -m 1473"
99+
100+ # Run netperf test
101+ try :
102+ output = vm_session .cmd_output (netperf_cmd , timeout = 120 )
103+ logging .info ("netperf UDP test completed" )
104+ return output
105+ except Exception as e :
106+ test .fail (f"netperf UDP test failed: { e } " )
107+
108+ def verify_packet_capture (session , log_file , vm_ip , target_ip ):
109+ """Verify that UDP packets were captured."""
110+ # Wait a bit for packets to be written
111+ utils_misc .wait_for (lambda : True , 5 )
112+
113+ # Read tcpdump log
114+ try :
115+ output = session .cmd_output (f"cat { log_file } " )
116+ logging .info ("tcpdump log content: %s" , output )
117+
118+ # Look for UDP packets with expected pattern
119+ pattern = f"IP { vm_ip } \.\\ d+ > { target_ip } \.\\ d+: UDP, length 1473"
120+ if re .search (pattern , output ):
121+ logging .info ("UDP packets successfully captured" )
122+ return True
123+ else :
124+ test .fail (f"Expected UDP packets not found in capture. Pattern: { pattern } " )
125+ except Exception as e :
126+ test .fail (f"Failed to read tcpdump log: { e } " )
127+
128+ vm_name = params .get ("main_vm" )
129+ vm = env .get_vm (vm_name )
130+
131+ # Migration parameters
132+ remote_host = params .get ("remote_host" )
133+ remote_user = params .get ("remote_user" , "root" )
134+ remote_passwd = params .get ("remote_passwd" )
135+ guest_os_type = params .get ("guest_os_type" , "linux" )
136+
137+ if not remote_host :
138+ test .cancel ("remote_host parameter is required" )
139+
140+ tcpdump_log = "/tmp/udp_capture.log"
141+
142+ try :
143+ # Setup VM
144+ if not vm .is_alive ():
145+ vm .start ()
146+ vm .wait_for_login ()
147+
148+ vm_session = vm .wait_for_login ()
149+ vm_ip = vm .get_address ()
150+
151+ logging .info ("VM IP: %s" , vm_ip )
152+ logging .info ("Remote host: %s" , remote_host )
153+
154+ # Setup remote host session
155+ remote_session = remote .remote_login ("ssh" , remote_host , "22" ,
156+ remote_user , remote_passwd ,
157+ r"[\#\$]\s*$" )
158+
159+ # Disable firewalls
160+ disable_firewall (vm_session , guest_os_type )
161+ disable_firewall (remote_session , "linux" )
162+
163+ # Install netperf on VM and remote host
164+ install_netperf (vm_session , guest_os_type )
165+ install_netperf (remote_session , "linux" )
166+
167+ # Start netserver on remote host
168+ start_netserver (remote_session )
169+
170+ # Start packet capture on remote host
171+ start_tcpdump (remote_session , vm_ip , tcpdump_log )
172+
173+ # Run UDP transfer test
174+ netperf_output = run_netperf_udp_test (vm_session , remote_host )
175+
176+ # Verify packet capture
177+ verify_packet_capture (remote_session , tcpdump_log , vm_ip , remote_host )
178+
179+ logging .info ("UDP transfer test completed successfully" )
180+
181+ finally :
182+ # Cleanup
183+ try :
184+ if 'remote_session' in locals ():
185+ # Stop tcpdump
186+ remote_session .cmd_status ("pkill tcpdump" )
187+ # Stop netserver
188+ remote_session .cmd_status ("pkill netserver" )
189+ # Remove log file
190+ remote_session .cmd_status (f"rm -f { tcpdump_log } " )
191+ remote_session .close ()
192+ except Exception as e :
193+ logging .warning ("Cleanup failed: %s" , e )
194+
195+ try :
196+ if 'vm_session' in locals ():
197+ vm_session .close ()
198+ except Exception as e :
199+ logging .warning ("VM session cleanup failed: %s" , e )
0 commit comments